From 0799b6fda01c4317dd8578f3a1f786a42c4c6e95 Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch>
Date: Wed, 4 Sep 2024 11:06:59 +0200
Subject: [PATCH] update appmonitor client

---
 .../appmonitor/git_update_appmonitor.sh       | 108 +++++++++++++++---
 .../plugins/apps/iml-appmonitor-server.php    |  18 +++
 .../plugins/apps/inc_appcheck_end.php         |  15 +++
 .../plugins/apps/inc_appcheck_start.php       |  40 +++++++
 .../appmonitor/plugins/apps/wordpress.php     | 101 ++++++++++++++++
 public_html/appmonitor/tests/test_plugins.php |  48 ++++++++
 6 files changed, 315 insertions(+), 15 deletions(-)
 create mode 100644 public_html/appmonitor/plugins/apps/inc_appcheck_end.php
 create mode 100644 public_html/appmonitor/plugins/apps/inc_appcheck_start.php
 create mode 100644 public_html/appmonitor/plugins/apps/wordpress.php
 create mode 100644 public_html/appmonitor/tests/test_plugins.php

diff --git a/public_html/appmonitor/git_update_appmonitor.sh b/public_html/appmonitor/git_update_appmonitor.sh
index 32f5b785..117d5084 100755
--- a/public_html/appmonitor/git_update_appmonitor.sh
+++ b/public_html/appmonitor/git_update_appmonitor.sh
@@ -1,7 +1,19 @@
 #!/bin/bash
 # ======================================================================
 #
-#   A P P M O N I T O R  ::  CLIENT - UPDATE
+#   
+#    _____ _____ __                   _____         _ _           
+#   |     |     |  |      ___ ___ ___|     |___ ___|_| |_ ___ ___ 
+#   |-   -| | | |  |__   | .'| . | . | | | | . |   | |  _| . |  _|
+#   |_____|_|_|_|_____|  |__,|  _|  _|_|_|_|___|_|_|_|_| |___|_|  
+#                            |_| |_|                              
+#                             _ _         _                                            
+#                         ___| |_|___ ___| |_                                          
+#                        |  _| | | -_|   |  _|                                         
+#                        |___|_|_|___|_|_|_|   
+#                                                                 
+#
+#                         INSTALLER + UPDATER
 #
 # This script will install or update the appmonitor client only.
 #
@@ -16,6 +28,7 @@
 # 2022-04-12  0.2  <axel.hahn@iml.unibe.ch>  add help; exclude unneeded files
 # 2022-05-03  0.3  <axel.hahn@iml.unibe.ch>  create general_include.php
 # 2024-07-25  0.4  <axel.hahn@iml.unibe.ch>  update quoting and comments
+# 2024-07-31  0.5  <axel.hahn@iml.unibe.ch>  Show more helpful information; wait on 1st install; added param -n
 # ======================================================================
 
 # ----------------------------------------------------------------------
@@ -23,12 +36,16 @@
 # ----------------------------------------------------------------------
 
 readonly git_repo_url="https://github.com/iml-it/appmonitor.git"
+readonly docs_url="https://os-docs.iml.unibe.ch/appmonitor/PHP_client/index.html"
 readonly line="______________________________________________________________________________"
-readonly version="0.4"
+readonly version="0.5"
 
 git_target=/tmp/git_data__appmonitor
 client_from="${git_target}/public_html/client"
 client_to="."
+isUpdate=0
+wait=1
+
 
 cd "$( dirname "$0" )" || exit 1
 
@@ -88,20 +105,22 @@ function _gitUpdate(){
 # ----------------------------------------------------------------------
 
 cat <<ENDOFHEADER
+$line
+
+    IML Appmonitor client   ::   installer + updater  v$version
+$line
 
-          +-----------------------------------+
-          |                                   |
-          |  INSTALLER  |                     |
-          |      +      |  Appmonitor client  |
-          |   UPDATER   |                     |
-          |                                   |
-          +--------------------------- v$version --+
 
 ENDOFHEADER
 
 case "$1" in
     -h|--help)
         cat <<ENDOFHELP
