This paper introduces several common Linux monitoring scripts, which can realize automatic monitoring and alarming of host network card traffic, system condition, host disk space, CPU and memory usage, etc. Shell scripts written according to your needs are better able to meet the requirements, and can refine the overall monitoring of the host.
Recently, some friends from the internet asked me about the server monitoring problems, ask common server monitoring in addition to using open source software, such as: Cacti,nagios Monitor can write their own shell scripts? Shell scripts written to their needs are more satisfying It can refine the overall monitoring of the host.
Below is a few of my commonly used host monitoring scripts, we can change according to their own situation, and hope to give you a little help.
1. Check the host network card traffic
#!/bin/bash #network #Mike. Xu while:; Do time= ' date +%m '-"%d" "%k": "%m ' day= ' date +%m"-"%d ' rx_before= ' ifconfig eth0|sed-n" 8 "P|awk ' {print \$2} ' |cut-c7-' Tx_ Before= ' ifconfig eth0|sed-n "8" P|awk ' {print \$6} ' |cut-c7-' sleep 2 rx_after= ' ifconfig eth0|sed-n ' 8 ' P|awk ' {print \$2} ' |cut-c7-' tx_after= ' ifconfig eth0|sed-n "8" P|awk ' {print \$6} ' |cut-c7-' rx_result=\$[(rx_after-rx_before)/256] Tx_ result=\$[(Tx_after-tx_before)/256] echo "\ $time now_in_speed:" \ $rx _result "kbps now_out_speed:" \ $tx _result "Kbps" Sleep 2 Done
2. System condition Monitoring
#!/bin/sh #systemstat. Sh #Mike. Xu ip=192.168.1.227 top-n 2| grep "Cpu"./temp/cpu.txt free-m | grep "Mem"./temp/mem.txt df-k | grep "sda1"./temp/drive_sda1.txt #df-k | grep sda2 "./temp/drive_sda2.txt df-k | grep "/mnt/storage_0"./temp/mnt_storage_0.txt df-k | grep "/mnt/storage_pic"./temp/mnt_storage_pic.txt time= ' Date +%m '. %d ""%k ":"%M ' connect= ' Netstat-na | grep "219.238.148.30:80" | Wc-l ' echo ' \ $time \ $connect './temp/connect_count.txt
3, monitor the disk space of the host, when the use of more than 90% in space by sending mail to send a warning
#!/bin/bash #monitor Available disk space space= ' DF | Sed-n '/\/\$/P ' | Gawk ' {print \$5} ' | Sed ' s/%//' if [\ $SPACE-ge] Then [email protected] fi
4. Monitor CPU and Memory usage
#!/bin/bash #script to capture system statistics Outfile=/home/xu/capstats.csv
Date= ' Date +%m/%d/%y '
Time= ' Date +%k:%m:%s '
timeout= ' uptime '
Vmout= ' Vmstat 1 2 '
Users= ' echo \ $TIMEOUT | Gawk ' {print \$4} '
Load= ' echo \ $TIMEOUT | Gawk ' {print \$9} ' | Sed "s/,//"
Free= ' echo \ $VMOUT | Sed-n '/[0-9]/p ' | Sed-n ' 2p ' | Gawk ' {print \$4} '
Idle= ' echo \ $VMOUT | Sed-n '/[0-9]/p ' | Sed-n ' 2p ' |gawk ' {print \$15} '
echo "\ $DATE, \ $TIME, \ $USERS, \ $LOAD, \ $FREE, \ $IDLE" \ $OUTFILE
5, all-round monitoring of the host
#!/bin/bash # check_xu.sh # 0 * * * */home/check_xu.sh dat= "' Date +%y%m%d '" hour= "' Date +%h '" dir= "/home/oslog/host_\${da T}/\${hour} "delay=60 count=60 # whether the responsible directory exist if! Test-d \${dir} then/bin/mkdir-p \${dir} Fi # General check export term=linux/usr/bin/top-b-D \${delay}-N \${count} > \${dir}/top_\${dat}.log 2>&1 & # CPU Check/usr/bin/sar-u \${delay} \${count} > \${dir}/cpu_\${dat}.lo G 2>&1 & #/usr/bin/mpstat-p 0 \${delay} \${count} > \${dir}/cpu_0_\${dat}.log 2>&1 & #/usr/bin/m Pstat-p 1 \${delay} \${count} > \${dir}/cpu_1_\${dat}.log 2>&1 & # memory Check/usr/bin/vmstat \${DELAY} \ ${count} > \${dir}/vmstat_\${dat}.log 2>&1 & # I/O check/usr/bin/iostat \${delay} \${count} > \${DIR}/io Stat_\${dat}.log 2>&1 & # Network Check/usr/bin/sar-n DEV \${delay} \${count} > \${dir}/net_\${dat}.log 2& Gt;&1 & #/usr/bin/sar-n Edev \${delay} \${count} > \${diR}/net_edev_\${dat}.log 2>&1 &
Put it in crontab. Auto-execute every hour:
0 * * * */home/check_xu.sh
This generates hourly CPU, memory, network, and IO statistics in the/HOME/OSLOG/HOST_YYYYMMDD/HH directory.
If a time period is a problem, you can look at the corresponding log information to see how the host performance.
Several common Linux operating system monitoring scripts