forked from FreiFunkMuenster/node-stats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GraphiteManager.py
63 lines (52 loc) · 2.36 KB
/
GraphiteManager.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
#
# (c) 2015 dray <[email protected]>
#
# This script is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License or any later version.
#
# This script is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY. See the
# GNU General Public License for more details.
#
# For a copy of the GNU General Public License
# see <http://www.gnu.org/licenses/>.
#
import socket
import time
class GraphiteManager:
def __init__(self,server,port,domain):
self.server = server
self.port = port
self.domain = domain
self.message = ""
def prepareMessage(self, data):
for i in data['nodes']:
self.__addHieraDictMessage__(data['nodes'][i], "node.%s" % i)
self.__addDictMessage__("nodes."+self.domain+".firmware.%s.count",data['firmwarecount'])
self.__addDictMessage__("nodes."+self.domain+".branch.%s.count", data['branchcount'])
self.__addDictMessage__("nodes."+self.domain+".hardware.%s.count", data['hardwarecount'])
self.__addSingleMessage__("nodes."+self.domain+".autoupdate.count",data['autoupdate'])
self.__addSingleMessage__("nodes."+self.domain+".location.count",data['locationcount'])
self.__addSingleMessage__("nodes."+self.domain+".total.count",data['nodecount'])
self.__addSingleMessage__("nodes."+self.domain+".totalclient.count",data['totalclients'])
self.__addSingleMessage__("nodes."+self.domain+"clients_per_node",data['totalclients']/float(data['nodecount']))
def send(self):
sock = socket.socket()
sock.connect((self.server, int(self.port)))
sock.sendall(self.message.encode())
sock.close()
pass
def printout(self):
print(self.message)
def __addSingleMessage__(self,key,value):
self.message += "%s %s %d\n" %(key,value, int(time.time()))
def __addDictMessage__(self,key,dict):
for i in dict:
self.__addSingleMessage__(key % i, dict[i])
def __addHieraDictMessage__(self,data, path =''):
for k, v in data.iteritems():
if isinstance(v, dict):
self.__addHieraDictMessage__(v, path + '.' + k if len(path) > 0 else k)
else:
self.__addSingleMessage__(path + '.' + k, v)