1 #!/bin/sh 2 # 3 #File: check_snmp_uptime.sh 4 # 5 #Date: 2012年08月17日 6 # 7 ################################################## 8 9 # Exit-Codes: 10 STATE_OK=0 11 STATE_WARNING=1 12 STATE_CRITICAL=2 13 STATE_UNKNOWN=3 14 15 PROGNAME=`/usr/bin/basename $0` 16 print_usage() 17 { 18 echo "Usage: $PROGNAME --help" 19 } 20 usage() 21 { 22 cat << EOF 23 usage: `basename $0` -H <address> -v <snmp_version> -c <community> 24 example: check_snmp_systime.sh -H 10.0.1.6 -v 2c -c public 25 EOF 26 } 27 28 if [ $# -lt 1 ]; then 29 print_usage 30 exit $STATE_UNKNOWN 31 fi 32 33 while [ ! -z "$1" ]; do 34 case $1 in 35 -v) shift; version=$1 ;; 36 -H) shift; HOSTADDRESS=$1 ;; 37 -c) shift; community=$1 ;; 38 --help) usage; exit ;; 39 esac 40 shift 41 done 42 43 match1="Timeticks" 44 match2="days" 45 46 status=$(snmpwalk -v $version -c $community $HOSTADDRESS sysUpTime.0) 47 48 if echo "$status" | grep -q "$match1"; then 49 50 systime=`snmpwalk -v $version -c $community $HOSTADDRESS sysUpTime.0 | awk -F "=" '{print $2}' | awk -F ")" '{print $2}'` 51 # 52 #snmpwalk -v $version -c $community $HOSTADDRESS sysUpTime.0 | awk -F "=" '{print $2}' 53 #Timeticks: (167220400) 19 days, 8:30:04.00 54 # 55 #snmpwalk -v $version -c $community $HOSTADDRESS sysUpTime.0 | awk -F "=" '{print $2}' | awk -F ")" '{print $2}' 56 #19 days, 8:30:04.00 57 # 58 if echo "$systime" | grep -q "$match2"; then 59 #if [ $? -eq 0 ]; then 60 #$sysytime 是否包含 $match,-q 不显示所有输出 61 days=`echo $systime | awk -F "days" '{print $1}'` 62 #19天 63 hours=`echo $systime | awk -F "," '{print $2}' | awk -F ":" '{print $1}'` 64 #8小时 65 minutes=`echo $systime | awk -F "," '{print $2}' | awk -F ":" '{print $2}'` 66 #30分钟 67 echo "已运行 $days天,$hours小时$minutes分钟" 68 exit $STATE_OK 69 #elif [ $? -ne 0 ]; then 70 else 71 hours=`echo $systime | cut -f1 -d":"` 72 minutes=`echo $systime | cut -f2 -d":"` 73 echo "已运行$hours小时$minutes分钟" 74 exit $STATE_OK 75 fi 76 else 77 exit $STATE_UNKNOW 78 79 fi