2014-01-13 10:05:19 +00:00
< ? php
2016-11-22 09:46:41 +00:00
$init_modules = array ( 'web' , 'auth' );
2016-11-21 14:12:59 -06:00
require realpath ( __DIR__ . '/..' ) . '/includes/init.php' ;
2015-07-13 20:10:26 +02:00
2019-08-05 14:16:05 -05:00
if ( ! Auth :: check ()) {
2019-04-11 23:26:42 -05:00
die ( 'Unauthorized' );
2015-07-13 20:10:26 +02:00
}
2014-01-13 10:05:19 +00:00
2019-04-11 23:26:42 -05:00
set_debug ( $_REQUEST [ 'debug' ]);
2015-07-13 20:10:26 +02:00
$device = array ();
$ports = array ();
2016-01-05 10:21:08 -07:00
$bgp = array ();
2019-04-11 09:39:25 -05:00
$limit = ( int ) \LibreNMS\Config :: get ( 'webui.global_search_result_limit' );
2015-07-13 20:10:26 +02:00
if ( isset ( $_REQUEST [ 'search' ])) {
$search = mres ( $_REQUEST [ 'search' ]);
2016-02-04 18:16:36 -08:00
header ( 'Content-type: application/json' );
2015-07-13 20:10:26 +02:00
if ( strlen ( $search ) > 0 ) {
$found = 0 ;
if ( $_REQUEST [ 'type' ] == 'group' ) {
2019-04-11 09:39:25 -05:00
foreach ( dbFetchRows ( " SELECT id,name FROM device_groups WHERE name LIKE ? " , [ " % $search % " ]) as $group ) {
2015-07-13 20:10:26 +02:00
if ( $_REQUEST [ 'map' ]) {
$results [] = array (
'name' => 'g:' . $group [ 'name' ],
'group_id' => $group [ 'id' ],
);
2016-08-18 20:28:22 -05:00
} else {
2015-07-13 20:10:26 +02:00
$results [] = array ( 'name' => $group [ 'name' ]);
}
}
die ( json_encode ( $results ));
2016-08-18 20:28:22 -05:00
} elseif ( $_REQUEST [ 'type' ] == 'alert-rules' ) {
2019-04-11 09:39:25 -05:00
foreach ( dbFetchRows ( " SELECT name FROM alert_rules WHERE name LIKE ? " , [ " % $search % " ]) as $rules ) {
2015-07-13 20:10:26 +02:00
$results [] = array ( 'name' => $rules [ 'name' ]);
}
2014-03-18 22:36:22 +00:00
2015-07-13 20:10:26 +02:00
die ( json_encode ( $results ));
2016-08-18 20:28:22 -05:00
} elseif ( $_REQUEST [ 'type' ] == 'device' ) {
2015-07-13 20:10:26 +02:00
// Device search
2019-08-05 14:16:05 -05:00
if ( Auth :: user () -> hasGlobalRead ()) {
2019-04-11 09:39:25 -05:00
$results = dbFetchRows (
" SELECT * FROM `devices` LEFT JOIN `locations` ON `locations`.`id` = `devices`.`location_id` WHERE `devices`.`hostname` LIKE ? OR `locations`.`location` LIKE ? OR `devices`.`sysName` LIKE ? OR `devices`.`purpose` LIKE ? OR `devices`.`notes` LIKE ? ORDER BY `devices`.hostname LIMIT " . $limit ,
[ " % $search % " , " % $search % " , " % $search % " , " % $search % " , " % $search % " ]
);
2016-08-18 20:28:22 -05:00
} else {
2019-04-11 09:39:25 -05:00
$results = dbFetchRows (
" SELECT * FROM `devices` AS `D` INNER JOIN `devices_perms` AS `P` ON `P`.`device_id` = `D`.`device_id` LEFT JOIN `locations` ON `locations`.`id` = `D`.`location_id` WHERE `P`.`user_id` = ? AND (D.`hostname` LIKE ? OR D.`sysName` LIKE ? OR `locations`.`location` LIKE ?) ORDER BY hostname LIMIT " . $limit ,
2019-08-05 14:16:05 -05:00
[ Auth :: id (), " % $search % " , " % $search % " , " % $search % " ]
2019-04-11 09:39:25 -05:00
);
2015-07-13 20:10:26 +02:00
}
if ( count ( $results )) {
$found = 1 ;
$devices = count ( $results );
foreach ( $results as $result ) {
$name = $result [ 'hostname' ];
2016-10-07 10:28:12 +00:00
if ( $_REQUEST [ 'map' ] != 1 && $result [ 'sysName' ] != $name && ! empty ( $result [ 'sysName' ])) {
2016-07-09 22:24:30 +01:00
$name .= ' (' . $result [ 'sysName' ] . ') ' ;
}
2015-07-13 20:10:26 +02:00
if ( $result [ 'disabled' ] == 1 ) {
$highlight_colour = '#808080' ;
2016-08-18 20:28:22 -05:00
} elseif ( $result [ 'ignored' ] == 1 && $result [ 'disabled' ] == 0 ) {
2015-07-13 20:10:26 +02:00
$highlight_colour = '#000000' ;
2016-08-18 20:28:22 -05:00
} elseif ( $result [ 'status' ] == 0 && $result [ 'ignore' ] == 0 && $result [ 'disabled' ] == 0 ) {
2015-07-13 20:10:26 +02:00
$highlight_colour = '#ff0000' ;
2016-08-18 20:28:22 -05:00
} elseif ( $result [ 'status' ] == 1 && $result [ 'ignore' ] == 0 && $result [ 'disabled' ] == 0 ) {
2015-07-13 20:10:26 +02:00
$highlight_colour = '#008000' ;
}
2019-08-05 14:16:05 -05:00
if ( Auth :: user () -> hasGlobalRead ()) {
2019-04-11 09:39:25 -05:00
$num_ports = dbFetchCell ( 'SELECT COUNT(*) FROM `ports` WHERE device_id = ?' , [ $result [ 'device_id' ]]);
2016-08-18 20:28:22 -05:00
} else {
2019-08-05 14:16:05 -05:00
$num_ports = dbFetchCell ( 'SELECT COUNT(*) FROM `ports` AS `I`, `devices` AS `D`, `devices_perms` AS `P` WHERE `P`.`user_id` = ? AND `P`.`device_id` = `D`.`device_id` AND `I`.`device_id` = `D`.`device_id` AND D.device_id = ?' , [ Auth :: id (), $result [ 'device_id' ]]);
2015-07-13 20:10:26 +02:00
}
$device [] = array (
'name' => $name ,
'device_id' => $result [ 'device_id' ],
'url' => generate_device_url ( $result ),
'colours' => $highlight_colour ,
'device_ports' => $num_ports ,
2017-01-24 16:16:01 -06:00
'device_image' => getIcon ( $result ),
2015-07-13 20:10:26 +02:00
'device_hardware' => $result [ 'hardware' ],
2019-06-23 00:29:12 -05:00
'device_os' => \LibreNMS\Config :: getOsSetting ( $result [ 'os' ], 'text' ),
2015-07-13 20:10:26 +02:00
'version' => $result [ 'version' ],
'location' => $result [ 'location' ],
);
} //end foreach
} //end if
$json = json_encode ( $device );
2015-09-07 19:29:30 +01:00
die ( $json );
2016-08-18 20:28:22 -05:00
} elseif ( $_REQUEST [ 'type' ] == 'ports' ) {
2015-07-13 20:10:26 +02:00
// Search ports
2019-08-05 14:16:05 -05:00
if ( Auth :: user () -> hasGlobalRead ()) {
2019-04-11 09:39:25 -05:00
$results = dbFetchRows (
" SELECT `ports`.*,`devices`.* FROM `ports` LEFT JOIN `devices` ON `ports`.`device_id` = `devices`.`device_id` WHERE `ifAlias` LIKE ? OR `ifDescr` LIKE ? OR `ifName` LIKE ? ORDER BY ifDescr LIMIT " . $limit ,
[ " % $search % " , " % $search % " , " % $search % " ]
);
2016-08-18 20:28:22 -05:00
} else {
2019-04-11 09:39:25 -05:00
$results = dbFetchRows (
" SELECT DISTINCT(`I`.`port_id`), `I`.*, `D`.`hostname` FROM `ports` AS `I`, `devices` AS `D`, `devices_perms` AS `P`, `ports_perms` AS `PP` WHERE ((`P`.`user_id` = ? AND `P`.`device_id` = `D`.`device_id`) OR (`PP`.`user_id` = ? AND `PP`.`port_id` = `I`.`port_id` AND `I`.`device_id` = `D`.`device_id`)) AND `D`.`device_id` = `I`.`device_id` AND (`ifAlias` LIKE ? OR `ifDescr` LIKE ? OR `ifName` LIKE ?) ORDER BY ifDescr LIMIT " . $limit ,
2019-08-05 14:16:05 -05:00
[ Auth :: id (), Auth :: id (), " % $search % " , " % $search % " , " % $search % " ]
2019-04-11 09:39:25 -05:00
);
2015-07-13 20:10:26 +02:00
}
if ( count ( $results )) {
$found = 1 ;
foreach ( $results as $result ) {
2015-09-07 19:29:30 +01:00
$name = $result [ 'ifDescr' ] == $result [ 'ifAlias' ] ? $result [ 'ifName' ] : $result [ 'ifDescr' ];
2016-12-12 14:25:48 +00:00
$description = display ( $result [ 'ifAlias' ]);
2015-07-13 20:10:26 +02:00
if ( $result [ 'deleted' ] == 0 && ( $result [ 'ignore' ] == 0 || $result [ 'ignore' ] == 0 ) && ( $result [ 'ifInErrors_delta' ] > 0 || $result [ 'ifOutErrors_delta' ] > 0 )) {
// Errored ports
$port_colour = '#ffa500' ;
2016-08-18 20:28:22 -05:00
} elseif ( $result [ 'deleted' ] == 0 && ( $result [ 'ignore' ] == 1 || $result [ 'ignore' ] == 1 )) {
2015-07-13 20:10:26 +02:00
// Ignored ports
$port_colour = '#000000' ;
2016-08-18 20:28:22 -05:00
} elseif ( $result [ 'deleted' ] == 0 && $result [ 'ifAdminStatus' ] == 'down' && $result [ 'ignore' ] == 0 && $result [ 'ignore' ] == 0 ) {
2015-07-13 20:10:26 +02:00
// Shutdown ports
$port_colour = '#808080' ;
2016-08-18 20:28:22 -05:00
} elseif ( $result [ 'deleted' ] == 0 && $result [ 'ifOperStatus' ] == 'down' && $result [ 'ifAdminStatus' ] == 'up' && $result [ 'ignore' ] == 0 && $result [ 'ignore' ] == 0 ) {
2015-07-13 20:10:26 +02:00
// Down ports
$port_colour = '#ff0000' ;
2016-08-18 20:28:22 -05:00
} elseif ( $result [ 'deleted' ] == 0 && $result [ 'ifOperStatus' ] == 'up' && $result [ 'ignore' ] == 0 && $result [ 'ignore' ] == 0 ) {
2015-07-13 20:10:26 +02:00
// Up ports
$port_colour = '#008000' ;
} //end if
$ports [] = array (
'count' => count ( $results ),
'url' => generate_port_url ( $result ),
'name' => $name ,
'description' => $description ,
2019-04-11 09:39:25 -05:00
'colours' => $port_colour ,
2015-07-13 20:10:26 +02:00
'hostname' => $result [ 'hostname' ],
2015-09-07 19:29:30 +01:00
'port_id' => $result [ 'port_id' ],
2015-07-13 20:10:26 +02:00
);
} //end foreach
} //end if
$json = json_encode ( $ports );
2015-09-07 19:29:30 +01:00
die ( $json );
2016-08-18 20:28:22 -05:00
} elseif ( $_REQUEST [ 'type' ] == 'bgp' ) {
2015-07-13 20:10:26 +02:00
// Search bgp peers
2019-08-05 14:16:05 -05:00
if ( Auth :: user () -> hasGlobalRead ()) {
2019-04-11 09:39:25 -05:00
$results = dbFetchRows (
" SELECT `bgpPeers`.*,`devices`.* FROM `bgpPeers` LEFT JOIN `devices` ON `bgpPeers`.`device_id` = `devices`.`device_id` WHERE `astext` LIKE ? OR `bgpPeerIdentifier` LIKE ? OR `bgpPeerRemoteAs` LIKE ? ORDER BY `astext` LIMIT " . $limit ,
[ " % $search % " , " % $search % " , " % $search % " ]
);
2016-08-18 20:28:22 -05:00
} else {
2019-04-11 09:39:25 -05:00
$results = dbFetchRows (
" SELECT `bgpPeers`.*,`D`.* FROM `bgpPeers`, `devices` AS `D`, `devices_perms` AS `P` WHERE `P`.`user_id` = ? AND `P`.`device_id` = `D`.`device_id` AND `bgpPeers`.`device_id`=`D`.`device_id` AND (`astext` LIKE ? OR `bgpPeerIdentifier` LIKE ? OR `bgpPeerRemoteAs` LIKE ?) ORDER BY `astext` LIMIT " . $limit ,
2019-08-05 14:16:05 -05:00
[ Auth :: id (), " % $search % " , " % $search % " , " % $search % " ]
2019-04-11 09:39:25 -05:00
);
2015-07-13 20:10:26 +02:00
}
if ( count ( $results )) {
$found = 1 ;
foreach ( $results as $result ) {
$name = $result [ 'bgpPeerIdentifier' ];
$description = $result [ 'astext' ];
$remoteas = $result [ 'bgpPeerRemoteAs' ];
$localas = $result [ 'bgpLocalAs' ];
if ( $result [ 'bgpPeerAdminStatus' ] == 'start' && $result [ 'bgpPeerState' ] != 'established' ) {
// Session active but errored
$port_colour = '#ffa500' ;
2016-08-18 20:28:22 -05:00
} elseif ( $result [ 'bgpPeerAdminStatus' ] != 'start' ) {
2015-07-13 20:10:26 +02:00
// Session inactive
$port_colour = '#000000' ;
2016-08-18 20:28:22 -05:00
} elseif ( $result [ 'bgpPeerAdminStatus' ] == 'start' && $result [ 'bgpPeerState' ] == 'established' ) {
2015-07-13 20:10:26 +02:00
// Session Up
$port_colour = '#008000' ;
}
if ( $result [ 'bgpPeerRemoteAs' ] == $result [ 'bgpLocalAs' ]) {
2017-01-29 01:44:36 +02:00
$bgp_image = '<i class="fa fa-square fa-lg icon-theme" aria-hidden="true"></i>' ;
2016-08-18 20:28:22 -05:00
} else {
2017-01-29 01:44:36 +02:00
$bgp_image = '<i class="fa fa-external-link-square fa-lg icon-theme" aria-hidden="true"></i>' ;
2015-07-13 20:10:26 +02:00
}
$bgp [] = array (
'count' => count ( $results ),
'url' => generate_peer_url ( $result ),
'name' => $name ,
'description' => $description ,
'localas' => $localas ,
'bgp_image' => $bgp_image ,
'remoteas' => $remoteas ,
'colours' => $port_colour ,
'hostname' => $result [ 'hostname' ],
);
} //end foreach
} //end if
$json = json_encode ( $bgp );
2015-09-07 19:29:30 +01:00
die ( $json );
2016-08-18 20:28:22 -05:00
} elseif ( $_REQUEST [ 'type' ] == 'applications' ) {
2015-09-07 19:29:30 +01:00
// Device search
2019-08-05 14:16:05 -05:00
if ( Auth :: user () -> hasGlobalRead ()) {
2019-04-11 09:39:25 -05:00
$results = dbFetchRows (
" SELECT * FROM `applications` INNER JOIN `devices` ON devices.device_id = applications.device_id WHERE `app_type` LIKE ? OR `hostname` LIKE ? ORDER BY hostname LIMIT " . $limit ,
[ " % $search % " , " % $search % " ]
);
2016-08-18 20:28:22 -05:00
} else {
2019-04-11 09:39:25 -05:00
$results = dbFetchRows (
" SELECT * FROM `applications` INNER JOIN `devices` AS `D` ON `D`.`device_id` = `applications`.`device_id` INNER JOIN `devices_perms` AS `P` ON `P`.`device_id` = `D`.`device_id` WHERE `P`.`user_id` = ? AND (`app_type` LIKE ? OR `hostname` LIKE ?) ORDER BY hostname LIMIT " . $limit ,
2019-08-05 14:16:05 -05:00
[ Auth :: id (), " % $search % " , " % $search % " ]
2019-04-11 09:39:25 -05:00
);
2015-09-07 19:29:30 +01:00
}
if ( count ( $results )) {
$found = 1 ;
$devices = count ( $results );
foreach ( $results as $result ) {
$name = $result [ 'app_type' ];
if ( $result [ 'disabled' ] == 1 ) {
$highlight_colour = '#808080' ;
2016-08-18 20:28:22 -05:00
} elseif ( $result [ 'ignored' ] == 1 && $result [ 'disabled' ] == 0 ) {
2015-09-07 19:29:30 +01:00
$highlight_colour = '#000000' ;
2016-08-18 20:28:22 -05:00
} elseif ( $result [ 'status' ] == 0 && $result [ 'ignore' ] == 0 && $result [ 'disabled' ] == 0 ) {
2015-09-07 19:29:30 +01:00
$highlight_colour = '#ff0000' ;
2016-08-18 20:28:22 -05:00
} elseif ( $result [ 'status' ] == 1 && $result [ 'ignore' ] == 0 && $result [ 'disabled' ] == 0 ) {
2015-09-07 19:29:30 +01:00
$highlight_colour = '#008000' ;
}
$device [] = array (
'name' => $name ,
'hostname' => $result [ 'hostname' ],
'app_id' => $result [ 'app_id' ],
'device_id' => $result [ 'device_id' ],
'colours' => $highlight_colour ,
2017-01-24 16:16:01 -06:00
'device_image' => getIcon ( $result ),
2015-09-07 19:29:30 +01:00
'device_hardware' => $result [ 'hardware' ],
2019-06-23 00:29:12 -05:00
'device_os' => \LibreNMS\Config :: getOsSetting ( $result [ 'os' ], 'text' ),
2015-09-07 19:29:30 +01:00
'version' => $result [ 'version' ],
'location' => $result [ 'location' ],
);
} //end foreach
} //end if
$json = json_encode ( $device );
die ( $json );
2016-08-18 20:28:22 -05:00
} elseif ( $_REQUEST [ 'type' ] == 'munin' ) {
2015-09-07 19:29:30 +01:00
// Device search
2019-08-05 14:16:05 -05:00
if ( Auth :: user () -> hasGlobalRead ()) {
2019-04-11 09:39:25 -05:00
$results = dbFetchRows (
" SELECT * FROM `munin_plugins` INNER JOIN `devices` ON devices.device_id = munin_plugins.device_id WHERE `mplug_type` LIKE ? OR `mplug_title` LIKE ? OR `hostname` LIKE ? ORDER BY hostname LIMIT " . $limit ,
[ " % $search % " , " % $search % " , " % $search % " ]
);
2016-08-18 20:28:22 -05:00
} else {
2019-04-11 09:39:25 -05:00
$results = dbFetchRows (
" SELECT * FROM `munin_plugins` INNER JOIN `devices` AS `D` ON `D`.`device_id` = `munin_plugins`.`device_id` INNER JOIN `devices_perms` AS `P` ON `P`.`device_id` = `D`.`device_id` WHERE `P`.`user_id` = ? AND (`mplug_type` LIKE ? OR `mplug_title` LIKE ? OR `hostname` LIKE ?) ORDER BY hostname LIMIT " . $limit ,
2019-08-05 14:16:05 -05:00
[ Auth :: id (), " % $search % " , " % $search % " , " % $search % " ]
2019-04-11 09:39:25 -05:00
);
2015-09-07 19:29:30 +01:00
}
if ( count ( $results )) {
$found = 1 ;
$devices = count ( $results );
foreach ( $results as $result ) {
$name = $result [ 'mplug_title' ];
if ( $result [ 'disabled' ] == 1 ) {
$highlight_colour = '#808080' ;
2016-08-18 20:28:22 -05:00
} elseif ( $result [ 'ignored' ] == 1 && $result [ 'disabled' ] == 0 ) {
2015-09-07 19:29:30 +01:00
$highlight_colour = '#000000' ;
2016-08-18 20:28:22 -05:00
} elseif ( $result [ 'status' ] == 0 && $result [ 'ignore' ] == 0 && $result [ 'disabled' ] == 0 ) {
2015-09-07 19:29:30 +01:00
$highlight_colour = '#ff0000' ;
2016-08-18 20:28:22 -05:00
} elseif ( $result [ 'status' ] == 1 && $result [ 'ignore' ] == 0 && $result [ 'disabled' ] == 0 ) {
2015-09-07 19:29:30 +01:00
$highlight_colour = '#008000' ;
}
$device [] = array (
'name' => $name ,
'hostname' => $result [ 'hostname' ],
'device_id' => $result [ 'device_id' ],
'colours' => $highlight_colour ,
2017-01-24 16:16:01 -06:00
'device_image' => getIcon ( $result ),
2015-09-07 19:29:30 +01:00
'device_hardware' => $result [ 'hardware' ],
2019-06-23 00:29:12 -05:00
'device_os' => \LibreNMS\Config :: getOsSetting ( $result [ 'os' ], 'text' ),
2015-09-07 19:29:30 +01:00
'version' => $result [ 'version' ],
'location' => $result [ 'location' ],
'plugin' => $result [ 'mplug_type' ],
);
} //end foreach
} //end if
2015-09-20 10:13:56 +01:00
$json = json_encode ( $device );
die ( $json );
2016-08-18 20:28:22 -05:00
} elseif ( $_REQUEST [ 'type' ] == 'iftype' ) {
2015-09-20 10:13:56 +01:00
// Device search
2019-08-05 14:16:05 -05:00
if ( Auth :: user () -> hasGlobalRead ()) {
2019-04-11 09:39:25 -05:00
$results = dbFetchRows (
" SELECT `ports`.ifType FROM `ports` WHERE `ifType` LIKE ? GROUP BY ifType ORDER BY ifType LIMIT " . $limit ,
[ " % $search % " ]
);
2016-08-18 20:28:22 -05:00
} else {
2019-04-11 09:39:25 -05:00
$results = dbFetchRows (
" SELECT `I`.ifType FROM `ports` AS `I`, `devices` AS `D`, `devices_perms` AS `P`, `ports_perms` AS `PP` WHERE ((`P`.`user_id` = ? AND `P`.`device_id` = `D`.`device_id`) OR (`PP`.`user_id` = ? AND `PP`.`port_id` = `I`.`port_id` AND `I`.`device_id` = `D`.`device_id`)) AND `D`.`device_id` = `I`.`device_id` AND (`ifType` LIKE ?) GROUP BY ifType ORDER BY ifType LIMIT " . $limit ,
2019-08-05 14:16:05 -05:00
[ Auth :: id (), Auth :: id (), " % $search % " ]
2019-04-11 09:39:25 -05:00
);
2015-09-20 10:13:56 +01:00
}
if ( count ( $results )) {
$found = 1 ;
$devices = count ( $results );
foreach ( $results as $result ) {
$device [] = array (
'filter' => $result [ 'ifType' ],
);
} //end foreach
} //end if
2015-09-07 19:29:30 +01:00
$json = json_encode ( $device );
die ( $json );
2016-08-18 20:28:22 -05:00
} elseif ( $_REQUEST [ 'type' ] == 'bill' ) {
2015-09-30 20:11:18 +00:00
// Device search
2019-08-05 14:16:05 -05:00
if ( Auth :: user () -> hasGlobalRead ()) {
2019-04-11 09:39:25 -05:00
$results = dbFetchRows (
" SELECT `bills`.bill_id, `bills`.bill_name FROM `bills` WHERE `bill_name` LIKE ? OR `bill_notes` LIKE ? LIMIT " . $limit ,
[ " % $search % " , " % $search % " ]
);
2016-08-18 20:28:22 -05:00
} else {
2019-04-11 09:39:25 -05:00
$results = dbFetchRows (
" SELECT `bills`.bill_id, `bills`.bill_name FROM `bills` INNER JOIN `bill_perms` ON `bills`.bill_id = `bill_perms`.bill_id WHERE `bill_perms`.user_id = ? AND (`bill_name` LIKE ? OR `bill_notes` LIKE ?) LIMIT " . $limit ,
2019-08-05 14:16:05 -05:00
[ Auth :: id (), " % $search % " , " % $search % " ]
2019-04-11 09:39:25 -05:00
);
2015-09-30 20:11:18 +00:00
}
$json = json_encode ( $results );
die ( $json );
2015-07-13 20:10:26 +02:00
} //end if
} //end if
} //end if