[Git][NTPsec/ntpsec][master] Moved classes to one file
deamoneye
gitlab at mg.gitlab.com
Thu Jan 12 00:05:21 UTC 2017
deamoneye pushed to branch master at NTPsec / ntpsec
Commits:
22525ac0 by Keane Wolter at 2017-01-11T19:04:52-05:00
Moved classes to one file
- - - - -
1 changed file:
- contrib/temp-log.py
Changes:
=====================================
contrib/temp-log.py
=====================================
--- a/contrib/temp-log.py
+++ b/contrib/temp-log.py
@@ -5,15 +5,90 @@
# Writes logs to /var/log/ntpstats
# Requires root to run
-import temp_classes
-import time, os, logging, sys, logging.handlers
+import sys, re, time, subprocess, os, logging, logging.handlers
+
+class CpuTemp:
+ "Sensors on the CPU Core"
+ def __init__(self):
+ # pattern that matches the string that has the cpu temp
+ self._pattern = re.compile('^\s+temp\d+_input:\s+([\d\.]+).*$')
+ # Find the sensors binary and stores the path
+ self._sensors_path = subprocess.check_output(["which", "sensors"], universal_newlines=False).replace('\n', '')
+ if self._sensors_path == None:
+ raise Exception("Unable to find sensors binary")
+
+ def get_data(self):
+ "Collects the data and return the output as an array"
+ _index = 0
+ _data = []
+ self._record_temp()
+ # grab the needed output and format it to log
+ for record in self.sensors_output:
+ match = self._pattern.match(record)
+ if match and match.group(1):
+ _now = int(time.time())
+ _cpu_temprature = match.group(1)
+ _data.append('{} {} {}'.format(_now, _index, _cpu_temprature))
+ _index += 1
+
+ return _data
+
+ def _record_temp(self):
+ "Call the external command 'sensors -u' and get the data"
+ # grab the data from the "sensors -u" command
+ output = subprocess.check_output([self._sensors_path, "-u"], universal_newlines=True)
+ self.sensors_output = output.split('\n')
+
+class SmartCtl:
+ "Sensor on the Hard Drive"
+ def __init__(self, device):
+ if os.getuid() != 0:
+ raise IOError("You must be root!")
+ # Which drive to watch
+ self._device = device
+ # this regex matches temperature output lines from smartctl -a
+ self._pat = re.compile('194 Temperature_Celsius\s+\S+\s+(\d+)\s+')
+
+ def get_data(self):
+ "Collects the data and return the output as an array"
+ _output = subprocess.check_output(["smartctl", "-a", self._device], universal_newlines=True)
+ _lines = _output.split('\n')
+ for line in _lines:
+ match = self._pat.match(line)
+ now = int(time.time())
+ if match and match.group(1):
+ temp = match.group(1)
+ return ('%d SMART %s' % (now, temp))
+
+class ZoneTemp:
+ "Sensors on the CPU Zones"
+ def __init__(self):
+ self._base_dir = '/sys/class/thermal/'
+ self.zone_directories = []
+ for child in os.listdir(self._base_dir):
+ if re.compile('thermal_zone').match(child):
+ self.zone_directories.append(child)
+
+ def get_data(self):
+ "Collects the data and return the output as an array"
+ _zone = 0
+ _data = []
+ for zone in self.zone_directories:
+ _zone_data = open(os.path.join(os.path.join(self._base_dir, zone), 'temp'))
+ for line in _zone_data:
+ temp = float(line) / 1000
+ _now = int(time.time())
+ _data.append('{} {} {}'.format(_now, _zone, temp))
+ _zone = _zone+1
+ _zone_data.close()
+ return _data
# Global vars
version = 1.0
# Create objects
-cpu = temp_classes.CpuTemp()
-zone = temp_classes.ZoneTemp()
-hdd = temp_classes.SmartCtl('/dev/sda')
+cpu = CpuTemp()
+zone = ZoneTemp()
+hdd = SmartCtl('/dev/sda')
def logging_setup(fileName, levelName, logLevel):
"Create logging object with midnight rotation"
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/22525ac0bb81ee8e087c489a854d5b498ba10a0c
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20170112/410d8954/attachment.html>
More information about the vc
mailing list