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

zabbix监控之自定义item

时间:2018-12-09 01:05:20      阅读:298      评论:0      收藏:0      [点我收藏+]

标签:uptime   遇到   hone   ret   时间   sys   status   connect   mod   

 

 

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

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

[root@aliyun ~]# 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])

 手动执行:

[root@zabbix-server 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

[root@zabbix-server zabbix_agentd.d]# cat userparameter_script.conf 
UserParameter=script.getQueryCountFromMysql.py,/usr/lib/zabbix/alertscripts/getQueryCountFromMysql.py

 格式:UserParameter=key,command

手动在zabbix-server端获取值:

[root@zabbix-server 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的值已经可以显示出来了

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

[root@zabbix-server 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做下面配置:

[root@zabbix-server 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的值:

[root@zabbix-server alertscripts]# zabbix_get -s 172.16.23.131 -k script.getmysqlstatu.py[qps]
7

 

zabbix监控之自定义item

标签:uptime   遇到   hone   ret   时间   sys   status   connect   mod   

原文地址:https://www.cnblogs.com/jsonhc/p/10089873.html

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