InfluxSingleLine { public static InfluxSingleLine of(String lineStr, Configure configure, long receivedTime) { char[] chars = lineStr.toCharArray(); char sink = '\0'; int mode = 0; StringBuilder measurementSb = new StringBuilder(); StringBuilder tagKeySb = new StringBuilder(); StringBuilder tagValueSb = new StringBuilder(); boolean tagKeyMode = true; Map<String, String> tags = new HashMap<String, String>(); StringBuilder fieldKeySb = new StringBuilder(); StringBuilder fieldValueSb = new StringBuilder(); boolean fieldKeyMode = true; Map<String, String> fields = new HashMap<String, String>(); StringBuilder timestampSb = new StringBuilder(); for (int pos = 0; pos < lineStr.length(); pos++) { char c = chars[pos]; if (mode == 0) { if (sink == '\\') { measurementSb.append(c); sink = '\0'; } else { switch (c) { case '\\': sink = '\\'; break; case ',': mode++; break; default: measurementSb.append(c); break; } } } else if (mode == 1) { if (sink == '\\') { if (tagKeyMode) { tagKeySb.append(c); } else { tagValueSb.append(c); } sink = '\0'; } else { switch (c) { case '\\': sink = '\\'; break; case ' ': mode++; if (tagKeySb.length() > 0) { tags.put(tagKeySb.toString(), tagValueSb.toString()); } break; case '=': tagKeyMode = false; break; case ',': tagKeyMode = true; tags.put(tagKeySb.toString(), tagValueSb.toString()); tagKeySb = new StringBuilder(); tagValueSb = new StringBuilder(); break; default: if (tagKeyMode) { tagKeySb.append(c); } else { tagValueSb.append(c); } break; } } } else if (mode == 2) { if (sink == '\\') { if (fieldKeyMode) { fieldKeySb.append(c); } else { fieldValueSb.append(c); } sink = '\0'; } else { switch (c) { case '\\': if (sink != '"') { sink = '\\'; } break; case '"': if (sink == '"') { sink = '\0'; } else { sink = '"'; } break; case ' ': if (sink != '"') { mode++; if (fieldKeySb.length() > 0) { fields.put(fieldKeySb.toString(), fieldValueSb.toString()); } } break; case '=': if (sink != '"') { fieldKeyMode = false; } break; case ',': if (sink != '"') { fieldKeyMode = true; fields.put(fieldKeySb.toString(), fieldValueSb.toString()); fieldKeySb = new StringBuilder(); fieldValueSb = new StringBuilder(); } break; default: if (sink != '"') { if (fieldKeyMode) { fieldKeySb.append(c); } else { fieldValueSb.append(c); } } break; } } } else if (mode == 3) { timestampSb.append(c); } } String measurement = measurementSb.toString(); ScouterTgMtConfig tConfig = configure.telegrafInputConfigMap.get(measurement); if (tConfig == null) { return null; } if (!configure.input_telegraf_debug_enabled && tConfig.isDebugEnabled()) { Logger.println("TG006", "[line protocol received] " + lineStr); } if (!tConfig.isEnabled()) { return null; } if (!tConfig.isValidConfig()) { return null; } if (!tConfig.isTagFilterMatching(tags)) { return null; } try { return new InfluxSingleLine(tConfig, measurement, tags, fields, receivedTime, Long.parseLong(timestampSb.toString()) / 1000000, tConfig.isDebugEnabled()); } catch (Throwable t) { t.printStackTrace(); return null; } } private  InfluxSingleLine(ScouterTgMtConfig tConfig,
                             String measurement,
                             Map<String, String> tags,
                             Map<String, String> fields,
                             long receivedTime,
                             long timestampOrigin,
                             boolean debug); static String toLineStringKey(String lineString); static InfluxSingleLine of(String lineStr, Configure configure, long receivedTime); String getMeasurement(); String getHost(); String getFamily(); String getObjType(); String getObjTypeIcon(); String getObjName(); int getObjHash(); long getReceivedTime(); long getTimestampOrigin(); Map<String, String> getTags(); Map<CounterProtocol, NumberValue> getNumberFields(); boolean isDebug(); ObjectPack toObjectPack(String address, int deadTime); PerfCounterPack toPerfCounterPack(); @Override String toString();  }
