From 43f6d6c36443ce687f231642b913d55fabe3ec0e Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch>
Date: Wed, 19 Feb 2025 16:06:02 +0100
Subject: [PATCH] timesync: add help page and doc page

---
 check_timesync                   | 77 ++++++++++++++++++--------
 docs/20_Checks/_index.md         |  2 +-
 docs/20_Checks/check_timesync.md | 95 ++++++++++++++++++++++++++++++++
 3 files changed, 150 insertions(+), 24 deletions(-)
 create mode 100644 docs/20_Checks/check_timesync.md

diff --git a/check_timesync b/check_timesync
index 70f3870..91df8e0 100755
--- a/check_timesync
+++ b/check_timesync
@@ -15,40 +15,74 @@
 # 2020-03-05  v1.1  <axel.hahn@iml.unibe.ch> switch to ph.* helper functions
 # 2020-05-13  v1.2  <axel.hahn@iml.unibe.ch> update pre detect output of timedatectl
 # 2021-08-19  v1.3  <martin.gasser@iml.unibe.ch> update for chrony with timedatectl 
-
+# 2025-02-19  v1.4  <axel.hahn@unibe.ch>         update help page
 # ======================================================================
 
-. `dirname $0`/inc_pluginfunctions
-myos=`ph.getOS`
+. $(dirname $0)/inc_pluginfunctions
+self_APPVERSION=1.4
+
+myos="$( ph.getOS )"
 tmpfile=/tmp/check_timesync.tmp
 
 # ----------------------------------------------------------------------
-# pre check if timedatectl is available
+# functions
 # ----------------------------------------------------------------------
-which timedatectl >/dev/null
-if [ $? -ne 0 ]; then
-  ph.abort "timesync UNKNOWN: timedatectl was not found ... maybe your OS ${myos} is not supported"
+
+# show help text
+function showHelp(){
+    local _self; _self=$(basename $0)
+cat <<EOF
+$( ph.showImlHelpHeader )
+
+Check if time snychronisation is active.
+It uses timedatectl and detects systemd timesyncd and chronyd.
+
+SYNTAX:
+
+  $_self [-h]
+
+OPTIONS:
+
+  -h or --help   show this help.
+
+PARAMETERS:
+
+  None.
+
+EOF
+}
+
+# ----------------------------------------------------------------------
+# check params
+# ----------------------------------------------------------------------
+
+if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
+  showHelp
+  exit 0;
 fi
 
+if [ -n "$*" ]; then
+  echo "ERROR: no parameter expected."
+  exit 1
+fi
 
 # ----------------------------------------------------------------------
 # check output of timedatectl command
 # ----------------------------------------------------------------------
 
-# ph.execIfReady "timedatectl status | grep '^NTP synchronized'" >/dev/null
-# echo ----- output of timedatectl status >$tmpfile
-# timedatectl status >>$tmpfile
+ph.require "timedatectl"
+
 ph.execIfReady "timedatectl show >$tmpfile; grep '^NTPSynchronized' $tmpfile >/dev/null"
 
-cat $tmpfile | grep "^NTPSynchronized" >/dev/null
-if [ $? -ne 0 ]; then
+if ! cat $tmpfile | grep "^NTPSynchronized" >/dev/null
+then
   ph.setStatus "unknown"
   # ph.status "timesync: timedatectl has no line NTP synchronized ... maybe your OS ${myos} is not supported"
   ph.status "timesync: timedatectl has no line NTP synchronized"
   cat $tmpfile
 else
-  cat $tmpfile | grep "^NTPSynchronized=yes" >/dev/null
-  if [ $? -eq 0 ]; then
+  if cat $tmpfile | grep "^NTPSynchronized=yes" >/dev/null
+  then
     ph.status "timesync: a timesync service is active on this ${myos} host"
   else
     ph.setStatus "critical"
@@ -60,17 +94,14 @@ fi
 # ----------------------------------------------------------------------
 # detect sync service and show its output
 # ----------------------------------------------------------------------
+
 echo >>$tmpfile
 sSyncService=
-if [ -f /etc/systemd/timesyncd.conf ]; then
-  sSyncService="systemd-timesyncd"
-fi
-if [ -f /etc/chrony.conf ]; then
-  sSyncService="chronyd"
-fi
-if [ -f /etc/chrony/chrony.conf ]; then
-  sSyncService="chronyd"
-fi
+
+test -f /etc/systemd/timesyncd.conf && sSyncService="systemd-timesyncd"
+test -f /etc/chrony.conf && sSyncService="chronyd"
+test -f /etc/chrony/chrony.conf && sSyncService="chronyd"
+
 if [ -z $sSyncService ]; then
   echo "REMARK: no sync service detected ... or this sensu check does not support it" >>$tmpfile
 else
