|
| 1 | +#!/usr/bin/env python |
| 2 | +#coding:utf8 |
| 3 | + |
| 4 | +''' |
| 5 | +Created on 03.06.2015 |
| 6 | +''' |
| 7 | + |
| 8 | +import optparse |
| 9 | +import sys |
| 10 | +import traceback |
| 11 | +from getpass import getpass |
| 12 | +from core import ZabbixAPI |
| 13 | + |
| 14 | +def get_options(): |
| 15 | + usage = "usage: %prog [options]" |
| 16 | + OptionParser = optparse.OptionParser |
| 17 | + parser = OptionParser(usage) |
| 18 | + |
| 19 | + parser.add_option("-s","--server",action="store",type="string",\ |
| 20 | + dest="server",help="(REQUIRED)Zabbix Server URL.") |
| 21 | + parser.add_option("-u", "--username", action="store", type="string",\ |
| 22 | + dest="username",help="(REQUIRED)Username (Will prompt if not given).") |
| 23 | + parser.add_option("-p", "--password", action="store", type="string",\ |
| 24 | + dest="password",help="(REQUIRED)Password (Will prompt if not given).") |
| 25 | + parser.add_option("-H","--hostname",action="store",type="string",\ |
| 26 | + dest="hostname",help="(REQUIRED)hostname for hosts.") |
| 27 | + parser.add_option("-n","--name",action="store",type="string",\ |
| 28 | + dest="name",help="(REQUIRED)Name of the interface.") |
| 29 | + parser.add_option("-i","--interface",action="store",type="string",\ |
| 30 | + dest="interface",default="",help="(REQUIRED,Default:"")key of the interface.") |
| 31 | + parser.add_option("--pool",action="store",type="string",\ |
| 32 | + dest="pool",default="core-pool",help="(REQUIRED,Default:"")the interface pool.") |
| 33 | + |
| 34 | + options,args = parser.parse_args() |
| 35 | + |
| 36 | + if not options.server: |
| 37 | + options.server = raw_input('server http:') |
| 38 | + |
| 39 | + if not options.username: |
| 40 | + options.username = raw_input('Username:') |
| 41 | + |
| 42 | + if not options.password: |
| 43 | + options.password = getpass() |
| 44 | + |
| 45 | + return options, args |
| 46 | + |
| 47 | +def errmsg(msg): |
| 48 | + sys.stderr.write(msg + "\n") |
| 49 | + sys.exit(-1) |
| 50 | + |
| 51 | +if __name__ == "__main__": |
| 52 | + options, args = get_options() |
| 53 | + |
| 54 | + zapi = ZabbixAPI(options.server,options.username, options.password) |
| 55 | + |
| 56 | + hostname = options.hostname |
| 57 | + name = options.name |
| 58 | + i = options.interface |
| 59 | + pool = options.pool |
| 60 | + |
| 61 | + idc=hostname.rstrip('-summary') |
| 62 | + |
| 63 | + key_responseTime = i+"-responseTime" |
| 64 | + key_0_200 = i+"-0-200" |
| 65 | + key_200_500 = i+"-200-500" |
| 66 | + key_500_1000 = i+"-500-1000" |
| 67 | + key_1000_2000 = i+"-1000-2000" |
| 68 | + key_2000_999999 = i+"-2000-999999" |
| 69 | + key_Total_Requests = i+"-Total-Requests" |
| 70 | + key_retMsg_FAIL = i+"-retMsg-FAIL" |
| 71 | + key_retMsg_SUCC = i+"-retMsg-SUCC" |
| 72 | + |
| 73 | + print key_responseTime,key_0_200,key_Total_Requests |
| 74 | + |
| 75 | + hostid = zapi.host.get({"filter":{"host":hostname}})[0]["hostid"] |
| 76 | + i_responseTime = zapi.item.get({"output": "extend","hostids": hostid,"search":{"key_": key_responseTime}})[0]["itemid"] |
| 77 | + i_0_200 = zapi.item.get({"output": "extend","hostids": hostid,"search":{"key_": key_0_200}})[0]["itemid"] |
| 78 | + i_200_500 = zapi.item.get({"output": "extend","hostids": hostid,"search":{"key_": key_200_500}})[0]["itemid"] |
| 79 | + i_500_1000 = zapi.item.get({"output": "extend","hostids": hostid,"search":{"key_": key_500_1000}})[0]["itemid"] |
| 80 | + i_1000_2000 = zapi.item.get({"output": "extend","hostids": hostid,"search":{"key_": key_1000_2000}})[0]["itemid"] |
| 81 | + i_2000_999999 = zapi.item.get({"output": "extend","hostids": hostid,"search":{"key_": key_2000_999999}})[0]["itemid"] |
| 82 | + i_Total_Requests = zapi.item.get({"output": "extend","hostids": hostid,"search":{"key_": key_Total_Requests}})[0]["itemid"] |
| 83 | + i_retMsg_FAIL = zapi.item.get({"output": "extend","hostids": hostid,"search":{"key_": key_retMsg_FAIL}})[0]["itemid"] |
| 84 | + i_retMsg_SUCC = zapi.item.get({"output": "extend","hostids": hostid,"search":{"key_": key_retMsg_SUCC}})[0]["itemid"] |
| 85 | + |
| 86 | + api = "API ("+pool+") -- "+name |
| 87 | + api_response_time = "API ("+pool+") -- "+name+" response time" |
| 88 | + api_response_time_pie = "API ("+pool+") -- "+name+" response time (pie)" |
| 89 | + api_succ_rate_pie = "API ("+pool+") -- "+name+" succ rate (pie)" |
| 90 | + |
| 91 | + print i_responseTime,i_0_200,i_200_500,i_500_1000,i_1000_2000,i_2000_999999,i_Total_Requests,i_retMsg_SUCC,i_retMsg_FAIL |
| 92 | + print api,api_response_time,api_response_time_pie,api_succ_rate_pie |
| 93 | + |
| 94 | + zapi.graph.create({"name":api,"width":900,"height": 200,"gitems": [{"itemid": i_retMsg_FAIL,"color": "CC0000","drawtype":1,"yaxisside":0},{"itemid": i_retMsg_SUCC,"color": "00EE00","drawtype":1,"yaxisside":0},{"itemid": i_Total_Requests,"color": "C800C8","drawtype":0,"yaxisside":0},{"itemid": i_responseTime,"color": "0000BB","drawtype":0,"yaxisside":1}]}) |
| 95 | + zapi.graph.create({"name":api_response_time,"width":900,"height": 200,"gitems": [{"itemid": i_0_200,"color": "33FF33","drawtype":1,"yaxisside":0},{"itemid": i_200_500,"color": "008800","drawtype":1,"yaxisside":0},{"itemid": i_500_1000,"color": "CCCC00","drawtype":1,"yaxisside":0},{"itemid": i_1000_2000,"color": "FF3333","drawtype":1,"yaxisside":0},{"itemid": i_2000_999999,"color": "880000","drawtype":1,"yaxisside":0},{"itemid": i_Total_Requests,"color": "CC00CC","drawtype":0,"yaxisside":0},{"itemid": i_responseTime,"color": "0000BB","drawtype":0,"yaxisside":1}]}) |
| 96 | + zapi.graph.create({"name":api_response_time_pie,"width":900,"height": 300,"graphtype":2,"gitems": [{"itemid": i_0_200,"color": "33FF33"},{"itemid": i_200_500,"color": "008800"},{"itemid": i_500_1000,"color": "CCCC00"},{"itemid": i_1000_2000,"color": "FF3333"},{"itemid": i_2000_999999,"color": "880000"}]}) |
| 97 | + zapi.graph.create({"name":api_succ_rate_pie,"width":900,"height": 300,"graphtype":2,"gitems": [{"itemid": i_retMsg_FAIL,"color": "C80000"},{"itemid": i_retMsg_SUCC,"color": "00EE00"}]}) |
0 commit comments