ajax.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. use function Hestiacp\quoteshellarg\quoteshellarg;
  3. // Main include
  4. include $_SERVER["DOCUMENT_ROOT"] . "/inc/main.php";
  5. // Check user
  6. if ($_SESSION["userContext"] != "admin") {
  7. header("Location: /list/user");
  8. exit();
  9. }
  10. $requestPayload = json_decode(file_get_contents("php://input"), true);
  11. $allowedPeriods = ["daily", "weekly", "monthly", "yearly"];
  12. if (!empty($requestPayload["period"]) && in_array($requestPayload["period"], $allowedPeriods)) {
  13. $period = $requestPayload["period"];
  14. } else {
  15. $period = "daily";
  16. }
  17. if (!empty($requestPayload["service"])) {
  18. $service = $requestPayload["service"];
  19. } else {
  20. $service = "la";
  21. }
  22. // Data
  23. exec(
  24. HESTIA_CMD . "v-export-rrd " . quoteshellarg($service) . " " . quoteshellarg($period),
  25. $output,
  26. $return_var,
  27. );
  28. if ($return_var != 0) {
  29. http_response_code(500);
  30. exit("Error fetching RRD data");
  31. }
  32. $serviceUnits = [
  33. "la" => "Points",
  34. "mem" => "Mbytes",
  35. "apache2" => "Connections",
  36. "nginx" => "Connections",
  37. "mail" => "Queue Size",
  38. "ftp" => "Connections",
  39. "ssh" => "Connections",
  40. ];
  41. if (preg_match("/^net_/", $service)) {
  42. $serviceUnits[$service] = "KBytes";
  43. }
  44. if (preg_match("/^pgsql_/", $service)) {
  45. $serviceUnits[$service] = "Queries";
  46. }
  47. if (preg_match("/^mysql_/", $service)) {
  48. $serviceUnits[$service] = "Queries";
  49. }
  50. $data = json_decode(implode("", $output), true);
  51. $data["service"] = $service;
  52. $data["unit"] = $serviceUnits[$service] ?? null;
  53. echo json_encode($data);