Recently, some friends of the internet have asked me about the server monitoring problems, ask the common server monitoring in addition to using open source software, such as: Cacti,nagios monitoring can write their own shell script it? According to their own needs to write a shell script more to meet the needs, more fine-grained host monitoring of the comprehensiveness.
Here are a few of my common host monitoring scripts, we can make changes according to their own situation, hoping to give you a little help.
1, view the host network card traffic
#!/bin/bash
#network
#Mike. Xu
While:; Todo
Time= ' Date +%m '-"%d" "%k": "%m"
day= ' Date +%m '-'%d '
Rx_before= ' ifconfig eth0|sed-n "8" P|awk ' {print $} ' |cut-c7-'
Tx_before= ' ifconfig eth0|sed-n "8" P|awk ' {print $} ' |cut-c7-'
Sleep 2
Rx_after= ' ifconfig eth0|sed-n "8" P|awk ' {print $} ' |cut-c7-'
Tx_after= ' ifconfig eth0|sed-n "8" P|awk ' {print $} ' |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, monitoring the host's disk space, 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 $} ' | Sed ' s/%//'
If [$SPACE-ge 90]
Then
Fty89@163.com
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 $} '
Load= ' echo $TIMEOUT | Gawk ' {print $} ' | Sed "s/,//"
Free= ' echo $VMOUT | Sed-n '/[0-9]/p ' | Sed-n ' 2p ' | Gawk ' {print $} '
Idle= ' echo $VMOUT | Sed-n '/[0-9]/p ' | Sed-n ' 2p ' |gawk ' {print $} '
echo "$DATE, $TIME, $USERS, $LOAD, $FREE, $IDLE") $OUTFILE
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/Linux/
5, the Omni-directional monitoring host
#!/bin/bash
# check_xu.sh
# 0 * * * * */home/check_xu.sh
dat= "' Date +%y%m%d '"
Hour= "' Date +%h '"
Dir= "/home/oslog/host_${dat}/${hour}"
Delay=60
Count=60
# whether the responsible directory exist
if! Test-d ${dir}
Then
/bin/mkdir-p ${dir}