#!/usr/bin/env python
# coding: utf-8
"""\
Usage: zone-temper-log

Reads /sys/class/thermal/thermal_zone*/temp to find the CPU temperature.
Writes all temperatures found to stdout on one line, preceded by the
Unix UTC time in seconds.


Sample log:

1471582083 ZONE0 56.92
1471582084 ZONE0 57.458
1471582085 ZONE0 56.92
1471582086 ZONE0 56.92


Field 1: Unix UTC time in seconds
Field 2: Log source (ZONE)
Field 3: CPU Temperature

ntpviz will read all file temps* by default. Do not step on
temps used by temper-temp-log.

Sample crontab usage:

# take and log CPU temp every 5 minutes
*/5 * * * * /usr/local/sbin/zone-temp-log >> /var/log/ntpstats/tempsz
"""

from __future__ import print_function

import time

now = int(time.time())

zone = 0
while True:
    try:
        f = open('/sys/class/thermal/thermal_zone{0}/temp'.format(zone), 'r')
    except IOError:
        break
    for line in f:
        temp = float(line) / 1000
        print('{0} ZONE{1} {2}'.format(now, zone, temp))
    f.close()
    zone = zone + 1
