* Copyright (C) 2016 Cercel Valentin * * This widget is based on legacy frontpage module created by Paul Gear. * * 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 . */ $top_query = $widget_settings['top_query']; $sort_order = $widget_settings['sort_order']; $selected_sort_asc = ''; $selected_sort_desc = ''; if ($sort_order === 'asc') { $selected_sort_asc = 'selected'; } elseif ($sort_order === 'desc') { $selected_sort_desc = 'selected'; } $selected_traffic = ''; $selected_uptime = ''; $selected_ping = ''; $selected_cpu = ''; $selected_ram = ''; $selected_poller = ''; switch ($top_query) { case "traffic": $table_header = 'Traffic'; $selected_traffic = 'selected'; $graph_type = 'device_bits'; $graph_params = array(); break; case "uptime": $table_header = 'Uptime'; $selected_uptime = 'selected'; $graph_type = 'device_uptime'; $graph_params = array('tab' => 'graphs', 'group' => 'system'); break; case "ping": $table_header = 'Response time'; $selected_ping = 'selected'; $graph_type = 'device_ping_perf'; $graph_params = array('tab' => 'graphs', 'group' => 'poller'); break; case "cpu": $table_header = 'CPU Load'; $selected_cpu = 'selected'; $graph_type = 'device_processor'; $graph_params = array('tab' => 'health', 'metric' => 'processor'); break; case "ram": $table_header = 'Memory usage'; $selected_ram = 'selected'; $graph_type = 'device_mempool'; $graph_params = array('tab' => 'health', 'metric' => 'mempool'); break; case "poller": $table_header = 'Poller duration'; $selected_poller = 'selected'; $graph_type = 'device_poller_perf'; $graph_params = array('tab' => 'graphs', 'group' => 'poller'); } $widget_settings['device_count'] = $widget_settings['device_count'] > 0 ? $widget_settings['device_count'] : 5; $widget_settings['time_interval'] = $widget_settings['time_interval'] > 0 ? $widget_settings['time_interval'] : 15; if (defined('SHOW_SETTINGS') || empty($widget_settings)) { $common_output[] = '
'; } else { $interval = $widget_settings['time_interval']; (integer)$interval_seconds = ($interval * 60); (integer)$device_count = $widget_settings['device_count']; $common_output[] = '

Top ' . $device_count . ' devices (last ' . $interval . ' minutes)

'; $params = array('user' => $_SESSION['user_id'], 'interval' => array($interval_seconds), 'count' => array($device_count)); if ($top_query === 'traffic') { if (is_admin() || is_read()) { $query = ' SELECT *, sum(p.ifInOctets_rate + p.ifOutOctets_rate) as total FROM ports as p, devices as d WHERE d.device_id = p.device_id AND unix_timestamp() - p.poll_time < :interval AND ( p.ifInOctets_rate > 0 OR p.ifOutOctets_rate > 0 ) GROUP BY d.device_id ORDER BY total ' . $sort_order . ' LIMIT :count '; } else { $query = ' SELECT *, sum(p.ifInOctets_rate + p.ifOutOctets_rate) as total FROM ports as p, devices as d, `devices_perms` AS `P` WHERE `P`.`user_id` = :user AND `P`.`device_id` = `d`.`device_id` AND d.device_id = p.device_id AND unix_timestamp() - p.poll_time < :interval AND ( p.ifInOctets_rate > 0 OR p.ifOutOctets_rate > 0 ) GROUP BY d.device_id ORDER BY total ' . $sort_order . ' LIMIT :count '; } } elseif ($top_query === 'uptime') { if (is_admin() || is_read()) { $query = 'SELECT `uptime`, `hostname`, `last_polled`, `device_id`, `sysName` FROM `devices` WHERE unix_timestamp() - UNIX_TIMESTAMP(`last_polled`) < :interval ORDER BY `uptime` ' . $sort_order . ' LIMIT :count'; } else { $query = 'SELECT `uptime`, `hostname`, `last_polled`, `d`.`device_id`, `d`.`sysName` FROM `devices` as `d`, `devices_perms` AS `P` WHERE `P`.`user_id` = :user AND `P`.`device_id` = `d`.`device_id` AND unix_timestamp() - UNIX_TIMESTAMP(`last_polled`) < :interval ORDER BY `uptime` ' . $sort_order . ' LIMIT :count'; } } elseif ($top_query === 'ping') { if (is_admin() || is_read()) { $query = 'SELECT `last_ping_timetaken`, `hostname`, `last_polled`, `device_id`, `sysName` FROM `devices` WHERE unix_timestamp() - UNIX_TIMESTAMP(`last_polled`) < :interval ORDER BY `last_ping_timetaken` ' . $sort_order . ' LIMIT :count'; } else { $query = 'SELECT `last_ping_timetaken`, `hostname`, `last_polled`, `d`.`device_id`, `d`.`sysName` FROM `devices` as `d`, `devices_perms` AS `P` WHERE `P`.`user_id` = :user AND `P`.`device_id` = `d`.`device_id` AND unix_timestamp() - UNIX_TIMESTAMP(`last_polled`) < :interval ORDER BY `last_ping_timetaken` ' . $sort_order . ' LIMIT :count'; } } elseif ($top_query === 'cpu') { if (is_admin() || is_read()) { $query = 'SELECT `hostname`, `last_polled`, `d`.`device_id`, avg(`processor_usage`) as `cpuload`, `d`.`sysName` FROM `processors` AS `procs`, `devices` AS `d` WHERE `d`.`device_id` = `procs`.`device_id` AND unix_timestamp() - UNIX_TIMESTAMP(`last_polled`) < :interval GROUP BY `d`.`device_id`, `d`.`hostname`, `d`.`last_polled`, `d`.`sysName` ORDER BY `cpuload` ' . $sort_order . ' LIMIT :count'; } else { $query = 'SELECT `hostname`, `last_polled`, `d`.`device_id`, avg(`processor_usage`) as `cpuload`, `d`.`sysName` FROM `processors` AS procs, `devices` AS `d`, `devices_perms` AS `P` WHERE `P`.`user_id` = :user AND `P`.`device_id` = `procs`.`device_id` AND unix_timestamp() - UNIX_TIMESTAMP(`last_polled`) < :interval GROUP BY `procs`.`device_id`, `d`.`hostname`, `d`.`last_polled`, `d`.`sysName` ORDER BY `cpuload` ' . $sort_order . ' LIMIT :count'; } } elseif ($top_query === 'ram') { if (is_admin() || is_read()) { $query = 'SELECT `hostname`, `last_polled`, `d`.`device_id`, `mempool_perc`, `d`.`sysName` FROM `mempools` as `mem`, `devices` as `d` WHERE `d`.`device_id` = `mem`.`device_id` AND `mempool_descr` IN (\'Physical memory\',\'Memory\') AND unix_timestamp() - UNIX_TIMESTAMP(`last_polled`) < :interval ORDER BY `mempool_perc` ' . $sort_order . ' LIMIT :count'; } else { $query = 'SELECT `hostname`, `last_polled`, `d`.`device_id`, `mempool_perc`, `d`.`sysName` FROM `mempools` as `mem`, `devices` as `d`, `devices_perms` AS `P` WHERE `P`.`user_id` = :user AND `P`.`device_id` = `mem`.`device_id` AND `mempool_descr` IN (\'Physical memory\',\'Memory\') AND unix_timestamp() - UNIX_TIMESTAMP(`last_polled`) < :interval ORDER BY `mempool_perc` ' . $sort_order . ' LIMIT :count'; } } elseif ($top_query === 'poller') { if (is_admin() || is_read()) { $query = 'SELECT `last_polled_timetaken`, `hostname`, `last_polled`, `device_id`, `sysName` FROM `devices` WHERE unix_timestamp() - UNIX_TIMESTAMP(`last_polled`) < :interval ORDER BY `last_polled_timetaken` ' . $sort_order . ' LIMIT :count'; } else { $query = 'SELECT `last_polled_timetaken`, `hostname`, `last_polled`, `d`.`device_id`, `d`.`sysName` FROM `devices` as `d`, `devices_perms` AS `P` WHERE `P`.`user_id` = :user AND `P`.`device_id` = `d`.`device_id` AND unix_timestamp() - UNIX_TIMESTAMP(`last_polled`) < :interval ORDER BY `last_polled_timetaken` ' . $sort_order . ' LIMIT :count'; } } $common_output[] = '
'; foreach (dbFetchRows($query, $params) as $result) { $common_output[] = ' '; } $common_output[] = '
Device ' . $table_header . '
' . generate_device_link($result, shorthost($result['hostname'])) . ' ' . generate_device_link($result, generate_minigraph_image($result, $config['time']['day'], $config['time']['now'], $graph_type, 'no', 150, 21), $graph_params, 0, 0, 0) . '
'; }