Skip to content
Snippets Groups Projects
Commit d229f3d7 authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

Merge branch 'add-systemdunit' into 'master'

Add systemdunit

See merge request !161
parents b46130be ca9cc579
No related branches found
No related tags found
1 merge request!161Add systemdunit
#!/bin/bash
# ================================================================================
#
# CHECK A SINGLE SYSTEMD SERVICE
#
# (1)
# shows overview of important services - edit check_systemdservices.cfg to define
# them
#
# (2)
# resturns
# - OK if all systemd servises are running
# - UNKNOWN if systemctl command is not available
# - ERROR if any systemd service is not running
#
# -------------------------------------------------------------------------------
# 2023-09-05 v01.0 <axel.hahn@unibe.ch>
# ================================================================================
. $( dirname $0 )/inc_pluginfunctions
export self_APPVERSION=1.0
# ----------------------------------------------------------------------
# FUNCTIONS
# ----------------------------------------------------------------------
# show help text
function showHelp(){
local _self; _self=$(basename $0)
cat <<EOF
$( ph.showImlHelpHeader )
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.
SYNTAX:
$_self [-h] UNIT
OPTIONS:
-h this help
UNIT Name of a unit - see output of 'systemctl'
EXAMPLES:
$_self mysql.service
show status of service mysql
EOF
}
# ----------------------------------------------------------------------
# MAIN
# ----------------------------------------------------------------------
ph.hasParamoption "h" "$@"; bOptHelp=$?
if [ $bOptHelp -eq 0 -o $# -eq 0 ]; then
showHelp
exit 0
fi
ph.require "systemctl"
_service="${1}"
_status=$( systemctl --no-pager -l status "${_service}" 2>&1 )
if ! grep "Active: active (running) " <<< "${_status}" >/dev/null; then
ph.setStatus critical
fi
ph.status "${_status}"
ph.exit
# ----------------------------------------------------------------------
......@@ -49,6 +49,7 @@ There is one include script used by all checks:
* check_ssl
* check_ssl_certs
* check_systemdservices
* [check_systemdunit](check_systemdunit.md)
* check_timesync
* check_uptime
* hello
# 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
## Syntax
```txt
______________________________________________________________________
CHECK_SYSTEMDUNIT
v1.0
(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.
SYNTAX:
check_systemdunit [-h] UNIT
OPTIONS:
-h this help
UNIT Name of a unit - see output of 'systemctl'
EXAMPLES:
check_systemdunit mysql.service
show status of service mysql
```
## Examples
``$ ./check_systemdunit nginx`` returns
```txt
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 Tue 2023-09-05 08:04:45 CEST; 8h ago
Process: 577 ExecStart=/usr/bin/nginx -g pid /run/nginx.pid; error_log stderr; (code=exited, status=0/SUCCESS)
Main PID: 612 (nginx)
Tasks: 2 (limit: 18881)
Memory: 3.0M
CPU: 38ms
CGroup: /system.slice/nginx.service
├─612 "nginx: master process /usr/bin/nginx -g pid /run/nginx.pid; error_log stderr;"
└─613 "nginx: worker process"
...
```
If a service does not exist: ``./check_systemdunit justadummy`` returns
```txt
CRITICAL: Unit justadummy.service could not be found.
```
......@@ -39,6 +39,7 @@
# 2023-06-22 v1.11 <axel.hahn@unibe.ch> fix ph.toUnit with float values; shell fixes
# 2023-08-24 v1.12 <axel.hahn@unibe.ch> toUnit got 3rd param for count of digits after "."
# 2023-08-30 v1.13 <axel.hahn@unibe.ch> reverse return code in ph.hasParamoption to unix like return codes: 0=true; <>0 = false
# 2023-09-05 v1.14 <axel.hahn@unibe.ch> ph.require - show error below status line
# ======================================================================
......@@ -62,10 +63,11 @@ function ph.abort(){
# check required binaries in the path
# param(s) string name of binary to check with "which" command
function ph.require(){
which $* >/dev/null
if [ $? -ne 0 ]; then
local _out;
if ! _out=$( which $* 2>&1 ); then
ph.setStatus "unknown"
ph.status "$0 requires the following tools to run: $*"
echo "$_out"
ph.exit
fi
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment