码迷,mamicode.com
首页 > 其他好文 > 详细

zabbix (3)

时间:2020-06-22 09:17:35      阅读:52      评论:0      收藏:0      [点我收藏+]

标签:alt   pytho   filename   print   使用率   ofo   zha   itemid   add   

说明:
继续zabbix4.0的数据抽取展示测试。
对python不熟练, 所以先参考网友脚本,抽取zabbix数据,生成excel表格。
脚本如下:


#/usr/bin/env python
# -*- coding: utf-8 -*-

import MySQLdb
import xlsxwriter
import time

host_list = [‘192.168.164.131‘,
                ‘192.168.164.132‘
               ]

def sql_login(ip):
    conn = MySQLdb.connect(host=‘192.168.164.131‘ ,port=3306, user=‘root‘, passwd=‘123456‘, db=‘zabbix‘)
    cur = conn.cursor()
    sql_context = "select itemid, FROM_UNIXTIME(clock, ‘%Y-%m-%d %H:%i:%S‘), value  "                   "from  history_uint where itemid = (select itemid from items where items.key_ = ‘tcpconn‘ "                   "and items.hostid = (select hostid from hosts where host = ‘" + ip +                   "‘)) and clock >= UNIX_TIMESTAMP(‘2016-04-22 18:00:00‘) and clock < UNIX_TIMESTAMP(‘2016-04-24 00:00:00‘)"
    reCount = cur.execute(sql_context)
    nRet = cur.fetchall()
    cur.close()
    conn.close()

    print (reCount)
    #print nRet
    perfix = time.strftime(‘%y%m%d%H%M%S‘)
    xfilename = "./data/" + ip + "_conn_" + perfix + ".xlsx"
    workbook = xlsxwriter.Workbook(xfilename)
    worksheet1 = workbook.add_worksheet()
    worksheet1.write(‘A1‘, u‘主机地址‘)
    worksheet1.write(‘B1‘, u‘时间‘)
    worksheet1.write(‘C1‘, u‘TCP连接数‘)
    row = 1
    for i in nRet:
        print( i[0], i[1], i[2])
        worksheet1.write(row, 0, ip)
        worksheet1.write(row, 1, i[1])
        worksheet1.write(row, 2, i[2])
        row += 1
        if row == reCount:
            break
    workbook.close()

def run():
    for ip in host_list:
        sql_login(ip)

if __name__ == ‘__main__‘:

    run()

我需要的数据比较简单:
主机IP地址 CPU最大值 CPU最小值 内存使用率最大值 内存使用率最小值 磁盘/使用率
技术图片
接下里开始分析zabbix表结构,拿到最比较老的版本zabbix表结构说明,
https://blog.csdn.net/zhangxueleishamo/article/details/94400572
开始测试。

zabbix (3)

标签:alt   pytho   filename   print   使用率   ofo   zha   itemid   add   

原文地址:https://blog.51cto.com/12785714/2506218

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!