運維自動化之使用PHP+MYSQL+SHELL打造私有監控系統(二)

現在開始介紹php+mysql+shell監控系統
1、目的
       此監控系統主要是通過php+mysql+shell的方式,通過shell腳本對各個機器的其各個服務進行監控,達到及時的瞭解其各個應用服務的狀態(如果宕掉與啓動),在檢測應用服務宕掉時,記錄在日誌裏與mysql數據庫服務器裏,同時進行服務宕掉的郵件提示,並自動啓動宕掉的應用服務;在檢測應該服務啓動時,同時進行服務啓動的郵件提示,所有的監控內容都能在php製作的web裏進行瀏覽,同時能根據mysql裏的數據,把資源監控數據視圖化,在瀏覽數據的時候,更方便。
2、實現的機制
       監控方式主要是通過shell腳本的實現,針對應用服務運行的端口進行監控,如果其端口開啓,則證明此服務運行,反之則此服務沒有運行。
3、監控的應用服務
       監控的應用服務有:
A、應用服務器的服務監控
web:http、yu_tomcat、tomcat 共3個
交換: pas、ppas、mas、mmas、cas共5個
引擎:memcache、datastorageservice、http、dbstatserver共4個
B:應用服務器的資源監控
硬盤使用率、cpu使用率、硬件啓動信息錯誤、i/o使用率、15分鐘內的負載、內存使用率(包括內存與swap)、日誌錯誤信息、當前用戶登錄數
4、已經監控的省份
       目前天津、廣西、貴州、海南、河北、河南、湖北、湖南、寧夏、陝西、青海、西藏、新疆、包頭、甘肅、江西都已經部署完成,經過3個月的測試,沒有發現問題。
