-
Notifications
You must be signed in to change notification settings - Fork 0
/
munin-node.py
208 lines (181 loc) · 6.71 KB
/
munin-node.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import socket
import sys
import re
import os
from _thread import *
from subprocess import Popen, PIPE
import ipaddress
VERSION = ''
ENCODING = 'utf-8'
LINEBREAK = '\n'
PLUGINPATH = os.getcwd() + "\\plugins"
CONFIGS = {}
class Node:
def __init__(self):
self.readConfig()
self.debug = int(self.getConfigValue("debug",0)) == 1
if (self.debug):
print("running in debug mode..")
self.startServer()
def startServer(self):
s = None
try:
host = self.getConfigValue("host","")
if (host == "*"):
host = ""
port = int(self.getConfigValue("port","4949"))
if (host == "::1"):
host = ""
s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
else:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.settimeout(int(self.getConfigValue("timeout",60)))
s.bind((host, port))
except socket.error as msg:
print ("failed!" + str(msg.errno))
sys.exit()
s.listen(10)
while 1:
try:
conn, addr = s.accept()
start_new_thread(self.clientthread ,(conn,))
except socket.timeout as e:
pass
def readConfig(self):
regex = re.compile(r"^([^\s^#]+)\s{1,}([^\s]+)", re.MULTILINE)
configFileContent = ""
with open('munin-node.conf', 'r') as myfile:
configFileContent=myfile.read()
for match in regex.finditer(configFileContent):
config = match.group(1)
value = match.group(2)
if (config in CONFIGS):
oldValue = CONFIGS[config]
if (isinstance(oldValue, list)):
CONFIGS[config].append(value)
else:
CONFIGS[config] = [oldValue, value]
else:
CONFIGS[config] = value
def getConfigValue(self,value, defaultValue):
if (value in CONFIGS):
return CONFIGS[value]
else:
return defaultValue
def output(self,what):
return what.encode(ENCODING)
def hello(self):
hostname = self.getConfigValue("host_name",socket.getfqdn())
return "# munin node at " + hostname + LINEBREAK
def nodes(self):
return self.getConfigValue("host_name",socket.getfqdn()) + LINEBREAK + "." + LINEBREAK
def version(self):
with open(os.getcwd() + '\\.git\\refs\\heads\\master', 'r') as version:
VERSION=version.read()
return "munins node on " + self.getConfigValue("host_name",socket.getfqdn()) + " version: " + VERSION + LINEBREAK
def cap(self):
return "cap multigraph dirtyconfig" + LINEBREAK
def plugins(self):
files = [f for f in os.listdir(PLUGINPATH) if os.path.isfile(os.path.join(PLUGINPATH, f))]
result = ""
for f in files:
if (f.endswith(".py")):
result += f.replace(".py","") + " "
return result.strip() + LINEBREAK
def unknown(self):
return "# Unknown command. Try cap, list, nodes, config, fetch, version or quit" + LINEBREAK
def runPlugin(self,name):
return self.callPluginMethod(name,"fetch")
def configPlugin(self,name):
return self.callPluginMethod(name,"config")
def callPluginMethod(self,name, method):
if (name.find(".") == -1 and os.path.isfile(PLUGINPATH + "\\" + name + ".py")):
module = __import__(name.replace("\r",""))
class_ = getattr(module, name.replace("\r",""))
instance = class_()
return getattr(instance, method)() + LINEBREAK + "." + LINEBREAK
else:
return "# Unknown service" + LINEBREAK + "." + LINEBREAK
def isPeerAllowed(self, peer):
allowedPeers = self.getConfigValue("allow",[])
allowedPeersLength = len(allowedPeers)
allowedSubnets = self.getConfigValue("cidr_allow",[])
deniedSubnets = self.getConfigValue("cidr_deny",[])
matchesAllowedSubnets = self.matchesSubnet(allowedSubnets, peer)
matchesDeniedSubnets = self.matchesSubnet(deniedSubnets, peer)
# check if the peer (or a list of peers is)
if (allowedPeersLength > 0):
if (isinstance(allowedPeers, list)):
for i, peerToCheck in enumerate(allowedPeers):
result = re.match(peerToCheck,peer)
if (result != None):
return True
break
else:
result = re.match(allowedPeers,peer)
if (len(allowedSubnets) > 0):
return matchesAllowedSubnets == True and matchesDeniedSubnets == False
if (len(deniedSubnets) > 0):
return matchesDeniedSubnets
return False
def matchesSubnet(self,subnet, peer):
matches = False
if (isinstance(subnet, list)):
if (len(subnet) > 0):
for i, subnet in enumerate(subnet):
check = ipaddress.ip_address(peer) in ipaddress.ip_network(subnet)
if (check):
matches = True
break
else:
matches = ipaddress.ip_address(peer) in ipaddress.ip_network(subnet)
return matches
def clientthread(self, conn):
sys.path.append(PLUGINPATH)
if (self.debug):
print("connection attempt by " + conn.getpeername()[0])
if (self.isPeerAllowed(conn.getpeername()[0]) == False):
if (self.debug):
print("connection closed for " + conn.getpeername()[0])
conn.close()
else:
if (self.debug):
print("connection allowed for " + conn.getpeername()[0])
conn.send(self.output(self.hello()))
while True:
#Receiving from client
data = conn.recv(4096)
command = data.decode(ENCODING)
regex = re.compile(r"[^\s]+(\s[^\s]+)?")
matches = regex.match(command)
if (matches == None):
break
conn.close()
else:
extractedCommand = matches.group()
if (self.debug):
print("#"+ extractedCommand + "#")
if (extractedCommand == "nodes"):
conn.send(self.output(self.nodes()))
elif (extractedCommand == "help"):
conn.send(self.output(self.unknown()))
elif (extractedCommand == "version"):
conn.send(self.output(self.version()))
elif (extractedCommand == "cap"):
conn.send(self.output(self.cap()))
elif (extractedCommand == "list" or extractedCommand.startswith("list ")):
conn.send(self.output(self.plugins()))
elif (extractedCommand == "quit"):
conn.close()
break
else:
if (extractedCommand.startswith("fetch ")):
parts = extractedCommand.split(" ")
conn.send(self.output(self.runPlugin(parts[1])))
elif (extractedCommand.startswith("config ")):
parts = extractedCommand.split(" ")
conn.send(self.output(self.configPlugin(parts[1])))
else:
conn.send(self.output(self.unknown()))
n = Node()