linghaiyan 发表于 2018-8-19 10:02:14

shell脚本监控发邮件

  目标:监控一个服务进程,如果进程死掉发送邮件报警!
  一:安装一个邮件传送代理服务器!(也就是邮件发送工具, 我用sendmail)
  yum install sendmail
  vim /etc/mail/sendmail.mc
  DAEMON_OPTIONS(`Port=25,Addr=0.0.0.0, Name=MTA')dnl   #修改ip地址
  TRUST_AUTH_MECH(`EXTERNAL DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl    #着两行去掉dnl
  define(`confAUTH_MECHANISMS', `EXTERNAL GSSAPI DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl
m4 sendmail.mc > sendmail.cf  service sendmail start    启动sendmail服务
  ___________可能出现的问题!!_________
  m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf
  sendmail.mc:10: m4: cannot open `/usr/share/sendmail-cf/m4/cf.m4′: No such file or directory
  提示没有sendmail-cf这个目录即是没有安装这个东西
  #请安装sendmail-cf
  yum -y install sendmail-cf
  测试能否成功发送邮件!
  echo "fucking " |mail -s "11111111111111111" abc@163.com
  如果163邮箱可以收到邮件就ok了!!!
  ----------问题-----------------------------
  bash: mail: command not found的解决方法
  yum -y install mailx
  ----------------------------------------
二编写简单的shell脚本监控服务的进程!  vim /root/nginx.sh    (nginx.sh有执行权限)
  #!/bin/bash
  statu=`pgrep nginx`
  nowdate=`date`
  ip=`/sbin/ifconfig eth0 | awk '/inet addr/ { print $2 }'`
  if [ "$statu" = "" ]
  then
  echo -e " nginx is dead!! \n $nowdate$ip" | mail -s 'nginx is dead!!' dlzabc@163.com
  fi
这只是一个简单的shell脚本! 如果没有检测到nginx服务就发送邮件到163邮箱!!  三添加计划任务!!
  crontab -e
  * * * * */root/nginx.sh   #crontab 不能时时监控最少1分钟检测一次!
  这样就可以了, 当然这只是个例子而已,服务停掉了才通知就没有意义了!我们可以监控别的数据比如内存使用和cpu使用情况如果超过设定的值就报警!这样就能在出问题之前先得到消息了!!

页: [1]
查看完整版本: shell脚本监控发邮件