def parse_print(self, body): """ feats_tokens = '|'.join(shared.features) field_tokens = '|'.join(shared.tokens.names()) all_tokens = "{0}|{1}".format(field_tokens, feats_tokens) prex = "%({0})".format(all_tokens) """ name = "Print_{0}".format(shared.GetRandomString()) res = \ """ int {0}(const struct pptags* Tags) {{ std::cout << "{1}\\n"; return CUSTOM_ACTION; }} """.format(name, shared.PrintUtils.replace(body, Actions._sub_print)) # res = "std::cout << \"" #res += re.sub(prex, Actions._sub_print, body) return name, res
def generate(self, names): if not names: return "NULL", "" randname = "ActionList_{0}".format(shared.GetRandomString()) return randname, self._template.format(randname, ", ".join(names))
def parse_execute(self, body): name = "Execute_{0}".format(shared.GetRandomString()) res = \ """ int {0}(const struct pptags* Tags) {{ Buffer<256> command; command << "{1}" << 0U; system( (const char*) command.GetRawData()); return CUSTOM_ACTION; }} """.format(name, shared.PrintUtils.replace(body, Actions._sub_print)) return name, res
def parse_metric_reset(self, body): metric_tokens = '|'.join(shared.metrics) field_tokens = '|'.join(shared.tokens.names()) prex = "({0})[\.]reset[(]({1})?[)]".format(metric_tokens, field_tokens) mo = re.search(prex, body) if mo: metric_name, reset_key = mo.groups() else: return (None, None) name = "ResetMetric_{0}".format(shared.GetRandomString()) metric_id = shared.metrics[metric_name] if reset_key: reset_key = "Tags->Map[{0}]".format(shared.tokens[reset_key]) reset_body = "Tags->ResetMetrics[{0}].VMResetKey = {1};".format( metric_id, reset_key) else: reset_body = "" res = \ """ int {0}(const struct pptags* Tags) {{ Tags->ResetMetrics[{1}].VDReset = 1; Tags->ResetMetrics[{1}].VMReset = 1; {2} return CUSTOM_ACTION; }} """.format(name, metric_id, reset_body) return name, res
def parse_publish(self, body): print "BODY = " + body body = body.split(',') if len(body) != 2: raise Exception('Publish got ' + str(len(body)) + ' params, expected 2: ' + str(body)) channel = body[0] expr = body[1] name = "Publish_{0}".format(shared.GetRandomString()) res = \ """ int {0}(const struct pptags* Tags) {{ Tags->zmq_publish["{1}"] = "{2}"; return EXPORT_PACKET; }} """.format(name, channel, expr) if expr != "raw": res = \ """ int {0}(const struct pptags* Tags) {{ std::stringstream o; o << "{2}"; Tags->zmq_publish["{1}"] = o.str(); return EXPORT_PACKET; }} """.format(name, channel, shared.PrintUtils.replace(expr, Actions._sub_print)) return name, res
def parse_export(self, body): args = ['raw', ''] if body: args = body.split(',') if len(args) != 2: raise Exception('Export got ' + str(len(args)) + ' params, expected 2: ' + str(args)) args[0] = args[0].strip() if args[0] == 'raw': args[1] = '' args[1] = args[1].strip() name = "Export_{0}".format(shared.GetRandomString()) res = \ """ int {0}(const struct pptags* Tags) {{ Tags->export_channel = "{1}"; Tags->export_message = "{2}"; return EXPORT_PACKET; }} """.format(name, args[0], args[1]) # res = "std::cout << \"" #res += re.sub(prex, Actions._sub_print, body) return name, res
if StateName in LocalStates: State = LocalStates[StateName] if Ev.getAttribute('type') == 'packet': StateCode = State.getAttribute('code') StateLevel = State.getAttribute('level') StateDescription = State.getAttribute('description') code.append("{{\"{0}\",\"{1}\"}}".format(StateId, StateCode)) level.append("{{\"{0}\",\"{1}\"}}".format(StateId, StateLevel)) description.append("{{\"{0}\",\"{1}\"}}".format(StateId, StateDescription)) classes.MetricOp.reset() c.Condition.reset() RandToken = shared.GetRandomString() #print "Timeout => ", Timeout MetricOps = State.getElementsByTagName("use-metric") for mop in MetricOps: m = classes.MetricOp(mop, classes.Metric.D) MonstreamLibRows.append( classes.MetricOp.getCode(RandToken) ) #print " -> condition" Conditions = State.getElementsByTagName("condition")
def parse(self, acts): Actions.initMap() action_regex = Actions.regex() table_regex = "({0})([\.]reset)".format('|'.join(shared.tables)) output = "" actionlist = acts.split(';') actionnames = [] actionbodies = [] if not actionlist[0]: return (actionnames, actionbodies) for action in actionlist: mo = re.match(action_regex, action) if mo: tname, tkey, top, tvalue = mo.groups() if not top or not tvalue: break vptr = "" value = tvalue trandname = "{0}_action_{1}".format(tname, shared.GetRandomString()) actionnames.append(trandname) cname, ckey = shared.Table.try_parse_get(tvalue) if cname: vptr = "*" value = "TGET(tables.{0}, Tags->Map[{1}])".format( cname, shared.tokens[ckey]) elif tvalue in shared.features: value = "Tags->Features[{0}]".format( shared.features[tvalue]) # also add to features to calculate! tcode = \ """ int {0}(const struct pptags* Tags) {{ auto cur = TGET(tables.{4}, Tags->Map[{1}]); auto val = {3}; if (!cur || !val) {{ cur = tables.{4}->add(Tags->Map[{1}], std::remove_pointer<decltype(val)>::type()); }} if (val) *cur {2} {5}val; return CUSTOM_ACTION; }} """.format(trandname, shared.tokens[tkey], top, value, tname, vptr) actionbodies.append(tcode) else: # is a standard action ? # here we try to match a standard function body, then it will call # the correct handler (if exists) and pass it the args pact = re.match("(\w+)[(](.*)[)]", action) if pact: actname, actbody = pact.groups() handler = Actions._Map[actname] name, res = handler(actbody) actionnames.append(name) actionbodies.append(res) # print res else: name, res = Actions.parse_metric_reset(action) if name: # is a metric reset action ? actionnames.append(name) actionbodies.append(res) else: #substitution try to find action = re.sub(table_regex, 'table_\g<1>_reset', action) actionnames.append(action) """ if actionlist[0]: output = self._template.format( shared.GetRandomString(), \ ", ".join(actionnames) ) """ return (actionnames, actionbodies)
def parse_plot(self, body): feat_tokens = '|'.join(shared.features) field_tokens = '|'.join(shared.tokens.names()) prex = "(\w+)[ ]({0})[ ](.+)".format(field_tokens) mo = re.search(prex, body) if mo: stream, key, fnames = mo.groups() else: return (None, None) name = "Plot_{0}".format(shared.GetRandomString()) # find key_id key_id = shared.tokens[key] # now parsing feature names elements = fnames.split(' ') elem_template_code = '{{\\"name\\":\\"{0}\\",\\"value\\":%f}}' elem_templates = [] concrete_feat = [] for e in elements: elem_templates.append(elem_template_code.format(e)) concrete_feat.append("Tags->Features[{0}]".format( shared.features[e])) elem_template_s = "[{0}]".format(','.join(elem_templates)) features_tags = ",".join(concrete_feat) res = \ """ int {0}(const struct pptags* Tags) {{ static int piped = -1; static const char* _json_template = "{{\\"stream\\" : \\"{1}\\", \\"key\\" : \\"%s\\", \\"elements\\" : {3} }}\\n"; if (piped == -1) {{ mode_t mode = S_IRWXU|S_IRWXG|S_IRWXO; //mkfifo("{1}", mode); piped = open("{1}", O_WRONLY); }} if (piped != -1) {{ char elems[1024]; // fill elem template int written = snprintf(elems, 1024, _json_template, Tags->Map[{2}].GetString().c_str(), {4}); // send _json_data through pipe p write(piped, elems, written); }} else {{ throw std::runtime_error("[featurelib] ERROR: Pipe not ready!"); }} return CUSTOM_ACTION; }} """.format(name, stream, key_id, elem_template_s, features_tags) return name, res
def parse_save_ip(self, body): print "BODY = " + body body = body.split(',') if len(body) != 3: raise Exception('Publish got ' + str(len(body)) + ' params, expected 3: ' + str(body)) channel = body[0] expr = body[1] where = body[2].strip() name = "save_ip_{0}".format(shared.GetRandomString()) res = \ """ int {0}(const struct pptags* Tags) {{ std::string saved = ""; std::string where = "{3}"; where.erase(std::remove(where.begin(),where.end(),' '),where.end()); if (where == "w"){{ ::ip_wlist.insert (Tags->Map[0].GetString()); saved = "WHITE"; std::cout << "white list contain:"; for (const std::string& x: ip_wlist) std::cout << " " << x; std::cout << std::endl; }} else if (where == "b") {{ ::ip_blist.insert (Tags->Map[0].GetString()); saved = "BLACK"; std::cout << "black list contain:"; for (const std::string& x: ip_blist) std::cout << " " << x; std::cout << std::endl; }} std::stringstream o; o << "{2} saved in " << saved << " list!" << std::endl; Tags->zmq_publish["{1}"] = o.str(); return EXPORT_PACKET; }} """.format(name, channel, expr, where) if expr != "raw": res = \ """ int {0}(const struct pptags* Tags) {{ std::string saved = ""; std::string where = "{3}"; where.erase(std::remove(where.begin(),where.end(),' '),where.end()); if (where == "w"){{ ::ip_wlist.insert (Tags->Map[0].GetString()); saved = "WHITE"; std::cout << "white list contain:"; for (const std::string& x: ip_wlist) std::cout << " " << x; std::cout << std::endl; }} else if (where == "b") {{ ::ip_blist.insert (Tags->Map[0].GetString()); saved = "BLACK"; std::cout << "black list contain:"; for (const std::string& x: ip_blist) std::cout << " " << x; std::cout << std::endl; }} std::stringstream o; o << "{2} saved in " << saved << " list!" << std::endl; Tags->zmq_publish["{1}"] = o.str(); return EXPORT_PACKET; }} """.format(name, channel, shared.PrintUtils.replace(expr, Actions._sub_print), where) return name, res