1. 获取ip、cpu、内存信息、loadAverage
ip使用ifconfig获得
localip=$(ifconfig eth0 | grep 'inet addr' | awk '{print $2}' | cut -f2 -d:)
空闲cpu由mpstat获得
cpuidle=$(mpstat | grep all | awk '{print $11}')
内存使用情况由free获得
freemem=$(free | grep Mem | awk '{print $4}')
totalmem=$(free | grep Mem | awk '{print $2}')
pcent=$(free | grep Mem | awk '{print $4/$2}')
loadAverage由uptime获得
loadavg1=$(uptime | awk '{print $10}' | cut -f1 -d,)
loadavg5=$(uptime | awk '{print $11}' | cut -f1 -d,)
loadavg15=$(uptime | awk '{print $12}')
- ./etc/profile
- AlertNginxCpu=50
- AlertNginxMem=50
- AlertPcent=0.5
- AlertCpu=50
- AlertLoad=1
- msg=
- Subject='服务器超载警报'
- mailto(){
- /application/search/sendmail.py"$Subject""$msg"
- msg=
- Subject='服务器超载警报'
- }
- date=$(date)
- localip=$(/sbin/ifconfigeth0|grep'inetaddr'|awk'{print$2}'|cut-f2-d:)
- nginxcpu=$(psaxu|grepnginx|grepsearch|grepworker|awk'{print$3}')
- nginxmempcent=$(psaxu|grepnginx|grepsearch|grepworker|awk'{print$4}')
- nginxmem=$(psaxu|grepnginx|grepsearch|grepworker|awk'{print$6}')
- cpuidle=$(mpstat|grepall|awk'{print$11}')
- freemem=$(free|grepMem|awk'{print$4}')
- totalmem=$(free|grepMem|awk'{print$2}')
- freemempcent=$(free|grepMem|awk'{print$4/$2}')
- freeswap=$(free|grepSwap|awk'{print$4}')
- totalswap=$(free|grepSwap|awk'{print$2}')
- freeswappcent=$(free|grepSwap|awk'{print$4/$2}')
- loadavg1=$(uptime|awk'{print$10}'|cut-f1-d,)
- loadavg5=$(uptime|awk'{print$11}'|cut-f1-d,)
- loadavg15=$(uptime|awk'{print$12}')
- echo$date
- echo$localip
- echo'空闲cpu:'$cpuidle
- echo'空闲内存:'$freemem
- echo'总内存:'$totalmem
- echo'空闲内存比:'$freemempcent
- echo'空闲交换内存:'$freeswap
- echo'总交换内存:'$totalswap
- echo'空闲交换内存比:'$freeswappcent
- echo$loadavg1
- echo$loadavg5
- echo$loadavg15
- alldata=''$date'|'$localip'|nginx:'$nginxcpu','$nginxmempcent'('$nginxmem')|cpu_idle:'$cpuidle'|Mem:'$freemem'/'$totalmem'='$freemempcent'|Swap:'$freeswap'/'$totalswap'='$freeswappcent'|loadavg:'$loadavg1','$loadavg5','$loadavg15''
- echo$alldata>>monitor.log
- if[$(echo"$nginxcpu>=$AlertNginxCpu"|bc)=1];then
- msg=''$alldata'|nginxcpu'$nginxcpu':高于'$AlertNginxCpu''
- Subject=''$Subject':'$localip':nginxcpu'$nginxcpu'高于'$AlertNginxCpu''
- echo$msg>>monitor.log
- mailto
- fi
- if[$(echo"$nginxmempcent>=$AlertNginxMem"|bc)=1];then
- msg=''$alldata'|nginxmem'$nginxmempcent':高于'$AlertNginxMem''
- Subject=''$Subject':'$localip':nginxmem'$nginxmempcent'高于'$AlertNginxMem''
- echo$msg>>monitor.log
- mailto
- fi
- if[$(echo"$freeswappcent<=$AlertPcent"|bc)=1];then
- msg=''$alldata'|剩余虚拟内存占比'$freeswappcent':低于'$AlertPcent''
- Subject=''$Subject':'$localip':剩余虚拟内存占比'$freeswappcent'低于'$AlertPcent''
- echo$msg>>monitor.log
- mailto
- fi
- if[$(echo"$cpuidle<=$AlertCpu"|bc)=1];then
- msg=''$alldata'|cpu空闲时间'$cpuidle':低于'$AlertCpu''
- Subject=''$Subject':'$localip':cpu空闲时间'$cpuidle'低于'$AlertCpu''
- echo$msg>>monitor.log
- mailto
- fi
- if[$(echo"$loadavg1>=$AlertLoad"|bc)=1];then
- msg=''$alldata'|load'$loadavg1':高于'$AlertLoad''
- Subject=''$Subject':'$localip':load'$loadavg1'高于'$AlertLoad''
- echo$msg>>monitor.log
- mailto
- fi
- if[-n$msg];then
- echo'系统运行正常'
- fi
2. 空闲cpu小于cpu报警阈值或空闲内存比例低于内存报警阈值或loadAverage超过阈值时发送邮件报警
- importsmtplib
- importsys
- fromemail.mime.textimportMIMEText
- mailto_list=[""]
- mail_host="smtp.126.com"
- mail_user="monitor_algo"
- mail_pass=""
- mail_postfix="126.com"
- defsend_mail(to_list,sub,context):
- me=mail_user+"<"+mail_user+"@"+mail_postfix+">"
- msg=MIMEText(context)
- msg['Subject']=sub
- msg['From']=me
- msg['To']=";".join(to_list)
- try:
- send_smtp=smtplib.SMTP()
- send_smtp.connect(mail_host)
- send_smtp.login(mail_user,mail_pass)
- send_smtp.sendmail(me,to_list,msg.as_string())
- send_smtp.close()
- returnTrue
- except(Exception,e):
- print(str(e))
- returnFalse
- if__name__=="__main__":
- if(True==send_mail(mailto_list,sys.argv[1],sys.argv[2])):
- pass
- else:
- pass
3. 加入crontab定时任务
注意一点,crontan中的环境变量与用户环境的环境变量不一样,因其不会从缺省的用户profile文件中读入环境变量参数,最简单的方法是用source命令(.)
*/1 * * * * . /etc/profile; sh /application/search/monitor.sh
分享到:
相关推荐
linux下使用php+shell实现用户注册及磁盘配额文.pdf
├─012Linux基础篇_文件系统目录结构.avi ├─013Linux实操篇_远程登录XShell5.avi ├─014Linux实操篇_远程上传下载文件XFTP5.avi ├─015Linux实操篇_vi和vim的使用.avi ├─016Linux实操篇_关机重启注销.avi ├...
#!/bin/bash i=$(cat /dev/urandom | head -n 10 | md5sum | head -c 10) echo $i >> /data/1.log 2>&1 expect test.expect $i >> /data/1.log 2>&1 sleep 3 python /opt/file/se.py $i
Linux服务器下流量监控报警系统shell脚本实现.pdf
用Linux/UNIX Shell编程语言设计系统监控程序.pdf
本科毕业设计+python+基于Flask+Bootstrap+Mysql的项目审批系统(含源码+数据库) 安装相关库 $ git clone https://github.com/rmboot/approvalsystem $ cd approvalsystem $ pipenv install $ pipenv shell 初始化...
主要介绍了通过shell+python实现企业微信预警,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
使用Linux Shell实现主机及网站运行监控并报警.pdf
Linux系统的shell作为操作系统的外壳,为用户提供使用操作系统的接口。它是命令语言、命令解释程序及程序设计语言的统称。 shell是用户和Linux内核之间的接口程序,如果把Linux内核想象成一个球体的中心,shell...
1)启动性能监控脚本,cpu与memory信息记录于文件中。 ./watch_start runtime filePrefix runtime暂时为运行次数。根据runtime计算最佳统计间隔和次数的模块未实现。 记录文件名:filePrefix_timestamp_cpu.txt 或 ...
Linux常用的系统监控shell脚本
用C语言实现了Linux中Shell的部分功能,包括ls rm echo等基本常用命令,每个命令都有详细代码实现。
主要介绍了详解Linux使用shell+expect远程登录主机的相关资料,这里提供了详细的实现步骤,需要的朋友可以参考下
linux中shell交互原理,Python语法入门讲解以及实际应用例子,shell语法入门讲解以及实际应用案例
Linux+Shell+实例精解[1].part4
该程序使用场景说明:主要用域Linux服务器监控程序日志,如出现关键字异常则触发相应的动作或告警操作,通知到邮件联系人
Linux操作系统Shell脚本.zipLinux操作系统Shell脚本.zipLinux操作系统Shell脚本.zip Linux操作系统Shell脚本.zipLinux操作系统Shell脚本.zipLinux操作系统Shell脚本.zip Linux操作系统Shell脚本.zipLinux操作系统...
在服务器运维过程中,经常需要对服务器的各种资源进行监控,例如:CPU的负载监控,磁盘的使用率监控,进程数目监控等等,以在系统出现异常时及时报警,通知系统管理员。本文介绍在Linux系统下几种常见的监控需求及其...