Skip to content

Commit dd9650c

Browse files
authored
add cpu cores/processes information (#335)
* add cpu cores/processes * update
1 parent e3119a2 commit dd9650c

File tree

2 files changed

+102
-2
lines changed

2 files changed

+102
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* feature: Add Boost graphs panels - memory usage, pending records
1616
* feature: Add Syslog graphs panels - levels, number of messages
1717
* feature#331: Add variable panel height
18+
* feature#335: Add CPU cores/processes information
1819

1920
--- 4.0.4 ---
2021

panellib/analyze.php

+101-2
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,6 @@ function analyse_tree_host_graph($panel, $user_id) {
930930
}
931931
}
932932

933-
934933
$data = db_fetch_assoc('SELECT
935934
gt.id, gt.name, COUNT(DISTINCT graphs.local_graph_id) AS graphs,
936935
graphs.items AS graph_items, templates.items AS template_items
@@ -962,7 +961,6 @@ function analyse_tree_host_graph($panel, $user_id) {
962961
$panel['data'] .= '<span class="inpa_sq color_red"></span>' . __('Graph items/template items issue: %s', $sql_count, 'intropage') . '<br/>';
963962
}
964963

965-
966964
if (api_plugin_is_enabled('monitor')) {
967965
if ($allowed_devices !== false || $simple_perms) {
968966
if (!$simple_perms) {
@@ -990,6 +988,56 @@ function analyse_tree_host_graph($panel, $user_id) {
990988
}
991989
}
992990

991+
$cpu_cores = 0;
992+
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
993+
$output = shell_exec('wmic cpu get NumberOfCores');
994+
if (!is_null($output) && $output !== false) {
995+
$lines = preg_split('/\r\n|\r|\n/', $output);
996+
$cpu_cores = $lines[1];
997+
}
998+
} elseif (substr_count(strtolower(PHP_OS), 'darwin')) {
999+
$cpu_cores = shell_exec('sysctl -n hw.ncpu');
1000+
} else {
1001+
if (file_exists('/usr/bin/nproc')) {
1002+
$cpu_cores = shell_exec('/usr/bin/nproc');
1003+
} elseif (file_exists('/bin/nproc')) {
1004+
$cpu_cores = shell_exec('/bin/nproc');
1005+
} else {
1006+
$output = shell_exec('nproc');
1007+
if (!is_null($output) && $output !== false) {
1008+
$cpu_cores = $output;
1009+
}
1010+
}
1011+
}
1012+
$cpu_cores = trim($cpu_cores);
1013+
1014+
$sett = db_fetch_row('SELECT processes, threads FROM poller WHERE id = 1');
1015+
$color = 'green';
1016+
$text = __('OK');
1017+
1018+
if ($cpu_cores == 0) {
1019+
if ($sett['processes'] == 1 || $sett['threads'] == 1) {
1020+
$color = 'yellow';
1021+
$text = 'Cannot determine number of CPU cores. You have set only 1 process or thread for poller. You may have performance problems.';
1022+
}
1023+
} elseif ($cpu_cores == 1 && ($sett['processes'] == 1 || $sett['threads'] == 1)) {
1024+
$color = 'yellow';
1025+
$text = 'You have set only 1 process or thread for poller. You may have performance problems. Try to increase poller processes or threads.';
1026+
$total_errors++;
1027+
} elseif ($cpu_cores > 1) {
1028+
if ($sett['processes'] == 1 || $sett['threads'] == 1) {
1029+
$color = 'red';
1030+
$text = 'You have set only 1 process or thread for poller. You may have performance problems. Try to increase poller processes or threads.';
1031+
$total_errors++;
1032+
} elseif ($sett['processes']/$cpu_cores < 0.4 || $sett['threads']/$cpu_cores < 0.4) {
1033+
$color = 'yellow';
1034+
$text = 'You are using less than half of CPU cores. For better performance, consider increasing poller processes or threads. ';
1035+
}
1036+
}
1037+
1038+
$panel['data'] .= '<span class="inpa_sq color_' . $color . '"></span>' . __('Server CPU cores / processes / threads: %s / %s / %s', $cpu_cores, $sett['processes'], $sett['threads'], 'intropage');
1039+
$panel['data'] .= display_tooltip($text) . '<br/>';
1040+
9931041
if ($total_errors > 0) {
9941042
$panel['data'] = '<table class="cactiTable">
9951043
<tr>
@@ -1950,6 +1998,57 @@ function analyse_tree_host_graph_detail() {
19501998
}
19511999
}
19522000

2001+
$cpu_cores = 0;
2002+
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
2003+
$output = shell_exec('wmic cpu get NumberOfCores');
2004+
if (!is_null($output) && $output !== false) {
2005+
$lines = preg_split('/\r\n|\r|\n/', $output);
2006+
$cpu_cores = $lines[1];
2007+
}
2008+
} elseif (substr_count(strtolower(PHP_OS), 'darwin')) {
2009+
$cpu_cores = shell_exec('sysctl -n hw.ncpu');
2010+
} else {
2011+
if (file_exists('/usr/bin/nproc')) {
2012+
$cpu_cores = shell_exec('/usr/bin/nproc');
2013+
} elseif (file_exists('/bin/nproc')) {
2014+
$cpu_cores = shell_exec('/bin/nproc');
2015+
} else {
2016+
$output = shell_exec('nproc');
2017+
if (!is_null($output) && $output !== false) {
2018+
$cpu_cores = $output;
2019+
}
2020+
}
2021+
}
2022+
$cpu_cores = trim($cpu_cores);
2023+
2024+
$sett = db_fetch_row('SELECT processes, threads FROM poller WHERE id = 1');
2025+
$color = 'green';
2026+
$text = __('OK');
2027+
2028+
if ($cpu_cores == 0) {
2029+
if ($sett['processes'] == 1 || $sett['threads'] == 1) {
2030+
$color = 'yellow';
2031+
$text = 'Cannot determine number of CPU cores. You have set only 1 process or thread for poller. You may have performance problems.';
2032+
}
2033+
} elseif ($cpu_cores == 1 && ($sett['processes'] == 1 || $sett['threads'] == 1)) {
2034+
$color = 'yellow';
2035+
$text = 'You have set only 1 process or thread for poller. You may have performance problems. Try to increase poller processes or threads.';
2036+
$total_errors++;
2037+
} elseif ($cpu_cores > 1) {
2038+
if ($sett['processes'] == 1 || $sett['threads'] == 1) {
2039+
$color = 'red';
2040+
$text = 'You have set only 1 process or thread for poller. You may have performance problems. Try to increase poller processes or threads.';
2041+
$total_errors++;
2042+
} elseif ($sett['processes']/$cpu_cores < 0.4 || $sett['threads']/$cpu_cores < 0.4) {
2043+
$color = 'yellow';
2044+
$text = 'You are using less than half of CPU cores. For better performance, consider increasing poller processes or threads. ';
2045+
}
2046+
}
2047+
2048+
$panel['detail'] .= '<span class="inpa_sq color_' . $color . '"></span>' . __('Server CPU cores / processes / threads: %s / %s / %s', $cpu_cores, $sett['processes'], $sett['threads'], 'intropage');
2049+
$panel['detail'] .= display_tooltip($text) . '<br/>';
2050+
2051+
19532052
if ($total_errors > 0) {
19542053
$panel['detail'] = '<span class="txt_big">' . __('Found %s problems', $total_errors, 'intropage') . '</span><br/>' . $panel['detail'];
19552054
} else {

0 commit comments

Comments
 (0)