/usr/sbin/ntplogtemp is in ntpsec-ntpviz 1.1.0+dfsg1-1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 | #!/usr/bin/python3
# -*- coding: utf-8 -*-
"""\
usage: ntplogtemp [-h] [-l LOGFILE] [-o] [-q] [-v] [-w WAIT] [-V]
Program to log system temperatures
optional arguments:
-h, --help show this help message and exit
-l LOGFILE, --logfile LOGFILE
append log data to LOGFILE instead of stdout
-o, --once Run the output once and exit
-q, --quiet be quite
-v, --verbose be verbose
-w WAIT, --wait WAIT Set delay time in seconds, default is 60
-V, --version show program's version number and exit
See the manual page for details.
"""
from __future__ import print_function, division
import argparse
import glob
import logging
import logging.handlers
import os
import re
import subprocess
import sys
import time
try:
import ntp.util
except ImportError as e:
sys.stderr.write(
"ntplogtemp: can't find Python NTP library -- check PYTHONPATH.\n")
sys.stderr.write("%s\n" % e)
sys.exit(1)
def run_binary(cmd):
"""\
Run a binary
Return output if good, None if bad
"""
try:
# sadly subprocess.check_output() is not in Python 2.6
# so use Popen()
# this throws an exception if not found
proc = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True)
output = proc.communicate()[0].split("\n")
if proc.returncode:
# non-zero return code, fail
return None
except:
if args.verbose:
sys.stderr.write("Unable to run %s binary\n" % cmd[0])
# throws exception
return None
return output
class CpuTemp:
"Sensors on the CPU Core"
has_sensors = False
def __init__(self):
# check for sensors binary
ret = run_binary(["sensors", "-h"])
if ret is not None:
self.has_sensors = True
# pattern that matches the string that has the cpu temp
self._pattern = re.compile('^\s+temp\d+_input:\s+([\d\.]+).*$')
def get_data(self):
"Collects the data and return the output as an array"
if not self.has_sensors:
return None
_index = 0
_data = []
# grab the needed output
output = run_binary(["sensors", "-u"])
if output is not None:
for record in output:
match = self._pattern.match(record)
if match and match.group(1):
_now = int(time.time())
_cpu_temprature = match.group(1)
_data.append('%d LM%s %s' % (_now, _index, _cpu_temprature))
_index += 1
else:
self.has_sensors = False
if args.verbose:
sys.stderr.write("No sensors returned temperatures. Have you run sensors-detect?")
return _data
class SmartCtl:
"Sensor on the Hard Drive"
_drives = []
has_smartctl = False
def __init__(self):
ret = run_binary(["smartctl", "-h"])
if ret is not None:
self.has_smartctl = True
if self.has_smartctl:
# Which drive to watch
for child in glob.glob('/dev/sd?'):
self._drives.append(child)
self._drives = sorted(self._drives)
def get_data(self):
"Collects the data and return the output as an array"
if not self.has_smartctl:
return None
data = []
for _device in self._drives[:]:
output = run_binary(["smartctl", "-A", _device])
if output is None:
# do not keep trying on failure
self._drives.remove(_device)
else:
for line in output:
if line.startswith('194 '):
now = int(time.time())
temp = line.split()[9]
data.append('%d %s %s' % (now, _device, temp))
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.
"""
has_temper = False
def __init__(self):
# check for sensors binary
ret = run_binary(["temper-poll", "-h"])
if ret is not None:
self.has_temper = True
def get_data(self):
"Collects the data and return the output as an array"
if not self.has_temper:
return None
data = []
_device = 'TEMPER0'
# only one device can read the TEMPer at a time
# collisions will happen, so retry a few times
for attempt in range(0, 3):
# grab the needed output
output = run_binary(["temper-poll", "-c"])
try:
# make sure it is a temperature
temp = float(output[0])
now = int(time.time())
data.append('%d %s %s' % (now, _device, temp))
break
except:
# bad data, ignore it, for a bit
if args.verbose:
sys.stderr.write("TEMPer-poll failed\n")
if 0 == len(data):
self.has_temper = False
return data
class ZoneTemp:
"Zone sensors"
def __init__(self):
base_dir = '/sys/class/thermal/thermal_zone?/'
self.zones = []
for child in glob.glob(base_dir):
self.zones.append(child)
def get_data(self):
"Collects the data and return the output as an array"
_zone = 0
_data = []
for zone in self.zones:
_zone_data = open(os.path.join(zone, 'temp'))
for line in _zone_data:
temp = float(line) / 1000
_now = int(time.time())
_data.append('%d ZONE%s %s' % (_now, _zone, temp))
_zone += 1
_zone_data.close()
return _data
# Work with argvars
parser = argparse.ArgumentParser(description="Temperature sensor daemon",
epilog="""See the manual page for details.""")
parser.add_argument('-l', '--logfile',
dest='logfile',
help="append log data to LOGFILE instead of stdout",
nargs=1)
parser.add_argument('-o', '--once',
dest='once',
help="Run the output once and exit",
action='store_true')
parser.add_argument('-q', '--quiet',
action="store_true",
dest='quiet',
help="be quite")
parser.add_argument('-v', '--verbose',
action="store_true",
dest='verbose',
help="be verbose")
parser.add_argument('-w', '--wait',
default=[60],
dest='wait',
help="Set delay time in seconds, default is 60",
nargs=1,
type=int)
parser.add_argument('-V', '--version',
action="version",
version="ntplogtemp %s" % ntp.util.stdversion())
args = parser.parse_args()
def logging_setup():
"Create logging object"
logFormat = logging.Formatter('%(message)s')
# Create logger for cpuTemp
tempLogger = logging.getLogger()
tempLogger.setLevel(logging.INFO)
# Create file handler
if args.logfile:
# log to logfile
file = logging.handlers.TimedRotatingFileHandler(
args.logfile[0],
when='midnight',
interval=1)
else:
# log to stdout
file = logging.StreamHandler(sys.stdout)
file.setLevel(logging.INFO)
# Create the formatter and add it to the handler
file.setFormatter(logFormat)
# Add the handler to the logger
tempLogger.addHandler(file)
return tempLogger
def logData(log, data):
"log the data"
if data is not None:
for _item in data:
log.info(_item)
def log_data():
"Write all temperature readings to one file"
# Create objects
cpu = CpuTemp()
zone = ZoneTemp()
hdd = SmartCtl()
temper = Temper()
# Create the logger instance
Logger = logging_setup()
# Create data layout
logData(Logger, ["# time, sensor, value"])
# Write data to their respective logs
while True:
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:
log_data()
except (KeyboardInterrupt, SystemExit):
print("") # be nice to bash
sys.exit(0)
|