+    The IML Appmonitor is free software.
+
+        Source: https://github.com/iml-it/appmonitor
+        Docs: https://os-docs.iml.unibe.ch/appmonitor
+        License: GNU GPL 3.0
 
     This is a helper script to get the files of the IML Appmonitor
     client part only.
@@ -116,18 +135,26 @@ case "$1" in
     On additional runs it updates the files.
 
     USAGE:
+        $0 [OPTIONS] [TARGET]
 
-    $0 [target path]
+    OPTIONS:
+        -h|--help
+            Show this help and exit
+        -n|--nowait
+            Do not wait for RETURN on 1st installation.
+            Use it for an unattended installation.
 
-        default target is [.] (current directory)
-
-    $0 -h|--help
-
-        Show this help.
+    PARAMETERS:
+        TARGET 
+            optional target path for the client files
+            default target is "." (current directory)
 
 ENDOFHELP
         exit 0
         ;;
+    -n|--nowait)
+        wait=0
+        ;;
     *)
         if test -n "$1" 
             then
@@ -144,6 +171,32 @@ esac
 which rsync >/dev/null || exit 1
 which git >/dev/null || exit 1
 
+test -f general_include.php && isUpdate=1
+
+if [ $isUpdate -eq 0 ]; then
+    cat <<WELCOME
+    Welcome to the Appmonitor client installation!
+
+
+    This is a helper script to get the client files of the IML Appmonitor.
+    They will be installed into the directory "$client_to" $( test "$client_to" = "." && (echo; echo -n "    "; pwd) )
+
+        If this is not correct, press Ctrl + C to abort and use a
+        parameter to set another target directory.
+
+        "$( basename "$0" ) -h" shows a help and more options.
+
+
+WELCOME
+    if [ $wait -eq 1 ]; then
+        echo -n "    RETURN to continue ... "
+        read -r
+    fi
+else
+    echo "Updating local files ..."
+fi
+echo
+
 echo $line
 echo ">>> #1 of 3 >>> update local git data"
 echo
@@ -179,7 +232,32 @@ echo
 diff --color -r "$client_from" "$client_to"
 echo
 
+if [ $isUpdate -eq 0 ]; then
+    _fileupdate index.sample.php
+    cat <<INTRODUCTION
+$line
+
+
+    DONE!
+    The Appmonitor client was installed.
+
+    - Please edit index.php and general_include.php.
 
+    - If you have multiple applications below webroot then you can 
+      rename the file index.php to check-[appname].php eg.
+      check-cms.php, check-blog.php, ... 
+
+    - Start "$( basename "$0" )" again to perform an update.
+      Maybe you want to create a cronjob for this.
+
+INTRODUCTION
+else
+    echo "Appmonitor client was updated."
+fi
+echo
+
+echo "Documentation: $docs_url"
+echo
 echo $line
 echo done.
 
