仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1011|回复: 8
打印 上一主题 下一主题

[shell编程] 带来一篇shell剧本监控VPS体系资本

[复制链接]
因胸联盟 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-1-14 20:17:32 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

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
  1. #!/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.
  2. "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.
  3. "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%%.
  4. "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.
  5. "return1fireturn0}#alertwhenhttpdormysqldprocessdoesntexistfunctionhttpd_mysqld(){ps-ef|grephttpd|grep-vgrepif[$?-ne0];thenWARN_MSG=$WARN_MSG"httpdprocessdoesntexist.
  6. "return1elseps-ef|grepmysqld|grep-vgrepif[$?-ne0];thenWARN_MSG=$WARN_MSG"mysqldprocessdoesntexist.
  7. "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.
  8. "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教程讲得好,请记得点击右边漂浮的分享程序,把好文章分享给你的小伙伴们!
飘灵儿 该用户已被删除
沙发
发表于 2015-1-15 15:38:11 | 只看该作者

带来一篇shell剧本监控VPS体系资本

我也是坐沙发的
admin 该用户已被删除
板凳
发表于 2015-1-24 23:55:05 | 只看该作者
学习Linux应具备的。[书籍+网络资源]
地板
发表于 2015-2-2 19:05:27 | 只看该作者
即便是非英语国家的人发布技术文档,Linux也都首先翻译成英语在国际学术杂志和网络上发表。
变相怪杰 该用户已被删除
5#
发表于 2015-2-8 06:31:18 | 只看该作者
随着Linux应用的扩展,出现了不少Linux社区。有一些非常优秀的社区往往是Linux高手的舞台,如果在探讨高级技巧的论坛张贴非常初级的问题经常会没有结果。
老尸 该用户已被删除
6#
发表于 2015-2-25 01:26:32 | 只看该作者
如果你有庞大而复杂的测试条件,尽量把它剪裁得越小越好。可能你会遇到这种情况,对于一个问题会出现不同内容回答,这时你需要通过实践来验证。
兰色精灵 该用户已被删除
7#
发表于 2015-3-7 19:00:49 | 只看该作者
清楚了解网络的基础知识,特别是在Linux下应用知识,如接入internet等等。
柔情似水 该用户已被删除
8#
发表于 2015-3-15 19:30:45 | 只看该作者
学习Linux,应该怎样学,主要学些什么,一位Linux热心学习者,一段学习Linux的风云经验,历时十二个小时的思考总结,近十位网络Linux学习者权威肯定,为您学习Linux指明方向。
因胸联盟 该用户已被删除
9#
 楼主| 发表于 2015-3-22 04:33:32 | 只看该作者
目前全球有超过一百多个Linux发行版本,在国内也能找到十几个常见版本。如何选择请根据你的需求和能力,RedhatLinux和DebianLinux是网络管理员的理想选择。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-5-12 19:40

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表