diff --git a/CHANGELOG.md b/CHANGELOG.md index 2263bb32..4b0baeea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,10 +27,12 @@ Grafana: Icinga Director: +* all-the-rest.json: Add Debian 12 (Bookworm), add deb-lastactivity * all-the-rest.json: Increase file size warning for `/var/log/secure` Monitoring Plugins: +* deb-lastactivity ([PR #710](https://github.com/Linuxfabrik/monitoring-plugins/issues/710), thanks to [Yannic Schüpbach](https://github.com/Dissiyt)) * gitlab-health (fix #670) * gitlab-liveness (fix #670) * gitlab-readiness (fix #670) diff --git a/check-plugins/deb-lastactivity/README.rst b/check-plugins/deb-lastactivity/README.rst new file mode 100644 index 00000000..19571e07 --- /dev/null +++ b/check-plugins/deb-lastactivity/README.rst @@ -0,0 +1,71 @@ +Check deb-lastactivity +====================== + +Overview +-------- + +Checks the timespan since the last package manager activity, for example due to an apt install/update. + + +Fact Sheet +---------- + +.. csv-table:: + :widths: 30, 70 + + "Check Plugin Download", "https://github.com/Linuxfabrik/monitoring-plugins/tree/main/check-plugins/deb-lastactivity" + "Check Interval Recommendation", "Once a day" + "Can be called without parameters", "Yes" + "Compiled for", "Linux" + + +Help +---- + +.. code-block:: text + + usage: deb-lastactivity [-h] [-V] [-c CRIT] [-w WARN] + + Checks the timespan since the last package manager activity, for example due + to an apt install/update. + + options: + -h, --help show this help message and exit + -V, --version show program's version number and exit + -c CRIT, --critical CRIT + Set the critical threshold (in days). Default: 365 + -w WARN, --warning WARN + Set the warning threshold (in days). Default: 90 + + +Usage Examples +-------------- + +.. code-block:: bash + + ./deb-lastactivity --warning 90 --critical 365 + +Output: + +.. code-block:: text + + Last package manager activity is 5M 2W ago [WARNING] (thresholds 90D/365D). + + +States +------ + +* WARN or CRIT if last activity is above a given threshold. + + +Perfdata / Metrics +------------------ + +There is no perfdata. + + +Credits, License +---------------- + +* Authors: `Linuxfabrik GmbH, Zurich `_ +* License: The Unlicense, see `LICENSE file `_. diff --git a/check-plugins/deb-lastactivity/deb-lastactivity b/check-plugins/deb-lastactivity/deb-lastactivity old mode 100644 new mode 100755 index c848f812..db40443e --- a/check-plugins/deb-lastactivity/deb-lastactivity +++ b/check-plugins/deb-lastactivity/deb-lastactivity @@ -1,110 +1,109 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8; py-indent-offset: 4 -*- -# -# Author: Linuxfabrik GmbH, Zurich, Switzerland -# Contact: info (at) linuxfabrik (dot) ch -# https://www.linuxfabrik.ch/ -# License: The Unlicense, see LICENSE file. - -# https://github.com/Linuxfabrik/monitoring-plugins/blob/main/CONTRIBUTING.rst - -"""See the check's README for more details. -""" - -import argparse # pylint: disable=C0413 -import sys # pylint: disable=C0413 - -import lib.base # pylint: disable=C0413 -import lib.human # pylint: disable=C0413 -import lib.shell # pylint: disable=C0413 -import lib.time # pylint: disable=C0413 -import lib.txt # pylint: disable=C0413 -from lib.globals import (STATE_CRIT, STATE_OK, # pylint: disable=C0413 - STATE_UNKNOWN, STATE_WARN) - -__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland' -__version__ = '2023071203' - -DESCRIPTION = """Checks the timespan since the last deb activity, for example due to a yum/dnf - install/update.""" - -DEFAULT_WARN = 90 # days -DEFAULT_CRIT = 365 # days - - -def parse_args(): - """Parse command line arguments using argparse. - """ - parser = argparse.ArgumentParser(description=DESCRIPTION) - - parser.add_argument( - '-V', '--version', - action='version', - version='%(prog)s: v{} by {}'.format(__version__, __author__), - ) - - parser.add_argument( - '-c', '--critical', - default=DEFAULT_CRIT, - dest='CRIT', - help='Set the critical threshold (in days). Default: %(default)s', - type=int, - ) - - parser.add_argument( - '-w', '--warning', - default=DEFAULT_WARN, - dest='WARN', - help='Set the warning threshold (in days). Default: %(default)s', - type=int, - ) - - return parser.parse_args() - - -def main(): - """The main function. Hier spielt die Musik. - """ - - # parse the command line, exit with UNKNOWN if it fails - try: - args = parse_args() - except SystemExit: - sys.exit(STATE_UNKNOWN) - - # execute the shell command and return its result and exit code - stdout, stderr, retc = lib.base.coe(lib.shell.shell_exec( - #'deb --query --all --queryformat "%{INSTALLTIME} %{NAME}\n"' - 'dpkg-query --show -f=''${db-fsys:Last-Modified} ${Package}\n' '*' '| grep -v '^ '' - )) - if stderr: - lib.base.cu(stderr) - - # stdout: '1662130946 unbound-libs\n1662130898 NetworkManager-libnm\n' - try: - last_activity = lib.txt.mltext2array(stdout, skip_header=False, sort_key=0) # `sort | tail -1` - last_activity = int(last_activity[-1][0]) - except: - lib.base.cu('Unable to get deb info, maybe an update never happened.') - - # calculating the final check state - delta = lib.time.now() - last_activity - state = lib.base.get_state(delta, args.WARN * 24 * 60 * 60, args.CRIT * 24 * 60 * 60) - - # over and out - lib.base.oao( - 'Last deb/yum/dnf activity is {} days ago{} (thresholds {}D/{}D).'.format( - lib.human.seconds2human(delta), - lib.base.state2str(state, prefix=' '), - args.WARN, - args.CRIT, - ), - state, - ) - - -if __name__ == '__main__': - try: - main() - except Exception: # pylint: disable=W0703 - lib.base.cu() +#!/usr/bin/env python3 +# -*- coding: utf-8; py-indent-offset: 4 -*- +# +# Author: Linuxfabrik GmbH, Zurich, Switzerland +# Contact: info (at) linuxfabrik (dot) ch +# https://www.linuxfabrik.ch/ +# License: The Unlicense, see LICENSE file. + +# https://github.com/Linuxfabrik/monitoring-plugins/blob/main/CONTRIBUTING.rst + +"""See the check's README for more details. +""" + +import argparse # pylint: disable=C0413 +import sys # pylint: disable=C0413 + +import lib.base # pylint: disable=C0413 +import lib.human # pylint: disable=C0413 +import lib.shell # pylint: disable=C0413 +import lib.time # pylint: disable=C0413 +import lib.txt # pylint: disable=C0413 +from lib.globals import (STATE_CRIT, STATE_OK, # pylint: disable=C0413 + STATE_UNKNOWN, STATE_WARN) + +__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland' +__version__ = '2023091601' + +DESCRIPTION = """Checks the timespan since the last package manager activity, + for example due to an apt install/update.""" + +DEFAULT_WARN = 90 # days +DEFAULT_CRIT = 365 # days + + +def parse_args(): + """Parse command line arguments using argparse. + """ + parser = argparse.ArgumentParser(description=DESCRIPTION) + + parser.add_argument( + '-V', '--version', + action='version', + version='%(prog)s: v{} by {}'.format(__version__, __author__), + ) + + parser.add_argument( + '-c', '--critical', + default=DEFAULT_CRIT, + dest='CRIT', + help='Set the critical threshold (in days). Default: %(default)s', + type=int, + ) + + parser.add_argument( + '-w', '--warning', + default=DEFAULT_WARN, + dest='WARN', + help='Set the warning threshold (in days). Default: %(default)s', + type=int, + ) + + return parser.parse_args() + + +def main(): + """The main function. Hier spielt die Musik. + """ + + # parse the command line, exit with UNKNOWN if it fails + try: + args = parse_args() + except SystemExit: + sys.exit(STATE_UNKNOWN) + + # execute the shell command and return its result and exit code + stdout, stderr, retc = lib.base.coe(lib.shell.shell_exec( + "dpkg-query --show --showformat='${db-fsys:Last-Modified} ${Package}\n'" + )) + if stderr: + lib.base.cu(stderr) + + # stdout: 1680072089 adduser\n1680072121 apparmor\n1680072091 apt\n + try: + last_activity = lib.txt.mltext2array(stdout, skip_header=False, sort_key=0) + last_activity = int(last_activity[-1][0]) + except: + lib.base.cu('Unable to get dpkg-query info, maybe an update never happened.') + + # calculating the final check state + delta = lib.time.now() - last_activity + state = lib.base.get_state(delta, args.WARN * 24 * 60 * 60, args.CRIT * 24 * 60 * 60) + + # over and out + lib.base.oao( + 'Last package manager activity is {} ago{} (thresholds {}D/{}D).'.format( + lib.human.seconds2human(delta), + lib.base.state2str(state, prefix=' '), + args.WARN, + args.CRIT, + ), + state, + ) + + +if __name__ == '__main__': + try: + main() + except Exception: # pylint: disable=W0703 + lib.base.cu() diff --git a/check-plugins/deb-lastactivity/icingaweb2-module-director/deb-lastactivity.json b/check-plugins/deb-lastactivity/icingaweb2-module-director/deb-lastactivity.json new file mode 100644 index 00000000..b3618649 --- /dev/null +++ b/check-plugins/deb-lastactivity/icingaweb2-module-director/deb-lastactivity.json @@ -0,0 +1,110 @@ +{ + "Command": { + "cmd-check-deb-lastactivity": { + "arguments": { + "--critical": { + "value": "$deb_lastactivity_critical$" + }, + "--warning": { + "value": "$deb_lastactivity_warning$" + } + }, + "command": "/usr/lib64/nagios/plugins/deb-lastactivity", + "disabled": false, + "fields": [ + { + "datafield_id": 1, + "is_required": "n", + "var_filter": null + }, + { + "datafield_id": 2, + "is_required": "n", + "var_filter": null + } + ], + "imports": [], + "is_string": null, + "methods_execute": "PluginCheck", + "object_name": "cmd-check-deb-lastactivity", + "object_type": "object", + "timeout": 60, + "vars": {}, + "zone": null, + "uuid": "02f9ad07-230e-4e99-aa96-bdcb694a0620" + } + }, + "ServiceTemplate": { + "tpl-service-deb-lastactivity": { + "action_url": null, + "apply_for": null, + "assign_filter": null, + "check_command": "cmd-check-deb-lastactivity", + "check_interval": 86400, + "check_period": null, + "check_timeout": null, + "command_endpoint": null, + "disabled": false, + "display_name": null, + "enable_active_checks": null, + "enable_event_handler": null, + "enable_flapping": null, + "enable_notifications": true, + "enable_passive_checks": null, + "enable_perfdata": null, + "event_command": null, + "fields": [], + "flapping_threshold_high": null, + "flapping_threshold_low": null, + "groups": [], + "host": null, + "icon_image": "deb-lastactivity.png", + "icon_image_alt": null, + "imports": [ + "tpl-service-generic" + ], + "max_check_attempts": 5, + "notes": "Checks the timespan since the last package manager activity, for example due to an apt install/update.", + "notes_url": "https://github.com/Linuxfabrik/monitoring-plugins/tree/main/check-plugins/deb-lastactivity", + "object_name": "tpl-service-deb-lastactivity", + "object_type": "template", + "retry_interval": 60, + "service_set": null, + "template_choice": null, + "use_agent": null, + "use_var_overrides": null, + "vars": { + "criticality": "C", + "deb_lastactivity_critical": 365, + "deb_lastactivity_warning": 90 + }, + "volatile": null, + "zone": null, + "uuid": "062c6906-c02e-4b02-a36a-0fc4a30096eb" + } + }, + "Datafield": { + "1": { + "varname": "deb_lastactivity_critical", + "caption": "Deb Lastactivity: Critical", + "description": "Set the critical threshold (in days).", + "datatype": "Icinga\\Module\\Director\\DataType\\DataTypeString", + "format": null, + "settings": { + "visibility": "visible" + }, + "uuid": "202b7b3f-26a2-45df-b6e6-3c48e0d31fc3" + }, + "2": { + "varname": "deb_lastactivity_warning", + "caption": "Deb Lastactivity: Warning", + "description": "Set the warning threshold (in days).", + "datatype": "Icinga\\Module\\Director\\DataType\\DataTypeString", + "format": null, + "settings": { + "visibility": "visible" + }, + "uuid": "cca78b46-5874-4081-9731-ba946e3db5f7" + } + } +} diff --git a/check-plugins/deb-lastactivity/icingaweb2-module-director/deb-lastactivity.yml b/check-plugins/deb-lastactivity/icingaweb2-module-director/deb-lastactivity.yml new file mode 100644 index 00000000..11a4e268 --- /dev/null +++ b/check-plugins/deb-lastactivity/icingaweb2-module-director/deb-lastactivity.yml @@ -0,0 +1,5 @@ +--- +overwrites: + '["Command"]["cmd-check-deb-lastactivity"]["timeout"]': 60 + '["ServiceTemplate"]["tpl-service-deb-lastactivity"]["check_interval"]': 86400 + '["ServiceTemplate"]["tpl-service-deb-lastactivity"]["retry_interval"]': 60 diff --git a/check-plugins/deb-lastactivity/lib b/check-plugins/deb-lastactivity/lib deleted file mode 100644 index dc598c56..00000000 --- a/check-plugins/deb-lastactivity/lib +++ /dev/null @@ -1 +0,0 @@ -../lib \ No newline at end of file diff --git a/check-plugins/deb-lastactivity/lib b/check-plugins/deb-lastactivity/lib new file mode 120000 index 00000000..58677ddb --- /dev/null +++ b/check-plugins/deb-lastactivity/lib @@ -0,0 +1 @@ +../../lib \ No newline at end of file