nagios博大精深,可以以shell、perl等语句为nagios写插件,来满足自己监控的需要。本文写mysql中tps、qps的插件,并把收集到的结果以图形形式展现出来,这样输出的结果就有一定的要求了。
编写插件tps qps
check_qps 插件如下内容
#!/bin/sh
mytool="/usr/local/mysql/bin/mysql-umy_perfor -pmy_perfor"
state_ok=0
state_warning=1
state_critical=2
state_unknown=3
Uptime=`echo "show /*50000 global */ status like'uptime'"|$mytool -N`
for i in $Uptime
do
uptime_new=$i
done
$mytool -e "select total_numfrom test.monitor_status where statu_item='Uptime' order by id desc limit1">/home/zhaohp/monitor.txt
sed -i -e '1d'/home/zhaohp/monitor.txt
uptime_old=`cat/home/zhaohp/monitor.txt`
$mytool -e "insert intotest.monitor_status values (0,'Uptime',$uptime_new,now())"
if [ -n "$uptime_old" ]
then
uptime=$[$uptime_new-$uptime_old]
# echo $uptime
fi
# ***********************check_qps*******************************
Question=`echo "show /*50000 global */ status like'Queries'"|$mytool -N`
for i in $Question
do
queries_new=$i
done
$mytool -e "select total_numfrom test.monitor_status where statu_item='Question' order by id desc limit1" >/home/zhaohp/monitor.txt
sed -i -e '1d'/home/zhaohp/monitor.txt
queries_old=`cat/home/zhaohp/monitor.txt`
$mytool -e "insert intotest.monitor_status values (0,'Question',$queries_new,now())"
if [ -n "$uptime_old" ]
then
qps=$((($queries_new-$queries_old)/$uptime))
# echo $queries_new
# echo $queries_old
# echo $uptime
if [ $qps -le 10000 ]; then
echo "qps is ok - qps is $qps |qps=$qps;15000;20000"
exit $state_ok
elif [ $qps -le 15000 ]; then
echo "warning - qps is $qps |qps=$qps;15000;20000"
exit $state_warning
elif [ $qps -le 20000 ]; then
echo " critical - qps is $qps |qps=$qps;15000;20000"
exit $state_critical
else
echo "unkown"
exit $state_unknown
fi
fi
check_tps 插件如下内容
#!/bin/sh
mytool="/usr/local/mysql/bin/mysql-umy_perfor -pmy_perfor"
state_ok=0
state_warning=1
state_critical=2
state_unknown=3
Uptime=`echo "show /*50000 global */ status like'uptime'"|$mytool -N`
for i in $Uptime
do
uptime_new=$i
done
$mytool -e "select total_numfrom test.monitor_tps where statu_item='Uptime' order by id desc limit1">/home/zhaohp/monitor_uptime.txt
sed -i -e '1d'/home/zhaohp/monitor_uptime.txt
uptime_old=`cat/home/zhaohp/monitor_uptime.txt`
$mytool -e "insert intotest.monitor_tps values (0,'Uptime',$uptime_new,now())"
if [ -n "$uptime_old" ]
then
uptime=$[$uptime_new-$uptime_old]
# echo $uptime
fi
# *********************** TPS monitor********************************
commit=`echo "show globalstatus like 'Com_commit'"|$mytool -N`
for i in $commit
do
commit_new=$i
done
rollback=`echo "show globalstatus like 'Com_rollback'"|$mytool -N`
for i in $rollback
do
rollback_new=$i
done
$mytool -e "select total_numfrom test.monitor_tps where statu_item='com_commit' order by id desc limit1" >/home/zhaohp/monitor_commit.txt
sed -i -e '1d'/home/zhaohp/monitor_commit.txt
commit_old=`cat/home/zhaohp/monitor_commit.txt`
# echo "commit_old is$commit_old "
$mytool -e "select total_numfrom test.monitor_tps where statu_item='com_rollback' order by id desc limit1" >/home/zhaohp/monitor_rollback.txt
sed -i -e '1d'/home/zhaohp/monitor_rollback.txt
rollback_old=`cat/home/zhaohp/monitor_rollback.txt`
# echo " rollback is $rollback_old"
$mytool -e "insert intotest.monitor_tps values (0,'com_commit',$commit_new,now())"
$mytool -e "insert intotest.monitor_tps values (0,'com_rollback',$rollback_new,now())"
if [ -n "$uptime_old" ]
then
#tps=$((($commit_new+$rollback_new-$commit_old-$rollback_old)/$uptime))
total_new=$(($commit_new+$rollback_new))
# echo "total_new is $total_new"
total_old=$(($commit_old+$rollback_old))
# echo "total_old is $total_old"
tps=$((($total_new-$total_old)/$uptime))
if [ $tps -le 500 ]; then
echo "tps is ok - tps is$tps | tps=$tps;600;700"
exit $state_ok
elif [ $tps -le 600 ]; then
echo "warning - tps is $tps |tps=$tps;600;700"
exit $state_warning
elif [ $tps -le 700 ]; then
echo " critical - tps is $tps |tps=$tps;600;700"
exit $state_critical
else
echo "unkown"
exit $state_unknown
fi
fi
需要两张表 放在test库
CREATETABLE `monitor_status` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`statu_item` varchar(50) DEFAULT '',
`total_num` bigint(20) DEFAULT '0',
`CreateDate` timestamp NOT NULL DEFAULT'0000-00-00 00:00:00',
PRIMARY KEY (`id`)
)ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATETABLE `monitor_tps` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`statu_item` varchar(50) DEFAULT '',
`total_num` bigint(20) DEFAULT '0',
`CreateDate` timestamp NOT NULL DEFAULT'0000-00-00 00:00:00',
PRIMARY KEY (`id`)
)ENGINE=MyISAM DEFAULT CHARSET=utf8;
创建用户,创建的用户既用于此脚本又用于check_mysql_health插件
grantselect,update,insert,delete on test.* to 'my_perfor'@'localhost' identified by'my_perfor';
给脚本 check_qps、check_tps 赋权
chmod 755 /usr/local/nagios/libexec/check_qps
chmod 755 /usr/local/nagios/libexec/check_tps
创建存放文件目录
mkdir /home/zhaohp/
改变属主和权限
chown-R nagios.nagios /home/zhaohp/
chmod-R 777 /home/zhaohp/
注意脚本插件输出格式,不然pnp 脚本抓取不到数据
为什么不能出图
FAQ :
1、为什么/usr/local/nagios/share/perfdata/目录中生成不出数据
确认是否是权限问题。因为当时装好PNP时太性急了,看到PERFDATA目录没有生成数据就自己手动创建了两个主机名称的目录,(因为RRDTOOL需要过一会才会创建数据),这样导致权限不对,无法生成数据
因为创建这两目录所属用户和组成了root, nagios用户没有权限写入到root权限的目录中。
所以才创建不出数据。
2、为什么/usr/local/nagios/share/perfdata目录中还是没有数据生成,
解决方法:安装pnp的时候是否make install-config安装了模板安装了这些模板后进入/usr/local/nagios/etc/pnp目录中去掉后面的扩展。
是否在services.cfg文件中为服务添加了process_perf_data 1
这一项。否则不会生成数据的
我们在做完上面的这些后发现还是没有数据产生,这时你就要等五分到十分钟。因为pnp需要这么久才能产生数据,才能出图。