5shell監控腳本內容與解釋
1)web服務器(以甘肅web爲例)
  1. #!/bin/bash  
  2. #ip  
  3. web_ip=$(/sbin/ifconfig eth0|grep "inet addr"|cut -d : -f 2|awk '{print $1}')  
  4. ##eth0網卡的ip  
  5. #name  
  6. web_name=$(hostname)  
  7. ##web主機名  
  8. monitor_name=web 
  9. ##監控的服務器是什麼類型  
  10. here='gansu' 
  11. ##監控的省份  
  12. #mysql info  
  13. mysql_ip='1.1.1.1' 
  14. ##數據庫的ip  
  15. mysql_username='root' 
  16. ##數據庫的用戶  
  17. mysql_passwd='****' 
  18. #數據庫的密碼  
  19. mysql_database='monitor' 
  20. ##選擇的數據庫  
  21. memory_table=''$here'_memory'  
  22. load_table=''$here'_load'  
  23. io_table=''$here'_io'  
  24. hardware_table=''$here'_hardware'  
  25. message_table=''$here'_message'  
  26. user_table=''$here'_user'  
  27. disk_table=''$here'_disk'  
  28. cpu_table=''$here'_cpu'  
  29. service_table=''$here'_service'  
  30. ##想數據庫裏寫入的表  
  31. #date and log  
  32. day="$(date +%Y%m%d)" 
  33. worklog='/usr/local/monitor/logs/all_work_log' 
  34. downlog='/usr/local/monitor/logs/all_down_log' 
  35. ##日誌與實際  
  36. now="$(date +%Y-%m-%d-%T)" 
  37. #web service  
  38. tomcat="$(netstat -antl|grep 8080|wc -l)" 
  39. web_http="$(ps -ef|grep httpd|grep -v grep|wc -l)" 
  40. yz_tomcat="$(netstat -antl|grep 8081|wc -l)" 
  41. ##通過端口與服務在後頭的運行情況監控其運行狀態  
  42. #cpu service  
  43. alert_cpu='80' 
  44. ##cpu的報警閥值  
  45. web_cpu_idle="$(top -b -n 1 | grep Cpu | awk '{print $5}' | cut -f 1 -d .)" 
  46. web_cpu="$(echo 100 - $web_cpu_idle|/usr/bin/bc)" 
  47. ##監控cpu的使用率  
  48. #memory service  
  49. alert_mem='100' 
  50. ##內存的報警閥值  
  51. web_mem="$(/usr/bin/free -m|grep Mem|awk '{print $4}')" 
  52. web_swap="$(/usr/bin/free -m|grep Swap|awk '{print $3}')" 
  53. ##memory與swap的使用率  
  54. alert_swap='0' 
  55. ##swap的報警閥值  
  56. #memory log  
  57. memory_worklog='/usr/local/monitor/logs/mem_work_log' 
  58. memory_downlog='/usr/local/monitor/logs/mem_down_log' 
  59. #load service  
  60. cpu_count="$(grep -c 'model name' /proc/cpuinfo)" 
  61. alert_load="$(echo $cpu_count/2|/usr/bin/bc)" 
  62. web_load="$(uptime|awk '{print $NF}'|cut -f 1 -d .)" 
  63. web_load_15="$(uptime|awk '{print $NF}')" 
  64. #監控負載的使用率  
  65. #load log  
  66. load_worklog='/usr/local/monitor/logs/load_work_log' 
  67. load_downlog='/usr/local/monitor/logs/load_down_log' 
  68. #io service  
  69. alert_io='80' 
  70. web_io_idle_back="$(/usr/bin/iostat|awk 'NR==4{print $NF}'|cut -f 1 -d .)" 
  71. web_io_idle="$(echo 100 - $web_io_idle_back|/usr/bin/bc)" 
  72. ##io的使用值  
  73. #io log  
  74. io_worklog='/usr/local/monitor/logs/io_work_log' 
  75. io_downlog='/usr/local/monitor/logs/io_down_log' 
  76. #hardware service  
  77. web_hardware_error="$(dmesg|grep -i error|wc -l)" 
  78. web_info_error="$(dmesg|grep -i error)" 
  79. ##開機啓動的錯誤信息  
  80. #hardware error log  
  81. hard_worklog='/usr/local/monitor/logs/hard_work_log' 
  82. hard_downlog='/usr/local/monitor/logs/hard_down_log' 
  83. #message service  
  84. web_message_error_count="$(awk '/"$(date +%Y%m%d)"/' /var/log/messages|grep -i error|wc -l)"  
  85. web_message_error="$(awk '/"$(date +%Y%m%d)"/' /var/log/messages|grep -i error)"  
  86. ##日誌的錯誤信息  
  87. #message error log  
  88. message_worklog='/usr/local/monitor/logs/message_work_log' 
  89. message_downlog='/usr/local/monitor/logs/message_down_log' 
  90. #user service  
  91. web_user_count="$(/usr/bin/who|wc -l)" 
  92. web_user_info="$(/usr/bin/who)" 
  93. ##當前登錄的用戶值  
  94. #user  log  
  95. user_worklog='/usr/local/monitor/logs/user_work_log' 
  96. user_downlog='/usr/local/monitor/logs/user_down_log' 
  97. #disk service  
  98. alert_disk='80' 
  99. web_disk="$(/bin/df -H|sort -k5nr|grep -vE 'Filesystem|tmpfs|cdrom'|awk '{print $5 " " $1}')"  
  100. web_disk_use=$(echo $web_disk|awk '{print $1}'|cut -d '%' -f1)  
  101. web_disk_partition=$(echo $web_disk|awk '{print $2}')  
  102. #硬盤使用率  
  103. #disk log  
  104. disk_worklog='/usr/local/monitor/logs/disk_work_log' 
  105. disk_downlog='/usr/local/monitor/logs/disk_down_log' 
  106. #cpu log  
  107. cpu_worklog='/usr/local/monitor/logs/cpu_work_log' 
  108. cpu_downlog='/usr/local/monitor/logs/cpu_down_log' 
  109. #notification mail  
  110. email='[email protected]' 
  111. ##報警的接收人  
  112. #monitor  
  113. if [ ! -d "$loghere" ];then  
  114.     mkdir $loghere  
  115. fi  
  116. #check web service  
  117. if [ $web_http -ge 1 ];then  
  118.     echo "$now ShengFen: $here Server: $web_name Ip: $web_ip Service: http Monitor_Server: $monitor_name is working" >> $worklog-$day  
  119.     /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $service_table values ('','$here','$web_name','$web_ip','$monitor_name','web_http','working',now())";  
  120. else  
  121.     /etc/init.d/httpd start  
  122.     echo "$now ShengFen: $here Server: $web_name Ip: $web_ip Service: http Monitor_Server: $monitor_name is down"|/bin/mail -s "Notification ShengFen: $here Server: $web_name Service:  http  was a problem" $email  
  123.     echo "$now ShengFen: $here Server: $web_name Ip: $web_ip Service: http Monitor_Server: $montior_name is down" >> $downlog-$day  
  124.     /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $service_table values ('','$here','$web_name','$web_ip','$monitor_name','web_http','downing',now())";  
  125. fi  
  126.  
  127. if [ $tomcat -ge 1 ];then  
  128.     echo "$now ShengFen: $here Server: $web_name Ip: $web_ip Service: tomcat Monitor_Server: $monitor_name is working" >> $worklog-$day  
  129.     /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $service_table values ('','$here','$web_name','$web_ip','$monitor_name','tomcat','working',now())";  
  130. else  
  131.     /usr/local/monitor/shell/web_tomcat.sh  
  132.   echo "$now ShengFen: $here Server: $web_name Ip: $web_ip Service: tomcat Monitor_Server: $monitor_name is down"|/bin/mail -s "Notification ShengFen: $here Server: $web_name Service:  tomcat  was a problem" $email   
  133.   echo "$now ShengFen: $here Server: $web_name Ip: $web_ip Service: tomcat Monitor_Server: $montior_name is down" >> $downlog-$day  
  134.     /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $service_table values ('','$here','$web_name','$web_ip','$monitor_name','tomcat','downing',now())";  
  135. fi  
  136. if [ $yz_tomcat -ge 1 ];then  
  137.     echo "$now ShengFen: $here Server: $web_name Service: Ip: $web_ip yz_tomcat Monitor_Server: $monitor_name is working" >> $worklog-$day  
  138.     /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $service_table values ('','$here','$web_name','$web_ip','$monitor_name','yz_tomcat','working',now())";  
  139. else  
  140.     /usr/local/monitor/shell/web_yz_tomcat.sh  
  141.   echo "$now ShengFen: $here Server: $web_name Service: Ip: $web_ip yz_tomcat Monitor_Server: $monitor_name is down"|/bin/mail -s "Notification ShengFen: $here Server: $web_name Service:  yz_tomcat  was a problem" $email   
  142.   echo "$now ShengFen: $here Server: $web_name Service: Ip: $web_ip yz_tomcat Monitor_Server: $montior_name is down" >> $downlog-$day  
  143.     /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $service_table values ('','$here','$web_name','$web_ip','$monitor_name','yz_tomcat','downing',now())";  
  144. fi  
  145. #check cpu_idle  
  146. if [ $web_cpu -ge $alert_cpu ];then  
  147.     echo "$now ShengFen: $here Server: $web_name Ip: $web_ip Service: cpu_idle Monitor_Server: $monitor_name Cpu_use: $web_cpu"|/bin/mail -s "Notification ShengFen: $here Server: $web_name Service: cpu_use  was Exceed Threshold value: 80%" $email  
  148.     echo "$now ShengFen: $here Server: $web_name Ip: $web_ip Service: cpu_idle Monitor_Server: $monitor_name Cpu_use: $web_cpu" >> $cpu_downlog-$day  
  149.     /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $cpu_table values ('','$here','$web_name','$web_ip','$monitor_name','cpu_use','$alert_cpu','$web_cpu','abnormal',now())";  
  150. else  
  151.     echo "$now ShengFen: $here Server: $web_name Ip: $web_ip Service: cpu_idle Monitor_Server: $monitor_name Cpu_use: $web_cpu" >> $cpu_worklog-$day  
  152.     /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $cpu_table values ('','$here','$web_name','$web_ip','$monitor_name','cpu_use','$alert_cpu','$web_cpu','normal',now())";  
  153. fi  
  154. #check memory  
  155. if [ $web_mem -le $alert_mem ];then  
  156.         echo "$now ShengFen: $here Server: $web_name Ip: $web_ip Service: memory Monitor_Server: $monitor_name Free_mem: $web_mem"|/bin/mail -s "Notification ShengFen: $here Server: $web_name Service: memory  was Exceed Threshold value" $email  
  157.         echo "$now ShengFen: $here Server: $web_name Ip: $web_ip Service: memory Monitor_Server: $monitor_name Free_mem: $web_mem" >> $memory_downlog-$day  
  158.         /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $memory_table values ('','$here','$web_name','$web_ip','$monitor_name','memory','$alert_mem','$web_mem','abnormal',now())";  
  159. else  
  160.         echo "$now ShengFen: $here Server: $web_name Ip: $web_ip Service: memory Monitor_Server: $monitor_name Free_mem: $web_mem" >> $memory_worklog-$day  
  161.         /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $memory_table values ('','$here','$web_name','$web_ip','$monitor_name','memory','$alert_mem','$web_mem','normal',now())";  
  162. fi  
  163. #check swap  
  164. if [ $web_swap -gt $alert_swap ];then  
  165.         echo "$now ShengFen: $here Server: $web_name Ip: $web_ip Service: swap Monitor_Server: $monitor_name Swap_web: $web_swap"|/bin/mail -s "Notification ShengFen: $here Server: $web_name Service: swap Ip: $web_ip was Exceed Threshold value" $email  
  166.         echo "$now ShengFen: $here Server: $web_name Ip: $web_ip Service: swap Monitor_Server: $monitor_name Swap_web: $web_swap" >> $memory_downlog-$day  
  167.         /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database  -e "insert into $memory_table values ('','$here','$web_name','$web_ip','$monitor_name','swap','$alert_swap','$web_swap','abnormal',now())";  
  168. else  
  169.         echo "$now ShengFen: $here Server: $web_name Ip: $web_ip Service: swap Monitor_Server: $monitor_name Swap_web: $web_swap" >> $memory_worklog-$day  
  170.         /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $memory_table values ('','$here','$web_name','$web_ip','$monitor_name','swap','$alert_swap','$web_swap','normal',now())";  
  171. fi  
  172. #check load_15  
  173. if [ $web_load -ge $alert_load ];then  
  174.         echo "$now ShengFen: $here Server: $web_name Ip: $web_ip Service: load_15 Monitor_Server: $monitor_name Load_use: $web_load_15"|/bin/mail -s "Notification ShengFen: $here Server: $web_name Service: load_15 Ip: $web_ip was Exceed Threshold value" $email  
  175.         echo "$now ShengFen: $here Server: $web_name Ip: $web_ip Service: load_15 Monitor_Server: $monitor_name Load_use: $web_load_15" >> $load_downlog-$day  
  176.         /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $load_table values ('','$here','$web_name','$web_ip','$monitor_name','load_15','$alert_load','$web_load_15','abnormal',now())";  
  177. else  
  178.         echo "$now ShengFen: $here Server: $server Ip: $ip Service: load Monitor_Server: $monitor_name Load_use: $web_load_15" >> $load_worklog-$day  
  179.         /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $load_table values ('','$here','$web_name','$web_ip','$monitor_name','load_15','$alert_load','$web_load_15','normal',now())";  
  180. fi  
  181. #check io_idle  
  182. if [ $web_io_idle -ge $alert_io ];then  
  183.         echo "$now ShengFen: $here Server: $web_name Ip: $web_ip Service: io_idle Monitor_Server: $monitor_name Io_use: $web_io_idle"|/bin/mail -s "Notification ShengFen: $here Server: $web_name Service: io_use  was Exceed Threshold value: 80%" $email  
  184.         echo "$now ShengFen: $here Server: $web_name Ip: $web_ip Service: io_idle Monitor_Server: $monitor_name Io_use: $web_io_idle" >> $io_downlog-$day  
  185.         /usr/bin/mysql  -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $io_table values ('','$here','$web_name','$web_ip','$monitor_name','io_use','$alert_io','$web_io_idle','abnormal',now())";  
  186. else  
  187.         echo "$now ShengFen: $here Server: $web_name Ip: $web_ip Service: io_idle Monitor_Server: $monitor_name Io_use: $web_io_idle" >> $io_worklog-$day  
  188.         /usr/bin/mysql  -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $io_table values ('','$here','$web_name','$web_ip','$monitor_name','io_use','$alert_io','$web_io_idle','normal',now())";  
  189. fi  
  190. #check hareware error info  
  191. if [ $web_hardware_error -gt 0 ];then  
  192.         echo "$now ShengFen: $here Server: $web_name Ip: $web_ip Service: hardware_error Monitor_Server: $monitor_name Error: $web_info_error"|/bin/mail -s "Notification ShengFen: $here Server: $web_name Service: hardware_error  were some hardware imformation error" $email  
  193.         echo "$now ShengFen: $here Server: $web_name Ip: $web_ip Service: hardware_error Monitor_Server: $monitor_name Error: $web_info_error" >> $hard_downlog-$day  
  194.         /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $hardware_table values ('','$here','$web_name','$web_ip','$monitor_name','hardware_error','0','$web_hardware_error','abnormal',now())";  
  195. else  
  196.         echo "$now ShengFen: $here Server: $web_name Ip: $web_ip Service: hardware_error Monitor_Server: $monitor_name Error: Nothing" >> $hard_worklog-$day  
  197.         /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $hardware_table values ('','$here','$web_name','$web_ip','$monitor_name','hardware_error','0','$web_hardware_error','normal',now())";  
  198. fi  
  199. #check message error  
  200. if [ $web_message_error_count -ge 1 ];then  
  201.         echo "$now ShengFen: $here Server: $web_name Ip: $web_ip Service: message_error Monitor_Server: $monitor_name Message_error: $web_message_error"|/bin/mail -s "Notification ShengFen: $here Server: $web_name Service: message_error  were some message imformation error" $email  
  202.         echo "$now ShengFen: $here Server: $web_name Ip: $web_ip Service: message_error Monitor_Server: $monitor_name Message_error: $web_message_error" >> $message_downlog-$day  
  203.         /usr/bin/mysql-h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $message_table values ('','$here','$web_name','$web_ip','$monitor_name','message_error','1','$web_message_error_count','abnormal',now())";  
  204. else  
  205.         echo "$now ShengFen: $here Server: $web_name Ip: $web_ip Service: message_error Monitor_Server: $monitor_name Message_error: Nothing" >> $message_worklog-$day  
  206.         /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database  -e "insert into $message_table values ('','$here','$web_name','$web_ip','$monitor_name','message_error','1','$web_message_error_count','normal',now())";  
  207. fi  
  208. #check user  
  209. if [ $web_user_count -ge 3 ];then  
  210.         echo "$now ShengFen: $here Server: $web_name Ip: $web_ip Service: user Monitor_Server: $monitor_name User: $web_user_info"|/bin/mail -s "Notification ShengFen: $here Server: $web_name  Service: user  was Exceed Threshold value: 3" $email  
  211.         echo "$now ShengFen: $here Server: $web_name Ip: $web_ip Service: user Monitor_Server: $monitor_name User: $web_user_info" >> $user_downlog-$day  
  212.         /usr/bin/mysql  -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $user_table values ('','$here','$web_name','$web_ip','$monitor_name','user','3','$web_user_count','abnormal',now())";  
  213. else  
  214.         echo "$now ShengFen: $here Server: $web_name Ip: $web_ip Service: user Monitor_Server: $monitor_name User: normal" >> $user_worklog-$day  
  215.         /usr/bin/mysql  -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $user_table values ('','$here','$web_name','$web_ip','$monitor_name','user','3','$web_user_count','normal',now())";  
  216. fi  
  217. #check disk  
  218. if [ $web_disk_use -ge $alert_disk ];then  
  219.         echo "$now ShengFen: $here Server: $web_name Ip: $web_ip Service: disk_use Monitor_Server: $monitor_name Disk_use: $web_disk_partition ($web_disk_use%)"|/bin/mail -s "Warning!!! $here Server: $web_name Service: disk_use  was Exceed Threshold value : $alert_disk% " $email  
  220.         echo "$now ShengFen: $here Server: $web_name Ip: $web_ip Service: disk_use Monitor_Server: $monitor_name Disk_use: $web_disk_partion ($web_disk_use%)" >> $disk_downlog-$day  
  221.         /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database  -e "insert into $disk_table values ('','$here','$web_name','$web_ip','$monitor_name','disk_use','$alert_disk','$web_disk_partition','$web_disk_use','abnormal',now())";  
  222. else  
  223.         echo "$now ShengFen: $here Server: $web_name Ip: $web_ip Service: disk_use Monitor_Server: $monitor_name Disk_use: $web_disk_partition ($web_disk_use%)" >> $disk_worklog-$day  
  224.         /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database  -e "insert into $disk_table values ('','$here','$web_name','$web_ip','$monitor_name','disk_use','$alert_disk','$web_disk_partition','$web_disk_use','normal',now())";  
  225. fi  
