433 lines
12 KiB
Plaintext
Executable File
433 lines
12 KiB
Plaintext
Executable File
#!/usr/local/psa/bin/sw-engine-pleskrun
|
|
<?php
|
|
// Copyright 1999-2024. WebPros International GmbH. All rights reserved.
|
|
use PleskExt\Watchdog\System\Services;
|
|
|
|
require_once('sdk.php');
|
|
|
|
require("modules/watchdog/wdlib.php");
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
class ParsCmdLineErr extends WDExc
|
|
{
|
|
}
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
try {
|
|
|
|
main($argc, $argv);
|
|
|
|
} catch (ParsCmdLineErr $e) {
|
|
echo $e->getMessage() . "\n";
|
|
usage();
|
|
exit(1);
|
|
} catch (Exception $e) {
|
|
echo $e->getMessage() . "\n";
|
|
exit(1);
|
|
}
|
|
|
|
exit(0);
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
function main($argc, $argv)
|
|
{
|
|
$access_opts = array(
|
|
'start' => false,
|
|
'stop' => false,
|
|
'restart' => false,
|
|
'full-restart' => false,
|
|
|
|
'start-monit' => false,
|
|
'stop-monit' => false,
|
|
'restart-monit' => false,
|
|
'full-restart-monit' => false,
|
|
'start-wdcollect' => false,
|
|
'stop-wdcollect' => false,
|
|
'restart-wdcollect' => false,
|
|
'full-restart-wdcollect' => false,
|
|
'adapt' => false,
|
|
|
|
'regen-all' => false,
|
|
'regen-monitrc' => false,
|
|
|
|
'ping-monit' => false,
|
|
'ping-wdcollect' => false,
|
|
|
|
'plesk-name' => false,
|
|
'monit-service' => true,
|
|
'unmonit-service' => true,
|
|
'start-service' => true,
|
|
'stop-service' => true,
|
|
'restart-service' => true,
|
|
'service-status' => true,
|
|
'service-monit-status' => true,
|
|
|
|
'monit-disk' => true,
|
|
'space-rate' => true,
|
|
'inods-rate' => true,
|
|
'command' => true,
|
|
'unmonit-disk' => true,
|
|
'disk-status' => true,
|
|
'disk-monit-status' => true,
|
|
);
|
|
|
|
if (!is_plesk_configured()) {
|
|
// non fatal error
|
|
echo "Plesk is not configured. You should go to the Server Administration Panel and configure it.\n";
|
|
return 0;
|
|
}
|
|
|
|
unset($argv[0]);
|
|
$opts = parse_cmd_line($argv, $access_opts);
|
|
wd__db_connect();
|
|
|
|
$is_plesk_name = isset($opts['plesk-name']);
|
|
$space_rate = $space_rate_unit = $inods_rate = $inods_rate_unit = null;
|
|
i__set_space_rate($opts, $space_rate, $space_rate_unit);
|
|
i__set_inods_rate($opts, $inods_rate, $inods_rate_unit);
|
|
$command = !isset($opts['command']) ? null : $opts['command'];
|
|
$services = new Services();
|
|
|
|
if (isset($opts['start']))
|
|
start_wd();
|
|
elseif (isset($opts['stop']))
|
|
stop_wd();
|
|
elseif (isset($opts['restart']))
|
|
restart_wd();
|
|
elseif (isset($opts['full-restart']))
|
|
restart_wd(true);
|
|
elseif (isset($opts['start-monit']))
|
|
start_monit();
|
|
elseif (isset($opts['stop-monit']))
|
|
stop_monit();
|
|
elseif (isset($opts['restart-monit']))
|
|
restart_monit();
|
|
elseif (isset($opts['full-restart-monit']))
|
|
restart_monit(true);
|
|
elseif (isset($opts['start-wdcollect']))
|
|
start_wdcollect();
|
|
elseif (isset($opts['stop-wdcollect']))
|
|
stop_wdcollect();
|
|
elseif (isset($opts['restart-wdcollect']))
|
|
restart_wdcollect();
|
|
elseif (isset($opts['full-restart-wdcollect']))
|
|
restart_wdcollect(true);
|
|
elseif (isset($opts['adapt']))
|
|
restart_wd(true, true);
|
|
|
|
elseif (isset($opts['regen-all'])) {
|
|
create_monit_config();
|
|
} elseif (isset($opts['regen-monitrc']))
|
|
create_monit_config();
|
|
|
|
elseif (isset($opts['ping-monit']))
|
|
f_ping_monit();
|
|
elseif (isset($opts['ping-wdcollect']))
|
|
f_ping_wdcollect();
|
|
|
|
elseif (isset($opts['monit-service']))
|
|
monit_service(norm_service_name($services, $opts['monit-service'], $is_plesk_name));
|
|
elseif (isset($opts['unmonit-service']))
|
|
unmonit_service(norm_service_name($services, $opts['unmonit-service'], $is_plesk_name));
|
|
elseif (isset($opts['service-status']))
|
|
f_service_status(norm_service_name($services, $opts['service-status'], $is_plesk_name));
|
|
elseif (isset($opts['service-monit-status']))
|
|
f_service_monit_status(norm_service_name($services, $opts['service-monit-status'], $is_plesk_name));
|
|
|
|
elseif (isset($opts['monit-disk'])) {
|
|
if ('percent' === $space_rate_unit && (20 > $space_rate || 100 < $space_rate)) {
|
|
throw new ParsCmdLineErr("Invalid value $space_rate for parameter 'space_rate'");
|
|
}
|
|
|
|
if ('percent' === $inods_rate_unit && (20 > $inods_rate || 100 < $inods_rate)) {
|
|
throw new ParsCmdLineErr("Invalid value $inods_rate for parameter 'inods_rate'");
|
|
}
|
|
|
|
if (1 > $space_rate) {
|
|
throw new ParsCmdLineErr("Invalid value $space_rate for parameter 'space_rate': Enter the positive number.");
|
|
}
|
|
|
|
if (1 > $inods_rate) {
|
|
throw new ParsCmdLineErr("Invalid value $inods_rate for parameter 'inods_rate': Enter the positive number.");
|
|
}
|
|
|
|
if (!empty($command) && !isValidCommandForMonit($command)) {
|
|
throw new ParsCmdLineErr("Impossible executable command $command: Enter command without the \" and which reference to executable file"); // to check
|
|
}
|
|
|
|
monit_disk($opts['monit-disk'], $space_rate, $space_rate_unit, $inods_rate, $inods_rate_unit, $command);
|
|
}
|
|
elseif (isset($opts['unmonit-disk']))
|
|
unmonit_disk($opts['unmonit-disk']);
|
|
elseif (isset($opts['disk-status']))
|
|
f_disk_status($opts['disk-status']);
|
|
elseif (isset($opts['disk-monit-status']))
|
|
f_disk_monit_status($opts['disk-monit-status']);
|
|
|
|
else
|
|
usage();
|
|
}
|
|
|
|
function usage()
|
|
{
|
|
echo "Usage: wd <options>
|
|
Options are as follows:
|
|
--start Start all Watchdog services
|
|
--stop Stop Watchdog and all its services
|
|
--restart Restart Watchdog
|
|
--full-restart Rebuild configuration file and restart
|
|
|
|
--start-monit Start the Monit service
|
|
--stop-monit Stop the Monit service
|
|
--restart-monit Restart the Monit service
|
|
--full-restart-monit Rebuild configuration file and restart the Monit service
|
|
--start-wdcollect Start the wdcollect service
|
|
--stop-wdcollect Stop the wdcollect service
|
|
--restart-wdcollect Restart the wdcollect service
|
|
--full-restart-wdcollect Rebuild configuration file and restart the wdcollect service
|
|
--adapt Reconfigure the module and restart the monitoring service with new settings
|
|
|
|
--regen-all Create all configuration files
|
|
--regen-monitrc Create configuration file for the Monit service
|
|
|
|
--ping-monit Check via the network if the Monit service is working
|
|
--ping-wdcollect Check via the network if the the wdcollect service is working
|
|
|
|
--monit-service=<service> Enable monitoring service
|
|
--unmonit-service=<service> Disable monitoring service
|
|
--service-status=<service> Return status of monitored service
|
|
--service-monit-status=<service> Return the system monitoring status
|
|
[ --plesk-name ] Service name as shown in the Plesk control panel
|
|
|
|
--monit-disk=<device> Start monitoring a partition
|
|
[ --space-rate=<space_rate> ] (%, Gb, Mb)
|
|
[ --inods-rate=<inods_rate> ] (%, file)
|
|
[ --command=<command> ] Define a command to run when disk space usage reaches the defined threshold
|
|
--unmonit-disk Stop monitoring partition
|
|
--disk-status=<device> Return status of monitored partition
|
|
--disk-monit-status=<device> Return partition monitoring status\n";
|
|
}
|
|
|
|
function f_ping_monit()
|
|
{
|
|
echo pm_Locale::lmsg(monit_wait_start(30) ? 'DNT.statusRunning' : 'DNT.statusStopped') . "\n";
|
|
}
|
|
|
|
function f_ping_wdcollect()
|
|
{
|
|
echo pm_Locale::lmsg(wdcollect_wait_start(30) ? 'DNT.statusRunning' : 'DNT.statusStopped') . "\n";
|
|
}
|
|
|
|
function f_service_status($srv_name)
|
|
{
|
|
echo get_service_wd_status(i__get_service_status($srv_name));
|
|
}
|
|
|
|
function f_service_monit_status($srv_name)
|
|
{
|
|
echo get_service_monit_wd_status(i__get_service_status($srv_name));
|
|
}
|
|
|
|
function f_disk_status($device)
|
|
{
|
|
echo get_disk_wd_status(i__get_disk_status($device));
|
|
}
|
|
|
|
function f_disk_monit_status($device)
|
|
{
|
|
echo get_disk_monit_wd_status(i__get_disk_status($device));
|
|
}
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
function i__set_space_rate($opts, &$space_rate, &$space_rate_unit)
|
|
{
|
|
if (!isset($opts['space-rate'])) {
|
|
$space_rate = 80;
|
|
$space_rate_unit = 'percent';
|
|
return;
|
|
}
|
|
|
|
$m = sscanf($opts['space-rate'], "%f%s", $space_rate, $space_rate_unit);
|
|
if (1 > $m) {
|
|
throw new ParsCmdLineErr("Disk space rate is set incorrectly");
|
|
}
|
|
|
|
$space_rate_unit = 1 == $m ? "percent" : i__norm_space_unit($space_rate_unit);
|
|
if (false === $space_rate_unit) {
|
|
throw new ParsCmdLineErr("Disk space rate is set incorrectly");
|
|
}
|
|
|
|
if ("percent" == $space_rate_unit && (20 > $space_rate || 100 < $space_rate)) {
|
|
throw new ParsCmdLineErr("Disk space usage threshold has been set incorrectly: Enter a value from 20 to 100");
|
|
}
|
|
|
|
if (1 > $space_rate) {
|
|
throw new ParsCmdLineErr("Disk space usage threshold has been set incorrectly: Enter a value greater than 0");
|
|
}
|
|
}
|
|
|
|
function i__set_inods_rate($opts, &$inods_rate, &$inods_rate_unit)
|
|
{
|
|
if (!isset($opts['inods-rate'])) {
|
|
$inods_rate = 80;
|
|
$inods_rate_unit = 'percent';
|
|
return;
|
|
}
|
|
|
|
$m = sscanf($opts['inods-rate'], "%f%s", $inods_rate, $inods_rate_unit);
|
|
if (1 > $m) {
|
|
throw new ParsCmdLineErr("Files number rate is set incorrectly");
|
|
}
|
|
|
|
$inods_rate_unit = 1 == $m ? "file" : i__norm_inods_unit($inods_rate_unit);
|
|
if (false === $inods_rate_unit) {
|
|
throw new ParsCmdLineErr("Files number rate is set incorrectly");
|
|
}
|
|
|
|
if ("percent" == $inods_rate_unit && (20 > $inods_rate || 100 < $inods_rate)) {
|
|
throw new ParsCmdLineErr("Files number threshold has been set incorrectly: Enter a value from 20 to 100");
|
|
}
|
|
|
|
if (1 > $inods_rate) {
|
|
throw new ParsCmdLineErr("Files number threshold has been set incorrectly: Enter a value greater than 0");
|
|
}
|
|
}
|
|
|
|
function i__norm_space_unit($space_rate_unit)
|
|
{
|
|
if ("%" == $space_rate_unit || !stricmp("percent", $space_rate_unit))
|
|
return "percent";
|
|
|
|
if (!stricmp("Gb", $space_rate_unit) || !stricmp("gigabyte", $space_rate_unit))
|
|
return "gigabyte";
|
|
|
|
if (!stricmp("Mb", $space_rate_unit) || !stricmp("megabyte", $space_rate_unit))
|
|
return "megabyte";
|
|
|
|
if (!stricmp("Kb", $space_rate_unit) || !stricmp("kilobyte", $space_rate_unit))
|
|
return "kilobyte";
|
|
|
|
if (!stricmp("b", $space_rate_unit) || !stricmp("byte", $space_rate_unit))
|
|
return "byte";
|
|
|
|
return false;
|
|
}
|
|
|
|
function i__norm_inods_unit($inods_rate_unit)
|
|
{
|
|
if ("%" == $inods_rate_unit || !stricmp("percent", $inods_rate_unit))
|
|
return "percent";
|
|
|
|
return false;
|
|
}
|
|
|
|
function stricmp($s1, $s2)
|
|
{
|
|
return strncasecmp($s1, $s2, max(strlen($s1), strlen($s2)));
|
|
}
|
|
|
|
function i__get_service_status($srv_name)
|
|
{
|
|
$stats = get_monitoring_status();
|
|
|
|
if (!isset($stats['services'][$srv_name])) {
|
|
throw new WDExc("You did not configure monitoring for the $srv_name service");
|
|
}
|
|
|
|
if (!is_service($stats['services'][$srv_name]['type'])) {
|
|
throw new WDExc("$srv_name is not a service");
|
|
}
|
|
|
|
return $stats['services'][$srv_name];
|
|
}
|
|
|
|
function i__get_disk_status($device)
|
|
{
|
|
$device_name = ("/" == $device[0]) ? substr($device, 1) : $device;
|
|
$stats = get_monitoring_status();
|
|
|
|
if (!isset($stats['services'][$device_name])) {
|
|
throw new WDExc("You did not configure monitoring for the $device device");
|
|
}
|
|
|
|
if (!is_disk($stats['services'][$device_name]['type'])) {
|
|
throw new WDExc("$device is not a device");
|
|
}
|
|
|
|
return $stats['services'][$device_name];
|
|
}
|
|
|
|
function norm_service_name(Services $services, $service_name, $is_plesk_name)
|
|
{
|
|
if (!$is_plesk_name)
|
|
return $service_name;
|
|
|
|
$plesk_service_name = $service_name;
|
|
$service_name = $services->getWatchdogServiceName($plesk_service_name);
|
|
if ($service_name !== null) {
|
|
return $service_name;
|
|
}
|
|
|
|
throw new WDExc("There is no $plesk_service_name service in Plesk control panel");
|
|
}
|
|
|
|
function parse_cmd_line($argv, $all_opts)
|
|
{
|
|
$opts = array();
|
|
$i = 0;
|
|
$n = min(count($argv), 100);
|
|
reset($argv);
|
|
|
|
while ($n > $i) {
|
|
$opt = parse_option(current($argv));
|
|
check_option_on_access($opt, $all_opts);
|
|
$opts[$opt['name']] = $opt['value'];
|
|
next($argv);
|
|
++$i;
|
|
}
|
|
|
|
return $opts;
|
|
}
|
|
|
|
function check_option_on_access($opt, $all_opts)
|
|
{
|
|
if (!isset($all_opts[$opt['name']])) {
|
|
throw new ParsCmdLineErr("Unknown option '{$opt['name']}'");
|
|
}
|
|
|
|
if ($all_opts[$opt['name']]) {
|
|
if (is_bool($opt['value'])) {
|
|
throw new ParsCmdLineErr("Option '{$opt['name']}' requires an argument");
|
|
}
|
|
} else {
|
|
if (!is_bool($opt['value'])) {
|
|
throw new ParsCmdLineErr("Option '{$opt['name']}' does not require arguments");
|
|
}
|
|
}
|
|
}
|
|
|
|
function parse_option($opt)
|
|
{
|
|
if (strncmp("--", $opt, 2)) {
|
|
throw new ParsCmdLineErr("Unknown operand '$opt'");
|
|
}
|
|
|
|
if (false !== ($p = strpos($opt, "=", 2))) {
|
|
$name = substr($opt, 2, $p - 2);
|
|
$val = substr($opt, $p + 1);
|
|
} else {
|
|
$name = substr($opt, 2);
|
|
$val = true;
|
|
}
|
|
|
|
if (empty($name) || empty($val)) {
|
|
throw new ParsCmdLineErr("Option '$opt' is set up incorrectly");
|
|
}
|
|
|
|
return array('name' => $name, 'value' => $val);
|
|
}
|