• 配置需要监控的服务器
    • 数组定义:host_ports=(host_name=host_port=uri_path)
      host_name为容易识别的服务器名称
      host_port为服务器ip和服务端口
      uri_path为经济的请求路径
    • 为脚本运行的服务器开放防火墙端口,内网ip和外网ip皆可,但要与host_port一致
      -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp -s 121.45.111.17 --dport 8090 -j ACCEPT
  • 监控结果记录日志
    • 先记录日期
      echo `date +'%Y-%m-%d %H:%M:%S'` > $log
    • 选择notify_host,需要提供发送邮件的接口,所以前面定义数组时将有邮件接口的host放在前面,找到第一个运行正常的host即可作为notify_host
          if [ -z $notify_host ]; then
              notify_host=$host_port
          fi
    • 检查完后检查日志文件,大于1行则发邮件通知
      curl http://$notify_host/admin/monitor.html?log=$log > /dev/null 2>&1
  • crontab定时运行监控脚本
    • crontab配置,每20分钟检查一次
      */20 * * * * /soft/monitor.sh >> /soft/logs/monitor-cron.log
    • 运行效果,测试时每两分钟检查一次
    • 邮件通知

脚本内容
log=/soft/logs/monitor.log
echo `date +'%Y-%m-%d %H:%M:%S'`
echo `date +'%Y-%m-%d %H:%M:%S'` > $log
host_ports=(
caifuxiang-slave-test=121.45.111.17:8080=/monitor.html
ghcaiyuan-slave-test=121.45.111.17:8080=/monitor.html
caifuxiang-app=121.45.111.17:8080=/monitor.html
caifuxiang-app2=121.45.111.17:8080=/monitor.html
ghcaiyuan-app=121.45.111.17:8080=/monitor.html
ghcaiyuan-app2=121.45.111.17:8080=/monitor.html
caifuxiang-master-upload=121.45.111.17:8090=/upload/crossdomain.xml
ghcaiyuan-master-upload=121.45.111.17:8090=/upload/crossdomain.xml
)
#echo ${host_ports[*]}
for ((i=0;i<${#host_ports[@]};++i))
do
host_name=`echo ${host_ports[i]}|cut -d "=" -f 1`
host_port=`echo ${host_ports[i]}|cut -d "=" -f 2`
uri_path=`echo ${host_ports[i]}|cut -d "=" -f 3`
#echo $host_port
if curl http://$host_port$uri_path > /dev/null 2>&1
then
        #echo "$host_name is ok"
    if [ -z $notify_host ]; then
        notify_host=$host_port
    fi
else
    echo "$host_name is not ok"
        echo "$host_name is not ok" >> $log
fi
done
log_lines=`cat $log|wc -l`
#echo "$log lines: $log_lines"
if [ $log_lines -gt 1 ]; then
    echo "send notify email using: $notify_host, log: $log"
    #curl http://$notify_host/admin/monitor.html?log=$log > /dev/null 2>&1
    log_content=`cat $log` #post log_content to notify_host, cause it can't get file $log
    curl -d "log=$log_content" http://$notify_host/admin/monitor.html > /dev/null 2>&1
else
    echo "everything is ok"
fi

附件列表

linux服务监控脚本的更多相关文章

  1. linux centos7 定时执行服务监控脚本

    2021-08-25 1. 需求 在服务挂掉之后我们要怎么做才能保证服务在短时间内开启?可以编写脚本监控服务的状态,在服务挂掉后及时将其开启,并定时执行该脚本. 2. 脚本编写 思路:平常我们可以通过 ...

  2. Zabbix Linux http 监控脚本

    说明:自定义监控脚本,监控内存是否启用主进程 创建文件:vim check_http.sh #!/bin/bash result=`ps -ef | grep httpd | grep -v grep ...

  3. linux 服务发布脚本升级,远程发布,指定拉取远程dev,test等分支代码

    1.本地发布脚本 publish.sh #!/bin/sh currentDay=`date +%Y%m%d` currentTime=`date +%Y%m%d%H%M%S` tomcat1=/da ...

  4. Linux 负载监控脚本

    #!/bin/bash Date=`echo $(date +%Y\-%m\-%d\ %H:%M:%S)`HostName=`hostname`IP=`ifconfig eth0 | grep &qu ...

  5. linux服务器监控脚本

    #!/bin/bash #获取cpu使用率 cpuUsage=`top -n 1 | awk -F '[ %]+' 'NR==3 {print $2}'` #获取磁盘使用率 data_name=&qu ...

  6. linux网络监控脚本

    http://www.51testing.com/html/92/77492-828434.html

  7. linux重启服务的脚本命令

    最近做网站测试,每次测试完成都要重启服务,为此写了一个简单的shell脚本 linux服务重启shell脚本示例 2014年12月18日 linux服务重启脚本,如何实现linux服务的定时重启,可以 ...

  8. Linux 服务器系统监控脚本 Shell【转】

    转自: Linux 服务器系统监控脚本 Shell - 今日头条(www.toutiao.com)http://www.toutiao.com/i6373134402163048961/ 本程序在Ce ...

  9. Linux简单Shell脚本监控MySQL、Apache Web和磁盘空间

    Linux简单Shell脚本监控MySQL.Apache Web和磁盘空间 1. 目的或任务 当MySQL数据库.Apache Web服务器停止运行时,重新启动运行,并发送邮件通知: 当服务器磁盘的空 ...

随机推荐

  1. 我对CONTAINING_RECORD宏的详细解释

    宏CONTAINING_RECORD的用处其实还是相当大的, 而且很是方便, 它的主要作用是: 根据结构体中的某成员的指针来推算出该结构体的指针! 下面从一个简单的例子开始说起: 我们定义一个结构体, ...

  2. springMVC在jsp传对象到后台

    ==============实体entity======================= package com.jb.pub.entity; import java.io.Serializable ...

  3. jsp与El,jstl知识点总结归纳

    jsp与El,jstl知识点总结归纳 jsp部分 一.jsp的三大指令 page ,include,taglib 1.jsp中的page指令 <% page %>-设置jsp 例如: &l ...

  4. Hadoop伪分布配置与基于Eclipse开发环境搭建

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  5. thinkphp 模板显示display和assign的用法

    this->assign('name',$value); //在 Action 类里面使用 assign 方法对模板变量赋值,无论何种变量类型都统一使用 assign 赋值 $this-> ...

  6. Nmap 源代码学习四 软件简单使用

    软件安装环境是win7.使用Zenmap, nmap6.49BETA2 扫描主机port nmap -T4 -A -v 192.168.0.207 输出结果: 扫描整个子网 nmap 192.168. ...

  7. nginx 流媒体 flv 播放 以及上传大小 配置文件设置

    nginx 流媒体 flv 播放 以及上传大小 配置文件设置   server {listen 80;server_name localhost;root /www/web/default;index ...

  8. [MEAN Stack] First API -- 1. with Node.js, Express and MongoDB

    Learn how to import data into your MongoDB and then use Express to serve a simple Node.js API. Impor ...

  9. php获取当前url完整地址

    //获取当前访问的完整url地址 function getCurUrl() { $url = 'http://'; if (isset($_SERVER['HTTPS']) && $_ ...

  10. 利用shell脚本统计文件中出现次数最多的IP

    比如有如下文件test.txt 1  134.102.173.43 2  134.102.173.43 3  134.102.171.42 4  134.102.170.9 要统计出现次数最多的IP可 ...