-
Notifications
You must be signed in to change notification settings - Fork 10
/
monitor.php
120 lines (94 loc) · 3.45 KB
/
monitor.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
// Function to get CPU utilization percentage
include_once "conn/conn.php";
function getCPUUsage() {
$cpuPercentage = trim(shell_exec('top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk \'{print 100 - $1}\''));
$cpuPercentage = is_numeric($cpuPercentage) ? floatval($cpuPercentage) : 0.0;
return $cpuPercentage;
}
// Function to get RAM usage in percentage
function getRAMUsage() {
$ramTotal = trim(shell_exec("free | awk '/Mem:/ {print $2}'"));
$ramUsed = trim(shell_exec("free | awk '/Mem:/ {print $3}'"));
$ramTotal = is_numeric($ramTotal) ? floatval($ramTotal) : 0.0;
$ramUsed = is_numeric($ramUsed) ? floatval($ramUsed) : 0.0;
if ($ramTotal == 0) {
return 0.0;
}
$ramPercentage = ($ramUsed / $ramTotal) * 100;
return $ramPercentage;
}
// Function to calculate the network speed
function calculateNetworkSpeed() {
$networkFile = '/sys/class/net/eth0/statistics/rx_bytes';
$interval = 2; // Update interval in seconds
$bytesStart = trim(file_get_contents($networkFile));
sleep($interval);
$bytesEnd = trim(file_get_contents($networkFile));
$bytesDiff = $bytesEnd - $bytesStart;
$speedMbps = round($bytesDiff * 8 / $interval / 1024 / 1024, 2); // Calculate speed in Mbps
return $speedMbps;
}
// Function to get the disk utilization
function getDiskUtilization($directory) {
$diskTotal = disk_total_space($directory);
$diskFree = disk_free_space($directory);
if ($diskTotal === false || $diskFree === false) {
return 0.0;
}
$diskUtilization = ($diskTotal - $diskFree) / $diskTotal * 100;
return $diskUtilization;
}
// Function to get the actual directory path and append "/bin" to it
function getActualBinDirectory() {
$phpFileDir = __DIR__;
$binDirectory = $phpFileDir . '/bin';
return $binDirectory;
}
// Function to get the number of channels played
function getChannelsPlayedCount() {
global $db;
$query = "SELECT COUNT(*) AS played_count FROM canales WHERE pidm3u8 IS NOT NULL";
$result = $db->query($query);
$row = $result->fetch(PDO::FETCH_ASSOC);
return $row['played_count'];
}
// Function to get the total number of channels
function getTotalChannelsCount() {
global $db;
$query = "SELECT COUNT(*) AS total_count FROM canales";
$result = $db->query($query);
$row = $result->fetch(PDO::FETCH_ASSOC);
return $row['total_count'];
}
// Function to get the number of online users in specific folders
function getOnlineUsersCount() {
$folderPath = __DIR__ . '/bin/m3u8/';
$onlineUsersCount = 0;
$folders = glob($folderPath . '*', GLOB_ONLYDIR);
foreach ($folders as $folder) {
$folderFiles = glob($folder . '/*.m3u8');
foreach ($folderFiles as $file) {
$fileContent = file_get_contents($file);
preg_match_all('/#EXT-X-SESSION-KEY/', $fileContent, $matches);
$onlineUsersCount += count($matches[0]);
}
}
return $onlineUsersCount;
}
// Create an array to store the data
$data = array(
'label' => date('Y-m-d H:i:s'),
'cpu' => getCPUUsage(),
'ram' => getRAMUsage(),
'network' => calculateNetworkSpeed(),
'diskUtilization' => getDiskUtilization(getActualBinDirectory()),
'channelsPlayedCount' => getChannelsPlayedCount(),
'totalChannelsCount' => getTotalChannelsCount(),
'onlineUsersCount' => getOnlineUsersCount()
);
// Encode the data as JSON and output it
header('Content-Type: application/json');
echo json_encode($data);
exit(); // Add this line to exit the script
?>