shell脚本实现定时监控http服务的运行状态

注意:监控方法可以为端口、进程、URL模拟访问方式,或者三种方法综合。

说明:由于截止到目前仅讲了if语句,因此,就请大家用if语句来实现。

  [root@oldboy-B scripts]# cat apachemon

  #!/bin/sh

  #created by oldboy 20110523

  . /etc/init.d/functions

  HTTPPRONUM=`ps -ef|grep http|grep -v grep|wc -l`

  #if [ $HTTPPRONUM -lt 1 ];then

  if [[ $HTTPPRONUM -lt 1 ]];then

  action “httpd is not running” /bin/false

  action “httpd is not running” /bin/false >/tmp/httpd.log

  httpdctl restart >/dev/null 2>&1

  action “httpd is restart” /bin/true

  mail -s “`uname -n`’s httpd restarted at `(date)`” 31333741@qq.com

  exit 1

  else

  action “httpd is running” /bin/true

  exit 0

  fi
  [root@oldboy-B scripts]# apachemon

  httpd is running [确定]

  [root@oldboy-B scripts]# pkill httpd

  [root@oldboy-B scripts]# ps -ef |grep http |grep -v grep

  [root@oldboy-B scripts]# apachemon

  httpd is not running [失败]

  httpd is restart [确定]

  [root@oldboy-B scripts]# ps -ef|grep http|grep -v grep

  root 5845 1 1 15:59 ? 00:00:00 /usr/sbin/httpd -k restart

  apache 5852 5845 0 15:59 ? 00:00:00 /usr/sbin/httpd -k restart

  apache 5853 5845 0 15:59 ? 00:00:00 /usr/sbin/httpd -k restart

  apache 5854 5845 0 15:59 ? 00:00:00 /usr/sbin/httpd -k restart

  apache 5855 5845 0 15:59 ? 00:00:00 /usr/sbin/httpd -k restart

  apache 5856 5845 0 15:59 ? 00:00:00 /usr/sbin/httpd -k restart

  apache 5857 5845 0 15:59 ? 00:00:00 /usr/sbin/httpd -k restart

  apache 5858 5845 0 15:59 ? 00:00:00 /usr/sbin/httpd -k restart

  apache 5859 5845 0 15:59 ? 00:00:00 /usr/sbin/httpd -k restart
 

脚本改进

  [root@oldboy-B /]# echo oldboytest >/var/www/html/index.htm

  [root@oldboy-B /]# wget –quiet –spider http://10.0.0.161/index.htm

  [root@oldboy-B /]# echo $?

  0

  [root@oldboy-B /]# ll index.htm

  ls: index.htm: 没有那个文件或目录

  [root@oldboy-B scripts]# cat apachemon1

  #!/bin/sh

  #created by oldboy 20110523

  

  . /etc/init.d/functions

  #HTTPPRONUM=`ps -ef|grep http|grep -v grep|wc -l` #=====>这个是基于http方式进行判断

  wget –quiet –spider http://10.0.0.161/index.htm #=====>这个是基于WGET URL方式进行判断

  if [ $? -ne 0 ];then

  action “httpd is not running” /bin/false >/tmp/httpd.log

  httpdctl restart >/dev/null 2>&1

  action “httpd is restart” /bin/true >>/tmp/httpd.log

  mail -s “`uname -n`’s httpd restarted at `(date)`” mail@qq.com

  exit 1

  else

  action “httpd is running” /bin/true

  exit 0

  fi

真正使用时,有些输出是不需要的就去掉

  [root@oldboy-B scripts]# cat apachemon1

  #!/bin/sh

  #created by oldboy 20110523

  #

  . /etc/init.d/functions

  wget –quiet –spider http://10.0.0.161/index.htm #=====>这个是基于WGET URL方式进行判断

  if [ $? -ne 0 ];then

  action “httpd is not running” /bin/false >/tmp/httpd.log

  httpdctl restart >/dev/null 2>&1

  action “httpd is restart” /bin/true >>/tmp/httpd.log

  mail -s “`uname -n`’s httpd restarted at `(date)`” 31333741@qq.com

  exit 1

  fi

多条件判断的写法

  [root@oldboy-B scripts]# cat apachemon1

  #!/bin/sh

  #created by oldboy 20110523

  #

  . /etc/init.d/functions

  HTTPPORTNUM=`netstat -lnt|grep 80|grep -v grep|wc -l`

  HTTPPRONUM=`ps -ef|grep http|grep -v grep|wc -l`

  wget –quiet –spider http://10.0.0.161/index.htm && RETVAL=$?

  if [ $RETVAL -ne 0 ] || [ $HTTPPORTNUM -ne 1 ] || [ $HTTPPRONUM -lt 1 ] ;then

  #if [ "$RETVAL" != "0" -o "$HTTPPORTNUM" != "1" -o "$HTTPPRONUM" \< "1" ] ;then

  action “httpd is not running” /bin/false

  action “httpd is not running” /bin/false >/tmp/httpd.log

  httpdctl restart >/dev/null 2>&1

  action “httpd is restart” /bin/true

  mail -s “`uname -n`’s httpd restarted at `(date)`” 31333741@qq.com

  exit 1

  else

  action “httpd is running” /bin/true

  exit 0

  fi
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是Shell脚本+钉钉机器人实现服务服务监控告警的步骤: 1. 创建一个Shell脚本,用于监控服务器或服务运行状态,比如检查CPU、内存、硬盘使用情况、网络连接等。 2. 在Shell脚本中,使用curl命令调用钉钉机器人接口,将告警信息发送到指定的钉钉群中。比如: ``` #!/bin/bash # 获取CPU使用率 cpu_usage=$(top -b -n1 | grep "Cpu(s)" | awk '{print $2+$4}') # 获取内存使用率 mem_usage=$(free | awk '/Mem/{printf("%.2f"), $3/$2*100}') # 获取硬盘使用率 disk_usage=$(df -h | awk '$NF=="/"{printf "%s", $5}') # 获取网络连接数 net_conn=$(netstat -an | grep ESTABLISHED | wc -l) # 配置钉钉机器人URL和access_token url="https://oapi.dingtalk.com/robot/send?access_token=your_access_token" # 组装告警消息 msg="服务器告警:\nCPU使用率:${cpu_usage}%\n内存使用率:${mem_usage}%\n硬盘使用率:${disk_usage}\n当前连接数:${net_conn}" # 发送告警消息到钉钉群 curl -H "Content-Type: application/json" -d "{\"msgtype\": \"text\", \"text\": {\"content\": \"${msg}\"}}" ${url} ``` 3. 在服务器上设置定时任务,定期执行Shell脚本,比如每5分钟执行一次。可以使用crontab命令设置定时任务,比如: ``` */5 * * * * /path/to/your/script.sh ``` 4. 在钉钉群中添加机器人,并获取机器人的access_token。 5. 将access_token替换到Shell脚本中的url参数中,然后保存Shell脚本。 6. 等待定时任务执行,当服务器或服务运行状态异常时,Shell脚本会自动发送告警消息到钉钉群中。 以上就是使用Shell脚本+钉钉机器人实现服务服务监控告警的步骤。通过这种方式,可以及时发现服务器或服务运行异常,避免出现更大的问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值