Skip to content

Commit

Permalink
Initial release of check_hetzner_storage_box.py
Browse files Browse the repository at this point in the history
  • Loading branch information
martinseener committed Dec 9, 2018
1 parent 9f7f019 commit df2cb1c
Show file tree
Hide file tree
Showing 5 changed files with 222 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -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
Expand Down
72 changes: 72 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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 #<ID>).
-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 <STORAGE_BOX_ID> -u '<HETZNER_WS_USER>' -p '<HETZNER_WS_PASSWORD>'
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 <STORAGE_BOX_ID> -u '<HETZNER_WS_USER>' -p '<HETZNER_WS_PASSWORD>' -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).
138 changes: 138 additions & 0 deletions check_hetzner_storage_box.py
Original file line number Diff line number Diff line change
@@ -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__ = '[email protected]'
__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 #<ID>).',
)
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:])
5 changes: 5 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
certifi==2018.11.29
chardet==3.0.4
idna==2.7
requests==2.20.1
urllib3==1.24.1

0 comments on commit df2cb1c

Please sign in to comment.