Showing 1 changed files with 40 additions and 22 deletions
+40 -22
monitoring.d/raspberrypi
... ...
@@ -20,28 +20,46 @@ function cherrymusic () {
20 20
 }
21 21
 
22 22
 function webapps () {
23
-    local webapps_conf="$dir/${HOSTNAME}.webapps.conf"
24
-    if test -r $webapps_conf; then
25
-        local useragent=
26
-        local host=
27
-        local proto=
28
-        local webapps=()
29
-        local no_access=()
30
-        eval "$(< $webapps_conf)"
31
-        test -z "$useragent" && echo "manque user-agent" && return
32
-        test -z "$host" && echo "manque hôte" && return
33
-        test -z "$proto" && echo "manque protocole" && return
34
-        test $(( ${#webapps[@]} % 3 )) -ne 0 && echo "mauvais nombre de paramètres de tests" && return
35
-        for ((i=0; i<${#webapps[@]}; i+=3)); do
36
-            http_response=$(curl --silent --max-time 2 --user-agent "$useragent" --output /dev/null --write-out "%{response_code}" $proto://$host/${webapps[i]})
37
-            test $http_response -ne ${webapps[i+2]} && no_access[${#no_access[@]}]="${webapps[i+1]} ($http_response)"
38
-        done
39
-        if test ${#no_access[@]} -ne 0; then
40
-            echo "réponse incorrecte: ${no_access[@]}"
41
-        fi
42
-    else
43
-        echo "pas de conf! ($webapps_conf)"
44
-    fi
23
+    awk -v sq="'" '
24
+        /^[^#]+#\s*monitoring\s/ {
25
+            if ($1 == "listen") {
26
+                port = ":" gensub(";", "", "", $2)
27
+                proto = gensub(";", "", "", $3) == "ssl" ? "https" : "http"
28
+                pname = gensub(/^\s*listen\s.+#\s*monitoring\s+/,"", "")
29
+                if (proto == "https" && port == ":443") port = ""
30
+            }
31
+            if ($1 == "server_name") {
32
+                server_name = gensub(";", "", "", $2)
33
+                sname = gensub(/^\s*server_name\s.+#\s*monitoring\s+/,"", "")
34
+            }
35
+            if ($1 == "location" && sname != "" && pname != "" && sname == pname) {
36
+                l=gensub(/^\s*location\s[^\/]*(\/.*)\{\s*#\s*monitoring\s+(.+)$/,"\\1,\\2", "")
37
+                split(l, location, /\s*,\s*/)
38
+                if (location[2] == sname) {
39
+                    monitoring[location[2]][location[1]]["monitoring"] = location[3]
40
+                    monitoring[location[2]][location[1]]["response_code"] = location[4]
41
+                    monitoring[location[2]][location[1]]["url"] = proto "://" server_name port location[1] location[5]
42
+                    if (location[6] != "")
43
+                        monitoring[location[2]][location[1]]["user-agent"] = "--user-agent " sq location[6] sq
44
+                    else
45
+                        monitoring[location[2]][location[1]]["user-agent"] = "--user-agent " sq "Mozilla/5 (monitoring)" sq
46
+                }
47
+            }
48
+        }
49
+        END {
50
+            for (mid in monitoring) {
51
+                for (locid in monitoring[mid]) {
52
+                    curl = sprintf("curl --silent --max-time 2 --output /dev/null --write-out %{response_code} --output /dev/null %s %s",
53
+                          monitoring[mid][locid]["user-agent"],
54
+                          monitoring[mid][locid]["url"])
55
+                    curl | getline response_code
56
+                    close(curl)
57
+                    if (response_code != monitoring[mid][locid]["response_code"])
58
+                        printf("%s (%s)\n", monitoring[mid][locid]["monitoring"], response_code)
59
+                }
60
+            }
61
+        }
62
+    ' /etc/nginx/sites-enabled/*
45 63
 }
46 64
 
47 65
 function core_temp () {