2020-11-22 10:38:53 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2023-04-09 15:32:36 +00:00
|
|
|
from json import load
|
2020-11-22 10:38:53 +00:00
|
|
|
from sys import exit
|
|
|
|
|
2023-02-05 16:30:58 +00:00
|
|
|
from requests import get
|
|
|
|
|
2023-04-09 15:32:36 +00:00
|
|
|
with open('/etc/icinga2/notification_config.json') as f:
|
|
|
|
CONFIG = load(f)
|
2020-11-22 10:38:53 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
r = get(
|
|
|
|
'https://api.sipgate.com/v2/balance',
|
2023-04-09 15:32:36 +00:00
|
|
|
auth=(CONFIG['sipgate']['user'], CONFIG['sipgate']['password']),
|
2020-11-22 10:38:53 +00:00
|
|
|
headers={'Accept': 'application/json'},
|
|
|
|
)
|
|
|
|
|
|
|
|
if r.status_code == 401:
|
|
|
|
# Status code 401 means our login data is wrong. Since we're
|
|
|
|
# using the same login data in icinga_notification_wrapper, we
|
|
|
|
# won't be able to send SMS either. This should *not* result in
|
|
|
|
# this check going UNKNOWN.
|
|
|
|
print('CRITICAL: Getting the account balance failed with status code 401!')
|
|
|
|
exit(2)
|
|
|
|
else:
|
|
|
|
r.raise_for_status()
|
|
|
|
except Exception as e:
|
|
|
|
print(repr(e))
|
|
|
|
exit(3)
|
|
|
|
|
|
|
|
# No, we can't combine those two try..except blocks, because a connection
|
|
|
|
# error means the check is UNKNOWN, but a parsing error is CRITICAL.
|
|
|
|
try:
|
|
|
|
json = r.json()
|
|
|
|
money = json['amount']/10000
|
|
|
|
|
|
|
|
if money < 2:
|
|
|
|
print('CRITICAL: Only {} {} left in account!'.format(money, json['currency']))
|
|
|
|
exit(2)
|
|
|
|
elif money < 3:
|
|
|
|
print('WARNING: Only {} {} left in account!'.format(money, json['currency']))
|
|
|
|
exit(1)
|
|
|
|
else:
|
|
|
|
print('Account holds {} {}.'.format(money, json['currency']))
|
|
|
|
exit(0)
|
|
|
|
except Exception as e:
|
|
|
|
# API changed, returned malformed JSON or whatever. This is an error.
|
|
|
|
print(repr(e))
|
|
|
|
exit(2)
|