#!/bin/python3
#
import argparse
import nagios
import subprocess

GPU_ERROR = 80
GPU_WARNING = 76.5
#
CPU_ERROR = 80
CPU_WARNING = 76.5
#

if __name__ == "__main__":
    cpu_temp = subprocess.check_output(['cat', '/sys/class/thermal/thermal_zone0/temp']).decode('utf-8')
    try:
        cpu_temp = int(cpu_temp) / 1000
    except ValueError:
        cpu_temp = None

    gpu_temp = subprocess.check_output(['vcgencmd', 'measure_temp']).decode('utf-8')
    gpu_temp = gpu_temp[gpu_temp.find('=')+1:gpu_temp.find("'")]
    try:
        gpu_temp = float(gpu_temp)
    except ValueError:
        gpu_temp = None
    #
    n = nagios.Nagios()
    #
    if (gpu_temp or 0) > GPU_ERROR:
        status = n.ERROR
    elif (cpu_temp or 0) > CPU_ERROR:
        status = n.ERROR
    elif (gpu_temp or 0) > GPU_WARNING:
        status = n.WARNING
    elif (cpu_temp or 0) > CPU_WARNING:
        status = n.WARNING
    elif gpu_temp is None or cpu_temp is None:
        status = n.UNKNOWN
    else:
        status = n.OK
    #
    n.exit(status, f"cpu-temperature={cpu_temp}°C - gpu-temperature: {gpu_temp}°C")