From a3eb0b1fead4eb559d8e7f33f6416bb184e5d2d7 Mon Sep 17 00:00:00 2001
From: br0tkasten <arne@br0tkasten.de>
Date: Sun, 18 Feb 2024 12:48:18 +0100
Subject: [PATCH] update

---
 etc-monit/monit.d/01_httpd.cfg          |  3 --
 etc-monit/monit.d/99_checks.cfg         | 40 -------------------------
 etc/matrix.sh                           | 29 ++++++++++++++++++
 etc/monit.d/01_httpd.cfg                |  6 ++++
 {etc-monit => etc}/monit.d/02_alert.cfg |  0
 etc/monit.d/1_httpd.cfg                 |  7 +++++
 etc/monit.d/99_checks.cfg               | 37 +++++++++++++++++++++++
 {etc-monit => etc}/monitrc              |  0
 8 files changed, 79 insertions(+), 43 deletions(-)
 delete mode 100644 etc-monit/monit.d/01_httpd.cfg
 delete mode 100644 etc-monit/monit.d/99_checks.cfg
 create mode 100755 etc/matrix.sh
 create mode 100644 etc/monit.d/01_httpd.cfg
 rename {etc-monit => etc}/monit.d/02_alert.cfg (100%)
 create mode 100644 etc/monit.d/1_httpd.cfg
 create mode 100644 etc/monit.d/99_checks.cfg
 rename {etc-monit => etc}/monitrc (100%)

