zabbix监控之自定义item

Posted jsonhc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了zabbix监控之自定义item相关的知识,希望对你有一定的参考价值。

 

 

zabbix安装完成后,当需要使用自定义脚本构建自定义item必须注意以下几点:

1.首先使用zabbix_get手动在zabbix-server服务端获取监控的默认的item值,如下:

[[email protected] ~]# zabbix_get -s 39.106.219.238 -p 10050 -k "system.swap.size[,free]"
0

 当遇到zabbix_get命令没有找到时,解决办法:yum install -y zabbix_get

2.在zabbix-server端打开配置文件找到:

AlertScriptsPath=/usr/lib/zabbix/alertscripts

默认将自定义的脚本放在该位置

3.在zabbix-agent端打开配置文件找到:

Include=/usr/local/etc/zabbix_agentd.conf.d/.conf

确认上面几步后,现在开始创建自定义脚本创建自定义item:

1.编写item脚本:

#!/usr/bin/env python
import mysqldb

class Db:
    def __init__(self, host1, user1, password1, db1):
        self.host = host1
        self.user = user1
        self.password = password1
        self.db = db1
        try:
            self._conn = MySQLdb.connect(host=self.host, user=self.user, passwd=self.password, db=self.db)  
        except Exception as e:
            print e
        self._cursor = self._conn.cursor()  

    def fetchone(self, sql1):
        self._cursor.execute(sql1)   
        result = self._cursor.fetchone()   
        return result


host = ‘172.16.23.131‘
user = ‘root‘
db = ‘mysql‘
password = ‘redhat‘
sql = "show global status like ‘questions‘;"
sql1 = "show global status like ‘uptime‘;"
if __name__ == ‘__main__‘:
    db_conn = Db(host, user, password, db)
    result = db_conn.fetchone(sql)
    result1 = db_conn.fetchone(sql1)
    print int(result[1])/int(result1[1])

 手动执行:

[[email protected] zabbix_agentd.d]# python /usr/lib/zabbix/alertscripts/getQueryCountFromMysql.py 
7

 将该脚本赋予执行权限:chmod +x /usr/lib/zabbix/alertscripts/getQueryCountFromMysql.py

2.在zabbix-agent端编写配置文件:重启zabbix-agent服务

/etc/zabbix/zabbix_agentd.d/userparameter_script.conf

[[email protected] zabbix_agentd.d]# cat userparameter_script.conf 
UserParameter=script.getQueryCountFromMysql.py,/usr/lib/zabbix/alertscripts/getQueryCountFromMysql.py

 格式:UserParameter=key,command

手动在zabbix-server端获取值:

[[email protected] zabbix_agentd.d]# zabbix_get -s 172.16.23.131 -k script.getQueryCountFromMysql.py 
7

 手动获取到值后,在zabbix页面进行配置:

1.创建自定义template:

 

技术分享图片

2.创建自定义item:

技术分享图片

3.创建自定义graph:

技术分享图片

上面各步骤创建完成后,将此template加在host上:

技术分享图片

 

 过一段时间后,查看graphs:

技术分享图片

 

 可以看出自定义item的值已经可以显示出来了

 如果自定义脚本带有参数的话,做如下修改:

[[email protected] alertscripts]# cat /usr/lib/zabbix/alertscripts/getmysqlstatu.py 
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import MySQLdb
import sys

class Db:
    def __init__(self, host1, user1, password1, db1):
        self.host = host1
        self.user = user1
        self.password = password1
        self.db = db1
        try:
            self._conn = MySQLdb.connect(host=self.host, user=self.user, passwd=self.password, db=self.db)  # 连接mysql
        except Exception as e:
            print e
        self._cursor = self._conn.cursor()  # 创建游标

    def fetchone(self, sql1):
        self._cursor.execute(sql1)   # 执行sql语句
        result = self._cursor.fetchone()    # 获取执行的结果
        return result

    def getops(self):
        questions = db_conn.fetchone(sql_questions)
        uptime = db_conn.fetchone(sql_uptime)
        return int(questions[1])/int(uptime[1])


host = ‘127.0.0.1‘
user = ‘root‘
db = ‘mysql‘
password = ‘redhat‘
sql_questions = "show global status like ‘questions‘;"
sql_uptime = "show global status like ‘uptime‘;"
if __name__ == ‘__main__‘:
    db_conn = Db(host, user, password, db)
    result = db_conn.getops()
    if sys.argv[1] == ‘qps‘:
        print result

 

# chmod +x getmysqlstatu.py

 然后在zabbix-agent做下面配置:

[[email protected] alertscripts]# cat /etc/zabbix/zabbix_agentd.d/userparameter_script.conf 
UserParameter=script.getQueryCountFromMysql.py,/usr/lib/zabbix/alertscripts/getQueryCountFromMysql.py
UserParameter=script.getmysqlstatu.py[*],/usr/lib/zabbix/alertscripts/getmysqlstatu.py $1

 在zabbix-server端手动获取item的值:

[[email protected] alertscripts]# zabbix_get -s 172.16.23.131 -k script.getmysqlstatu.py[qps]
7

 

以上是关于zabbix监控之自定义item的主要内容,如果未能解决你的问题,请参考以下文章

第十篇Zabbix监控项之自定义监控磁盘IO

(十三)Zabbix_linux添加定义的ITEM监控项

zabbix自定义监控

zabbix自定义监控项一

Zabbix添加主机监控及自定义item监控

zabbix监控实例