diff --git a/bundles/icinga2/files/check_usv_snmp b/bundles/icinga2/files/check_usv_snmp new file mode 100644 index 0000000..2256a3f --- /dev/null +++ b/bundles/icinga2/files/check_usv_snmp @@ -0,0 +1,69 @@ +#!/usr/bin/env python3 + +from sys import argv, exit + +from easysnmp import Session + +try: + hostname = argv[1] + community = argv[2] +except IndexError: + print('Usage: {} '.format(argv[0])) + exit(3) + +try: + session = Session(hostname=hostname, community=community, version=2) + + items = session.walk('iso.3.6.1.2.1.33') + + result = {} + for item in items: + result[item.oid[len('iso.3.6.1.2.1.33')+1:]] = item.value + + warn = set() + crit = set() + + if int(result['1.2.2.0']): + warn.add('USV is on battery!') + + # Estimated runtime in minutes + if int(result['1.2.3.0']) < 15: + crit.add('Remaining runtime is less than 10 minutes! Only {} minutes left.'.format(result['1.2.3.0'])) + elif int(result['1.2.3.0']) < 25: + warn.add('Remaining runtime is less than 15 minutes! Only {} minutes left.'.format(result['1.2.3.0'])) + + # Battery status in percent + if int(result['1.2.4.0']) < 10: + crit.add('Remaining runtime is less than 10%! Only {}% left.'.format(result['1.2.4.0'])) + elif int(result['1.2.4.0']) < 25: + warn.add('Remaining runtime is less than 15%! Only {}% left.'.format(result['1.2.4.0'])) + + # Output load in percent + if int(result['1.4.4.1.5.1']) > 90: + crit.add('Output Power is more than 90% of rated capacity: {} W'.format(result['1.4.4.1.4.1'])) + elif int(result['1.4.4.1.5.1']) > 80: + warn.add('Output Power is more than 80% of rated capacity: {} W'.format(result['1.4.4.1.4.1'])) + + for line in sorted(crit): + print(line) + + for line in sorted(warn): + print(line) + + if crit: + exit(2) + elif warn: + exit(1) + else: + print('{model} running{from_bat} at {out_pct}% load ({out_w} W), battery at {bat_pct}% ({bat_min} min)'.format( + model=result['1.1.2.0'], + out_pct=result['1.4.4.1.5.1'], + out_w=result['1.4.4.1.4.1'], + bat_pct=result['1.2.4.0'], + bat_min=result['1.2.3.0'], + from_bat=' from battery' if int(result['1.2.2.0']) else '', + )) + exit(0) +except Exception as e: + print(repr(e)) + exit(3) diff --git a/bundles/icinga2/files/icinga2/check_commands.conf b/bundles/icinga2/files/icinga2/check_commands.conf index 92bddaa..2c914e2 100644 --- a/bundles/icinga2/files/icinga2/check_commands.conf +++ b/bundles/icinga2/files/icinga2/check_commands.conf @@ -105,3 +105,11 @@ object CheckCommand "check_smtp" { } } } + +object CheckCommand "check_usv" { + import "plugin-check-command" + import "ipv4-or-ipv6" + + command = [ "/usr/local/share/icinga/plugins/check_usv_snmp", "$address$", "$snmp_community$" ] + vars.snmp_community = "public" +} diff --git a/bundles/icinga2/items.py b/bundles/icinga2/items.py index 7cc600a..d5c889b 100644 --- a/bundles/icinga2/items.py +++ b/bundles/icinga2/items.py @@ -85,6 +85,9 @@ files = { '/usr/local/share/icinga/plugins/check_freifunk_node': { 'mode': '0755', }, + '/usr/local/share/icinga/plugins/check_usv_snmp': { + 'mode': '0755', + }, '/etc/sshmon.priv': { 'content': repo.vault.decrypt_file(join('sshmon', 'sshmon.key.vault')), 'owner': 'nagios', diff --git a/nodes/home/usv01.py b/nodes/home/usv01.py index 8730260..77a95a2 100644 --- a/nodes/home/usv01.py +++ b/nodes/home/usv01.py @@ -8,6 +8,16 @@ nodes['home.usv01'] = { }, }, }, + 'icinga2_api': { + 'usv': { + 'services': { + 'USV STATUS': { + 'check_command': 'check_usv', + 'vars.notification.mail': True, + }, + }, + }, + }, }, }