Esempio n. 1
0
def main():
    z = ZenossAPI()
    CI = sys.argv[1]
    deviceinfo = z.get_device(name=CI)
    uid = deviceinfo['uid']
    copy_template(z, uid)
    add_dp_to_graph(z, uid)
Esempio n. 2
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'])
def main():
    z = ZenossAPI()
    if len(sys.argv) != 2 or sys.argv[1] == '-h':
        usage()
    else:
        text_file = sys.argv[1]
        #if not os.path.isfile(text_file):
        #    sys.exit("The file {} does not exist.".format(text_file))
        with open(text_file, 'r') as f:
            lines = f.readlines()
            for line in lines:
                line_content = line.split(';')
                if len(line_content) == 4:
                    CI = line.split(';')[0]
                    print colored.yellow('='*30)
                    print colored.yellow('Working on {}...'.format(CI))
                    file_path = line.split(';')[1]
                    time_dif = line.split(';')[2]
                    severity = int(line.split(';')[3].replace('\n', ''))
                    try:
                        deviceinfo = z.get_device(name=CI)
                        deviceClass = deviceinfo['deviceClass']
                        if 'Windows/Base' in deviceClass:
                            uid = deviceinfo['uid']
                            #components = z.get_dev_components(uid)
                            #for component in components['result']['data']:
                            #    if component['name'] == 'Cegeka.UpdaterService':
                            collector = deviceinfo['collector']
                            bind_template(z, uid, file_path, time_dif, severity, collector)
                           #         break
                           # else:
                           #     print colored.yellow('INFO :'), '{} does not have the Cegeka.UpdaterService service. \
       # This CI will be skipped.'.format(CI)
                        else:
                            raise Exception('{} is not monitored through WinRM'.format(CI))
                    except Exception as e:
                        print colored.red('ERROR:'), 'Cannot apply the template on {}. {}'.format(CI, e)
                else:
                    print colored.red('ERROR:'), 'The line {} is not correct.'.format(line.replace('\n', ''))
                print colored.yellow('='*30)
                print '\n'
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'
#!/usr/bin/env python

from zen import ZenossAPI
from clint.textui import colored
import sys
import os

z = ZenossAPI()
with open(sys.argv[1], 'r') as f:
    for ci in f.readlines():
        ci = ci.replace('\n', '')
        print 'Working on {}'.format(ci)
        with open('/tmp/threshold_report.csv', 'a') as g:
            deviceinfo = z.get_device(name=ci)
            uid = deviceinfo['uid']
            templates = z.get_obj_templates(uid=uid)
            g.write(
                '\nCI NUMBER|TEMPLATE|THRESHOLD|THRESHOLD TYPE|DATAPOINTS|MinVal|MaxVal|SEVERITY|Time Period|Violation Percentage|ENABLED\n'
            )
            for template in templates['result']['data']:
                t_uid = template['uid']
                template_name = template['name']
                thresholds = z.get_thresholds(t_uid)['result']['data']
                if thresholds:
                    for thr in thresholds:
                        g.write('{}|{}|{}|{}|{}|{}|{}|{}|{}|{}|{}\n'.format(
                            ci, template_name, thr['name'], thr['type'],
                            thr['dataPoints'], thr['minval'] or 'n/a',
                            thr['maxval'] or 'n/a', thr['severity'],
                            thr.get('timePeriod', False) or 'n/a',
                            thr.get('violationPercentage', False) or 'n/a',