Advertisement
Guest User

Untitled

a guest
Dec 14th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 16.91 KB | None | 0 0
  1. import pymongo
  2. import pprint
  3. import csv
  4. import json
  5. import logging
  6. from yandex_geocoder import Client
  7. from pyzabbix import ZabbixAPI
  8. from progressbar import ProgressBar, Percentage, ETA, ReverseBar, RotatingMarker, Timer
  9.  
  10.  
  11. ###################################
  12. #-------------
  13. brest_x = 0
  14. brest_y = 0
  15. #----
  16. vitebsk_x = 0
  17. vitebsk_y = 0
  18. #----
  19. gomel_x = 0
  20. gomel_y = 0
  21. #----
  22. grodno_x = 0
  23. grodno_y = 0
  24. #----
  25. minsk_x = 0
  26. minsk_y = 0
  27. #----
  28. mogilev_x = 0
  29. mogilev_y = 0
  30. #-------------
  31. ###################################
  32. kioskid = ""
  33. camid = ""
  34. routerid = ""
  35. ###################################
  36.  
  37. #mongo connect
  38. try:
  39.     conn=pymongo.MongoClient('192.168.252.11', 27017)
  40. except pymongo.errors.ConnectionFailure:
  41.    print ("Could not connect to MongoDB: %s")
  42.  
  43. #zabbix api connect
  44. zapi = ZabbixAPI("http://192.168.252.13/zabbix")
  45. zapi.login(user="Admin", password="1111111")
  46.  
  47. #mongo block
  48. def main():
  49.     ################################################
  50.     ####               mongo block              ####
  51.     ################################################
  52.     db = conn.artixcs
  53.     shops = db.shop
  54.     cashs = db.cash
  55.  
  56.     for shop in shops.find({'displayCode': {'$nin' : ['All', '99999', '500386', '300019', '300026', '400198', '400201', '500020', '500019', '500024', '500022']}}):
  57.         ipaddr = ''
  58.  
  59.         for cash in cashs.find({'parent.$ref': 'shop',  'parent.$id': shop['_id']}):
  60.             ipaddr = cash['ip']
  61.  
  62.         adrs = shop['title'].replace(shop['displayCode'], '').strip()
  63.         ip = ipaddr.split('.')
  64.         s = '.'
  65.         iprouter = s.join(list(ip)[0:3])+'.1'
  66.         ipcam = s.join(list(ip)[0:3])+'.12'
  67.  
  68.         gr = db.dereference(shop['parent']) # ['title']
  69.         print("##################")
  70.         print(shop['displayCode'])
  71.         print(ipaddr)
  72.         print(adrs)
  73.         print(ipcam)
  74.         print(iprouter)
  75.         d = {'codeshop': shop['displayCode'], 'codegroup': gr['displayCode'], 'shopaddr': shop['title'], 'ip': ipaddr, 'addr': adrs, 'ipcam': ipcam, 'iprouter': iprouter}
  76.  
  77.  
  78.     #################################################
  79.     #################################################
  80.         if not ipaddr:
  81.             break
  82.  
  83.         ####################################
  84.         ####     Yandex Geocode         ####
  85.         ####################################
  86.         try:
  87.             cord = Client.coordinates(adrs)
  88.  
  89.         except Exception:
  90.             pass
  91.  
  92.         x, y = cord
  93.         ####################################
  94.  
  95.  
  96.         #Hostget for KIOSK
  97.         hostgetkiosk = zapi.host.get(filter={"host":"Kiosk "+ shop['displayCode']})
  98.         #print(hostgetkiosk)
  99.         #Hostget for CAMERA
  100.         hostgetcam = zapi.host.get(filter={"host":"Camera "+ shop['displayCode']})
  101.         #print(hostgetcam)
  102.         #Hostget for ROUTER
  103.         hostgetrouter = zapi.host.get(filter={"host":"Router "+ shop['displayCode']})
  104.         #print(hostgetrouter)
  105.  
  106.  
  107.         #HostID from Kiosk number
  108.         def kioskid():
  109.             kioskid = zapi.host.get(filter={"host":"Kiosk "+ shop['displayCode']})[0]["hostid"]
  110.             return kioskid
  111.  
  112.         #HostID for CAMERA
  113.         def camid():
  114.             camid = zapi.host.get(filter={"host":"Camera "+ shop['displayCode']})[0]["hostid"]
  115.             return camid
  116.  
  117.         #HostID for ROUTER
  118.         def routerid():
  119.             routerid = zapi.host.get(filter={"host":"Router "+ shop['displayCode']})[0]["hostid"]
  120.             return routerid
  121.  
  122.  
  123.  
  124.         #Zabbix add KIOSK block
  125.         if not hostgetkiosk:
  126.              zapi.host.create(
  127.                  host= "Kiosk "+shop['displayCode'],
  128.                  status= 0,
  129.                  interfaces=[{
  130.                      "type": 1,
  131.                      "main": "1",
  132.                      "useip": 1,
  133.                      "ip": ipaddr,
  134.                      "dns": "",
  135.                      "port": 10050
  136.                  }],
  137.                  groups=[{
  138.                      "groupid": 18
  139.                  }],
  140.                  templates=[{
  141.                      "templateid": 10001
  142.                  },
  143.                  {
  144.                      "templateid": 10256
  145.                  }],
  146.                  inventory_mode= 0,
  147.                  inventory ={
  148.                     "location_lat": y,
  149.                     "location_lon": x
  150.                  })
  151.              print("Kiosk add")
  152.         else:
  153.             zapi.host.update({
  154.                 "hostid": kioskid(),
  155.                 "status": 0,
  156.                 "groups":[
  157.                      {
  158.                          "groupid": 18
  159.                      }
  160.                 ],
  161.                 "templates":[
  162.                      {
  163.                          "templateid": 10001
  164.                      },
  165.                      {
  166.                          "templateid": 10256
  167.                      }
  168.                 ],
  169.                 "inventory_mode": 0,
  170.                 "inventory":
  171.                 {
  172.                     "location_lat": y,
  173.                     "location_lon": x
  174.                 }
  175.             })
  176.         print("Kiosk update")
  177.  
  178.         #Zabbix add CAMERA block
  179.         if not hostgetcam:
  180.              zapi.host.create(
  181.                  host= "Camera "+shop['displayCode'],
  182.                  status= 0,
  183.                  interfaces=[{
  184.                      "type": 1,
  185.                      "main": "1",
  186.                      "useip": 1,
  187.                      "ip": ipcam,
  188.                      "dns": "",
  189.                      "port": 10050
  190.                  }],
  191.                  groups=[{
  192.                      "groupid": 19
  193.                  }],
  194.                  templates=[{
  195.                      "templateid": 10256
  196.                  }])
  197.              print("Camera add")
  198.         else:
  199.             zapi.host.update({
  200.                 "hostid": camid(),
  201.                 "status": 0,
  202.                 "groups":[
  203.                      {
  204.                          "groupid": 19
  205.                      }
  206.                 ],
  207.                 "templates":[
  208.                      {
  209.                          "templateid": 10256
  210.                      }
  211.                 ],
  212.                 "inventory_mode": 0,
  213.                 "inventory":
  214.                 {
  215.                     "location_lat": y,
  216.                     "location_lon": x
  217.                 }
  218.             })
  219.         print("Camera update")
  220.         #Zabbix add ROUTER block
  221.         if not hostgetrouter:
  222.              zapi.host.create(
  223.                  host= "Router "+shop['displayCode'],
  224.                  status= 0,
  225.                  interfaces=[{
  226.                      "type": 1,
  227.                      "main": "1",
  228.                      "useip": 1,
  229.                      "ip": iprouter,
  230.                      "dns": "",
  231.                      "port": 10050
  232.                  }],
  233.                  groups=[{
  234.                      "groupid": 20
  235.                  }],
  236.                  templates=[{
  237.                      "templateid": 10256
  238.                  }],
  239.                  inventory_mode= 0,
  240.                  inventory ={
  241.                     "location_lat": y,
  242.                     "location_lon": x
  243.                  })
  244.              print("Router add")
  245.         else:
  246.             zapi.host.update({
  247.                 "hostid": routerid(),
  248.                 "status": 0,
  249.                 "groups":[
  250.                      {
  251.                          "groupid": 20
  252.                      }
  253.                 ],
  254.                 "templates":[
  255.                      {
  256.                          "templateid": 10256
  257.                      }
  258.                 ],
  259.                 "inventory_mode": 0,
  260.                 "inventory":
  261.                 {
  262.                     "location_lat": y,
  263.                     "location_lon": x
  264.                 }
  265.             })
  266.         print("Router update")
  267.  
  268.  
  269.  
  270.  
  271.         #Get GraphID for KIOSK
  272.         graphgetkiosk = zapi.graph.get(hostids=kioskid())
  273.         for graph_id in graphgetkiosk:
  274.             if graph_id['name'] == 'Потери':
  275.                graphkiosk = graph_id['graphid']
  276.  
  277.         #Get GraphID for CAMERA
  278.         graphgetcam = zapi.graph.get(hostids=camid())
  279.         for graph_id in graphgetcam:
  280.             if graph_id['name'] == 'Потери':
  281.                graphcam = graph_id['graphid']
  282.  
  283.         #Get GraphID for ROUTER
  284.         graphgetrouter = zapi.graph.get(hostids=routerid())
  285.         for graph_id in graphgetrouter:
  286.             if graph_id['name'] == 'Потери':
  287.                graphrouter = graph_id['graphid']
  288.  
  289.  
  290.         #############################
  291.         #zapi.screen.delete(253)
  292.         #zapi.screen.create(
  293.         #       name = "Vitebsk",
  294.         #       hsize = 3,
  295.         #       vsize = 100
  296.         #)
  297.         #############################
  298.  
  299.  
  300.  
  301.         #############################
  302.         ###         Brest         ###
  303.         #############################
  304.         def brest():
  305.             global brest_x
  306.             global brest_y
  307.  
  308.             #-------------
  309.             zapi.screenitem.updatebyposition({
  310.                 'screenid': '255',
  311.                 'resourcetype': '0',
  312.                 'resourceid': graphkiosk,
  313.                 'width': 500,
  314.                 'height': 100,
  315.                 'x': brest_x,
  316.                 'y': brest_y
  317.             },
  318.             {
  319.                 'screenid': '262',
  320.                 'resourcetype': '0',
  321.                 'resourceid': graphcam,
  322.                 'width': 500,
  323.                 'height': 100,
  324.                 'x': brest_x,
  325.                 'y': brest_y
  326.             },
  327.             {
  328.                 'screenid': '268',
  329.                 'resourcetype': '0',
  330.                 'resourceid': graphrouter,
  331.                 'width': 500,
  332.                 'height': 100,
  333.                 'x': brest_x,
  334.                 'y': brest_y
  335.             })
  336.  
  337.             #print(brest_x, brest_y)
  338.             #Coordinates counter
  339.             if brest_x == 2:
  340.                 brest_x = 0
  341.                 brest_y += 1
  342.             else:
  343.                 brest_x += 1
  344.  
  345.         #############################
  346.         ###        Vitebsk        ###
  347.         #############################
  348.         def vitebsk():
  349.             global vitebsk_x
  350.             global vitebsk_y
  351.             #-------------
  352.             zapi.screenitem.updatebyposition({
  353.                 'screenid': '260',
  354.                 'resourcetype': '0',
  355.                 'resourceid': graphkiosk,
  356.                 'width': 500,
  357.                 'height': 100,
  358.                 'x': vitebsk_x,
  359.                 'y': vitebsk_y
  360.             },
  361.             {
  362.                 'screenid': '263',
  363.                 'resourcetype': '0',
  364.                 'resourceid': graphcam,
  365.                 'width': 500,
  366.                 'height': 100,
  367.                 'x': vitebsk_x,
  368.                 'y': vitebsk_y
  369.             },
  370.             {
  371.                 'screenid': '269',
  372.                 'resourcetype': '0',
  373.                 'resourceid': graphrouter,
  374.                 'width': 500,
  375.                 'height': 100,
  376.                 'x': vitebsk_x,
  377.                 'y': vitebsk_y
  378.             })
  379.  
  380.             #print(vitebsk_x, vitebsk_y)
  381.             #Coordinates counter
  382.             if vitebsk_x == 2:
  383.                 vitebsk_x = 0
  384.                 vitebsk_y += 1
  385.             else:
  386.                 vitebsk_x += 1
  387.  
  388.         #############################
  389.         ###         Gomel         ###
  390.         #############################
  391.         def gomel():
  392.             global gomel_x
  393.             global gomel_y
  394.             #-------------
  395.             zapi.screenitem.updatebyposition({
  396.                 'screenid': '256',
  397.                 'resourcetype': '0',
  398.                 'resourceid': graphkiosk,
  399.                 'width': 500,
  400.                 'height': 100,
  401.                 'x': gomel_x,
  402.                 'y': gomel_y
  403.             },
  404.             {
  405.                 'screenid': '264',
  406.                 'resourcetype': '0',
  407.                 'resourceid': graphcam,
  408.                 'width': 500,
  409.                 'height': 100,
  410.                 'x': gomel_x,
  411.                 'y': gomel_y
  412.             },
  413.             {
  414.                 'screenid': '270',
  415.                 'resourcetype': '0',
  416.                 'resourceid': graphrouter,
  417.                 'width': 500,
  418.                 'height': 100,
  419.                 'x': gomel_x,
  420.                 'y': gomel_y
  421.             })
  422.  
  423.             #print(gomel_x, gomel_y)
  424.             #Coordinates counter
  425.             if gomel_x == 2:
  426.                 gomel_x = 0
  427.                 gomel_y += 1
  428.             else:
  429.                 gomel_x += 1
  430.  
  431.         #############################
  432.         ###        Grodno         ###
  433.         #############################
  434.         def grodno():
  435.             global grodno_x
  436.             global grodno_y
  437.             #-------------
  438.             zapi.screenitem.updatebyposition({
  439.                 'screenid': '257',
  440.                 'resourcetype': '0',
  441.                 'resourceid': graphkiosk,
  442.                 'width': 500,
  443.                 'height': 100,
  444.                 'x': grodno_x,
  445.                 'y': grodno_y
  446.             },
  447.             {
  448.                 'screenid': '265',
  449.                 'resourcetype': '0',
  450.                 'resourceid': graphcam,
  451.                 'width': 500,
  452.                 'height': 100,
  453.                 'x': grodno_x,
  454.                 'y': grodno_y
  455.             },
  456.             {
  457.                 'screenid': '271',
  458.                 'resourcetype': '0',
  459.                 'resourceid': graphrouter,
  460.                 'width': 500,
  461.                 'height': 100,
  462.                 'x': grodno_x,
  463.                 'y': grodno_y
  464.             })
  465.  
  466.             #print(grodno_x, grodno_y)
  467.             #Coordinates counter
  468.             if grodno_x == 2:
  469.                 grodno_x = 0
  470.                 grodno_y += 1
  471.             else:
  472.                 grodno_x += 1
  473.  
  474.         #############################
  475.         ###         Minsk         ###
  476.         #############################
  477.         def minsk():
  478.             global minsk_x
  479.             global minsk_y
  480.             #-------------
  481.             zapi.screenitem.updatebyposition({
  482.                 'screenid': '258',
  483.                 'resourcetype': '0',
  484.                 'resourceid': graphkiosk,
  485.                 'width': 500,
  486.                 'height': 100,
  487.                 'x': minsk_x,
  488.                 'y': minsk_y
  489.             },
  490.             {
  491.                 'screenid': '261',
  492.                 'resourcetype': '0',
  493.                 'resourceid': graphcam,
  494.                 'width': 500,
  495.                 'height': 100,
  496.                 'x': minsk_x,
  497.                 'y': minsk_y
  498.             },
  499.             {
  500.                 'screenid': '267',
  501.                 'resourcetype': '0',
  502.                 'resourceid': graphrouter,
  503.                 'width': 500,
  504.                 'height': 100,
  505.                 'x': minsk_x,
  506.                 'y': minsk_y
  507.             })
  508.  
  509.             #print(minsk_x, minsk_y)
  510.             #Coordinates counter
  511.             if minsk_x == 2:
  512.                 minsk_x = 0
  513.                 minsk_y += 1
  514.             else:
  515.                 minsk_x += 1
  516.  
  517.         #############################
  518.         ###        Mogilev        ###
  519.         #############################
  520.         def mogilev():
  521.             global mogilev_x
  522.             global mogilev_y
  523.             #-------------
  524.             zapi.screenitem.updatebyposition({
  525.                 'screenid': '259',
  526.                 'resourcetype': '0',
  527.                 'resourceid': graphkiosk,
  528.                 'width': 500,
  529.                 'height': 100,
  530.                 'x': mogilev_x,
  531.                 'y': mogilev_y
  532.             },
  533.             {
  534.                 'screenid': '266',
  535.                 'resourcetype': '0',
  536.                 'resourceid': graphcam,
  537.                 'width': 500,
  538.                 'height': 100,
  539.                 'x': mogilev_x,
  540.                 'y': mogilev_y
  541.             },
  542.             {
  543.                 'screenid': '272',
  544.                 'resourcetype': '0',
  545.                 'resourceid': graphrouter,
  546.                 'width': 500,
  547.                 'height': 100,
  548.                 'x': mogilev_x,
  549.                 'y': mogilev_y
  550.             })
  551.  
  552.             #print(mogilev_x, mogilev_y)
  553.             #Coordinates counter
  554.             if mogilev_x == 2:
  555.                 mogilev_x = 0
  556.                 mogilev_y += 1
  557.             else:
  558.                 mogilev_x += 1
  559.  
  560.  
  561.         #Case switcher
  562.         switcher = {
  563.                 1: brest,
  564.                 2: vitebsk,
  565.                 3: gomel,
  566.                 4: grodno,
  567.                 5: minsk,
  568.                 6: mogilev
  569.             }
  570.  
  571.         #Function for case switcher
  572.         def numbers_to_strings(argument):
  573.             # Get the function from switcher dictionary
  574.             func = switcher.get(argument, "nothing")
  575.             # Execute the function
  576.             return func()
  577.  
  578.         #Init case switcher function
  579.         #print(shop['title'][0].rstrip())
  580.         numbers_to_strings(int(shop['title'][0].rstrip()))
  581.  
  582.  
  583. if __name__ == '__main__':
  584.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement