|
1 | | -"""bubble - re-emit a log record with all superdomains |
| 1 | +"""bubble - re-emit a log record with superdomain |
2 | 2 |
|
3 | | - | bubble [field=host] |
| 3 | + | bubble [field=host] [parts=3] |
4 | 4 |
|
5 | | -add 'superhost' and 'parts' fields. |
| 5 | +adds 'superhost' field |
6 | 6 |
|
7 | 7 | """ |
8 | 8 |
|
|
13 | 13 |
|
14 | 14 | ip_rex = re.compile(ipregex) |
15 | 15 |
|
16 | | -def super_domains(host): |
17 | | - """ |
18 | | - FIXME |
19 | | - >>> list(super_domains("a.b.com")) |
20 | | - {'host': '*.b.com', 'parts': 2}, {'host': '*.com', 'parts': 1}] |
21 | | - >>> list(super_domains("1.2.3.4")) |
22 | | - [{'host': '1.2.3.*', 'parts': 3}, {'host': '1.2.*', 'parts': 2}, {'host': '1.*', 'parts': 1}] |
23 | | - """ |
24 | | - |
| 16 | +def super_domain(host, output_parts): |
25 | 17 | parts = host.split(".") |
26 | 18 | num_parts = len(parts) |
27 | | - yield dict(superhost=host, parts=num_parts) |
| 19 | + if output_parts > num_parts: |
| 20 | + return host |
| 21 | + |
28 | 22 | if ip_rex.match(host): |
29 | | - for x in range(1,4): |
30 | | - host = '.'.join(parts[:-x]) |
31 | | - p = num_parts - x |
32 | | - yield dict(superhost=host, parts=p) |
| 23 | + host = '.'.join(parts[:-output_parts]) |
33 | 24 | else: |
34 | | - for x in range(num_parts-1,0,-1): |
35 | | - host = '.'.join(parts[-x:]) |
36 | | - p = x |
37 | | - yield dict(superhost=host, parts=p) |
| 25 | + host = '.'.join(parts[-output_parts:]) |
| 26 | + |
| 27 | + return host |
| 28 | + |
| 29 | +def add_superhost(results, field, num_parts): |
| 30 | + for r in results: |
| 31 | + if field not in r: |
| 32 | + continue |
| 33 | + d = super_domain(r[field], num_parts) |
| 34 | + r['superhost'] = d |
| 35 | + yield r |
| 36 | + |
38 | 37 |
|
39 | 38 | try: |
40 | 39 | keywords, options = splunk.Intersplunk.getKeywordsAndOptions() |
41 | 40 | field = options.get('field', 'hostname') |
| 41 | + num_parts = int(options.get('parts', 2)) |
42 | 42 |
|
43 | 43 | results,dummyresults,settings = splunk.Intersplunk.getOrganizedResults() |
44 | | - newresults = [] |
45 | | - for r in results: |
46 | | - if field not in r: |
47 | | - continue |
48 | | - for info in super_domains(r[field]): |
49 | | - info.update(r) |
50 | | - newresults.append(info) |
| 44 | + results = add_superhost(results, field, num_parts) |
| 45 | + |
51 | 46 | except: |
52 | 47 | import traceback |
53 | 48 | stack = traceback.format_exc() |
54 | | - newresults = splunk.Intersplunk.generateErrorResults("Error : Traceback: " + str(stack)) |
| 49 | + results = splunk.Intersplunk.generateErrorResults("Error : Traceback: " + str(stack)) |
55 | 50 |
|
56 | | -splunk.Intersplunk.outputResults( newresults ) |
| 51 | +splunk.Intersplunk.outputResults( results ) |
0 commit comments