• 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   : hbbdgyb@163.com
    # @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

  • 相关阅读:
    改写历史,永久删除git库的物理文件
    双调排序
    GitHub从无到有
    Nginx的安装与基本应用
    Django从无到有的艰苦历程
    pycharm 相关设置问题
    ORM介绍
    Django中的过滤器
    FBV和CBV的差异
    django中models field详解
  • 原文地址:https://www.cnblogs.com/guoyabin/p/9352911.html
Copyright © 2020-2023  润新知