Created
December 7, 2017 10:36
-
-
Save borg-z/acc5e58be26cbabc925ddd3a92a2b701 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pyzabbix import ZabbixAPI | |
import requests | |
import re | |
from ipaddress import IPv4Network, IPv4Address | |
import ast | |
from deepdiff import DeepDiff | |
class billing_to_zabbix(): | |
def __init__(self): | |
self.ip4regex = re.compile('(\d+\.){3}\d+') | |
# excluded network | |
exclude_net = ['192.168.0.1/24'] | |
# excluded others | |
exclude_others = ['host'] | |
exclude = [IPv4Network(net).hosts() for net in exclude_net] | |
self.exclude_hosts = [] | |
for net in exclude: | |
for host in net: | |
self.exclude_hosts.append(host.__str__()) | |
self.exclude_hosts = self.exclude_hosts + exclude_others | |
#id from map --- #template id --- #group id | |
self.dev_type_dict = {'67': {'template': '10105', 'group': '70'}} | |
self.zapi = ZabbixAPI("http://zabbix") | |
self.zapi.login('log', 'pass') | |
def get_billing_hosts(self): | |
"""Get from billing hosts that exact in dev_type_dict, than create output dict | |
billing format "6486 1 3 10.145.0.170 Калинина 10" | |
""" | |
from_billing = requests.get( | |
'https://billing').text.splitlines() | |
self.billing_hosts = dict() | |
for host in from_billing: | |
host = host.split() | |
if host[2] in list(self.dev_type_dict.keys()): | |
self.billing_hosts[host[3]] = dict(self.dev_type_dict[host[2]]) | |
self.billing_hosts[host[3]]['address'] = ' '.join(host[4::]) | |
return self.billing_hosts | |
def get_zabbix_hosts(self): | |
'''Get data from zabbix''' | |
# login to zabbix API | |
self.zab_hosts = dict() | |
self.zab_hosts_id = dict() | |
# get hosts with snmp agent | |
zabbix_hosts_out = self.zapi.host.get( | |
output=["host", "hostid", "description"], filter={"type": 2}) | |
# exclude hosts from specific networks | |
zabbix_hosts_out = [ | |
host for host in zabbix_hosts_out if host['host'] not in self.exclude_hosts] | |
zabbix_hosts_id = [host['hostid'] for host in zabbix_hosts_out] | |
# create host to group dict. Need for find group by hostid. | |
# Faster the create request for each host to API | |
host_to_group = dict() | |
for group in self.zapi.hostgroup.get(selectHosts=zabbix_hosts_id): | |
g = group['groupid'] | |
for i in group['hosts']: | |
host_to_group[i['hostid']] = g | |
# same as group, but template | |
host_to_template = dict() | |
for template in self.zapi.template.get(selectHosts=zabbix_hosts_id): | |
t = template['templateid'] | |
for i in template['hosts']: | |
host_to_template[i['hostid']] = t | |
# create output dict | |
for i in zabbix_hosts_out: | |
host = i['host'] | |
address = i['description'] | |
devid = i['hostid'] | |
group = host_to_group[devid] | |
try: | |
template = host_to_template[devid] | |
except: | |
template = None | |
self.zab_hosts[host] = {'address': address, | |
'group': group, 'template': template} | |
self.zab_hosts_id[host] = {'hostid': devid} | |
return self.zab_hosts | |
def hosts_to_ids(self, hosts): | |
hosts_ids = [x['hostid'] for x in self.zapi.host.get( | |
output=["hostid", "host"]) if x['host'] in hosts] | |
return hosts_ids | |
def monkeybusines(self): | |
''' | |
Get data from billing and create dict like a | |
'10.141.0.220': {'address': 'Восход 45 к1','group': '17','template': '19534'}, | |
''' | |
billing = self.get_billing_hosts() | |
#same as billing but forf zabbix api | |
zab = self.get_zabbix_hosts() | |
#Diff between two dict | |
diff = DeepDiff(zab, billing) | |
# delete | |
if 'dictionary_item_removed' in list(diff.keys()): | |
to_delete = [ast.literal_eval(a.split('root')[1])[ | |
0] for a in diff['dictionary_item_removed']] | |
to_delete = self.hosts_to_ids(to_delete) | |
print('delete: {}'.format(to_delete)) | |
self.zapi.host.delete(*to_delete) | |
# create | |
if 'dictionary_item_added' in list(diff.keys()): | |
to_create = [ast.literal_eval(a.split('root')[1])[ | |
0] for a in diff['dictionary_item_added']] | |
print('create: {}'.format(to_create)) | |
for ip in to_create: | |
if self.ip4regex.match(ip): | |
self.zapi.host.create( | |
host=ip, | |
interfaces={'type': '2', 'main': '1', 'useip': '1', | |
"ip": ip, 'dns': '', 'port': '161'}, | |
groups=[{"groupid": billing[ip]['group']}], | |
templates=[{"templateid": billing[ip]['template']}], | |
description=billing[ip]['address'] | |
) | |
if 'values_changed' in list(diff.keys()): | |
to_change_keys = list(diff['values_changed'].keys()) | |
hosts = [ast.literal_eval(x.split('root')[1].strip( | |
'][').split('][')[0]) for x in to_change_keys] | |
print('modify: {}'.format(hosts)) | |
for i in to_change_keys: | |
host = ast.literal_eval( | |
i.split('root')[1].strip('][').split('][')[0]) | |
hostid = self.zab_hosts_id[host]['hostid'] | |
changekey = ast.literal_eval( | |
i.split('root')[1].strip('][').split('][')[1]) | |
changevalue = diff['values_changed'][i]['new_value'] | |
if changekey == 'address': | |
changekey = 'description' | |
if changekey == 'template': | |
changekey = 'templates' | |
changevalue = [{"templateid": changevalue}] | |
if changekey == 'group': | |
changekey = 'groups' | |
changevalue = [{"groupid": changevalue}] | |
change = {changekey: changevalue} | |
self.zapi.host.update( | |
hostid=hostid, | |
**change | |
) | |
if __name__ == "__main__": | |
pesi = billing_to_zabbix() | |
pesi.monkeybusines() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment