minor changes and init.d script for websocket engine

This commit is contained in:
fbsanchez 2019-10-23 15:23:21 +02:00
parent 5b3fd5dd79
commit 66ceb93d04
3 changed files with 267 additions and 23 deletions

View File

@ -64,10 +64,67 @@ function quickShell()
ui_require_css_file('wizard');
ui_require_css_file('discovery');
// Settings.
// WebSocket host, where to connect.
if (isset($config['ws_host']) === false) {
config_update_value('ws_host', $_SERVER['SERVER_ADDR']);
}
if (isset($config['ws_port']) === false) {
config_update_value('ws_port', 8080);
}
if (isset($config['ws_url']) === false) {
$ws_url = 'http://'.$config['ws_host'].':'.$config['ws_port'];
} else {
$ws_url = $config['ws_url'];
}
// Gotty settings. Internal communication (WS).
if (isset($config['gotty_host']) === false) {
config_update_value('gotty_host', '127.0.0.1');
}
if (isset($config['gotty_telnet_port']) === false) {
config_update_value('gotty_telnet_port', 8082);
}
if (isset($config['gotty_ssh_port']) === false) {
config_update_value('gotty_ssh_port', 8081);
}
// Username. Retrieve from form.
if (empty($username) === true) {
// No username provided, ask for it.
$wiz = new Wizard();
$test = file_get_contents($ws_url);
if ($test === false) {
ui_print_error_message(__('WebService engine has not been started, please check documentation.'));
$wiz->printForm(
[
'form' => [
'method' => 'POST',
'action' => '#',
],
'inputs' => [
[
'class' => 'w100p',
'arguments' => [
'name' => 'submit',
'label' => __('Retry'),
'type' => 'submit',
'attributes' => 'class="sub next"',
'return' => true,
],
],
],
]
);
return;
}
$wiz->printForm(
[
'form' => [
@ -121,28 +178,6 @@ function quickShell()
return;
}
// WebSocket host, where to connect.
if (isset($config['ws_host']) === false) {
config_update_value('ws_host', $_SERVER['SERVER_ADDR']);
}
if (isset($config['ws_port']) === false) {
config_update_value('ws_port', 8080);
}
// Gotty settings. Internal communication (WS).
if (isset($config['gotty_host']) === false) {
config_update_value('gotty_host', '127.0.0.1');
}
if (isset($config['gotty_telnet_port']) === false) {
config_update_value('gotty_telnet_port', 8082);
}
if (isset($config['gotty_ssh_port']) === false) {
config_update_value('gotty_ssh_port', 8081);
}
// Initialize Gotty Client.
$host = $config['gotty_host'];
if ($method == 'ssh') {
@ -161,9 +196,24 @@ function quickShell()
return;
}
$test = file_get_contents($ws_url);
if ($test === false) {
if (empty($wiz) === true) {
$wiz = new Wizard();
}
ui_print_error_message(__('WebService engine has not been started, please check documentation.'));
echo $wiz->printGoBackButton('#');
return;
}
$r = file_get_contents('http://'.$host.':'.$port.'/js/hterm.js');
if (empty($r) === true) {
ui_print_error_message(__('WebService engine has not been started, please check documentation.'));
if (empty($wiz) === true) {
$wiz = new Wizard();
}
ui_print_error_message(__('WebService engine is not working properly, please check documentation.'));
echo $wiz->printGoBackButton('#');
return;
}

View File

@ -0,0 +1,19 @@
[Unit]
Description=php webSocket
After=syslog.target network.target
[Service]
User=apache
Type=simple
ExecStart=php /var/www/html/pandora_console/ws.php >> /var/www/html/pandora_console/pandora_console.log 2>&1
TimeoutStopSec=20
KillMode=process
Restart=always
RestartSec=2
StandardOutput=file:/var/www/html/pandora_console/pandora_console.log
StandardError=file:/var/www/html/pandora_console/pandora_console.log
[Install]
WantedBy=multi-user.target
Alias=websocket.service

175
pandora_console/websocket.sh Executable file
View File

@ -0,0 +1,175 @@
#!/bin/bash
# Copyright (c) 2005-2019 Artica ST
#
# /etc/init.d/websocket
#
# System startup script for Pandora FMS Console websocket engine
#
# Comments to support chkconfig on RedHat Linux
# chkconfig: 2345 90 10
# description: Pandora FMS Console webscoket engine startup script
#
# Comments to support LSB init script conventions
### BEGIN INIT INFO
# Provides: websocket
# Required-Start: $syslog cron
# Should-Start: $network cron mysql
# Required-Stop: $syslog
# Should-Stop: $network
# Default-Start: 2 3 5
# Default-Stop: 0 1 6
# Short-Description: Pandora FMS Console websocket engine startup script
# Description: Pandora FMS Console websocket engine startup script
### END INIT INFO
if [ -x /lib/lsb/init-functions ]; then
. /lib/lsb/init-functions
fi
# If you want to run several pandora Console Websocket engines in this machine, just copy
# this script to another name, editing PANDORA_HOME to the new .conf
export WS_ENGINE="/var/www/html/pandora_console/ws.php"
export PHP=/usr/bin/php
export WS_LOG="/var/www/html/pandora_console/pandora_console.log"
export GOTTY="/tmp/"
# Environment variables
if [[ -z ${PANDORA_RB_PRODUCT_NAME} ]]; then
PANDORA_RB_PRODUCT_NAME="Pandora FMS"
fi
if [[ -z ${PANDORA_RB_COPYRIGHT_NOTICE} ]]; then
PANDORA_RB_COPYRIGHT_NOTICE="Artica ST"
fi
export PANDORA_RB_PRODUCT_NAME=$PANDORA_RB_PRODUCT_NAME
export PANDORA_RB_COPYRIGHT_NOTICE=$PANDORA_RB_COPYRIGHT_NOTICE
# Uses a wait limit before sending a KILL signal, before trying to stop
# Pandora FMS Console Websocket engine nicely. Some big systems need some time before close
# all pending tasks / threads.
export MAXWAIT=60
# Check for SUSE status scripts
if [ -f /etc/rc.status ]
then
. /etc/rc.status
rc_reset
else
# Define part of rc functions for non-suse systems
function rc_status () {
RETVAL=$?
case $1 in
-v) RETVAL=0;;
esac
}
function rc_exit () { exit $RETVAL; }
function rc_failed () { RETVAL=${1:-1}; }
RETVAL=0
fi
# This function replace pidof, not working in the same way in different linux distros
function pidof_pandora () {
# This sets COLUMNS to XXX chars, because if command is run
# in a "strech" term, ps aux don't report more than COLUMNS
# characters and this will not work.
COLUMNS=300
PANDORA_PID=`ps aux | grep "$PHP $WS_ENGINE" | grep -v grep | tail -1 | awk '{ print $2 }'`
echo $PANDORA_PID
}
# Main script
if [ ! -x $GOTTY ]
then
echo "Gotty not found in $GOTTY"
rc_failed 5 # program is not installed
rc_exit
fi
if [ ! -f $PHP ]
then
echo "$PHP not found, please install version >= 7.0"
rc_failed 5 # program is not installed
rc_exit
fi
case "$1" in
start)
PANDORA_PID=`pidof_pandora`
if [ ! -z "$PANDORA_PID" ]
then
echo "$PANDORA_RB_PRODUCT_NAME Console Websocket engine is currently running on this machine with PID ($PANDORA_PID)."
rc_exit # running start on a service already running
fi
export PERL_LWP_SSL_VERIFY_HOSTNAME=0
$PHP $WS_ENGINE >> $WS_LOG 2>&1 &
sleep 1
PANDORA_PID=`pidof_pandora`
if [ ! -z "$PANDORA_PID" ]
then
echo "$PANDORA_RB_PRODUCT_NAME Console Websocket engine is now running with PID $PANDORA_PID"
rc_status -v
else
echo "Cannot start $PANDORA_RB_PRODUCT_NAME Console Websocket engine. Aborted."
echo "Check $PANDORA_RB_PRODUCT_NAME log files at $WS_LOG"
rc_failed 7 # program is not running
fi
;;
stop)
PANDORA_PID=`pidof_pandora`
if [ -z "$PANDORA_PID" ]
then
echo "$PANDORA_RB_PRODUCT_NAME Console Websocket engine is not running, cannot stop it."
rc_exit # running stop on a service already stopped or not running
else
echo "Stopping $PANDORA_RB_PRODUCT_NAME Console Websocket engine"
kill $PANDORA_PID > /dev/null 2>&1
COUNTER=0
while [ $COUNTER -lt $MAXWAIT ]
do
_PID=`pidof_pandora`
if [ "$_PID" != "$PANDORA_PID" ]
then
COUNTER=$MAXWAIT
fi
COUNTER=`expr $COUNTER + 1`
sleep 1
done
# Send a KILL -9 signal to process, if it's alive after 60secs, we need
# to be sure is really dead, and not pretending...
if [ "$_PID" = "$PANDORA_PID" ]
then
kill -9 $PANDORA_PID > /dev/null 2>&1
fi
rc_status -v
fi
;;
status)
PANDORA_PID=`pidof_pandora`
if [ -z "$PANDORA_PID" ]
then
echo "$PANDORA_RB_PRODUCT_NAME Console Websocket engine is not running."
rc_failed 7 # program is not running
else
echo "$PANDORA_RB_PRODUCT_NAME Console Websocket engine is running with PID $PANDORA_PID."
rc_status
fi
;;
force-reload|restart)
$0 stop
$0 start
;;
*)
echo "Usage: $0 { start | stop | restart | status }"
exit 1
esac
rc_exit