Example #1
0
def get_matching_resources(api, search_field, pattern):
    log.msg("Finding matching resources")
    try:
        resources = api.api_call('GET', options['endpoint'])
    except circonusapi.CirconusAPIError, e:
        print "ERROR: %s" % e
        return None
Example #2
0
def get_matching_resources(api, search_field, pattern):
    log.msg("Finding matching resources")
    try:
        resources = api.api_call('GET', options['endpoint'])
    except circonusapi.CirconusAPIError, e:
        print "ERROR: %s" % e
        return None
def run_query(params, api):
    log.msg("Querying endpoint: %s" % params['endpoint'])
    results = api.api_call("GET", params['endpoint'])
    filtered_results = []
    k, v = params['filter'].split('=', 1)
    log.debug("Filter is checking that %s matches %s" % (k,v))
    for r in results:
        match = re.search(v, r[k])
        if match:
            for i, j in enumerate(match.groups()):
                # Adds group1, group2 etc. variables
                r['group%s' % (i+1)] = j
            filtered_results.append(r)

    return filtered_results
def run_query(params, api):
    log.msg("Querying endpoint: %s" % params['endpoint'])
    results = api.api_call("GET", params['endpoint'])
    filtered_results = []
    k, v = params['filter'].split('=', 1)
    log.debug("Filter is checking that %s matches %s" % (k, v))
    for r in results:
        match = re.search(v, r[k])
        if match:
            for i, j in enumerate(match.groups()):
                # Adds group1, group2 etc. variables
                r['group%s' % (i + 1)] = j
            filtered_results.append(r)

    return filtered_results
Example #5
0
def tag_resources(api, resources, tags, search_field):
    log.msg("Tagging resources:")
    for r in resources:
        old_tags = set(r['tags'])
        new_tags = old_tags | set(tags)
        data = {'tags': list(new_tags)}
        # Exceptions for differnet endpoint types
        if options['endpoint'] == 'graph':
            # You have to provide title/datapoints with any graph changes
            data['title'] = r['title']
            data['datapoints'] = r['datapoints']
        log.debug("Data for %s: %s" % (r['_cid'], data))
        log.msgnb("%s: %s... " % (r['_cid'], r[search_field]))
        if old_tags == new_tags:
            log.msgnf("No change")
            continue
        try:
            api.api_call("PUT", r['_cid'], data)
            log.msgnf("Done")
        except circonusapi.CirconusAPIError, e:
            log.msgnf("Failed")
            log.error(e)
Example #6
0
def tag_resources(api, resources, tags, search_field):
    log.msg("Tagging resources:")
    for r in resources:
        old_tags = set(r['tags'])
        new_tags = old_tags | set(tags)
        data = {'tags': list(new_tags)}
        # Exceptions for differnet endpoint types
        if options['endpoint'] == 'graph':
            # You have to provide title/datapoints with any graph changes
            data['title'] = r['title']
            data['datapoints'] = r['datapoints']
        log.debug("Data for %s: %s" % (r['_cid'], data))
        log.msgnb("%s: %s... " % (r['_cid'], r[search_field]))
        if old_tags == new_tags:
            log.msgnf("No change")
            continue
        try:
            api.api_call("PUT", r['_cid'], data)
            log.msgnf("Done")
        except circonusapi.CirconusAPIError, e:
            log.msgnf("Failed")
            log.error(e)
    api_token = c.get('tokens', account)
    api = circonusapi.CirconusAPI(api_token)

    if params['debug']:
        api.debug = True
        log.debug_enabled = True

    t = template.Template(params['template'])
    params['vars'] = t.parse_nv_params(params['vars'])
    results = run_query(params, api)
    to_add = []
    for r in results:
        merged_params = merge_params(params['vars'], r)
        processed = t.sub(merged_params)
        to_add.append(processed)
    log.msg("Adding the following:")
    # Mapping of endpoints to which attribute is used as a friendly name
    # TODO - add this as a library function?
    title_fields = {
        "/graph": "title",
        "/check_bundle": "display_name",
        "/rule_set": "metric_name",
        "/worksheet": "description",
        "/template": "name",
        "/contact_group": "name",
        "/account": "name",
        "/broker": "_name",
        "/user": "******"
    }
    for r in to_add:
        field = title_fields[r['_cid']]
