{"id":147,"date":"2013-08-31T08:36:11","date_gmt":"2013-08-31T00:36:11","guid":{"rendered":"http:\/\/ufqi.com\/blog\/?p=147"},"modified":"2013-09-08T07:21:23","modified_gmt":"2013-09-07T23:21:23","slug":"server-monitoring-shell-script","status":"publish","type":"post","link":"https:\/\/ufqi.com\/blog\/server-monitoring-shell-script\/","title":{"rendered":"\u670d\u52a1\u5668\u76d1\u63a7\u811a\u672c"},"content":{"rendered":"<p>\u6700\u8fd1\u65f6\u4e0d\u65f6\u6709\u670b\u53cb\u95ee\u6211\u5173\u4e8e\u670d\u52a1\u5668\u76d1\u63a7\u65b9\u9762\u7684\u95ee\u9898\uff0c\u95ee\u5e38\u7528\u7684\u670d\u52a1\u5668\u76d1\u63a7\u9664\u4e86\u7528\u5f00\u6e90\u8f6f\u4ef6\uff0c\u6bd4\u5982\uff1acacti\uff0cnagios\u76d1\u63a7\u5916\u662f\u5426\u53ef\u4ee5\u81ea\u5df1\u5199shell\u811a\u672c\u5462\uff1f\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\u5199\u51fa\u7684shell\u811a\u672c\u66f4\u80fd\u6ee1\u8db3\u9700\u6c42\uff0c\u66f4\u80fd\u7ec6\u5316\u4e3b\u673a\u76d1\u63a7\u7684\u5168\u9762\u6027\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u6211\u5e38\u7528\u7684\u51e0\u4e2a\u4e3b\u673a\u76d1\u63a7\u7684\u811a\u672c\uff0c\u5927\u5bb6\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u60c5\u51b5\u5728\u8fdb\u884c\u4fee\u6539\uff0c\u5e0c\u671b\u80fd\u7ed9\u5927\u5bb6\u4e00\u70b9\u5e2e\u52a9\u3002<\/p>\n<h3>1\u3001\u67e5\u770b\u4e3b\u673a\u7f51\u5361\u6d41\u91cf<\/h3>\n<div>\n<div id=\"highlighter_316685\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>1<\/div>\n<div>2<\/div>\n<div>3<\/div>\n<div>4<\/div>\n<div>5<\/div>\n<div>6<\/div>\n<div>7<\/div>\n<div>8<\/div>\n<div>9<\/div>\n<div>10<\/div>\n<div>11<\/div>\n<div>12<\/div>\n<div>13<\/div>\n<div>14<\/div>\n<div>15<\/div>\n<div>16<\/div>\n<div>17<\/div>\n<div>18<\/div>\n<\/td>\n<td>\n<div>\n<div><code>#!\/bin\/bash<\/code><\/div>\n<div><code>#!\/bin\/bash<\/code><\/div>\n<div><code>#network<\/code><\/div>\n<div><code>#Mike.Xu<\/code><\/div>\n<div><code>while<\/code> <code>: ; <\/code><code>do<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>time<\/code><code>=<\/code><code>'date +%m\"-\"%d\" \"%k\":\"%M'<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>day=<\/code><code>'date +%m\"-\"%d'<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>rx_before=<\/code><code>'ifconfig eth0|sed -n \"8\"p|awk '<\/code><code>{print $2}<\/code><code>'|cut -c7-'<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>tx_before=<\/code><code>'ifconfig eth0|sed -n \"8\"p|awk '<\/code><code>{print $6}<\/code><code>'|cut -c7-'<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>sleep<\/code> <code>2<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>rx_after=<\/code><code>'ifconfig eth0|sed -n \"8\"p|awk '<\/code><code>{print $2}<\/code><code>'|cut -c7-'<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>tx_after=<\/code><code>'ifconfig eth0|sed -n \"8\"p|awk '<\/code><code>{print $6}<\/code><code>'|cut -c7-'<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>rx_result=$[(rx_after-rx_before)<\/code><code>\/256<\/code><code>]<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>tx_result=$[(tx_after-tx_before)<\/code><code>\/256<\/code><code>]<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>echo<\/code> <code>\"$time Now_In_Speed: \"<\/code><code>$rx_result<\/code><code>\"kbps Now_OUt_Speed: \"<\/code><code>$tx_result<\/code><code>\"kbps\"<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>sleep<\/code> <code>2<\/code><\/div>\n<div><code>done<\/code><\/div>\n<div><code>done<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<h3>2\u3001\u7cfb\u7edf\u72b6\u51b5\u76d1\u63a7<\/h3>\n<div>\n<div id=\"highlighter_451954\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>1<\/div>\n<div>2<\/div>\n<div>3<\/div>\n<div>4<\/div>\n<div>5<\/div>\n<div>6<\/div>\n<div>7<\/div>\n<div>8<\/div>\n<div>9<\/div>\n<div>10<\/div>\n<div>11<\/div>\n<div>12<\/div>\n<div>13<\/div>\n<\/td>\n<td>\n<div>\n<div><code>#!\/bin\/sh<\/code><\/div>\n<div><code>#systemstat.sh<\/code><\/div>\n<div><code>#Mike.Xu<\/code><\/div>\n<div><code>IP=192.168.1.227<\/code><\/div>\n<div><code>top<\/code> <code>-n 2| <\/code><code>grep<\/code> <code>\"Cpu\"<\/code> <code>&gt;&gt;.<\/code><code>\/temp\/cpu<\/code><code>.txt<\/code><\/div>\n<div><code>free<\/code> <code>-m | <\/code><code>grep<\/code> <code>\"Mem\"<\/code> <code>&gt;&gt; .<\/code><code>\/temp\/mem<\/code><code>.txt<\/code><\/div>\n<div><code>df<\/code> <code>-k | <\/code><code>grep<\/code> <code>\"sda1\"<\/code> <code>&gt;&gt; .<\/code><code>\/temp\/drive_sda1<\/code><code>.txt<\/code><\/div>\n<div><code>#df -k | grep sda2 &gt;&gt; .\/temp\/drive_sda2.txt<\/code><\/div>\n<div><code>df<\/code> <code>-k | <\/code><code>grep<\/code> <code>\"\/mnt\/storage_0\"<\/code> <code>&gt;&gt; .<\/code><code>\/temp\/mnt_storage_0<\/code><code>.txt<\/code><\/div>\n<div><code>df<\/code> <code>-k | <\/code><code>grep<\/code> <code>\"\/mnt\/storage_pic\"<\/code> <code>&gt;&gt; .<\/code><code>\/temp\/mnt_storage_pic<\/code><code>.txt<\/code><\/div>\n<div><code>time<\/code><code>=`<\/code><code>date<\/code> <code>+%m<\/code><code>\".\"<\/code><code>%d<\/code><code>\" \"<\/code><code>%k<\/code><code>\":\"<\/code><code>%M`<\/code><\/div>\n<div><code>connect=`<\/code><code>netstat<\/code> <code>-na | <\/code><code>grep<\/code> <code>\"219.238.148.30:80\"<\/code> <code>| <\/code><code>wc<\/code> <code>-l`<\/code><\/div>\n<div><code>echo<\/code> <code>\"$time\u00a0 $connect\"<\/code> <code>&gt;&gt; .<\/code><code>\/temp\/connect_count<\/code><code>.txt<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<h3>3\u3001\u76d1\u63a7\u4e3b\u673a\u7684\u78c1\u76d8\u7a7a\u95f4\uff0c\u5f53\u4f7f\u7528\u7a7a\u95f4\u8d85\u8fc790\uff05\u5c31\u901a\u8fc7\u53d1mail\u6765\u53d1\u8b66\u544a<\/h3>\n<div>\n<div id=\"highlighter_694646\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>1<\/div>\n<div>2<\/div>\n<div>3<\/div>\n<div>4<\/div>\n<div>5<\/div>\n<div>6<\/div>\n<div>7<\/div>\n<\/td>\n<td>\n<div>\n<div><code>#!\/bin\/bash<\/code><\/div>\n<div><code>#monitor available disk space<\/code><\/div>\n<div><code>SPACE=<\/code><code>'df | sed -n '<\/code><code>\/ \\ \/ $ \/ p<\/code><code>' | gawk '<\/code><code>{print $5}<\/code><code>' | sed\u00a0 '<\/code><code>s\/%<\/code><code>\/\/<\/code><code>'<\/code><\/div>\n<div><code>if<\/code> <code>[ $SPACE -<\/code><code>ge<\/code> <code>90 ]<\/code><\/div>\n<div><code>then<\/code><\/div>\n<div><code>fty89@163.com<\/code><\/div>\n<div><code>fi<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<h3>4\u3001\u00a0\u76d1\u63a7CPU\u548c\u5185\u5b58\u7684\u4f7f\u7528\u60c5\u51b5<\/h3>\n<div>\n<div id=\"highlighter_524302\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>1<\/div>\n<div>2<\/div>\n<div>3<\/div>\n<div>4<\/div>\n<div>5<\/div>\n<div>6<\/div>\n<div>7<\/div>\n<div>8<\/div>\n<div>9<\/div>\n<div>10<\/div>\n<div>11<\/div>\n<div>12<\/div>\n<\/td>\n<td>\n<div>\n<div><code>#!\/bin\/bash<\/code><\/div>\n<div><code>#script\u00a0 to capture system statistics<\/code><\/div>\n<div><code>OUTFILE=<\/code><code>\/home\/xu\/capstats<\/code><code>.csv<\/code><\/div>\n<div><code>DATE=<\/code><code>'date +%m\/%d\/%Y'<\/code><\/div>\n<div><code>TIME=<\/code><code>'date +%k:%m:%s'<\/code><\/div>\n<div><code>TIMEOUT=<\/code><code>'uptime'<\/code><\/div>\n<div><code>VMOUT=<\/code><code>'vmstat 1 2'<\/code><\/div>\n<div><code>\u00a0<\/code><code>USERS=<\/code><code>'echo $TIMEOUT | gawk '<\/code><code>{print $4}<\/code><code>' '<\/code><\/div>\n<div><code>LOAD=<\/code><code>'echo $TIMEOUT | gawk '<\/code><code>{print $9}<\/code><code>' | sed \"s\/,\/\/'<\/code> <code>'<\/code><\/div>\n<div><code>FREE=<\/code><code>'echo $VMOUT | sed -n '<\/code><code>\/[0-9]<\/code><code>\/p<\/code><code>' | sed -n '<\/code><code>2p<\/code><code>' | gawk '<\/code><code>{print $4} <\/code><code>' '<\/code><\/div>\n<div><code>IDLE=<\/code><code>'echo\u00a0 $VMOUT | sed -n '<\/code><code>\/[0-9]<\/code><code>\/p<\/code><code>' | sed -n '<\/code><code>2p<\/code><code>' |gawk '<\/code><code>{print $15}<\/code><code>' '<\/code><\/div>\n<div><code>echo<\/code> <code>\"$DATE,$TIME,$USERS,$LOAD,$FREE,$IDLE\"<\/code> <code>&gt;&gt; $OUTFILE<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<h3>5\u3001\u5168\u65b9\u4f4d\u76d1\u63a7\u4e3b\u673a<\/h3>\n<div>\n<div id=\"highlighter_35722\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>1<\/div>\n<div>2<\/div>\n<div>3<\/div>\n<div>4<\/div>\n<div>5<\/div>\n<div>6<\/div>\n<div>7<\/div>\n<div>8<\/div>\n<div>9<\/div>\n<div>10<\/div>\n<div>11<\/div>\n<div>12<\/div>\n<div>13<\/div>\n<div>14<\/div>\n<div>15<\/div>\n<div>16<\/div>\n<div>17<\/div>\n<div>18<\/div>\n<div>19<\/div>\n<div>20<\/div>\n<div>21<\/div>\n<div>22<\/div>\n<div>23<\/div>\n<div>24<\/div>\n<div>25<\/div>\n<div>26<\/div>\n<div>27<\/div>\n<div>28<\/div>\n<\/td>\n<td>\n<div>\n<div><code>#!\/bin\/bash<\/code><\/div>\n<div><code># check_xu.sh<\/code><\/div>\n<div><code># 0 * * * * \/home\/check_xu.sh<\/code><\/div>\n<div><\/div>\n<div><code>DAT=<\/code><code>\"`date +%Y%m%d`\"<\/code><\/div>\n<div><code>HOUR=<\/code><code>\"`date +%H`\"<\/code><\/div>\n<div><code>DIR=<\/code><code>\"\/home\/oslog\/host_${DAT}\/${HOUR}\"<\/code><\/div>\n<div><code>DELAY=60<\/code><\/div>\n<div><code>COUNT=60<\/code><\/div>\n<div><code># whether the responsible directory exist<\/code><\/div>\n<div><code>if<\/code> <code>! <\/code><code>test<\/code> <code>-d ${DIR}<\/code><\/div>\n<div><code>then<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>\/bin\/mkdir<\/code> <code>-p ${DIR}<\/code><\/div>\n<div><code>fi<\/code><\/div>\n<div><code># general check<\/code><\/div>\n<div><code>export<\/code> <code>TERM=linux<\/code><\/div>\n<div><code>\/usr\/bin\/top<\/code> <code>-b -d ${DELAY} -n ${COUNT} &gt; ${DIR}<\/code><code>\/top_<\/code><code>${DAT}.log 2&gt;&amp;1 &amp;<\/code><\/div>\n<div><code># cpu check<\/code><\/div>\n<div><code>\/usr\/bin\/sar<\/code> <code>-u ${DELAY} ${COUNT} &gt; ${DIR}<\/code><code>\/cpu_<\/code><code>${DAT}.log 2&gt;&amp;1 &amp;<\/code><\/div>\n<div><code>#\/usr\/bin\/mpstat -P 0 ${DELAY} ${COUNT} &gt; ${DIR}\/cpu_0_${DAT}.log 2&gt;&amp;1 &amp;<\/code><\/div>\n<div><code>#\/usr\/bin\/mpstat -P 1 ${DELAY} ${COUNT} &gt; ${DIR}\/cpu_1_${DAT}.log 2&gt;&amp;1 &amp;<\/code><\/div>\n<div><code># memory check<\/code><\/div>\n<div><code>\/usr\/bin\/vmstat<\/code> <code>${DELAY} ${COUNT} &gt; ${DIR}<\/code><code>\/vmstat_<\/code><code>${DAT}.log 2&gt;&amp;1 &amp;<\/code><\/div>\n<div><code># I\/O check<\/code><\/div>\n<div><code>\/usr\/bin\/iostat<\/code> <code>${DELAY} ${COUNT} &gt; ${DIR}<\/code><code>\/iostat_<\/code><code>${DAT}.log 2&gt;&amp;1 &amp;<\/code><\/div>\n<div><code># network check<\/code><\/div>\n<div><code>\/usr\/bin\/sar<\/code> <code>-n DEV ${DELAY} ${COUNT} &gt; ${DIR}<\/code><code>\/net_<\/code><code>${DAT}.log 2&gt;&amp;1 &amp;<\/code><\/div>\n<div><code>#\/usr\/bin\/sar -n EDEV ${DELAY} ${COUNT} &gt; ${DIR}\/net_edev_${DAT}.log 2&gt;&amp;1 &amp;<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>\u653e\u5728crontab\u91cc\u6bcf\u5c0f\u65f6\u81ea\u52a8\u6267\u884c\uff1a<\/p>\n<div>\n<div id=\"highlighter_687847\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>1<\/div>\n<\/td>\n<td>\n<div>\n<div><code>0 * * * * <\/code><code>\/home\/check_xu<\/code><code>.sh<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>\u8fd9\u6837\u4f1a\u5728\/home\/oslog\/host_yyyymmdd\/hh\u76ee\u5f55\u4e0b\u751f\u6210\u5404\u5c0f\u65f6cpu\u3001\u5185\u5b58\u3001\u7f51\u7edc\uff0cIO\u7684\u7edf\u8ba1\u6570\u636e\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u5982\u679c\u67d0\u4e2a\u65f6\u95f4\u6bb5\u4ea7\u751f\u95ee\u9898\u4e86\uff0c\u5c31\u53ef\u4ee5\u53bb\u770b\u5bf9\u5e94\u7684\u65e5\u5fd7\u4fe1\u606f\uff0c\u770b\u770b\u5f53\u65f6\u7684\u4e3b\u673a\u6027\u80fd\u5982\u4f55\u3002<\/p>\n<p>\u51fa\u5904\uff1a<a href=\"http:\/\/www.dbasky.net\/archives\/2009\/10\/shell.html\">http:\/\/www.dbasky.net\/archives\/2009\/10\/shell.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u65f6\u4e0d\u65f6\u6709\u670b\u53cb\u95ee\u6211\u5173\u4e8e\u670d\u52a1\u5668\u76d1\u63a7\u65b9\u9762\u7684\u95ee\u9898\uff0c\u95ee\u5e38\u7528\u7684\u670d\u52a1\u5668\u76d1\u63a7\u9664\u4e86\u7528\u5f00\u6e90\u8f6f\u4ef6\uff0c &hellip; <a href=\"https:\/\/ufqi.com\/blog\/server-monitoring-shell-script\/\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[],"_links":{"self":[{"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/posts\/147"}],"collection":[{"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/comments?post=147"}],"version-history":[{"count":4,"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/posts\/147\/revisions"}],"predecessor-version":[{"id":149,"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/posts\/147\/revisions\/149"}],"wp:attachment":[{"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/media?parent=147"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/categories?post=147"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/tags?post=147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}