#!/usr/bin/env python3

from re import findall
from subprocess import check_output
from sys import exit

ITERATIONS = 10

try:
    top_output = None

    top_output = check_output(rf"top -b -n{ITERATIONS} -d1 | grep -i '^%cpu'", shell=True).decode('UTF-8')

    cpu_usage = {}
    for value, identifier in findall(r'([0-9\.\,]{3,5}) ([a-z]{2})', top_output):
        if identifier not in cpu_usage:
            cpu_usage[identifier] = 0.0
        cpu_usage[identifier] += float(value.replace(',', '.'))

    output = []
    for identifier, value_added in cpu_usage.items():
        value = value_added / ITERATIONS
        output.append(f"{value:.2f} {identifier}")
        cpu_usage[identifier] = value

    print(f"Average over {ITERATIONS} seconds: " + ", ".join(output))

    warn = set()
    crit = set()

    # steal
    if cpu_usage['st'] > 10:
        crit.add('CPU steal is {}% (>10%)'.format(cpu_usage['st']))
    elif cpu_usage['st'] > 5:
        warn.add('CPU steal is {}% (>5%)'.format(cpu_usage['st']))

    # iowait
    if cpu_usage['wa'] > 60:
        crit.add('IOwait is {}% (>60%)'.format(cpu_usage['wa']))
    elif cpu_usage['wa'] > 30:
        warn.add('IOwait is {}% (>30%)'.format(cpu_usage['wa']))

    total_usage = cpu_usage['us'] + cpu_usage['sy']
    if total_usage > 90:
        crit.add('Total CPU usage is {:.1f}% ({}% user, {}% system, >90%)'.format(
            total_usage,
            cpu_usage['us'],
            cpu_usage['sy'],
        ))
    elif total_usage > 80:
        warn.add('Total CPU usage is {:.1f}% ({}% user, {}% system > 80%)'.format(
            total_usage,
            cpu_usage['us'],
            cpu_usage['sy'],
        ))

    for line in sorted(crit):
        print(line)

    for line in sorted(warn):
        print(line)

    if crit:
        exit(2)
    elif warn:
        exit(1)
    else:
        exit(0)
except Exception as e:
    print(repr(e))
    exit(3)