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

本贴最后更新于 1650 天前,其中的信息可能已经时移世易

1.ubuntu 下安装和配置

sudo apt-get install mailutils

以下保持默认即可
Postfix Configuration
image.png

image.png

image.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 才能调用各大邮箱提供商的账号密码进行邮件发送,用邮箱登录密码是发送不成功的!

image.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 为全丢包,其余数值为部分丢包,下图可看出效果

image.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 &

邮件效果:
image.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
  • crontab

    crontab 命令常见于 类 Unix 的操作系统之中,用于设置周期性被执行的指令。该命令从标准输入设备读取指令,并将其存放于“crontab”文件中,以供之后读取和执行。该词来源于希腊语 chronos(χρνο),原意是时间。

    12 引用 • 25 回帖 • 1 关注
  • Linux

    Linux 是一套免费使用和自由传播的类 Unix 操作系统,是一个基于 POSIX 和 Unix 的多用户、多任务、支持多线程和多 CPU 的操作系统。它能运行主要的 Unix 工具软件、应用程序和网络协议,并支持 32 位和 64 位硬件。Linux 继承了 Unix 以网络为核心的设计思想,是一个性能稳定的多用户网络操作系统。

    914 引用 • 930 回帖 • 1 关注

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...
  • someone34810

    这咋办嘛 那咋整嘛 这谁顶的住嘛

  • 其他回帖
  • alanfans

    360,百度云监控都有现成的功能
    image.png

  • namelysweet

    也可以用 fping,直接带网段的,不用一个一个输了: Linux fping 命令

  • someone
    作者

    我们公司总部经常整栋楼停电,还不带提前通知的,光纤也经常被挖断 ┑( ̄Д  ̄)┍

  • 查看全部回帖