手机版
你好,游客 登录 注册
背景:
阅读新闻

自己写的监控磁盘空间的Nagios插件脚本

[日期:2012-07-18] 来源:Linux社区  作者:xiangliangyu2008 [字体:大 中 小]

Nrpe会把最后的输出及返回数值传给Nagios,Nagios接收到返回值ST_OK=0;ST_WR=1;ST_CR=2;ST_UK=3就能给出Ok 、Warning 、Critical、Unknown这4个状态,而Nrpe给的输出就能反应此服务的状态了。

监控磁盘空间举例:

  1. #!/bin/sh  
  2. ST_OK=0  
  3. ST_WR=1  
  4. ST_CR=2  
  5. ST_UK=3  
  6. print_help() {  
  7.     echo "$PROGNAME -w INT -c INT"  
  8.     echo "Options:"  
  9.     echo "  -w/--warning)"  
  10.     echo "     Sets a warning number"  
  11.     echo "  -c/--critical)"  
  12.     echo "     Sets a critical level for io"  
  13.     exit $ST_UK  
  14. }  
  15. while test -n "$1"; do  
  16.     case "$1" in  
  17.         -help|-h)  
  18.             print_help  
  19.             exit $ST_UK  
  20.             ;;  
  21.         --warning|-w)  
  22.             warning=$2  
  23.         shift  
  24.             ;;  
  25.         --critical|-c)  
  26.             critical=$2  
  27.         shift  
  28.             ;;  
  29.         *)  
  30.             echo "Unknown argument: $1"  
  31.             print_help  
  32.             exit $ST_UK  
  33.             ;;  
  34.         esac  
  35.     shift  
  36. done  
  37. get_wcdiff() {  
  38.     if [ ! -z "$warning" -a ! -z "$critical" ]  
  39.     then  
  40.         wclvls=1  
  41.   
  42.         if [ ${warning} -gt ${critical} ]  
  43.         then  
  44.             wcdiff=1  
  45.         fi  
  46.     elif [ ! -z "$warning" -a -z "$critical" ]  
  47.     then  
  48.         wcdiff=2  
  49.     elif [ -z "$warning" -a ! -z "$critical" ]  
  50.     then  
  51.         wcdiff=3  
  52.     fi  
  53. }  
  54. val_wcdiff() {  
  55.     if [ "$wcdiff" = 1 ]  
  56.     then  
  57.         echo "Please adjust your warning/critical thresholds. The warning \  
  58. must be lower than the critical level!"  
  59.         exit $ST_UK  
  60.     elif [ "$wcdiff" = 2 ]  
  61.     then  
  62.         echo "Please also set a critical value when you want to use \  
  63. warning/critical thresholds!"  
  64.         exit $ST_UK  
  65.     elif [ "$wcdiff" = 3 ]  
  66.     then  
  67.         echo "Please also set a warning value when you want to use \  
  68. warning/critical thresholds!"  
  69.         exit $ST_UK  
  70.     fi  
  71. }  
  72. get_status() {  
  73. if [ `df -h |grep " /data" |wc -l` -eq 1 ];then  
  74.  disk_stat=(`df -h | gawk '$NF == "/data" {print $0}'`)  
  75.   if [ `echo ${disk_stat[4]}|grep data |wc -l ` -eq 1 ];then  
  76.  empty_space=${disk_stat[2]}  
  77.  status=${disk_stat[3]}  
  78.   else  
  79.  empty_space=${disk_stat[3]}  
  80.  status=${disk_stat[4]}  
  81.   fi  
  82. else  
  83.  disk_stat=(`df -h | gawk '$NF == "/" {print $0}'`)  
  84.  if [ `echo ${disk_stat[0]} |grep "/" |wc -l` -eq 0 ];then  
  85.   empty_space=${disk_stat[2]}  
  86.   status=${disk_stat[3]}  
  87.  else  
  88.   empty_space=${disk_stat[3]}  
  89.   status=${disk_stat[4]}  
  90.  fi  
  91. fi  
  92.     status=`echo $status|cut -d'%' -f1`  
  93. }  
  94. get_wcdiff  
  95. val_wcdiff  
  96. get_status  
  97. if [ -n "$warning" -a -n "$critical" ]  
  98. then  
  99.     if [ `expr $status \> $warning` -eq 1 -a `expr  $status \< $critical` -eq 1 ]  
  100.     then  
  101.         echo "WARNING- disk_empty_space is $empty_space used $status %"  
  102.         exit $ST_WR  
  103.     elif [ `expr $status \> $critical` -eq 1 ]  
  104.     then  
  105.         echo "CRITICAL- disk_empty_space is $empty_space used $status %"  
  106.         exit $ST_CR  
  107.     else  
  108.         echo "OK- disk_empty_space is $empty_space used $status %"  
  109.         exit $ST_OK  
  110.     fi  
  111. fi  
linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款