zabbix安裝完成後,當須要使用自定義腳本構建自定義item必須注意如下幾點:python
1.首先使用zabbix_get手動在zabbix-server服務端獲取監控的默認的item值,以下:mysql
[root@aliyun ~]# zabbix_get -s 39.106.219.238 -p 10050 -k "system.swap.size[,free]" 0
當遇到zabbix_get命令沒有找到時,解決辦法:yum install -y zabbix_getsql
2.在zabbix-server端打開配置文件找到:bash
默認將自定義的腳本放在該位置fetch
3.在zabbix-agent端打開配置文件找到:server
確認上面幾步後,如今開始建立自定義腳本建立自定義item:blog
1.編寫item腳本:ip
#!/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])
手動執行:get
[root@zabbix-server zabbix_agentd.d]# python /usr/lib/zabbix/alertscripts/getQueryCountFromMysql.py 7
將該腳本賦予執行權限:chmod +x /usr/lib/zabbix/alertscripts/getQueryCountFromMysql.pyit
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
獲取qps和tps雙指標:
[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)