CREATIVE CHAOS   ▋ blog

Monitoring B2 bucket sizes with Check-MK

PUBLISHED ON 28/08/2019 — EDITED ON 11/12/2023 — SYSOPS

This knowledge was brought to you by Guru.

Intro

Here in front of you lies a little check for B2 bucket sizes that I hacked together for use in our company. We are using Backblaze/B2 extensively for storage and backups. So it is very useful to know if data usage drops or raises significantly, as that could be an early indication that something is not going well and it should be checked by someone.

The check is plain stupid, it lists all the buckets that are accessible via b2 cli client, for each one requests its size and prints out the data, so Check-MK can parse it and display wonderful plots.

Howto

Monitoring server

On the monitoring server (the one where Check-MK is installed), we need to add a custom check for B2. This part basically parses the output of the check_mk_agent from the monitored machine.

If you upgrade Check_MK, don’t forget to copy the check.

Check

vim /opt/omd/versions/1.5.0p16.cre/share/check_mk/checks/b2
#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
#

# <<<b2>>>
# bucket1 29
# bucket2 18464514387

# set default value of variable (user can override in main.mk)
# default values in GB
b2_size_default_values = (100, 200)

# the inventory function (dummy)
def inventory_b2_size(info):
    for line in info:
        bucket = line[0]
        size = line[1]

        yield bucket, "b2_size_default_values"

# the check function (dummy)
def check_b2_size(item, params, info):
    warn, crit = params
    warn_b = warn * 1024 * 1024 * 1024
    crit_b = crit * 1024 * 1024 * 1024

    for line in info:
        if line[0] == item:
            size = int(line[1])
            perfdata = [ ( "size", size, warn_b, crit_b ) ]
            if size > crit_b:
                return 2, "Size: %s" % get_bytes_human_readable(size), perfdata
            if size > warn_b:
                return 1, "Size: %s" % get_bytes_human_readable(size), perfdata
            else:
                return 0, "Size: %s" % get_bytes_human_readable(size), perfdata

# declare the check to Checkmk
check_info["b2.size"] = {
    'check_function':            check_b2_size,
    'inventory_function':        inventory_b2_size,
    'service_description':       'B2 bucket size %s',
    'has_perfdata':              True,
}

Monitored client

Requirements

B2 Command Line Tool

Bucket size check

vim /opt/b2-check-size.sh
#!/bin/bash


BUCKETS="$(b2 list-buckets | awk -F'  ' '{ print $3}')"
for BUCKET in $BUCKETS; do
    b2 get-bucket --showSize $BUCKET | grep totalSize | awk '{print $2}' | sed "s@^@$BUCKET @"
done > /tmp/b2-size

Cron job

In our case, the bucket size changes mostly when backups are done in the night. The query to B2 is pretty slow and can increase the cost of service, so we are caching the data to a temporary file /tmp/b2-size.

vim /etc/cron.d/check-b2-size
# Check B2 bucket sizes

# set PATH
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

# m h   dom mon dow   user    command
00  05  *   *   *   root    /opt/b2-check-size.sh

# vim: set ts=4 sw=0 et:

Check plugin

vim usr/lib/check_mk_agent/plugins/mk_b2
#!/bin/bash

function check_b2_size {
    cat /tmp/b2-size
}

if which b2 > /dev/null ; then
    echo '<<<b2>>>'
    out=$(check_b2_size)
    if [ -z "$out" ]; then
        echo "??"
    else
        echo "$out"
    fi
fi

See Also