Ejemplo n.º 1
0
def update_hostinterface_ip_dns(token, url, host, host_id):
    payload = dict(jsonrpc='2.0',
                   method='hostinterface.update',
                   params=dict(interfaceid=host["interface_id"]),
                   id=1,
                   auth=token)

    if "ip_address" in host.keys():
        payload["params"]["ip"] = host["ip_address"]
    if "dns" in host.keys():
        payload["params"]["dns"] = host["dns"]
    

    if debug_toolkit.DRYRUN: 
        dry_request(url=url, headers=HEADERS, payload=payload)
    else:
        response = requests.post (url=url, headers=HEADERS, data=json.dumps(payload), verify=False)

        if "error" in response.json():
            print("Error while updating ip or dns; host ID:", host_id)
        else:
            if "ip_address" in host.keys():
                print("Host ID:", host_id, "new IP-address:", host["ip_address"])
            if "dns" in host.keys():
                print("Host ID:", host_id, "new dns:", host["dns"])
Ejemplo n.º 2
0
def get_items(token, host, item_type, output="extend", DRYRUN=False):
    """ex-get-templates & get-proxies"""
    url = 'https://%s/zabbix/api_jsonrpc.php' % host
    params = {
                    "output": output #,
                    #"selectInterface": "extend"
                }
    

    if item_type == 'host':
        params['selectInventory'] = ["location", "location_lon", "location_lat"]
        params['selectParentTemplates'] = ["host","name"]
        params['selectGroups'] = ["groupid"]

    payload = {
                "jsonrpc": "2.0",
                "method": item_type + ".get",
                "params": params,
                "auth": token,
                "id": 1
            }

    if debug_toolkit.DRYRUN and DRYRUN: 
        dry_request(url=url, headers=HEADERS, payload=payload)
    else:
        response = requests.post (url=url, headers=HEADERS, data=json.dumps(payload), verify=False)

        if 'result' in response.json().keys():
            return response.json()['result']
        else:
            print('Error! Incorrect response from get_items.\n' + response.json()['result'])
            exit()
Ejemplo n.º 3
0
def get_hosts_by_groupids(token, host, ids, DRYRUN=False):
    url = 'https://%s/zabbix/api_jsonrpc.php' % host
    payload = dict(jsonrpc='2.0',
                        method='host.get',
                        params=dict(output=['name', 'host'],
                                    selectInventory=["alias","location","location_lon","location_lat"],
                                    selectGroups=["groupid", "name"],
                                    selectParentTemplates=["templateid"],
                                    selectInterfaces=["ip", "interfaceid", "dns", "type"],
                                    groupids=ids),
                        id=1,
                        auth=token)



    if debug_toolkit.DRYRUN and DRYRUN: 
        dry_request(url=url, headers=HEADERS, payload=payload)
    else:
        response = requests.post (url=url, headers=HEADERS, data=json.dumps(payload), verify=False)

        if 'result' in response.json().keys():
            return response.json()['result']
        else:
            print('Error! Incorrect response from get_hosts_by_ids.')
            exit()
Ejemplo n.º 4
0
def create_table_record(table, new_keys, id_field, server, user, password):
    url = 'http://{}/api/now/table/{}'.format(server, table)
    headers = {
        "Content-Type": "application/json",
        "Accept": "application/json"
    }
    #for key, value in new_keys.items():
    #payload = dict(id=key, name=value)
    #for new_record in new_keys:
    #    payload = new_record
    for key, value in new_keys.items():
        payload = {id_field: key, 'host': value}

        if debug_toolkit.DRYRUN:
            dry_request(url=url,
                        headers=HEADERS,
                        method='post',
                        payload=json.dumps(payload))
        else:
            response = requests.post(url,
                                     auth=(user, password),
                                     headers=headers,
                                     data=json.dumps(payload))
            if response.status_code == 201:
                print('Создана следующая запись "{} - {}"'.format(key, value))
                if debug_toolkit.DEBUG: print(response.json())
            else:
                print(
                    'При создании записи "{} - {}" возникли проблемы:'.format(
                        key, value))
                print(response.json())
Ejemplo n.º 5
0
def get_hostgroups(token, host, DRYRUN=False):
    url = 'https://%s/zabbix/api_jsonrpc.php' % host
    payload = dict(jsonrpc='2.0',
                         method='hostgroup.get',
                         params=dict(output=['groupid', 'name']),
                         auth=token,
                         id=1)
    if debug_toolkit.DRYRUN and DRYRUN: 
        dry_request(url=url, headers=HEADERS, payload=payload)
    else:
        response = requests.post (url=url, headers=HEADERS, data=json.dumps(payload), verify=False)
        if 'result' in response.json().keys():
            return response.json()['result']
        else:
            print('Error! Incorrect response from get_servicenow_groups_from_zabbix.')
            exit()
Ejemplo n.º 6
0
def delete_table_record(table, uid, server, user, password):
    url = 'http://{}/api/now/table/{}/{}'.format(server, table, uid)
    headers = {
        "Content-Type": "application/json",
        "Accept": "application/json"
    }

    if debug_toolkit.DRYRUN:
        dry_request(url=url, headers=HEADERS, method='delete')
    else:
        response = requests.delete(url, auth=(user, password), headers=headers)
        if response.status_code == 204:
            print('Запись успешно удалена.')
        else:
            print('Статус: {}, Ответ: {}'.format(response.status_code,
                                                 response.json()))
Ejemplo n.º 7
0
def update_host_groups(token, url, host, host_id):
    payload = dict(jsonrpc='2.0',
                   method='host.update',
                   params=dict(hostid=host_id,
                               groups=host["groups"]),
                   id=1,
                   auth=token)
    

    if debug_toolkit.DRYRUN: 
        dry_request(url=url, headers=HEADERS, payload=payload)
    else:
        response = requests.post (url=url, headers=HEADERS, data=json.dumps(payload), verify=False)
        if "error" in response.json():
            print("Error while updating groups; host ID:", host_id)
        else:
            print("Host ID:", host_id, "new groups:", host["groups"])
Ejemplo n.º 8
0
def update_hostinterface_type(token, url, host, host_id):
    payload = dict(jsonrpc='2.0',
                   method='hostinterface.update',
                   params=dict(interfaceid=host["interface_id"],
                               type=host["interface_type"]),
                   id=1,
                   auth=token)
    

    if debug_toolkit.DRYRUN: 
        dry_request(url=url, headers=HEADERS, payload=payload)
    else:
        response = requests.post (url=url, headers=HEADERS, data=json.dumps(payload), verify=False)
        if "error" in response.json():
            print("Error while updating interface_type; host ID:", host_id)
        else:
            print("Host ID:", host_id, "new interface_type:", host["interface_type"])
Ejemplo n.º 9
0
def get_table_records(server, user, password, table, query="", DRYRUN=False):
    url = 'http://{}/api/now/table/{}?{}'.format(server, table, query)

    if debug_toolkit.DRYRUN and DRYRUN:
        dry_request(url=url, headers=HEADERS)
    else:
        response = requests.get(url=url,
                                headers=HEADERS,
                                timeout=TIMEOUT,
                                auth=(user, password),
                                verify=False)
        if 'result' in response.json().keys():
            return response.json()['result']
        else:
            print('Error! Incorrect response from get_table_records.\n',
                  response.json())
            exit()
Ejemplo n.º 10
0
def create_hostgroups(new_groups, token, server):
    url = 'https://%s/zabbix/api_jsonrpc.php' % server
    payload = dict(jsonrpc='2.0',
                   method='hostgroup.create',
                   params=dict(name=''),
                   auth=token,
                   id=1)
    
    for group in new_groups:
        payload['params']['name'] = group

        if debug_toolkit.DRYRUN: 
            dry_request(url=url, headers=HEADERS, payload=payload)
        else:
            response = requests.post (url=url, headers=HEADERS, data=json.dumps(payload), verify=False)
            if "error" in response.json():
                print("Error while creating group ", group)
            else:
                print("The group", group, "has been created.")
Ejemplo n.º 11
0
def get_token(server, user, password, DRYRUN=False):
    url = 'https://%s/zabbix/api_jsonrpc.php' % server
    payload = dict(jsonrpc='2.0',
                         method='user.login',
                         params=dict(user=user,
                                     password=password),
                         id=1)
    headers = {'content-type': 'application/json', 'cache-control': 'no-cache'}
    req_dict = {'url':url, 'headers':headers, 'data':json.dumps(payload)}

    if debug_toolkit.DRYRUN and DRYRUN: 
        dry_request(url=url, headers=headers, payload=payload)
    else:
        response = requests.post (url=url, headers=HEADERS, data=json.dumps(payload), verify=False)

        if 'result' in response.json().keys():
            return response.json()['result']
        else:
            print('Error! Incorrect response from get_token.')
            exit() 
Ejemplo n.º 12
0
def modify_table_records(table, uid, new_value, server, user, password):
    url = 'http://{}/api/now/table/{}/{}'.format(server, table, uid)
    headers = {
        "Content-Type": "application/json",
        "Accept": "application/json"
    }
    #payload = dict(name=new_value)
    payload = dict(host=new_value)

    if debug_toolkit.DRYRUN:
        dry_request(url=url, headers=HEADERS, method='put', payload=payload)
    else:
        response = requests.put(url,
                                auth=(user, password),
                                headers=headers,
                                data=json.dumps(payload))
        if response.status_code == 200:
            print(' Выполнено.')
        else:
            print('При синхронизации значения возникли проблемы...')
            print(response.json())
Ejemplo n.º 13
0
def update_host_inventory(token, url, host, host_id):
    payload = dict(jsonrpc='2.0',
                   method='host.update',
                   params=dict(hostid=host_id,inventory={}),
                   id=1,
                   auth=token)
    if "location" in host.keys():
        payload["params"]['inventory']['location'] = host["location"]
        payload["params"]['inventory']['location_lon'] = host["longitude"]
        payload["params"]['inventory']['location_lat'] = host["latitude"]
    

    if debug_toolkit.DRYRUN: 
        dry_request(url=url, headers=HEADERS, payload=payload)
    else:
        response = requests.post (url=url, headers=HEADERS, data=json.dumps(payload), verify=False)

        if "error" in response.json():
            print("Error while updating host or name; host ID:", host_id)
        else:
            if "location" in host.keys():
                print("Host ID:", host_id, "new host location:", host["location"])
Ejemplo n.º 14
0
def update_host_name(token, url, host, host_id):
    payload = dict(jsonrpc='2.0',
                   method='host.update',
                   params=dict(hostid=host_id),
                   id=1,
                   auth=token)
    if "host" in host.keys():
        payload["params"]["host"] = host["host"]
    if "name" in host.keys():
        if host["name"]: payload["params"]["name"] = host["name"]
    

    if debug_toolkit.DRYRUN: 
        dry_request(url=url, headers=HEADERS, payload=payload)
    else:
        response = requests.post (url=url, headers=HEADERS, data=json.dumps(payload), verify=False)

        if "error" in response.json():
            print("Error while updating host or name; host ID:", host_id)
        else:
            if "host" in host.keys():
                print("Host ID:", host_id, "new host value:", host["host"])
            if "name" in host.keys():
                print("Host ID:", host_id, "new name value:", host["name"])
Ejemplo n.º 15
0
def create_hosts(token, server, new_hosts, groupids, export_group):
    url = 'https://%s/zabbix/api_jsonrpc.php' % server    
    payload = dict(jsonrpc='2.0',
                   method='host.create',
                   auth=token,
                   id=1,
                   params=dict(name='',
                               host='',
                               templates=[],
                               groups=[dict(groupid='')],
                               inventory_mode=0,
                               interfaces=[dict(type=None,
                                                main=1,
                                                useip=1,
                                                ip='',
                                                dns='',
                                                port='161')],
                               inventory=dict(alias='')))
    
    for nh in new_hosts:
        payload['params']['groups'] = [dict(groupid='')]
        #if 'templates' in payload['params']:
        payload['params']['templates'] = []

        payload["params"]["name"] = nh["name"]["value"]
        payload["params"]["host"] = nh["name"]["value"] + "_" + nh["sys_id"]["value"]
        
        #if nh['x_itgra_monitoring_zabbix_template']:

        payload['params']['templates'] = [{'templateid': number} for number in nh['x_itgra_monitoring_zabbix_template']["value"]]
        
        payload['params']['inventory']['alias'] = nh['sys_id']["value"]

        payload['params']['inventory']['location_lat'] = nh['latitude']["value"]
        payload['params']['inventory']['location_lon'] = nh['longitude']["value"]


        # Determine which proxy to use by domain name (just hard code proxy id)
        # if 'sn.vcloud.kz' in nh["fqdn"] or 'sn.vcloud.kz' in nh["name"]:
        if nh['x_itgra_monitoring_zabbix_proxy']["value"]: payload['params']['proxy_hostid'] = nh['x_itgra_monitoring_zabbix_proxy']["value"]

        payload['params']['groups'][0]['groupid'] = groupids[nh['sys_class_name']["display_value"]]
        payload['params']['groups'].append(dict(groupid=export_group))
        payload['params']['interfaces'][0]['ip'] = nh['ip_address']["value"]
        payload["params"]["interfaces"][0]["dns"] = nh["fqdn"]["value"]
        
        if nh["sys_class_name"]["display_value"] == "Linux Server" or nh["sys_class_name"]["display_value"] == "Windows Server":
            payload["params"]["interfaces"][0]["type"] = 1
        else:
            payload["params"]["interfaces"][0]["type"] = 2


        if debug_toolkit.DRYRUN: 
            dry_request(url=url, headers=HEADERS, payload=payload)
        else:
            response = requests.post (url=url, headers=HEADERS, data=json.dumps(payload), verify=False)

            if 'error' in response.json():
                print('\nError while creating host "%s"\n' % nh['name'], response.json())
            else:
                print('Создан новый узел %s' % payload["params"]["name"])
            
    print('\nПроцедура синхронизации новых хостов завершена\n')