diff --git a/html/includes/object-cache.inc.php b/html/includes/object-cache.inc.php index 45abaf45af..b6b9a50840 100644 --- a/html/includes/object-cache.inc.php +++ b/html/includes/object-cache.inc.php @@ -1,5 +1,7 @@ = 5) -{ - $devices['count'] = dbFetchCell("SELECT COUNT(*) FROM devices"); - $devices['up'] = dbFetchCell("SELECT COUNT(*) FROM devices WHERE `status` = '1' AND `ignore` = '0' AND `disabled` = '0'"); - $devices['down'] = dbFetchCell("SELECT COUNT(*) FROM devices WHERE `status` = '0' AND `ignore` = '0' AND `disabled` = '0'"); - $devices['ignored'] = dbFetchCell("SELECT COUNT(*) FROM devices WHERE `ignore` = '1' AND `disabled` = '0'"); - $devices['disabled'] = dbFetchCell("SELECT COUNT(*) FROM devices WHERE `disabled` = '1'"); - - $ports['count'] = dbFetchCell("SELECT COUNT(*) FROM ports WHERE `deleted` = '0'"); - $ports['up'] = dbFetchCell("SELECT COUNT(*) FROM ports AS I, devices AS D WHERE I.`deleted` = '0' AND D.`device_id` = I.`device_id` AND I.`ignore` = '0' AND D.`ignore` = '0' AND I.`ifOperStatus` = 'up'"); - $ports['down'] = dbFetchCell("SELECT COUNT(*) FROM ports AS I, devices AS D WHERE I.`deleted` = '0' AND D.`device_id` = I.`device_id` AND I.`ignore` = '0' AND D.`ignore` = '0' AND I.`ifOperStatus` = 'down' AND I.`ifAdminStatus` = 'up'"); - $ports['shutdown'] = dbFetchCell("SELECT COUNT(*) FROM ports AS I, devices AS D WHERE I.`deleted` = '0' AND D.`device_id` = I.`device_id` AND I.`ignore` = '0' AND D.`ignore` = '0' AND I.`ifAdminStatus` = 'down'"); - $ports['errored'] = dbFetchCell("SELECT COUNT(*) FROM ports AS I, devices AS D WHERE I.`deleted` = '0' AND D.`device_id` = I.`device_id` AND I.`ignore` = '0' AND D.`ignore` = '0' AND (I.`ifInErrors_delta` > '0' OR I.`ifOutErrors_delta` > '0')"); - $ports['ignored'] = dbFetchCell("SELECT COUNT(*) FROM ports AS I, devices AS D WHERE I.`deleted` = '0' AND D.`device_id` = I.`device_id` AND (I.`ignore` = '1' OR D.`ignore` = '1')"); - - $services['count'] = dbFetchCell("SELECT COUNT(*) FROM services"); - $services['up'] = dbFetchCell("SELECT COUNT(*) FROM services WHERE `service_ignore` = '0' AND `service_disabled` = '0' AND `service_status` = '1'"); - $services['down'] = dbFetchCell("SELECT COUNT(*) FROM services WHERE `service_ignore` = '0' AND `service_disabled` = '0' AND `service_status` = '0'"); - $services['ignored'] = dbFetchCell("SELECT COUNT(*) FROM services WHERE `service_ignore` = '1' AND `service_disabled` = '0'"); - $services['disabled'] = dbFetchCell("SELECT COUNT(*) FROM services WHERE `service_disabled` = '1'"); -} -else -{ - $devices['count'] = dbFetchCell("SELECT COUNT(*) FROM devices AS D, devices_perms AS P WHERE P.`user_id` = ? AND P.`device_id` = D.`device_id`", array($_SESSION['user_id'])); - $devices['up'] = dbFetchCell("SELECT COUNT(*) FROM devices AS D, devices_perms AS P WHERE P.`user_id` = ? AND P.`device_id` = D.`device_id` AND D.`status` = '1' AND D.`ignore` = '0' AND D.`disabled` = '0'", array($_SESSION['user_id'])); - $devices['down'] = dbFetchCell("SELECT COUNT(*) FROM devices AS D, devices_perms AS P WHERE P.`user_id` = ? AND P.`device_id` = D.`device_id` AND D.`status` = '0' AND D.`ignore` = '0' AND D.`disabled` = '0'", array($_SESSION['user_id'])); - $devices['ignored'] = dbFetchCell("SELECT COUNT(*) FROM devices AS D, devices_perms AS P WHERE P.`user_id` = ? AND P.`device_id` = D.`device_id` AND D.`ignore` = '1' AND D.`disabled` = '0'", array($_SESSION['user_id'])); - $devices['disabled'] = dbFetchCell("SELECT COUNT(*) FROM devices AS D, devices_perms AS P WHERE P.`user_id` = ? AND P.`device_id` = D.`device_id` AND D.`disabled` = '1'", array($_SESSION['user_id'])); - - $ports['count'] = dbFetchCell("SELECT COUNT(*) FROM ports AS I, devices AS D, devices_perms AS P WHERE I.`deleted` = '0' AND P.`user_id` = ? AND P.`device_id` = D.`device_id` AND I.`device_id` = D.`device_id`", array($_SESSION['user_id'])); - $ports['up'] = dbFetchCell("SELECT COUNT(*) FROM ports AS I, devices AS D, devices_perms AS P WHERE I.`deleted` = '0' AND P.`user_id` = ? AND P.`device_id` = D.`device_id` AND I.`device_id` = D.`device_id` AND I.`ignore` = '0' AND D.`ignore` = '0' AND I.`ifOperStatus` = 'up'", array($_SESSION['user_id'])); - $ports['down'] = dbFetchCell("SELECT COUNT(*) FROM ports AS I, devices AS D, devices_perms AS P WHERE I.`deleted` = '0' AND P.`user_id` = ? AND P.`device_id` = D.`device_id` AND I.`device_id` = D.`device_id` AND I.`ignore` = '0' AND D.`ignore` = '0' AND I.`ifOperStatus` = 'down' AND I.`ifAdminStatus` = 'up'", array($_SESSION['user_id'])); - $ports['shutdown'] = dbFetchCell("SELECT COUNT(*) FROM ports AS I, devices AS D, devices_perms AS P WHERE I.`deleted` = '0' AND P.`user_id` = ? AND P.`device_id` = D.`device_id` AND I.`device_id` = D.`device_id` AND I.`ignore` = '0' AND D.`ignore` = '0' AND I.`ifAdminStatus` = 'down'", array($_SESSION['user_id'])); - $ports['errored'] = dbFetchCell("SELECT COUNT(*) FROM ports AS I, devices AS D, devices_perms AS P WHERE I.`deleted` = '0' AND P.`user_id` = ? AND P.`device_id` = D.`device_id` AND I.`device_id` = D.`device_id` AND I.`ignore` = '0' AND D.`ignore` = '0' AND (I.`ifInErrors_delta` > '0' OR I.`ifOutErrors_delta` > '0')", array($_SESSION['user_id'])); - $ports['ignored'] = dbFetchCell("SELECT COUNT(*) FROM ports AS I, devices AS D, devices_perms AS P WHERE I.`deleted` = '0' AND P.`user_id` = ? AND P.`device_id` = D.`device_id` AND I.`device_id` = D.`device_id` AND (I.`ignore` = '1' OR D.`ignore` = '1')", array($_SESSION['user_id'])); - - $services['count'] = dbFetchCell("SELECT COUNT(*) FROM services AS S, devices AS D, devices_perms AS P WHERE P.`user_id` = ? AND P.`device_id` = D.`device_id` AND S.`device_id` = D.`device_id`", array($_SESSION['user_id'])); - $services['up'] = dbFetchCell("SELECT COUNT(*) FROM services AS S, devices AS D, devices_perms AS P WHERE P.`user_id` = ? AND P.`device_id` = D.`device_id` AND S.`device_id` = D.`device_id` AND S.`service_ignore` = '0' AND S.`service_disabled` = '0' AND S.`service_status` = '1'", array($_SESSION['user_id'])); - $services['down'] = dbFetchCell("SELECT COUNT(*) FROM services AS S, devices AS D, devices_perms AS P WHERE P.`user_id` = ? AND P.`device_id` = D.`device_id` AND S.`device_id` = D.`device_id` AND S.`service_ignore` = '0' AND S.`service_disabled` = '0' AND S.`service_status` = '0'", array($_SESSION['user_id'])); - $services['ignored'] = dbFetchCell("SELECT COUNT(*) FROM services AS S, devices AS D, devices_perms AS P WHERE P.`user_id` = ? AND P.`device_id` = D.`device_id` AND S.`device_id` = D.`device_id` AND S.`service_ignore` = '1' AND S.`service_disabled` = '0'", array($_SESSION['user_id'])); - $services['disabled'] = dbFetchCell("SELECT COUNT(*) FROM services AS S, devices AS D, devices_perms AS P WHERE P.`user_id` = ? AND P.`device_id` = D.`device_id` AND S.`device_id` = D.`device_id` AND S.`service_disabled` = '1'", array($_SESSION['user_id'])); -} +$devices = new ObjCache('devices'); +$ports = new ObjCache('ports'); +$services = new ObjCache('services'); if ($devices['down']) { $devices['bgcolour'] = "#ffcccc"; } else { $devices['bgcolour'] = "transparent"; } if ($ports['down']) { $ports['bgcolour'] = "#ffcccc"; } else { $ports['bgcolour'] = "#e5e5e5"; } diff --git a/includes/caches/devices.inc.php b/includes/caches/devices.inc.php new file mode 100644 index 0000000000..b8b5574eb8 --- /dev/null +++ b/includes/caches/devices.inc.php @@ -0,0 +1,52 @@ += 5 ) { + + $data['count'] = array( + 'query' => "SELECT COUNT(*) FROM devices", + ); + + $data['up'] = array( + 'query' => "SELECT COUNT(*) FROM devices WHERE `status` = '1' AND `ignore` = '0' AND `disabled` = '0'", + ); + + $data['down'] = array( + 'query' => "SELECT COUNT(*) FROM devices WHERE `status` = '0' AND `ignore` = '0' AND `disabled` = '0'", + ); + + $data['ignored'] = array( + 'query' => "SELECT COUNT(*) FROM devices WHERE `ignore` = '1' AND `disabled` = '0'", + ); + + $data['disabled'] = array( + 'query' => "SELECT COUNT(*) FROM devices WHERE `disabled` = '1'", + ); + +} else { + + $data['count'] = array( + 'query' => "SELECT COUNT(*) FROM devices AS D, devices_perms AS P WHERE P.`user_id` = ? AND P.`device_id` = D.`device_id`", + 'params' => array($_SESSION['user_id']) + ); + + $data['up'] = array( + 'query' => "SELECT COUNT(*) FROM devices AS D, devices_perms AS P WHERE P.`user_id` = ? AND P.`device_id` = D.`device_id` AND D.`status` = '1' AND D.`ignore` = '0' AND D.`disabled` = '0'", + 'params' => array($_SESSION['user_id']) + ); + + $data['down'] = array( + 'query' => "SELECT COUNT(*) FROM devices AS D, devices_perms AS P WHERE P.`user_id` = ? AND P.`device_id` = D.`device_id` AND D.`status` = '0' AND D.`ignore` = '0' AND D.`disabled` = '0'", + 'params' => array($_SESSION['user_id']) + ); + + $data['ignored'] = array( + 'query' => "SELECT COUNT(*) FROM devices AS D, devices_perms AS P WHERE P.`user_id` = ? AND P.`device_id` = D.`device_id` AND D.`ignore` = '1' AND D.`disabled` = '0'", + 'params' => array($_SESSION['user_id']) + ); + + $data['disabled'] = array( + 'query' => "SELECT COUNT(*) FROM devices AS D, devices_perms AS P WHERE P.`user_id` = ? AND P.`device_id` = D.`device_id` AND D.`disabled` = '1'", + 'params' => array($_SESSION['user_id']) + ); + +} diff --git a/includes/caches/ports.inc.php b/includes/caches/ports.inc.php new file mode 100644 index 0000000000..a9d4508f5b --- /dev/null +++ b/includes/caches/ports.inc.php @@ -0,0 +1,61 @@ += 5 ) { + + $data['count'] = array( + 'query' => "SELECT COUNT(*) FROM ports WHERE `deleted` = '0'", + ); + + $data['up'] = array( + 'query' => "SELECT COUNT(*) FROM ports AS I, devices AS D WHERE I.`deleted` = '0' AND D.`device_id` = I.`device_id` AND I.`ignore` = '0' AND D.`ignore` = '0' AND I.`ifOperStatus` = 'up'", + ); + + $data['down'] = array( + 'query' => "SELECT COUNT(*) FROM ports AS I, devices AS D WHERE I.`deleted` = '0' AND D.`device_id` = I.`device_id` AND I.`ignore` = '0' AND D.`ignore` = '0' AND I.`ifOperStatus` = 'down' AND I.`ifAdminStatus` = 'up'", + ); + + $data['shutdown'] = array( + 'query' => "SELECT COUNT(*) FROM ports AS I, devices AS D WHERE I.`deleted` = '0' AND D.`device_id` = I.`device_id` AND I.`ignore` = '0' AND D.`ignore` = '0' AND I.`ifAdminStatus` = 'down'", + ); + + $data['errored'] = array( + 'query' => "SELECT COUNT(*) FROM ports AS I, devices AS D WHERE I.`deleted` = '0' AND D.`device_id` = I.`device_id` AND I.`ignore` = '0' AND D.`ignore` = '0' AND (I.`ifInErrors_delta` > '0' OR I.`ifOutErrors_delta` > '0')", + ); + + $data['ignored'] = array( + 'query' => "SELECT COUNT(*) FROM ports AS I, devices AS D WHERE I.`deleted` = '0' AND D.`device_id` = I.`device_id` AND (I.`ignore` = '1' OR D.`ignore` = '1')", + ); + +} else { + + $data['count'] = array( + 'query' => "SELECT COUNT(*) FROM ports AS I, devices AS D, devices_perms AS P WHERE I.`deleted` = '0' AND P.`user_id` = ? AND P.`device_id` = D.`device_id` AND I.`device_id` = D.`device_id`", + 'params' => array($_SESSION['user_id']) + ); + + $data['up'] = array( + 'query' => "SELECT COUNT(*) FROM ports AS I, devices AS D, devices_perms AS P WHERE I.`deleted` = '0' AND P.`user_id` = ? AND P.`device_id` = D.`device_id` AND I.`device_id` = D.`device_id` AND I.`ignore` = '0' AND D.`ignore` = '0' AND I.`ifOperStatus` = 'up'", + 'params' => array($_SESSION['user_id']) + ); + + $data['down'] = array( + 'query' => "SELECT COUNT(*) FROM ports AS I, devices AS D, devices_perms AS P WHERE I.`deleted` = '0' AND P.`user_id` = ? AND P.`device_id` = D.`device_id` AND I.`device_id` = D.`device_id` AND I.`ignore` = '0' AND D.`ignore` = '0' AND I.`ifOperStatus` = 'down' AND I.`ifAdminStatus` = 'up'", + 'params' => array($_SESSION['user_id']) + ); + + $data['shutdown'] = array( + 'query' => "SELECT COUNT(*) FROM ports AS I, devices AS D, devices_perms AS P WHERE I.`deleted` = '0' AND P.`user_id` = ? AND P.`device_id` = D.`device_id` AND I.`device_id` = D.`device_id` AND I.`ignore` = '0' AND D.`ignore` = '0' AND I.`ifAdminStatus` = 'down'", + 'params' => array($_SESSION['user_id']) + ); + + $data['errored'] = array( + 'query' => "SELECT COUNT(*) FROM ports AS I, devices AS D, devices_perms AS P WHERE I.`deleted` = '0' AND P.`user_id` = ? AND P.`device_id` = D.`device_id` AND I.`device_id` = D.`device_id` AND I.`ignore` = '0' AND D.`ignore` = '0' AND (I.`ifInErrors_delta` > '0' OR I.`ifOutErrors_delta` > '0')", + 'params' => array($_SESSION['user_id']) + ); + + $data['ignored'] = array( + 'query' => "SELECT COUNT(*) FROM ports AS I, devices AS D, devices_perms AS P WHERE I.`deleted` = '0' AND P.`user_id` = ? AND P.`device_id` = D.`device_id` AND I.`device_id` = D.`device_id` AND (I.`ignore` = '1' OR D.`ignore` = '1')", + 'params' => array($_SESSION['user_id']) + ); + +} \ No newline at end of file diff --git a/includes/caches/services.inc.php b/includes/caches/services.inc.php new file mode 100644 index 0000000000..6cb4179e75 --- /dev/null +++ b/includes/caches/services.inc.php @@ -0,0 +1,38 @@ += 5 ) { + + $data['count'] = array( 'query' => "SELECT COUNT(*) FROM services"); + $data['up'] = array( 'query' => "SELECT COUNT(*) FROM services WHERE `service_ignore` = '0' AND `service_disabled` = '0' AND `service_status` = '1'"); + $data['down'] = array( 'query' => "SELECT COUNT(*) FROM services WHERE `service_ignore` = '0' AND `service_disabled` = '0' AND `service_status` = '0'"); + $data['ignored'] = array( 'query' => "SELECT COUNT(*) FROM services WHERE `service_ignore` = '1' AND `service_disabled` = '0'"); + $data['disabled'] = array( 'query' => "SELECT COUNT(*) FROM services WHERE `service_disabled` = '1'"); + +} else { + + $data['count'] = array( + 'query' => "SELECT COUNT(*) FROM services AS S, devices AS D, devices_perms AS P WHERE P.`user_id` = ? AND P.`device_id` = D.`device_id` AND S.`device_id` = D.`device_id`", + 'params' => array($_SESSION['user_id']) + ); + + $data['up'] = array( + 'query' => "SELECT COUNT(*) FROM services AS S, devices AS D, devices_perms AS P WHERE P.`user_id` = ? AND P.`device_id` = D.`device_id` AND S.`device_id` = D.`device_id` AND S.`service_ignore` = '0' AND S.`service_disabled` = '0' AND S.`service_status` = '1'", + 'params' => array($_SESSION['user_id']) + ); + + $data['down'] = array( + 'query' => "SELECT COUNT(*) FROM services AS S, devices AS D, devices_perms AS P WHERE P.`user_id` = ? AND P.`device_id` = D.`device_id` AND S.`device_id` = D.`device_id` AND S.`service_ignore` = '0' AND S.`service_disabled` = '0' AND S.`service_status` = '0'", + 'params' => array($_SESSION['user_id']) + ); + + $data['ignored'] = array( + 'query' => "SELECT COUNT(*) FROM services AS S, devices AS D, devices_perms AS P WHERE P.`user_id` = ? AND P.`device_id` = D.`device_id` AND S.`device_id` = D.`device_id` AND S.`service_ignore` = '1' AND S.`service_disabled` = '0'", + 'params' => array($_SESSION['user_id']) + ); + + $data['disabled'] = array( + 'query' => "SELECT COUNT(*) FROM services AS S, devices AS D, devices_perms AS P WHERE P.`user_id` = ? AND P.`device_id` = D.`device_id` AND S.`device_id` = D.`device_id` AND S.`service_disabled` = '1'", + 'params' => array($_SESSION['user_id']) + ); + +} \ No newline at end of file diff --git a/includes/object-cache.inc.php b/includes/object-cache.inc.php new file mode 100644 index 0000000000..bada138f0a --- /dev/null +++ b/includes/object-cache.inc.php @@ -0,0 +1,113 @@ + + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ + +/** + * Object-Cache + * @author f0o + * @copyright 2015 f0o, LibreNMS + * @license GPL + * @package LibreNMS + * @subpackage Cache + */ + +class ObjCache implements ArrayAccess { + private $data = array(); + private $obj = ""; + + /** + * Initialize ObjCache + * @param string $obj Name of Object + * @return void + */ + public function __construct( $obj ) { + global $config; + $this->obj = $obj; + if( isset($GLOBALS['_ObjCache'][$obj]) ) { + $this->data = $GLOBALS['_ObjCacheSkell'][$obj]; + } else { + if( !is_array($GLOBALS['_ObjCacheSkell']) ) { + $GLOBALS['_ObjCacheSkell'] = array(); + } + if( !is_array($GLOBALS['_ObjCache']) ) { + $GLOBALS['_ObjCache'] = array(); + } + if( file_exists($config['install_dir'].'/includes/caches/'.$obj.'.inc.php') ) { + $data = array(); + include($config['install_dir'].'/includes/caches/'.$obj.'.inc.php'); + $this->data = $data; + $GLOBALS['_ObjCacheSkell'][$obj] = $this->data; + if( !is_array($GLOBALS['_ObjCache'][$obj]) ) { + $GLOBALS['_ObjCache'][$obj] = $this->data; + } + } + } + } + + /** + * Check if data exists + * @param string $obj Name of Data-Object + * @return boolean + */ + public function offsetExists( $obj ) { + if( isset($this->data[$obj]) ) { + return true; + } + return false; + } + + /** + * Get Data-Object + * @param string $obj Name of Data-Object + * @return mixed + */ + public function offsetGet( $obj ) { + if( isset($this->data[$obj]) ) { + if( isset($this->data[$obj]['value']) ) { + return $this->data[$obj]['value']; + } elseif( isset($GLOBALS['_ObjCache'][$this->obj][$obj]['value']) ) { + return $GLOBALS['_ObjCache'][$this->obj][$obj]['value']; + } else { + $GLOBALS['_ObjCache'][$this->obj][$obj]['value'] = dbFetchCell($this->data[$obj]['query'],$this->data[$obj]['params']); + return $GLOBALS['_ObjCache'][$this->obj][$obj]['value']; + } + } + } + + /** + * Overrides internal Cache-Object + * @param string $obj Name of Data-Object + * @param mixed $value Value + * @return boolean + */ + public function offsetSet( $obj, $value ) { + if( !is_array($this->data[$obj]) ) { + $this->data[$obj] = array(); + } + $this->data[$obj]['value'] = $value; + return $this->data[$obj]['value']; + } + + /** + * Reset Data-Object + * @param string $obj Name of Data-Object + * @return mixed + */ + public function offsetUnset( $obj ) { + unset($this->data[$obj]['value']); + return true; + } + +}