本文主要是介绍nagios 监控脚本,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
脚本数量(
[root@localhost etc]# ll /shell/nagios/
total 28
-rwxr-xr-x 1 root root 372 Oct 31 10:25 check_cpu
-rwxr-xr-x 1 root root 694 Oct 31 11:42 check_disk1
-rwxr-xr-x 1 nagios nagios 366 Nov 1 14:10 check_disk1_siz
-rwxr-xr-x 1 root root 228 Nov 1 11:11 check_file_num
-rwxr-xr-x 1 root root 501 Oct 30 11:34 check_mem
-rwxr-xr-x 1 root root 420 Oct 30 11:34 check_net
-rwxr-xr-x 1 root root 358 Oct 30 11:36 check_pro
)
1、[root@localhost nagios]# cat check_cpu
#!/bin/bash
#
CPU_USE=`top -b1 n2|grep "Cpu"|awk NR==2|awk -F"," '{print $1}'|awk '{print $2}'|awk -F"us" '{print $1}'`
CPU_COM=`top -b1 n2|grep "Cpu"|awk NR==2|awk -F"," '{print $1}'|awk '{print $2}'|awk -F"%" '{print $1}'`
CPU_WAR=`expr $CPU_COM \> 80.0`
if [ $CPU_WAR -ne 1 ];then
echo "CPU OK.used:$CPU_USE"
exit 0
else
echo "CPU WARNING.used:$CPU_USE"
exit 1
fi
2、[root@localhost nagios]# cat check_mem
#!/bin/bash
#
mem_tatol=`free -m|awk NR==2 |awk '{print $2}'`
mem_use=`free -m|awk NR==2 |awk '{print $3}'`
mem_free=`free -m|awk NR==2 |awk '{print $4}'`
if [ $mem_use -lt 1900 ];then
echo "MEM OK: tatol:$mem_tatol"M",used:$mem_use"M",free:$mem_free"M""
exit 0
#elif [ $mem_use -gt 3500 ]&&[ $mem_use lt 3800 ];then
# echo "MEM WARNING: tatol:$mem_tatol"M",used:$mem_use"M",free:$mem_free"M""."
# exit 1
else
echo "MEM CRITICAL: tatol:$mem_tatol"M",used:$mem_use"M",free:$mem_free"M"."
exit 2
fi
3、[root@localhost nagios]# cat check_disk1
#!/bin/bash
#
DIS_PAT="$1"
#CHE_COM=`/usr/local/nagios/libexec/check_disk -w 10 -c 5% -p $DIS_PAT`
DIS_FRE=`/usr/local/nagios/libexec/check_disk -w 10 -c 5% -p $DIS_PAT|awk -F":" '{print $2}'|awk '{print $2}'`
DIS_USE=`/usr/local/nagios/libexec/check_disk -w 10 -c 5% -p $DIS_PAT|awk -F"=" '{print $3}'|awk -F"MB" '{print $1}'`
DIS_TAT=`expr $DIS_FRE + $DIS_USE`
DIS_WAR=`echo "scale=0;$DIS_TAT * 0.9 "|bc|awk -F"." '{print $1}'`
#USE_COM=`expr $DIS_USE \> $DIS_WAR`
if [ $DIS_USE -lt $DIS_WAR ];then
echo "DISK:$DIS_PAT OK. tatol:$DIS_TAT MB,used:$DIS_USE MB,free:$DIS_FRE MB"
exit 0
else
echo ""echo "DISK:$DIS_PAT WARNING. tatol:$DIS_TAT MB,used:$DIS_USE MB,free:$DIS_FRE MB"
exit 1
fi
[root@localhost nagios]# cat check_disk1_siz
#!/bin/bash
#
###取磁盘数量
a=0
for b in {a..z}
do
df -hT |grep sd$b >> /dev/null
if [ $? -eq 0 ];then
let a++
echo "sd$b $DIS_SIZ" >> /var/log/size.txt
else
break
fi
done
echo "硬盘块数:$a"
###定义配置文件格式
for pat in `df -hT|sed '1d'|awk '{print $7}'|grep -v "media"|grep -v "Mounted"`
do
/usr/local/nagios/libexec/check_disk1 $pat
done
[root@localhost nagios]#
4、[root@localhost nagios]# cat check_net
#!/bin/bash
#
NET_NUM="$1"
REC_TRAFFIC=`sar -n DEV 1 1 |grep $NET_NUM |awk '{print $5}'|awk NR==2`
SEN_TRAFFIC=`sar -n DEV 1 1 |grep $NET_NUM |awk '{print $6}'|awk NR==2`
COMPARE=`expr $REC_TRAFFIC \> 8000`
if [ $COMPARE -eq 0 ];then
echo "$NET_NUM OK.接收流量:$REC_TRAFFIC KB,发送流量:$SEN_TRAFFIC KB"
exit 0
else
echo "$NET_NUM WARNING.接收流量:$REC_TRAFFIC KB,发送流量:$SEN_TRAFFIC KB."
exit 1
fi
5、[root@localhost libexec]# cat check_pro
#!/bin/bash
#
PRO_NAME=$1
ps -elf |grep $PRO_NAME |grep -v "check_pro $PRO_NAME"|grep -v "grep $PRO_NAME" >> /dev/null
LIVE=`echo $?`
if [ $LIVE -eq 0 ];then
echo "$PRO_NAME OK. $PRO_NAME:running"
exit 0
else
echo "$PRO_NAME Critical. $PRO_NAME:stoped"
fi
6、[root@localhost libexec]# cat check_file_num
#!/bin/bash
#
FILE_DIR=$1
FILE_NUM=`ls -l $FILE_DIR|grep -v "^d"|wc -l`
FILE_NUM_1=`expr $FILE_NUM - 1`
if [ $FILE_NUM_1 -gt 100 ];then
echo "$FILE_DIR 目录中文件个数OK:$FILE_NUM_1 个"
exit 0
else
echo "$FILE_DIR 目录中文件个数WARNING:$FILE_NUM_1 个"
exit 1
fi
这篇关于nagios 监控脚本的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!