设为首页 收藏本站
查看: 865|回复: 0

Nagios+pnp4nagios+rrdtool 安装配置为nagios添加自定义插件(三)

[复制链接]

尚未签到

发表于 2015-9-8 09:12:04 | 显示全部楼层 |阅读模式
  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需要这么久才能产生数据,才能出图。

  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-110877-1-1.html 上篇帖子: linux系统下Nagios+rrdtool+Pnp4nagios监控环境的搭建 下篇帖子: install nagios pnp4nagios on centos 6
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表