Check Systemd unit

Introduction

This check shows the status of a systemd unit.

A unit is everything listed by systemctl command - services, timers, targets, …

Requirements

  • systemctl binary (which is on each systemd based linux system)

Syntax

______________________________________________________________________

CHECK_SYSTEMDUNIT
v1.7

(c) Institute for Medical Education - University of Bern
Licence: GNU GPL 3

https://os-docs.iml.unibe.ch/icinga-checks/Checks/check_systemdunit.html
______________________________________________________________________

Check a unit using systemctl status.

The status is "unknown" if the command systemctl is not found.
The status is "critical" if the service does not exist or is not running.

When checking a service with multiple instances you get status "warning"
if not all instances are active.

SYNTAX:
  check_systemdunit [-h|-l|-s|-r] UNIT

OPTIONS:

  -h     this help
  -l     list all units
  -s     list service units
  -r     handle UNIT as a regex and search for a single unit. A uniq regex
         is needed to match a single unit. The initial idea was to match a
         servie that has different service names on differen os eg.
         - apache2 vs httpd
         - mysld vs mariadb

  UNIT   Name of a unit - see output of 'systemctl' 

EXAMPLES:

  check_systemdunit -s
         list all existing services. For a unit check you need to add the name
         in the 1st column.

  check_systemdunit nginx.service
         show status of nginx webservice

  check_systemdunit -r "(apache2|httpd)\.service"
         Detect name of Apache httpd service by given regex and show status
         of the found service name

  check_systemdunit something@*
         Check if all instances of a service "something@.service" are active.
         If not all instances are running you get status "warning", if none 
         is running "critical".

  check_systemdunit something@2.service
         Check instance 2 of service "something".

Examples

List services

You maybe want to start to get a list of services to pick an existing one that you wanna check periodically.

You can use systemctl --no-legend --no-pager --type service or $ ./check_systemdunit -s

List of service units:

  alsa-restore.service                                  loaded active exited  Save/Restore Sound Card State
  apparmor.service                                      loaded active exited  Load AppArmor profiles
  avahi-daemon.service                                  loaded active running Avahi mDNS/DNS-SD Stack
  bluetooth.service                                     loaded active running Bluetooth service
  clamav-daemon.service                                 loaded active running Clam AntiVirus userspace daemon
  clamav-freshclam.service                              loaded active running ClamAV virus database updater
  colord.service                                        loaded active running Manage, Install and Generate Color Profiles
  cronie.service                                        loaded active running Periodic Command Scheduler
  ...

Other units

With $ ./check_systemdunit -l you get a grouped list of all unit types. check_systemdunit handles all types - not only services.

Check a service

To check a single service you need to add the unit name in the 1st column.

$ ./check_systemdunit nginx returns

OK: nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: disabled)
     Active: active (running) since Fri 2023-10-20 08:04:35 CEST; 3h 14min ago
    Process: 783 ExecStart=/usr/bin/nginx (code=exited, status=0/SUCCESS)
   Main PID: 787 (nginx)
      Tasks: 2 (limit: 18881)
     Memory: 3.8M
        CPU: 39ms
     CGroup: /system.slice/nginx.service
             -787 "nginx: master process /usr/bin/nginx"
             -788 "nginx: worker process"
...

If a service does not exist: ./check_systemdunit justadummy returns

CRITICAL: Unit justadummy.service could not be found.

Regex examples

Here are a few examples for services with regex:

  • different names on different os:
    • check_systemdunit -r '(apache2|httpd)\.service'
  • Some operateing write a service with “d” - some not. To define the service that can have the “d” or not
    • check_systemdunit -r 'nmb[d]{0,1}.service'
    • check_systemdunit -r 'smb[d]{0,1}.service'
    • check_systemdunit -r 'ssh[d]{0,1}.service'
  • Placeholders for version numbers
    • check_systemdunit -r 'php.*fpm\.service'

Check a service with multiple instances

Systemd services with multiple instances can be detected automatically. In the list of the systemctl command the an instance by a number of the instance in the unit name.

All instances

To check if all instances are running use * at the end of the servicename (like you would do with systemctl status myservice*).

The command check_systemdunit myservice* will return a status line how many active and existing instanecs were found:

OK: 4 of 4 myservice@* units are active
...

A single instance

To check if all instances are running use myservice[number] at the end of the servicename.

The command check_systemdunit myservice2 checks the 2nd instance. It is handled like single service check.