From 0ce33ddf7e4e4029f37d031799240c8287b2dc7b Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch>
Date: Thu, 12 Oct 2023 14:49:12 +0200
Subject: [PATCH] add check_journallog

---
 check_journallog | 130 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 130 insertions(+)
 create mode 100755 check_journallog

diff --git a/check_journallog b/check_journallog
new file mode 100755
index 0000000..5f85292
--- /dev/null
+++ b/check_journallog
@@ -0,0 +1,130 @@
+#!/bin/bash
+# ======================================================================
+#
+# Check count of written entries in journallog
+#
+# requirements:
+# - journalctl
+#
+# ----------------------------------------------------------------------
+# 2023-10-12  v0.1  <axel.hahn@unibe.ch>     initial version
+# ======================================================================
+
+
+. $( dirname $0 )/inc_pluginfunctions
+
+export self_APPVERSION=0.1
+
+tmpfile=/tmp/last_journallog_line_${USER}.txt
+
+# ----------------------------------------------------------------------
+# FUNCTIONS
+# ----------------------------------------------------------------------
+
+# show help text
+function showHelp(){
+    local _self; _self=$(basename $0)
+cat <<EOF
+$( ph.showImlHelpHeader )
+
+Show number of lines in journallog per min.
+A strong change of the written lines per min COULD indicate a problem.
+
+The status is ...
+- unknown - if journalctl is not available
+          - if the script is started the 1st time and stores the last 
+            line of the journallog
+- ok - when showing written tnries per min
+- warning/ critical - when giving -c and -w parameter values
+
+This plugin sends performancedata.
+
+SYNTAX:
+  $_self [-h] [-w WARN_LIMIT] [-c CRITICAL_LIMIT]
+
+OPTIONS:
+  -h                this help
+  -w VALUE          warning level  (default: 0)
+  -c VALUE          critical level (default: 0)
+
+PARAMETERS:
+  None.
+
+EXAMPLES:
+
+  $_self  show count of newly written log entries
+  $_self -w 100 -c 200
+                    Set warning level to 100 written lines per min and
+                    critical to 200.
+  $_self -w 100
+                    Set warning level to 100 written lines per min but
+                    no critical limit. This check then never will send a
+                    critical status.
+EOF
+}
+
+# ----------------------------------------------------------------------
+# MAIN
+# ----------------------------------------------------------------------
+
+# handle params
+ph.hasParamoption "h" "$@"; bOptHelp=$?
+
+if [ $bOptHelp -eq 0 ]; then
+    showHelp
+    exit 0
+fi
+
+ph.require journalctl
+
+typeset -i iWarnLimit=$(     ph.getValueWithParam 0 w "$@")
+typeset -i iCriticalLimit=$( ph.getValueWithParam 0 c "$@")
+
+
+lastLine=$( cat "$tmpfile" 2>/dev/null )
+if [ -z "$lastLine" ]; then
+    ph.setStatus unknown
+    ph.status "Initializing ... storing last line of jorunallog ..."
+    journalctl -n 1 > "$tmpfile"
+else
+    ts=$( cut -f 1-3 -d " " <<< "$lastLine" )
+    typeset -i iAge; iAge=$( ph.getFileAge "$tmpfile" )
+    data=$( journalctl --since "$ts" )
+
+    # for next run: insert last handled line
+    tail -1 <<< "$data" > "$tmpfile"
+
+    # detect last seen position and count lines from there
+    typeset -i iTotal; iTotal=$( wc -l <<< "$data" )
+    typeset -i iFrom;  iFrom=$( grep -Fn "$lastLine" <<< "$data" | head -1 | cut -f 1 -d ':' )
+    typeset -i iLines; iLines=$iTotal-$iFrom
+    typeset -i iLinespermin; iLinespermin=$iLines*60/$iAge
+
+    if [ $iWarnLimit -gt 0 -a $iWarnLimit -le $iLinespermin ]; then
+      ph.setStatus "warning"
+    fi
+    if [ $iCriticalLimit -gt 0 -a $iCriticalLimit -le $iLinespermin ]; then 
+      ph.setStatus "critical"
+    fi 
+
+    ph.perfadd "lines-per-min"    "${iLinespermin}"
+
+    # output
+    ph.status "Journallog stored $iLinespermin lines per min"
+    sLimits=""
+    test $iWarnLimit     -gt 0 && sLimits+="WARNING at $iWarnLimit"
+    test $iWarnLimit     -gt 0 || sLimits+="No warning level"
+    sLimits+="; "
+    test $iCriticalLimit -gt 0 && sLimits+="CRITICAL at $iCriticalLimit"
+    test $iCriticalLimit -gt 0 || sLimits+="No critical level "
+
+    echo "Limits: $sLimits"
+    # echo "Found $iLines new line(s) in the last $iAge sec"
+
+fi
+
+# cat "$tmpfile"
+
+ph.exit
+
+# ----------------------------------------------------------------------
-- 
GitLab