File metrics-nosend.py deleted (index c48f252..0000000) |
1 |
|
#!/usr/bin/python3 |
|
2 |
|
|
|
3 |
|
|
|
4 |
|
import time |
|
5 |
|
import os |
|
6 |
|
import socket |
|
7 |
|
import json |
|
8 |
|
import importlib |
|
9 |
|
import sys |
|
10 |
|
|
|
11 |
|
Path=sys.path[0] |
|
12 |
|
ConfigFileLocation=Path+"/conf/metrics.conf" |
|
13 |
|
MetricsLocation=Path+"/metrics.d" |
|
14 |
|
|
|
15 |
|
sys.path.append(MetricsLocation) |
|
16 |
|
|
|
17 |
|
## Read from Configfile |
|
18 |
|
with open(ConfigFileLocation,'r') as ConfigFile: |
|
19 |
|
Configuration = json.load(ConfigFile) |
|
20 |
|
|
|
21 |
|
GraphHost=Configuration["GraphHost"] |
|
22 |
|
GraphPort=Configuration["GraphPort"] |
|
23 |
|
LogFile=Configuration["LogFile"] |
|
24 |
|
|
|
25 |
|
### / End Read Configfile |
|
26 |
|
|
|
27 |
|
def WriteLog(message): |
|
28 |
|
Log = open(LogFile,'a') |
|
29 |
|
message=str(time.asctime())+' '+message |
|
30 |
|
Log.write(message) |
|
31 |
|
Log.close() |
|
32 |
|
|
|
33 |
|
def graphSend(content): |
|
34 |
|
s = socket.socket() |
|
35 |
|
s.connect((GraphHost, GraphPort)) |
|
36 |
|
s.sendall(content.encode()) |
|
37 |
|
s.close() |
|
38 |
|
|
|
39 |
|
try: |
|
40 |
|
import LinuxNetwork |
|
41 |
|
import LinuxDisk |
|
42 |
|
import LinuxCPU |
|
43 |
|
import LinuxMem |
|
44 |
|
except: |
|
45 |
|
print ("SNAFU") |
|
46 |
|
sys.exit(2) |
|
47 |
|
|
|
48 |
|
|
|
49 |
|
|
|
50 |
|
while True: |
|
51 |
|
toSend="" |
|
52 |
|
toSend+=LinuxNetwork.network() |
|
53 |
|
toSend+=LinuxDisk.disk() |
|
54 |
|
toSend+=LinuxCPU.cpu() |
|
55 |
|
toSend+=LinuxMem.mem() |
|
56 |
|
WriteLog ("START: Sending metrics --> ") |
|
57 |
|
try: |
|
58 |
|
#graphSend(toSend) |
|
59 |
|
print(toSend) |
|
60 |
|
WriteLog (" OK\n") |
|
61 |
|
except Exception as err: |
|
62 |
|
WriteLog (" ERROR: %s\n" % (err)) |
|
63 |
|
pass |
|
64 |
|
time.sleep(9) |
|
File metrics.py changed (mode: 100755) (index df2978c..7bba099) |
... |
... |
import json |
8 |
8 |
import importlib |
import importlib |
9 |
9 |
import sys |
import sys |
10 |
10 |
|
|
|
11 |
|
import logging |
|
12 |
|
from logging.handlers import RotatingFileHandler |
|
13 |
|
|
11 |
14 |
Path=sys.path[0] |
Path=sys.path[0] |
12 |
15 |
ConfigFileLocation=Path+"/conf/metrics.conf" |
ConfigFileLocation=Path+"/conf/metrics.conf" |
13 |
16 |
MetricsLocation=Path+"/metrics.d" |
MetricsLocation=Path+"/metrics.d" |
|
... |
... |
sys.path.append(MetricsLocation) |
23 |
26 |
# - dynamic import based on config file (list of modules would be faaaantastic) -> see importlib |
# - dynamic import based on config file (list of modules would be faaaantastic) -> see importlib |
24 |
27 |
|
|
25 |
28 |
|
|
26 |
|
|
|
27 |
|
|
|
28 |
29 |
## Read from Configfile |
## Read from Configfile |
29 |
30 |
with open(ConfigFileLocation,'r') as ConfigFile: |
with open(ConfigFileLocation,'r') as ConfigFile: |
30 |
31 |
Configuration = json.load(ConfigFile) |
Configuration = json.load(ConfigFile) |
|
... |
... |
with open(ConfigFileLocation,'r') as ConfigFile: |
32 |
33 |
GraphHost=Configuration["GraphHost"] |
GraphHost=Configuration["GraphHost"] |
33 |
34 |
GraphPort=Configuration["GraphPort"] |
GraphPort=Configuration["GraphPort"] |
34 |
35 |
LogFile=Configuration["LogFile"] |
LogFile=Configuration["LogFile"] |
|
36 |
|
LogLevel=Configuration["LogLevel"] |
|
37 |
|
ActiveModules=Configuration["ActiveModules"] |
|
38 |
|
|
|
39 |
|
ConfigFile.close() |
35 |
40 |
|
|
36 |
41 |
### / End Read Configfile |
### / End Read Configfile |
37 |
42 |
|
|
38 |
|
def WriteLog(message): |
|
39 |
|
Log = open(LogFile,'a') |
|
40 |
|
message=str(time.asctime())+' '+message |
|
41 |
|
Log.write(message) |
|
42 |
|
Log.close() |
|
|
43 |
|
|
|
44 |
|
### Logging ### |
|
45 |
|
|
|
46 |
|
logger=logging.getLogger() |
|
47 |
|
logger.setLevel(LogLevel) |
|
48 |
|
|
|
49 |
|
fileHandler = logging.handlers.RotatingFileHandler(LogFile,'a',maxBytes=1024 * 1024 * 100,backupCount=3) |
|
50 |
|
fileHandler.setLevel(LogLevel) |
|
51 |
|
|
|
52 |
|
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') |
|
53 |
|
fileHandler.setFormatter(formatter) |
|
54 |
|
|
|
55 |
|
logger.addHandler(fileHandler) |
|
56 |
|
logger.info("--- START ---") |
|
57 |
|
|
|
58 |
|
|
|
59 |
|
### / Logging ### |
|
60 |
|
|
43 |
61 |
|
|
44 |
62 |
def graphSend(content): |
def graphSend(content): |
45 |
63 |
s = socket.socket() |
s = socket.socket() |
|
64 |
|
logger.debug("Sending to: %s : %s",GraphHost,GraphPort) |
46 |
65 |
s.connect((GraphHost, GraphPort)) |
s.connect((GraphHost, GraphPort)) |
47 |
66 |
s.sendall(content.encode()) |
s.sendall(content.encode()) |
48 |
67 |
s.close() |
s.close() |
|
... |
... |
if os.fork(): |
52 |
71 |
sys.exit() |
sys.exit() |
53 |
72 |
|
|
54 |
73 |
try: |
try: |
55 |
|
import LinuxNetwork |
|
56 |
|
import LinuxDisk |
|
57 |
|
import LinuxCPU |
|
58 |
|
import LinuxMem |
|
|
74 |
|
import Linux |
59 |
75 |
except: |
except: |
60 |
76 |
print ("SNAFU") |
print ("SNAFU") |
61 |
77 |
sys.exit(2) |
sys.exit(2) |
|
... |
... |
except: |
63 |
79 |
|
|
64 |
80 |
|
|
65 |
81 |
while True: |
while True: |
|
82 |
|
|
|
83 |
|
with open(ConfigFileLocation,'r') as ConfigFile: |
|
84 |
|
Configuration = json.load(ConfigFile) |
|
85 |
|
LogLevel=Configuration["LogLevel"] |
|
86 |
|
GraphHost=Configuration["GraphHost"] |
|
87 |
|
GraphPort=Configuration["GraphPort"] |
|
88 |
|
LogFile=Configuration["LogFile"] |
|
89 |
|
LogLevel=Configuration["LogLevel"] |
|
90 |
|
ActiveModules=Configuration["ActiveModules"] |
|
91 |
|
|
|
92 |
|
logger.setLevel(LogLevel) |
|
93 |
|
ConfigFile.close() |
|
94 |
|
|
66 |
95 |
toSend="" |
toSend="" |
67 |
|
toSend+=LinuxNetwork.network() |
|
68 |
|
toSend+=LinuxDisk.disk() |
|
69 |
|
toSend+=LinuxCPU.cpu() |
|
70 |
|
toSend+=LinuxMem.mem() |
|
71 |
|
WriteLog ("START: Sending metrics --> ") |
|
|
96 |
|
|
|
97 |
|
toSend+=Linux.network() |
|
98 |
|
toSend+=Linux.disk() |
|
99 |
|
toSend+=Linux.cpu() |
|
100 |
|
toSend+=Linux.mem() |
|
101 |
|
logger.info("Sending metrics") |
|
102 |
|
logger.debug(toSend) |
72 |
103 |
try: |
try: |
73 |
104 |
graphSend(toSend) |
graphSend(toSend) |
74 |
|
WriteLog (" OK\n") |
|
|
105 |
|
logger.info("OK") |
75 |
106 |
except Exception as err: |
except Exception as err: |
76 |
|
WriteLog (" ERROR: %s\n" % (err)) |
|
|
107 |
|
logger.error(err) |
77 |
108 |
pass |
pass |
78 |
109 |
time.sleep(9) |
time.sleep(9) |