@Test public void of_test() { InfluxSingleLine.of("mem,host=GunMac.skbroadband,region=seoul used=11656097792i,free=1467994112i,cached=0i,buffered=0i,wired=2481405952i,slab=0i,available_percent=32.152581214904785,total=17179869184i,available=5523771392i,active=8165543936i,inactive=4055777280i,used_percent=67.84741878509521 1532269780000000000", Configure.getInstance(), System.currentTimeMillis()); }
@Test public void of_test_InfluxSingleLine_can_parse_line_protocol_with_tag_filter_unmatching_case() { Configure.newInstanceForTestCase(); System.setProperty("input_telegraf_$cpu$_counter_mappings", "usage_user:tg-$cpu$-user:$cpu$ user:%:false" + ",usage_system:tg-$cpu$-sys:$cpu$ sys:%:false"); System.setProperty("input_telegraf_$cpu$_objFamily_base", "HOST-METRIC"); System.setProperty("input_telegraf_$cpu$_objType_base", "HOST-METRIC"); System.setProperty("input_telegraf_$cpu$_objName_base", "HOST-METRIC"); System.setProperty("input_telegraf_$cpu$_host_tag", "host"); System.setProperty("input_telegraf_$cpu$_tag_filter", "cpu:cpu-total,cpu:cpu2"); Configure.newInstanceForTestCase(); String protocol = "cpu,cpu=cpu-0,host=vm0.us" + " usage_user=18.315842078960518,usage_iowait=0,usage_irq=0,usage_softirq=0" + ",usage_guest=0,usage_system=7.8210894552723635,usage_idle=73.86306846576711" + ",usage_nice=0,usage_steal=0,usage_guest_nice=0" + " 1532269780000000000"; InfluxSingleLine line = InfluxSingleLine.of(protocol, Configure.getInstance(), System.currentTimeMillis()); assertNull(line); }
@Test public void of_test_InfluxSingleLine_can_parse_line_protocol_with_tag_filter_unmatching_case_in_not_condition() { Configure.newInstanceForTestCase(); System.setProperty("input_telegraf_$cpu$_counter_mappings", "usage_user:tg-$cpu$-user:$cpu$ user:%:false" + ",usage_system:tg-$cpu$-sys:$cpu$ sys:%:false"); System.setProperty("input_telegraf_$cpu$_objFamily_base", "HOST-METRIC"); System.setProperty("input_telegraf_$cpu$_objType_base", "HOST-METRIC"); System.setProperty("input_telegraf_$cpu$_objName_base", "HOST-METRIC"); System.setProperty("input_telegraf_$cpu$_host_tag", "host"); System.setProperty("input_telegraf_$cpu$_tag_filter", "cpu:!cpu-0"); Configure.newInstanceForTestCase(); String protocol = "cpu,cpu=cpu-0,host=vm0.us" + " usage_user=18.315842078960518,usage_iowait=0,usage_irq=0,usage_softirq=0" + ",usage_guest=0,usage_system=7.8210894552723635,usage_idle=73.86306846576711" + ",usage_nice=0,usage_steal=0,usage_guest_nice=0" + " 1532269780000000000"; InfluxSingleLine line = InfluxSingleLine.of(protocol, Configure.getInstance(), System.currentTimeMillis()); assertNull(line); }
@Test public void of_test_InfluxSingleLine_can_parse_line_protocol_with_tag_filter_matching_case() { Configure.newInstanceForTestCase(); System.setProperty("input_telegraf_$cpu$_counter_mappings", "usage_user:tg-$cpu$-user:$cpu$ user:%:false" + ",usage_system:tg-$cpu$-sys:$cpu$ sys:%:false"); System.setProperty("input_telegraf_$cpu$_objFamily_base", "HOST-METRIC"); System.setProperty("input_telegraf_$cpu$_objType_base", "HOST-METRIC"); System.setProperty("input_telegraf_$cpu$_objName_base", "HOST-METRIC"); System.setProperty("input_telegraf_$cpu$_host_tag", "host"); System.setProperty("input_telegraf_$cpu$_tag_filter", "cpu:cpu-0,cpu:cpu-total"); Configure.newInstanceForTestCase(); String protocol = "cpu,cpu=cpu-total,host=vm0.us" + " usage_user=18.315842078960518,usage_iowait=0,usage_irq=0,usage_softirq=0" + ",usage_guest=0,usage_system=7.8210894552723635,usage_idle=73.86306846576711" + ",usage_nice=0,usage_steal=0,usage_guest_nice=0" + " 1532269780000000000"; InfluxSingleLine line = InfluxSingleLine.of(protocol, Configure.getInstance(), System.currentTimeMillis()); assertNotNull(line); }
@Test public void of_test_InfluxSingleLine_can_parse_line_protocol_with_tag_filter_matching_case_in_not_condition() { Configure.newInstanceForTestCase(); System.setProperty("input_telegraf_$cpu$_counter_mappings", "usage_user:tg-$cpu$-user:$cpu$ user:%:false" + ",usage_system:tg-$cpu$-sys:$cpu$ sys:%:false"); System.setProperty("input_telegraf_$cpu$_objFamily_base", "HOST-METRIC"); System.setProperty("input_telegraf_$cpu$_objType_base", "HOST-METRIC"); System.setProperty("input_telegraf_$cpu$_objName_base", "HOST-METRIC"); System.setProperty("input_telegraf_$cpu$_host_tag", "host"); System.setProperty("input_telegraf_$cpu$_tag_filter", "cpu:!cpu-0"); Configure.newInstanceForTestCase(); String protocol = "cpu,cpu=cpu-total,host=vm0.us" + " usage_user=18.315842078960518,usage_iowait=0,usage_irq=0,usage_softirq=0" + ",usage_guest=0,usage_system=7.8210894552723635,usage_idle=73.86306846576711" + ",usage_nice=0,usage_steal=0,usage_guest_nice=0" + " 1532269780000000000"; InfluxSingleLine line = InfluxSingleLine.of(protocol, Configure.getInstance(), System.currentTimeMillis()); assertNotNull(line); }
@Test public void of_test_InfluxSingleLine_can_parse_quoted_value() { System.setProperty("input_telegraf_$mem$_counter_mappings" , "used:tg-mem-used" + ",active:active:active:byte:false" + ",available:available:available:byte:false" + ",available_percent:tg-mem-free-pct:memory percent:%:false"); System.setProperty("input_telegraf_$mem$_objFamily_base", "HOST-METRIC"); System.setProperty("input_telegraf_$mem$_objType_base", "HOST-METRIC"); System.setProperty("input_telegraf_$mem$_objName_base", "HOST-METRIC"); System.setProperty("input_telegraf_$mem$_host_tag", "host"); Configure.newInstanceForTestCase(); String protocol = "mem,host=vm0.us,os=aix,key=memory1" + " used=11656097792i,free=1467994112i,quoted=\"It is quoted\",cached=0i,buffered=0i,wired=2481405952i" + ",slab=0i,available_percent=32.152581214904785,total=17179869184i,available=5523771392i" + ",active=8165543936i,inactive=4055777280i,used_percent=67.84741878509521" + " 1532269780000000000"; InfluxSingleLine line = InfluxSingleLine.of(protocol, Configure.getInstance(), System.currentTimeMillis()); assertEquals(line.timestampOrigin, 1532269780000d, 1000); }
@Test @Ignore public void perf_test() { String metric = "mem,host=GunMac.skbroadband,region=seoul used=11656097792i,free=1467994112i,cached=0i,buffered=0i,wired=2481405952i,slab=0i,available_percent=32.152581214904785,total=17179869184i,available=5523771392i,active=8165543936i,inactive=4055777280i,used_percent=67.84741878509521 1532269780000000000"; long start = System.currentTimeMillis(); for (int i = 0; i < 300000; i++) { InfluxSingleLine.of(metric, Configure.getInstance(), start); } System.out.println("[of millis]" + (System.currentTimeMillis() - start)); start = System.currentTimeMillis(); for (int i = 0; i < 300000; i++) { InfluxSingleLine.of(metric, Configure.getInstance(), start); } System.out.println("[of millis]" + (System.currentTimeMillis() - start)); start = System.currentTimeMillis(); for (int i = 0; i < 300000; i++) { InfluxSingleLine.of(metric, Configure.getInstance(), start); } System.out.println("[of millis]" + (System.currentTimeMillis() - start)); }