forked from filipealmeida/probespawner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
netstatntc.py
73 lines (64 loc) · 2.23 KB
/
netstatntc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# Any copyright is dedicated to the Public Domain.
# http://creativecommons.org/publicdomain/zero/1.0/
from dummyprobe import DummyProbe
import subprocess
import re
import platform
import org.joda.time.DateTime as DateTime
import logging
logger = logging.getLogger(__name__)
#qActive Internet connections (w/o servers)
#Proto Recv-Q Send-Q Local Address Foreign Address State
class LinuxNetstatNTC(DummyProbe):
def initialize(self):
self.regexHeader = re.compile(r'^Proto[\t ]+Recv-Q[\t ]+Send-Q[\t ]+Local Address[\t ]+Foreign Address[\t ]+State')
self.regexActiveHeader = re.compile(r'^Active (\w+)')
self.regexBoundary = re.compile(r'[\t ]+')
self.regexIpSplit = re.compile(r'(.*):(.+)$')
self.fields = ['protocol', 'receive-q', 'send-q', 'local', 'foreign', 'state', 'user', 'inode']
if (self.getInputProperty("command") != None):
self.cmd = self.getInputProperty("command")
else:
self.cmd = "netstat -ntc"
def tick(self):
stream = subprocess.Popen(self.cmd, shell=True, bufsize=0, stdout=subprocess.PIPE)
dt = str(DateTime())
ps = 0 #parser state
fields = []
state = 0
out = {}
nowStr = self.getCycleProperty("startdt")
for line in stream.stdout:
line = line.rstrip()
matchActiveHeader = re.search(self.regexActiveHeader, line)
if (matchActiveHeader):
out = {}
if (matchActiveHeader.group(1) == 'Internet'):
state = 5
else:
state = 0
elif (state == 5):
matchHeader = re.search(self.regexHeader, line)
if (matchHeader):
state = 10
out["@timestamp"] = nowStr
out["host"] = platform.node()
out["class"] = "tcpconnections"
elif (state == 10):
idx = 0
values = re.split(self.regexBoundary, line)
for value in values:
field = self.fields[idx]
if ((field == 'receive-q') or (field == 'send-q')):
values[idx] = float(value)
elif (field == 'local'):
pair = re.search(self.regexIpSplit, value)
out['localip'] = pair.group(1)
out['localport'] = float(pair.group(2))
elif (field == 'foreign'):
pair = re.search(self.regexIpSplit, value)
out['foreignip'] = pair.group(1)
out['foreignport'] = float(pair.group(2))
out[field] = values[idx]
idx+=1
self.processData(out)