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']] log.msg(r[field]) if util.confirm("%s additions to be made. Continue?" % len(to_add)): for r in to_add: field = title_fields[r['_cid']] log.msgnb("Adding entry %s..." % r[field]) try: api.api_call("POST", r['_cid'], r) except circonusapi.CirconusAPIError, e: log.msgnf("Failed") log.error(e) continue log.msgnf("Success")
if type(data) == dict: new_data = [] for k, v in data.items(): v['_cid'] = k new_data.append(v) return new_data return data def make_additions(api, data): for i in data: endpoint = re.sub("(?!^)/.*", "", i['_cid']) print "Making API Call: POST %s ..." % (endpoint), try: api.api_call("POST", endpoint, i) except circonusapi.CirconusAPIError, e: print "Error" print " %s" % e continue print "Success" if __name__ == '__main__': args = parse_options() if len(args) != 1: usage() sys.exit(2) api = get_api() data = load_json_file(args[0]) data = fix_data_format(data) if util.confirm("%s additions, OK to continue?" % len(data)): make_additions(api, data)
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 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")
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']] log.msg(r[field]) if util.confirm("%s additions to be made. Continue?" % len(to_add)): for r in to_add: field = title_fields[r['_cid']] log.msgnb("Adding entry %s..." % r[field]) try: api.api_call("POST", r['_cid'], r) except circonusapi.CirconusAPIError, e: log.msgnf("Failed") log.error(e) continue log.msgnf("Success")
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)