Use php curl library calls to access emthtpd via socket.

Connect emhttpd events to websocket publisher endpoints.
This commit is contained in:
Tom Mortensen
2017-05-22 14:07:21 -07:00
parent dbf84c7b7d
commit e7cee3ca1c
15 changed files with 96 additions and 80 deletions
+1 -1
View File
@@ -4,7 +4,7 @@
<!ENTITY name "dynamix">
<!ENTITY author "Bergware">
<!ENTITY branch "master">
<!ENTITY version "2017.05.16"> <!-- Intended new version of Dynamix webGUI -->
<!ENTITY version "2017.05.22"> <!-- Intended new version of Dynamix webGUI -->
<!ENTITY unRAID "6.4.0"> <!-- Change to new unRAID version when issued together -->
<!ENTITY pluginURL "https://raw.github.com/limetech/webgui/&branch;/plugins/&name;/&name;.plg">
]>
+7
View File
@@ -0,0 +1,7 @@
#!/bin/bash
# daemonize the 'diskload' script
DAEMON="/usr/local/emhttp/webGui/scripts/diskload"
if [[ "$(pgrep -f $DAEMON)" == "" ]]; then
logger "Starting $DAEMON"
$DAEMON &>/dev/null &
fi
-2
View File
@@ -1,2 +0,0 @@
#!/bin/bash
/usr/local/emhttp/webGui/scripts/rc.diskload start >/dev/null
+10
View File
@@ -0,0 +1,10 @@
#!/usr/bin/php
<?PHP
// The cpuload.ini file is generated by emhttpd just before the 'heartbeat' event callout.
// todo: having emhttpd directly publish json would be more efficient
require_once "/usr/local/emhttp/webGui/include/publish.php";
$data = @parse_ini_file("/var/local/emhttp/cpuload.ini", true);
if ($data !== FALSE) {
publish("cpuload", json_encode($data, JSON_NUMERIC_CHECK));
}
?>
+26
View File
@@ -0,0 +1,26 @@
#!/usr/bin/php
<?PHP
// todo: this should generate/publish json-encoded string and let subscriber handle html
require_once "/usr/local/emhttp/webGui/include/publish.php";
$var = @parse_ini_file("/var/local/emhttp/var.ini");
$fsState=$var['fsState'];
if ($fsState == "Stopped")
$state = "<span class='red strong'>Array Stopped</span>";
elseif ($fsState == "Starting")
$state = "<span class='orange strong'>Array Starting</span>";
else {
$state = "<span class='green strong'>Array Started</span>";
$mdResync = $var['mdResync'];
if ($mdResync > 0) {
$mdResyncAction = $var['mdResyncAction'];
if (strstr($mdResyncAction, "recon")) $action="Parity-Sync / Data-Rebuild";
elseif (strstr($mdResyncAction, "clear")) $action="Clearing";
elseif ($mdResyncAction == "check") $action="Read-Check";
elseif (strstr($mdResyncAction, "check")) $action="Parity-Check";
$mdResyncPos = $var['mdResyncPos'];
$progress = number_format($mdResyncPos/($mdResync/100+1),1);
$state .= "&bullet;<span class='orange strong'>$action $progress %</span>";
}
}
publish("watchdog", $state);
?>
+7
View File
@@ -0,0 +1,7 @@
#!/bin/bash
# stop diskload daemon
DAEMON="diskload"
if [[ "$(pgrep $DAEMON)" != "" ]]; then
logger "Stopping $DAEMON"
pkill $DAEMON
fi
-2
View File
@@ -1,2 +0,0 @@
#!/bin/bash
/usr/local/emhttp/webGui/scripts/rc.diskload stop >/dev/null
+33
View File
@@ -0,0 +1,33 @@
<?PHP
/* Copyright 2005-2017, Lime Technology
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*/
?>
<?
function curl_socket($socket, $url, $postdata = NULL)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_UNIX_SOCKET_PATH, $socket);
if ($postdata !== NULL) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
curl_close($ch);
}
function refresh_emhttp_state()
{
curl_socket("/var/run/emhttpd.socket", "http://localhost/status.htm");
}
function publish($endpoint, $message)
{
curl_socket("/var/run/nginx.socket", "http://localhost/pub/$endpoint?buffer_length=1", $message);
}
?>
+3 -2
View File
@@ -1,4 +1,5 @@
#!/bin/bash
# todo: this should generate/publish json-encoded string
ini=/var/local/emhttp/diskload.ini
declare -a sector reads writes
@@ -12,7 +13,7 @@ for dev in $(awk '/(sd[a-z]*|nvme[0-9]n1) /{print $3}' /proc/diskstats); do
((c++))
done
# start daemon
# poll
while :; do
stats=($(awk '/(sd[a-z]*|nvme[0-9]n1) /{print $3,$6,$10,$4,$8}' /proc/diskstats))
c=0; s=${#stats[@]}
@@ -26,4 +27,4 @@ while :; do
((c++))
done
sleep $t
done &
done
+2 -1
View File
@@ -12,7 +12,8 @@
*/
?>
<?
exec("curl -s --unix-socket /var/run/emhttpd.socket http://localhost/status.htm");
require_once "/usr/local/emhttp/webGui/include/publish.php";
refresh_emhttp_state();
$var = parse_ini_file("/var/local/emhttp/var.ini");
$devs = parse_ini_file("/var/local/emhttp/devs.ini",true);
$disks = parse_ini_file("/var/local/emhttp/disks.ini",true);
-30
View File
@@ -1,30 +0,0 @@
#!/bin/sh
script="diskload"
daemon="/usr/local/emhttp/webGui/scripts/$script"
case $1 in
start)
if [[ -z $(pgrep -f $daemon) ]]; then
$daemon 1>/dev/null 2>&1
echo "$script started"
else
echo "$script already running!"
fi
;;
stop)
if [[ -n $(pgrep -f $daemon) ]]; then
pkill -f $daemon 1>/dev/null 2>&1
timer=5
until [[ -z $(pgrep -f $daemon) || $timer -eq 0 ]]; do
((timer--))
sleep 1
done
echo "$script stopped"
else
echo "$script not running!"
fi
;;
*)
echo "Usage: $(basename $0) start|stop"
esac
+2 -1
View File
@@ -12,7 +12,8 @@
*/
?>
<?
exec("curl -s --unix-socket /var/run/emhttpd.socket http://localhost/status.htm");
require_once "/usr/local/emhttp/webGui/include/publish.php";
refresh_emhttp_state();
$var = parse_ini_file("/var/local/emhttp/var.ini");
$disks = parse_ini_file("/var/local/emhttp/disks.ini",true);
-36
View File
@@ -1,36 +0,0 @@
#!/bin/bash
PREV=
while :; do
. /var/local/emhttp/var.ini
case $fsState in
Stopped)
DATA="<span class='red strong'>Array Stopped</span>"
;;
Starting)
DATA="<span class='orange strong'>Array Starting</span>"
;;
*)
DATA="<span class='green strong'>Array Started</span>"
;;
esac
if [[ $mdResync -gt 0 ]]; then
MODE=
if [[ $mdResyncAction =~ recon ]]; then
MODE='Parity-Sync / Data-Rebuild'
elif [[ $mdResyncAction =~ clear ]]; then
MODE='Clearing'
elif [[ $mdResyncAction == check ]]; then
MODE='Read-Check'
elif [[ $mdResyncAction =~ check ]]; then
MODE='Parity-Check'
fi
p=$((mdResyncPos*1000/mdResync)); w=${p:0:-1}
DATA="$DATA&bullet;<span class='orange strong'>$MODE ${w:-0}.${p: -1} %</span>"
fi
if [[ $DATA != $PREV ]]; then
curl -s --unix-socket /var/run/nginx.socket -X POST -d "$DATA" http://localhost/pub/watchdog?buffer_length=1 &>/dev/null
PREV=$DATA
fi
sleep 3
done &
+2 -3
View File
@@ -16,6 +16,7 @@ $docroot = $_SERVER['DOCUMENT_ROOT'];
require_once "$docroot/webGui/include/Helpers.php";
require_once "$docroot/webGui/include/PageBuilder.php";
require_once "$docroot/webGui/include/publish.php";
// Extract the 'querystring'
// variables provided by emhttp:
@@ -33,9 +34,6 @@ if (empty($path)) {
}
}
// Tell emhttp to refresh vars
exec("curl -s --unix-socket /var/run/emhttpd.socket http://localhost/status.htm");
// The current "task" is the first element of the path
$task = strtok($path, '/');
@@ -43,6 +41,7 @@ $task = strtok($path, '/');
extract(parse_plugin_cfg('dynamix',true));
// Read emhttp status
refresh_emhttp_state();
$var = parse_ini_file('state/var.ini');
$sec = parse_ini_file('state/sec.ini',true);
$devs = parse_ini_file('state/devs.ini',true);