2)交換服務器(以甘肅交換爲例,解釋內容參照甘肅web)
  1. #!/bin/bash  
  2. #ip  
  3. jh_ip=$(/sbin/ifconfig eth0|grep "inet addr"|cut -d : -f 2|awk '{print $1}')  
  4. #name  
  5. jh_name=$(hostname)  
  6. monitor_name=jiaohuan 
  7. here='gansu' 
  8. #mysql info  
  9. mysql_ip='1.1.1.1' 
  10. mysql_username='root' 
  11. mysql_passwd='****' 
  12. mysql_database='monitor' 
  13. memory_table=''$here'_memory'  
  14. load_table=''$here'_load'  
  15. io_table=''$here'_io'  
  16. hardware_table=''$here'_hardware'  
  17. message_table=''$here'_message'  
  18. user_table=''$here'_user'  
  19. disk_table=''$here'_disk'  
  20. cpu_table=''$here'_cpu'  
  21. service_table=''$here'_service'  
  22. #date and log  
  23. day="$(date +%Y%m%d)" 
  24. worklog='/usr/local/monitor/logs/all_work_log' 
  25. downlog='/usr/local/monitor/logs/all_down_log' 
  26. now="$(date +%Y-%m-%d-%T)" 
  27. loghere='/usr/local/monitor/logs' 
  28. #jiaohuan service  
  29. pas="$(ps -ef|grep pas|grep -v grep|grep -v ppas|wc -l)" 
  30. ppas="$(ps -ef|grep ppas|grep -v grep|wc -l)" 
  31. cas="$(ps -ef|grep cas|grep -v grep|wc -l)" 
  32. mas="$(ps -ef|grep mas|grep -v grep|grep -v mmas|wc -l)" 
  33. mmas="$(ps -ef|grep mmas|grep -v grep|wc -l)" 
  34. #cpu service  
  35. alert_cpu='80' 
  36. jh_cpu_idle="$(top -b -n 1 | grep Cpu | awk '{print $5}' | cut -f 1 -d .)" 
  37. jh_cpu="$(echo 100 - $jh_cpu_idle|/usr/bin/bc)" 
  38. #memory service  
  39. alert_mem='100' 
  40. jh_mem="$(/usr/bin/free -m|grep Mem|awk '{print $4}')" 
  41. jh_swap="$(/usr/bin/free -m|grep Swap|awk '{print $3}')" 
  42. alert_swap='0' 
  43. #memory log  
  44. memory_worklog='/usr/local/monitor/logs/mem_work_log' 
  45. memory_downlog='/usr/local/monitor/logs/mem_down_log' 
  46. #load service  
  47. cpu_count="$(grep -c 'model name' /proc/cpuinfo)" 
  48. alert_load="$(echo $cpu_count/2|/usr/bin/bc)" 
  49. jh_load="$(uptime|awk '{print $NF}'|cut -f 1 -d .)" 
  50. jh_load_15="$(uptime|awk '{print $NF}')" 
  51. #load log  
  52. load_worklog='/usr/local/monitor/logs/load_work_log' 
  53. load_downlog='/usr/local/monitor/logs/load_down_log' 
  54. #io service  
  55. alert_io='80' 
  56. jh_io_idle_back="$(/usr/bin/iostat|awk 'NR==4{print $NF}'|cut -f 1 -d .)" 
  57. jh_io_idle="$(echo 100 - $jh_io_idle_back|/usr/bin/bc)" 
  58. #io log  
  59. io_worklog='/usr/local/monitor/logs/io_work_log' 
  60. io_downlog='/usr/local/monitor/logs/io_down_log' 
  61. #hardware service  
  62. jh_hardware_error="$(dmesg|grep -i error|wc -l)" 
  63. jh_info_error="$(dmesg|grep -i error)" 
  64. #hardware error log  
  65. hard_worklog='/usr/local/monitor/logs/hard_work_log' 
  66. hard_downlog='/usr/local/monitor/logs/hard_down_log' 
  67. #message service  
  68. jh_message_error_count="$(awk '/"$(date +%Y%m%d)"/' /var/log/messages|grep -i error|wc -l)"  
  69. jh_message_error="$(awk '/"$(date +%Y%m%d)"/' /var/log/messages|grep -i error)"  
  70. #message error log  
  71. message_worklog='/usr/local/monitor/logs/message_work_log' 
  72. message_downlog='/usr/local/monitor/logs/message_down_log' 
  73. #user service  
  74. jh_user_count="$(/usr/bin/who|wc -l)" 
  75. jh_user_info="$(/usr/bin/who)" 
  76. #user  log  
  77. user_worklog='/usr/local/monitor/logs/user_work_log' 
  78. user_downlog='/usr/local/monitor/logs/user_down_log' 
  79. #disk service  
  80. alert_disk='80' 
  81. jh_disk="$(/bin/df -H|sort -k5nr|grep -vE 'Filesystem|tmpfs|cdrom'|awk '{print $5 " " $1}')"  
  82. jh_disk_use=$(echo $jh_disk|awk '{print $1}'|cut -d '%' -f1)  
  83. jh_disk_partition=$(echo $jh_disk|awk '{print $2}')  
  84. #disk log  
  85. disk_worklog='/usr/local/monitor/logs/disk_work_log' 
  86. disk_downlog='/usr/local/monitor/logs/disk_down_log' 
  87. #cpu log  
  88. cpu_worklog='/usr/local/monitor/logs/cpu_work_log' 
  89. cpu_downlog='/usr/local/monitor/logs/cpu_down_log' 
  90. #notification mail  
  91. email='[email protected]' 
  92. #monitor  
  93. if [ ! -d "$loghere" ];then  
  94.     mkdir $loghere  
  95. fi  
  96. #monitor  
  97. #jiaohuan check  
  98. if [ $pas -ge 1 ];then  
  99.         echo "$now ShengFen: $here Server: $jh_name Ip: $jh_ip Service: pas Monitor_Server: $monitor_name is working" >> $worklog-$day  
  100.     /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $service_table values ('','$here','$jh_name','$jh_ip','$monitor_name','pas','working',now())";  
  101. else  
  102.                 /usr/local/lbs/bin4.0.7.7/pas -daemon  
  103.         echo "$now ShengFen: $here Server: $jh_name Ip: $jh_ip Service: pas Monitor_Server: $monitor_name is down"|/bin/mail -s "Notification ShengFen: $here Server: $jh_name Service:  pas  was a problem" $email  
  104.         echo "$now ShengFen: $here Server: $jh_name Ip: $jh_ip Service: pas Monitor_Server: $montior_name is down" >> $downlog-$day  
  105.     /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $service_table values ('','$here','$jh_name','$jh_ip','$monitor_name','pas','downing',now())";  
  106. fi  
  107. if [ $ppas -ge 1 ];then  
  108.         echo "$now ShengFen: $here Server: $jh_name Ip: $jh_ip Service: ppas Monitor_Server: $monitor_name is working" >> $worklog-$day  
  109.     /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $service_table values ('','$here','$jh_name','$jh_ip','$monitor_name','ppas','working',now())";  
  110. else  
  111.                 /usr/local/lbs/bin4.0.7.7/ppas -daemon  
  112.         echo "$now ShengFen: $here Server: $jh_name Ip: $jh_ip Service: ppas Monitor_Server: $monitor_name is down"|/bin/mail -s "Notification ShengFen: $here Server: $jh_name Service:  ppas  was a problem" $email  
  113.         echo "$now ShengFen: $here Server: $jh_name Ip: $jh_ip Service: ppas Monitor_Server: $montior_name is down" >> $downlog-$day  
  114.     /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $service_table values ('','$here','$jh_name','$jh_ip','$monitor_name','ppas','downing',now())";  
  115. fi  
  116. if [ $mas -ge 1 ];then  
  117.         echo "$now ShengFen: $here Server: $jh_name Ip: $jh_ip Service: mas Monitor_Server: $monitor_name is working" >> $worklog-$day  
  118.     /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $service_table values ('','$here','$jh_name','$jh_ip','$monitor_name','mas','working',now())";  
  119. else  
  120.                 /usr/local/lbs/bin4.0.7.7/mas -daemon  
  121.         echo "$now ShengFen: $here Server: $jh_name Ip: $jh_ip Service: mas Monitor_Server: $monitor_name is down"|/bin/mail -s "Notification ShengFen: $here Server: $jh_name Service:  mas  was a problem" $email  
  122.         echo "$now ShengFen: $here Server: $jh_name Ip: $jh_ip Service: mas Monitor_Server: $montior_name is down" >> $downlog-$day  
  123.     /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $service_table values ('','$here','$jh_name','$jh_ip','$monitor_name','mas','downing',now())";  
  124. fi  
  125. if [ $mmas -ge 1 ];then  
  126.         echo "$now ShengFen: $here Server: $jh_name Ip: $jh_ip Service: mmas Monitor_Server: $monitor_name is working" >> $worklog-$day  
  127.     /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $service_table values ('','$here','$jh_name','$jh_ip','$monitor_name','mmas','working',now())";  
  128. else  
  129.                 /usr/local/lbs/bin4.0.7.7/mmas -daemon  
  130.         echo "$now ShengFen: $here Server: $jh_name Ip: $jh_ip Service: mmas Monitor_Server: $monitor_name is down"|/bin/mail -s "Notification ShengFen: $here Server: $jh_name Service:  mmas  was a problem" $email  
  131.         echo "$now ShengFen: $here Server: $jh_name Ip: $jh_ip Service: mmas Monitor_Server: $montior_name is down" >> $downlog-$day  
  132.     /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $service_table values ('','$here','$jh_name','$jh_ip','$monitor_name','mmas','downing',now())";  
  133. fi  
  134. if [ $cas -ge 1 ];then  
  135.         echo "$now ShengFen: $here Server: $jh_name Ip: $jh_ip Service: cas Monitor_Server: $monitor_name is working" >> $worklog-$day  
  136.     /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $service_table values ('','$here','$jh_name','$jh_ip','$monitor_name','cas','working',now())";  
  137. else  
  138.                 /usr/local/lbs/bin4.0.7.7/cas -daemon  
  139.         echo "$now ShengFen: $here Server: $jh_name Ip: $jh_ip Service: cas Monitor_Server: $monitor_name is down"|/bin/mail -s "Notification ShengFen: $here Server: $jh_name Service:  cas  was a problem" $email  
  140.         echo "$now ShengFen: $here Server: $jh_name Ip: $jh_ip Service: cas Monitor_Server: $montior_name is down" >> $downlog-$day  
  141.     /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $service_table values ('','$here','$jh_name','$jh_ip','$monitor_name','cas','downing',now())";  
  142. fi  
  143. #check cpu_idle  
  144. if [ $jh_cpu -ge $alert_cpu ];then  
  145.     echo "$now ShengFen: $here Server: $jh_name Ip: $jh_ip Service: cpu_idle Monitor_Server: $monitor_name Cpu_use: $jh_cpu"|/bin/mail -s "Notification ShengFen: $here Server: $jh_name Service: cpu_use  was Exceed Threshold value: 80%" $email  
  146.     echo "$now ShengFen: $here Server: $jh_name Ip: $jh_ip Service: cpu_idle Monitor_Server: $monitor_name Cpu_use: $jh_cpu" >> $cpu_downlog-$day  
  147.     /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $cpu_table values ('','$here','$jh_name','$jh_ip','$monitor_name','cpu_use','$alert_cpu','$jh_cpu','abnormal',now())";  
  148. else  
  149.     echo "$now ShengFen: $here Server: $jh_name Ip: $jh_ip Service: cpu_idle Monitor_Server: $monitor_name Cpu_use: $jh_cpu" >> $cpu_worklog-$day  
  150.     /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $cpu_table values ('','$here','$jh_name','$jh_ip','$monitor_name','cpu_use','$alert_cpu','$jh_cpu','normal',now())";  
  151. fi  
  152. #check memory  
  153. if [ $jh_mem -le $alert_mem ];then  
  154.         echo "$now ShengFen: $here Server: $jh_name Ip: $jh_ip Service: memory Monitor_Server: $monitor_name Free_mem: $jh_mem"|/bin/mail -s "Notification ShengFen: $here Server: $jh_name Service: memory  was Exceed Threshold value" $email  
  155.         echo "$now ShengFen: $here Server: $jh_name Ip: $jh_ip Service: memory Monitor_Server: $monitor_name Free_mem: $jh_mem" >> $memory_downlog-$day  
  156.         /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $memory_table values ('','$here','$jh_name','$jh_ip','$monitor_name','memory','$alert_mem','$jh_mem','abnormal',now())";  
  157. else  
  158.         echo "$now ShengFen: $here Server: $jh_name Ip: $jh_ip Service: memory Monitor_Server: $monitor_name Free_mem: $jh_mem" >> $memory_worklog-$day  
  159.         /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $memory_table values ('','$here','$jh_name','$jh_ip','$monitor_name','memory','$alert_mem','$jh_mem','normal',now())";  
  160. fi  
  161. #check swap  
  162. if [ $jh_swap -gt $alert_swap ];then  
  163.         echo "$now ShengFen: $here Server: $jh_name Ip: $jh_ip Service: swap Monitor_Server: $monitor_name Swap_jh: $jh_swap"|/bin/mail -s "Notification ShengFen: $here Server: $jh_name Service: swap Ip: $jh_ip was Exceed Threshold value" $email  
  164.         echo "$now ShengFen: $here Server: $jh_name Ip: $jh_ip Service: swap Monitor_Server: $monitor_name Swap_jh: $jh_swap" >> $memory_downlog-$day  
  165.         /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database  -e "insert into $memory_table values ('','$here','$jh_name','$jh_ip','$monitor_name','swap','$alert_swap','$jh_swap','abnormal',now())";  
  166. else  
  167.         echo "$now ShengFen: $here Server: $jh_name Ip: $jh_ip Service: swap Monitor_Server: $monitor_name Swap_jh: $jh_swap" >> $memory_worklog-$day  
  168.         /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $memory_table values ('','$here','$jh_name','$jh_ip','$monitor_name','swap','$alert_swap','$jh_swap','normal',now())";  
  169. fi  
  170. #check load_15  
  171. if [ $jh_load -ge $alert_load ];then  
  172.         echo "$now ShengFen: $here Server: $jh_name Ip: $jh_ip Service: load_15 Monitor_Server: $monitor_name Load_use: $jh_load_15"|/bin/mail -s "Notification ShengFen: $here Server: $jh_name Service: load_15 Ip: $jh_ip was Exceed Threshold value" $email   
  173.         echo "$now ShengFen: $here Server: $jh_name Ip: $jh_ip Service: load_15 Monitor_Server: $monitor_name Load_use: $jh_load_15" >> $load_downlog-$day  
  174.         /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $load_table values ('','$here','$jh_name','$jh_ip','$monitor_name','load_15','$alert_load','$jh_load_15','abnormal',now())";  
  175. else  
  176.         echo "$now ShengFen: $here Server: $server Ip: $ip Service: load Monitor_Server: $monitor_name Load_use: $jh_load_15" >> $load_worklog-$day  
  177.         /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $load_table values ('','$here','$jh_name','$jh_ip','$monitor_name','load_15','$alert_load','$jh_load_15','normal',now())";  
  178. fi  
  179. #check io_idle  
  180. if [ $jh_io_idle -ge $alert_io ];then  
  181.         echo "$now ShengFen: $here Server: $jh_name Ip: $jh_ip Service: io_idle Monitor_Server: $monitor_name Io_use: $jh_io_idle"|/bin/mail -s "Notification ShengFen: $here Server: $jh_name Service: io_use  was Exceed Threshold value: 80%" $email  
  182.         echo "$now ShengFen: $here Server: $jh_name Ip: $jh_ip Service: io_idle Monitor_Server: $monitor_name Io_use: $jh_io_idle" >> $io_downlog-$day  
  183.         /usr/bin/mysql  -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $io_table values ('','$here','$jh_name','$jh_ip','$monitor_name','io_use','$alert_io','$jh_io_idle','abnormal',now())";  
  184. else  
  185.         echo "$now ShengFen: $here Server: $jh_name Ip: $jh_ip Service: io_idle Monitor_Server: $monitor_name Io_use: $jh_io_idle" >> $io_worklog-$day  
  186.         /usr/bin/mysql  -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $io_table values ('','$here','$jh_name','$jh_ip','$monitor_name','io_use','$alert_io','$jh_io_idle','normal',now())";  
  187. fi  
  188. #check hareware error info  
  189. if [ $jh_hardware_error -gt 0 ];then  
  190.         echo "$now ShengFen: $here Server: $jh_name Ip: $jh_ip Service: hardware_error Monitor_Server: $monitor_name Error: $jh_info_error"|/bin/mail -s "Notification ShengFen: $here Server: $jh_name Service: hardware_error  were some hardware imformation error" $email  
  191.         echo "$now ShengFen: $here Server: $jh_name Ip: $jh_ip Service: hardware_error Monitor_Server: $monitor_name Error: $jh_info_error" >> $hard_downlog-$day  
  192.         /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $hardware_table values ('','$here','$jh_name','$jh_ip','$monitor_name','hardware_error','0','$jh_hardware_error','abnormal',now())";  
  193. else  
  194.         echo "$now ShengFen: $here Server: $jh_name Ip: $jh_ip Service: hardware_error Monitor_Server: $monitor_name Error: Nothing" >> $hard_worklog-$day  
  195.         /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $hardware_table values ('','$here','$jh_name','$jh_ip','$monitor_name','hardware_error','0','$jh_hardware_error','normal',now())";  
  196. fi  
  197. #check message error  
  198. if [ $jh_message_error_count -ge 1 ];then  
  199.         echo "$now ShengFen: $here Server: $jh_name Ip: $jh_ip Service: message_error Monitor_Server: $monitor_name Message_error: $jh_message_error"|/bin/mail -s "Notification ShengFen: $here Server: $jh_name Service: message_error  were some message imformation error" $email  
  200.         echo "$now ShengFen: $here Server: $jh_name Ip: $jh_ip Service: message_error Monitor_Server: $monitor_name Message_error: $jh_message_error" >> $message_downlog-$day  
  201.         /usr/bin/mysql-h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $message_table values ('','$here','$jh_name','$jh_ip','$monitor_name','message_error','1','$jh_message_error_count','abnormal',now())";  
  202. else  
  203.         echo "$now ShengFen: $here Server: $jh_name Ip: $jh_ip Service: message_error Monitor_Server: $monitor_name Message_error: Nothing" >> $message_worklog-$day  
  204.         /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database  -e "insert into $message_table values ('','$here','$jh_name','$jh_ip','$monitor_name','message_error','1','$jh_message_error_count','normal',now())";  
  205. fi  
  206. #check user  
  207. if [ $jh_user_count -ge 3 ];then  
  208.         echo "$now ShengFen: $here Server: $jh_name Ip: $jh_ip Service: user Monitor_Server: $monitor_name User: $jh_user_info"|/bin/mail -s "Notification ShengFen: $here Server: $jh_name  Service: user  was Exceed Threshold value: 3" $email  
  209.         echo "$now ShengFen: $here Server: $jh_name Ip: $jh_ip Service: user Monitor_Server: $monitor_name User: $jh_user_info" >> $user_downlog-$day  
  210.         /usr/bin/mysql  -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $user_table values ('','$here','$jh_name','$jh_ip','$monitor_name','user','3','$jh_user_count','abnormal',now())";  
  211. else  
  212.         echo "$now ShengFen: $here Server: $jh_name Ip: $jh_ip Service: user Monitor_Server: $monitor_name User: normal" >> $user_worklog-$day  
  213.         /usr/bin/mysql  -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $user_table values ('','$here','$jh_name','$jh_ip','$monitor_name','user','3','$jh_user_count','normal',now())";  
  214. fi  
  215. #check disk  
  216. if [ $jh_disk_use -ge $alert_disk ];then  
  217.         echo "$now ShengFen: $here Server: $jh_name Ip: $jh_ip Service: disk_use Monitor_Server: $monitor_name Disk_use: $jh_disk_partition ($jh_disk_use%)"|/bin/mail -s "Warning!!! $here Server: $jh_name Service: disk_use  was Exceed Threshold value : $alert_disk% " $email  
  218.         echo "$now ShengFen: $here Server: $jh_name Ip: $jh_ip Service: disk_use Monitor_Server: $monitor_name Disk_use: $jh_disk_partion ($jh_disk_use%)" >> $disk_downlog-$day  
  219.         /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database  -e "insert into $disk_table values ('','$here','$jh_name','$jh_ip','$monitor_name','disk_use','$alert_disk','$jh_disk_partition','$jh_disk_use','abnormal',now())";  
  220. else  
  221.         echo "$now ShengFen: $here Server: $jh_name Ip: $jh_ip Service: disk_use Monitor_Server: $monitor_name Disk_use: $jh_disk_partition ($jh_disk_use%)" >> $disk_worklog-$day  
  222.         /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database  -e "insert into $disk_table values ('','$here','$jh_name','$jh_ip','$monitor_name','disk_use','$alert_disk','$jh_disk_partition','$jh_disk_use','normal',now())";  
  223. fi  

下一篇文章地址:

運維自動化之使用PHP+MYSQL+SHELL打造私有監控系統(三)

http://dl528888.blog.51cto.com/2382721/1035142

本文出自 “吟—技術交流” 博客,請務必保留此出處http://dl528888.blog.51cto.com/2382721/1035131

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章