AussieBroadband CheckMK Plugin

I’ve recently changed my ISP to AussieBroadband.  Since I’m now working under a quota, I want a way to monitor my quota in CheckMK.  Enter a bit of python.  If you want to use this, you’ll need to adjust the hashbang to suit your actual OMD site, and then pass a parameter which is your username:password to get onto your ABB account.

#!/omd/sites/checkmk/bin/python2.7
#
# Parses AussieBroadband Quota Page to generate a CheckMK alert and stats pages
#

import requests
import HTMLParser
import re
import time
import sys

# Create a basic HTML parser for fetching the Total Used up/down and Data Left fields
class UsageParser(HTMLParser.HTMLParser):
    # Always given in MB
    quota_up = None
    quota_down = None
    quota_left = None

    # This flag determines the state of the parser
    usage_state = None

    def handle_data(self, data):
        if data == "Total Used":
            # This flag is followed by an upload value
            self.usage_state = "upload"
        elif data == "Data Left":
            # This flag is followed by the quota left value
            self.usage_state = "remain"
        elif self.usage_state is not None:
            m = re.match("([0-9\.]+) ([KMGT])B",data)
            if m:
                value = float(m.group(1))
                unit = m.group(2)

                # Convert unit to MB
                if unit == "K":
                    value = value/1024.0
                elif unit == "M":
                    value = value
                elif unit == "G":
                    value = value*1024.0
                elif unit == "T":
                    value = value*1024.0*1024.0

                # Put unit in correct pool based on value and reset parser state
                if self.usage_state == "upload":
                    self.quota_up = value
                    # Uploads are followed by downloads
                    self.usage_state = "download"
                elif self.usage_state == "download":
                    self.quota_down = value
                    self.usage_state = None
                elif self.usage_state == "remain":
                    self.quota_left = value
                    self.usage_state = None

#####################################################################

status = 0
statustext = "OK"

try:
    creds = sys.argv[1].split(":")

    payload = {
        'login_username': creds[0],
        'login_password': creds[1],
        'submit': 'Login'
    }

    # Fetch the usage page and parse it
    r = requests.post("https://my.aussiebroadband.com.au/usage.php", data=payload)
    parser = UsageParser()
    parser.feed(r.text)

    # Derive some parameters for the check
    total = parser.quota_left + parser.quota_up + parser.quota_down
    critthresh = 0.10*total
    warnthresh = 0.25*total

    # Determine the status of the check
    if parser.quota_left < critthresh:
        status = 2
        statustext = "CRITICAL"
    elif parser.quota_left < warnthresh:
        status = 1
        statustext = "WARNING"

    # Format the output message
    print "{7} - {1} MB quota remaining|left={1};{2};{3};0;{4}, upload={5}, download={6}".format( \
        status, \
        int(parser.quota_left), \
        int(warnthresh), \
        int(critthresh), \
        int(total), \
        int(parser.quota_up), \
        int(parser.quota_down), \
        statustext)

except:
    print "UNKNOWN - Unable to parse usage page!"
    status = 3
    statustext = "UNKNOWN"

sys.exit(status)

Enjoy.  It’s pretty quick and dirty, but it works.  You put this into your site’s local/lib/nagios/plugins directory, then add it as a classical check.

Raspberry Pi Temperature Monitoring with CheckMK

The Raspberry Pi running Raspian has some built-in temperature sensors.  The sensor is on the CPU die, and you can find it at;

/sys/class/thermal/thermal_zone0/temp

CheckMK supports the idea of local checks.  A local check is a simple script that runs in the agent on a host and performs whatever check processing and verification that’s required on the client end.  This means you cannot customize the warn/crit thresholds from the CheckMK host end.  But they’re easy to write.

The above simplistic script reads in the CPU temperature of the RPi, and sets a warn threshold of 90% of the throttling temperature with a critical threshold of 100% of the throttling temperature.

If you add this into;

/usr/lib/check_mk_agent/local

On your Raspian install, then manually run check_mk_agent, you’ll see in the <<<local>>> section the output from the sensor.  You can then edit the host in CheckMK and add the new service that is automatically inventoried.  I assume here that your CPU die never gets below 0 degrees (should be fairly sensible in most circumstances, I imagine).

Easy!