diff --git a/LibreNMS/IRCBot.php b/LibreNMS/IRCBot.php index f55184d21e..e205902c38 100644 --- a/LibreNMS/IRCBot.php +++ b/LibreNMS/IRCBot.php @@ -808,11 +808,11 @@ class IRCBot case 'devices': case 'device': case 'dev': - $devcount = array_pop(dbFetchRow('SELECT count(*) FROM devices'.$d_w)); - $devup = array_pop(dbFetchRow("SELECT count(*) FROM devices WHERE status = '1' AND `ignore` = '0'".$d_a)); - $devdown = array_pop(dbFetchRow("SELECT count(*) FROM devices WHERE status = '0' AND `ignore` = '0'".$d_a)); - $devign = array_pop(dbFetchRow("SELECT count(*) FROM devices WHERE `ignore` = '1'".$d_a)); - $devdis = array_pop(dbFetchRow("SELECT count(*) FROM devices WHERE `disabled` = '1'".$d_a)); + $devcount = dbFetchCell('SELECT count(*) FROM devices'.$d_w); + $devup = dbFetchCell("SELECT count(*) FROM devices WHERE status = '1' AND `ignore` = '0'".$d_a); + $devdown = dbFetchCell("SELECT count(*) FROM devices WHERE status = '0' AND `ignore` = '0'".$d_a); + $devign = dbFetchCell("SELECT count(*) FROM devices WHERE `ignore` = '1'".$d_a); + $devdis = dbFetchCell("SELECT count(*) FROM devices WHERE `disabled` = '1'".$d_a); if ($devup > 0) { $devup = $this->_color($devup, 'green'); } @@ -828,12 +828,12 @@ class IRCBot case 'ports': case 'port': case 'prt': - $prtcount = array_pop(dbFetchRow('SELECT count(*) FROM ports'.$p_w)); - $prtup = array_pop(dbFetchRow("SELECT count(*) FROM ports AS I, devices AS D WHERE I.ifOperStatus = 'up' AND I.ignore = '0' AND I.device_id = D.device_id AND D.ignore = '0'".$p_a)); - $prtdown = array_pop(dbFetchRow("SELECT count(*) FROM ports AS I, devices AS D WHERE I.ifOperStatus = 'down' AND I.ifAdminStatus = 'up' AND I.ignore = '0' AND D.device_id = I.device_id AND D.ignore = '0'".$p_a)); - $prtsht = array_pop(dbFetchRow("SELECT count(*) FROM ports AS I, devices AS D WHERE I.ifAdminStatus = 'down' AND I.ignore = '0' AND D.device_id = I.device_id AND D.ignore = '0'".$p_a)); - $prtign = array_pop(dbFetchRow("SELECT count(*) FROM ports AS I, devices AS D WHERE D.device_id = I.device_id AND (I.ignore = '1' OR D.ignore = '1')".$p_a)); - $prterr = array_pop(dbFetchRow("SELECT count(*) FROM ports AS I, devices AS D WHERE D.device_id = I.device_id AND (I.ignore = '0' OR D.ignore = '0') AND (I.ifInErrors_delta > '0' OR I.ifOutErrors_delta > '0')".$p_a)); + $prtcount = dbFetchCell('SELECT count(*) FROM ports'.$p_w); + $prtup = dbFetchCell("SELECT count(*) FROM ports AS I, devices AS D WHERE I.ifOperStatus = 'up' AND I.ignore = '0' AND I.device_id = D.device_id AND D.ignore = '0'".$p_a); + $prtdown = dbFetchCell("SELECT count(*) FROM ports AS I, devices AS D WHERE I.ifOperStatus = 'down' AND I.ifAdminStatus = 'up' AND I.ignore = '0' AND D.device_id = I.device_id AND D.ignore = '0'".$p_a); + $prtsht = dbFetchCell("SELECT count(*) FROM ports AS I, devices AS D WHERE I.ifAdminStatus = 'down' AND I.ignore = '0' AND D.device_id = I.device_id AND D.ignore = '0'".$p_a); + $prtign = dbFetchCell("SELECT count(*) FROM ports AS I, devices AS D WHERE D.device_id = I.device_id AND (I.ignore = '1' OR D.ignore = '1')".$p_a); +// $prterr = dbFetchCell("SELECT count(*) FROM ports AS I, devices AS D WHERE D.device_id = I.device_id AND (I.ignore = '0' OR D.ignore = '0') AND (I.ifInErrors_delta > '0' OR I.ifOutErrors_delta > '0')".$p_a); if ($prtup > 0) { $prtup = $this->_color($prtup, 'green'); } @@ -849,36 +849,24 @@ class IRCBot case 'services': case 'service': case 'srv': - $srvcount = array_pop(dbFetchRow('SELECT count(service_id) FROM services'.$d_w)); - $srvup = array_pop(dbFetchRow("SELECT count(service_id) FROM services WHERE service_status = '0' AND service_ignore ='0'".$d_a)); - $srvdown = array_pop(dbFetchRow("SELECT count(service_id) FROM services WHERE service_status = '2' AND service_ignore = '0'".$d_a)); - $srvwarn = array_pop(dbFetchRow("SELECT count(service_id) FROM services WHERE service_status = '1' AND service_ignore = '0'".$d_a)); - $srvunknown = array_pop(dbFetchRow("SELECT count(service_id) FROM services WHERE service_status = '3' AND service_ignore = '0'".$d_a)); - $srvign = array_pop(dbFetchRow("SELECT count(service_id) FROM services WHERE service_ignore = '1'".$d_a)); - $srvdis = array_pop(dbFetchRow("SELECT count(service_id) FROM services WHERE service_disabled = '1'".$d_a)); - $srvcountcolor = array($srvdown, $srvwarn, $srvunknown); - if ($srvup > 0) { - $srvup = $this->_color($srvup, 'green'); + $status_counts = array(); + $status_colors = array(0 => 'green', 3 => 'lightblue', 1 => 'yellow', 2 => 'red'); + $srvcount = dbFetchCell('SELECT COUNT(*) FROM services'.$d_w); + $srvign = dbFetchCell("SELECT COUNT(*) FROM services WHERE service_ignore = 1".$d_a); + $srvdis = dbFetchCell("SELECT COUNT(*) FROM services WHERE service_disabled = 1".$d_a); + $service_status = dbFetchRows("SELECT `service_status`, COUNT(*) AS `count` FROM `services` WHERE `service_disabled`=0 AND `service_ignore`=0 $d_a GROUP BY `service_status`"); + $service_status = array_combine(array_column($service_status, 'service_status'), array_column($service_status, 'count')); // key by status + + foreach ($status_colors as $status => $color) { + if (isset($service_status[$status])) { + $status_counts[$status] = $this->_color($service_status[$status], $color); + $srvcount = $this->_color($srvcount, $color, null, 'bold'); // upgrade the main count color + } else { + $status_counts[$status] = 0; + } } - if ($srvdown > 0) { - $srvdown = $this->_color($srvdown, 'red'); - } - if ($srvwarn > 0) { - $srvwarn = $this->_color($srvwarn, 'yellow'); - } - if ($srvunknown > 0) { - $srvunknown = $this->_color($srvunknown, 'lightblue'); - } - if ($srvcountcolor[0] > 0) { - $srvcount = $this->_color($srvcount, 'red', null, 'bold'); - } elseif ($srvcountcolor[1] > 0) { - $srvcount = $this->_color($srvcount, 'yellow', null, 'bold'); - } elseif ($srvcountcolor[2] > 0) { - $srvcount = $this->_color($srvcount, 'lightblue', null, 'bold'); - } else { - $srvcount = $this->_color($srvcount, 'green', null, 'bold'); - } - $msg = 'Services: '.$srvcount.' ('.$srvup.' up, '.$srvdown.' down, '.$srvwarn.' warning, '.$srvunknown.' unknown, '.$srvign.' ignored, '.$srvdis.' disabled'.')'; + + $msg = "Services: $srvcount ({$status_counts[0]} up, {$status_counts[2]} down, {$status_counts[1]} warning, {$status_counts[3]} unknown, $srvign ignored, $srvdis disabled)"; break; default: