北京SEO

分享一个VPS系统监控资源的脚本

2019/10/10/17:46:44  阅读:1637  来源:谷歌SEO算法  标签: 微服务

以下是本人自己民写的一个Shell脚本,可以监控使用的VPS的CPU、Load、Memory、网络传输、网站能否打开等情况,感觉挺有用的,现在分享出来给需要的朋友.

几个月前开始使用VPS,每月限制300GB流量,流量方便基本够用了,但是有时候由于受到一些恶意访问,导致CPU、Memory等资源消耗较大,导致VPS上的博客网站响应时间太慢甚至有时根本无法打开网页,所以,我简单做了个脚本来进行监控和发邮件报警.

由于不是做很专业的运维,暂时不想上很专业的监控工具,如:Nagios、Puppet、Cacti、Zabbix等,所以自己手动了个Shell脚本来监控我关心的CPU、Load、Memory、网络传输、博客网站能否打开等内容,将该监控脚本分享如下:

https://github.com/smilejay/shell/blob/master/sh2013/vps_monitor.sh,代码如下:

  1. #!/bin/bash
  2. #set-x
  3. #thescripttomonitormyVPS
  4. #Itwillalertwhenmemory,load,CPU%,networking,httpd/mysqldorhome-page
  5. #isinanabnormalstate.
  6. #author:Jay
  7. #date:2013-10-16
  8. EMAIL="smile665@gmail.com"
  9. WARN_MSG=""
  10. #alertwhenfreememoryislessthan50MB
  11. functionmem_free()
  12. {
  13. threshold=50#50MBfreememory
  14. free_mem=$(free-m|grep"buffers/cache"|awk'{print$4}')
  15. if[$free_mem-lt$threshold];then
  16. WARN_MSG=$WARN_MSG"Freememeoryislessthan$thresholdMB.n"
  17. return1
  18. fi
  19. return0
  20. }
  21. #alertwhenload(5min)islargerthan4
  22. functionload_avg()
  23. {
  24. threshold=4#loadis4
  25. load_5min=$(cat/proc/loadavg|awk'{print$2}')
  26. if[$(echo"$load_5min>$threshold"|bc)-eq1];then
  27. WARN_MSG=$WARN_MSG"5minaverageloadislargerthan$threshold.n"
  28. return1
  29. fi
  30. return0
  31. }
  32. #alertwhenCPUidle%islessthan20%
  33. functioncpu_idle()
  34. {
  35. threshold=20#CPUidle%20%
  36. cpu_idle=$(sar15|grep-i'Average'|awk'{print$NF}')
  37. if[$(echo"$cpu_idle<$threshold"|bc)-eq1];then
  38. #inprintfcmd,%%representsasingle%
  39. WARN_MSG=$WARN_MSG"CPUidle%%islessthan$threshold%%.n"
  40. return1
  41. fi
  42. return0
  43. }
  44. #alertwhennetworkingtxspeedislargerthan80kB/s
  45. functionnetwork_tx()
  46. {
  47. threshold=80#TXspeed80kB/s
  48. interface=eth0
  49. tx_speed=$(sar-nDEV105|grep"Average"|grep"$interface"|awk'{print$6}')
  50. if[$(echo"$tx_speed>$threshold"|bc)-eq1];then
  51. WARN_MSG=$WARN_MSG"networkingTXspeedislargerthan$thresholdkB/s.n"
  52. return1
  53. fi
  54. return0
  55. }
  56. #alertwhenhttpdormysqldprocessdoesn'texist
  57. functionhttpd_mysqld()
  58. {
  59. ps-ef|grep'httpd'|grep-v'grep'
  60. if[$?-ne0];then
  61. WARN_MSG=$WARN_MSG"httpdprocessdoesn'texist.n"
  62. return1
  63. else
  64. ps-ef|grep'mysqld'|grep-v'grep'
  65. if[$?-ne0];then
  66. WARN_MSG=$WARN_MSG"mysqldprocessdoesn'texist.n"
  67. return1
  68. fi
  69. fi
  70. return0
  71. }
  72. #alertwhen'Stayhungry'doesn'texistinthehomepagehttp://smilejay.com/
  73. functionhome_page()
  74. {
  75. url=http://smilejay.com/
  76. keywords="Stayhungry"
  77. curl-sL$url|grep-i"$keywords"
  78. if[$?-ne0];then
  79. WARN_MSG=$WARN_MSG"keywords'$keywords'doesn'texistonlink$url.n"--phpfensi.com
  80. return1
  81. fi
  82. return0
  83. }
  84. #useanarraytostorethereturnvalueofeachmonitoringfunction
  85. mem_free
  86. chk[1]=$?
  87. load_avg
  88. chk[2]=$?
  89. cpu_idle
  90. chk[3]=$?
  91. network_tx
  92. chk[4]=$?
  93. httpd_mysqld
  94. chk[5]=$?
  95. home_page
  96. chk[6]=$?
  97. #sendwarningemailtothewebmasterifanyofthecheckfails.
  98. foriin$(seq16)
  99. do
  100. if[${chk[i]}-ne0];then
  101. printf"$WARN_MSG"|mailx-s"Warningfromsmilejay.com"$EMAIL
  102. exit1
  103. fi
  104. done

广告内容

分享一个VPS系统监控资源的脚本 分享一个VPS系统监控资源的脚本 分享一个VPS系统监控资源的脚本

相关阅读

热门评论

萧红SEO 萧红SEO

SEO爱好者,分享SEO经验~

总篇数212

精选文章

RMAN中catalog和nocatalog区别介绍 小技巧:为Linux下的文件分配多个权限 zimbra8.5.1安装第三方签名ssl证书的步骤 解决mysql不能远程连接数据库方法 windows服务器mysql增量备份批处理数据库 mysql中slow query log慢日志查询分析 JavaScript跨域问题总结 Linux下负载均衡软件LVS配置(VS/DR)教程 mysql中权限参数说明 MYSQL(错误1053)无法正常启动

SEO最新算法