linu系统脚本监控

前两天公司要求改一下系统的监控脚本,这里分享一下。
 
部署监控的步骤是这样的:把initforshellmonitor.sh和system-monitor.sh传到同一目录下,运行initforshellmonitor.sh部署监控即可。
以下是脚本内容
cat initforshellmonitor.sh
#!/bin/sh
#crontab
crontab -l>tmp
echo "0,5,10,15,20,25,30,35,40,45,50,55 * * * * `pwd`/system-person-monitor.sh > /dev/null 2>&1">>tmp
crontab tmp
rm -rf tmp
#initialization of parameter
##loadfazhi
loadfazhi=$[`cat /proc/cpuinfo |grep processor |wc -l`*2]
loadfazhi=$`echo $loadfazhi` | sed 's/./\\&/g'
echo "loadfazhi="$loadfazhi
sed -i "s/loadfazhi\=/loadfazhi\=$loadfazhi/" system-person-monitor.sh
#totaleth
for i in ` ifconfig |grep Ethernet|awk -F Link ' {printf $1}'`
do
eth=$eth" "$i
done
eth=$`echo $eth` | sed 's/./\\&/g'
echo "eth="$eth
sed -i "s/totaleth\=eth/totaleth\=\"$eth\"/" system-person-monitor.sh
#mkdir
##########
mkdir log
mkdir log/sms
mkdir log/load
mkdir log/cpu
mkdir log/memory
mkdir log/disk
mkdir log/netflux
mkdir log/ping
mkdir tmp
 
cat system-person-monitor.sh
#!/bin/sh
#fazhi#

