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

3)引擎服務器(以甘肅引擎爲例,腳本解釋參照甘肅web)
  1. #!/bin/bash  
  2. #ip  
  3. yq_ip=$(/sbin/ifconfig eth0|grep "inet addr"|cut -d : -f 2|awk '{print $1}')  
  4. #name  
  5. yq_name=$(hostname)  
  6. monitor_name=yinqing 
  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. #yinqing service  
  29. memcache="$(ps -ef|grep 11211|grep -v grep|wc -l)" 
  30. datastorageservice="$(netstat -ant|grep 52010|wc -l)" 
  31. dbstatserver="$(ps -ef|grep dbstatserver|grep -v grep|wc -l)" 
  32. yq_http="$(netstat -antl|grep 80|wc -l)" 
  33. #cpu service  
  34. alert_cpu='80' 
  35. yq_cpu_idle="$(top -b -n 1 | grep Cpu | awk '{print $5}' | cut -f 1 -d .)" 
  36. yq_cpu="$(echo 100 - $yq_cpu_idle|/usr/bin/bc)" 
  37. #memory service  
  38. alert_mem='100' 
  39. yq_mem="$(/usr/bin/free -m|grep Mem|awk '{print $4}')" 
  40. yq_swap="$(/usr/bin/free -m|grep Swap|awk '{print $3}')" 
  41. alert_swap='0' 
  42. #memory log  
  43. memory_worklog='/usr/local/monitor/logs/mem_work_log' 
  44. memory_downlog='/usr/local/monitor/logs/mem_down_log' 
  45. #load service  
  46. cpu_count="$(grep -c 'model name' /proc/cpuinfo)" 
  47. alert_load="$(echo $cpu_count/2|/usr/bin/bc)" 
  48. yq_load="$(uptime|awk '{print $NF}'|cut -f 1 -d .)" 
  49. yq_load_15="$(uptime|awk '{print $NF}')" 
  50. #load log  
  51. load_worklog='/usr/local/monitor/logs/load_work_log' 
  52. load_downlog='/usr/local/monitor/logs/load_down_log' 
  53. #io service  
  54. alert_io='80' 
  55. yq_io_idle_back="$(/usr/bin/iostat|awk 'NR==4{print $NF}'|cut -f 1 -d .)" 
  56. yq_io_idle="$(echo 100 - $yq_io_idle_back|/usr/bin/bc)" 
  57. #io log  
  58. io_worklog='/usr/local/monitor/logs/io_work_log' 
  59. io_downlog='/usr/local/monitor/logs/io_down_log' 
  60. #hardware service  
  61. yq_hardware_error="$(dmesg|grep -i error|wc -l)" 
  62. yq_info_error="$(dmesg|grep -i error)" 
  63. #hardware error log  
  64. hard_worklog='/usr/local/monitor/logs/hard_work_log' 
  65. hard_downlog='/usr/local/monitor/logs/hard_down_log' 
  66. #message service  
  67. yq_message_error_count="$(awk '/"$(date +%Y%m%d)"/' /var/log/messages|grep -i error|wc -l)"  
  68. yq_message_error="$(awk '/"$(date +%Y%m%d)"/' /var/log/messages|grep -i error)"  
  69. #message error log  
  70. message_worklog='/usr/local/monitor/logs/message_work_log' 
  71. message_downlog='/usr/local/monitor/logs/message_down_log' 
  72. #user service  
  73. yq_user_count="$(/usr/bin/who|wc -l)" 
  74. yq_user_info="$(/usr/bin/who)" 
  75. #user  log  
  76. user_worklog='/usr/local/monitor/logs/user_work_log' 
  77. user_downlog='/usr/local/monitor/logs/user_down_log' 
  78. #disk service  
  79. alert_disk='80' 
  80. yq_disk="$(/bin/df -H|sort -k5nr|grep -vE 'Filesystem|tmpfs|cdrom'|awk '{print $5 " " $1}')"  
  81. yq_disk_use=$(echo $yq_disk|awk '{print $1}'|cut -d '%' -f1)  
  82. yq_disk_partition=$(echo $yq_disk|awk '{print $2}')  
  83. #disk log  
  84. disk_worklog='/usr/local/monitor/logs/disk_work_log' 
  85. disk_downlog='/usr/local/monitor/logs/disk_down_log' 
  86. #cpu log  
  87. cpu_worklog='/usr/local/monitor/logs/cpu_work_log' 
  88. cpu_downlog='/usr/local/monitor/logs/cpu_down_log' 
  89. #notification mail  
  90. email='[email protected]' 
  91. #monitor  
  92. if [ ! -d "$loghere" ];then  
  93.     mkdir $loghere  
  94. fi  
  95. #yinqing check  
  96. if [ $memcache -ge 1 ];then  
  97.         echo "$now ShengFen: $here Server: $yq_name Ip: $yq_ip Service: memcache Monitor_Server: $monitor_name is working" >> $worklog-$day  
  98.     /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $service_table values ('','$here','$yq_name','$yq_ip','$monitor_name','memcache','working',now())";  
  99. else  
  100.                 /opt/memcached-1.4.5/memcached -d -m 1024 -p 11211 -u root  
  101.         echo "$now ShengFen: $here Server: $yq_name Ip: $yq_ip Service: memcache Monitor_Server: $monitor_name is down"|/bin/mail -s "Notification ShengFen: $here Server: $yq_name Service:  memcache  was a problem" $email  
  102.         echo "$now ShengFen: $here Server: $yq_name Ip: $yq_ip Service: memcache Monitor_Server: $montior_name is down" >> $downlog-$day  
  103.     /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $service_table values ('','$here','$yq_name','$yq_ip','$monitor_name','memcache','downing',now())";  
  104. fi  
  105. if [ $datastorageservice -ge 1 ];then  
  106.         echo "$now ShengFen: $here Server: $yq_name Ip: $yq_ip Service: datastorageservice Monitor_Server: $monitor_name is working" >> $worklog-$day  
  107.     /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $service_table values ('','$here','$yq_name','$yq_ip','$monitor_name','datastorageservice','working',now())";  
  108. else  
  109.                 /usr/local/lbs/bin/DataStorageService_0.sh restart >>/dev/null  
  110.         echo "$now ShengFen: $here Server: $yq_name Ip: $yq_ip Service: datastorageservice Monitor_Server: $monitor_name is down"|/bin/mail -s "Notification ShengFen: $here Server: $yq_name Service:  datastorageservice  was a problem" $email  
  111.         echo "$now ShengFen: $here Server: $yq_name Ip: $yq_ip Service: datastorageservice Monitor_Server: $montior_name is down" >> $downlog-$day  
  112.     /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $service_table values ('','$here','$yq_name','$yq_ip','$monitor_name','datastorageservice','downing',now())";  
  113. fi  
  114. if [ $dbstatserver -ge 1 ];then  
  115.         echo "$now ShengFen: $here Server: $yq_name Ip: $yq_ip Service: dbstatserver Monitor_Server: $monitor_name is working" >> $worklog-$day  
  116.     /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $service_table values ('','$here','$yq_name','$yq_ip','$monitor_name','dbstatserver','working',now())";  
  117. else  
  118.                 /bin/sh /usr/local/lbs/java/lib/dbstatserver/dbstatserver.sh  
  119.         echo "$now ShengFen: $here Server: $yq_name Ip: $yq_ip Service: dbstatserver Monitor_Server: $monitor_name is down"|/bin/mail -s "Notification ShengFen: $here Server: $yq_name Service:  dbstatserver  was a problem" $email  
  120.         echo "$now ShengFen: $here Server: $yq_name Ip: $yq_ip Service: dbstatserver Monitor_Server: $montior_name is down" >> $downlog-$day  
  121.     /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $service_table values ('','$here','$yq_name','$yq_ip','$monitor_name','dbstatserver','downing',now())";  
  122. fi  
  123. if [ $yq_http -ge 1 ];then  
  124.         echo "$now ShengFen: $here Server: $yq_name Ip: $yq_ip Service: http Monitor_Server: $monitor_name is working" >> $worklog-$day  
  125.     /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $service_table values ('','$here','$yq_name','$yq_ip','$monitor_name','yq_http','working',now())";  
  126. else  
  127.                 service httpd restart  
  128.         echo "$now ShengFen: $here Server: $yq_name Ip: $yq_ip Service: http Monitor_Server: $monitor_name is down"|/bin/mail -s "Notification ShengFen: $here Server: $yq_name Service:  http  was a problem" $email  
  129.         echo "$now ShengFen: $here Server: $yq_name Service: http Monitor_Server: $montior_name is down" >> $downlog-$day  
  130.     /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $service_table values ('','$here','$yq_name','$yq_ip','$monitor_name','yq_http','downing',now())";  
  131. fi  
  132. #check cpu_idle  
  133. if [ $yq_cpu -ge $alert_cpu ];then  
  134.     echo "$now ShengFen: $here Server: $yq_name Ip: $yq_ip Service: cpu_idle Monitor_Server: $monitor_name Cpu_use: $yq_cpu"|/bin/mail -s "Notification ShengFen: $here Server: $yq_name Service: cpu_use  was Exceed Threshold value: 80%" $email  
  135.     echo "$now ShengFen: $here Server: $yq_name Ip: $yq_ip Service: cpu_idle Monitor_Server: $monitor_name Cpu_use: $yq_cpu" >> $cpu_downlog-$day  
  136.     /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $cpu_table values ('','$here','$yq_name','$yq_ip','$monitor_name','cpu_use','$alert_cpu','$yq_cpu','abnormal',now())";  
  137. else  
  138.     echo "$now ShengFen: $here Server: $yq_name Ip: $yq_ip Service: cpu_idle Monitor_Server: $monitor_name Cpu_use: $yq_cpu" >> $cpu_worklog-$day  
  139.     /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $cpu_table values ('','$here','$yq_name','$yq_ip','$monitor_name','cpu_use','$alert_cpu','$yq_cpu','normal',now())";  
  140. fi  
  141. #check memory  
  142. if [ $yq_mem -le $alert_mem ];then  
  143.         echo "$now ShengFen: $here Server: $yq_name Ip: $yq_ip Service: memory Monitor_Server: $monitor_name Free_mem: $yq_mem"|/bin/mail -s "Notification ShengFen: $here Server: $yq_name Service: memory  was Exceed Threshold value" $email  
  144.         echo "$now ShengFen: $here Server: $yq_name Ip: $yq_ip Service: memory Monitor_Server: $monitor_name Free_mem: $yq_mem" >> $memory_downlog-$day  
  145.         /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $memory_table values ('','$here','$yq_name','$yq_ip','$monitor_name','memory','$alert_mem','$yq_mem','abnormal',now())";  
  146. else  
  147.         echo "$now ShengFen: $here Server: $yq_name Ip: $yq_ip Service: memory Monitor_Server: $monitor_name Free_mem: $yq_mem" >> $memory_worklog-$day  
  148.         /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $memory_table values ('','$here','$yq_name','$yq_ip','$monitor_name','memory','$alert_mem','$yq_mem','normal',now())";  
  149. fi  
  150. #check swap  
  151. if [ $yq_swap -gt $alert_swap ];then  
  152.         echo "$now ShengFen: $here Server: $yq_name Ip: $yq_ip Service: swap Monitor_Server: $monitor_name Swap_yq: $yq_swap"|/bin/mail -s "Notification ShengFen: $here Server: $yq_name Service: swap Ip: $yq_ip was Exceed Threshold value" $email  
  153.         echo "$now ShengFen: $here Server: $yq_name Ip: $yq_ip Service: swap Monitor_Server: $monitor_name Swap_yq: $yq_swap" >> $memory_downlog-$day  
  154.         /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database  -e "insert into $memory_table values ('','$here','$yq_name','$yq_ip','$monitor_name','swap','$alert_swap','$yq_swap','abnormal',now())";  
  155. else  
  156.         echo "$now ShengFen: $here Server: $yq_name Ip: $yq_ip Service: swap Monitor_Server: $monitor_name Swap_yq: $yq_swap" >> $memory_worklog-$day  
  157.         /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $memory_table values ('','$here','$yq_name','$yq_ip','$monitor_name','swap','$alert_swap','$yq_swap','normal',now())";  
  158. fi  
  159. #check load_15  
  160. if [ $yq_load -ge $alert_load ];then  
  161.         echo "$now ShengFen: $here Server: $yq_name Ip: $yq_ip Service: load_15 Monitor_Server: $monitor_name Load_use: $yq_load_15"|/bin/mail -s "Notification ShengFen: $here Server: $yq_name Service: load_15 Ip: $yq_ip was Exceed Threshold value" $email   
  162.         echo "$now ShengFen: $here Server: $yq_name Ip: $yq_ip Service: load_15 Monitor_Server: $monitor_name Load_use: $yq_load_15" >> $load_downlog-$day  
  163.         /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $load_table values ('','$here','$yq_name','$yq_ip','$monitor_name','load_15','$alert_load','$yq_load_15','abnormal',now())";  
  164. else  
  165.         echo "$now ShengFen: $here Server: $server Ip: $ip Service: load Monitor_Server: $monitor_name Load_use: $yq_load_15" >> $load_worklog-$day  
  166.         /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $load_table values ('','$here','$yq_name','$yq_ip','$monitor_name','load_15','$alert_load','$yq_load_15','normal',now())";  
  167. fi  
  168. #check io_idle  
  169. if [ $yq_io_idle -ge $alert_io ];then  
  170.         echo "$now ShengFen: $here Server: $yq_name Ip: $yq_ip Service: io_idle Monitor_Server: $monitor_name Io_use: $yq_io_idle"|/bin/mail -s "Notification ShengFen: $here Server: $yq_name Service: io_use  was Exceed Threshold value: 80%" $email  
  171.         echo "$now ShengFen: $here Server: $yq_name Ip: $yq_ip Service: io_idle Monitor_Server: $monitor_name Io_use: $yq_io_idle" >> $io_downlog-$day  
  172.         /usr/bin/mysql  -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $io_table values ('','$here','$yq_name','$yq_ip','$monitor_name','io_use','$alert_io','$yq_io_idle','abnormal',now())";  
  173. else  
  174.         echo "$now ShengFen: $here Server: $yq_name Ip: $yq_ip Service: io_idle Monitor_Server: $monitor_name Io_use: $yq_io_idle" >> $io_worklog-$day  
  175.         /usr/bin/mysql  -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $io_table values ('','$here','$yq_name','$yq_ip','$monitor_name','io_use','$alert_io','$yq_io_idle','normal',now())";  
  176. fi  
  177. #check hareware error info  
  178. if [ $yq_hardware_error -gt 0 ];then  
  179.         echo "$now ShengFen: $here Server: $yq_name Ip: $yq_ip Service: hardware_error Monitor_Server: $monitor_name Error: $yq_info_error"|/bin/mail -s "Notification ShengFen: $here Server: $yq_name Service: hardware_error  were some hardware imformation error" $email  
  180.         echo "$now ShengFen: $here Server: $yq_name Ip: $yq_ip Service: hardware_error Monitor_Server: $monitor_name Error: $yq_info_error" >> $hard_downlog-$day  
  181.         /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $hardware_table values ('','$here','$yq_name','$yq_ip','$monitor_name','hardware_error','0','$yq_hardware_error','abnormal',now())";  
  182. else  
  183.         echo "$now ShengFen: $here Server: $yq_name Ip: $yq_ip Service: hardware_error Monitor_Server: $monitor_name Error: Nothing" >> $hard_worklog-$day  
  184.         /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $hardware_table values ('','$here','$yq_name','$yq_ip','$monitor_name','hardware_error','0','$yq_hardware_error','normal',now())";  
  185. fi  
  186. #check message error  
  187. if [ $yq_message_error_count -ge 1 ];then  
  188.         echo "$now ShengFen: $here Server: $yq_name Ip: $yq_ip Service: message_error Monitor_Server: $monitor_name Message_error: $yq_message_error"|/bin/mail -s "Notification ShengFen: $here Server: $yq_name Service: message_error  were some message imformation error" $email  
  189.         echo "$now ShengFen: $here Server: $yq_name Ip: $yq_ip Service: message_error Monitor_Server: $monitor_name Message_error: $yq_message_error" >> $message_downlog-$day  
  190.         /usr/bin/mysql-h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $message_table values ('','$here','$yq_name','$yq_ip','$monitor_name','message_error','1','$yq_message_error_count','abnormal',now())";  
  191. else  
  192.         echo "$now ShengFen: $here Server: $yq_name Ip: $yq_ip Service: message_error Monitor_Server: $monitor_name Message_error: Nothing" >> $message_worklog-$day  
  193.         /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database  -e "insert into $message_table values ('','$here','$yq_name','$yq_ip','$monitor_name','message_error','1','$yq_message_error_count','normal',now())";  
  194. fi  
  195. #check user  
  196. if [ $yq_user_count -ge 3 ];then  
  197.         echo "$now ShengFen: $here Server: $yq_name Ip: $yq_ip Service: user Monitor_Server: $monitor_name User: $yq_user_info"|/bin/mail -s "Notification ShengFen: $here Server: $yq_name  Service: user  was Exceed Threshold value: 3" $email  
  198.         echo "$now ShengFen: $here Server: $yq_name Ip: $yq_ip Service: user Monitor_Server: $monitor_name User: $yq_user_info" >> $user_downlog-$day  
  199.         /usr/bin/mysql  -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $user_table values ('','$here','$yq_name','$yq_ip','$monitor_name','user','3','$yq_user_count','abnormal',now())";  
  200. else  
  201.         echo "$now ShengFen: $here Server: $yq_name Ip: $yq_ip Service: user Monitor_Server: $monitor_name User: normal" >> $user_worklog-$day  
  202.         /usr/bin/mysql  -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $user_table values ('','$here','$yq_name','$yq_ip','$monitor_name','user','3','$yq_user_count','normal',now())";  
  203. fi  
  204. #check disk  
  205. if [ $yq_disk_use -ge $alert_disk ];then  
  206.         echo "$now ShengFen: $here Server: $yq_name Ip: $yq_ip Service: disk_use Monitor_Server: $monitor_name Disk_use: $yq_disk_partition ($yq_disk_use%)"|/bin/mail -s "Warning!!! $here Server: $yq_name Service: disk_use  was Exceed Threshold value : $alert_disk% " $email  
  207.         echo "$now ShengFen: $here Server: $yq_name Ip: $yq_ip Service: disk_use Monitor_Server: $monitor_name Disk_use: $yq_disk_partion ($yq_disk_use%)" >> $disk_downlog-$day  
  208.         /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database  -e "insert into $disk_table values ('','$here','$yq_name','$yq_ip','$monitor_name','disk_use','$alert_disk','$yq_disk_partition','$yq_disk_use','abnormal',now())";  
  209. else  
  210.         echo "$now ShengFen: $here Server: $yq_name Ip: $yq_ip Service: disk_use Monitor_Server: $monitor_name Disk_use: $yq_disk_partition ($yq_disk_use%)" >> $disk_worklog-$day  
  211.         /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database  -e "insert into $disk_table values ('','$here','$yq_name','$yq_ip','$monitor_name','disk_use','$alert_disk','$yq_disk_partition','$yq_disk_use','normal',now())";  
  212. fi  
