Skip to content

Commit

Permalink
deb-lastactivity: Improve after PR
Browse files Browse the repository at this point in the history
  • Loading branch information
markuslf committed Sep 16, 2023
1 parent 917df4f commit f7238f8
Show file tree
Hide file tree
Showing 6 changed files with 298 additions and 111 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
71 changes: 71 additions & 0 deletions check-plugins/deb-lastactivity/README.rst
Original file line number Diff line number Diff line change
@@ -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 <https://www.linuxfabrik.ch>`_
* License: The Unlicense, see `LICENSE file <https://unlicense.org/>`_.
219 changes: 109 additions & 110 deletions check-plugins/deb-lastactivity/deb-lastactivity
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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()
Loading

0 comments on commit f7238f8

Please sign in to comment.