loadfazhi=
loadcishu="2"#load次数,连续监测两次以上load值超出范围则告警,下同
cpufazhi="85"
cpucishu="3"
memfazhi="99"
memcishu="3"
pingfazhi="200"
pingcishu="5"
diskfazhi="95"
netfazhi="5000"
totaleth=eth
msgid=phonenumber
##############################################################################################
#variable#
dir1="log"
time1=`date  +%Y%m%d"-"%T`
time2=`date +%Y%m%d`
ipis=`ifconfig |grep  192.168|awk -F : '{printf $2}' |awk -F " " '{printf $1}'`
#preparation#
sendmsg()
{
msginsending="alarm $1 $2 $3 $4 $5 $time1"
echo $msginsending >>log/sms/log
}
check_load()
{
#load#
load=`uptime|awk -F "average: " '{print $2}'`
echo $load
loadtmp=`echo $load |awk -F "." '{ print $1}'`
if [ $loadtmp -ge $loadfazhi ] ; then
echo "waring load : $ipis $time1 load = $load" >> log/load/$ipis.alltest.system.$time2.log
echo "history load : $ipis $time1 load = $load" >> log/load/$ipis.alltest.system.$time2.log
echo "1"  >> tmp/load.tmp
else
echo "history load : $ipis $time1 load = $load" >> log/load/$ipis.alltest.system.$time2.log
>tmp/load.tmp
fi
loadb=`cat tmp/load.tmp|wc -l`
if [ $loadb -ge $loadcishu ] ; then
sendmsg $ipis load= $loadtmp
echo "sms load : $ipis $time1 load = $loadtmp" >> log/load/$ipis.alltest.system.$time2.log
echo yes
else
echo no
fi
}
#cpu#
check_cpu()
{
cpukong=`vmstat 1 2|tail -n 1 |awk '{print $15}'`
cpu=`expr 100 - $cpukong`
echo $cpu
if [ $cpu -ge $cpufazhi ] ; then
echo "alarm cpu : $ipis $time1 cpu used = $cpu%!" >> log/cpu/$ipis.alltest.system.$time2.log
echo "history cpu : $ipis $time1 cpu used = $cpu%!" >> log/cpu/$ipis.alltest.system.$time2.log
echo "1"  >> tmp/cpu.tmp
else
echo "history cpu : $ipis $time1 cpu used = $cpu%!" >> log/cpu/$ipis.alltest.system.$time2.log
>tmp/cpu.tmp
fi
cpub=`cat tmp/cpu.tmp|wc -l`
if [[ $cpub -ge $cpucishu ]] ; then
#ssh monitor "fetion.sh phone="$msgid" msg='alarm cpu : $ipis $time1 cpu = $cpu%'"
sendmsg $ipis cpu= $cpu%
echo "sms cpu : $ipis $time1 cpu used = $cpu%!" >> log/cpu/$ipis.alltest.system.$time2.log
echo yes
else
echo no
fi
}
#diskspace#
check_disk()
{
percent=`df -h | grep -v Used | grep -v boot | grep -v mnt | grep -v shm | awk '{if ( $1 ~/^[0-9]/ ) print $4 ; else print $5}'| grep -v ^$ | sed "s/%/ /g"`
diski=0
for i in $percent
do
   if [ $i -gt $diski ]
      then   diski=$i
   fi
done
    diskweizhi=`df -h | sed "s/%/ /g" | awk -v a="$diski" '$4 == a  || $5 == a' | awk '{if ( $1 !~/^[0-9]/ ) print $6"Avail-"$4 ;else pri
nt $5"-"$3 }'`
echo "history disk : $ipis $time1 max disk used is $diski%, $diskweizhi!" >> log/disk/$ipis.alltest.system.$time2.log
echo space $diski
if [ $diski -ge $diskfazhi ] ; then
   echo $diski
  echo "alarm disk : $ipis $time1 max disk used is $diski%, $diskweizhi !" >> log/disk/$ipis.alltest.system.$time2.log
  sendmsg $ipis $diskweizhi disk used is $diski%
  unset diskweizhi
  unset diski
  unset i
  unset percent
fi
}
check_inode()
{
#disk inode#
percent=`df -i | grep -v Used | grep -v boot | grep -v mnt | grep -v shm | awk '{if ( $1 ~/^[0-9]/ ) print $4 ; else print $5}'| grep -v ^$ | sed "s/%/ /g"`
diski=0
for i in $percent
do
   if [ $i -gt $diski ]
      then   diski=$i
   fi
done
echo inode $diski
    diskweizhi=`df -i | sed "s/%/ /g" | awk -v a="$diski" '$4 == a  || $5 == a' | awk '{if ( $1 !~/^[0-9]/ ) print $6"Avail-"$4 ;else print $5"-"$3 }'`
echo "history disk : $ipis $time1 max inode used is $diski%, $diskweizhi!" >> log/disk/$ipis.alltest.system.$time2.log
if [ $diski -ge $diskfazhi ] ; then
   echo $diski
  echo "alarm disk : $ipis $time1 max inode used is $diski%, $diskweizhi !" >> log/disk/$ipis.alltest.system.$time2.log
 sendmsg $ipis $diskweizhi inode used is $diski%
  echo "sms disk : $ipis $time1 max inode used is $diski%, $diskweizhi !" >> log/disk/$ipis.alltest.system.$time2.log
  unset diskweizhi
fi
}
check_memory()
{
mem=`free|grep Mem|awk '{print $3/$2*100 }'|awk 'BEGIN { a=0 }; { a=a+$0 }; END { printf "%.2f\n", a/NR }'`
echo $mem
memtmp=`echo $mem |awk -F "." '{ print $1}'`
if [ $memtmp -ge $memfazhi ] ; then
echo "alarm memory : $ipis $time1 memory used = $mem%!" >> log/memory/$ipis.alltest.system.$time2.log
echo "history memory : $ipis $time1 memory used = $mem%!" >> log/memory/$ipis.alltest.system.$time2.log
echo "1"  >> tmp/memory.tmp
else
echo "history memory : $ipis $time1 memory used = $mem%!" >> log/memory/$ipis.alltest.system.$time2.log
>tmp/memory.tmp
fi
memb=`cat tmp/memory.tmp|wc -l`
if [ $memb -ge $memcishu ] ; then
sendmsg $ipis mem= $memtmp
echo "sms memory : $ipis $time1 memory used = $mem%!" >> log/memory/$ipis.alltest.system.$time2.log
echo yes
else
echo no
fi
}
check_netflux()
{
typeset in in_old dif_in
typeset out out_old dif_out
typeset timer
typeset eth
eth=$1
timer=$2
in_old=$(cat /proc/net/dev | grep $eth | sed -e "s/\(.*\)\:\(.*\)/\2/g" | awk ' { print $1 }' )
out_old=$(cat /proc/net/dev | grep $eth | sed -e "s/\(.*\)\:\(.*\)/\2/g" | awk ' { print $9 }' )
sleep ${timer}
in=$(cat /proc/net/dev | grep $eth | sed -e "s/\(.*\)\:\(.*\)/\2/g" | awk ' { print $1 }' )
out=$(cat /proc/net/dev | grep $eth | sed -e "s/\(.*\)\:\(.*\)/\2/g" | awk ' { print $9 }' )
dif_in=$(((in-in_old)/timer))
dif_in=$[dif_in/1024]
if [[ $dif_in -ge $netfazhi ]] ; then
sendmsg $ipis $eth netfluxIN=$dif_in"KByte/s"
echo "warning  $eth netfluxIN=$dif_in $time2" >>log/netflux/$ipis.alltest.system.$time2.log
echo yes
else
echo no
fi
dif_out=$(((out-out_old)/timer))
dif_out=$[dif_out/1024]
if [[ $dif_out -ge $netfazhi ]] ; then
sendmsg $ipis $eth netfluxOUT=$dif_out"KByte/s"
echo "warning  $eth netfluxOUT=$dif_out $time2" >>log/netflux/$ipis.alltest.system.$time2.log
echo yes
else
echo no
fi
echo "IN: ${dif_in} KByte/s OUT: ${dif_out} KByte/s" $time2>>log/netflux/$ipis.alltest.system.$time2.log
in_old=${in}
out_old=${out}
}
check_ping()
{
ping=`ping www.cnnic.com.cn -c 5 | awk -F'=' '/time=/ {n++;sum+=$NF+0}END{print sum/n}'|awk -F "." '{print$1}'`
echo $ping
if [ $ping -ge $pingfazhi ] ; then
    echo "alarm ping : $ipis $time1 ping = $ping ms!" >> log/ping/$ipis.alltest.system.$time2.log
    echo "history ping : $ipis $time1 ping = $ping ms!"  >> log/ping/$ipis.alltest.system.$time2.log
    echo "1"  >> tmp/ping.system.tmp
else
    echo "history ping : $ipis $time1 ping = $ping ms!"  >> log/ping/$ipis.alltest.system.$time2.log
    > tmp/ping.system.tmp
fi
pingb=`cat tmp/ping.system.tmp|wc -l`
if [ $pingb -ge $pingcishu ] ; then
sendmsg $ipis ping= $ping ms
  echo "sms  $ipis  ping = $ping ms! $time1" >> log/ping/$ipis.alltest.system.$time2.log
echo yes
else
echo no
fi
}
check_ping
for i in $totaleth
do
echo $i
check_netflux $i 15 &
done
check_memory
check_inode
check_disk
check_cpu
check_load
----------------------------------------------
发告警用的是飞性・・・・・
另外祝大家和自己新年快乐!

你可能感兴趣的:(职场,休闲,linux脚本监控)