4)備用服務器(以甘肅備用爲例,腳本解釋參照甘肅web)
  1. #!/bin/bash  
  2. #ip  
  3. by_ip=$(/sbin/ifconfig eth0|grep "inet addr"|cut -d : -f 2|awk '{print $1}')  
  4. #name  
  5. by_name=$(hostname)  
  6. monitor_name=beiyong 
  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. #cpu service  
  29. alert_cpu='80' 
  30. by_cpu_idle="$(top -b -n 1 | grep Cpu | awk '{print $5}' | cut -f 1 -d .)" 
  31. by_cpu="$(echo 100 - $by_cpu_idle|/usr/bin/bc)" 
  32. #memory service  
  33. alert_mem='100' 
  34. by_mem="$(/usr/bin/free -m|grep Mem|awk '{print $4}')" 
  35. by_swap="$(/usr/bin/free -m|grep Swap|awk '{print $3}')" 
  36. alert_swap='0' 
  37. #memory log  
  38. memory_worklog='/usr/local/monitor/logs/mem_work_log' 
  39. memory_downlog='/usr/local/monitor/logs/mem_down_log' 
  40. #load service  
  41. cpu_count="$(grep -c 'model name' /proc/cpuinfo)" 
  42. alert_load="$(echo $cpu_count/2|/usr/bin/bc)" 
  43. by_load="$(uptime|awk '{print $NF}'|cut -f 1 -d .)" 
  44. by_load_15="$(uptime|awk '{print $NF}')" 
  45. #load log  
  46. load_worklog='/usr/local/monitor/logs/load_work_log' 
  47. load_downlog='/usr/local/monitor/logs/load_down_log' 
  48. #io service  
  49. alert_io='80' 
  50. by_io_idle_back="$(/usr/bin/iostat|awk 'NR==4{print $NF}'|cut -f 1 -d .)" 
  51. by_io_idle="$(echo 100 - $by_io_idle_back|/usr/bin/bc)" 
  52. #io log  
  53. io_worklog='/usr/local/monitor/logs/io_work_log' 
  54. io_downlog='/usr/local/monitor/logs/io_down_log' 
  55. #hardware service  
  56. by_hardware_error="$(dmesg|grep -i error|wc -l)" 
  57. by_info_error="$(dmesg|grep -i error)" 
  58. #hardware error log  
  59. hard_worklog='/usr/local/monitor/logs/hard_work_log' 
  60. hard_downlog='/usr/local/monitor/logs/hard_down_log' 
  61. #message service  
  62. by_message_error_count="$(awk '/"$(date +%Y%m%d)"/' /var/log/messages|grep -i error|wc -l)"  
  63. by_message_error="$(awk '/"$(date +%Y%m%d)"/' /var/log/messages|grep -i error)"  
  64. #message error log  
  65. message_worklog='/usr/local/monitor/logs/message_work_log' 
  66. message_downlog='/usr/local/monitor/logs/message_down_log' 
  67. #user service  
  68. by_user_count="$(/usr/bin/who|wc -l)" 
  69. by_user_info="$(/usr/bin/who)" 
  70. #user  log  
  71. user_worklog='/usr/local/monitor/logs/user_work_log' 
  72. user_downlog='/usr/local/monitor/logs/user_down_log' 
  73. #disk service  
  74. alert_disk='80' 
  75. by_disk="$(/bin/df -H|sort -k5nr|grep -vE 'Filesystem|tmpfs|cdrom'|awk '{print $5 " " $1}')"  
  76. by_disk_use=$(echo $by_disk|awk '{print $1}'|cut -d '%' -f1)  
  77. by_disk_partition=$(echo $by_disk|awk '{print $2}')  
  78. #disk log  
  79. disk_worklog='/usr/local/monitor/logs/disk_work_log' 
  80. disk_downlog='/usr/local/monitor/logs/disk_down_log' 
  81. #cpu log  
  82. cpu_worklog='/usr/local/monitor/logs/cpu_work_log' 
  83. cpu_downlog='/usr/local/monitor/logs/cpu_down_log' 
  84. #notification mail  
  85. email='[email protected]' 
  86. #monitor  
  87. if [ ! -d "$loghere" ];then  
  88.     mkdir $loghere  
  89. fi  
  90. #check cpu_idle  
  91. #check cpu_idle  
  92. if [ $by_cpu -ge $alert_cpu ];then  
  93.     echo "$now ShengFen: $here Server: $by_name Ip: $by_ip Service: cpu_idle Monitor_Server: $monitor_name Cpu_use: $by_cpu"|/bin/mail -s "Notification ShengFen: $here Server: $by_name Service: cpu_use  was Exceed Threshold value: 80%" $email  
  94.     echo "$now ShengFen: $here Server: $by_name Ip: $by_ip Service: cpu_idle Monitor_Server: $monitor_name Cpu_use: $by_cpu" >> $cpu_downlog-$day  
  95.     /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $cpu_table values ('','$here','$by_name','$by_ip','$monitor_name','cpu_use','$alert_cpu','$by_cpu','abnormal',now())";  
  96. else  
  97.     echo "$now ShengFen: $here Server: $by_name Ip: $by_ip Service: cpu_idle Monitor_Server: $monitor_name Cpu_use: $by_cpu" >> $cpu_worklog-$day  
  98.     /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $cpu_table values ('','$here','$by_name','$by_ip','$monitor_name','cpu_use','$alert_cpu','$by_cpu','normal',now())";  
  99. fi  
  100. #check memory  
  101. if [ $by_mem -le $alert_mem ];then  
  102.         echo "$now ShengFen: $here Server: $by_name Ip: $by_ip Service: memory Monitor_Server: $monitor_name Free_mem: $by_mem"|/bin/mail -s "Notification ShengFen: $here Server: $by_name Service: memory  was Exceed Threshold value" $email  
  103.         echo "$now ShengFen: $here Server: $by_name Ip: $by_ip Service: memory Monitor_Server: $monitor_name Free_mem: $by_mem" >> $memory_downlog-$day  
  104.         /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $memory_table values ('','$here','$by_name','$by_ip','$monitor_name','memory','$alert_mem','$by_mem','abnormal',now())";  
  105. else  
  106.         echo "$now ShengFen: $here Server: $by_name Ip: $by_ip Service: memory Monitor_Server: $monitor_name Free_mem: $by_mem" >> $memory_worklog-$day  
  107.         /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $memory_table values ('','$here','$by_name','$by_ip','$monitor_name','memory','$alert_mem','$by_mem','normal',now())";  
  108. fi  
  109. #check swap  
  110. if [ $by_swap -gt $alert_swap ];then  
  111.         echo "$now ShengFen: $here Server: $by_name Ip: $by_ip Service: swap Monitor_Server: $monitor_name Swap_by: $by_swap"|/bin/mail -s "Notification ShengFen: $here Server: $by_name Service: swap Ip: $by_ip was Exceed Threshold value" $email  
  112.         echo "$now ShengFen: $here Server: $by_name Ip: $by_ip Service: swap Monitor_Server: $monitor_name Swap_by: $by_swap" >> $memory_downlog-$day  
  113.         /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database  -e "insert into $memory_table values ('','$here','$by_name','$by_ip','$monitor_name','swap','$alert_swap','$by_swap','abnormal',now())";  
  114. else  
  115.         echo "$now ShengFen: $here Server: $by_name Ip: $by_ip Service: swap Monitor_Server: $monitor_name Swap_by: $by_swap" >> $memory_worklog-$day  
  116.         /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $memory_table values ('','$here','$by_name','$by_ip','$monitor_name','swap','$alert_swap','$by_swap','normal',now())";  
  117. fi  
  118. #check load_15  
  119. if [ $by_load -ge $alert_load ];then  
  120.         echo "$now ShengFen: $here Server: $by_name Ip: $by_ip Service: load_15 Monitor_Server: $monitor_name Load_use: $by_load_15"|/bin/mail -s "Notification ShengFen: $here Server: $by_name Service: load_15 Ip: $by_ip was Exceed Threshold value" $email   
  121.         echo "$now ShengFen: $here Server: $by_name Ip: $by_ip Service: load_15 Monitor_Server: $monitor_name Load_use: $by_load_15" >> $load_downlog-$day  
  122.         /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $load_table values ('','$here','$by_name','$by_ip','$monitor_name','load_15','$alert_load','$by_load_15','abnormal',now())";  
  123. else  
  124.         echo "$now ShengFen: $here Server: $server Ip: $ip Service: load Monitor_Server: $monitor_name Load_use: $by_load_15" >> $load_worklog-$day  
  125.         /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $load_table values ('','$here','$by_name','$by_ip','$monitor_name','load_15','$alert_load','$by_load_15','normal',now())";  
  126. fi  
  127. #check io_idle  
  128. if [ $by_io_idle -ge $alert_io ];then  
  129.         echo "$now ShengFen: $here Server: $by_name Ip: $by_ip Service: io_idle Monitor_Server: $monitor_name Io_use: $by_io_idle"|/bin/mail -s "Notification ShengFen: $here Server: $by_name Service: io_use  was Exceed Threshold value: 80%" $email  
  130.         echo "$now ShengFen: $here Server: $by_name Ip: $by_ip Service: io_idle Monitor_Server: $monitor_name Io_use: $by_io_idle" >> $io_downlog-$day  
  131.         /usr/bin/mysql  -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $io_table values ('','$here','$by_name','$by_ip','$monitor_name','io_use','$alert_io','$by_io_idle','abnormal',now())";  
  132. else  
  133.         echo "$now ShengFen: $here Server: $by_name Ip: $by_ip Service: io_idle Monitor_Server: $monitor_name Io_use: $by_io_idle" >> $io_worklog-$day  
  134.         /usr/bin/mysql  -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $io_table values ('','$here','$by_name','$by_ip','$monitor_name','io_use','$alert_io','$by_io_idle','normal',now())";  
  135. fi  
  136. #check hareware error info  
  137. if [ $by_hardware_error -gt 0 ];then  
  138.         echo "$now ShengFen: $here Server: $by_name Ip: $by_ip Service: hardware_error Monitor_Server: $monitor_name Error: $by_info_error"|/bin/mail -s "Notification ShengFen: $here Server: $by_name Service: hardware_error  were some hardware imformation error" $email  
  139.         echo "$now ShengFen: $here Server: $by_name Ip: $by_ip Service: hardware_error Monitor_Server: $monitor_name Error: $by_info_error" >> $hard_downlog-$day  
  140.         /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $hardware_table values ('','$here','$by_name','$by_ip','$monitor_name','hardware_error','0','$by_hardware_error','abnormal',now())";  
  141. else  
  142.         echo "$now ShengFen: $here Server: $by_name Ip: $by_ip Service: hardware_error Monitor_Server: $monitor_name Error: Nothing" >> $hard_worklog-$day  
  143.         /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $hardware_table values ('','$here','$by_name','$by_ip','$monitor_name','hardware_error','0','$by_hardware_error','normal',now())";  
  144. fi  
  145. #check message error  
  146. if [ $by_message_error_count -ge 1 ];then  
  147.         echo "$now ShengFen: $here Server: $by_name Ip: $by_ip Service: message_error Monitor_Server: $monitor_name Message_error: $by_message_error"|/bin/mail -s "Notification ShengFen: $here Server: $by_name Service: message_error  were some message imformation error" $email  
  148.         echo "$now ShengFen: $here Server: $by_name Ip: $by_ip Service: message_error Monitor_Server: $monitor_name Message_error: $by_message_error" >> $message_downlog-$day  
  149.         /usr/bin/mysql-h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $message_table values ('','$here','$by_name','$by_ip','$monitor_name','message_error','1','$by_message_error_count','abnormal',now())";  
  150. else  
  151.         echo "$now ShengFen: $here Server: $by_name Ip: $by_ip Service: message_error Monitor_Server: $monitor_name Message_error: Nothing" >> $message_worklog-$day  
  152.         /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database  -e "insert into $message_table values ('','$here','$by_name','$by_ip','$monitor_name','message_error','1','$by_message_error_count','normal',now())";  
  153. fi  
  154. #check user  
  155. if [ $by_user_count -ge 3 ];then  
  156.         echo "$now ShengFen: $here Server: $by_name Ip: $by_ip Service: user Monitor_Server: $monitor_name User: $by_user_info"|/bin/mail -s "Notification ShengFen: $here Server: $by_name  Service: user  was Exceed Threshold value: 3" $email  
  157.         echo "$now ShengFen: $here Server: $by_name Ip: $by_ip Service: user Monitor_Server: $monitor_name User: $by_user_info" >> $user_downlog-$day  
  158.         /usr/bin/mysql  -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $user_table values ('','$here','$by_name','$by_ip','$monitor_name','user','3','$by_user_count','abnormal',now())";  
  159. else  
  160.         echo "$now ShengFen: $here Server: $by_name Ip: $by_ip Service: user Monitor_Server: $monitor_name User: normal" >> $user_worklog-$day  
  161.         /usr/bin/mysql  -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $user_table values ('','$here','$by_name','$by_ip','$monitor_name','user','3','$by_user_count','normal',now())";  
  162. fi  
  163. #check disk  
  164. if [ $by_disk_use -ge $alert_disk ];then  
  165.         echo "$now ShengFen: $here Server: $by_name Ip: $by_ip Service: disk_use Monitor_Server: $monitor_name Disk_use: $by_disk_partition ($by_disk_use%)"|/bin/mail -s "Warning!!! $here Server: $by_name Service: disk_use  was Exceed Threshold value : $alert_disk% " $email  
  166.         echo "$now ShengFen: $here Server: $by_name Ip: $by_ip Service: disk_use Monitor_Server: $monitor_name Disk_use: $by_disk_partion ($by_disk_use%)" >> $disk_downlog-$day  
  167.         /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database  -e "insert into $disk_table values ('','$here','$by_name','$by_ip','$monitor_name','disk_use','$alert_disk','$by_disk_partition','$by_disk_use','abnormal',now())";  
  168. else  
  169.         echo "$now ShengFen: $here Server: $by_name Ip: $by_ip Service: disk_use Monitor_Server: $monitor_name Disk_use: $by_disk_partition ($by_disk_use%)" >> $disk_worklog-$day  
  170.         /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database  -e "insert into $disk_table values ('','$here','$by_name','$by_ip','$monitor_name','disk_use','$alert_disk','$by_disk_partition','$by_disk_use','normal',now())";  
  171. fi  

 下一篇文章地址:

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

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

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