[Git][NTPsec/ntpsec][master] 2 commits: temp-log.py: more output clean up.

Gary E. Miller gitlab at mg.gitlab.com
Fri Jan 27 01:53:21 UTC 2017


Gary E. Miller pushed to branch master at NTPsec / ntpsec


Commits:
fbb2e429 by Gary E. Miller at 2017-01-26T17:25:57-08:00
temp-log.py: more output clean up.

- - - - -
33da600f by Gary E. Miller at 2017-01-26T17:52:42-08:00
temper-poll: add TEMPer support, and some tweaks

- - - - -


1 changed file:

- contrib/temp-log.py


Changes:

=====================================
contrib/temp-log.py
=====================================
--- a/contrib/temp-log.py
+++ b/contrib/temp-log.py
@@ -1,9 +1,8 @@
 #!/usr/bin/env python
 # temp-log.py: A script that will be run eventually as a daemon
-#              to log temperature of a system
+#              to log temperatures of a system
 # Usage:
-# temp-log.py [-h] [-V] [-l]
-# Writes logs to /var/log/ntpstats
+# temp-log.py [-h] [-q] [-v] [-V] [-l LOGFILE]
 # Requires root to run
 
 import argparse
@@ -19,8 +18,7 @@ try:
     import ntp.util
 except ImportError as e:
     sys.stderr.write("temp-log: can't find Python NTP modules "
-                     "-- check PYTHONPATH.\n")
-    sys.stderr.write("%s\n" % e)
+                     "-- check PYTHONPATH.\n%s\n" % e)
     sys.exit(1)
 
 
@@ -28,13 +26,12 @@ class CpuTemp:
     "Sensors on the CPU Core"
     no_sensors = False
 
-
     def __init__(self):
-        # Find the sensors binary
+        # check for sensors binary
         try:
-            subprocess.check_output(["which",
-                                     "sensors"],
-                                     stderr=subprocess.STDOUT,)
+            # sadly subprocess.check_output() is not in Python 2.6
+            subprocess.check_output(["which", "sensors"],
+                                    stderr=subprocess.STDOUT,)
         except:
             # skip sensors, many systems can not use sensors
             self.no_sensors = True
@@ -52,6 +49,7 @@ class CpuTemp:
         _index = 0
         _data = []
         # grab the needed output
+        # sadly subprocess.check_output() is not in Python 2.6
         _output = subprocess.check_output(['sensors', "-u"],
                                           universal_newlines=True).split('\n')
         for record in _output:
@@ -80,9 +78,10 @@ class SmartCtl:
 
     def get_data(self):
         "Collects the data and return the output as an array"
-        out = ""
+        data = []
         for _device in self._drives[:]:
             try:
+                # sadly subprocess.check_output() is not in Python 2.6
                 _output = subprocess.check_output(["smartctl", "-A",
                                                   _device],
                                                   universal_newlines=True
@@ -91,11 +90,58 @@ class SmartCtl:
                     if line.startswith('194 '):
                         now = int(time.time())
                         temp = line.split()[9]
-                        out += ('%d %s %s\n' % (now, _device, temp))
+                        data.append('%d %s %s' % (now, _device, temp))
             except:
                 # do not keep trying on failure
                 self._drives.remove(_device)
-        return out
+        return data
+
+
+class Temper:
+    """\
+    Reads 'temper-poll -c' for room temperature data.
+
+    Before you can use this class you must have a TEMPer USB thermometer
+    plugged in, and the temper-python package must be installed and configured.
+    See their documentation for that procedure.
+"""
+    no_temper = False
+
+    def __init__(self):
+        # check for temper-poll binary
+        try:
+            # sadly subprocess.check_output() is not in Python 2.6
+            subprocess.check_output(["which", "temper-poll"],
+                                    stderr=subprocess.STDOUT,)
+        except:
+            # skip temper, many systems do not have it
+            self.no_temper = True
+            if args.verbose:
+                sys.stderr.write("Unable to find temper-poll binary")
+
+    def get_data(self):
+        "Collects the data and return the output as an array"
+        if self.no_temper:
+            return None
+
+        data = []
+        _device = 'TEMPER0'
+        # grab the needed output
+        # sadly subprocess.check_output() is not in Python 2.6
+        try:
+            proc = subprocess.Popen(["temper-poll", "-c"],
+                                    stdout=subprocess.PIPE,
+                                    stderr=subprocess.STDOUT,
+                                    universal_newlines=True)
+            temp = proc.communicate()[0]
+            # make sure it is a temperature
+            temp = float(temp)
+            now = int(time.time())
+            data.append('%d %s %s' % (now, _device, temp))
+        except:
+            # bad data, ignore it
+            self.no_temper = True
+        return data
 
 
 class ZoneTemp:
@@ -185,31 +231,22 @@ def logData(log, data):
     "log the data"
     if data is None:
         return
-    if data is "":
-        return
 
     if type(data) in (tuple, list):
         for _item in data:
             log.info(_item)
     else:
-        log.info(data)
+        if data is not "":
+            log.info(data)
 
 
 def log_data():
     "Write all temperature readings to one file"
-    try:
-        # Create objects
-        cpu = CpuTemp()
-        zone = ZoneTemp()
-        hdd = SmartCtl()
-    except IOError as ioe:
-        if not args.quiet:
-            sys.stderr.write("Unable to run: " + str(ioe) + "\n")
-        sys.exit(1)
-    except Exception as e:
-        if not args.quiet:
-            sys.stderr.write("Unable to run: " + str(e) + "\n")
-        sys.exit(1)
+    # Create objects
+    cpu = CpuTemp()
+    zone = ZoneTemp()
+    hdd = SmartCtl()
+    temper = Temper()
 
     # Create the logger instance
     Logger = logging_setup(log, logging.INFO)
@@ -222,12 +259,17 @@ def log_data():
         logData(Logger, zone.get_data())
         logData(Logger, cpu.get_data())
         logData(Logger, hdd.get_data())
+        logData(Logger, temper.get_data())
         if args.once:
             sys.exit(0)
         time.sleep(args.wait[0])
 
 
 args = parser.parse_args()
+if os.getuid():
+    sys.stderr.write("You must be root!")
+    sys.exit(1)
+
 try:
     if args.logfile:
         log = args.logfile[0]



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/b47b8940a44d536cfb6a514f7e69429fb9ff94a0...33da600f8897baaf338dc2e98082aa3c841a4a27
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20170127/fea6402a/attachment.html>


More information about the vc mailing list