diff --git a/agent_based/snmp_uptime.py b/agent_based/snmp_uptime.py new file mode 100644 index 0000000000000000000000000000000000000000..12f736f1f21e8a673998501c0cdd404b7f0473d6 --- /dev/null +++ b/agent_based/snmp_uptime.py @@ -0,0 +1,81 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# Copyright (C) 2019 tribe29 GmbH - License: GNU General Public License v2 +# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and +# conditions defined in the file COPYING, which is part of this source code package. + +# 2022-03-06: extended to use snmpEngineTime if available +# snmpEngineTime : The number of seconds since the value of the snmpEngineBoots object last changed. When +# incrementing this object's value would cause it to exceed its maximum, snmpEngineBoots is +# incremented as if a re-initialization had occurred, and this object's value consequently +# reverts to zero. +# snmpEngineBoots: The number of times that the SNMP engine has (re-)initialized itself since snmpEngineID was +# last configured. On Cisco Device this gives the number of reboots +# + +from typing import Optional + +from cmk.base.plugins.agent_based.agent_based_api.v1 import exists, register, SNMPTree +from cmk.base.plugins.agent_based.agent_based_api.v1.type_defs import StringTable +from cmk.base.plugins.agent_based.utils import uptime + + +def parse_snmp_uptime(string_table: StringTable) -> Optional[uptime.Section]: + """ + >>> parse_snmp_uptime([['2297331594', '']]) + Section(uptime_sec=22973315, message=None) + >>> parse_snmp_uptime([['124:21:26:42.03', '124:21:29:01.14']]) + Section(uptime_sec=10790941, message=None) + >>> None is parse_snmp_uptime([[u'', u'Fortigate 80C']]) # nonsense + True + + """ + if not string_table: + return None + + sysUpTime, hrSystemUptime, snmpEngineTime = string_table[0] + + ticks = sysUpTime or hrSystemUptime + + if len(ticks) < 3: + return None + + try: + return uptime.Section(int(snmpEngineTime), None) + except ValueError: + pass + + try: + return uptime.Section(int(ticks[:-2]), None) + except ValueError: + pass + + try: + days, h, m, s = ticks.split(":") + return uptime.Section( + (int(days) * 86400) + (int(h) * 3600) + (int(m) * 60) + int(float(s)), + None, + ) + except ValueError: + pass + + return None + + +register.snmp_section( + name="snmp_uptime", + parsed_section_name="uptime", + parse_function=parse_snmp_uptime, + fetch=SNMPTree( + base=".1.3.6.1", + oids=[ + # On Linux appliances: .1.3.6.1.2.1.1.3.0 means uptime of snmpd + # .1.3.6.1.2.1.25.1.1.0 means system uptime + "2.1.1.3", # DISMAN-EVENT-MIB::sysUpTime + "2.1.25.1.1", # HOST-RESOURCES-MIB::hrSystemUptime + "6.3.10.2.1.3", # SNMP-FRAMEWORK-MIB::snmpEngineTime + ], + ), + detect=exists(".1.3.6.1.2.1.1.1.0"), +) + diff --git a/packages/snmp_uptime b/packages/snmp_uptime index c94b8c3015857b36bdb1086874f0d55cf453b830..b8c3394217e6c432ac8e9f0a07c93d6ab2a2bc9a 100644 --- a/packages/snmp_uptime +++ b/packages/snmp_uptime @@ -1,10 +1,15 @@ -{'author': u'Th.L. Th.L. (thl-cmk[at]outlook[dot]com)', - 'description': '\trewrite of snmp_uptime\n\tuses hrSystemUptime instead of sysUpTime if available.\n\thrSystemUptime is the real system uptime,\n\tsysUpTime is the uptime of the snmp agent\n', +{'author': 'Th.L. (thl-cmk[at]outlook[dot]com)', + 'description': 'This check is based on the snmp_uptime check form CMK 2.0. It ' + 'adds the SNMP-FRAMEWORK-MIB::snmpEngineTime OID to the check. ' + 'This in seconds not ticks and therefore the Uptime can now be ' + 'around 68 years before a rollover of the counter.\n' + '\n', 'download_url': 'https://thl-cmk.hopto.org', - 'files': {'checks': ['snmp_uptime']}, + 'files': {'agent_based': ['snmp_uptime.py']}, 'name': 'snmp_uptime', 'num_files': 1, - 'title': u'monitor system uptime via hrSystemUptime', - 'version': '20190810.v.0.1', - 'version.min_required': '1.2.8b8', - 'version.packaged': '1.4.0p35'} \ No newline at end of file + 'title': 'SNMP Uptime', + 'version': '20220306.v0.0.1', + 'version.min_required': '2021.09.20', + 'version.packaged': '2021.09.20', + 'version.usable_until': None} \ No newline at end of file diff --git a/snmp_uptime.mkp b/snmp_uptime.mkp index 2a53fa8c896d56f70250c3f8f56a4ff92537158c..68aa670db90158091d5bad148e878c39c616d9ba 100644 Binary files a/snmp_uptime.mkp and b/snmp_uptime.mkp differ