您的位置:首页 > 运维架构 > Linux

不错的LINUX 系统监控脚本

2013-03-20 17:15 267 查看
#! /bin/bash

export LANG=C

 
Email=XXX@A.COM,CCC@B.cn

 

time=`date "+%Y-%m-%d %H:%M:%S"`

 

log=`date +%Y-%m-%d`.log

config=config.ini

if [[ -f $config && -s $config ]];then

 

        for ip in `cat $config`

 

                do

 

# -------------------------------------------------------------------------------------------------

 

# MAIL

 

# -------------------------------------------------------------------------------------------------

 

        ssh $ip pwd &> /dev/null

 

        if [ $? -ne 0 ];then

 

                 echo "host:$ip SSH no open!" | mail -s "$ip SSH no log" $Email

 

                 echo "$time $ip SSH  inspect over,SSH:Failed.">>$log               

        else

 

                 echo "$time $ip SSH  inspect over,SSH:Ok." >>$log

 

# -------------------------------------------------------------------------------------------------

 

# TIME DA

 

# ----------------------------------------------------------

 

#config NTP Server

 

server="ntp.fudan.edu.cn"

 

ssh $ip /usr/sbin/ntpdate -s $server

 

ssh $ip /usr/sbin/hwclock --systohc

# -------------------------------------------------------------------------------------------------

 

# ping test,

 

# -------------------------------------------------------------------------------------------------

 

 

site=www.baidu.com

 

ssh $ip ping -c3 www.baidu.com >/dev/null

 

if [  $? -ne 0 ]; then

 

                   echo "host: $ip internet link DOWN !" | mail -s "$ip ping " $Email

 

                        echo "$time $ip LINK,network:Failed.">>$log

 

                else

 

                        echo "$time $ip LINK,network:Ok."   >>$log

 

fi

 

# -------------------------------------------------------------------------------------------------

 

# inspect disk

 

# -------------------------------------------------------------------------------------------------

 

space_warn="85"

 

ssh $ip df -P | grep "^/dev"| awk '{print $0}' | while read x

 

        do

 

                space_name=`echo $x | awk '{print $1}'`

 

                space_per=`echo $x | awk '{print $5}' | sed 's/%//g'`

                space_used=`echo $x | awk '{print $3}'`

 

               

#                disk=$[$space_used*2]

 

                if [ $space_per -ge $space_warn ];then

 

                                echo -e  "host: $ip disk $space_name  $space_used  $space_per %,!" | mail -s "$ip disK " $Email

 

                                echo -e "$time $ip  $space_name disk used inspect over,fettle:Failed.">>$log

 

                        else

 

                                echo -e "$time $ip $space_name  inspect over,fettle:Ok."  >>$log               

                fi

 

        done

 

# -------------------------------------------------------------------------------------------------

 

# swap

 

# -------------------------------------------------------------------------------------------------

 

swap_warn=60

 

swap_total=`ssh $ip free -m | grep "Swap" | awk '{print $2}'`

swap_free=`ssh $ip free -m | grep "Swap" | awk '{print $4}' `

swap_used=`ssh $ip free -m | grep "Swap" | awk '{print $3}' `

 

if [ $swap_used -ne 0 ];then

 

        swap_per=$[$swap_used*100/$swap_total]

 

        if [ $swap_per -ge $swap_warn ];then

 

                        echo "host:$ip Swap fress $swap_free M, $swap_per%,!" | mail -s "$ip SWAP" $Email

 

                        echo "$time $ip Swap  inspect over,fettle:Failed." >> $log

 

                else

 

                        echo "$time $ip  Swap inspect over,fettle:Ok." >> $log

 

                fi

 

fi       

# -------------------------------------------------------------------------------------------------

 

# CPU

 

# -------------------------------------------------------------------------------------------------

 

 

cpu_warn=80

 

# cpu_free=`ssh $ip top -b -n 1 | grep "Cpu" | awk '{print $5}' | sed 's/%id,//g'`

cpu_used=`ssh $ip mpstat 1 2 | grep "Average" | awk '{print $3}'`

 

# cpu_used=$(echo "100 - $cpu_free" | bc)

 

if [ `echo "$cpu_used >= $cpu_warn" | bc ` -eq 1 ];then

 

                echo "host: $ip CPU us $cpu_used%,!" | mail -s "$ip CPU " $Email

 

               echo "$time $ip  CPU inspect over,fettle:Failed." >> $log

 

        else

 

                echo "$time $ip CPU inspect over,fettle:Ok." >> $log       

fi

 

# -------------------------------------------------------------------------------------------------

 

# user

 

# -------------------------------------------------------------------------------------------------

 

users_max=2

 

users_now=`ssh $ip uptime | awk '{print $6}'`

 

if [ $users_now -ge $users_max ];then

 

                 echo -e  "$ip login user $users_now!" | mail -s "$ip user login" $Email

 

                echo "$time $ip user,inspect over,fettle:Failed." >> $log

 

        else

 

                echo "$time $ip user,inspect over,fettle:Ok." >> $log       

fi

 

# -------------------------------------------------------------------------------------------------

 

# 15 minute load  average 0.7(one cup)

 

# -------------------------------------------------------------------------------------------------

 

load_warn=0.2

 

cpu_num=`ssh $ip cat /proc/cpuinfo | grep -c "model name"`

 

load_num=`ssh $ip uptime | awk '{print $10}'|awk -F"," '{print $1}'`

 

load_average=`echo "scale=3; $load_num/$cpu_num" | bc`

 

