Linux基础之-利用shell脚本实现自动监控系统服务
目的:监控集群内nginx及nfs服务运行是否正常,如任一服务异常,则发送邮件通知用户
条件: 1. 主机及子机IP地址,hostname已确定;
2. 主机与子机能够免密通讯,即基于密匙通讯(相关命令:ssh-keygen;ssh-copy-id -i web1);
需要的文件:
1. python邮件发送工具;
2. nfc.sh监控脚本,监控nginx及nfs服务状态,并调用mail发送工具通知用户;
3. nfc-install.sh监控部署脚本,运行在主机,为子机配置文件,执行命令;
详细代码:
1. 邮件发送工具
将以下代码创建到“/usr/bin/mail”文件内,并赋予执行权限(chmod +x /usr/bin/mail)
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import sys
import smtplib
import email.mime.multipart
import email.mime.text server = 'smtp.163.com'
port = '' def sendmail(server,port,user,pwd,msg):
smtp = smtplib.SMTP()
smtp.connect(server,port)
smtp.login(user, pwd)
smtp.sendmail(msg['from'], msg['to'], msg.as_string())
smtp.quit()
print('邮件发送成功email has send out !') if __name__ == '__main__':
msg = email.mime.multipart.MIMEMultipart()
msg['Subject'] = 'check your service of nginx and nfs'
msg['From'] = 'python4_mail@163.com'
msg['To'] = 'python4_recvmail@163.com'
user = 'python4_mail'
pwd = ''
content='%s\n%s' %('\n'.join(sys.argv[1:4]),' '.join(sys.argv[4:])) #格式处理,专门针对我们的邮件格式 txt = email.mime.text.MIMEText(content, _charset='utf-8')
msg.attach(txt) sendmail(server,port,user,pwd,msg)
python通过SMTP发送邮件失败:
错误1:smtplib.SMTPAuthenticationError: (550, b‘User has no permission‘)
我们使用python发送邮件时相当于自定义客户端根据用户名和密码登录,然后使用SMTP服务发送邮件,新注册的163邮箱是默认不开启客户端授权的(对指定的邮箱大师客户端默认开启),因此登录总是被拒绝,解决办法(以163邮箱为例):进入163邮箱-设置-客户端授权密码-开启(授权码是用于登录第三方邮件客户端的专用密码)
错误2:smtplib.SMTPAuthenticationError: (535, b‘Error: authentication failed‘)
以163邮箱为例,在开启POP3/SMTP服务,并开启客户端授权密码时会设置授权码,将这个授权码代替smtplib.SMTP().login(user,password)方法中的password即可。
2. nfc.sh监控脚本
#! /bin/bash #nginx及nfs服务监控脚本,如果异常,将发送邮件通知
function monitor_nfc() {
systemctl status nginx
nginx=$?
systemctl status nfs
nfs=$?
clear
if [ $nginx -eq ] && [ $nfs -eq ]
then
msg="TIME:$(date +%F_%T)
HOSTNAME:$(hostname)
IPADDR:$(ifconfig |awk 'NR==2{print $2}')
MSG:nginx.service and nfs.service is both running"
echo msg
# /usr/bin/mail $msg #服务运行正常,不发送邮件通知
elif [ $nginx -ne ] && [ $nfs -eq ]
then
msg="TIME:$(date +%F_%T)
HOSTNAME:$(hostname)
IPADDR:$(ifconfig |awk 'NR==2{print $2}')
MSG:nginx.service is dead,nfs.service is running"
echo $msg
/usr/bin/mail $msg
elif [ $nginx -ne ] && [ $nfs -ne ]
then
msg="TIME:$(date +%F_%T)
HOSTNAME:$(hostname)
IPADDR:$(ifconfig |awk 'NR==2{print $2}')
MSG:nginx.service and nfs.service is both dead"
echo $msg
/usr/bin/mail $msg
elif [ $nginx -eq ] && [ $nfs -ne ]
then
msg="TIME:$(date +%F_%T)
HOSTNAME:$(hostname)
IPADDR:$(ifconfig |awk 'NR==2{print $2}')
MSG:nginx.service is running,nfs.service is dead"
echo $msg
/usr/bin/mail $msg fi
} monitor_nfc &>> /tmp/monitor.log
3. nfc-install监控部署脚本
#! /bin/bash #首先执行主机的nfc.sh服务监控脚本
/root/nfc.sh #然后将主机的服务监控脚本nfc.sh和发送邮件文件上传至web机器
for i in {,,}
do
scp /root/nfc.sh 192.168..$i:/share/ #将主机的服务监控脚本nfc.sh上传至web机器
scp /usr/bin/mail 192.168..$i:/usr/bin/ #将发送邮件文件上传至web机器
ssh root@192.168..$i chmod +x /share/nfc.sh #增加nfc脚本文件的执行权限
ssh root@192.168..$i chmod +x /usr/bin/mail #增加发送邮件文件的执行权限
ssh root@192.168..$i /share/nfc.sh #执行nfc脚本监控功能
done ssh 192.168.47.133 #最终回到主机终端
详见图片

