diff --git a/LibreNMS/Modules/OS.php b/LibreNMS/Modules/OS.php index 243c743031..1ae92ef1a3 100644 --- a/LibreNMS/Modules/OS.php +++ b/LibreNMS/Modules/OS.php @@ -59,7 +59,7 @@ class OS implements Module $os->pollOS(); } else { // legacy poller files - $device = $os->getDevice(); + global $graphs, $device; if (is_file(base_path('/includes/polling/os/' . $device['os'] . '.inc.php'))) { // OS Specific include base_path('/includes/polling/os/' . $device['os'] . '.inc.php'); diff --git a/LibreNMS/Util/Graph.php b/LibreNMS/Util/Graph.php index dac4feff9d..064f3fbbae 100644 --- a/LibreNMS/Util/Graph.php +++ b/LibreNMS/Util/Graph.php @@ -55,7 +55,7 @@ class Graph if ($device != null) { // find the MIB subtypes - $graphs = $device->graphs(); + $graphs = $device->graphs->pluck('graph'); foreach (Config::get('graph_types') as $type => $type_data) { foreach (array_keys($type_data) as $subtype) { diff --git a/app/Models/Device.php b/app/Models/Device.php index b06d9e54b3..2232866a66 100644 --- a/app/Models/Device.php +++ b/app/Models/Device.php @@ -230,18 +230,6 @@ class Device extends BaseModel return asset('images/os/generic.svg'); } - /** - * Get list of enabled graphs for this device. - * - * @return \Illuminate\Support\Collection - */ - public function graphs() - { - return DB::table('device_graphs') - ->where('device_id', $this->device_id) - ->pluck('graph'); - } - /** * Update the max_depth field based on parents * Performs SQL query, so make sure all parents are saved first @@ -532,6 +520,11 @@ class Device extends BaseModel return $this->hasMany(\App\Models\Eventlog::class, 'device_id', 'device_id'); } + public function graphs() + { + return $this->hasMany(\App\Models\DeviceGraph::class, 'device_id'); + } + public function groups() { return $this->belongsToMany(\App\Models\DeviceGroup::class, 'device_group_device', 'device_id', 'device_group_id'); diff --git a/app/Models/DeviceGraph.php b/app/Models/DeviceGraph.php new file mode 100644 index 0000000000..a988c621f2 --- /dev/null +++ b/app/Models/DeviceGraph.php @@ -0,0 +1,9 @@ +getAttribs(); $device['attribs'] = $attribs; @@ -290,9 +290,6 @@ function poll_device($device, $force_module = false) $response = device_is_up($device, true); if ($response['status'] == '1') { - $graphs = array(); - $oldgraphs = array(); - if ($device['snmp_disable']) { Config::set('poller_modules', []); } else { @@ -367,28 +364,15 @@ function poll_device($device, $force_module = false) if (!$force_module && !empty($graphs)) { echo "Enabling graphs: "; - // FIXME EVENTLOGGING -- MAKE IT SO WE DO THIS PER-MODULE? - // This code cycles through the graphs already known in the database and the ones we've defined as being polled here - // If there any don't match, they're added/deleted from the database. - // Ideally we should hold graphs for xx days/weeks/polls so that we don't needlessly hide information. - foreach (dbFetch('SELECT `graph` FROM `device_graphs` WHERE `device_id` = ?', array($device['device_id'])) as $graph) { - if (isset($graphs[$graph['graph']])) { - $oldgraphs[$graph['graph']] = true; - } else { - dbDelete('device_graphs', '`device_id` = ? AND `graph` = ?', array($device['device_id'], $graph['graph'])); - } - } - - foreach ($graphs as $graph => $value) { - if (!isset($oldgraphs[$graph])) { + $graphs = collect($graphs)->keys(); + DeviceCache::getPrimary()->graphs->keyBy('graph')->collect()->except($graphs)->each->delete(); // delete extra graphs + DeviceCache::getPrimary()->graphs() // create missing graphs + ->saveMany($graphs->diff(DeviceCache::getPrimary()->graphs->pluck('graph'))->map(function ($graph) { echo '+'; - dbInsert(array('device_id' => $device['device_id'], 'graph' => $graph), 'device_graphs'); - } - - echo $graph.' '; - } + return new DeviceGraph(['graph' => $graph]); + })); echo PHP_EOL; - }//end if + } // Ping response if (can_ping_device($attribs) === true && !empty($response['ping_time'])) {