Update system_status widget add memory details

Completed work on disk usage percentage
This commit is contained in:
FusionPBX 2024-09-13 19:15:15 -06:00 committed by GitHub
parent aad87759ac
commit 8e9527a1c1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 164 additions and 73 deletions

View File

@ -24,7 +24,84 @@
$row_style["0"] = "row_style0";
$row_style["1"] = "row_style1";
//disk usage
//get the CPU details
if (stristr(PHP_OS, 'BSD') || stristr(PHP_OS, 'Linux')) {
$result = shell_exec('ps -A -o pcpu');
$percent_cpu = 0;
foreach (explode("\n", $result) as $value) {
if (is_numeric($value)) { $percent_cpu = $percent_cpu + $value; }
}
if (stristr(PHP_OS, 'BSD')) {
$result = shell_exec("dmesg | grep -i --max-count 1 CPUs | sed 's/[^0-9]*//g'");
$cpu_cores = trim($result);
}
if (stristr(PHP_OS, 'Linux')) {
$result = @trim(shell_exec("grep -P '^processor' /proc/cpuinfo"));
$cpu_cores = count(explode("\n", $result));
}
if ($cpu_cores > 1) { $percent_cpu = $percent_cpu / $cpu_cores; }
$percent_cpu = round($percent_cpu, 2);
//uptime
$result = shell_exec('uptime');
$load_average = sys_getloadavg();
}
//get memory details
function get_memory_details() {
if (PHP_OS == 'Linux') {
$meminfo = file_get_contents("/proc/meminfo");
$data = [];
foreach (explode("\n", $meminfo) as $line) {
if (preg_match('/^(\w+):\s+(\d+)\skB$/', $line, $matches)) {
$data[$matches[1]] = $matches[2];
}
}
if (isset($data['MemTotal']) && isset($data['MemAvailable'])) {
$array['total_memory'] = $data['MemTotal'];
$array['available_memory'] = $data['MemAvailable'];
$array['used_memory'] = $array['total_memory'] - $array['available_memory'];
$array['memory_usage'] = ($array['used_memory'] / $array['total_memory']) * 100;
$array['memory_percent'] = round($array['memory_usage'], 2);
return $array;
}
}
if (PHP_OS == 'FreeBSD') {
//define the output array
$output = [];
// get the memory information using sysctl
exec('sysctl -n hw.physmem hw.pagesize vm.stats.vm.v_free_count vm.stats.vm.v_inactive_count vm.stats.vm.v_cache_count vm.stats.vm.v_wire_count', $output);
if (count($output) === 6) {
list($array['total_memory'], $page_size, $free_pages, $inactive_pages, $cache_pages, $wired_pages) = $output;
// total memory in bytes
$array['total_memory'] = (int)$array['total_memory'];
// pages to bytes conversion
$array['available_memory'] = ($free_pages + $inactive_pages + $cache_pages) * (int)$page_size;
$array['used_memory'] = $array['total_memory'] - $array['available_memory'];
// calculate memory usage percentage
$array['memory_usage'] = ($array['used_memory'] / $array['total_memory']) * 100;
$array['memory_percent'] = round($array['memory_usage'], 2) . '%';
return $array;
}
}
return false;
}
$memory_details = get_memory_details();
//disk usage
if (PHP_OS == 'FreeBSD' || PHP_OS == 'Linux') {
$tmp = shell_exec("df / 2>&1");
$tmp = explode("\n", $tmp);
@ -33,88 +110,102 @@
foreach ($tmp as $stat) {
if (substr_count($stat, '%') > 0) { $percent_disk_usage = rtrim($stat,'%'); break; }
}
}
if (!empty($percent_disk_usage)) {
//add half doughnut chart
echo " <div class='hud_content' ".($dashboard_details_state == "disabled" ?: "onclick=\"$('#hud_system_status_details').slideToggle('fast'); toggle_grid_row_end('".$dashboard_name."')\"").">\n";
echo " <span class='hud_title'><a onclick=\"document.location.href='".PROJECT_PATH."/app/system/system.php'\">".$text['label-system_status']."</a></span>\n";
//show the results
echo " <div class='hud_content' ".($dashboard_details_state == "disabled" ?: "onclick=\"$('#hud_system_status_details').slideToggle('fast'); toggle_grid_row_end('".$dashboard_name."')\"").">\n";
echo " <span class='hud_title'><a onclick=\"document.location.href='".PROJECT_PATH."/app/system/system.php'\">".$text['label-system_status']."</a></span>\n";
if (!isset($dashboard_chart_type) || $dashboard_chart_type == "doughnut") {
?>
<div class='hud_chart' style='width: 175px;'><canvas id='system_status_chart'></canvas></div>
if (!isset($dashboard_chart_type) || $dashboard_chart_type == "doughnut") {
?>
<div class='hud_chart' style='width: 175px;'><canvas id='system_status_chart'></canvas></div>
<script>
const system_status_chart = new Chart(
document.getElementById('system_status_chart').getContext('2d'),
{
type: 'doughnut',
data: {
datasets: [{
data: ['<?php echo $percent_disk_usage; ?>', 100 - '<?php echo $percent_disk_usage; ?>'],
backgroundColor: [
<?php
if ($percent_disk_usage <= 80) {
echo "'".($settings->get('theme', 'dashboard_disk_usage_chart_main_color')[0] ?? '#03c04a')."',\n";
} else if ($percent_disk_usage <= 90) {
echo "'".($settings->get('theme', 'dashboard_disk_usage_chart_main_color')[1] ?? '#ff9933')."',\n";
} else if ($percent_disk_usage > 90) {
echo "'".($settings->get('theme', 'dashboard_disk_usage_chart_main_color')[2] ?? '#ea4c46')."',\n";
}
?>
'<?php echo ($settings->get('theme', 'dashboard_disk_usage_chart_sub_color') ?? '#d4d4d4'); ?>'
],
borderColor: '<?php echo $settings->get('theme', 'dashboard_chart_border_color'); ?>',
borderWidth: '<?php echo $settings->get('theme', 'dashboard_chart_border_width'); ?>',
}]
},
options: {
circumference: 180,
rotation: 270,
plugins: {
chart_number_2: {
text: '<?php echo round($percent_disk_usage); ?>'
}
<script>
const system_status_chart = new Chart(
document.getElementById('system_status_chart').getContext('2d'),
{
type: 'doughnut',
data: {
datasets: [{
data: ['<?php echo $percent_disk_usage; ?>', 100 - '<?php echo $percent_disk_usage; ?>'],
backgroundColor: [
<?php
if ($percent_disk_usage <= 80) {
echo "'".($settings->get('theme', 'dashboard_disk_usage_chart_main_color')[0] ?? '#03c04a')."',\n";
} else if ($percent_disk_usage <= 90) {
echo "'".($settings->get('theme', 'dashboard_disk_usage_chart_main_color')[1] ?? '#ff9933')."',\n";
} else if ($percent_disk_usage > 90) {
echo "'".($settings->get('theme', 'dashboard_disk_usage_chart_main_color')[2] ?? '#ea4c46')."',\n";
}
},
plugins: [{
id: 'chart_number_2',
beforeDraw(chart, args, options){
const {ctx, chartArea: {top, right, bottom, left, width, height} } = chart;
ctx.font = chart_text_size + ' ' + chart_text_font;
ctx.textBaseline = 'middle';
ctx.textAlign = 'center';
ctx.fillStyle = '<?php echo $dashboard_number_text_color; ?>';
ctx.fillText(options.text + '%', width / 2, top + (height / 2) + 35);
ctx.save();
}
}]
?>
'<?php echo ($settings->get('theme', 'dashboard_disk_usage_chart_sub_color') ?? '#d4d4d4'); ?>'
],
borderColor: '<?php echo $settings->get('theme', 'dashboard_chart_border_color'); ?>',
borderWidth: '<?php echo $settings->get('theme', 'dashboard_chart_border_width'); ?>',
}]
},
options: {
circumference: 180,
rotation: 270,
plugins: {
chart_number_2: {
text: '<?php echo round($percent_disk_usage); ?>'
}
}
);
</script>
<?php
}
if ($dashboard_chart_type == "number") {
echo " <span class='hud_stat'>".round($percent_disk_usage)."%</span>";
}
if ($dashboard_chart_type == "progress_bar") {
echo " <span class='hud_title' style='text-align: left; font-size: 11px; line-height: 1.8; font-weight: unset; padding-left: 41px;'>".$text['label-disk_usage']."</span>\n";
echo " <div class='progress_container' style='width: 80%; height: 15px; border-radius: 4px; background: ".($settings->get('theme', 'dashboard_disk_usage_chart_sub_color') ?? '#d4d4d4').";'>\n";
echo " <div class='progress_bar' style='width: ".$percent_disk_usage."%; height: 15px; border-radius: 4px; font-size: x-small; color: ".$row['dashboard_number_text_color']."; background: ".($settings->get('theme', 'dashboard_disk_usage_chart_main_color') ?? '#03c04a').";'>".round($percent_disk_usage)."%</div>\n";
echo " </div>\n";
}
/*
if ($dashboard_chart_type == "progress_bar") {
echo " <span class='hud_title' style='text-align: left; font-size: 11px; line-height: 1.8; font-weight: unset; padding-left: 41px;'>".$text['label-cpu_usage']."</span>\n";
echo " <div class='progress_container' style='width: 80%; height: 15px; border-radius: 4px; background: ".($settings->get('theme', 'dashboard_cpu_usage_chart_sub_color') ?? '#d4d4d4').";'>\n";
echo " <div class='progress_bar' style='width: ".$percent_cpu."%; height: 15px; border-radius: 4px; font-size: x-small; color: ".$row['dashboard_number_text_color']."; background: ".($settings->get('theme', 'dashboard_cpu_usage_chart_main_color') ?? '#03c04a').";'>".round($percent_cpu)."%</div>\n";
echo " </div>\n";
}
*/
},
plugins: [{
id: 'chart_number_2',
beforeDraw(chart, args, options){
const {ctx, chartArea: {top, right, bottom, left, width, height} } = chart;
ctx.font = chart_text_size + ' ' + chart_text_font;
ctx.textBaseline = 'middle';
ctx.textAlign = 'center';
ctx.fillStyle = '<?php echo $dashboard_number_text_color; ?>';
ctx.fillText(options.text + '%', width / 2, top + (height / 2) + 35);
ctx.save();
}
}]
}
);
</script>
<?php
}
if ($dashboard_chart_type == "number") {
echo " <span class='hud_stat'>".round($percent_disk_usage)."%</span>";
}
if ($dashboard_chart_type == "progress_bar") {
//cpu usage
if ($dashboard_row_span > 1) {
echo " <span class='hud_title cpu_usage' style='text-align: left; font-size: 11px; line-height: 1.8; font-weight: unset; padding-left: 9%;'>".$text['label-processor_usage']." $dashboard_row_span</span>\n";
echo " <div class='progress_container' style='width: 80%; height: 15px; border-radius: 4px; background: ".($settings->get('theme', 'dashboard_cpu_usage_chart_sub_color') ?? '#d4d4d4').";'>\n";
echo " <div class='progress_bar' style='width: ".$percent_cpu."%; height: 15px; border-radius: 4px; font-size: x-small; color: ".$row['dashboard_number_text_color']."; background: ".($settings->get('theme', 'dashboard_cpu_usage_chart_main_color') ?? '#03c04a').";'>".round($percent_cpu)."%</div>\n";
echo " </div>\n";
echo " <div style='height: 20px'>&nbsp;</div>\n";
}
//disk usage
if ($dashboard_row_span >= 1) {
echo " <span class='hud_title' style='text-align: left; font-size: 11px; line-height: 1.8; font-weight: unset; padding-left: 9%;'>".$text['label-disk_usage']."</span>\n";
echo " <div class='progress_container' style='width: 80%; height: 15px; border-radius: 4px; background: ".($settings->get('theme', 'dashboard_disk_usage_chart_sub_color') ?? '#d4d4d4').";'>\n";
echo " <div class='progress_bar' style='width: ".$percent_disk_usage."%; height: 15px; border-radius: 4px; font-size: x-small; color: ".$row['dashboard_number_text_color']."; background: ".($settings->get('theme', 'dashboard_disk_usage_chart_main_color') ?? '#03c04a').";'>".round($percent_disk_usage)."%</div>\n";
echo " </div>\n";
echo " <div style='height: 20px'>&nbsp;</div>\n";
}
//percent memory
if ($dashboard_row_span > 1) {
echo " <span class='hud_title' style='text-align: left; font-size: 11px; line-height: 1.8; font-weight: unset; padding-left: 9%;'>".$text['label-memory_usage']."</span>\n";
echo " <div class='progress_container' style='width: 80%; height: 15px; border-radius: 4px; background: ".($settings->get('theme', 'dashboard_disk_usage_chart_sub_color') ?? '#d4d4d4').";'>\n";
echo " <div class='progress_bar' style='width: ".round($memory_details['memory_percent'])."%; height: 15px; border-radius: 4px; font-size: x-small; color: ".$row['dashboard_number_text_color']."; background: ".($settings->get('theme', 'dashboard_disk_usage_chart_main_color') ?? '#03c04a').";'>".round($memory_details['memory_percent'])."%</div>\n";
echo " </div>\n";
}
}
echo " </div>\n";
if ($dashboard_details_state != 'disabled') {
echo "<div class='hud_details hud_box' id='hud_system_status_details'>";
echo "<table class='tr_hover' width='100%' cellpadding='0' cellspacing='0' border='0'>\n";