linux监控php脚本执行,LINUX学习:Linux基础之-利用shell脚本实现自动监控系统服务...

本文介绍如何使用Linux shell脚本来监控Nginx和NFS服务的状态,当服务异常时,通过Python脚本发送邮件通知。主要内容包括:1) Python邮件发送脚本的编写和错误处理;2) shell脚本监控服务状态并发送通知;3) 脚本部署步骤。通过这些脚本,可以在多台主机间实现服务状态的自动化监控和报警。
摘要由CSDN通过智能技术生成

《LINUX学习:Linux基础之-利用shell脚本实现自动监控系统服务》要点:

本文介绍了LINUX学习: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)

d146922de48edb9356bc7992187933c2.gif

#!/usr/bin/python#-*- coding: UTF-8 -*-

importsysimportsmtplibimportemail.mime.multipartimportemail.mime.text

server= 'smtp.163.com'port= '25'

defsendmail(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= '123456789'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)

View Code

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监控剧本

d146922de48edb9356bc7992187933c2.gif

#! /bin/bash

#nginx及nfs服务监控脚本,如果非常,将发送邮件通知functionmonitor_nfc() {

systemctl status nginx

nginx=$?systemctl status nfs

nfs=$?

clear

if [ $nginx -eq 0 ] && [ $nfs -eq 0]thenmsg="TIME:$(date +%F_%T)

HOSTNAME:$(hostname)

IPADDR:$(ifconfig |awk 'NR==2{print $2}')

MSG:nginx.service and nfs.service is both running" echomsg

#/usr/bin/mail $msg #服务运行正常,不发送邮件通知elif [ $nginx -ne 0 ] && [ $nfs -eq 0]thenmsg="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 $msgelif [ $nginx -ne 0 ] && [ $nfs -ne 0]thenmsg="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 $msgelif [ $nginx -eq 0 ] && [ $nfs -ne 0]thenmsg="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 $msgfi}

monitor_nfc&>> /tmp/monitor.log

View Code

3. nfc-install监控部署脚本

d146922de48edb9356bc7992187933c2.gif

#! /bin/bash

#首先执行主机的nfc.sh服务监控脚本/root/nfc.sh#然后将主机的服务监控脚本nfc.sh和发送邮件文件上传至web机器for i in {134,135,136}do

scp /root/nfc.sh 192.168.47.$i:/share/#将主机的服务监控脚本nfc.sh上传至web机器scp /usr/bin/mail 192.168.47.$i:/usr/bin/#将发送邮件文件上传至web机器ssh root@192.168.47.$i chmod +x /share/nfc.sh#增加nfc脚本文件的执行权限ssh root@192.168.47.$i chmod +x /usr/bin/mail #增加发送邮件文件的执行权限ssh root@192.168.47.$i /share/nfc.sh#执行nfc脚本监控功效done

ssh 192.168.47.133 #最终回到主机终端

View Code

详见图片

2dc06136deba3bb1b199e7932bf51fda.png

成果:

主机

e8137c3f9ce045966c859dbed96ff7ca.png

子机1

3184285bbdf901de2f43c8967a1ead82.png

本文永远更新链接地址:

维易PHP培训学院每天发布《LINUX学习:Linux基础之-利用shell脚本实现自动监控系统服务》等实战技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培养人才。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值