def api_json(self, request, id): event = get_event(id) if not event: self.response_not_found() # Get event class e_class = None if event.status in ("A", "S"): for l in event.log: match = self.rx_parse_log.match(l.message) if match: e_class = match.group(1) r = ["["] r += [" {"] r += [' "profile": "%s",' % json_escape(event.managed_object.profile.name)] if e_class: r += [' "event_class__name": "%s",' % e_class] r += [' "raw_vars": {'] rr = [] for k in event.raw_vars: if k in ("collector", "severity", "facility"): continue rr += [' "%s": "%s"' % (json_escape(k), json_escape(str(event.raw_vars[k])))] r += [",\n".join(rr)] r += [" }"] r += [" }"] r += ["]"] return "\n".join(r)
def handle_show(self, options, events, show_json=False): limit = int(options["limit"]) to_suppress = options["suppress"] seen = set() # Message hashes if show_json: self.stdout.write("[\n") spool = None else: self.stdout.write("ID, Object, Class, Subject\n") for e in events: subject = unescape(e.subject) if to_suppress: # Replace volatile parts s = self.rx_volatile_date.sub("", subject) s = self.rx_ip.sub("$IP", s) s = self.rx_float.sub("$FLOAT", s) s = self.rx_int.sub("$INT", s) sh = hashlib.sha1(smart_bytes(s)).hexdigest() # Check subject is already seen if sh in seen: # Suppress seen continue seen.add(sh) if show_json: if spool: print(spool + ",") s = [" {"] s += [' "profile": "%s",' % json_escape(e.managed_object.profile.name)] s += [' "raw_vars": {'] x = [] vars = e.raw_vars keys = [] lkeys = [k for k in vars if k not in ("1.3.6.1.2.1.1.3.0",)] for k in ("source", "profile", "1.3.6.1.6.3.1.1.4.1.0"): if k in vars: keys += [k] lkeys.remove(k) keys += sorted(lkeys) for k in keys: if k in ("collector",): continue x += [' "%s": "%s"' % (json_escape(k), json_escape(vars[k]))] s += [",\n".join(x)] s += [" }"] s += [" }"] spool = "\n".join(s) else: self.stdout.write( "%s, %s, %s, %s\n" % (e.id, e.managed_object.name, e.event_class.name, subject) ) if limit: limit -= 1 if not limit: break if show_json: if spool: self.stdout.write(spool) print("]")
def api_json(self, request, id): event = get_event(id) if not event: self.response_not_found() # Get event class e_class = None if event.status in ("A", "S"): for ll in event.log: match = self.rx_parse_log.match(ll.message) if match: e_class = match.group(1) r = {"profile": event.managed_object.profile.name} if e_class: r["event_class__name"] = "%s" % e_class r["raw_vars"] = { json_escape(k): json_escape(str(event.raw_vars[k])) for k in event.raw_vars if k not in {"collector", "severity", "facility"} } if event.source: r["raw_vars"]["source"] = event.source return smart_text(orjson.dumps(r, option=orjson.OPT_INDENT_2))
def convert(cls, o, i, order=None): if o is None: return indent("null", i) if isinstance(o, six.string_types): return indent('"%s"' % json_escape(o), i) elif isinstance(o, bool): return indent("true" if o else "false", i) elif isinstance(o, six.integer_types): return indent("%d" % o, i) elif isinstance(o, float): return indent(str(o), i) elif isinstance(o, uuid.UUID): return indent('"%s"' % o, i) elif isinstance(o, list): if len(o) == 0: return indent("[]", i) t = [cls.convert(e, 0, order) for e in o] lt = reduce(lambda x, y: x + y, [len(x) for x in t]) lt += i + (len(o) - 1) * 2 if lt > 72: # Long line r = [indent("[", i)] r += [",\n".join(indent(x, i + 4) for x in t)] r += [indent("]", i)] return "\n".join(r) else: r = "[%s]" % ", ".join(t) return indent(r, i) elif isinstance(o, dict): if not o: return indent("{}", i) keys = sorted(o) if order: nk = [k for k in order if k in keys] nk += [k for k in keys if k not in order] keys = nk r = ",\n".join("%s: %s" % (cls.convert(k, 0), cls.convert(o[k], 0)) for k in keys) return indent("{\n%s\n}" % indent(r, 4), i) raise ValueError("Cannot encode %r" % o)