diff --git a/docs/20_Checks/_index.md b/docs/20_Checks/_index.md
index ca5cc74..f79f96c 100644
--- a/docs/20_Checks/_index.md
+++ b/docs/20_Checks/_index.md
@@ -51,6 +51,6 @@
 * [check_ssl_certs](check_ssl_certs.md)
 * check_systemdservices
 * [check_systemdunit](check_systemdunit.md)
-* check_timesync
+* [check_timesync](check_timesync.md)
 * check_uptime
 * hello
diff --git a/docs/20_Checks/check_timesync.md b/docs/20_Checks/check_timesync.md
new file mode 100644
index 0000000..54015dc
--- /dev/null
+++ b/docs/20_Checks/check_timesync.md
@@ -0,0 +1,95 @@
+## Check timesync
+
+Script: `check_timesync`
+
+**check_timesync** is a plugin to check if the time syncronisation is active.
+
+## Requirements
+
+* timedatectl
+* ntp deamon: systemd timesyncd or chronyd
+
+## Standalone installation
+
+From this repository ypu need next to this script:
+
+* `inc_pluginfunctions` shared function for all IML checks written in bash
+
+## Syntax
+
+Start the script with `-h` or `--help` to get the help.
+
+```txt
+______________________________________________________________________
+
+CHECK_TIMESYNC
+v1.4
+
+(c) Institute for Medical Education - University of Bern
+Licence: GNU GPL 3
+
+https://os-docs.iml.unibe.ch/icinga-checks/Checks/check_timesync.html
+______________________________________________________________________
+
+Check if time snychronisation is active.
+It uses timedatectl and detects systemd timesyncd and chronyd.
+
+SYNTAX:
+
+  check_timesync [-h]
+
+OPTIONS:
+
+  -h or --help   show this help.
+
+PARAMETERS:
+
+  None.
+
+```
+
+## Examples
+
+No parameter is needed.
+
+`./check_timesync`
+
+returns
+
+```txt
+OK: timesync: a timesync service is active on this manjaro host
+NTPSynchronized=yes
+
+Timezone=Europe/Zurich
+LocalRTC=no
+CanNTP=yes
+NTP=yes
+NTPSynchronized=yes
+TimeUSec=Wed 2025-02-19 15:58:54 CET
+RTCTimeUSec=Wed 2025-02-19 15:58:55 CET
+
+----- output of systemctl status systemd-timesyncd
+● systemd-timesyncd.service - Network Time Synchronization
+     Loaded: loaded (/usr/lib/systemd/system/systemd-timesyncd.service; enabled; preset: enabled)
+     Active: active (running) since Wed 2025-02-19 08:40:28 CET; 7h ago
+ Invocation: 46d0b3bfae1042e0b650fe8f993b5004
+       Docs: man:systemd-timesyncd.service(8)
+   Main PID: 531 (systemd-timesyn)
+     Status: "Contacted time server 185.54.81.25:123 (2.manjaro.pool.ntp.org)."
+      Tasks: 2 (limit: 36694)
+     Memory: 2.2M (peak: 3.4M)
+        CPU: 117ms
+     CGroup: /system.slice/systemd-timesyncd.service
+             └─531 /usr/lib/systemd/systemd-timesyncd
+
+Feb 19 08:40:28 axels-laptop systemd[1]: Started Network Time Synchronization.
+Feb 19 08:40:29 axels-laptop systemd-timesyncd[531]: Network configuration changed, trying to establish connection.
+Feb 19 08:40:29 axels-laptop systemd-timesyncd[531]: Network configuration changed, trying to establish connection.
+Feb 19 08:40:29 axels-laptop systemd-timesyncd[531]: Network configuration changed, trying to establish connection.
+Feb 19 08:40:29 axels-laptop systemd-timesyncd[531]: Network configuration changed, trying to establish connection.
+Feb 19 08:40:30 axels-laptop systemd-timesyncd[531]: Network configuration changed, trying to establish connection.
+Feb 19 08:40:30 axels-laptop systemd-timesyncd[531]: Network configuration changed, trying to establish connection.
+Feb 19 08:40:30 axels-laptop systemd-timesyncd[531]: Network configuration changed, trying to establish connection.
+Feb 19 08:41:00 axels-laptop systemd-timesyncd[531]: Contacted time server 185.54.81.25:123 (2.manjaro.pool.ntp.org).
+Feb 19 08:41:00 axels-laptop systemd-timesyncd[531]: Initial clock synchronization to Wed 2025-02-19 08:41:00.942942 CET.
+```
-- 
GitLab