这里为了简单只写在linux系统下如何利用nagios对系统进行定制监控。nagios在linux的监控流程是使用nrpe客户端来调用/usr/local/nagios/libexec目录下的脚本,然后将获取的数据返回给nagios,那些脚本会返回不同的状态值以及一些信息。nagios就是根据这些返回值来判断服务状态是否正常以及是否需要告警。这些状态值说明如下:
1、返回的状态值为0,表示OK,没有异常。
2、返回的状态值为1,表示WARNING,会触发WARNING报警。
3、返回的状态值为2,表示CRITICAL,会触发CRITICAL报警。
4、返回状态为3,表示UNKNOWN,
那面详细得来说明如何通过脚本定制监控数据库的连接数:
第一步:先写好获取数据的脚本,脚本中明确定义返回的状态吗,脚本内容如下:
- #!/bin/bash
- #Written by carl zhang
- #This Nagios plugin can be check your mysql connections status
- USAGE_Method="$(basename $0) [-w|--warning] [-c|--critical] "
- USAGE_Value="WARNING value must be small than CRITICAL value: `basename $0` $*"
- MYSQLUSER=nagios
- MYSQLPS=nagiosadmin
- MYSQLBIN=/usr/local/mysql/bin/mysql
- STATE_OK=0
- STATE_WARNING=1
- STATE_CRITICAL=2
- STATE_UNKNOWN=3
- if [ $# -lt 4 ];then
- echo
- echo "Usage: $USAGE_Method"
- echo
- exit 0
- fi
- while [ $# -gt 0 ];do
- case "$1" in
- -w|--warning)
- shift
- WARNING=$1
- ;;
- -c|--critical)
- shift
- CRITICAL=$1
- ;;
- esac
- shift
- done
- if [[ $WARNING -eq $CRITICAL || $WARNING -gt $CRITICAL ]]
- then
- echo
- echo "$USAGE_Value"
- echo
- echo "Usage: $USAGE_Method"
- echo
- exit 0
- fi
- used_connections=$($MYSQLBIN -u$MYSQLUSER -p$MYSQLPS -e "show status like 'Threads_connected'\G" | grep "Value" | awk '{print $2}')
- max_connections=$($MYSQLBIN -u$MYSQLUSER -p$MYSQLPS -e "show variables like 'max_connections'\G" | grep "Value" | awk '{print $2}')
- PERCENT=$(bc <<< "scale=2;$used_connections/$max_connections" | tr '^.' ' ')
- if [[ "$PERCENT" -gt "$CRITICAL" ]]
- then
- echo "CRITICAL - used_connections is : $used_connections"
- exit 2
- fi
- if [[ "$PERCENT" -gt "$WARNING" && "$PERCENT" -lt "$CRITICAL" ]]
- then
- echo "WARNING - used_connections is : $used_connections"
- exit 1
- fi
- if [[ "$PERCENT" -lt "$WARNING" ]]
- then
- echo "OK - used_connections is : $used_connections"
- exit 0
- fi
因为这里涉及到mysql数据库的权限,因此需要在mysql中添加naigos帐号,这一步操作命令如下:
- GRANT PROCESS, FILE, SUPER, REPLICATION CLIENT ON *.* TO 'nagios'@'localhost' IDENTIFIED BY 'nagiosadmin';
- FLUSH PRIVILEGES;
第二步:配置nrpe.cnf配置文件,定义获取数据的命令,以供nagios服务器端调用,配置如下:
- vim /usr/local/nagios/etc/nrpe.cfg
- command[check_mysql_connections]=/usr/local/nagios/libexec/check_mysql_connections -w 75 -c 85
保存后,重新启动nrpe客户端,执行如下命令:
- killall nrpe
- /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
第三步:在nagios服务器端配置服务器调用该命令进行监控,配置如下:
- define service {
- use generic-service
- host_name zhongshan_server_xx_xxx
- service_description check_mysql_connections
- check_period 24x7
- notifications_enabled 1
- max_check_attempts 3
- normal_check_interval 3
- retry_check_interval 2
- contact_groups dbadmins
- notification_interval 10
- notification_period 24x7
- notification_options w,u,c,r
- check_command check_nrpe!check_mysql_connections
- }
最后检查配置问题以后,重新加载nagios的配置,具体操作命令如下:
- /etc/rc.d/init.d/nagios checkconfig
- /etc/rc.d/init.d/nagios reload
至此整个监控mysql连接数的流程就算添加完成了。其他的自定义监控也一样,拿脚本稍微改一改就能搞定了。希望对大家有所帮助!
阅读(1383) | 评论(0) | 转发(0) |