shell实现脚本监控服务器及web应用

   实际工作中我们需要知道部署在服务器上的应用有没有问题,但是人为的操作太麻烦有咩有简单的方式呢shell来监控我们服务器运行状态以及服务器上部署的应用,如果出现异常就会自动发送一个邮件给我们,开始搞起。。。

老套路,先梳理思路

监控apache web服务

监控mysql数据库

监控服务器硬盘使用情况

监控服务器的内存使用

  废话不多说,直接上代码

1.apache web 服务器

!/bin/bash
# 表示请求链接3秒钟,不要返回的测试数据
nc -w 3 localhost 80 &>/dev/null
if [ $? -eq 0 ];then
        str="apache web status Running!"
else
        str="apache web status Shuting!"
fi
# 发送的主题,邮件地址
echo str|mail -s 'apache web server' admin@lampym.com

2.监控mysql

!/bin/bash
# 表示请求链接3秒钟,不要返回的测试数据
nc -w 3 localhost 3306 &>/dev/null
if [ $? -eq 0 ];then
        str="mysql server status Running!"
else
        str="mysql server status Shuting!"
fi
# 发送的主题,邮件地址
echo str|mail -s 'mysql server status' admin@lampym.com

3.监控服务器disk

#!/bin/bash
:<<!
NR表示行数,$5表示第5列,具体的自己根据实际调整
!
ds=`df |awk '{if(NR==4){print int($5)}}'`
# 这里45根据实际需要更改
if [ $ds -lt 45 ];then
	str="disk space is less then!!!"
else
	str="disk space is greate than 45%!!!"
fi
echo $str|mailx -s 'linux server disk space' admin@lampym.com

4.监控服务器monery 

#!/bin/bash
:<<!
具体的自己根据实际调整
!
mery=`df |awk '{if(NR==2){print int($3*100/$2)}}'`
if [ $mery -lt 50 ];then
	str="mery space is less then 50%!!!"
else
	str="mery space is greate than 50%!!!"
fi
echo $str|mailx -s 'linux server mery space' admin@lampym.com

 整合一下

#!/bin/bash
# 功能:监控资源
# 名称:cont.sh
# 作者:枫客浪人
# 版本:0.1
# 联系方式:xxxx
# apache 应用服务 
apache_web(){
    nc -w 3 localhost 80 &>/dev/null
  if [ $? -eq 0 ];then
        str="apache web status Running!"
  else
        str="apache web status Shuting!"
  fi
    # 发送的主题,邮件地址
  echo str|mail -s 'apache web server' admin@lampym.com
}
# mysql 服务
mysql_db(){
    nc -w 3 localhost 3306 &>/dev/null
    if [ $? -eq 0 ];then
        str="mysql server status Running!"
    else
        str="mysql server status Shuting!"
    fi
        # 发送的主题,邮件地址
    echo str|mail -s 'mysql server status' admin@lampym.com 
}
# 磁盘使用情况
disk_mnt(){
    ds=`df |awk '{if(NR==4){print int($5)}}'`
    # 这里45根据实际需要更改
    if [ $ds -lt 45 ];then
        str="disk space is less then!!!"
    else
	str="disk space is greate than 45%!!!"
    fi
    echo $str|mailx -s 'linux server disk space' admin@lampym.com
}
# 内存使用情况
meny_mnt(){
    mery=`df |awk '{if(NR==2){print int($3*100/$2)}}'`
    if [ $mery -lt 50 ];then
	str="mery space is less then 50%!!!"
    else
	str="mery space is greate than 50%!!!"
    fi
    echo $str|mailx -s 'linux server mery space' admin@lampym.com
}
min(){
apache_web()
mysql_db()
disk_mnt()
meny_mnt()
}

 

  个人觉得还可将脚本更加复杂化,加入更多我们想要的信息,比如报错后具体的错误信息等等,当然这只是简单的例子,如果有需要,小伙伴们可以自己添加自己需要的内容。。。。。

 

  关于自动监控策略,我这里采用的是Linux中的定时crontab,编写计划,自动监控,每天发送一份报告,这样每天我都会收到服务器的一个状态

 

crontab -e

每天13:10分执行代码发送一份邮件

 

 

  

转载于:https://www.cnblogs.com/syketw23/p/7671050.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值