def putCollectors(): data = request.json sumo = SumoLogic(data["apiid"], data["apikey"]) response = {"errors": [], "success": []} # Go through each collector in the collector_map: for collectorid in data["collector_map"].keys(): # Go through each source for a collector listed in the collector map for sourceid in data["collector_map"][collectorid]: # Find the souce that matches the name (they're by name, for UI) for sourcename in data["source_names"].keys(): # Do we skip this source altogether? (Over)Complicated by transient nature of 'selected' if not "selected" in data["source_names"][sourcename] or ( "selected" in data["source_names"][sourcename] and not data["source_names"][sourcename]["selected"] ): # If there's a match, send the source to Sumo for update if sourceid in data["source_names"][sourcename]["memberids"]: # Are we just here to delete? if ( "delete" in data["source_names"][sourcename] and data["source_names"][sourcename]["delete"] == True ): print "- Deleting collector %s's source %s named %s." % ( collectorid, str(sourceid), sourcename, ) result = sumo.delete_source(collectorid, {"source": {"id": sourceid}}) print "- Delete Source: %s" % result.status_code else: # We'll be mutating this, so keep the original re-usable sourcepayload = deepcopy(data["source_names"][sourcename]["flattened"]) # Blacklists must be a list of path expressions, or missing: if "blacklist" in sourcepayload and not isinstance(sourcepayload["blacklist"], list): blklst = [] [ blklst.append(blacklist.strip()) for blacklist in sourcepayload["blacklist"].split(",") ] sourcepayload["blacklist"] = blklst # Remove keys marked to be ignored for ignorekey in data["source_names"][sourcename]["ignore"]: if ignorekey in sourcepayload: del sourcepayload[ignorekey] # The ID is deliberately absent from the flattened data, add sourcepayload["id"] = sourceid # Grrrrr: # "All modifiable fields must be provided, and all immutable # fields must match those existing in the system." --Sumo sourcepayload["sourceType"] = data["all_sources"][str(sourceid)]["sourceType"] # Convert boolean string to booleans sourcepayload = fixBooleans(sourcepayload) print "+ Updating Collector %s's source %s named %s" % (collectorid, sourceid, sourcename) # You have to get the etag from a collector call # TODO: refactor the initial fetch to include this somehow. throwaway, etag = sumo.source(collectorid, sourceid) result = sumo.update_source(collectorid, {"source": sourcepayload}, etag) sleep(0.15) print "+ Source Update: %s" % result.status_code # , result.text) # if str(result.status_code).startswith("2"): # response['success'].append(result) # else: # response['errors'].append(result) break else: print ". Skipping source %s" % sourcename # TODO: actually return useful information return jsonify(results=response)
# Deletes all sources (not collectors) in a given category. # # python rm-src-by-cat.py <accessId> <accessKey> <category> import sys from sumologic import SumoLogic args = sys.argv sumo = SumoLogic(args[1], args[2]) cat = args[3] cs = sumo.collectors() for c in cs: ss = sumo.sources(c['id']) for s in ss: if s['category'] == cat: sv, _ = sumo.source(c['id'], s['id']) print(sumo.delete_source(c['id'], sv).text)
# Deletes all sources (not collectors) in a given category. # # python rm-src-by-cat.py <accessId> <accessKey> <category> import sys from sumologic import SumoLogic args = sys.argv sumo = SumoLogic(args[1], args[2]) cat = args[3] cs = sumo.collectors() for c in cs: ss = sumo.sources(c['id']) for s in ss: if s['category'] == cat: sv, _ = sumo.source(c['id'], s['id']) print sumo.delete_source(c['id'], sv).text