设为首页 收藏本站
查看: 455|回复: 0

[经验分享] 如何采用Python zabbix_api 获取性能数据

[复制链接]

尚未签到

发表于 2019-1-23 06:16:17 | 显示全部楼层 |阅读模式
  如何采用Python zabbix_api 获取性能数据
  

  最近领导需要一份数据,OpenStack ,VMware,物理机之间的性能报告,在撰写报告之前需要数据支撑,我们采用的是zabbix 监控,需要采取一周内的历史数据作为对比,那数据如何获取,请看以下章节
  一:安装zabbix api 接口,修改zabbix api 调用接口,获取数据
  

*****************************************************************************************
easy_install zabbix_api
*****************************************************************************************
   
#!/usr/bin/python
# The research leading to these results has received funding from the
# European Commission's Seventh Framework Programme (FP7/2007-13)
# under grant agreement no 257386.
#http://www.bonfire-project.eu/
# Copyright 2012 Yahya Al-Hazmi, TU Berlin
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License

# this script fetches resource monitoring information from Zabbix-Server
# through Zabbix-API
#
# To run this script you need to install python-argparse "apt-get install python-argparse"
from zabbix_api import ZabbixAPI
import sys
import datetime
import time
import argparse
def fetch_to_csv(username,password,server,hostname,key,output,datetime1,datetime2,debuglevel):
        zapi = ZabbixAPI(server=server, log_level=debuglevel)
        try:
zapi.login(username, password)
except:
        print "zabbix server is not reachable: %s" % (server)
sys.exit()
        host = zapi.host.get({"filter":{"host":hostname}, "output":"extend"})
if(len(host)==0):
print "hostname: %s not found in zabbix server: %s, exit" % (hostname,server)
sys.exit()
else:
hostid=host[0]["hostid"]
print '*' * 100
print key
print '*' * 100
if(key==""):
print '*' * 100
items = zapi.item.get({"filter":{"hostid":hostid} , "output":"extend"})
if(len(items)==0):
print "there is no item in hostname: %s, exit" % (hostname)
                        sys.exit()
dict={}
for item in items:
dict[str(item['itemid'])]=item['key_']
if (output == ''):
                        output=hostname+".csv"
f = open(output, 'w')
str1="#key;timestamp;value\n"
if (datetime1=='' and datetime2==''):
for itemid in items:
itemidNr=itemid["itemid"]
                        str1=str1+itemid["key_"]+";"+itemid["lastclock"]+";"+itemid["lastvalue"]+"\n"
f.write(str1)
                        print "Only the last value from each key has been fetched, specify t1 or t1 and t2 to fetch more data"
f.close()
elif (datetime1!='' and datetime2==''):
try:
                        d1=datetime.datetime.strptime(datetime1,'%Y-%m-%d %H:%M:%S')
except:
print "time data %s does not match format Y-m-d H:M:S, exit" % (datetime1)
sys.exit()
                        timestamp1=time.mktime(d1.timetuple())
                        timestamp2=int(round(time.time()))
                inc=0
history = zapi.history.get({"hostids":[hostid,],"time_from":timestamp1,"time_till":timestamp2, "output":"extend" })
                for h in history:
                        str1=str1+dict[h["itemid"]]+";"+h["clock"]+";"+h["value"]+"\n"
                        inc=inc+1
                        f.write(str1)
                        f.close()
                        print str(inc) +" records has been fetched and saved into: " + output
elif (datetime1=='' and datetime2!=''):
for itemid in items:
                                itemidNr=itemid["itemid"]
                                str1=str1+itemid["key_"]+";"+itemid["lastclock"]+";"+itemid["lastvalue"]+"\n"
                        f.write(str1)
                        print "Only the last value from each key has been fetched, specify t1 or t1 and t2 to fetch more data"
                        f.close()
else:
try:
                        d1=datetime.datetime.strptime(datetime1,'%Y-%m-%d %H:%M:%S')
                        except:
                                print "time data %s does not match format Y-m-d H:M:S, exit" % (datetime1)
                                sys.exit()
try:
                        d2=datetime.datetime.strptime(datetime2,'%Y-%m-%d %H:%M:%S')
                        except:
                                print "time data %s does not match format Y-m-d H:M:S, exit" % (datetime2)
                                sys.exit()
                        timestamp1=time.mktime(d1.timetuple())
                        timestamp2=time.mktime(d2.timetuple())
inc=0
                        history = zapi.history.get({"hostids":[hostid,],"time_from":timestamp1,"time_till":timestamp2, "output":"extend" })
                        for h in history:
                                str1=str1+dict[h["itemid"]]+";"+h["clock"]+";"+h["value"]+"\n"
                                inc=inc+1
                        f.write(str1)
                        f.close()
                        print str(inc) +" records has been fetched and saved into: " + output
else:
#print "key is: %s" %(key)
        itemid = zapi.item.get({"filter":{"key_":key, "hostid":hostid} , "output":"extend"})