Example #8
0
        log.debug_enabled = True
    if len(args) < 2:
        usage()
        sys.exit(2)
    api = get_api()
    pattern = args[0]
    tags = args[1:]

    for t in tags:
        if ':' not in t:
            log.error("Tag '%s' should be of the form category:tag" % t)
            sys.exit(1)

    # What field to search on for a given resource type
    search_fields = {
        'check_bundle': 'display_name',
        'graph': 'title',
        'worksheet': 'title'
    }
    # Default to 'title' as a guess for unknown resource types
    search_field = search_fields.get(options['endpoint'], 'title')
    resources = get_matching_resources(api, search_field, pattern)
    log.msg("Matching resources:")
    for r in resources:
        print "    %5s: %s" % (r['_cid'], r[search_field])
    if util.confirm("Do you want to tag these resources with: %s?" % (
            ', '.join(tags))):
        tag_resources(api, resources, tags, search_field)
    else:
        log.msg("Not applying tags")
Example #9
0
        log.debug_enabled = True
    if len(args) < 2:
        usage()
        sys.exit(2)
    api = get_api()
    pattern = args[0]
    tags = args[1:]

    for t in tags:
        if ':' not in t:
            log.error("Tag '%s' should be of the form category:tag" % t)
            sys.exit(1)

    # What field to search on for a given resource type
    search_fields = {
        'check_bundle': 'display_name',
        'graph': 'title',
        'worksheet': 'title'
    }
    # Default to 'title' as a guess for unknown resource types
    search_field = search_fields.get(options['endpoint'], 'title')
    resources = get_matching_resources(api, search_field, pattern)
    log.msg("Matching resources:")
    for r in resources:
        print "    %5s: %s" % (r['_cid'], r[search_field])
    if util.confirm("Do you want to tag these resources with: %s?" %
                    (', '.join(tags))):
        tag_resources(api, resources, tags, search_field)
    else:
        log.msg("Not applying tags")
        log.debug_enabled = True

    t = template.Template(params['template'])
    params['vars'] = t.parse_nv_params(params['vars'])
    results = run_query(params, api)
    to_add = []
    for r in results:
        merged_params = merge_params(params['vars'], r)
        processed = t.sub(merged_params)
        # Allow multiple resources per template by making the template into a
        # list
        if type(processed) == list:
            to_add.extend(processed)
        else:
            to_add.append(processed)
    log.msg("Adding the following:")
    # Mapping of endpoints to which attribute is used as a friendly name
    # TODO - add this as a library function?
    title_fields = {
        "/graph": "title",
        "/check_bundle": "display_name",
        "/rule_set": "metric_name",
        "/worksheet": "description",
        "/template": "name",
        "/contact_group": "name",
        "/account": "name",
        "/broker": "_name",
        "/user": "******"
    }
    for r in to_add:
        field = title_fields[r['_cid']]
Example #11
0
        if o == '-p':
            params['snmp_port'] = a

    # Rest of the command line args
    try:
        params['target'] = args[0]
        params['friendly_name'] = args[1]
    except IndexError:
        usage(params)
        sys.exit(1)
    try:
        params['pattern'] = args[2]
    except IndexError:
        params['pattern'] = None

    # Now initialize the API
    api_token = c.get('tokens', account)
    api = circonusapi.CirconusAPI(api_token)

    if params['debug']:
        api.debug = True
        log.debug_enabled = True

    ports = get_ports(params)
    log.msg("About to add checks for the following ports:")
    for port in sorted(ports):
        log.msg(port)
    params['ports'] = ports
    if util.confirm():
        add_checks(params)
Example #12
0
        if o == '-p':
            params['snmp_port'] = a

    # Rest of the command line args
    try:
        params['target'] = args[0]
        params['friendly_name'] = args[1]
    except IndexError:
        usage(params)
        sys.exit(1)
    try:
        params['pattern'] = args[2]
    except IndexError:
        params['pattern'] = None

    # Now initialize the API
    api_token = c.get('tokens', account)
    api = circonusapi.CirconusAPI(api_token)

    if params['debug']:
        api.debug = True
        log.debug_enabled = True

    ports = get_ports(params)
    log.msg("About to add checks for the following ports:")
    for port in sorted(ports):
        log.msg(port)
    params['ports'] = ports
    if util.confirm():
        add_checks(params)