|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
如果您觉得本篇CentOSLinux教程讲得好,请记得点击右边漂浮的分享程序,把好文章分享给你的好朋友们!几个月前入手下手利用VPS,每个月限定300GB流量,流量便利基础够用了,可是偶然候因为遭到一些歹意会见,招致CPU、Memory等资本损耗较年夜,招致VPS上的博客网站呼应工夫太慢乃至偶然基本没法翻开网页。以是,我复杂做了个剧本来举行监控和发邮件报警。
因为不是做很专业的运维,临时不想上很专业的监控工具,如:Nagios、Puppet、Cacti、Zabbix等,以是本人手动了个Shell剧本来监控我体贴的CPU、Load、Memory、收集传输、博客网站可否翻开等外容。将该监控剧本分享以下:
https://github.com/smilejay/shell/blob/master/sh2013/vps_monitor.sh- #!/bin/bash#set-x#thescripttomonitormyVPS#Itwillalertwhenmemory,load,CPU%,networking,httpd/mysqldorhome-page#isinanabnormalstate.#author:Jay#date:2013-10-16EMAIL="smile665@gmail.com"WARN_MSG=""#alertwhenfreememoryislessthan50MBfunctionmem_free(){threshold=50#50MBfreememoryfree_mem=$(free-m|grep"buffers/cache"|awk{print$4})if[$free_mem-lt$threshold];thenWARN_MSG=$WARN_MSG"Freememeoryislessthan$thresholdMB.
- "return1fireturn0}#alertwhenload(5min)islargerthan4functionload_avg(){threshold=4#loadis4load_5min=$(cat/proc/loadavg|awk{print$2})if[$(echo"$load_5min>$threshold"|bc)-eq1];thenWARN_MSG=$WARN_MSG"5minaverageloadislargerthan$threshold.
- "return1fireturn0}#alertwhenCPUidle%islessthan20%functioncpu_idle(){threshold=20#CPUidle%20%cpu_idle=$(sar15|grep-iAverage|awk{print$NF})if[$(echo"$cpu_idle<$threshold"|bc)-eq1];then#inprintfcmd,%%representsasingle%WARN_MSG=$WARN_MSG"CPUidle%%islessthan$threshold%%.
- "return1fireturn0}#alertwhennetworkingtxspeedislargerthan80kB/sfunctionnetwork_tx(){threshold=80#TXspeed80kB/sinte***ce=eth0tx_speed=$(sar-nDEV105|grep"Average"|grep"$inte***ce"|awk{print$6})if[$(echo"$tx_speed>$threshold"|bc)-eq1];thenWARN_MSG=$WARN_MSG"networkingTXspeedislargerthan$thresholdkB/s.
- "return1fireturn0}#alertwhenhttpdormysqldprocessdoesntexistfunctionhttpd_mysqld(){ps-ef|grephttpd|grep-vgrepif[$?-ne0];thenWARN_MSG=$WARN_MSG"httpdprocessdoesntexist.
- "return1elseps-ef|grepmysqld|grep-vgrepif[$?-ne0];thenWARN_MSG=$WARN_MSG"mysqldprocessdoesntexist.
- "return1fifireturn0}#alertwhenStayhungrydoesntexistinthehomepagehttp://smilejay.com/functionhome_page(){url=http://smilejay.com/keywords="Stayhungry"curl-sL$url|grep-i"$keywords"if[$?-ne0];thenWARN_MSG=$WARN_MSG"keywords$keywordsdoesntexistonlink$url.
- "return1fireturn0}#useanarraytostorethereturnvalueofeachmonitoringfunctionmem_freechk[1]=$?load_avgchk[2]=$?cpu_idlechk[3]=$?network_txchk[4]=$?httpd_mysqldchk[5]=$?home_pagechk[6]=$?#sendwarningemailtothewebmasterifanyofthecheckfails.foriin$(seq16)doif[${chk[i]}-ne0];thenprintf"$WARN_MSG"|mailx-s"Warningfromsmilejay.com"$EMAILexit1fidone
复制代码
如果您觉得本篇CentOSLinux教程讲得好,请记得点击右边漂浮的分享程序,把好文章分享给你的小伙伴们! |
|