码迷,mamicode.com
首页 > 其他好文 > 详细

zabbix 使用进阶(五)

时间:2016-11-16 03:08:15      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:zabbix-agent   key   

zabbix 使用进阶(五)

===============================================================================

概述:


===============================================================================

回顾:

技术分享


----------------------------------------------------------------------------------------

自定义key:

   ---

定义:

  • 在zabbix agent端的配置文件中由用户通过UserParameter指令定义用户自定义参数;

格式:

  • UserParamter=<key>,<command>

  • UserParamter=<key[*]>,<command> $1...$9

 注意:

  • 类似awk命令自带$1...$9,需要改写为$$1, $$2, ...$$9;

演示1:
  在agent端自定义key,并通过传递参数来获取相应的数据。

-------------------------------------------------------------------------------

1.自定义key不带参数

   1)首先编辑agent端(CentOS 7.2-2)的配置文件/etc/zabbix/zabbix_agentd.conf在UserParameter中定义获取空余内存的key,如下:

技术分享

  2)重启agent端的zabbix-agent服务,在zabbix-server端使用get可以正常获取数据,说明我们在agent端自定义的key起作用了;

技术分享

  3)现在,我们就可以在zabbix-web端使用在agent端自定义的key来监控CentOS 7.2-2主机上的空闲内存大小了,如下:

技术分享

技术分享


技术分享



自定义key可传递参数:

  1.上面我们定义的key不可传递参数,这样的话使用就不太灵活,比如在同一个监控项中有不同的指标(内存中有空闲内存,可用内存,总内存),所以能够传递参数的话,就可灵活的获取不同的指标。

  如下,还是编辑agent的配置文件,自定义有关内存相关的key,并且可以传递参数;

技术分享

  2.重启agent端的zabbix-agent服务,在zabbix-server端使用get可以正常获取传递不同参数下的对应的数据,如下:

技术分享


演示2:

  在agent端自定义可传递参数的key,来获取nginx的状态信息:

-------------------------------------------------------------------------------

  1.首先在agent端主机,编辑其nginx的配置文件/etc/nginx/conf.d/default.conf,添加能够获取status的locations,如下:

技术分享

 

  获取nginx状态信息如下:

[root@centos7 ~]# curl http://192.168.1.19/ngxstatus
Active connections: 1 
server accepts handled requests
 6 6 4 
Reading: 0 Writing: 1 Waiting: 0 

[root@centos7 ~]# curl -s http://localhost/ngxstatus |awk ‘/^Active/{print $3}‘  //获取Active信息
1
[root@centos7 ~]# curl -s http://localhost/ngxstatus |awk ‘NR==3{print $1}‘   //获取accepts信息
20

 2.如上,我们要想监控agent端nginx服务的一个状态信息,很容易实现,只需要在其agent的配置文件中定义key即可实现;但是如果想要监控nginx服务的所有状态信息(上面的7项)就需要在key中传递参数了,我们这里可以通过定义脚本传递参数来实现,如下:

[root@centos7 ~]# vim /usr/bin/ngxstatus.sh
#!/bin/bash
#description:获取nginx的状态信息
#
host=‘127.0.0.1‘
port=‘80‘
statusurl=‘/ngxstatus‘

active() {
       curl -s http://${host}:${port}${statusurl} | awk ‘/^Active/{print $3}‘
}

accepts() {
       curl -s http://${host}:${port}${statusurl} | awk ‘NR==3{print $1}‘
}

handled() {
      curl -s http://${host}:${port}${statusurl} | awk ‘NR==3{print $2}‘
}

requests() {
      curl -s http://${host}:${port}${statusurl} | awk ‘NR==3{print $3}‘
}

reading() {
      curl -s http://${host}:${port}${statusurl} | awk ‘NR==4{print $2}‘
}

writing() {
      curl -s http://${host}:${port}${statusurl} | awk ‘NR==4{print $4}‘
}

waiting() {
      curl -s http://${host}:${port}${statusurl} | awk ‘NR==4{print $6}‘
}

$1   //传递的参数

[root@centos7 ~]# chmod +x /usr/bin/ngxstatus.sh

 脚本测试如下:

[root@centos7 ~]# ngxstatus.sh active
1
[root@centos7 ~]# ngxstatus.sh accepts
27
[root@centos7 ~]# ngxstatus.sh handled
28
[root@centos7 ~]# ngxstatus.sh requests
27
[root@centos7 ~]# ngxstatus.sh reading
0
[root@centos7 ~]# ngxstatus.sh writing
1
[root@centos7 ~]# ngxstatus.sh waiting
0

  3.如上,脚本已经定义好了,现在我们去定义zabbix-agent的配置文件,自定义key来调用脚本来获取nginx的状态信息,如下:

技术分享

  4.重启agent端的zabbix-agent服务,在zabbix-server端使用get可以正常获取传递不同参数下的对应的数据,如下:


































zabbix 使用进阶(五)

标签:zabbix-agent   key   

原文地址:http://1992tao.blog.51cto.com/11606804/1873310

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!