Common Shell scripts for host monitoring

Source: Internet
Author: User

Recently, some people have asked me questions about server monitoring from time to time. I asked if common server monitoring software, such as cacti and nagios, can I write shell scripts by myself? The shell scripts written based on your needs can better meet your needs and refine the comprehensiveness of host monitoring.

The following are some of my commonly used host monitoring scripts. You can modify them based on your situation and hope to help you.

1. View host Nic traffic

 
 
  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. system status monitoring

 
 
  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. Monitor the disk space of the host. If the disk space exceeds 90%, send a mail to send a warning.

 
 
  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. Monitor CPU and memory usage

 
 
  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. Comprehensive host monitoring

 
 
  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 & 

In crontab, automatic execution is performed hourly:

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

The cpu, memory, network, and IO statistics of each hour are generated in the/home/oslog/host_yyyymmdd/hh directory.

If a problem occurs in a certain period of time, you can view the corresponding log information to see how the host performance was at that time.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.