mihai.leontescu / metrics (public) (License: GPLv3) (since 2018-07-12) (hash sha1)
Metrics for Linux to feed to Graphite in python
Disclaimer: I am not a programmer, I know some things can be done better. I choose to do them so, so I am able to understand them 80 years later :)
List of commits:
Subject Hash Author Date (UTC)
small improvements e3d54651bfc60828b98038ad8de298bc6d6bfd5b root 2019-06-21 12:54:18
the module too 28931b7c63965793d698335cd32f2cd56c3eb7d4 root 2019-06-08 22:03:10
Memory 89a64ee592f8808156bfda9639be723a1f2158ce root 2019-06-08 22:02:43
pam pam a49f891505bf8fb0ef5feaf5bbb9e32056044608 root 2019-06-07 21:57:53
oh hell.. 6e51570fbf7b9e608a39fe889878730b2da5fdc3 mihai.leontescu 2018-07-12 19:35:22
out with the logfiles :) 7ab23b68dd2baf3f0ffd0b143c7d81cb4050b48c mihai.leontescu 2018-07-12 19:34:34
graphana examples 63d6edb628092720782795a1385349fe14fa7e58 mihai.leontescu 2018-07-12 19:34:02
initial commit pre-alpha d4f64fe23a814b563d1df7fea142daae8aaa0a75 mihai.leontescu 2018-07-12 19:29:49
Commit e3d54651bfc60828b98038ad8de298bc6d6bfd5b - small improvements
Author: root
Author date (UTC): 2019-06-21 12:54
Committer name: root
Committer date (UTC): 2019-06-21 12:54
Parent(s): 28931b7c63965793d698335cd32f2cd56c3eb7d4
Signing key:
Tree: 0b3dc9354f4a480fabb22f92ac688bd14f5d64c8
File Lines added Lines deleted
conf/metrics.conf 2 1
metrics-nosend.py 0 64
metrics.py 49 18
File conf/metrics.conf changed (mode: 100755) (index b59303c..01f2dad)
1 1 { {
2 2 "LogFile": "/opt/metrics/logs/metrics.log", "LogFile": "/opt/metrics/logs/metrics.log",
3 "LogLevel" : "INFO",
3 4 "GraphHost" : "10.49.91.5", "GraphHost" : "10.49.91.5",
4 5 "GraphPort" : 2003, "GraphPort" : 2003,
5 "ActiveModules" : ["LinuxNetwork","LinuxDisk","LinuxCPU","LinuxMem"]
6 "ActiveModules" : ["Linux"]
6 7 } }
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)
Hints:
Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://rocketgit.com/user/mihai.leontescu/metrics

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@ssh.rocketgit.com/user/mihai.leontescu/metrics

Clone this repository using git:
git clone git://git.rocketgit.com/user/mihai.leontescu/metrics

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a merge request:
... clone the repository ...
... make some changes and some commits ...
git push origin main