-
Notifications
You must be signed in to change notification settings - Fork 2
/
check_hetzner_storage_box.py
executable file
·164 lines (143 loc) · 4.75 KB
/
check_hetzner_storage_box.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python
# # -*- 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-2024, viafintech GmbH'
__license__ = 'MIT'
__version__ = '1.1.5'
__maintainer__ = 'Martin Seener'
__email__ = '[email protected]'
__status__ = 'Production'
def validate_robot_ws(user, password):
try:
r = requests.get('https://robot-ws.your-server.de/storagebox',
auth=(user, password))
r.raise_for_status()
except requests.exceptions.HTTPError as err:
print('UNKNOWN - The following error occured: {}'.format(err))
sys.exit(3)
return True
def get_storage_box_info(storage_box, user, password):
try:
r = requests.get('https://robot-ws.your-server.de/storagebox/' +
storage_box,
auth=(user, password))
r.raise_for_status()
except requests.exceptions.HTTPError:
print('UNKNOWN - Can\'t find storage box (#{})'
.format(storage_box))
name = r.json()['storagebox']['name']
quota = float(r.json()['storagebox']['disk_quota'])
usage = float(r.json()['storagebox']['disk_usage'])
free = round(100 - (usage / quota) * 100, 1)
return name, quota, usage, free
def check_storage_box(storage_box, user, password, warning, critical):
name, quota, usage, free = get_storage_box_info(storage_box,
user,
password)
# PerfData
perf_warning = round(quota * (100 - warning) / 100, 1)
perf_critical = round(quota * (100 - critical) / 100, 1)
if free <= critical:
print('CRITICAL - Free disk size of Storage Box #{} ({}) '
'is less than {}% of the quota!'
'|Usage={}MB;{};{};0;{}'
.format(storage_box,
name,
critical,
usage,
perf_warning,
perf_critical,
quota)
)
sys.exit(2)
elif free <= warning:
print('WARNING - Free disk size of Storage Box #{} ({}) '
'is less than {}% of the quota!'
'|Usage={}MB;{};{};0;{}'
.format(storage_box,
name,
warning,
usage,
perf_warning,
perf_critical,
quota)
)
sys.exit(1)
elif warning < free:
print('OK - Free disk size of Storage Box #{} ({}) '
'is currently {}%'
'|Usage={}MB;{};{};0;{}'
.format(storage_box,
name,
free,
usage,
perf_warning,
perf_critical,
quota)
)
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_robot_ws(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:])