Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pymongo
- import pprint
- import csv
- import json
- import logging
- from yandex_geocoder import Client
- from pyzabbix import ZabbixAPI
- from progressbar import ProgressBar, Percentage, ETA, ReverseBar, RotatingMarker, Timer
- ###################################
- #-------------
- brest_x = 0
- brest_y = 0
- #----
- vitebsk_x = 0
- vitebsk_y = 0
- #----
- gomel_x = 0
- gomel_y = 0
- #----
- grodno_x = 0
- grodno_y = 0
- #----
- minsk_x = 0
- minsk_y = 0
- #----
- mogilev_x = 0
- mogilev_y = 0
- #-------------
- ###################################
- kioskid = ""
- camid = ""
- routerid = ""
- ###################################
- #mongo connect
- try:
- conn=pymongo.MongoClient('192.168.252.11', 27017)
- except pymongo.errors.ConnectionFailure:
- print ("Could not connect to MongoDB: %s")
- #zabbix api connect
- zapi = ZabbixAPI("http://192.168.252.13/zabbix")
- zapi.login(user="Admin", password="1111111")
- #mongo block
- def main():
- ################################################
- #### mongo block ####
- ################################################
- db = conn.artixcs
- shops = db.shop
- cashs = db.cash
- for shop in shops.find({'displayCode': {'$nin' : ['All', '99999', '500386', '300019', '300026', '400198', '400201', '500020', '500019', '500024', '500022']}}):
- ipaddr = ''
- for cash in cashs.find({'parent.$ref': 'shop', 'parent.$id': shop['_id']}):
- ipaddr = cash['ip']
- adrs = shop['title'].replace(shop['displayCode'], '').strip()
- ip = ipaddr.split('.')
- s = '.'
- iprouter = s.join(list(ip)[0:3])+'.1'
- ipcam = s.join(list(ip)[0:3])+'.12'
- gr = db.dereference(shop['parent']) # ['title']
- print("##################")
- print(shop['displayCode'])
- print(ipaddr)
- print(adrs)
- print(ipcam)
- print(iprouter)
- d = {'codeshop': shop['displayCode'], 'codegroup': gr['displayCode'], 'shopaddr': shop['title'], 'ip': ipaddr, 'addr': adrs, 'ipcam': ipcam, 'iprouter': iprouter}
- #################################################
- #################################################
- if not ipaddr:
- break
- ####################################
- #### Yandex Geocode ####
- ####################################
- try:
- cord = Client.coordinates(adrs)
- except Exception:
- pass
- x, y = cord
- ####################################
- #Hostget for KIOSK
- hostgetkiosk = zapi.host.get(filter={"host":"Kiosk "+ shop['displayCode']})
- #print(hostgetkiosk)
- #Hostget for CAMERA
- hostgetcam = zapi.host.get(filter={"host":"Camera "+ shop['displayCode']})
- #print(hostgetcam)
- #Hostget for ROUTER
- hostgetrouter = zapi.host.get(filter={"host":"Router "+ shop['displayCode']})
- #print(hostgetrouter)
- #HostID from Kiosk number
- def kioskid():
- kioskid = zapi.host.get(filter={"host":"Kiosk "+ shop['displayCode']})[0]["hostid"]
- return kioskid
- #HostID for CAMERA
- def camid():
- camid = zapi.host.get(filter={"host":"Camera "+ shop['displayCode']})[0]["hostid"]
- return camid
- #HostID for ROUTER
- def routerid():
- routerid = zapi.host.get(filter={"host":"Router "+ shop['displayCode']})[0]["hostid"]
- return routerid
- #Zabbix add KIOSK block
- if not hostgetkiosk:
- zapi.host.create(
- host= "Kiosk "+shop['displayCode'],
- status= 0,
- interfaces=[{
- "type": 1,
- "main": "1",
- "useip": 1,
- "ip": ipaddr,
- "dns": "",
- "port": 10050
- }],
- groups=[{
- "groupid": 18
- }],
- templates=[{
- "templateid": 10001
- },
- {
- "templateid": 10256
- }],
- inventory_mode= 0,
- inventory ={
- "location_lat": y,
- "location_lon": x
- })
- print("Kiosk add")
- else:
- zapi.host.update({
- "hostid": kioskid(),
- "status": 0,
- "groups":[
- {
- "groupid": 18
- }
- ],
- "templates":[
- {
- "templateid": 10001
- },
- {
- "templateid": 10256
- }
- ],
- "inventory_mode": 0,
- "inventory":
- {
- "location_lat": y,
- "location_lon": x
- }
- })
- print("Kiosk update")
- #Zabbix add CAMERA block
- if not hostgetcam:
- zapi.host.create(
- host= "Camera "+shop['displayCode'],
- status= 0,
- interfaces=[{
- "type": 1,
- "main": "1",
- "useip": 1,
- "ip": ipcam,
- "dns": "",
- "port": 10050
- }],
- groups=[{
- "groupid": 19
- }],
- templates=[{
- "templateid": 10256
- }])
- print("Camera add")
- else:
- zapi.host.update({
- "hostid": camid(),
- "status": 0,
- "groups":[
- {
- "groupid": 19
- }
- ],
- "templates":[
- {
- "templateid": 10256
- }
- ],
- "inventory_mode": 0,
- "inventory":
- {
- "location_lat": y,
- "location_lon": x
- }
- })
- print("Camera update")
- #Zabbix add ROUTER block
- if not hostgetrouter:
- zapi.host.create(
- host= "Router "+shop['displayCode'],
- status= 0,
- interfaces=[{
- "type": 1,
- "main": "1",
- "useip": 1,
- "ip": iprouter,
- "dns": "",
- "port": 10050
- }],
- groups=[{
- "groupid": 20
- }],
- templates=[{
- "templateid": 10256
- }],
- inventory_mode= 0,
- inventory ={
- "location_lat": y,
- "location_lon": x
- })
- print("Router add")
- else:
- zapi.host.update({
- "hostid": routerid(),
- "status": 0,
- "groups":[
- {
- "groupid": 20
- }
- ],
- "templates":[
- {
- "templateid": 10256
- }
- ],
- "inventory_mode": 0,
- "inventory":
- {
- "location_lat": y,
- "location_lon": x
- }
- })
- print("Router update")
- #Get GraphID for KIOSK
- graphgetkiosk = zapi.graph.get(hostids=kioskid())
- for graph_id in graphgetkiosk:
- if graph_id['name'] == 'Потери':
- graphkiosk = graph_id['graphid']
- #Get GraphID for CAMERA
- graphgetcam = zapi.graph.get(hostids=camid())
- for graph_id in graphgetcam:
- if graph_id['name'] == 'Потери':
- graphcam = graph_id['graphid']
- #Get GraphID for ROUTER
- graphgetrouter = zapi.graph.get(hostids=routerid())
- for graph_id in graphgetrouter:
- if graph_id['name'] == 'Потери':
- graphrouter = graph_id['graphid']
- #############################
- #zapi.screen.delete(253)
- #zapi.screen.create(
- # name = "Vitebsk",
- # hsize = 3,
- # vsize = 100
- #)
- #############################
- #############################
- ### Brest ###
- #############################
- def brest():
- global brest_x
- global brest_y
- #-------------
- zapi.screenitem.updatebyposition({
- 'screenid': '255',
- 'resourcetype': '0',
- 'resourceid': graphkiosk,
- 'width': 500,
- 'height': 100,
- 'x': brest_x,
- 'y': brest_y
- },
- {
- 'screenid': '262',
- 'resourcetype': '0',
- 'resourceid': graphcam,
- 'width': 500,
- 'height': 100,
- 'x': brest_x,
- 'y': brest_y
- },
- {
- 'screenid': '268',
- 'resourcetype': '0',
- 'resourceid': graphrouter,
- 'width': 500,
- 'height': 100,
- 'x': brest_x,
- 'y': brest_y
- })
- #print(brest_x, brest_y)
- #Coordinates counter
- if brest_x == 2:
- brest_x = 0
- brest_y += 1
- else:
- brest_x += 1
- #############################
- ### Vitebsk ###
- #############################
- def vitebsk():
- global vitebsk_x
- global vitebsk_y
- #-------------
- zapi.screenitem.updatebyposition({
- 'screenid': '260',
- 'resourcetype': '0',
- 'resourceid': graphkiosk,
- 'width': 500,
- 'height': 100,
- 'x': vitebsk_x,
- 'y': vitebsk_y
- },
- {
- 'screenid': '263',
- 'resourcetype': '0',
- 'resourceid': graphcam,
- 'width': 500,
- 'height': 100,
- 'x': vitebsk_x,
- 'y': vitebsk_y
- },
- {
- 'screenid': '269',
- 'resourcetype': '0',
- 'resourceid': graphrouter,
- 'width': 500,
- 'height': 100,
- 'x': vitebsk_x,
- 'y': vitebsk_y
- })
- #print(vitebsk_x, vitebsk_y)
- #Coordinates counter
- if vitebsk_x == 2:
- vitebsk_x = 0
- vitebsk_y += 1
- else:
- vitebsk_x += 1
- #############################
- ### Gomel ###
- #############################
- def gomel():
- global gomel_x
- global gomel_y
- #-------------
- zapi.screenitem.updatebyposition({
- 'screenid': '256',
- 'resourcetype': '0',
- 'resourceid': graphkiosk,
- 'width': 500,
- 'height': 100,
- 'x': gomel_x,
- 'y': gomel_y
- },
- {
- 'screenid': '264',
- 'resourcetype': '0',
- 'resourceid': graphcam,
- 'width': 500,
- 'height': 100,
- 'x': gomel_x,
- 'y': gomel_y
- },
- {
- 'screenid': '270',
- 'resourcetype': '0',
- 'resourceid': graphrouter,
- 'width': 500,
- 'height': 100,
- 'x': gomel_x,
- 'y': gomel_y
- })
- #print(gomel_x, gomel_y)
- #Coordinates counter
- if gomel_x == 2:
- gomel_x = 0
- gomel_y += 1
- else:
- gomel_x += 1
- #############################
- ### Grodno ###
- #############################
- def grodno():
- global grodno_x
- global grodno_y
- #-------------
- zapi.screenitem.updatebyposition({
- 'screenid': '257',
- 'resourcetype': '0',
- 'resourceid': graphkiosk,
- 'width': 500,
- 'height': 100,
- 'x': grodno_x,
- 'y': grodno_y
- },
- {
- 'screenid': '265',
- 'resourcetype': '0',
- 'resourceid': graphcam,
- 'width': 500,
- 'height': 100,
- 'x': grodno_x,
- 'y': grodno_y
- },
- {
- 'screenid': '271',
- 'resourcetype': '0',
- 'resourceid': graphrouter,
- 'width': 500,
- 'height': 100,
- 'x': grodno_x,
- 'y': grodno_y
- })
- #print(grodno_x, grodno_y)
- #Coordinates counter
- if grodno_x == 2:
- grodno_x = 0
- grodno_y += 1
- else:
- grodno_x += 1
- #############################
- ### Minsk ###
- #############################
- def minsk():
- global minsk_x
- global minsk_y
- #-------------
- zapi.screenitem.updatebyposition({
- 'screenid': '258',
- 'resourcetype': '0',
- 'resourceid': graphkiosk,
- 'width': 500,
- 'height': 100,
- 'x': minsk_x,
- 'y': minsk_y
- },
- {
- 'screenid': '261',
- 'resourcetype': '0',
- 'resourceid': graphcam,
- 'width': 500,
- 'height': 100,
- 'x': minsk_x,
- 'y': minsk_y
- },
- {
- 'screenid': '267',
- 'resourcetype': '0',
- 'resourceid': graphrouter,
- 'width': 500,
- 'height': 100,
- 'x': minsk_x,
- 'y': minsk_y
- })
- #print(minsk_x, minsk_y)
- #Coordinates counter
- if minsk_x == 2:
- minsk_x = 0
- minsk_y += 1
- else:
- minsk_x += 1
- #############################
- ### Mogilev ###
- #############################
- def mogilev():
- global mogilev_x
- global mogilev_y
- #-------------
- zapi.screenitem.updatebyposition({
- 'screenid': '259',
- 'resourcetype': '0',
- 'resourceid': graphkiosk,
- 'width': 500,
- 'height': 100,
- 'x': mogilev_x,
- 'y': mogilev_y
- },
- {
- 'screenid': '266',
- 'resourcetype': '0',
- 'resourceid': graphcam,
- 'width': 500,
- 'height': 100,
- 'x': mogilev_x,
- 'y': mogilev_y
- },
- {
- 'screenid': '272',
- 'resourcetype': '0',
- 'resourceid': graphrouter,
- 'width': 500,
- 'height': 100,
- 'x': mogilev_x,
- 'y': mogilev_y
- })
- #print(mogilev_x, mogilev_y)
- #Coordinates counter
- if mogilev_x == 2:
- mogilev_x = 0
- mogilev_y += 1
- else:
- mogilev_x += 1
- #Case switcher
- switcher = {
- 1: brest,
- 2: vitebsk,
- 3: gomel,
- 4: grodno,
- 5: minsk,
- 6: mogilev
- }
- #Function for case switcher
- def numbers_to_strings(argument):
- # Get the function from switcher dictionary
- func = switcher.get(argument, "nothing")
- # Execute the function
- return func()
- #Init case switcher function
- #print(shop['title'][0].rstrip())
- numbers_to_strings(int(shop['title'][0].rstrip()))
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement