Пример #1
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-c', dest='collector', help='Zenoss collector.', required=True)
    args = parser.parse_args()
    collector = args.collector
    username = "******"
    password = secrets.f5_certificate_check[username]['password']

    z = ZenossAPI()
    big_ip_devices = z.get_devices('/zport/dmd/Devices/Network/BIG-IP')
    collector_devices = []
    if big_ip_devices['result'].get('devices', False):
        for device in big_ip_devices['result']['devices']:
            if device['collector'] == collector:
                collector_devices.append(device)
    else:
        print 'No BIG-IP devices found on {}\n{}'.format(collector, big_ip_devices)

    for device in collector_devices:
        command = "/opt/zenoss/scripts/get_f5_details.py {} {} '{}'".format(device['ipAddressString'], username, password)
        stdout, stderr = Popen(command, stdout=PIPE, stderr=PIPE, shell=True).communicate()
        if not stderr:
            correct_device_class = '/Devices' + stdout.replace('\n', '')
            zenoss_device = z.get_device(name=device['name'])
            current_device_class = zenoss_device['deviceClass']
            if current_device_class.split('/')[-1].isdigit():
                current_device_class = current_device_class.replace('/{}'.format(current_device_class.split('/')[-1]), '')
            if 'Cannot determine the class' in correct_device_class:
                print '{} for {}.'.format(correct_device_class, device['name'])
            else:
                print 'Class of {}: {}. Correct class: {}.'.format(device['name'], current_device_class, correct_device_class)
            if current_device_class == correct_device_class:
                print '\t\t\tOK'
            else:
                print '\t\t\tNOT_OK'
                file_name = '/tmp/f5_add_{}'.format(device['name'])
                f = open(file_name, 'w')
                f.write('{};{};{};{};{}'.format(device['name'], device['ipAddressString'], username, password, zenoss_device['snmpCommunity']))
                f.close()
                add_command = "/opt/zenoss/scripts/add_f5_device.py --file {} -u monapps -p '***' -d".format(file_name)
                stdout, stderr = Popen(add_command, stdout=PIPE, stderr=PIPE, shell=True).communicate()
                print stdout
                print stderr
        else:
            print 'Cannot get details for {}'.format(device['name'])
Пример #2
0
"""
Prod
"""

from zen import ZenossAPI

z = ZenossAPI()
result_json = {}

big_ip_devices_all = z.get_devices(
    deviceClass='/zport/dmd/Devices/Network/BIG-IP')['result']['devices']
big_ip_devices = [(x['name'], x['uid']) for x in big_ip_devices_all]

for dev in big_ip_devices:
    name = dev[0]
    uid = dev[1]
    class_name = uid.replace('/devices/{}'.format(name), '')
    result_json[name] = {}
    result_json[name]['class'] = class_name.replace('/zport/dmd/Devices', '')

    templates = [x['uid'] for x in z.get_templates(uid)['result']]

    for template in templates:
        template_name = template.split('/')[-1]
        result_json[name][template_name] = {
            'datasources': {},
            'thresholds': {}
        }
        datasources = [{
            'name': x['name'],
            'source': x['source']
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-c',
                        dest='collector',
                        help='Zenoss collector.',
                        required=True)
    args = parser.parse_args()
    collector = args.collector
    template_name = 'CgkF5VpnConn'

    z = ZenossAPI()
    big_ip_devices = z.get_devices('/zport/dmd/Devices/Network/BIG-IP')
    collector_devices = []
    if big_ip_devices['result'].get('devices', False):
        for device in big_ip_devices['result']['devices']:
            if device['collector'] == collector:
                collector_devices.append(device)
    else:
        print 'No BIG-IP devices found on {}\n{}'.format(
            collector, big_ip_devices)

    for device in collector_devices:
        zenoss_device = z.get_device(name=device['name'])
        templates = z.get_templates(zenoss_device['uid'])['result']

        template_found = False
        template_local = False
        differences_found = False
        for template in templates:
            if template_name in template['text'] and template[
                    'path'] == 'Locally Defined':
                template_found = True
                template_local = True
                break
            elif template_name in template['text']:
                template_found = True
                break

        if template_found and template_local:
            print 'Working on {}'.format(device['name'])
            datasource_uid = z.get_data_sources(
                template['uid'])['result']['data'][0]['uid']
            zenoss_datapoints = [{
                'newId': x['newId'],
                'uid': x['uid']
            } for x in z.get_data_points(template['uid'])['result']['data']]
            zenoss_datapoints_no_uid = set(
                [x['newId'] for x in zenoss_datapoints])
            graphs = [{
                'id': x['id'],
                'uid': x['uid']
            } for x in z.list_graphs(template['uid'])['result']]
            snmp_datapoints = set(
                get_snmp_data(device['ipAddressString'],
                              zenoss_device['snmpCommunity']))
            missing_datapoints = list(snmp_datapoints -
                                      zenoss_datapoints_no_uid)
            extra_datapoints = list(zenoss_datapoints_no_uid - snmp_datapoints)

            if missing_datapoints or extra_datapoints:
                differences_found = True

            if differences_found:
                print '\t--differences found.'
                print 'Missing: {}'.format(missing_datapoints)
                print 'Extra: {}'.format(extra_datapoints)
                for ds in missing_datapoints:
                    dp_uid = ''
                    add_dp = z.add_data_point(datasource_uid, ds)
                    if add_dp['result'].get('success', False):
                        print '\t-----datapoint {} added.'.format(ds)
                    else:
                        print '\t-----datapoint {} NOT ADDED. ERROR.'.format(
                            ds)
                    add_graph = z.add_graph(template['uid'], ds)
                    if add_graph['result'].get('success', False):
                        print '\t-----graph {} added.'.format(ds)
                    else:
                        print '\t-----graph {} NOT ADDED. ERROR.'.format(ds)
                    sleep(3)
                    all_datapoints = [{
                        'newId': x['newId'],
                        'uid': x['uid']
                    } for x in z.get_data_points(template['uid'])['result']
                                      ['data']]
                    all_graphs = [{
                        'id': x['id'],
                        'uid': x['uid']
                    } for x in z.list_graphs(template['uid'])['result']]
                    for dp in all_datapoints:
                        if ds == dp['newId']:
                            dp_uid = dp['uid']
                            break
                    for graph in all_graphs:
                        if graph['id'] == ds:
                            add_dp_to_graph = z.add_data_point_to_graph(
                                dp_uid, graph['uid'])
                            if add_dp_to_graph['result'].get('success', False):
                                print '\t-----datapoint {} added to the graph.'.format(
                                    ds)
                            else:
                                print '\t-----datapoint {} was not added to the graph. ERROR.'.format(
                                    ds)
                            break

                for ds in extra_datapoints:
                    for dp in zenoss_datapoints:
                        if dp['newId'] == ds:
                            del_dp = z.del_data_point(dp['uid'])
                            if del_dp['result'].get('success', False):
                                print '\t-----datapoint {} removed.'.format(ds)
                            else:
                                print '\t-----datapoint {} was not removed. ERROR.'.format(
                                    ds)
                    for dp in graphs:
                        if dp['id'] == ds:
                            del_graph = z.del_graph(dp['uid'])
                            if del_graph['result'].get('success', False):
                                print '\t-----graph {} removed.'.format(ds)
                            else:
                                print '\t-----graph {} was not removed. ERROR.'.format(
                                    ds)
            else:
                print '\t--no differences found.'
        elif template_found:
            print 'Working on {}'.format(device['name'])
            print 'Template globally bound.'
        print '\n\n'