-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.php
executable file
·107 lines (86 loc) · 6.18 KB
/
utils.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
if (file_exists("./config.php")) {
include "./config.php";
} else if (file_exists("../config.php")) {
include "../config.php";
} else if (file_exists("../../config.php")) {
include "../../config.php";
}
// The `last_heartbeat` function gets the time since the last heartbeat from the instance.
function last_heartbeat($config) {
$alerts_file_path = $config["interface_directory"] . "/alerts.json";
$status_file_path = $config["interface_directory"] . "/status.json";
if (is_dir($config["interface_directory"]) == true) { // Check to make sure the specified interface directory exists.
if (file_exists($alerts_file_path)) { // Check to see if the alert file exists.
$heartbeat_log = array_keys(json_decode(file_get_contents($alerts_file_path), true)); // Load the heartbeat log from the JSON data in the alerts file.
} else { // If the heartbeat file doesn't exist, then load a blank placeholder instead.
$heartbeat_log = array(0); // Set the heartbeat log to a blank placeholder.
}
if (file_exists($status_file_path)) { // Check to see if the status message log.
$status_log = array_keys(json_decode(file_get_contents($status_file_path), true)); // Load the status message log from the JSON data in the alerts file.
} else { // If the heartbeat file doesn't exist, then load a blank placeholder instead.
$status_log = array(); // Set the status message log to a placeholder array.
}
} else {
$heartbeat_log = array(0); // Set the heartbeat log to a blank placeholder.
$status_log = array(); // Set the status message log to a placeholder array.
}
$last_alert_heartbeat = microtime(true) - floatval(end($heartbeat_log)); // Calculate how many seconds ago the last heartbeat was, using the alert log.
if (sizeof($status_log) > 0) { // Check to make sure there is at least one status log entry
$last_status_heartbeat = microtime(true) - floatval(end($status_log)); // Calculate how many seconds ago the last heartbeat was, using the status log.
} else {
$last_status_heartbeat = microtime(true) - 0;
}
if ($last_status_heartbeat < $last_alert_heartbeat) {
$last_heartbeat = $last_status_heartbeat;
} else {
$last_heartbeat = $last_alert_heartbeat;
}
if ($last_heartbeat < -5) { // If the heartbeat happened more than 5 seconds in the future, then assume the clocks are desynced.
$last_heartbeat = -1;
} else if ($last_heartbeat < 0) { // If the heartbeat is only a few seconds in the future, then assume the time since the last heartbeat is 0 seconds.
$last_heartbeat = 0;
}
return $last_heartbeat;
}
// The `is_alive` function checks to see if the linked instance is running, based on its heartbeat.
function is_alive($config) {
$last_heartbeat = last_heartbeat($config);
if ($last_heartbeat == -1) {
return false;
} else if ($last_heartbeat < $config["heartbeat_threshold"]) { // Only consider the system online if it's last heartbeat was within a certain number of seconds ago.
return true;
} else { // If the last heartbeat exceeded the time to be considered online, display a message that the system is offline.
return false;
}
}
// The `verify_permissions` function checks to see if all permissions are set correctly, and that all files are in their expected locations.
function verify_permissions($config) {
$verify_command = "sudo -u " . $config["exec_user"] . " echo verify"; // Prepare the command to verify permissions.
$command_output = shell_exec($verify_command); // Execute the command, and record its output.
$command_output = trim($command_output); // Remove whitespaces from the end and beginning of the command output.
if ($command_output !== "verify") { // Check to see if the command output differs from the expected output.
echo "<p class=\"error\">PHP does not have the necessary permissions to manage this system as '" . $config["exec_user"] . "' using the '" . shell_exec("whoami") . "' user.</p>"; // Display an error briefly explaining the problem.
}
if (is_writable("./") == false) { // Check to see if the controller's root directory is writable.
echo "<p class=\"error\">The " . $config["product_name"] . " directory is not writable. Please verify the permissions of the " . getcwd() . " directory.</p>";
} else if (is_writable("./start.sh") == false and file_exists("./start.sh") == true) { // Check to see if the controller's start script is writable.
echo "<p class=\"error\">The start.sh script in the " . getcwd() . " directory is not writable.</p>";
}
$instance_configuration_path = $config["instance_directory"] . "/config.json"; // This is the file path to the configuration file of the Assassin instance.
if (is_dir($config["instance_directory"]) == false) { // Check to see if the root Assassin instance directory exists.
echo "<p class=\"error\">The instance directory doesn't appear to exist at " . $config["instance_directory"] . ". Please adjust the controller configuration.</p>";
echo "<a class=\"button\" href=\"./settings.php\">Settings</a>";
} else if (file_exists($instance_configuration_path) == false) { // Check to see if the instance configuration file exists.
echo "<p class=\"error\">The instance configuration file doesn't appear to exist at " . $instance_configuration_path . ". Please make sure the 'Instance Directory' points to a valid instance of Assassin.</p>";
echo "<a class=\"button\" href=\"./settings.php\">Settings</a>";
} else if (is_writable($instance_configuration_path) == false) { // Check to see if the instance configuration file is writable.
echo "<p class=\"error\">The instance configuration file at " . $instance_configuration_path . " doesn't appear to be writable.</p>";
echo "<a class=\"button\" href=\"./settings.php\">Settings</a>";
}
}
// The `decimal_precision` function rounds off a given decimal number to a given number of decimal points.
function decimal_precision($number, $config) {
return round($number * (10**$config["precision"]["coordinates"])) / (10**$config["precision"]["coordinates"]);
}
?>