diff --git a/bundles/gitea/metadata.py b/bundles/gitea/metadata.py index b645fd2..939609f 100644 --- a/bundles/gitea/metadata.py +++ b/bundles/gitea/metadata.py @@ -75,7 +75,7 @@ def icinga_check_for_new_release(metadata): 'services': { 'GITEA UPDATE': { 'command_on_monitored_host': '/usr/local/share/icinga/plugins/check_github_for_new_release go-gitea/gitea v{}'.format(metadata.get('gitea/version')), - 'retry_interval': '60m', + 'check_interval': '60m', }, }, }, diff --git a/bundles/icinga2/files/check_sipgate_account_balance b/bundles/icinga2/files/check_sipgate_account_balance new file mode 100644 index 0000000..8e8ce2d --- /dev/null +++ b/bundles/icinga2/files/check_sipgate_account_balance @@ -0,0 +1,47 @@ +#!/usr/bin/env python3 + +from requests import get +from sys import exit + +SIPGATE_USER = '${node.metadata['icinga2']['sipgate_user']}' +SIPGATE_PASS = '${node.metadata['icinga2']['sipgate_pass']}' + +try: + r = get( + 'https://api.sipgate.com/v2/balance', + auth=(SIPGATE_USER, SIPGATE_PASS), + 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) diff --git a/bundles/icinga2/files/icinga2/check_commands.conf b/bundles/icinga2/files/icinga2/check_commands.conf index 9f59c72..0d79cc5 100644 --- a/bundles/icinga2/files/icinga2/check_commands.conf +++ b/bundles/icinga2/files/icinga2/check_commands.conf @@ -79,6 +79,13 @@ object CheckCommand "check_imap" { } } +object CheckCommand "check_sipgate_account_balance" { + import "plugin-check-command" + import "ipv4-or-ipv6" + + command = [ "/usr/local/share/icinga/plugins/check_sipgate_account_balance" ] +} + object CheckCommand "check_smtp" { import "plugin-check-command" import "ipv4-or-ipv6" diff --git a/bundles/icinga2/items.py b/bundles/icinga2/items.py index b0e5874..0836885 100644 --- a/bundles/icinga2/items.py +++ b/bundles/icinga2/items.py @@ -71,6 +71,10 @@ files = { '/usr/local/share/icinga/plugins/check_by_sshmon': { 'mode': '0755', }, + '/usr/local/share/icinga/plugins/check_sipgate_account_balance': { + 'mode': '0755', + 'content_type': 'mako', + }, '/etc/sshmon.priv': { 'content': repo.vault.decrypt_file(join('sshmon', 'sshmon.key.vault')), 'owner': 'nagios', diff --git a/bundles/icinga2/metadata.py b/bundles/icinga2/metadata.py index 2378a9c..91ce91d 100644 --- a/bundles/icinga2/metadata.py +++ b/bundles/icinga2/metadata.py @@ -30,6 +30,16 @@ defaults = { 'root': repo.vault.password_for(f'{node.name} icinga2 api root'), }, }, + 'icinga2_api': { + 'icinga2': { + 'services': { + 'SIPGATE ACCOUNT BALANCE': { + 'check_command': 'check_sipgate_account_balance', + 'check_interval': '30m', + }, + }, + }, + }, 'icingaweb2': { 'setup-token': repo.vault.password_for(f'{node.name} icingaweb2 setup-token'), }, diff --git a/bundles/mautrix-telegram/metadata.py b/bundles/mautrix-telegram/metadata.py index f4782dc..5873d80 100644 --- a/bundles/mautrix-telegram/metadata.py +++ b/bundles/mautrix-telegram/metadata.py @@ -51,7 +51,7 @@ def icinga_check_for_new_release(metadata): 'services': { 'MAUTRIX-TELEGRAM UPDATE': { 'command_on_monitored_host': '/usr/local/share/icinga/plugins/check_github_for_new_release tulir/mautrix-telegram {}'.format(metadata.get('mautrix-telegram/version')), - 'retry_interval': '60m', + 'check_interval': '60m', }, }, },