结果:
主机

子机1

参考资料
1. http://www.cnblogs.com/linhaifeng/p/6602149.html
Linux基础之-利用shell脚本实现自动监控系统服务的更多相关文章
- 【shell脚本】自动监控tomcat服务===autoCheck.sh
自动监控tomcat服务,当tommcat服务挂掉时自动重启 一.脚本内容 [root@localhost ]# cat /root/autoCheck.sh #!/bin/bash startTom ...
- Shell 脚本来自动监控 Linux 系统的内存
# vim /scripts/swap-warning.sh #!/bin/bash #提取本服务器的IP地址信息 IP=`ifconfig eth0 | grep "inet addr&q ...
- linux的基本操作(shell 脚本的基础知识)
shell 脚本的基础知识 日常的linux系统管理工作中必不可少的就是shell脚本,如果不会写shell脚本,那么你就不算一个合格的管理员.目前很多单位在招聘linux系统管理员时,shell脚本 ...
- 《Linux命令行与shell脚本编程大全 第3版》Shell脚本编程基础---57
以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:
- 利用shell脚本实现计划任务功能 V1.2
2013.05.10 mytask 1.2 主程序休眠时间分成若干小的时间片断分段休眠,避免长时间的休眠不能及时响应系统信号. 2013.05.07 mytask 1.1 昨天发布了mytask1. ...
- Centos7下crontab+shell脚本定期自动删除文件
问题描述: 最近有个需求,就是rsync每次同步的数据量很多,但是需要保留的数据库bak文件 保留7天就够了,所以需要自动清理文件夹内的bak文件 解决方案: 利用shell脚本来定期删除文件夹内的任 ...
- 自学Linux命令行与Shell脚本之路
自学Linux命令行与Shell脚本之路[第一回]:初识Linux 1.1 自学Linux Shell1.1-Linux初识 1.2 自学Linux Shell1.2-Linux目录结构 1.3 ...
- 《Linux命令行与shell脚本编程大全》第十一章 构建基本脚本
11.1使用多个命令 $date;who // 命令列表,加入分号就可以,这样会依次执行.参见5.2.1节 注意区分$(date;who),这个是进程列表,会生成一个子shell来执行 Shel ...
- Linux命令行与Shell脚本编程大全
快来参加<Linux命令行与Shell脚本编程大全>学习吧,提升技能,展示自我. 点击链接即可进入学习:https://s.imooc.com/WTmCO6H 课程亮点适合零基础读者,从零 ...
随机推荐
- Linux新手随手笔记1.9-使用Apache搭建网站
搭建网站 网站服务:让用户能够通过浏览器访问到的服务器上的文档资源. 对比Windows 和Linux部署服务方法对比 Windows:IIS Linux :Apache ,nginx Ap ...
- c# Config配置文件读写
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.C ...
- 最新 php oracle 数据库连接 数据库分页
php 5连接 oracle 10g php oracle 分页 <?php//buyicode studio 20/12/2009//总记录数$sql = "select ROWNU ...
- LOJ6500. 「雅礼集训 2018 Day2」操作(哈希+差分)
题目链接 https://loj.ac/problem/6500 题解 区间取反 \(01\) 串的经典套路是差分.我们令 \(b_i = a_i\ {\rm xor}\ a_{i - 1}\)(\( ...
- stark - 1 ⇲一些理念
⒈.django项目启动时,自定义执行某个py文件. 在任意的app的apps.py中的Config类中定义ready方法,并调用autodiscover_modules from django.ap ...
- HDU - 5306 剪枝的线段树
题意:给定\(a[1...n]\),\(m\)次操作,0表示使\([L,R]\)中的值\(a[i]=min(a[i],x)\),其余的1是查最值2是查区间和 本题是吉利爷的2016论文题,1 2套路不 ...
- 1095. Maximum Swap —— Weekly Challenge
题目限定输入是[0, 10^8],因而不用考虑负数或者越界情况,算是减小了难度. public class Solution { /** * @param num: a non-negative in ...
- selenium 多窗口(windows)及ITargetLocator使用总结
1. selenium能实现窗口切换的原理 2. 常见命令 2.1 WindowHandle 2.2 WindowHandles 2.3 SwitchTo 3. 使用JavaScript新建窗口 4. ...
- Android中直接打开高德APP进行导航
1.判断是否安装有高德APP //高德APPprivate PackageManager mPackageManager;private static List<String> mPack ...
- XAMPP中Apache和Mysql启动失败问题总结
一.Apache启动失败 xampp启动时显示的错误为: 9:52:41 [Apache] Attempting to start Apache app... 9:52:41 [Apache] ...