此笔记为监控  mysql数据 剩余容量

首先要在mysql上创建一个测试表 在test数据库里use test;

create table for_nagios_monitor ( id int(10) not null auto_increment, primary key(id)) ENGINE=InnoDB DEFAULT CHARSET=gbk;

flush privileges;


脚本退出状态
exit 0  OK
exit 1  WARNING
exit 2  CRITICAL
exit 3  UNKNOWN

被监控端:
1.把脚本放到 /libexec下
2.修改nrpe.cfg

command[check_mysql_free_space]=/usr/bin/sudo sh /usr/local/nagios-64/libexec/check_mysql_free_space.sh -w 500000 -c 490
##########因为nagios权限不够,跑起这个脚本要root权限,所以加了sudo 命令上去,而执行sudo时,会需要输入密码
为了使nagios  sudo到 root事不用修改密码,所以要修改 /etc/sudoers
vi  /etc/sudoers:
添加
nagios ALL=(ALL) NOPASSWD:/usr/local/nagios/libexec/check_disk_health.sh
建议:最好进行sudo的一些控制,很多网方法就是nagios ALL=NOPASSWD:ALL
############################################设置nagios sudo时不需要输入密码,修改这个文件要在root权限下工作##
手动测试下结果
监控端:
 
  修改services.cfg

define service {
host_name        dog
service_description   check-mysql-free-space
check_period          24x7
max_check_attempts    4
normal_check_interval 3
retry_check_interval  2
contact_groups        heme
notification_interval   10
notification_period     24x7
notification_options    w,u,c,r
check_command           check_nrpe!check_mysql_free_space  

################################脚本内容

###############################################
 1 #!/bin/sh
      2
      3 if [ $# -le 3 ];then
      4         echo "USAGE: $0 -w xx -c yy"
      5         exit
      6 fi
      7 StatFile="/tmp/mysql_free_space.log"
      8 echo "show table status like 'for_nagios_monitor'\G" | /usr/local/mysql/bin/mysql test > $StatFile
      9 ####
     10 remain_size=`cat $StatFile|grep "InnoDB free"|awk '{printf($4);}'`
     11
     12 if [ "$remain_size" -le $4 ];
     13 then
     14         echo "CRITICAL: remain "$remain_size" KB"
     15 elif [ "$remain_size" -le $2 ];then
     16         echo "WARNING: remain "$remain_size" KB"
     17 else
     18   echo "OK: remain "$remain_size" KB"
     19 fi
     20
     21 rm  "$StatFile"
##############################小技巧##################################################
就是要确定问题在哪,经过一大番的查找眼睛偶尔看到一个有趣的小方法
修复nrpe.conf配置文件 在要检查的命令后面添加>>/tmp/output 2>&1 将错误导出到文件中进行查看
command[check_sda_health]=/usr/bin/sudo /usr/local/nagios/libexec/check_disk_health.sh -d /dev/sda >>/tmp/output 2>&1