if(len(itemid)==0):
print "item key: %s not found in hostname: %s" % (key,hostname)
sys.exit()
        itemidNr=itemid[0]["itemid"]
        if (output == ''):
            output=hostname+".csv"
        f = open(output, 'w')
        str1="#key;timestamp;value\n"
        if (datetime1=='' and datetime2==''):
            str1=str1+key+";"+itemid[0]["lastclock"]+";"+itemid[0]["lastvalue"]+"\n"
            #f.write(str1)
f.write(str1)
f.close()
    print "Only the last value has been fetched, specify t1 or t1 and t2 to fetch more data"
        elif (datetime1!='' and datetime2==''):
            d1=datetime.datetime.strptime(datetime1,'%Y-%m-%d %H:%M:%S')
            timestamp1=time.mktime(d1.timetuple())
            timestamp2=int(round(time.time()))
    history = zapi.history.get({"history":itemid[0]["value_type"],"time_from":timestamp1,"time_till":timestamp2, "itemids":[itemidNr,], "output":"extend" })
            inc=0
            for h in history:
                str1 = str1 + key + ";" + h["clock"] +";"+h["value"] + "\n"
                inc=inc+1
f.write(str1)
                        f.close()
            print str(inc) +" records has been fetched and saved into: " + output
        elif (datetime1=='' and datetime2!=''):
    str1=str1+key+";"+itemid[0]["lastclock"]+";"+itemid[0]["lastvalue"]+"\n"
f.write(str1)
                        f.close()
    print "Only the last value has been fetched, specify t1 or t1 and t2 to fetch more data"
else:
            d1=datetime.datetime.strptime(datetime1,'%Y-%m-%d %H:%M:%S')
            d2=datetime.datetime.strptime(datetime2,'%Y-%m-%d %H:%M:%S')
            timestamp1=time.mktime(d1.timetuple())
            timestamp2=time.mktime(d2.timetuple())
            history = zapi.history.get({"history":itemid[0]["value_type"],"time_from":timestamp1,"time_till":timestamp2, "itemids":[itemidNr,], "output":"extend" })
            inc=0
            for h in history:
                str1 = str1 + key + ";" + h["clock"] +";"+h["value"] + "\n"
                inc=inc+1
            print str(inc) +" records has been fetched and saved into: " + output
        f.write(str1)
                        f.close()
parser = argparse.ArgumentParser(description='Fetch history from aggregator and save it into CSV file')
parser.add_argument('-s', dest='server_IP', required=True,
                   help='aggregator IP address')
parser.add_argument('-n', dest='hostname', required=True,
                   help='name of the monitored host')
parser.add_argument('-k', dest='key',default='',
                   help='zabbix item key, if not specified the script will fetch all keys for the specified hostname')
parser.add_argument('-u', dest='username', default='Admin',
                   help='zabbix username, default Admin')
parser.add_argument('-p', dest='password', default='zabbix',
                   help='zabbix password')
parser.add_argument('-o', dest='output', default='',
                   help='output file name, default hostname.csv')
parser.add_argument('-t1', dest='datetime1', default='',
                   help='begin date-time, use this pattern \'2011-11-08 14:49:43\' if only t1 specified then time period will be t1-now ')
parser.add_argument('-t2', dest='datetime2', default='',
                   help='end date-time, use this pattern \'2011-11-08 14:49:43\'')
parser.add_argument('-v', dest='debuglevel', default=0, type=int,
                   help='log level, default 0')
args = parser.parse_args()
fetch_to_csv(args.username, args.password, "http://"+args.server_IP+"/zabbix", args.hostname, args.key, args.output, args.datetime1,args.datetime2,args.debuglevel)  

  二:撰写通过key获取一周内的数据
  items : 在zabbix 中搜索主机,选择最新数据,找到项目(items),点击进入,能看到机器的所有keys,在负载到程序的items 字典中,程序会循环读取items ,获取数据
#/usr/bin/env python
#-*-coding:UTF-8
"""
    wget http://doc.bonfire-project.eu/R4.1/_static/scripts/fetch_items_to_csv.py
"""
import os,sys,time
users=u'admin'
pawd = 'zabbix'
exc_py = '/data/zabbix/fetch_items_to_csv.py'
os.system('easy_install zabbix_api')
os.system('mkdir -p /data/zabbix/cvs/')
if not os.path.exists(exc_py):
    os.system("mkdir -p /data")
    os.system("wget http://doc.bonfire-project.eu/R4.1/_static/scripts/fetch_items_to_csv.py -O /data/zabbix/fetch_items_to_csv.py")
