常用的主机监控的 Shell 脚本

系统 Linux
本文主要分享了几个常用的主机监控的 Shell 脚本,根据自己的需求写出的shell脚本更能满足需求,更能细化主机监控的全面性。大家可以根据自己的情况在进行修改,希望能给大家一点帮助。

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

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

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

责任编辑:黄丹 来源: 博客
相关推荐

2013-08-30 10:25:22

Shell主机监控

2010-10-13 09:45:50

Linux监控脚本

2014-08-13 14:48:01

LinuxShell脚本

2013-04-18 14:54:08

Linux监控脚本Linux监控

2010-03-26 15:28:05

Python编写

2012-02-20 23:02:15

Linux

2020-09-23 06:00:04

ShellLinux邮件监控

2014-05-16 11:38:27

Shell 脚本监控

2020-11-26 07:48:24

Shell 脚本内置

2019-11-13 08:31:43

Oracle数据库脚本

2022-06-09 08:07:15

Shell脚本Linux

2023-07-31 08:45:10

Shell脚本

2011-03-21 13:10:13

NagiosWindows

2019-10-24 07:57:37

Linuxshell获取时间

2021-05-12 10:17:15

Shell工具Linux

2017-06-26 16:04:11

LinuxShell命令

2022-05-02 18:29:35

bashshellLinux

2020-04-01 15:11:36

Shell命令Linux

2009-07-20 15:42:34

监控JRubyJProfiler

2010-06-01 10:32:04

linux Mrtg
点赞
收藏

51CTO技术栈公众号