zabbix主动上报mysql数据库内容

zabbix_sender命令支持主动上报数据,web服务端添加对应机器和采集器即可。

2015年刚接触zabbix时候,用的上报sqlserver脚本是select数据后插入到临时表,bcp下载到本地txt,zabbix_sender在汇报数据。

最近需要一个网站留言数量查询图形,数据库是mysql的,10分钟统计一次,统计当前时间戳10分钟内count和。

# -*- coding: utf-8 -*-
# @Time    : 2018/7/16 10:49
# @Author  : GuoYabin
# @Email   : [email protected]
# @File    : mysql-liuyan.py

import pymysql
import time
import os


class mysqlpiplines(object):

    def __init__(self):
        self.db=pymysql.connect(
       host="127.0.0.1",
        user="zabbix",
       password="P#S^6b",
       database="jmw_message",)
        self.cursor=self.db.cursor()


    def select_sql(self):
        #当前时间
        nowtime  = int(time.time())
        #10分钟之前
        lasttime = nowtime - 600
        selectsql="select count(publish_time) from t_message where publish_time > %s and publish_time < %s" % (lasttime,nowtime)
        self.cursor.execute(selectsql)
        return (self.cursor.fetchone()[0])

    def __del__(self):
        self.db.close()

if __name__ == '__main__':
    zabbix_sender="/usr/local/service/zabbix/bin/zabbix_sender"
    #zabbix_sender = "D:\\zabbix\\zabbix_sender.exe"
    zabbix_server="192.168.1.1"
    my_ip="10.1.2.100"
    mysqlexe = mysqlpiplines()
    os.system("%s -z %s -s %s -k %s -o %s -vv" % (zabbix_sender,zabbix_server,my_ip,'liuyan',mysqlexe.select_sql()))

  

python3系列写的,需要安装pymysql

猜你喜欢

转载自www.cnblogs.com/guoyabin/p/9352911.html
今日推荐