diff --git a/etc-monit/monit.d/01_httpd.cfg b/etc-monit/monit.d/01_httpd.cfg
deleted file mode 100644
index 02c542a..0000000
--- a/etc-monit/monit.d/01_httpd.cfg
+++ /dev/null
@@ -1,3 +0,0 @@
-set httpd 
-  port 2812
-  allow "192.168.178.0/24"
\ No newline at end of file
diff --git a/etc-monit/monit.d/99_checks.cfg b/etc-monit/monit.d/99_checks.cfg
deleted file mode 100644
index 3c54b1b..0000000
--- a/etc-monit/monit.d/99_checks.cfg
+++ /dev/null
@@ -1,40 +0,0 @@
-check host br0tkasten.de with address br0tkasten.de
-	if failed ping then alert
-
-check host git.br0tkasten.de with address git.br0tkasten.de
-	if failed port 443 protocol https then alert
-	if failed port 4848 protocol ssh then alert
-
-check host ha.br0tkasten.de with address ha.br0tkasten.de
-	if failed port 443 protocol https then alert
-
-check host jitsi.br0tkasten.de with address jitsi.br0tkasten.de
-	if failed port 443 protocol https then alert
-
-check host mail.br0tkasten.de with address mail.br0tkasten.de
-	if failed port 25 protocol smtp then alert
-	if failed port 143 protocol imap then alert
-	if failed port 443 protocol https then alert
-
-check host matrix.br0tkasten.de with address matrix.br0tkasten.de
-	if failed port 443 protocol https then alert
-
-check host motion.br0tkasten.de with address motion.br0tkasten.de
-	if failed port 443 protocol https then alert
-
-check host mrtg.br0tkasten.de with address mrtg.br0tkasten.de
-	if failed port 443 protocol https status = 401 then alert
-
-check host www.br0tkasten.de with address www.br0tkasten.de
-	if failed port 443 protocol https then alert
-
-check host www.br0tcraft.de with address www.br0tcraft.de
-	if failed port 443 protocol https then alert
-
-check host www.familienhilfe-baeumler.de with address www.familienhilfe-baeumler.de
-	if failed port 443 protocol https then alert
-
-check host www.somachtstefaniedas.de with address www.somachtstefaniedas.de
-	if failed port 443 protocol https then alert
-
-
diff --git a/etc/matrix.sh b/etc/matrix.sh
new file mode 100755
index 0000000..b9cb25f
--- /dev/null
+++ b/etc/matrix.sh
@@ -0,0 +1,29 @@
+#!/bin/sh
+
+baseurl="https://matrix.br0tkasten.de"
+roomId="!30E9BL9QJp3BJqWp:matrix.br0tkasten.de"
+
+message="${MONIT_HOST} (${MONIT_SERVICE}) ${MONIT_EVENT}: ${MONIT_DESCRIPTION}"
+
+if [ "x$message" = "x" ]
+then
+        exit 1
+fi
+
+# login
+token=$(curl --connect-timeout 5 -m 10 -s $baseurl/_matrix/client/r0/login -H 'Accept: application/json' -X POST -d @/etc/monit/matrix_login.json | jq -r .access_token)
+
+# send message
+msgId=$(date +%s%N)
+#curl --connect-timeout 5 -m 10 -s -XPUT -H "Content-Type: application/json;charset=utf-8"  -d "{ \"msgtype\": \"m.text\", \"body\": \"$message\" }" "$baseurl/_matrix/client/r0/rooms/$roomId/send/m.room.message/$msgId?access_token=$token" >/dev/null
+curl --connect-timeout 5 -m 10 -s -XPUT -H "Content-Type: application/json;charset=utf-8" "$baseurl/_matrix/client/r0/rooms/$roomId/send/m.room.message/$msgId?access_token=$token" --data-binary @- << EOF >/dev/null
+{
+  "msgtype": "m.text",
+  "body": "$message"
+}
+EOF
+
+# logout
+curl --connect-timeout 5 -m 10 -s $baseurl/_matrix/client/r0/logout?access_token=$token -X POST >/dev/null
+
+logger "alert: $message"
diff --git a/etc/monit.d/01_httpd.cfg b/etc/monit.d/01_httpd.cfg
new file mode 100644
index 0000000..bfae1df
--- /dev/null
+++ b/etc/monit.d/01_httpd.cfg
@@ -0,0 +1,6 @@
+set httpd 
+  port 2812
+  allow "192.168.178.0/24"
+  allow "10.4.0.1/32"
+  allow "10.3.0.1/32"
+  allow "10.5.0.0/24"
diff --git a/etc-monit/monit.d/02_alert.cfg b/etc/monit.d/02_alert.cfg
similarity index 100%
rename from etc-monit/monit.d/02_alert.cfg
rename to etc/monit.d/02_alert.cfg
diff --git a/etc/monit.d/1_httpd.cfg b/etc/monit.d/1_httpd.cfg
new file mode 100644
index 0000000..e2ad9fc
--- /dev/null
+++ b/etc/monit.d/1_httpd.cfg
@@ -0,0 +1,7 @@
+# Generated by entrypoint.sh
+# Generated by entrypoint.sh
+set httpd
+    port 2812
+    allow 127.0.0.1
+    allow "::1"
+    allow admin:buhNu0Oewais
diff --git a/etc/monit.d/99_checks.cfg b/etc/monit.d/99_checks.cfg
new file mode 100644
index 0000000..d29fd6b
--- /dev/null
+++ b/etc/monit.d/99_checks.cfg
@@ -0,0 +1,37 @@
+check host br0tkasten.de with address br0tkasten.de
+	if failed ping for 3 cycles then exec "/etc/monit/matrix.sh"
+
+check host git.br0tkasten.de with address git.br0tkasten.de
+	if failed port 443 protocol https for 3 cycles then exec "/etc/monit/matrix.sh"
+	if failed port 4848 protocol ssh for 3 cycles then exec "/etc/monit/matrix.sh"
+
+check host mail.br0tkasten.de with address mail.br0tkasten.de
+	if failed port 25 protocol smtp for 3 cycles then exec "/etc/monit/matrix.sh"
+	if failed port 143 protocol imap for 3 cycles then exec "/etc/monit/matrix.sh"
+	if failed port 443 protocol https for 3 cycles then exec "/etc/monit/matrix.sh"
+
+check host matrix.br0tkasten.de with address matrix.br0tkasten.de
+	if failed port 443 protocol https for 3 cycles then exec "/etc/monit/matrix.sh"
+
+check host motion.br0tkasten.de with address motion.br0tkasten.de
+	if failed port 443 protocol https for 3 cycles then exec "/etc/monit/matrix.sh"
+
+check host mrtg.br0tkasten.de with address mrtg.br0tkasten.de
+	if failed port 443 protocol https status = 401 for 3 cycles then exec "/etc/monit/matrix.sh"
+
+check host www.br0tkasten.de with address www.br0tkasten.de
+	if failed port 443 protocol https for 3 cycles then exec "/etc/monit/matrix.sh"
+
+check host www.br0tcraft.de with address www.br0tcraft.de
+	if failed port 443 protocol https for 3 cycles then exec "/etc/monit/matrix.sh"
+
+check host www.creative-crafter.de with address www.creative-crafter.de
+	if failed port 443 protocol https for 3 cycles then exec "/etc/monit/matrix.sh"
+
+check host www.familienhilfe-baeumler.de with address www.familienhilfe-baeumler.de
+	if failed port 443 protocol https for 3 cycles then exec "/etc/monit/matrix.sh"
+
+check host www.somachtstefaniedas.de with address www.somachtstefaniedas.de
+	if failed port 443 protocol https for 3 cycles then exec "/etc/monit/matrix.sh"
+
+
diff --git a/etc-monit/monitrc b/etc/monitrc
similarity index 100%
rename from etc-monit/monitrc
rename to etc/monitrc