#!/usr/bin/env python3

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

try:
    top_output = None

    for line in check_output(['top', '-b', '-n1', '-d1']).decode('UTF-8').splitlines():
        if line.lower().strip().startswith('%cpu'):
            top_output = line.lower().split(':', 2)[1]
            break

    if not top_output:
        print('%cpu not found in top output')
        exit(3)

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

    warn = set()
    crit = set()

    print(top_output)

    # 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)