CI opt-test: Drop Python2 & Bash in Fedora latest.
[ntpsec.git] / contrib / cpu-temp-log
blob0fa5acf76fbf4496c72c48f69cbc62eb47494d56
1 #! /usr/bin/env python
2 # coding: utf-8
3 """\
4 Usage: cpu-temper-log
6 Reads 'sensors -u' for temperature data. Writes all temperatures found
7 to stdout. One temp per line, preceded by the unix UTC time in seconds,
8 and an identifier.
10 Before you can use this utility lm_sensors must be installed and
11 configured. See their documentation for that procedure.
13 How many temperatures, which temeratures and in which order will depend
14 on your lm_sensors configuration and your motherboard.
16 Sample log from an Supermicro Quad Core Xeon:
18 1471573103 LM0 37.000
19 1471573103 LM1 35.000
20 1471573103 LM2 31.000
21 1471573103 LM3 31.000
22 1471573103 LM4 30.000
23 1471573104 LM0 37.000
25 Field 1: unix time in seconds since the star of the epoch
26 Field 2: Log source (LM)
27 Field 3: temperature in degrees C
29 Sample crontab usage:
31 # take and log cpu temp every 5 mins
32 */5 * * * * /usr/local/sbin/cpu-temp-log >> /var/log/ntpstats/temps
34 This file may only be useful as a template. The way to read your system
35 temperatures will be hardware specific.
37 """
39 from __future__ import print_function, division
41 import sys
42 import re
43 import time
44 import subprocess
46 try:
47 # sadly subprocess.check_output() is not in Python 2.6
48 proc = subprocess.Popen(["sensors", "-u"],
49 stdout=subprocess.PIPE,
50 stderr=subprocess.STDOUT,
51 universal_newlines=True)
52 output = proc.communicate()[0]
53 except:
54 print("Unable to run 'sensors -u'")
55 exit(0)
57 lines = output.split('\n')
59 # this regex matches temperature output lines from 'sensors -u'
60 pat = re.compile(r'^\s+temp\d+_input:\s+([\d\.]+).*$')
62 now = int(time.time())
64 line = ''
65 index = 0
66 for line in lines:
67 match = pat.match(line)
68 if match and match.group(1):
69 temp = match.group(1)
70 sys.stdout.write('%d LM%d %s\n' % (now, index, temp))
71 index += 1