From ac9e80bf2bf9c56da63ffa84c84d62bf3cc62837 Mon Sep 17 00:00:00 2001 From: anima Date: Fri, 7 Feb 2025 17:07:18 +0100 Subject: [PATCH] add fan check --- checks/check_snmp_synology.py | 64 ++++++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/checks/check_snmp_synology.py b/checks/check_snmp_synology.py index 7a3e9f1..e71364a 100755 --- a/checks/check_snmp_synology.py +++ b/checks/check_snmp_synology.py @@ -8,7 +8,7 @@ - https://easysnmp.readthedocs.io/en/latest/session_api.html """ -__version__ = '0.3.0' +__version__ = '0.4.0' __author__ = 'anima' # imports @@ -186,6 +186,62 @@ class SNMPSynologyPowerSupplyContext(nagiosplugin.Context): return self.result_cls(nagiosplugin.Unknown, "unknown", metric) +# +# Synology [DSM & DSM UC] Returns error if system fan fails +# +class SNMPSynologyFansResource(nagiosplugin.Resource): + def __init__(self, session) -> None: + self.session = session + + def probe(self) -> list: + """check fans status (normal or failed) + + Returns: + nagiosplugin.Metric: single metric element (return) + """ + baseoid = '.1.3.6.1.4.1.6574.1' + fans = dict() + fans['system'] = '.4.1.0' + fans['cpu'] = '.4.2.0' + + for fan, oid in fans.items(): + result = self.session.get(baseoid + oid).value + yield nagiosplugin.Metric(name=fan, value=result, context='fan_context') + + +class SNMPSynologyFansResult(nagiosplugin.Result): + def __str__(self): + if self.metric.value == '1': status = 'normal' + else: status = 'failed' + return f'{self.metric.name} fan is in status {status}!' + + +class SNMPSynologyFansContext(nagiosplugin.Context): + def __init__(self, name): + super().__init__(name, fmt_metric='{name} is', result_cls=SNMPSynologyFansResult) + + def evaluate(self, metric, resource): + if metric.value == '2': + return self.result_cls(nagiosplugin.Critical, "critical", metric) + elif metric.value == '1': + return self.result_cls(nagiosplugin.Ok, "ok", metric) + return self.result_cls(nagiosplugin.Unknown, "unknown", metric) + + +class SNMPSynologyFansSummary(nagiosplugin.Summary): + def verbose(self, results): + result_str = '' + for result in results: + result_str += f'{str(result)}\n' + return result_str + + def ok(self, results): + return + + def problem(self, results): + return + + # # Arguments # @@ -209,6 +265,7 @@ def parse_args() -> argparse.Namespace: 'system', 'systemp', 'powersupply', + 'fans', ], help='check mode to run') @@ -269,6 +326,11 @@ def main(): SNMPSynologyPowerSupplyContext(name='powersupply_context'), nagiosplugin.Summary()) check.name = "Power Supply Status" + case 'fans': + check = nagiosplugin.Check(SNMPSynologyFansResource(session=session), + SNMPSynologyFansContext(name='fan_context'), + SNMPSynologyFansSummary()) + check.name = "Fans Status" case _: raise nagiosplugin.CheckError(f'Unknown check mode: {args.check_mode}')