我常用的主机监控Shell脚本

最近时不时有朋友问我关于服务器监控方面的问题,问常用的服务器监控除了用开源软件,比如:cacti,nagios监控外是否可以自己写shell脚本呢?根据自己的需求写出的shell脚本更能满足需求,更能细化主机监控的全面性。

创新互联成立于2013年,先为铁西等服务建站,铁西等地企业,进行企业商务咨询服务。为铁西企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

下面是我常用的几个主机监控的脚本,大家可以根据自己的情况在进行修改,希望能给大家一点帮助。

1、查看主机网卡流量

 
 
 
  1. #!/bin/bash 
  2. #!/bin/bash 
  3. #network 
  4. #Mike.Xu 
  5. while : ; do 
  6.       time='date +%m"-"%d" "%k":"%M' 
  7.       day='date +%m"-"%d' 
  8.       rx_before='ifconfig eth0|sed -n "8"p|awk '{print $2}'|cut -c7-' 
  9.       tx_before='ifconfig eth0|sed -n "8"p|awk '{print $6}'|cut -c7-' 
  10.       sleep 2 
  11.       rx_after='ifconfig eth0|sed -n "8"p|awk '{print $2}'|cut -c7-' 
  12.       tx_after='ifconfig eth0|sed -n "8"p|awk '{print $6}'|cut -c7-' 
  13.       rx_result=$[(rx_after-rx_before)/256] 
  14.       tx_result=$[(tx_after-tx_before)/256] 
  15.       echo "$time Now_In_Speed: "$rx_result"kbps Now_OUt_Speed: "$tx_result"kbps" 
  16.       sleep 2 
  17. done 
  18. done 

2、系统状况监控

 
 
 
  1. #!/bin/sh 
  2. #systemstat.sh 
  3. #Mike.Xu 
  4. IP=192.168.1.227 
  5. top -n 2| grep "Cpu" >>./temp/cpu.txt 
  6. free -m | grep "Mem" >> ./temp/mem.txt 
  7. df -k | grep "sda1" >> ./temp/drive_sda1.txt 
  8. #df -k | grep sda2 >> ./temp/drive_sda2.txt 
  9. df -k | grep "/mnt/storage_0" >> ./temp/mnt_storage_0.txt 
  10. df -k | grep "/mnt/storage_pic" >> ./temp/mnt_storage_pic.txt 
  11. time=`date +%m"."%d" "%k":"%M` 
  12. connect=`netstat -na | grep "219.238.148.30:80" | wc -l` 
  13. echo "$time  $connect" >> ./temp/connect_count.txt 

3、监控主机的磁盘空间,当使用空间超过90%就通过发mail来发警告

 
 
 
  1. #!/bin/bash 
  2. #monitor available disk space 
  3. SPACE='df | sed -n '/ \ / $ / p' | gawk '{print $5}' | sed  's/%//' 
  4. if [ $SPACE -ge 90 ] 
  5. then 
  6. fty89@163.com 
  7. fi 

4、 监控CPU和内存的使用情况

 
 
 
  1. #!/bin/bash 
  2. #script  to capture system statistics 
  3. OUTFILE=/home/xu/capstats.csv 
  4. DATE='date +%m/%d/%Y' 
  5. TIME='date +%k:%m:%s' 
  6. TIMEOUT='uptime' 
  7. VMOUT='vmstat 1 2' 
  8.  USERS='echo $TIMEOUT | gawk '{print $4}' ' 
  9. LOAD='echo $TIMEOUT | gawk '{print $9}' | sed "s/,//' ' 
  10. FREE='echo $VMOUT | sed -n '/[0-9]/p' | sed -n '2p' | gawk '{print $4} ' ' 
  11. IDLE='echo  $VMOUT | sed -n '/[0-9]/p' | sed -n '2p' |gawk '{print $15}' ' 
  12. echo "$DATE,$TIME,$USERS,$LOAD,$FREE,$IDLE" >> $OUTFILE 

5、全方位监控主机

 
 
 
  1. #!/bin/bash 
  2. # check_xu.sh 
  3. # 0 * * * * /home/check_xu.sh 
  4.   
  5. DAT="`date +%Y%m%d`" 
  6. HOUR="`date +%H`" 
  7. DIR="/home/oslog/host_${DAT}/${HOUR}" 
  8. DELAY=60 
  9. COUNT=60 
  10. # whether the responsible directory exist 
  11. if ! test -d ${DIR} 
  12. then 
  13.         /bin/mkdir -p ${DIR} 
  14. fi 
  15. # general check 
  16. export TERM=linux 
  17. /usr/bin/top -b -d ${DELAY} -n ${COUNT} > ${DIR}/top_${DAT}.log 2>&1 & 
  18. # cpu check 
  19. /usr/bin/sar -u ${DELAY} ${COUNT} > ${DIR}/cpu_${DAT}.log 2>&1 & 
  20. #/usr/bin/mpstat -P 0 ${DELAY} ${COUNT} > ${DIR}/cpu_0_${DAT}.log 2>&1 & 
  21. #/usr/bin/mpstat -P 1 ${DELAY} ${COUNT} > ${DIR}/cpu_1_${DAT}.log 2>&1 & 
  22. # memory check 
  23. /usr/bin/vmstat ${DELAY} ${COUNT} > ${DIR}/vmstat_${DAT}.log 2>&1 & 
  24. # I/O check 
  25. /usr/bin/iostat ${DELAY} ${COUNT} > ${DIR}/iostat_${DAT}.log 2>&1 & 
  26. # network check 
  27. /usr/bin/sar -n DEV ${DELAY} ${COUNT} > ${DIR}/net_${DAT}.log 2>&1 & 
  28. #/usr/bin/sar -n EDEV ${DELAY} ${COUNT} > ${DIR}/net_edev_${DAT}.log 2>&1 & 

放在crontab里每小时自动执行:

 
 
 
  1. 0 * * * * /home/check_xu.sh 

这样会在/home/oslog/host_yyyymmdd/hh目录下生成各小时cpu、内存、网络,IO的统计数据

如果某个时间段产生问题了,就可以去看对应的日志信息,看看当时的主机性能如何。

原文链接:http://blog.jobbole.com/46942/

新闻标题:我常用的主机监控Shell脚本
文章转载:http://www.shufengxianlan.com/qtweb/news3/15003.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联