Guest User

Untitled

a guest
Dec 5th, 2011
102
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python
  2.  
  3. import sys
  4. import re
  5. from zabbix_api import ZabbixAPI, ZabbixAPIException
  6.  
  7. zbx_server = "http://server/zabbix/"
  8. zbx_username = "Admin"
  9. zbx_password = "zabbix"
  10. zebra_conf = "/etc/quagga/zebra.conf"
  11. host_name="Router"
  12.  
  13. oids = {
  14.     "IN": ".1.3.6.1.2.1.31.1.1.1.6",  # ifHCInOctets
  15.     "OUT": ".1.3.6.1.2.1.31.1.1.1.10" # ifHCOutOctets
  16.     }
  17.  
  18. def err_msg(msg):
  19.     sys.stderr.write("Fail: " + str(msg) + "\n")
  20.     sys.exit(-1)
  21.  
  22. def read_zebra_conf(zebra_conf):
  23.     try:
  24.         f = open(zebra_conf, 'r')
  25.     except IOError, e:
  26.         err_msg(e)
  27.     else:
  28.         ifaces = {}
  29.         for line in f:
  30.             iface_m = re.search('^interface\svlan(\d+)', line)
  31.             if iface_m:
  32.                 desc_m = re.search('^\s*description\s+(.*)', f.next())
  33.                 if desc_m:
  34.                     vlan = iface_m.group(1)
  35.                     desc = desc_m.group(1)
  36.                     ifaces[vlan] = desc
  37.         return ifaces
  38.         f.close()
  39.  
  40. def add_item(host_id, vlan, oid_id):
  41.     try:
  42.         item_id = zapi.item.create({
  43.             "hostid": host_id,
  44.             "description": vlan + "-" + oid_id,
  45.             "type": 4,
  46.             "snmp_oid": oids[oid_id] + "." + vlan,
  47.             "snmp_community": "zabbix",
  48.             "snmp_port": "161",
  49.             "key_": vlan + "." + oid_id,
  50.             "value_type": 3,
  51.             "data_type": 0,
  52.             "units": "bit/s",
  53.             "multiplier": 1,
  54.             "formula": 8,
  55.             "delay": 60,
  56.             "history": 90,
  57.             "trends": 365,
  58.             "status": 0,
  59.             "delta": 1
  60.             })['itemids'][0]
  61.         return item_id
  62.     except ZabbixAPIException, e:
  63.         err_msg(e)
  64.  
  65. def add_graph(graph_name, in_item_id, out_item_id):
  66.     try:
  67.         zapi.graph.create({
  68.             "name": graph_name,
  69.             "width": 900,
  70.             "height": 200,
  71.             "graphtype": 0,
  72.             "gitems": [
  73.                {"itemid": in_item_id,
  74.                 "drawtype": 5,
  75.                 "sortorder": 1,
  76.                 "color": "009900",
  77.                 "yaxisside": 1,
  78.                 "calc_fnc": 2,
  79.                 "type": 0,
  80.                 "periods_cnt": 5},
  81.                {"itemid": out_item_id,
  82.                 "drawtype": 5,
  83.                 "sortorder": 2,
  84.                 "color": "000099",
  85.                 "yaxisside": 1,
  86.                 "calc_fnc": 2,
  87.                 "type": 0,
  88.                 "periods_cnt": 5}
  89.             ]
  90.         })
  91.     except ZabbixAPIException, e:
  92.         err_msg(e)
  93.  
  94. if  __name__ == "__main__":
  95.  
  96.     ifaces = read_zebra_conf(zebra_conf)
  97.  
  98.     zapi = ZabbixAPI(server = zbx_server, log_level = 0)
  99.  
  100.     try:
  101.         zapi.login(zbx_username, zbx_password)
  102.     except ZabbixAPIException, e:
  103.         err_msg(e)
  104.  
  105.     host_id = zapi.host.get({"filter": {"host": host_name}})[0]["hostid"]
  106.     if host_id == []:
  107.         err_msg("Host '" + host_name + "' not found")
  108.  
  109.     # get exists graphs
  110.     graphs_ex_ids = {}
  111.     graphs_ex_names = {}
  112.     for graph in zapi.graph.get({"filter": {"host": host_name}, "output": "extend"}):
  113.         vlan = re.search('.*\[(\d+)\]', graph["name"]).group(1)
  114.         name = re.search('(.*)\s\[\d+\]', graph["name"]).group(1)
  115.         graphs_ex_ids[vlan] = graph["graphid"]
  116.         graphs_ex_names[vlan] = name
  117.  
  118.     for vlan in ifaces:
  119.         graph_name = ifaces[vlan] + " [" + vlan + "]"
  120.         graph_name = graph_name.decode('utf-8')
  121.  
  122.         if (vlan in graphs_ex_names):
  123.             if not (graph_name == graphs_ex_names[vlan] + " [" + vlan + "]"):
  124.                 zapi.graph.update({"graphid": graphs_ex_ids[vlan], "name": graph_name})
  125.         else:
  126.             in_item_id = add_item(host_id, vlan, 'IN')
  127.             out_item_id = add_item(host_id, vlan, 'OUT')
  128.             add_graph(graph_name, in_item_id, out_item_id)
  129.  
RAW Paste Data