随笔 - 120  文章 - 0  评论 - 35  阅读 - 85万

zabbix监控之自定义item

 

 

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

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

1
2
[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脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/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])

 手动执行:

1
2
[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

1
2
[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端获取值:

1
2
[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的值已经可以显示出来了

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
[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

 

1
# chmod +x getmysqlstatu.py

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

1
2
3
[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的值:

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

 获取qps和tps双指标:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
[root@zabbix-server alertscripts]# cat 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 getqps(self):
        questions = db_conn.fetchone(sql_questions)
        uptime = db_conn.fetchone(sql_uptime)
        return int(questions[1])/int(uptime[1])
 
    def gettps(self):
        Com_rollback = db_conn.fetchone(sql_Com_rollback)
        Com_commit = db_conn.fetchone(sql_Com_commit)
        uptime = db_conn.fetchone(sql_uptime)
        return (int(Com_rollback[1])+int(Com_commit[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';"
sql_Com_rollback = "show global status like 'Com_rollback';"
sql_Com_commit = "show global status like 'Com_commit';"
if __name__ == '__main__':
    db_conn = Db(host, user, password, db)
    if sys.argv[1] == 'qps':
        result_qps = db_conn.getqps()
        print result_qps
    if sys.argv[1] == 'tps':
        result_tps = db_conn.gettps()
        print float(result_tps)

  

posted on   wadeson  阅读(829)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示