updates
This commit is contained in:
@@ -1014,3 +1014,199 @@ if ($type == "execute_command") {
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
____ _ ____ _ __ __ _
|
||||
/ ___| _ _ ___| |_ ___ _ __ ___ | _ \ / ___| ___ _ ____ _(_) ___ ___| \/ | __ _ _ __ __ _ __ _ ___ _ __ ___ ___ _ __ | |_
|
||||
\___ \| | | / __| __/ _ \ '_ ` _ \| | | | \___ \ / _ \ '__\ \ / / |/ __/ _ \ |\/| |/ _` | '_ \ / _` |/ _` |/ _ \ '_ ` _ \ / _ \ '_ \| __|
|
||||
___) | |_| \__ \ || __/ | | | | | |_| | ___) | __/ | \ V /| | (_| __/ | | | (_| | | | | (_| | (_| | __/ | | | | | __/ | | | |_
|
||||
|____/ \__, |___/\__\___|_| |_| |_|____/ |____/ \___|_| \_/ |_|\___\___|_| |_|\__,_|_| |_|\__,_|\__, |\___|_| |_| |_|\___|_| |_|\__|
|
||||
|___/ |___/
|
||||
*/
|
||||
|
||||
// Get systemd services status
|
||||
if ($type == "get_systemd_services") {
|
||||
try {
|
||||
// List of NebuleAir services to monitor
|
||||
$services = [
|
||||
'nebuleair-npm-data.timer',
|
||||
'nebuleair-envea-data.timer',
|
||||
'nebuleair-sara-data.timer',
|
||||
'nebuleair-bme280-data.timer',
|
||||
'nebuleair-mppt-data.timer',
|
||||
'nebuleair-db-cleanup-data.timer'
|
||||
];
|
||||
|
||||
$serviceStatus = [];
|
||||
|
||||
foreach ($services as $service) {
|
||||
// Get service active status
|
||||
$activeCmd = "systemctl is-active " . escapeshellarg($service) . " 2>/dev/null";
|
||||
$activeStatus = trim(shell_exec($activeCmd));
|
||||
$isActive = ($activeStatus === 'active');
|
||||
|
||||
// Get service enabled status
|
||||
$enabledCmd = "systemctl is-enabled " . escapeshellarg($service) . " 2>/dev/null";
|
||||
$enabledStatus = trim(shell_exec($enabledCmd));
|
||||
$isEnabled = ($enabledStatus === 'enabled');
|
||||
|
||||
// Clean up service name for display
|
||||
$displayName = str_replace(['.timer', 'nebuleair-', '-data'], '', $service);
|
||||
$displayName = ucfirst(str_replace('-', ' ', $displayName));
|
||||
|
||||
$serviceStatus[] = [
|
||||
'name' => $service,
|
||||
'display_name' => $displayName,
|
||||
'active' => $isActive,
|
||||
'enabled' => $isEnabled
|
||||
];
|
||||
}
|
||||
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'services' => $serviceStatus
|
||||
], JSON_PRETTY_PRINT);
|
||||
|
||||
} catch (Exception $e) {
|
||||
echo json_encode([
|
||||
'success' => false,
|
||||
'error' => $e->getMessage()
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
// Restart a systemd service
|
||||
if ($type == "restart_systemd_service") {
|
||||
$service = $_GET['service'] ?? null;
|
||||
|
||||
if (empty($service)) {
|
||||
echo json_encode([
|
||||
'success' => false,
|
||||
'error' => 'No service specified'
|
||||
]);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Validate service name (security check)
|
||||
$allowedServices = [
|
||||
'nebuleair-npm-data.timer',
|
||||
'nebuleair-envea-data.timer',
|
||||
'nebuleair-sara-data.timer',
|
||||
'nebuleair-bme280-data.timer',
|
||||
'nebuleair-mppt-data.timer',
|
||||
'nebuleair-db-cleanup-data.timer'
|
||||
];
|
||||
|
||||
if (!in_array($service, $allowedServices)) {
|
||||
echo json_encode([
|
||||
'success' => false,
|
||||
'error' => 'Invalid service name'
|
||||
]);
|
||||
exit;
|
||||
}
|
||||
|
||||
try {
|
||||
// Restart the service
|
||||
$command = "sudo systemctl restart " . escapeshellarg($service) . " 2>&1";
|
||||
$output = shell_exec($command);
|
||||
|
||||
// Check if restart was successful
|
||||
$statusCmd = "systemctl is-active " . escapeshellarg($service) . " 2>/dev/null";
|
||||
$status = trim(shell_exec($statusCmd));
|
||||
|
||||
if ($status === 'active' || empty($output)) {
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'message' => "Service $service restarted successfully"
|
||||
]);
|
||||
} else {
|
||||
echo json_encode([
|
||||
'success' => false,
|
||||
'error' => "Failed to restart service: $output"
|
||||
]);
|
||||
}
|
||||
|
||||
} catch (Exception $e) {
|
||||
echo json_encode([
|
||||
'success' => false,
|
||||
'error' => $e->getMessage()
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
// Enable/disable a systemd service
|
||||
if ($type == "toggle_systemd_service") {
|
||||
$service = $_GET['service'] ?? null;
|
||||
$enable = $_GET['enable'] ?? null;
|
||||
|
||||
if (empty($service) || $enable === null) {
|
||||
echo json_encode([
|
||||
'success' => false,
|
||||
'error' => 'Missing service name or enable parameter'
|
||||
]);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Validate service name (security check)
|
||||
$allowedServices = [
|
||||
'nebuleair-npm-data.timer',
|
||||
'nebuleair-envea-data.timer',
|
||||
'nebuleair-sara-data.timer',
|
||||
'nebuleair-bme280-data.timer',
|
||||
'nebuleair-mppt-data.timer',
|
||||
'nebuleair-db-cleanup-data.timer'
|
||||
];
|
||||
|
||||
if (!in_array($service, $allowedServices)) {
|
||||
echo json_encode([
|
||||
'success' => false,
|
||||
'error' => 'Invalid service name'
|
||||
]);
|
||||
exit;
|
||||
}
|
||||
|
||||
try {
|
||||
$enable = filter_var($enable, FILTER_VALIDATE_BOOLEAN);
|
||||
$action = $enable ? 'enable' : 'disable';
|
||||
|
||||
// Enable/disable the service
|
||||
$command = "sudo systemctl $action " . escapeshellarg($service) . " 2>&1";
|
||||
$output = shell_exec($command);
|
||||
|
||||
// If disabling, also stop the service
|
||||
if (!$enable) {
|
||||
$stopCommand = "sudo systemctl stop " . escapeshellarg($service) . " 2>&1";
|
||||
$stopOutput = shell_exec($stopCommand);
|
||||
}
|
||||
|
||||
// If enabling, also start the service
|
||||
if ($enable) {
|
||||
$startCommand = "sudo systemctl start " . escapeshellarg($service) . " 2>&1";
|
||||
$startOutput = shell_exec($startCommand);
|
||||
}
|
||||
|
||||
// Check if the operation was successful
|
||||
$statusCmd = "systemctl is-enabled " . escapeshellarg($service) . " 2>/dev/null";
|
||||
$status = trim(shell_exec($statusCmd));
|
||||
|
||||
$expectedStatus = $enable ? 'enabled' : 'disabled';
|
||||
|
||||
if ($status === $expectedStatus) {
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'message' => "Service $service " . ($enable ? 'enabled and started' : 'disabled and stopped') . " successfully"
|
||||
]);
|
||||
} else {
|
||||
echo json_encode([
|
||||
'success' => false,
|
||||
'error' => "Failed to $action service: $output"
|
||||
]);
|
||||
}
|
||||
|
||||
} catch (Exception $e) {
|
||||
echo json_encode([
|
||||
'success' => false,
|
||||
'error' => $e->getMessage()
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user