当前位置:   article > 正文

Zabbix数据Python处理简单实现_python zabbix 怎么修改数据

python zabbix 怎么修改数据
# coding=utf-8
import os
import time
import pymysql

conn = pymysql.connect(host='localhost', user='root', password='',port=13306, database='zabbix', read_timeout=60,write_timeout=60)
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
sql0 = 'select itemid items;'
rows = cursor.execute(sql0)

clock = os.popen("/usr/bin/date -d '-7days' '+%s'").read().strip()

history_delete = "delete from history where itemid=%s and clock <%s"
history_uint_delete = "delete from history_uint where itemid=%s and clock <%s"
result = cursor.fetchall()

def write2file(name,content):
    with open("%s.txt"%name,"a") as f:
        f.write(content+"\n") 

for item in result:
    itemid = item["itemid"]
    try:
        timeb = time.time()
        history_rst = cursor.execute(history_delete%(itemid,clock))
        timea = time.time()
        time_diff = timea - timeb
        conn.commit()
        write2file("history_success",history_delete%(itemid,clock)+"******%s******"%time_diff)
        # 提交之后,获取刚插入的数据的ID
        # last_id = cursor.lastrowid
        # print(last_id)
    except Exception,e:
        print e
        write2file("history_failure",history_delete%(itemid,clock))
        conn.rollback()

    try:
        timeb_uint = time.time()
        history_uint_rst = cursor.execute(history_uint_delete%(itemid,clock))
        timea_uint = time.time()
        time_diff_uint = timea_uint - timeb_uint
        conn.commit()
        write2file("history_uint_success",history_uint_delete%(itemid,clock)+"******%s******"%time_diff_uint)
        last_id = cursor.lastrowid
    except Exception,e:
        print e
        write2file("history_uint_failure",history_uint_delete%(itemid,clock))
        conn.rollback()
    
cursor.close()
conn.close()

  • 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
  • 52
  • 53
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/49185
推荐阅读
相关标签
  

闽ICP备14008679号