From df2cb1c1bf73c259e6a27874b419fc3dc40b31f1 Mon Sep 17 00:00:00 2001 From: Martin Seener Date: Sun, 9 Dec 2018 13:03:11 +0100 Subject: [PATCH] Initial release of check_hetzner_storage_box.py --- CHANGELOG.md | 6 ++ LICENSE | 2 +- README.md | 72 ++++++++++++++++++ check_hetzner_storage_box.py | 138 +++++++++++++++++++++++++++++++++++ requirements.txt | 5 ++ 5 files changed, 222 insertions(+), 1 deletion(-) create mode 100644 CHANGELOG.md create mode 100644 README.md create mode 100755 check_hetzner_storage_box.py create mode 100644 requirements.txt diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..01d5866 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,6 @@ +# Change Log +All notable changes to this project will be documented in this file. +This project adheres to [Semantic Versioning](http://semver.org/). + +## [1.0.0] - 2018-12-09 +- Initial release diff --git a/LICENSE b/LICENSE index e2d7e36..af1c529 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2018 Martin +Copyright (c) 2018 Martin Seener Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md new file mode 100644 index 0000000..60617e8 --- /dev/null +++ b/README.md @@ -0,0 +1,72 @@ +# check_hetzner_storage_box Icinga/Nagios check command + +This check can be used with Icinga and Nagios and will check the free space of your Hetzner Storage Box and alerts you if it is below certain thresholds. + +## Requirements + +`check_hetzner_storage_box` requires Python 3.x to run and has been successfully tested with Python 3.4 on Debian and Python 3.7 on macOS. + +This check command depends on the following python modules: + * requests + * argparse + +**Installation on Debian / Ubuntu** +``` +apt install python3-requests +``` + +**Installation on Redhat 6 / CentOS 6 (not tested!)** +``` +yum install python-argparse python34-requests +``` + +**Installation on Redhat 7 / CentOS 7 (not tested!)** +``` +yum install python-requests +``` + +**Installation using the requirements file with pip** +``` +pip install -r requirements.txt +``` + +## Usage + +``` +usage: check_hetzner_storage_box.py [-h] [-s STORAGE_BOX] [-u USER] + [-p PASSWORD] [-w WARNING] [-c CRITICAL] + +check_hetzner_storage_box is an Icinga/Nagios-compatible check that checks for +the free space of a storage box in your Hetzner Robot account and alerts upon +reasonable thresholds. + +optional arguments: + -h, --help show this help message and exit + -s STORAGE_BOX, --storage-box STORAGE_BOX + Enter the Storage Box ID. You can see this in your + Robot WebUI Storage Box overview (for ex. BX40 #). + -u USER, --user USER Enter the Hetzner Robot Webservice username. + -p PASSWORD, --password PASSWORD + Enter the Hetzner Robot Webservice password. + -w WARNING, --warning WARNING + Enter the WARNING threshold in percent (free). + -c CRITICAL, --critical CRITICAL + Enter the CRITICAL threshold in percent (free). +``` + +With a WARNING treshold of 10.0% and CRITICAL treshold of 5.0% (default). +``` +./check_hetzner_storage_box.py -s -u '' -p '' +OK - Free disk size of Storage Box #123456 (Backup-Box-1) is currently 59.6% +``` + +With own WARNING and CRITICAL tresholds. + +``` +./check_hetzner_storage_box.py -s -u '' -p '' -w 25 -c 15 +CRITICAL - Free disk size of Storage Box #123457 (Backup-Box-2) is less than 15.0% of the quota! +``` + +## Contribution and License + +Feel free to contribute. It's licensed under the [MIT LICENSE](LICENSE). diff --git a/check_hetzner_storage_box.py b/check_hetzner_storage_box.py new file mode 100755 index 0000000..b8dd7a3 --- /dev/null +++ b/check_hetzner_storage_box.py @@ -0,0 +1,138 @@ +#!/usr/bin/env python3 +# # -*- coding: utf-8 -*- + +""" +check_hetzner_storage_box provides an icinga/nagios check command +to check for free space of your Hetzner Storage Boxes. +""" + +from __future__ import print_function +import sys +import argparse +import requests + + +__author__ = 'Martin Seener' +__copyright__ = 'Copyright 2018, Martin Seener' +__license__ = 'MIT' +__version__ = '1.0.0' +__maintainer__ = 'Martin Seener' +__email__ = 'martin@sysorchestra.com' +__status__ = 'Production' + + +def validate_storage_box(storage_box, user, password): + r = requests.get('https://robot-ws.your-server.de/storagebox/' + + storage_box, + auth=(user, password)) + if r.status_code == 401: + print('UNKNOWN - Webservice is not enabled or user/password is wrong') + sys.exit(3) + elif r.status_code == 403: + print('UNKNOWN - Rate limit exeeded. Please don\'t check too often!.') + sys.exit(3) + elif r.status_code == 404: + print('UNKNOWN - Can\'t find the storage box with ID ' + storage_box) + sys.exit(3) + elif r.status_code == 200: + return True + else: + print('UNKOWN - An unknown error occured!') + sys.exit(3) + + +def check_storage_box(storage_box, user, password, warning, critical): + r = requests.get('https://robot-ws.your-server.de/storagebox/' + + storage_box, + auth=(user, password)) + + disk_name = r.json()['storagebox']['name'] + disk_quota = r.json()['storagebox']['disk_quota'] + disk_usage = r.json()['storagebox']['disk_usage'] + disk_free_percent = round(100 - (disk_usage / disk_quota) * 100, 1) + + if disk_free_percent <= critical: + print('CRITICAL - Free disk size of Storage Box #{} ({}) is less than {}% of the quota!'.format( + storage_box, + disk_name, + critical + )) + sys.exit(2) + elif disk_free_percent <= warning: + print('WARNING - Free disk size of Storage Box #{} ({}) is less than {}% of the quota!'.format( + storage_box, + disk_name, + warning + )) + sys.exit(1) + elif warning < disk_free_percent: + print('OK - Free disk size of Storage Box #{} ({}) is currently {}%'.format( + storage_box, + disk_name, + disk_free_percent + )) + sys.exit() + else: + print('UNKNOWN - Unknown error occured!') + sys.exit(3) + + +def main(args): + parser = argparse.ArgumentParser( + description='\ + check_hetzner_storage_box is an Icinga/Nagios-compatible\ + check that checks for the free space of a storage\ + box in your Hetzner Robot account and alerts\ + upon reasonable thresholds.', + ) + + parser.add_argument( + '-s', + '--storage-box', + dest='storage_box', + type=str, + help='Enter the Storage Box ID. You can see this in your Robot WebUI\ + Storage Box overview (for ex. BX40 #).', + ) + parser.add_argument( + '-u', + '--user', + type=str, + help='Enter the Hetzner Robot Webservice username.', + ) + parser.add_argument( + '-p', + '--password', + type=str, + help='Enter the Hetzner Robot Webservice password.', + ) + parser.add_argument( + '-w', + '--warning', + default=10.0, + type=float, + help='Enter the WARNING threshold in percent (free).', + ) + parser.add_argument( + '-c', + '--critical', + default=5.0, + type=float, + help='Enter the CRITICAL threshold in percent (free).', + ) + + args = parser.parse_args() + if args.storage_box and validate_storage_box(args.storage_box, + args.user, + args.password): + check_storage_box(args.storage_box, + args.user, + args.password, + args.warning, + args.critical) + else: + parser.print_help() + + +if __name__ == '__main__': + main(sys.argv[1:]) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..2979b0c --- /dev/null +++ b/requirements.txt @@ -0,0 +1,5 @@ +certifi==2018.11.29 +chardet==3.0.4 +idna==2.7 +requests==2.20.1 +urllib3==1.24.1