#tmp=`echo "$load_average >= $load_warn" | bc`

#if [[  $load_average -ge $load_warn ]];then

if [ `echo "$load_average >= $load_warn" | bc` -eq 1 ];then

 

                echo "$ip  15 minute load average 0$load_average!" | mail -s "$ip load average " $Email

 

                echo "$time $ip load average inspect over,fettle:Failed." >> $log

 

        else

 

                echo "$time $ip load average inspect over,fettle:Ok." >> $log       

fi

 

# -------------------------------------------------------------------------------------------------

 

# IP network link  8000

 

# -------------------------------------------------------------------------------------------------

 

 

conns_warn=8000

 

ip_conns=`ssh $ip netstat -an | grep tcp | grep EST | wc -l`

 

if [ $ip_conns -ge $conns_warn ];then

 

        echo "$ip connections $ip_conns,!" | mail -s "$ip IP " $Email

 

                echo "$time $ip c
4000
onnections inspect over,fettle:Failed." >> $log

 

        else

 

                echo "$time $ip connections inspect over,fettle:Ok." >> $log       

       

fi

 

# -------------------------------------------------------------------------------------------------

 

# Apache TEST "200"

 

# -------------------------------------------------------------------------------------------------

 

httpd=`ssh $ip ps -ef  |grep httpd |awk '{if($3==1)print $0}'|awk '{if($1=="root")print $3 }'`

 

if [ "$httpd" != "1" ];then

 

        echo "$time $ip Apache,restart......" >> $log

 

        ssh $ip /etc/init.d/httpd restart &> /dev/null

 

        ssh $ip sleep 20

        httpd=`ssh $ip ps -ef  |grep httpd |awk '{if($3==1)print $0}'|awk '{if($1=="root")print $3 }'`

 

        if [ "$httpd" != "1" ];then

 

                result=`wget -o /dev/stdout "http://$ip/" | grep "HTTP"| awk '{print $6}'`

 

                if [ "$result" != "200" ];then

 

                         echo "host: $ip Apache http prot is down!" | mail -s "$ip Apache is down" $Email

 

                         echo "$time $ip Apache inspect over,fettle:Failed." >> $log

 

 

                else

 

                        echo "$time $ip Apache inspect over,fettle:Ok." >> $log

 

               

                fi

 

        else

 

                echo "HOST: $ip Apache server is down!" | mail -s "$ip Apache " $Email

 

                echo "$time $ip Apache inspect over,fettle:Failed." >> $log                       

        fi

 

else

 

        result=`wget -o /dev/stdout "http://$ip/" | grep "HTTP" | awk '{print $6}'`

 

                if [ "$result" != "200" ];then

 

                         echo "host: $ip  Apache server http link  is down!" | mail -s "$ip Apache" $Email

 

                         echo "$time $ip Apache inspect over,fettle:Failed." >> $log

                else

 

                        echo "$time $ip Apache inspect over,fettle:Ok." >> $log

 

                fi

 

fi

 

 

# -------------------------------------------------------------------------------------------------

 

# MySQL prot 3360

# -------------------------------------------------------------------------------------------------

 

PORT=`ssh $ip netstat -na|grep "LISTEN"|grep "3306"|awk -F[:" "]+ '{print $5}'`

 

if [[ $PORT == 3306 ]];then

 

        echo "$time $ip MySQL inspect over fettle: Ok." >> $log

 

else

 

        echo "$time $ip MySQL server is down restart......" >> $log

 

        ssh $ip /etc/init.d/mysqld restart &>/dev/null

 

        PORT=`ssh $ip netstat -na|grep "LISTEN"|grep "3306"|awk -F[:" "]+ '{print $5}'`

 

        if [[ $PORT -eq 3306 ]];then

 

                        echo "$time $ip  MySQL inspect over fettlea:Ok." >> $log

 

       else

 

                        echo "host $ip MySQL server is down!" | mail -s "$ip MySQL si down" $Email

 

                 echo "$time $ip MySQL inspect over,fettle:Failed." >> $log

 

       fi

 

fi

 

 

# -------------------------------------------------------------------------------------------------

 

# eth

 

# -------------------------------------------------------------------------------------------------

 

 

speed_warn=10240

 

send_before=`ifconfig eth0 | grep bytes | awk '{print $6}' | awk -F : '{print $2}'`

 

recv_before=`ifconfig eth0 | grep bytes | awk '{print $2}' | awk -F : '{print $2}'`

 

sleep 1

 

send_after=`ifconfig eth0 | grep bytes | awk '{print $6}' | awk -F : '{print $2}'`

 

recv_after=`ifconfig eth0 | grep bytes | awk '{print $2}' | awk -F : '{print $2}'`

 

send_bytes=`expr $send_after - $send_before`

 

recv_bytes=`expr $recv_after - $recv_before`

 

send_speed=`expr $send_bytes / 1024`

 

recv_speed=`expr $recv_bytes / 1024`

 

if [[ `echo "$send_speed >= $speed_warn" | bc` -eq 1  || `echo "$recv_speed >= $speed_warn" | bc` -eq 1 ]];then

 

                echo -e "$ip  speed $send_speed Kb/s(out)/$recv_speed Kb/s(in)!" | mail -s "$ip speed" $Email

 

                echo "$time $ip network inspect over,fettle:Failed." >> $log

 

        else

 

                echo "$time $ip network inspect over,fettle:Ok." >> $log       

fi

 

fi       

                done

 

else

 

        echo  "config feil is full "

 

fi

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  监控脚本