Skip to content

Commit 4ca2ba3

Browse files
committed
add map reduce for top n for elk
1 parent 3fb8f2d commit 4ca2ba3

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

reporting/top_n.elk

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
"top_n": {
2+
"scripted_metric": {
3+
"init_script": """
4+
state.top_n = new HashMap();
5+
state.top_n["dns_top_qname2"] = new LinkedHashMap();
6+
state.top_n["dns_top_qname3"] = new LinkedHashMap();
7+
state.top_n["dns_top_nxdomain"] = new LinkedHashMap();
8+
state.top_n["dns_top_qtype"] = new LinkedHashMap();
9+
state.top_n["dns_top_rcode"] = new LinkedHashMap();
10+
state.top_n["dns_top_refused"] = new LinkedHashMap();
11+
state.top_n["dns_top_srvfail"] = new LinkedHashMap();
12+
state.top_n["dns_top_udp_ports"] = new LinkedHashMap();
13+
state.top_n["packets_top_ASN"] = new LinkedHashMap();
14+
state.top_n["packets_top_geoLoc"] = new LinkedHashMap();
15+
state.top_n["packets_top_ipv4"] = new LinkedHashMap();
16+
state.top_n["packets_top_ipv6"] = new LinkedHashMap();
17+
state.top_n["xact_in_top_slow"] = new LinkedHashMap();
18+
state.top_n["xact_out_top_slow"] = new LinkedHashMap();
19+
""",
20+
"map_script": """
21+
long deep = doc["http.packets_deep_samples"][0].longValue();
22+
long total = doc["http.packets_total"][0].longValue();
23+
double adjust = 1.0;
24+
if (total > 0L && deep > 0L) {
25+
adjust = Math.round(1.0 / (deep.doubleValue() / total.doubleValue()));
26+
}
27+
for (Map.Entry entry: state.top_n.entrySet()) {
28+
for (int i = 0; i <= 9; i++) {
29+
String name_key = "http." + entry.getKey() + "_" + String.valueOf(i) + "_name.raw";
30+
String val_key = "http." + entry.getKey() + "_" + String.valueOf(i) + "_estimate";
31+
if (doc.containsKey(name_key) && doc[name_key].size() > 0 && doc[val_key].size() > 0) {
32+
String name = doc[name_key][0].toLowerCase();
33+
long val = doc[val_key][0].longValue();
34+
if (state.top_n[entry.getKey()].containsKey(name)) {
35+
state.top_n[entry.getKey()][name] += (long)(val*adjust);
36+
}
37+
else {
38+
state.top_n[entry.getKey()][name] = (long)(val*adjust);
39+
}
40+
}
41+
}
42+
}
43+
""",
44+
"combine_script": """
45+
for (Map.Entry entry: state.top_n.entrySet()) {
46+
ArrayList list = state.top_n[entry.getKey()].entrySet().stream().sorted(Map.Entry.comparingByValue())
47+
.collect(Collectors.toList());
48+
Collections.reverse(list);
49+
state.top_n[entry.getKey()].clear();
50+
int i = 0;
51+
for (Map.Entry subentry: list) {
52+
i++;
53+
if (i > 10)
54+
break;
55+
state.top_n[entry.getKey()].put(subentry.getKey(), subentry.getValue());
56+
}
57+
}
58+
return state.top_n;
59+
""",
60+
"reduce_script": """
61+
HashMap top_n = new HashMap();
62+
for (shard_map in states) {
63+
for (Map.Entry entry : shard_map.entrySet()) {
64+
if (!top_n.containsKey(entry.getKey())) {
65+
top_n[entry.getKey()] = new LinkedHashMap();
66+
}
67+
for (Map.Entry subentry : entry.getValue().entrySet()) {
68+
if (top_n[entry.getKey()].containsKey(subentry.getKey())) {
69+
top_n[entry.getKey()][subentry.getKey()] += subentry.getValue();
70+
}
71+
else {
72+
top_n[entry.getKey()][subentry.getKey()] = subentry.getValue();
73+
}
74+
}
75+
}
76+
}
77+
for (Map.Entry entry: top_n.entrySet()) {
78+
ArrayList list = top_n[entry.getKey()].entrySet().stream().sorted(Map.Entry.comparingByValue())
79+
.collect(Collectors.toList());
80+
Collections.reverse(list);
81+
top_n[entry.getKey()].clear();
82+
int i = 0;
83+
for (Map.Entry subentry: list) {
84+
i++;
85+
if (i > 10)
86+
break;
87+
top_n[entry.getKey()].put(subentry.getKey(), subentry.getValue());
88+
}
89+
}
90+
return top_n;
91+
"""
92+
}
93+
}

0 commit comments

Comments
 (0)