def show_items(moniter, dip):
    items = dict()
    items['io_read_win'] = "perf_counter[\\2\\16]"
    items['io_write_win'] = "perf_counter[\\2\\18]"
    items['io_read_lin'] = "iostat[,rkB/s]"
    items['io_write_lin'] = "iostat[,wkB/s]"
    items['io_read_lin_sda'] = "iostat[sda,rkB/s]"
    items['io_write_lin_sda'] = "iostat[sda,wkB/s]"
    items['io_read_lin_sdb'] = "iostat[sdb,rkB/s]"
    items['io_write_lin_sdb'] = "iostat[sdb,wkB/s]"
    # Add items, iostate vdb,vdb
    items['io_read_lin_vda'] = "iostat[vda,rkB/s]"
    items['io_write_lin_vda'] = "iostat[vda,wkB/s]"
    items['io_read_lin_vdb'] = "iostat[vdb,rkB/s]"
    items['io_write_lin_vdb'] = "iostat[vdb,wkB/s]"
    items['cpu_util'] = "system.cpu.util"
    items['net_in_linu_vm_web'] = "net.if.in[eth0]"
    items['net_out_lin_vm_web'] = "net.if.out[eth0]"
    items['net_in_linu_vm_db'] = "net.if.in[eth1]"
    items['net_out_lin_vm_db'] = "net.if.out[eth1]"
    items['net_in_win_vm'] = "net.if.in[Red Hat VirtIO Ethernet Adapter]"
    items['net_in_win_vm_2'] = "net.if.in[Red Hat VirtIO Ethernet Adapter #2]"
    items['net_in_win_vm_3'] = "net.if.in[Red Hat VirtIO Ethernet Adapter #3]"
    items['net_out_win_vm'] = "net.if.out[Red Hat VirtIO Ethernet Adapter]"
    items['net_out_win_vm_2'] = "net.if.out[Red Hat VirtIO Ethernet Adapter #2]"
    items['net_out_win_vm_3'] = "net.if.out[Red Hat VirtIO Ethernet Adapter #3]"
    items['net_in_phy_web_lin'] = "net.if.in[bond0]"
    items['net_out_phy_web_lin'] = "net.if.out[bond0]"
    items['net_in_phy_db_lin'] = "net.if.in[bond1]"
    items['net_out_phy_db_lin'] = "net.if.out[bond1]"
    items['net_in_phy_web_win'] = "net.if.in[TEAM : WEB-TEAM]"
    items['net_out_phy_web_win'] = "net.if.in[TEAM : WEB-TEAM]"
    items['net_in_phy_db_win'] = "net.if.in[TEAM : DB Team]"
    items['net_out_phy_db_win'] = "net.if.out[TEAM : DB Team]"
    items['net_in_phy_web_win_1'] = "net.if.in[TEAM : web]"
    items['net_out_phy_web_win_1'] = "net.if.out[TEAM : web]"
    items['net_in_phy_db_win_1'] = "net.if.in[TEAM : DB]"
    items['net_out_phy_db_win_1'] = "net.if.out[TEAM : DB]"
    items['net_in_win_pro'] = "net.if.in[Intel(R) PRO/1000 MT Network Connection]"
    items['net_out_win_pro'] = "net.if.out[Intel(R) PRO/1000 MT Network Connection]"
    items['net_in_phy_web_hp'] = "net.if.in[HP Network Team #1]"
    items['net_out_phy_web_hp'] = "net.if.out[HP Network Team #1]"
    items['iis_conntion'] = "perf_counter[\\Web Service(_Total)\\Current Connections]"
    items['tcp_conntion'] = "k.tcp.conn[ESTABLISHED]"
    for x,y in items.items():
        os.system('mkdir -p /data/zabbix/cvs/%s' % dip)
        cmds = """
        python /data/zabbix/fetch_items_to_csv.py -s '%s' -n '%s' -k '%s' -u 'admin' -p '%s' -t1 '2015-06-23 00:00:01' -t2 '2015-06-30 00:00:01' -o /data/zabbix/cvs/%s/%s_%s.cvs""" %(moniter,dip,y,pawd,dip,dip,x)
        os.system(cmds)
        print "*"  * 100
        print cmds
        print "*" * 100

def work():
    moniter='192.168.1.1'
    ip_list = ['192.168.1.15','192.168.1.13','192.168.1.66','192.168.1.5','192.168.1.7','192.168.1.16','192.168.1.38','192.168.1.2','192.168.1.13','192.168.1.10']
    for ip in ip_list:
        show_items(moniter,ip )

if __name__ == "__main__":
    sc = work()  

  三:数据采集完毕,进行格式化输出
#!/usr/bin/env python
#-*-coding:utf8-*-
import os,sys,time
workfile = '/home/zabbix/zabbix/sjz/'
def collect_info():
    dict_doc = dict()
    for i in os.listdir(workfile):
        dict_doc = list()
        for v in os.listdir('%s%s' %(workfile,i)):
            dict_doc.append(v)
    count = 0
    for x,y in dict_doc.items():
        for p in y:
            fp = '%s/%s/%s' %(workfile,x,p)
            op = open(fp,'r').readlines()
            np = '%s.txt' %p
            os.system( """ cat %s|awk -F";" '{print $3}' > %s """ %(fp,np))
            count += 1
    print count
if __name__ == "__main__":
    sc = collect_info()  

  四,整理数据,汇报成图形,撰写技术报告




  

  

  






运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-666435-1-1.html 上篇帖子: 使用zabbix监控oracle基本使用情况 下篇帖子: CentOS7.5 下zabbix3.0.18监控CPU负载
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表