diff --git a/public_html/appmonitor/plugins/apps/iml-appmonitor-server.php b/public_html/appmonitor/plugins/apps/iml-appmonitor-server.php
index 0c468221..01797f87 100755
--- a/public_html/appmonitor/plugins/apps/iml-appmonitor-server.php
+++ b/public_html/appmonitor/plugins/apps/iml-appmonitor-server.php
@@ -163,6 +163,24 @@ $oMonitor->addCheck(
 // ----------------------------------------------------------------------
 include 'shared_check_ssl.php';
 
+
+$oMonitor->addCheck(
+    [
+        "name" => "plugin Load",
+        "description" => "current load",
+        "group" => 'monitor',
+        "parent" => false,
+        "check" => [
+            "function" => "Loadmeter",
+            "params" => [
+                "warning" => 1.0,
+                "error" => 3,
+            ],
+        ],
+        "worstresult" => RESULT_OK
+    ]
+);
+
 // ----------------------------------------------------------------------
 // plugin test
 // ----------------------------------------------------------------------
diff --git a/public_html/appmonitor/plugins/apps/inc_appcheck_end.php b/public_html/appmonitor/plugins/apps/inc_appcheck_end.php
new file mode 100644
index 00000000..5f958628
--- /dev/null
+++ b/public_html/appmonitor/plugins/apps/inc_appcheck_end.php
@@ -0,0 +1,15 @@
+<?php
+/* ______________________________________________________________________
+ * 
+ * A P P M O N I T O R  ::  CLIENT - INCLUDE FOR APP CHECKS :: ON END
+ * ______________________________________________________________________
+ */
+
+// $bStandalone was set in inc_appcheck_start.php
+// send response if client was not initialized there
+if($bStandalone){
+    $oMonitor->setResult();
+    $oMonitor->render();
+}
+
+// ----------------------------------------------------------------------
diff --git a/public_html/appmonitor/plugins/apps/inc_appcheck_start.php b/public_html/appmonitor/plugins/apps/inc_appcheck_start.php
new file mode 100644
index 00000000..93e17e63
--- /dev/null
+++ b/public_html/appmonitor/plugins/apps/inc_appcheck_start.php
@@ -0,0 +1,40 @@
+<?php
+/* ______________________________________________________________________
+ * 
+ * A P P M O N I T O R  ::  CLIENT - INCLUDE FOR APP CHECKS :: ON START
+ * ______________________________________________________________________
+ */
+
+// ----------------------------------------------------------------------
+// CHECK IF THE APPROOT IS SET
+// ----------------------------------------------------------------------
+
+if (!$sApproot) {
+    header('HTTP/1.0 503 Service Unavailable');
+    echo "<h1>503 Service Unavailable</h1>";
+    echo 'ERROR:'.PHP_EOL;
+    echo '$sApproot was not set. Define it before including the application check.'.PHP_EOL;
+    echo 'Set the base folder of your application installation.'.PHP_EOL;
+    echo PHP_EOL;
+    echo 'Example:'.PHP_EOL;
+    echo '$sApproot = $_SERVER[\'DOCUMENT_ROOT\'];'.PHP_EOL;
+    echo '$sApproot = $_SERVER[\'DOCUMENT_ROOT\'].\'/myapp\';'.PHP_EOL;
+    die();
+}
+
+// initialize client and set very basic metadata ... if needed
+$bStandalone=!(class_exists('appmonitor') && isset($oMonitor));
+if($bStandalone){
+    require_once(__DIR__.'/../../classes/appmonitor-client.class.php');
+    $oMonitor = new appmonitor();
+    $oMonitor->setWebsite('Wordpress Instance');
+
+    @include __DIR__.'/../../general_include.php';
+}
+
+// ----------------------------------------------------------------------
+// FUNCTIONS
+// ----------------------------------------------------------------------
+
+
+// ----------------------------------------------------------------------
diff --git a/public_html/appmonitor/plugins/apps/wordpress.php b/public_html/appmonitor/plugins/apps/wordpress.php
new file mode 100644
index 00000000..711a15fc
--- /dev/null
+++ b/public_html/appmonitor/plugins/apps/wordpress.php
@@ -0,0 +1,101 @@
+<?php
+/* ______________________________________________________________________
+ * 
+ * WORK IN PROGRESS
+ * 
+ * A P P M O N I T O R  ::  CLIENT - CHECK
+ * ______________________________________________________________________
+ * 
+ * Check for a Wordpress instance.
+ * Blogsoftware https://wordpress.org/
+ * 
+ * It checks 
+ * - the write access to the config file
+ * - connect to mysql database (which is read from config)
+ * - ssl certificate (on https request only)
+ * 
+ * @author: <axel.hahn@unibe.ch>
+ * ----------------------------------------------------------------------
+ * 2018-11-07  v0.01
+ * 2019-05-24  v0.02  detect include or standalone mode
+ * 2019-05-24  v0.03  detect include or standalone mode
+ * 2024-07-31  v0.04  first version for wordpress check in plugins/apps/ 
+ */
+
+
+require 'inc_appcheck_start.php';
+
+// ----------------------------------------------------------------------
+// Read config items
+// ----------------------------------------------------------------------
+
+$sConfigfile = $sApproot . '/wp-config.php';
+if (!file_exists($sConfigfile)) {
+    header('HTTP/1.0 503 Service Unavailable');
+    die('ERROR: Config file [wp-config.php] was not found. Set a correct $sApproot pointing to wordpress install dir.');
+}
+
+require($sConfigfile);
+$aDb=[
+  'server'   => DB_HOST,
+  'username' => DB_USER,
+  'password' => DB_PASSWORD,
+  'database' => DB_NAME,
+  // 'port'     => ??,
+]; 
+
+// ----------------------------------------------------------------------
+// checks
+// ----------------------------------------------------------------------
+
+$oMonitor->addCheck(
+    [
+        "name" => "config file",
+        "description" => "The config file must be writable",
+        "check" => [
+            "function" => "File",
+            "params" => [
+                "filename" => $sConfigfile,
+                "file" => true,
+                "readable" => true,
+                "writable" => true,
+            ],
+        ],
+    ]
+);
+
+$oMonitor->addCheck(
+    [
+        "name" => "Mysql Connect",
+        "description" => "Connect mysql server " . $aDb['server'] . " as user " . $aDb['username'] . " to scheme " . $aDb['database'],
+        "parent" => "config file",
+        "check" => [
+            "function" => "MysqlConnect",
+            "params" => [
+                "server"   => $aDb['server'],
+                "user"     => $aDb['username'],
+                "password" => $aDb['password'],
+                "db"       => $aDb['database'],
+                // "port"     => $aDb['port'],
+            ],
+        ],
+    ]
+);
+
+if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']){
+    $oMonitor->addCheck(
+        [
+            "name" => "Certificate check",
+            "description" => "Check if SSL cert is valid and does not expire soon",
+            "check" => [
+                "function" => "Cert",
+            ],
+        ]
+    );
+}
+
+// ----------------------------------------------------------------------
+
+require 'inc_appcheck_end.php';
+
+// ----------------------------------------------------------------------
diff --git a/public_html/appmonitor/tests/test_plugins.php b/public_html/appmonitor/tests/test_plugins.php
new file mode 100644
index 00000000..bd62fa4f
--- /dev/null
+++ b/public_html/appmonitor/tests/test_plugins.php
@@ -0,0 +1,48 @@
+<?php
+/*
+    TEST CLIENT CHECKS
+*/
+
+// ----------------------------------------------------------------------
+// INIT
+$sApproot = str_replace('\\', '/', dirname(__DIR__));
+
+
+// require_once(__DIR__.'/../classes/client_all_in_one.php');
+// echo "OK: file client_all_in_one.php was loaded\n";
+
+require_once(__DIR__.'/../classes/appmonitor-client.class.php');
+
+$oMonitor = new appmonitor();
+
+$oMonitor->listChecks();
+
+
+echo "OK: class appmonitor was initialized\n";
+
+
+// ----------------------------------------------------------------------
+$oMonitor->addTag('monitoring');
+
+// ----------------------------------------------------------------------
+$oMonitor->addCheck(
+    [
+        "name" => "check config subdir",
+        "description" => "Check config target directory",
+        "check" => [
+            "function" => "File",
+            "params" => [
+                "filename" => $sApproot . "/server/config",
+                "dir" => true,
+                "writable" => true,
+            ],
+        ],
+    ]
+);
+echo "OK: the plugin File check was added.\n";
+
+// ----------------------------------------------------------------------
+$oMonitor->setResult();
+echo "OK: setResult() was executed.\n";
+
+// ----------------------------------------------------------------------
-- 
GitLab