This commit is contained in:
Your Name
2025-05-26 17:42:29 +02:00
parent 4fc06ccce0
commit cd8cf349dd
9 changed files with 1207 additions and 295 deletions

View File

@@ -664,3 +664,215 @@ if ($type == "wifi_scan_old") {
echo $json_data;
}
/*
____ _ ____ _ __ __ _
/ ___| _ _ ___| |_ ___ _ __ ___ | _ \ / ___| ___ _ ____ _(_) ___ ___| \/ | __ _ _ __ __ _ __ _ ___ _ __ ___ ___ _ __ | |_
\___ \| | | / __| __/ _ \ '_ ` _ \| | | | \___ \ / _ \ '__\ \ / / |/ __/ _ \ |\/| |/ _` | '_ \ / _` |/ _` |/ _ \ '_ ` _ \ / _ \ '_ \| __|
___) | |_| \__ \ || __/ | | | | | |_| | ___) | __/ | \ V /| | (_| __/ | | | (_| | | | | (_| | (_| | __/ | | | | | __/ | | | |_
|____/ \__, |___/\__\___|_| |_| |_|____/ |____/ \___|_| \_/ |_|\___\___|_| |_|\__,_|_| |_|\__,_|\__, |\___|_| |_| |_|\___|_| |_|\__|
|___/ |___/
*/
// Get systemd services status
if ($type == "get_systemd_services") {
try {
// List of NebuleAir services to monitor with descriptions and frequencies
$services = [
'moduleair-npm-data.timer' => [
'description' => 'Collects particulate matter data from NextPM sensor',
'frequency' => 'Every 10 seconds'
],
'moduleair-co2-data.timer' => [
'description' => 'Reads environmental data from Envea sensors',
'frequency' => 'Every 10 seconds'
],
'moduleair-sara-data.timer' => [
'description' => 'Transmits collected data via 4G cellular modem',
'frequency' => 'Every 60 seconds'
],
'moduleair-bme280-data.timer' => [
'description' => 'Monitors temperature and humidity (BME280)',
'frequency' => 'Every 2 minutes'
],
'moduleair-db-cleanup-data.timer' => [
'description' => 'Cleans up old data from database',
'frequency' => 'Daily'
]
];
$serviceStatus = [];
foreach ($services as $service => $serviceInfo) {
// 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,
'description' => $serviceInfo['description'],
'frequency' => $serviceInfo['frequency'],
'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 = [
'moduleair-npm-data.timer',
'moduleair-sara-data.timer',
'nebuleair-bme280-data.timer',
'moduleair-co2-data.timer',
'moduleair-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 = [
'moduleair-npm-data.timer',
'moduleair-sara-data.timer',
'nebuleair-bme280-data.timer',
'moduleair-co2-data.timer',
'moduleair-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()
]);
}
}