linux摄像头宕机,监控Linux服务器是否宕机并发送邮件的解决方案

ec1e884aa5ea0517173c86f225ae4f03.png

1.ubuntu下安装和配置

sudo apt-get install mailutils

以下保持默认即可

Postfix Configuration

e6aed3b32229442ec7c89b53f658d75e.png

723d1978281d3664d478ffb15b72c1a0.png

a31dcb6c8cccf9c389364595b6126491.png

2.centos下安装和配置

1.安装

yum -y install sendmail

yum -y install mailx

2.配置

mail.rc新增以下内容

set from=your-email@example.com

set smtp-auth-user=your-email@example.com

set smtp=smtp.example.com

set smtp-auth-password=your-password

set stmp-auth=login

该文件主要配置邮件服务器,部署mail文件确保邮箱的授权码开启,只有开启授权码,后面cent中mail才能调用各大邮箱提供商的账号密码进行邮件发送,用邮箱登录密码是发送不成功的!

c18872aa9cc6ce4142891af06efa3c6e.png

说明:

from: 对方收到邮件时显示的发件人

smtp: 指定第三方发送邮件的smtp服务器地址

smtp-auth-user: 第三方发邮件的用户名

smtp-auth-password: 用户名对应密码(邮箱授权码)

smtp-auth: SMTP的认证方式。默认是LOGIN,也可改为CRAM-MD5或PLAIN方式

3.编写检测脚本ping.sh

#!/bin/bash

Date=`date -d "today" +"%Y-%m-%dT%H-%M-%S"`

echo "根据当前时间创建日志文件"

mkdir -p /log/Ping/

touch /log/Ping/${Date}.log

servers="192.168.4.9 192.168.4.10 \

192.168.4.11 192.168.4.12 192.168.4.13 192.168.4.14 \

192.168.4.21 192.168.4.22 192.168.4.23 192.168.4.24 \

192.168.4.31 192.168.4.32 192.168.4.33 192.168.4.34 \

192.168.4.41 192.168.4.42 192.168.4.43 192.168.4.44"

for server in ${servers}

do

ping_result=`/bin/ping -c 4 ${server} | grep % | awk -F[:" "]+ '{print $6}' | tr -d '%'`

if [[ ${ping_result} -eq "0" ]]

then

echo "${server} is ok"

echo "${server} is ok" >> /log/Ping/${Date}.log

elif [[ ${ping_result} -eq "100" ]]

then

echo "${server} is down"

echo "${server} is down" >> /log/Ping/${Date}.log

else

echo "${server} is packet loss"

echo "${server} is packet loss" >> /log/Ping/${Date}.log

fi

done

/usr/bin/mail -s " Server Status" your-email@example.com < /log/Ping/${Date}.log

#删除log文件

rm -rf /log/Ping/${Date}.log

发件箱和收件箱可以为同一个

注意:脚本中的判断条件中0和100的意思分别为服务器的丢包率,0为不丢包,100为全丢包,其余数值为部分丢包,下图可看出效果

44245729a2a0f7d30ff60dcfe6f1c091.png

执行脚本结果:

root@ubuntu:~/server_monitor# bash ping.sh

根据当前时间创建日志文件

192.168.4.9 is ok

192.168.4.11 is ok

192.168.4.12 is ok

192.168.4.13 is ok

192.168.4.14 is ok

192.168.4.21 is ok

192.168.4.22 is ok

192.168.4.23 is ok

192.168.4.24 is ok

192.168.4.31 is ok

192.168.4.32 is ok

192.168.4.33 is ok

192.168.4.34 is ok

192.168.4.41 is ok

192.168.4.42 is ok

192.168.4.43 is ok

192.168.4.44 is ok

4.使用crontab定时任务每隔半小时执行检测脚本

SHELL=/bin/bash

PATH=/sbin:/bin:/usr/sbin:/usr/bin

MAILTO=root

# For details see man 4 crontabs

# Example of job definition:

# .---------------- minute (0 - 59)

# | .------------- hour (0 - 23)

# | | .---------- day of month (1 - 31)

# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...

# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat

# | | | | |

# * * * * * user-name command to be executed

*/30 * * * * /root/server_monitor/ping.sh > /dev/null &

邮件效果:

a4b370fff9097ee1a5cea25f4daa6307.png

5.优化

若觉得半个小时时间太频繁,可以设置检测到服务器宕机或者丢包的时候发邮件,正常情况下不发

#!/bin/bash

Date=`date -d "today" +"%Y-%m-%dT%H-%M-%S"`

echo "根据当前时间创建日志文件"

mkdir -p /log/Ping/

touch /log/Ping/${Date}_normal.log

touch /log/Ping/${Date}_unnormal.log

servers="192.168.4.9 192.168.4.10 \

192.168.4.11 192.168.4.12 192.168.4.13 192.168.4.14 \

192.168.4.21 192.168.4.22 192.168.4.23 192.168.4.24 \

192.168.4.31 192.168.4.32 192.168.4.33 192.168.4.34 \

192.168.4.41 192.168.4.42 192.168.4.43 192.168.4.44"

for server in ${servers}

do

ping_result=`/bin/ping -c 4 ${server} | grep % | awk -F[:" "]+ '{print $6}' | tr -d '%'`

if [[ ${ping_result} -eq "0" ]]

then

echo "${server} is ok"

echo "${server} is ok" >> /log/Ping/${Date}_normal.log

elif [[ ${ping_result} -eq "100" ]]

then

echo "${server} is down"

echo "${server} is down" >> /log/Ping/${Date}_unnormal.log

else

echo "${server} is packet loss"

echo "${server} is packet loss" >> /log/Ping/${Date}_unnormal.log

fi

done

if [ -s /log/Ping/${Date}_unnormal.log ];then

echo "不为空,发送邮件"

/usr/bin/mail -s " Server Status" your-email@example.com < /log/Ping/${Date}_unnormal.log

else

echo "为空,不发送邮件"

fi

#删除log文件

rm -rf /log/Ping/${Date}_*.log

“The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time.” – Tom Cargill

标  题:监控Linux服务器是否宕机并发送邮件的解决方案

作  者:出  处:https://leif.fun/articles/2019/09/09/1568019358404.html

关于博主:坐标南京,运维工程师,如有问题探讨可以直接下方留言。

声援博主:如果您觉得文章对您有帮助,可以评论、订阅、收藏。您的鼓励是博主的最大动力!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值