2014-07-07 00:15:04 +01:00
< ? php
/*
* LibreNMS
*
* Copyright ( c ) 2014 Neil Lathwood < https :// github . com / laf / http :// www . lathwood . co . uk / fa >
*
* 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 . Please see LICENSE . txt at the top level of
* the source code distribution for details .
*/
2018-09-19 13:47:45 +01:00
use LibreNMS\Alerting\QueryBuilderParser ;
2018-09-19 23:38:01 +01:00
use LibreNMS\Authentication\LegacyAuth ;
use LibreNMS\Config ;
2019-04-10 16:33:25 -05:00
use LibreNMS\Exceptions\InvalidIpException ;
use LibreNMS\Util\IPv4 ;
2017-11-18 11:33:03 +01:00
2016-08-18 20:28:22 -05:00
function authToken ( \Slim\Route $route )
{
2018-09-11 07:51:35 -05:00
if ( Auth :: check ()) {
$user = Auth :: user ();
2017-12-02 16:53:45 -06:00
// Fake session so the standard auth/permissions checks work
2018-09-11 07:51:35 -05:00
$_SESSION = [
'username' => $user -> username ,
'user_id' => $user -> user_id ,
'userlevel' => $user -> level
];
2015-07-13 20:10:26 +02:00
2017-12-02 16:53:45 -06:00
return ;
2015-07-13 20:10:26 +02:00
}
2017-12-02 16:53:45 -06:00
api_error ( 401 , 'API Token is missing or invalid; please supply a valid token' );
2014-07-07 00:15:04 +01:00
}
2017-11-29 09:42:52 +00:00
function api_success ( $result , $result_name , $message = null , $code = 200 , $count = null , $extra = null )
{
if ( isset ( $result ) && ! isset ( $result_name )) {
api_error ( 500 , 'Result name not specified' );
}
2017-12-02 16:53:45 -06:00
2017-11-29 09:42:52 +00:00
$app = \Slim\Slim :: getInstance ();
$app -> response -> setStatus ( $code );
$app -> response -> headers -> set ( 'Content-Type' , 'application/json' );
$output = array ( 'status' => 'ok' );
2017-12-02 16:53:45 -06:00
2017-11-29 09:42:52 +00:00
if ( isset ( $result )) {
$output [ $result_name ] = $result ;
}
if ( isset ( $message ) && $message != '' ) {
$output [ 'message' ] = $message ;
}
if ( ! isset ( $count ) && is_array ( $result )) {
$count = count ( $result );
}
if ( isset ( $count )) {
$output [ 'count' ] = $count ;
}
if ( isset ( $extra )) {
$output = array_merge ( $output , $extra );
}
echo _json_encode ( $output );
$app -> stop ();
} // end api_success()
2015-07-13 20:10:26 +02:00
2017-11-29 09:42:52 +00:00
function api_success_noresult ( $code , $message = null )
{
api_success ( null , null , $message , $code );
} // end api_success_noresult
function api_error ( $statusCode , $message )
{
$app = \Slim\Slim :: getInstance ();
$app -> response -> setStatus ( $statusCode );
$app -> response -> headers -> set ( 'Content-Type' , 'application/json' );
$output = array (
'status' => 'error' ,
'message' => $message
);
echo _json_encode ( $output );
$app -> stop ();
} // end api_error()
2018-02-09 21:14:55 +00:00
function check_bill_permission ( $bill_id )
{
if ( ! bill_permitted ( $bill_id )) {
api_error ( 403 , 'Insufficient permissions to access this bill' );
}
}
2017-11-29 09:42:52 +00:00
function check_device_permission ( $device_id )
{
if ( ! device_permitted ( $device_id )) {
api_error ( 403 , 'Insufficient permissions to access this device' );
}
}
function check_port_permission ( $port_id , $device_id )
{
if ( ! device_permitted ( $device_id ) && ! port_permitted ( $port_id , $device_id )) {
api_error ( 403 , 'Insufficient permissions to access this port' );
}
}
function check_is_admin ()
{
2018-09-11 07:51:35 -05:00
if ( ! LegacyAuth :: user () -> hasGlobalAdmin ()) {
2017-11-29 09:42:52 +00:00
api_error ( 403 , 'Insufficient privileges' );
}
}
function check_is_read ()
{
2018-09-11 07:51:35 -05:00
if ( ! LegacyAuth :: user () -> hasGlobalRead ()) {
2017-11-29 09:42:52 +00:00
api_error ( 403 , 'Insufficient privileges' );
}
}
function check_not_demo ()
{
global $config ;
if ( $config [ 'api_demo' ] == 1 ) {
api_error ( 500 , 'This feature isn\'t available in the demo' );
}
}
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function get_graph_by_port_hostname ()
{
2015-07-13 20:10:26 +02:00
// This will return a graph for a given port by the ifName
global $config ;
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
$hostname = $router [ 'hostname' ];
2017-07-04 23:41:22 +02:00
$device_id = ctype_digit ( $hostname ) ? $hostname : getidbyname ( $hostname );
2015-07-13 20:10:26 +02:00
$vars = array ();
$vars [ 'port' ] = urldecode ( $router [ 'ifname' ]);
$vars [ 'type' ] = $router [ 'type' ] ? : 'port_bits' ;
2018-04-11 09:02:04 -04:00
$vars [ 'output' ] = $_GET [ 'output' ] ? : 'display' ;
2015-07-13 20:10:26 +02:00
if ( ! empty ( $_GET [ 'from' ])) {
$vars [ 'from' ] = $_GET [ 'from' ];
}
if ( ! empty ( $_GET [ 'to' ])) {
$vars [ 'to' ] = $_GET [ 'to' ];
}
2015-12-21 18:22:36 +00:00
if ( $_GET [ 'ifDescr' ] == true ) {
2015-12-19 19:10:18 +00:00
$port = 'ifDescr' ;
2016-08-18 20:28:22 -05:00
} else {
2015-12-19 19:10:18 +00:00
$port = 'ifName' ;
}
2015-07-13 20:10:26 +02:00
$vars [ 'width' ] = $_GET [ 'width' ] ? : 1075 ;
$vars [ 'height' ] = $_GET [ 'height' ] ? : 300 ;
$auth = '1' ;
2017-07-04 23:41:22 +02:00
$vars [ 'id' ] = dbFetchCell ( " SELECT `P`.`port_id` FROM `ports` AS `P` JOIN `devices` AS `D` ON `P`.`device_id` = `D`.`device_id` WHERE `D`.`device_id`=? AND `P`.` $port `=? AND `deleted` = 0 LIMIT 1 " , array ( $device_id , $vars [ 'port' ]));
2017-12-02 16:53:45 -06:00
2017-11-29 09:42:52 +00:00
check_port_permission ( $vars [ 'id' ], $device_id );
2017-03-14 05:39:43 +13:00
$app -> response -> headers -> set ( 'Content-Type' , get_image_type ());
2017-01-25 19:16:59 +00:00
rrdtool_initialize ( false );
2019-04-11 23:26:42 -05:00
include 'includes/html/graphs/graph.inc.php' ;
2017-01-25 19:16:59 +00:00
rrdtool_close ();
2018-04-11 09:02:04 -04:00
if ( $vars [ 'output' ] === 'base64' ) {
api_success ([ 'image' => $base64_output , 'content-type' => get_image_type ()], 'image' );
}
2014-07-08 14:39:19 +01:00
}
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function get_port_stats_by_port_hostname ()
{
2015-07-13 20:10:26 +02:00
// This will return port stats based on a devices hostname and ifName
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
$hostname = $router [ 'hostname' ];
$device_id = ctype_digit ( $hostname ) ? $hostname : getidbyname ( $hostname );
$ifName = urldecode ( $router [ 'ifname' ]);
2016-12-08 15:40:00 +00:00
$port = dbFetchRow ( 'SELECT * FROM `ports` WHERE `device_id`=? AND `ifName`=? AND `deleted` = 0' , array ( $device_id , $ifName ));
2017-12-02 16:53:45 -06:00
2017-11-29 09:42:52 +00:00
check_port_permission ( $port [ 'port_id' ], $device_id );
2016-10-15 00:42:54 +01:00
2016-11-08 16:26:24 -06:00
$in_rate = $port [ 'ifInOctets_rate' ] * 8 ;
$out_rate = $port [ 'ifOutOctets_rate' ] * 8 ;
$port [ 'in_rate' ] = formatRates ( $in_rate );
$port [ 'out_rate' ] = formatRates ( $out_rate );
$port [ 'in_perc' ] = number_format ( $in_rate / $port [ 'ifSpeed' ] * 100 , 2 , '.' , '' );
$port [ 'out_perc' ] = number_format ( $out_rate / $port [ 'ifSpeed' ] * 100 , 2 , '.' , '' );
2016-10-15 00:42:54 +01:00
$port [ 'in_pps' ] = format_bi ( $port [ 'ifInUcastPkts_rate' ]);
$port [ 'out_pps' ] = format_bi ( $port [ 'ifOutUcastPkts_rate' ]);
2016-12-23 21:54:13 +01:00
//only return requested columns
if ( isset ( $_GET [ 'columns' ])) {
$cols = explode ( " , " , $_GET [ 'columns' ]);
foreach ( array_keys ( $port ) as $c ) {
if ( ! in_array ( $c , $cols )) {
unset ( $port [ $c ]);
}
}
}
2017-11-29 09:42:52 +00:00
api_success ( $port , 'port' );
2014-07-07 00:15:04 +01:00
}
2014-07-08 14:39:19 +01:00
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function get_graph_generic_by_hostname ()
{
2015-07-13 20:10:26 +02:00
// This will return a graph type given a device id.
global $config ;
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
$hostname = $router [ 'hostname' ];
2017-01-19 10:09:20 +00:00
$sensor_id = $router [ 'sensor_id' ] ? : null ;
2015-07-13 20:10:26 +02:00
$vars = array ();
$vars [ 'type' ] = $router [ 'type' ] ? : 'device_uptime' ;
2018-04-11 09:02:04 -04:00
$vars [ 'output' ] = $_GET [ 'output' ] ? : 'display' ;
2017-01-19 10:09:20 +00:00
if ( isset ( $sensor_id )) {
$vars [ 'id' ] = $sensor_id ;
2017-12-06 22:27:30 +00:00
if ( str_contains ( $vars [ 'type' ], '_wireless' )) {
$vars [ 'type' ] = str_replace ( 'device_' , '' , $vars [ 'type' ]);
} else {
// If this isn't a wireless graph we need to fix the name.
$vars [ 'type' ] = str_replace ( 'device_' , 'sensor_' , $vars [ 'type' ]);
}
2017-01-19 10:09:20 +00:00
}
2016-05-04 15:50:41 -04:00
// use hostname as device_id if it's all digits
$device_id = ctype_digit ( $hostname ) ? $hostname : getidbyname ( $hostname );
$device = device_by_id_cache ( $device_id );
2017-11-29 09:42:52 +00:00
check_device_permission ( $device_id );
2015-07-13 20:10:26 +02:00
if ( ! empty ( $_GET [ 'from' ])) {
$vars [ 'from' ] = $_GET [ 'from' ];
}
if ( ! empty ( $_GET [ 'to' ])) {
$vars [ 'to' ] = $_GET [ 'to' ];
}
$vars [ 'width' ] = $_GET [ 'width' ] ? : 1075 ;
$vars [ 'height' ] = $_GET [ 'height' ] ? : 300 ;
$auth = '1' ;
$vars [ 'device' ] = dbFetchCell ( 'SELECT `D`.`device_id` FROM `devices` AS `D` WHERE `D`.`hostname`=?' , array ( $hostname ));
2017-03-14 05:39:43 +13:00
$app -> response -> headers -> set ( 'Content-Type' , get_image_type ());
2017-01-25 19:16:59 +00:00
rrdtool_initialize ( false );
2019-04-11 23:26:42 -05:00
include 'includes/html/graphs/graph.inc.php' ;
2017-01-25 19:16:59 +00:00
rrdtool_close ();
2018-04-11 09:02:04 -04:00
if ( $vars [ 'output' ] === 'base64' ) {
api_success ([ 'image' => $base64_output , 'content-type' => get_image_type ()], 'image' );
}
2014-07-08 14:39:19 +01:00
}
2014-07-13 21:28:26 +01:00
2018-01-18 18:43:15 +03:00
function list_locations ()
{
check_is_read ();
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
$locations = dbFetchRows ( " SELECT `locations`.* FROM `locations` WHERE `locations`.`location` IS NOT NULL " );
$total_locations = count ( $locations );
if ( $total_locations == 0 ) {
api_error ( 404 , 'Locations do not exist' );
}
api_success ( $locations , 'locations' );
}
2016-08-18 20:28:22 -05:00
function get_device ()
{
2015-07-13 20:10:26 +02:00
// return details of a single device
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
$hostname = $router [ 'hostname' ];
// use hostname as device_id if it's all digits
$device_id = ctype_digit ( $hostname ) ? $hostname : getidbyname ( $hostname );
// find device matching the id
$device = device_by_id_cache ( $device_id );
if ( ! $device ) {
2017-11-29 09:42:52 +00:00
api_error ( 404 , " Device $hostname does not exist " );
}
check_device_permission ( $device_id );
$host_id = get_vm_parent_id ( $device );
if ( is_numeric ( $host_id )) {
$device = array_merge ( $device , array ( 'parent_id' => $host_id ));
2015-07-13 20:10:26 +02:00
}
2017-11-29 09:42:52 +00:00
api_success ( array ( $device ), 'devices' );
2014-09-27 15:25:43 +10:00
}
2016-08-18 20:28:22 -05:00
function list_devices ()
{
2015-07-13 20:10:26 +02:00
// This will return a list of devices
$order = $_GET [ 'order' ];
$type = $_GET [ 'type' ];
2015-08-23 11:57:56 +00:00
$query = mres ( $_GET [ 'query' ]);
$param = array ();
2018-01-14 23:43:55 +03:00
2015-07-13 20:10:26 +02:00
if ( empty ( $order )) {
$order = 'hostname' ;
}
if ( stristr ( $order , ' desc' ) === false && stristr ( $order , ' asc' ) === false ) {
2018-01-14 23:43:55 +03:00
$order = 'd.`' . $order . '` ASC' ;
2015-07-13 20:10:26 +02:00
}
2018-04-07 14:29:46 +02:00
$select = " d.*, GROUP_CONCAT(dd.device_id) AS dependency_parent_id, GROUP_CONCAT(dd.hostname) AS dependency_parent_hostname, `lat`, `lng` " ;
Refactored and update Location Geocoding (#9359)
- Fix location so it is a regular database relation (this allows multiple devices to be accurately linked to one location and saves api calls)
- Parse coordinates from the location more consistently
- Add settings to webui
- ~~Used [PHP Geocoder](http://geocoder-php.org/), which has lots of backends and is well tested. (also includes reverse and geoip)~~
- Google Maps, Bing, Mapquest, and OpenStreetMap supported initially.
- Default to OpenStreetMap, which doesn't require a key. They will liberally hand out bans if you exceed 1 query per second though.
- All other Geocoding APIs require an API key. (Google requires a credit card on file, but seems to be the most accurate)
- Update all (I think) sql queries to handle the new structure
- Remove final vestiges of override_sysLocation as a device attribute
- Update existing device groups and rules in DB
- Tested all APIs with good/bad location, no/bad/good key, and no connection.
- Cannot fix advanced queries that use location
This blocks #8868
DO NOT DELETE THIS TEXT
#### Please note
> Please read this information carefully. You can run `./scripts/pre-commit.php` to check your code before submitting.
- [x] Have you followed our [code guidelines?](http://docs.librenms.org/Developing/Code-Guidelines/)
#### Testers
If you would like to test this pull request then please run: `./scripts/github-apply <pr_id>`, i.e `./scripts/github-apply 5926`
After you are done testing, you can remove the changes with `./scripts/github-remove`. If there are schema changes, you can ask on discord how to revert.
2018-11-28 16:49:18 -06:00
$join = " LEFT JOIN `device_relationships` AS dr ON dr.`child_device_id` = d.`device_id` LEFT JOIN `devices` AS dd ON dr.`parent_device_id` = dd.`device_id` LEFT JOIN `locations` ON `locations`.`id` = `d`.`location_id` " ;
2018-01-14 23:43:55 +03:00
2015-07-13 20:10:26 +02:00
if ( $type == 'all' || empty ( $type )) {
$sql = '1' ;
2018-08-28 18:44:47 +02:00
} elseif ( $type == 'active' ) {
$sql = " `d`.`ignore`='0' AND `d`.`disabled`='0' " ;
2017-02-08 22:57:15 +01:00
} elseif ( $type == 'location' ) {
Refactored and update Location Geocoding (#9359)
- Fix location so it is a regular database relation (this allows multiple devices to be accurately linked to one location and saves api calls)
- Parse coordinates from the location more consistently
- Add settings to webui
- ~~Used [PHP Geocoder](http://geocoder-php.org/), which has lots of backends and is well tested. (also includes reverse and geoip)~~
- Google Maps, Bing, Mapquest, and OpenStreetMap supported initially.
- Default to OpenStreetMap, which doesn't require a key. They will liberally hand out bans if you exceed 1 query per second though.
- All other Geocoding APIs require an API key. (Google requires a credit card on file, but seems to be the most accurate)
- Update all (I think) sql queries to handle the new structure
- Remove final vestiges of override_sysLocation as a device attribute
- Update existing device groups and rules in DB
- Tested all APIs with good/bad location, no/bad/good key, and no connection.
- Cannot fix advanced queries that use location
This blocks #8868
DO NOT DELETE THIS TEXT
#### Please note
> Please read this information carefully. You can run `./scripts/pre-commit.php` to check your code before submitting.
- [x] Have you followed our [code guidelines?](http://docs.librenms.org/Developing/Code-Guidelines/)
#### Testers
If you would like to test this pull request then please run: `./scripts/github-apply <pr_id>`, i.e `./scripts/github-apply 5926`
After you are done testing, you can remove the changes with `./scripts/github-remove`. If there are schema changes, you can ask on discord how to revert.
2018-11-28 16:49:18 -06:00
$sql = " `locations`.`location` LIKE '% " . $query . " %' " ;
2016-08-18 20:28:22 -05:00
} elseif ( $type == 'ignored' ) {
2018-01-22 21:10:39 +00:00
$sql = " `d`.`ignore`='1' AND `d`.`disabled`='0' " ;
2016-08-18 20:28:22 -05:00
} elseif ( $type == 'up' ) {
2018-01-22 21:10:39 +00:00
$sql = " `d`.`status`='1' AND `d`.`ignore`='0' AND `d`.`disabled`='0' " ;
2016-08-18 20:28:22 -05:00
} elseif ( $type == 'down' ) {
2018-01-22 21:10:39 +00:00
$sql = " `d`.`status`='0' AND `d`.`ignore`='0' AND `d`.`disabled`='0' " ;
2016-08-18 20:28:22 -05:00
} elseif ( $type == 'disabled' ) {
2018-01-22 21:10:39 +00:00
$sql = " `d`.`disabled`='1' " ;
2017-06-19 12:49:43 -05:00
} elseif ( $type == 'os' ) {
2018-01-22 21:10:39 +00:00
$sql = " `d`.`os`=? " ;
2017-06-19 12:49:43 -05:00
$param [] = $query ;
2016-08-18 20:28:22 -05:00
} elseif ( $type == 'mac' ) {
2018-01-14 23:43:55 +03:00
$join .= " LEFT JOIN `ports` AS p ON d.`device_id` = p.`device_id` LEFT JOIN `ipv4_mac` AS m ON p.`port_id` = m.`port_id` " ;
$sql = " m.`mac_address`=? " ;
$select .= " ,p.* " ;
2015-08-23 11:57:56 +00:00
$param [] = $query ;
2016-08-18 20:28:22 -05:00
} elseif ( $type == 'ipv4' ) {
2018-01-14 23:43:55 +03:00
$join .= " LEFT JOIN `ports` AS p ON d.`device_id` = p.`device_id` LEFT JOIN `ipv4_addresses` AS a ON p.`port_id` = a.`port_id` " ;
$sql = " a.`ipv4_address`=? " ;
$select .= " ,p.* " ;
2015-08-23 11:57:56 +00:00
$param [] = $query ;
2016-08-18 20:28:22 -05:00
} elseif ( $type == 'ipv6' ) {
2018-01-14 23:43:55 +03:00
$join .= " LEFT JOIN `ports` AS p ON d.`device_id` = p.`device_id` LEFT JOIN `ipv6_addresses` AS a ON p.`port_id` = a.`port_id` " ;
$sql = " a.`ipv6_address`=? OR a.`ipv6_compressed`=? " ;
$select .= " ,p.* " ;
$param = array ( $query , $query );
2016-08-18 20:28:22 -05:00
} else {
2015-07-13 20:10:26 +02:00
$sql = '1' ;
}
2018-01-14 23:43:55 +03:00
2018-09-11 07:51:35 -05:00
if ( ! LegacyAuth :: user () -> hasGlobalRead ()) {
2018-07-09 14:23:38 -05:00
$sql .= " AND `d`.`device_id` IN (SELECT device_id FROM devices_perms WHERE user_id = ?) " ;
2018-09-11 07:51:35 -05:00
$param [] = LegacyAuth :: id ();
2017-11-29 09:42:52 +00:00
}
2015-07-13 20:10:26 +02:00
$devices = array ();
2018-01-14 23:43:55 +03:00
$dev_query = " SELECT $select FROM `devices` AS d $join WHERE $sql GROUP BY d.`hostname` ORDER BY $order " ;
foreach ( dbFetchRows ( $dev_query , $param ) as $device ) {
2017-01-26 22:38:43 +00:00
$host_id = get_vm_parent_id ( $device );
2016-01-20 18:19:15 +00:00
$device [ 'ip' ] = inet6_ntop ( $device [ 'ip' ]);
2017-01-26 22:38:43 +00:00
if ( is_numeric ( $host_id )) {
$device [ 'parent_id' ] = $host_id ;
}
2015-07-13 20:10:26 +02:00
$devices [] = $device ;
}
2017-11-29 09:42:52 +00:00
api_success ( $devices , 'devices' );
2014-07-13 21:28:26 +01:00
}
2014-07-16 01:02:55 +01:00
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function add_device ()
{
2017-11-29 09:42:52 +00:00
check_is_admin ();
2015-07-13 20:10:26 +02:00
// This will add a device using the data passed encoded with json
// FIXME: Execution flow through this function could be improved
global $config ;
$data = json_decode ( file_get_contents ( 'php://input' ), true );
2017-11-29 09:42:52 +00:00
2017-10-28 05:59:25 +02:00
$additional = array ();
2015-07-13 20:10:26 +02:00
// keep scrutinizer from complaining about snmpver not being set for all execution paths
$snmpver = 'v2c' ;
if ( empty ( $data )) {
2017-11-29 09:42:52 +00:00
api_error ( 400 , 'No information has been provided to add this new device' );
}
if ( empty ( $data [ 'hostname' ])) {
api_error ( 400 , 'Missing the device hostname' );
2015-07-13 20:10:26 +02:00
}
$hostname = $data [ 'hostname' ];
$port = $data [ 'port' ] ? mres ( $data [ 'port' ]) : $config [ 'snmp' ][ 'port' ];
$transport = $data [ 'transport' ] ? mres ( $data [ 'transport' ]) : 'udp' ;
$poller_group = $data [ 'poller_group' ] ? mres ( $data [ 'poller_group' ]) : 0 ;
2017-01-06 22:47:00 +01:00
$force_add = $data [ 'force_add' ] ? true : false ;
2017-10-28 05:59:25 +02:00
$snmp_disable = ( $data [ 'snmp_disable' ]);
if ( $snmp_disable ) {
$additional = array (
'os' => $data [ 'os' ] ? mres ( $data [ 'os' ]) : 'ping' ,
'hardware' => $data [ 'hardware' ] ? mres ( $data [ 'hardware' ]) : '' ,
'snmp_disable' => 1 ,
);
} elseif ( $data [ 'version' ] == 'v1' || $data [ 'version' ] == 'v2c' ) {
2015-07-13 20:10:26 +02:00
if ( $data [ 'community' ]) {
$config [ 'snmp' ][ 'community' ] = array ( $data [ 'community' ]);
}
$snmpver = mres ( $data [ 'version' ]);
2016-08-18 20:28:22 -05:00
} elseif ( $data [ 'version' ] == 'v3' ) {
2015-07-13 20:10:26 +02:00
$v3 = array (
'authlevel' => mres ( $data [ 'authlevel' ]),
'authname' => mres ( $data [ 'authname' ]),
'authpass' => mres ( $data [ 'authpass' ]),
'authalgo' => mres ( $data [ 'authalgo' ]),
'cryptopass' => mres ( $data [ 'cryptopass' ]),
'cryptoalgo' => mres ( $data [ 'cryptoalgo' ]),
);
2018-08-29 15:16:33 +01:00
array_unshift ( $config [ 'snmp' ][ 'v3' ], $v3 );
2015-07-13 20:10:26 +02:00
$snmpver = 'v3' ;
2016-08-18 20:28:22 -05:00
} else {
2017-11-29 09:42:52 +00:00
api_error ( 400 , 'You haven\'t specified an SNMP version to use' );
}
try {
$device_id = addHost ( $hostname , $snmpver , $port , $transport , $poller_group , $force_add , 'ifIndex' , $additional );
} catch ( Exception $e ) {
api_error ( 500 , $e -> getMessage ());
2015-07-13 20:10:26 +02:00
}
2017-11-29 09:42:52 +00:00
api_success_noresult ( 201 , " Device $hostname ( $device_id ) has been added successfully " );
2014-07-16 01:02:55 +01:00
}
2014-09-17 21:03:02 +01:00
2016-08-18 20:28:22 -05:00
function del_device ()
{
2017-11-29 09:42:52 +00:00
check_is_admin ();
2015-07-13 20:10:26 +02:00
// This will add a device using the data passed encoded with json
global $config ;
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
$hostname = $router [ 'hostname' ];
2017-11-29 09:42:52 +00:00
check_not_demo ();
if ( empty ( $hostname )) {
api_error ( 400 , 'No hostname has been provided to delete' );
}
2015-07-13 20:10:26 +02:00
2017-11-29 09:42:52 +00:00
// allow deleting by device_id or hostname
$device_id = ctype_digit ( $hostname ) ? $hostname : getidbyname ( $hostname );
$device = null ;
if ( $device_id ) {
// save the current details for returning to the client on successful delete
$device = device_by_id_cache ( $device_id );
2014-09-17 21:03:02 +01:00
}
2014-09-27 16:32:55 +10:00
2018-07-13 17:08:00 -05:00
if ( ! $device ) {
2017-11-29 09:42:52 +00:00
api_error ( 404 , " Device $hostname not found " );
}
$response = delete_device ( $device_id );
if ( empty ( $response )) {
// FIXME: Need to provide better diagnostics out of delete_device
api_error ( 500 , 'Device deletion failed' );
}
// deletion succeeded - include old device details in response
api_success ( array ( $device ), 'devices' , $response );
2014-09-17 21:03:02 +01:00
}
2014-10-01 20:49:34 +01:00
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function get_vlans ()
{
2014-10-01 20:53:15 +01:00
// This will list all vlans for a given device
2015-07-13 20:10:26 +02:00
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
2014-10-01 20:53:15 +01:00
$hostname = $router [ 'hostname' ];
2017-11-29 09:42:52 +00:00
2015-07-13 20:10:26 +02:00
if ( empty ( $hostname )) {
2017-11-29 09:42:52 +00:00
api_error ( 500 , 'No hostname has been provided' );
}
2015-07-13 20:10:26 +02:00
2017-11-29 09:42:52 +00:00
$device_id = ctype_digit ( $hostname ) ? $hostname : getidbyname ( $hostname );
$device = null ;
if ( $device_id ) {
// save the current details for returning to the client on successful delete
$device = device_by_id_cache ( $device_id );
2014-10-01 20:49:34 +01:00
}
2015-07-13 20:10:26 +02:00
2017-11-29 09:42:52 +00:00
if ( ! $device ) {
api_error ( 404 , " Device $hostname not found " );
}
check_device_permission ( $device_id );
$vlans = dbFetchRows ( 'SELECT vlan_vlan,vlan_domain,vlan_name,vlan_type,vlan_mtu FROM vlans WHERE `device_id` = ?' , array ( $device_id ));
api_success ( $vlans , 'vlans' );
2014-10-01 20:49:34 +01:00
}
2014-10-09 16:20:04 +00:00
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function show_endpoints ()
{
2014-10-09 16:20:04 +00:00
global $config ;
2015-07-13 20:10:26 +02:00
$app = \Slim\Slim :: getInstance ();
$routes = $app -> router () -> getNamedRoutes ();
2014-10-09 16:47:11 +00:00
$output = array ();
2015-07-13 20:10:26 +02:00
foreach ( $routes as $route ) {
$output [ $route -> getName ()] = $config [ 'base_url' ] . $route -> getPattern ();
2014-10-09 16:20:04 +00:00
}
2015-07-13 20:10:26 +02:00
2014-10-09 16:20:04 +00:00
$app -> response -> setStatus ( '200' );
$app -> response -> headers -> set ( 'Content-Type' , 'application/json' );
echo _json_encode ( $output );
}
2014-10-26 15:48:51 +00:00
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function list_bgp ()
{
2017-11-29 09:42:52 +00:00
check_is_read ();
2015-07-13 20:10:26 +02:00
$app = \Slim\Slim :: getInstance ();
2017-11-29 09:42:52 +00:00
2015-07-13 20:10:26 +02:00
$sql = '' ;
2014-10-29 21:30:35 +00:00
$sql_params = array ();
2017-03-13 02:17:09 +00:00
$hostname = $_GET [ 'hostname' ] ? : '' ;
$asn = $_GET [ 'asn' ] ? : '' ;
2015-07-13 20:10:26 +02:00
$device_id = ctype_digit ( $hostname ) ? $hostname : getidbyname ( $hostname );
if ( is_numeric ( $device_id )) {
2017-03-13 02:17:09 +00:00
$sql = ' AND `devices`.`device_id` = ?' ;
$sql_params [] = $device_id ;
}
if ( ! empty ( $asn )) {
$sql = ' AND `devices`.`bgpLocalAs` = ?' ;
$sql_params [] = $asn ;
2014-10-26 15:48:51 +00:00
}
2015-07-13 20:10:26 +02:00
2017-03-13 02:17:09 +00:00
$bgp_sessions = dbFetchRows ( " SELECT `bgpPeers`.* FROM `bgpPeers` LEFT JOIN `devices` ON `bgpPeers`.`device_id` = `devices`.`device_id` WHERE `bgpPeerState` IS NOT NULL AND `bgpPeerState` != '' $sql " , $sql_params );
2014-10-26 15:48:51 +00:00
$total_bgp_sessions = count ( $bgp_sessions );
2017-11-29 09:42:52 +00:00
if ( ! is_numeric ( $total_bgp_sessions )) {
api_error ( 500 , 'Error retrieving bgpPeers' );
2014-10-29 21:30:35 +00:00
}
2015-07-13 20:10:26 +02:00
2017-11-29 09:42:52 +00:00
api_success ( $bgp_sessions , 'bgp_sessions' );
2017-09-07 20:04:36 +01:00
}
2017-11-30 22:51:20 +00:00
function get_bgp ()
{
check_is_read ();
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
$bgpPeerId = $router [ 'id' ];
if ( ! is_numeric ( $bgpPeerId )) {
api_error ( 400 , 'Invalid id has been provided' );
}
$bgp_session = dbFetchRows ( " SELECT * FROM `bgpPeers` WHERE `bgpPeerState` IS NOT NULL AND `bgpPeerState` != '' AND bgpPeer_id = ? " , array ( $bgpPeerId ));
$bgp_session_count = count ( $bgp_session );
if ( ! is_numeric ( $bgp_session_count )) {
api_error ( 500 , 'Error retrieving BGP peer' );
}
if ( $bgp_session_count == 0 ) {
api_error ( 404 , " BGP peer $bgpPeerId does not exist " );
}
api_success ( $bgp_session , 'bgp_session' );
}
2018-01-18 18:43:15 +03:00
function list_cbgp ()
{
$app = \Slim\Slim :: getInstance ();
$sql = '' ;
$sql_params = array ();
$hostname = $_GET [ 'hostname' ] ? : '' ;
$device_id = ctype_digit ( $hostname ) ? $hostname : getidbyname ( $hostname );
if ( is_numeric ( $device_id )) {
check_device_permission ( $device_id );
$sql = " AND `devices`.`device_id` = ? " ;
$sql_params [] = $device_id ;
}
2018-09-11 07:51:35 -05:00
if ( ! LegacyAuth :: user () -> hasGlobalRead ()) {
2018-01-18 18:43:15 +03:00
$sql .= " AND `bgpPeers_cbgp`.`device_id` IN (SELECT device_id FROM devices_perms WHERE user_id = ?) " ;
2018-09-11 07:51:35 -05:00
$sql_params [] = LegacyAuth :: id ();
2018-01-18 18:43:15 +03:00
}
$bgp_counters = array ();
foreach ( dbFetchRows ( " SELECT `bgpPeers_cbgp`.* FROM `bgpPeers_cbgp` LEFT JOIN `devices` ON `bgpPeers_cbgp`.`device_id` = `devices`.`device_id` WHERE `bgpPeers_cbgp`.`device_id` IS NOT NULL $sql " , $sql_params ) as $bgp_counter ) {
$host_id = get_vm_parent_id ( $device );
$device [ 'ip' ] = inet6_ntop ( $device [ 'ip' ]);
if ( is_numeric ( $host_id )) {
$device [ 'parent_id' ] = $host_id ;
}
$bgp_counters [] = $bgp_counter ;
}
$total_bgp_counters = count ( $bgp_counters );
if ( $total_bgp_counters == 0 ) {
api_error ( 404 , 'BGP counters does not exist' );
}
api_success ( $bgp_counters , 'bgp_counters' );
}
2017-09-07 20:04:36 +01:00
function list_ospf ()
{
2017-11-29 09:42:52 +00:00
check_is_read ();
2017-09-07 20:04:36 +01:00
$app = \Slim\Slim :: getInstance ();
$sql = '' ;
$sql_params = array ();
$hostname = $_GET [ 'hostname' ];
$device_id = ctype_digit ( $hostname ) ? $hostname : getidbyname ( $hostname );
if ( is_numeric ( $device_id )) {
$sql = ' AND `device_id`=?' ;
$sql_params = array ( $device_id );
}
$ospf_neighbours = dbFetchRows ( " SELECT * FROM ospf_nbrs WHERE `ospfNbrState` IS NOT NULL AND `ospfNbrState` != '' $sql " , $sql_params );
$total_ospf_neighbours = count ( $ospf_neighbours );
2017-11-29 09:42:52 +00:00
if ( ! is_numeric ( $total_ospf_neighbours )) {
api_error ( 500 , 'Error retrieving ospf_nbrs' );
2017-09-07 20:04:36 +01:00
}
2017-11-29 09:42:52 +00:00
api_success ( $ospf_neighbours , 'ospf_neighbours' );
2014-10-26 15:48:51 +00:00
}
2014-11-12 23:05:47 +00:00
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function get_graph_by_portgroup ()
{
2017-11-29 09:42:52 +00:00
check_is_read ();
2015-07-13 20:10:26 +02:00
global $config ;
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
2017-03-13 02:17:09 +00:00
$group = $router [ 'group' ] ? : '' ;
$id = $router [ 'id' ] ? : '' ;
2015-07-13 20:10:26 +02:00
$vars = array ();
2018-04-11 09:02:04 -04:00
$vars [ 'output' ] = $_GET [ 'output' ] ? : 'display' ;
2015-07-13 20:10:26 +02:00
if ( ! empty ( $_GET [ 'from' ])) {
$vars [ 'from' ] = $_GET [ 'from' ];
}
if ( ! empty ( $_GET [ 'to' ])) {
$vars [ 'to' ] = $_GET [ 'to' ];
}
$vars [ 'width' ] = $_GET [ 'width' ] ? : 1075 ;
$vars [ 'height' ] = $_GET [ 'height' ] ? : 300 ;
$auth = '1' ;
2017-03-13 02:17:09 +00:00
$if_list = '' ;
$ports = array ();
2015-07-13 20:10:26 +02:00
2017-03-13 02:17:09 +00:00
if ( ! empty ( $id )) {
$if_list = $id ;
} else {
$ports = get_ports_from_type ( explode ( ',' , $group ));
}
if ( empty ( $if_list )) {
$seperator = '' ;
foreach ( $ports as $port ) {
$if_list .= $seperator . $port [ 'port_id' ];
$seperator = ',' ;
}
2015-07-13 20:10:26 +02:00
}
unset ( $seperator );
$vars [ 'type' ] = 'multiport_bits_separate' ;
$vars [ 'id' ] = $if_list ;
2017-03-14 05:39:43 +13:00
$app -> response -> headers -> set ( 'Content-Type' , get_image_type ());
2017-01-25 19:16:59 +00:00
rrdtool_initialize ( false );
2019-04-11 23:26:42 -05:00
include 'includes/html/graphs/graph.inc.php' ;
2017-01-25 19:16:59 +00:00
rrdtool_close ();
2018-04-11 09:02:04 -04:00
if ( $vars [ 'output' ] === 'base64' ) {
api_success ([ 'image' => $base64_output , 'content-type' => get_image_type ()], 'image' );
}
2014-11-12 23:05:47 +00:00
}
2014-11-29 15:43:19 +00:00
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function get_components ()
{
2015-08-15 16:01:43 +10:00
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
$hostname = $router [ 'hostname' ];
// Do some filtering if the user requests.
$options = array ();
2016-01-14 05:22:37 +10:00
// We need to specify the label as this is a LIKE query
2015-08-15 16:01:43 +10:00
if ( isset ( $_GET [ 'label' ])) {
// set a label like filter
$options [ 'filter' ][ 'label' ] = array ( 'LIKE' , $_GET [ 'label' ]);
2016-08-18 20:28:22 -05:00
unset ( $_GET [ 'label' ]);
2015-08-15 16:01:43 +10:00
}
2016-01-14 05:22:37 +10:00
// Add the rest of the options with an equals query
2016-03-30 16:39:39 +10:00
foreach ( $_GET as $k => $v ) {
$options [ 'filter' ][ $k ] = array ( '=' , $v );
2015-08-15 16:01:43 +10:00
}
// use hostname as device_id if it's all digits
$device_id = ctype_digit ( $hostname ) ? $hostname : getidbyname ( $hostname );
2017-11-29 09:42:52 +00:00
check_device_permission ( $device_id );
2016-08-21 08:07:14 -05:00
$COMPONENT = new LibreNMS\Component ();
2016-08-18 20:28:22 -05:00
$components = $COMPONENT -> getComponents ( $device_id , $options );
2015-08-15 16:01:43 +10:00
2017-11-29 09:42:52 +00:00
api_success ( $components [ $device_id ], 'components' );
2015-08-15 16:01:43 +10:00
}
2016-08-18 20:28:22 -05:00
function add_components ()
{
2017-11-29 09:42:52 +00:00
check_is_admin ();
2016-03-30 16:39:39 +10:00
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
$hostname = $router [ 'hostname' ];
$ctype = $router [ 'type' ];
// use hostname as device_id if it's all digits
$device_id = ctype_digit ( $hostname ) ? $hostname : getidbyname ( $hostname );
2016-08-21 08:07:14 -05:00
$COMPONENT = new LibreNMS\Component ();
2016-08-18 20:28:22 -05:00
$component = $COMPONENT -> createComponent ( $device_id , $ctype );
2016-03-30 16:39:39 +10:00
2017-11-29 09:42:52 +00:00
api_success ( $component , 'components' );
2016-03-30 16:39:39 +10:00
}
2016-08-18 20:28:22 -05:00
function edit_components ()
{
2017-11-29 09:42:52 +00:00
check_is_admin ();
2016-03-30 16:39:39 +10:00
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
$hostname = $router [ 'hostname' ];
$data = json_decode ( file_get_contents ( 'php://input' ), true );
// use hostname as device_id if it's all digits
$device_id = ctype_digit ( $hostname ) ? $hostname : getidbyname ( $hostname );
2016-08-21 08:07:14 -05:00
$COMPONENT = new LibreNMS\Component ();
2016-03-30 16:39:39 +10:00
2017-11-29 09:42:52 +00:00
if ( ! $COMPONENT -> setComponentPrefs ( $device_id , $data )) {
api_error ( 500 , 'Components could not be edited.' );
2016-03-30 16:39:39 +10:00
}
2017-11-29 09:42:52 +00:00
api_success_noresult ( 200 );
2016-03-30 16:39:39 +10:00
}
2016-08-18 20:28:22 -05:00
function delete_components ()
{
2017-11-29 09:42:52 +00:00
check_is_admin ();
2016-03-30 16:39:39 +10:00
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
$cid = $router [ 'component' ];
2016-08-21 08:07:14 -05:00
$COMPONENT = new LibreNMS\Component ();
2016-03-30 16:39:39 +10:00
if ( $COMPONENT -> deleteComponent ( $cid )) {
2017-11-29 09:42:52 +00:00
api_success_noresult ( 200 );
2016-08-18 20:28:22 -05:00
} else {
2017-11-29 09:42:52 +00:00
api_error ( 500 , 'Components could not be deleted.' );
2016-03-30 16:39:39 +10:00
}
}
2016-08-18 20:28:22 -05:00
function get_graphs ()
{
2014-11-29 15:43:19 +00:00
global $config ;
2015-07-13 20:10:26 +02:00
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
2014-11-29 15:43:19 +00:00
$hostname = $router [ 'hostname' ];
2015-06-13 16:14:19 +10:00
// FIXME: this has some overlap with html/pages/device/graphs.inc.php
2014-11-29 15:43:19 +00:00
// use hostname as device_id if it's all digits
$device_id = ctype_digit ( $hostname ) ? $hostname : getidbyname ( $hostname );
2017-11-29 09:42:52 +00:00
check_device_permission ( $device_id );
2015-07-13 20:10:26 +02:00
$graphs = array ();
$graphs [] = array (
'desc' => 'Poller Time' ,
'name' => 'device_poller_perf' ,
);
$graphs [] = array (
'desc' => 'Ping Response' ,
'name' => 'device_ping_perf' ,
);
foreach ( dbFetchRows ( 'SELECT * FROM device_graphs WHERE device_id = ? ORDER BY graph' , array ( $device_id )) as $graph ) {
$desc = $config [ 'graph_types' ][ 'device' ][ $graph [ 'graph' ]][ 'descr' ];
$graphs [] = array (
'desc' => $desc ,
'name' => 'device_' . $graph [ 'graph' ],
);
2014-11-29 15:43:19 +00:00
}
2015-07-13 20:10:26 +02:00
2017-11-29 09:42:52 +00:00
return api_success ( $graphs , 'graphs' );
2014-11-29 15:43:19 +00:00
}
2014-12-01 20:27:50 +00:00
2017-01-19 10:09:20 +00:00
function list_available_health_graphs ()
{
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
$hostname = $router [ 'hostname' ];
$device_id = ctype_digit ( $hostname ) ? $hostname : getidbyname ( $hostname );
2017-11-29 09:42:52 +00:00
check_device_permission ( $device_id );
2017-01-19 10:09:20 +00:00
if ( isset ( $router [ 'type' ])) {
list ( $dump , $type ) = explode ( '_' , $router [ 'type' ]);
}
$sensor_id = $router [ 'sensor_id' ] ? : null ;
$graphs = array ();
if ( isset ( $type )) {
if ( isset ( $sensor_id )) {
$graphs = dbFetchRows ( 'SELECT * FROM `sensors` WHERE `sensor_id` = ?' , array ( $sensor_id ));
} else {
foreach ( dbFetchRows ( 'SELECT `sensor_id`, `sensor_descr` FROM `sensors` WHERE `device_id` = ? AND `sensor_class` = ? AND `sensor_deleted` = 0' , array ( $device_id , $type )) as $graph ) {
$graphs [] = array (
'sensor_id' => $graph [ 'sensor_id' ],
'desc' => $graph [ 'sensor_descr' ],
);
}
}
} else {
foreach ( dbFetchRows ( 'SELECT `sensor_class` FROM `sensors` WHERE `device_id` = ? AND `sensor_deleted` = 0 GROUP BY `sensor_class`' , array ( $device_id )) as $graph ) {
$graphs [] = array (
'desc' => ucfirst ( $graph [ 'sensor_class' ]),
'name' => 'device_' . $graph [ 'sensor_class' ],
);
}
2018-05-19 00:36:06 -04:00
$device = \App\Models\Device :: find ( $device_id );
if ( $device ) {
if ( $device -> processors () -> count () > 0 ) {
array_push ( $graphs , array (
'desc' => 'Processors' ,
'name' => 'device_processor'
));
}
2018-07-09 14:23:38 -05:00
2018-05-19 00:36:06 -04:00
if ( $device -> storage () -> count () > 0 ) {
array_push ( $graphs , array (
'desc' => 'Storage' ,
'name' => 'device_storage'
));
}
2018-07-09 14:23:38 -05:00
2018-05-19 00:36:06 -04:00
if ( $device -> mempools () -> count () > 0 ) {
array_push ( $graphs , array (
'desc' => 'Memory Pools' ,
'name' => 'device_mempool'
));
}
}
2017-01-19 10:09:20 +00:00
}
2017-11-29 09:42:52 +00:00
return api_success ( $graphs , 'graphs' );
2017-01-19 10:09:20 +00:00
}
2015-07-13 20:10:26 +02:00
2017-12-06 22:27:30 +00:00
function list_available_wireless_graphs ()
{
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
$hostname = $router [ 'hostname' ];
$device_id = ctype_digit ( $hostname ) ? $hostname : getidbyname ( $hostname );
check_device_permission ( $device_id );
if ( isset ( $router [ 'type' ])) {
list (, , $type ) = explode ( '_' , $router [ 'type' ]);
}
$sensor_id = $router [ 'sensor_id' ] ? : null ;
$graphs = array ();
if ( isset ( $type )) {
if ( isset ( $sensor_id )) {
$graphs = dbFetchRows ( 'SELECT * FROM `wireless_sensors` WHERE `sensor_id` = ?' , array ( $sensor_id ));
} else {
foreach ( dbFetchRows ( 'SELECT `sensor_id`, `sensor_descr` FROM `wireless_sensors` WHERE `device_id` = ? AND `sensor_class` = ? AND `sensor_deleted` = 0' , array ( $device_id , $type )) as $graph ) {
$graphs [] = array (
'sensor_id' => $graph [ 'sensor_id' ],
'desc' => $graph [ 'sensor_descr' ],
);
}
}
} else {
foreach ( dbFetchRows ( 'SELECT `sensor_class` FROM `wireless_sensors` WHERE `device_id` = ? AND `sensor_deleted` = 0 GROUP BY `sensor_class`' , array ( $device_id )) as $graph ) {
$graphs [] = array (
'desc' => ucfirst ( $graph [ 'sensor_class' ]),
'name' => 'device_wireless_' . $graph [ 'sensor_class' ],
);
}
}
return api_success ( $graphs , 'graphs' );
}
2016-08-18 20:28:22 -05:00
function get_port_graphs ()
{
2015-07-13 20:10:26 +02:00
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
2014-11-30 15:45:10 +00:00
$hostname = $router [ 'hostname' ];
2015-07-13 20:10:26 +02:00
if ( isset ( $_GET [ 'columns' ])) {
2014-11-30 15:45:10 +00:00
$columns = $_GET [ 'columns' ];
2016-08-18 20:28:22 -05:00
} else {
2014-11-30 15:45:10 +00:00
$columns = 'ifName' ;
}
2017-11-14 15:15:12 +00:00
validate_column_list ( $columns , 'ports' );
2014-11-30 15:45:10 +00:00
// use hostname as device_id if it's all digits
2015-07-13 20:10:26 +02:00
$device_id = ctype_digit ( $hostname ) ? $hostname : getidbyname ( $hostname );
2017-11-29 09:42:52 +00:00
$sql = '' ;
$params = array ( $device_id );
if ( ! device_permitted ( $device_id )) {
$sql = 'AND `port_id` IN (select `port_id` from `ports_perms` where `user_id` = ?)' ;
2018-09-11 07:51:35 -05:00
array_push ( $params , LegacyAuth :: id ());
2017-11-29 09:42:52 +00:00
}
2017-12-02 16:53:45 -06:00
2017-11-29 09:42:52 +00:00
$ports = dbFetchRows ( " SELECT $columns FROM `ports` WHERE `device_id` = ? AND `deleted` = '0' $sql ORDER BY `ifIndex` ASC " , $params );
api_success ( $ports , 'ports' );
2014-11-30 15:45:10 +00:00
}
2014-12-02 23:43:11 +00:00
2017-02-07 02:46:09 -07:00
function get_ip_addresses ()
{
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
2017-11-29 09:42:52 +00:00
$ipv4 = array ();
$ipv6 = array ();
2017-02-07 02:46:09 -07:00
if ( isset ( $router [ 'hostname' ])) {
$hostname = $router [ 'hostname' ];
// use hostname as device_id if it's all digits
$device_id = ctype_digit ( $hostname ) ? $hostname : getidbyname ( $hostname );
2017-11-29 09:42:52 +00:00
check_device_permission ( $device_id );
2017-02-07 02:46:09 -07:00
$ipv4 = dbFetchRows ( " SELECT `ipv4_addresses`.* FROM `ipv4_addresses` JOIN `ports` ON `ports`.`port_id`=`ipv4_addresses`.`port_id` WHERE `ports`.`device_id` = ? AND `deleted` = 0 " , array ( $device_id ));
$ipv6 = dbFetchRows ( " SELECT `ipv6_addresses`.* FROM `ipv6_addresses` JOIN `ports` ON `ports`.`port_id`=`ipv6_addresses`.`port_id` WHERE `ports`.`device_id` = ? AND `deleted` = 0 " , array ( $device_id ));
2018-01-18 18:43:15 +03:00
$ip_addresses_count = count ( array_merge ( $ipv4 , $ipv6 ));
if ( $ip_addresses_count == 0 ) {
api_error ( 404 , " Device $device_id does not have any IP addresses " );
}
2017-02-07 02:46:09 -07:00
} elseif ( isset ( $router [ 'portid' ])) {
$port_id = urldecode ( $router [ 'portid' ]);
2017-11-29 09:42:52 +00:00
check_port_permission ( $port_id , null );
2017-02-07 02:46:09 -07:00
$ipv4 = dbFetchRows ( " SELECT * FROM `ipv4_addresses` WHERE `port_id` = ? " , array ( $port_id ));
$ipv6 = dbFetchRows ( " SELECT * FROM `ipv6_addresses` WHERE `port_id` = ? " , array ( $port_id ));
2018-01-18 18:43:15 +03:00
$ip_addresses_count = count ( array_merge ( $ipv4 , $ipv6 ));
if ( $ip_addresses_count == 0 ) {
api_error ( 404 , " Port $port_id does not have any IP addresses " );
}
} elseif ( isset ( $router [ 'id' ])) {
check_is_read ();
$network_id = $router [ 'id' ];
$ipv4 = dbFetchRows ( " SELECT * FROM `ipv4_addresses` WHERE `ipv4_network_id` = ? " , array ( $network_id ));
$ipv6 = dbFetchRows ( " SELECT * FROM `ipv6_addresses` WHERE `ipv6_network_id` = ? " , array ( $network_id ));
$ip_addresses_count = count ( array_merge ( $ipv4 , $ipv6 ));
if ( $ip_addresses_count == 0 ) {
api_error ( 404 , " IP network $network_id does not exist or is empty " );
}
2017-02-07 02:46:09 -07:00
}
2017-11-29 09:42:52 +00:00
api_success ( array_merge ( $ipv4 , $ipv6 ), 'addresses' );
2017-02-07 02:46:09 -07:00
}
function get_port_info ()
{
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
$port_id = urldecode ( $router [ 'portid' ]);
2017-11-29 09:42:52 +00:00
check_port_permission ( $port_id , null );
2017-02-07 02:46:09 -07:00
// use hostname as device_id if it's all digits
$port = dbFetchRows ( " SELECT * FROM `ports` WHERE `port_id` = ? AND `deleted` = 0 " , array ( $port_id ));
2017-11-29 09:42:52 +00:00
api_success ( $port , 'port' );
2017-02-07 02:46:09 -07:00
}
function get_all_ports ()
{
$app = \Slim\Slim :: getInstance ();
if ( isset ( $_GET [ 'columns' ])) {
$columns = $_GET [ 'columns' ];
} else {
2017-11-29 09:42:52 +00:00
$columns = 'port_id, ifName' ;
2017-02-07 02:46:09 -07:00
}
2017-11-14 15:15:12 +00:00
validate_column_list ( $columns , 'ports' );
2017-11-29 09:42:52 +00:00
$params = array ();
$sql = '' ;
2018-09-11 07:51:35 -05:00
if ( ! LegacyAuth :: user () -> hasGlobalRead ()) {
2017-11-29 09:42:52 +00:00
$sql = ' AND (device_id IN (SELECT device_id FROM devices_perms WHERE user_id = ?) OR port_id IN (SELECT port_id FROM ports_perms WHERE user_id = ?))' ;
2018-09-11 07:51:35 -05:00
array_push ( $params , LegacyAuth :: id ());
array_push ( $params , LegacyAuth :: id ());
2017-11-29 09:42:52 +00:00
}
$ports = dbFetchRows ( " SELECT $columns FROM `ports` WHERE `deleted` = 0 $sql " , $params );
2017-02-07 02:46:09 -07:00
2017-11-29 09:42:52 +00:00
api_success ( $ports , 'ports' );
2017-02-07 02:46:09 -07:00
}
2016-08-18 20:28:22 -05:00
function get_port_stack ()
{
2016-04-05 13:03:17 -07:00
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
$hostname = $router [ 'hostname' ];
// use hostname as device_id if it's all digits
$device_id = ctype_digit ( $hostname ) ? $hostname : getidbyname ( $hostname );
2017-11-29 09:42:52 +00:00
check_device_permission ( $device_id );
2016-04-05 14:25:45 -07:00
if ( isset ( $_GET [ 'valid_mappings' ])) {
$mappings = dbFetchRows ( " SELECT * FROM `ports_stack` WHERE (`device_id` = ? AND `ifStackStatus` = 'active' AND (`port_id_high` != '0' AND `port_id_low` != '0')) ORDER BY `port_id_high` ASC " , array ( $device_id ));
} else {
$mappings = dbFetchRows ( " SELECT * FROM `ports_stack` WHERE `device_id` = ? AND `ifStackStatus` = 'active' ORDER BY `port_id_high` ASC " , array ( $device_id ));
}
2017-11-29 09:42:52 +00:00
api_success ( $mappings , 'mappings' );
2016-04-05 13:03:17 -07:00
}
2016-08-18 20:28:22 -05:00
function list_alert_rules ()
{
2017-11-29 09:42:52 +00:00
check_is_read ();
2015-07-13 20:10:26 +02:00
$app = \Slim\Slim :: getInstance ();
2014-12-16 20:39:58 +00:00
$router = $app -> router () -> getCurrentRoute () -> getParams ();
2015-07-13 20:10:26 +02:00
$sql = '' ;
$param = array ();
if ( isset ( $router [ 'id' ]) && $router [ 'id' ] > 0 ) {
2014-12-16 20:39:58 +00:00
$rule_id = mres ( $router [ 'id' ]);
2015-07-13 20:10:26 +02:00
$sql = 'WHERE id=?' ;
$param = array ( $rule_id );
2014-12-16 20:39:58 +00:00
}
2015-07-13 20:10:26 +02:00
$rules = dbFetchRows ( " SELECT * FROM `alert_rules` $sql " , $param );
2017-11-29 09:42:52 +00:00
api_success ( $rules , 'rules' );
2014-12-16 20:39:58 +00:00
}
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function list_alerts ()
{
2017-11-29 09:42:52 +00:00
check_is_read ();
2015-07-13 20:10:26 +02:00
$app = \Slim\Slim :: getInstance ();
2014-12-16 20:39:58 +00:00
$router = $app -> router () -> getCurrentRoute () -> getParams ();
2018-08-26 07:42:21 -05:00
$sql = " SELECT `D`.`hostname`, `A`.*, `R`.`severity` FROM `alerts` AS `A`, `devices` AS `D`, `alert_rules` AS `R` WHERE `D`.`device_id` = `A`.`device_id` AND `A`.`rule_id` = `R`.`id` AND `A`.`state` IN " ;
2015-10-08 15:15:44 +02:00
if ( isset ( $_GET [ 'state' ])) {
2018-08-26 07:42:21 -05:00
$param = explode ( ',' , $_GET [ 'state' ]);
2016-08-18 20:28:22 -05:00
} else {
2018-08-26 07:42:21 -05:00
$param = [ 1 ];
2014-12-16 20:39:58 +00:00
}
2018-08-26 07:42:21 -05:00
$sql .= dbGenPlaceholders ( count ( $param ));
2015-07-13 20:10:26 +02:00
if ( isset ( $router [ 'id' ]) && $router [ 'id' ] > 0 ) {
2018-08-26 07:42:21 -05:00
$param [] = $router [ 'id' ];
$sql .= 'AND `A`.id=?' ;
2014-12-16 20:39:58 +00:00
}
2015-07-13 20:10:26 +02:00
2018-08-28 18:44:47 +02:00
$severity = $_GET [ 'severity' ];
if ( isset ( $severity )) {
if ( in_array ( $severity , [ 'ok' , 'warning' , 'critical' ])) {
$param [] = $severity ;
$sql .= ' AND `R`.severity=?' ;
}
}
$order = $_GET [ 'order' ] ? : " timestamp desc " ;
$sql .= ' ORDER BY A.' . $order ;
2018-08-26 07:42:21 -05:00
$alerts = dbFetchRows ( $sql , $param );
2017-11-29 09:42:52 +00:00
api_success ( $alerts , 'alerts' );
2014-12-16 20:39:58 +00:00
}
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function add_edit_rule ()
{
2017-11-29 09:42:52 +00:00
check_is_admin ();
2015-07-13 20:10:26 +02:00
$app = \Slim\Slim :: getInstance ();
2014-12-16 20:39:58 +00:00
$data = json_decode ( file_get_contents ( 'php://input' ), true );
2018-09-19 13:47:45 +01:00
if ( json_last_error ()) {
api_error ( 500 , " We couldn't parse the provided json " );
}
2014-12-16 20:39:58 +00:00
$rule_id = mres ( $data [ 'rule_id' ]);
2018-09-19 13:47:45 +01:00
$tmp_devices = ( array ) mres ( $data [ 'devices' ]);
$groups = ( array ) $data [ 'groups' ];
if ( empty ( $tmp_devices ) && ! isset ( $rule_id )) {
api_error ( 400 , 'Missing the devices or global device (-1)' );
2017-11-29 09:42:52 +00:00
}
2017-12-02 16:53:45 -06:00
2018-09-19 13:47:45 +01:00
$devices = [];
foreach ( $tmp_devices as $device ) {
if ( $device == " -1 " ) {
continue ;
}
$devices [] = ctype_digit ( $device ) ? $device : getidbyname ( $device );
2014-12-16 20:39:58 +00:00
}
2018-09-19 13:47:45 +01:00
$builder = $data [ 'builder' ] ? : $data [ 'rule' ];
if ( empty ( $builder )) {
api_error ( 400 , 'Missing the alert builder rule' );
2014-12-16 20:39:58 +00:00
}
2015-07-13 20:10:26 +02:00
2015-07-01 21:35:37 +01:00
$name = mres ( $data [ 'name' ]);
if ( empty ( $name )) {
2017-11-29 09:42:52 +00:00
api_error ( 400 , 'Missing the alert rule name' );
2015-07-01 21:35:37 +01:00
}
2015-07-13 20:10:26 +02:00
2014-12-16 20:39:58 +00:00
$severity = mres ( $data [ 'severity' ]);
2015-07-13 20:10:26 +02:00
$sevs = array (
'ok' ,
'warning' ,
'critical' ,
);
if ( ! in_array ( $severity , $sevs )) {
2017-11-29 09:42:52 +00:00
api_error ( 400 , 'Missing the severity' );
2014-12-16 20:39:58 +00:00
}
2015-07-13 20:10:26 +02:00
2014-12-16 20:39:58 +00:00
$disabled = mres ( $data [ 'disabled' ]);
2015-07-13 20:10:26 +02:00
if ( $disabled != '0' && $disabled != '1' ) {
2014-12-16 20:39:58 +00:00
$disabled = 0 ;
}
2015-07-13 20:10:26 +02:00
$count = mres ( $data [ 'count' ]);
$mute = mres ( $data [ 'mute' ]);
$delay = mres ( $data [ 'delay' ]);
2018-09-19 13:47:45 +01:00
$override_query = $data [ 'override_query' ];
$adv_query = $data [ 'adv_query' ];
2014-12-16 20:39:58 +00:00
$delay_sec = convert_delay ( $delay );
2015-07-13 20:10:26 +02:00
if ( $mute == 1 ) {
2014-12-16 20:39:58 +00:00
$mute = true ;
2016-08-18 20:28:22 -05:00
} else {
2014-12-16 20:39:58 +00:00
$mute = false ;
}
2018-09-19 13:47:45 +01:00
$extra = [
2015-07-13 20:10:26 +02:00
'mute' => $mute ,
'count' => $count ,
'delay' => $delay_sec ,
2018-09-19 13:47:45 +01:00
'options' =>
[
'override_query' => $override_query
],
];
2014-12-16 20:39:58 +00:00
$extra_json = json_encode ( $extra );
2018-09-19 13:47:45 +01:00
if ( $override_query === 'on' ) {
$query = $adv_query ;
} else {
$query = QueryBuilderParser :: fromJson ( $builder ) -> toSql ();
if ( empty ( $query )) {
api_error ( 500 , " We couldn't parse your rule " );
}
}
2016-08-18 20:28:22 -05:00
if ( ! isset ( $rule_id )) {
2015-10-16 16:18:18 +02:00
if ( dbFetchCell ( 'SELECT `name` FROM `alert_rules` WHERE `name`=?' , array ( $name )) == $name ) {
2017-11-29 09:42:52 +00:00
api_error ( 500 , 'Addition failed : Name has already been used' );
2015-10-16 16:18:18 +02:00
}
2016-08-18 20:28:22 -05:00
} else {
if ( dbFetchCell ( " SELECT name FROM alert_rules WHERE name=? AND id !=? " , array ( $name , $rule_id )) == $name ) {
2018-09-19 13:47:45 +01:00
api_error ( 500 , 'Update failed : Invalid rule id' );
2015-10-16 16:18:18 +02:00
}
2015-07-01 21:35:37 +01:00
}
2017-11-29 09:42:52 +00:00
if ( is_numeric ( $rule_id )) {
2018-09-19 13:47:45 +01:00
if ( ! ( dbUpdate ( array ( 'name' => $name , 'builder' => $builder , 'query' => $query , 'severity' => $severity , 'disabled' => $disabled , 'extra' => $extra_json ), 'alert_rules' , 'id=?' , array ( $rule_id )) >= 0 )) {
2017-11-29 09:42:52 +00:00
api_error ( 500 , 'Failed to update existing alert rule' );
2014-12-16 20:39:58 +00:00
}
2018-09-19 13:47:45 +01:00
} elseif ( ! $rule_id = dbInsert ( array ( 'name' => $name , 'builder' => $builder , 'query' => $query , 'severity' => $severity , 'disabled' => $disabled , 'extra' => $extra_json ), 'alert_rules' )) {
2017-11-29 09:42:52 +00:00
api_error ( 500 , 'Failed to create new alert rule' );
2014-12-16 20:39:58 +00:00
}
2015-07-13 20:10:26 +02:00
2018-09-19 13:47:45 +01:00
dbSyncRelationship ( 'alert_device_map' , 'rule_id' , $rule_id , 'device_id' , $devices );
dbSyncRelationship ( 'alert_group_map' , 'rule_id' , $rule_id , 'group_id' , $groups );
2017-11-29 09:42:52 +00:00
api_success_noresult ( 200 );
2014-12-16 20:39:58 +00:00
}
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function delete_rule ()
{
2017-11-29 09:42:52 +00:00
check_is_admin ();
2015-07-13 20:10:26 +02:00
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
2014-12-16 20:39:58 +00:00
$rule_id = mres ( $router [ 'id' ]);
2015-07-13 20:10:26 +02:00
if ( is_numeric ( $rule_id )) {
if ( dbDelete ( 'alert_rules' , '`id` = ? LIMIT 1' , array ( $rule_id ))) {
2017-11-29 09:42:52 +00:00
api_success_noresult ( 200 , 'Alert rule has been removed' );
2016-08-18 20:28:22 -05:00
} else {
2017-11-29 09:42:52 +00:00
api_success_noresult ( 200 , 'No alert rule by that ID' );
2014-12-16 20:39:58 +00:00
}
2016-08-18 20:28:22 -05:00
} else {
2017-11-29 09:42:52 +00:00
api_error ( 400 , 'Invalid rule id has been provided' );
2014-12-16 20:39:58 +00:00
}
}
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function ack_alert ()
{
2017-11-29 09:42:52 +00:00
check_is_admin ();
2018-09-19 23:38:01 +01:00
2015-07-13 20:10:26 +02:00
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
2014-12-16 20:39:58 +00:00
$alert_id = mres ( $router [ 'id' ]);
2018-09-19 23:38:01 +01:00
$data = json_decode ( file_get_contents ( 'php://input' ), true );
2017-11-29 09:42:52 +00:00
if ( ! is_numeric ( $alert_id )) {
api_error ( 400 , 'Invalid alert has been provided' );
}
2018-09-19 23:38:01 +01:00
$alert = dbFetchRow ( 'SELECT note, info FROM alerts WHERE id=?' , [ $alert_id ]);
$note = $alert [ 'note' ];
$info = json_decode ( $alert [ 'info' ], true );
if ( ! empty ( $note )) {
$note .= PHP_EOL ;
}
$note .= date ( Config :: get ( 'dateformat.long' )) . " - Ack ( " . Auth :: user () -> username . " ) { $data [ 'note' ] } " ;
$info [ 'until_clear' ] = $data [ 'until_clear' ];
$info = json_encode ( $info );
if ( dbUpdate ([ 'state' => 2 , 'note' => $note , 'info' => $info ], 'alerts' , '`id` = ? LIMIT 1' , [ $alert_id ])) {
2017-11-29 09:42:52 +00:00
api_success_noresult ( 200 , 'Alert has been acknowledged' );
2016-08-18 20:28:22 -05:00
} else {
2017-11-29 09:42:52 +00:00
api_success_noresult ( 200 , 'No Alert by that ID' );
2014-12-16 20:39:58 +00:00
}
2015-10-08 16:45:36 +02:00
}
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function unmute_alert ()
{
2017-11-29 09:42:52 +00:00
check_is_admin ();
2018-09-19 23:38:01 +01:00
2015-10-08 16:45:36 +02:00
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
$alert_id = mres ( $router [ 'id' ]);
2018-09-19 23:38:01 +01:00
$data = json_decode ( file_get_contents ( 'php://input' ), true );
2017-11-29 09:42:52 +00:00
if ( ! is_numeric ( $alert_id )) {
2018-09-19 23:38:01 +01:00
api_error ( 400 , 'Invalid alert has been provided' );
}
$alert = dbFetchRow ( 'SELECT note, info FROM alerts WHERE id=?' , [ $alert_id ]);
$note = $alert [ 'note' ];
$info = json_decode ( $alert [ 'info' ], true );
if ( ! empty ( $note )) {
$note .= PHP_EOL ;
2015-10-08 16:45:36 +02:00
}
2018-09-19 23:38:01 +01:00
$note .= date ( Config :: get ( 'dateformat.long' )) . " - Ack ( " . Auth :: user () -> username . " ) { $data [ 'note' ] } " ;
2015-10-08 16:45:36 +02:00
2018-09-19 23:38:01 +01:00
if ( dbUpdate ([ 'state' => 1 , 'note' => $note ], 'alerts' , '`id` = ? LIMIT 1' , [ $alert_id ])) {
2017-11-29 09:42:52 +00:00
api_success_noresult ( 200 , 'Alert has been unmuted' );
} else {
api_success_noresult ( 200 , 'No alert by that ID' );
}
2014-12-16 20:39:58 +00:00
}
2015-03-23 15:15:05 +00:00
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function get_inventory ()
{
2015-03-23 15:15:05 +00:00
global $config ;
2015-07-13 20:10:26 +02:00
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
2017-11-29 09:42:52 +00:00
2015-03-23 15:15:05 +00:00
$hostname = $router [ 'hostname' ];
// use hostname as device_id if it's all digits
$device_id = ctype_digit ( $hostname ) ? $hostname : getidbyname ( $hostname );
2017-11-29 09:42:52 +00:00
check_device_permission ( $device_id );
2015-07-13 20:10:26 +02:00
$sql = '' ;
$params = array ();
2015-03-23 15:15:05 +00:00
if ( isset ( $_GET [ 'entPhysicalClass' ]) && ! empty ( $_GET [ 'entPhysicalClass' ])) {
2015-07-13 20:10:26 +02:00
$sql .= ' AND entPhysicalClass=?' ;
2015-03-23 15:15:05 +00:00
$params [] = mres ( $_GET [ 'entPhysicalClass' ]);
}
2015-07-13 20:10:26 +02:00
2015-03-23 15:15:05 +00:00
if ( isset ( $_GET [ 'entPhysicalContainedIn' ]) && ! empty ( $_GET [ 'entPhysicalContainedIn' ])) {
2015-07-13 20:10:26 +02:00
$sql .= ' AND entPhysicalContainedIn=?' ;
2015-03-23 15:15:05 +00:00
$params [] = mres ( $_GET [ 'entPhysicalContainedIn' ]);
2016-08-18 20:28:22 -05:00
} else {
2015-03-23 15:15:05 +00:00
$sql .= ' AND entPhysicalContainedIn="0"' ;
}
2015-07-13 20:10:26 +02:00
2015-03-23 15:15:05 +00:00
if ( ! is_numeric ( $device_id )) {
2017-11-29 09:42:52 +00:00
api_error ( 400 , 'Invalid device provided' );
2015-03-23 15:15:05 +00:00
}
2017-11-29 09:42:52 +00:00
$sql .= ' AND `device_id`=?' ;
$params [] = $device_id ;
$inventory = dbFetchRows ( " SELECT * FROM `entPhysical` WHERE 1 $sql " , $params );
2015-07-13 20:10:26 +02:00
2017-11-29 09:42:52 +00:00
api_success ( $inventory , 'inventory' );
2015-03-23 15:15:05 +00:00
}
2015-05-07 00:32:58 +01:00
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function list_oxidized ()
{
2017-11-29 09:42:52 +00:00
check_is_read ();
2015-12-02 20:22:38 +00:00
global $config ;
2015-07-13 20:10:26 +02:00
$app = \Slim\Slim :: getInstance ();
2017-11-16 21:21:52 +00:00
$router = $app -> router () -> getCurrentRoute () -> getParams ();
2015-07-13 20:10:26 +02:00
2017-11-16 21:21:52 +00:00
$hostname = $router [ 'hostname' ];
2015-07-13 20:10:26 +02:00
$devices = array ();
2015-12-02 20:22:38 +00:00
$device_types = " ' " . implode ( " ',' " , $config [ 'oxidized' ][ 'ignore_types' ]) . " ' " ;
$device_os = " ' " . implode ( " ',' " , $config [ 'oxidized' ][ 'ignore_os' ]) . " ' " ;
2017-11-16 21:21:52 +00:00
$sql = '' ;
$params = array ();
if ( $hostname ) {
$sql = " AND hostname = ? " ;
$params = array ( $hostname );
}
2018-11-29 13:04:11 -06:00
foreach ( dbFetchRows ( " SELECT hostname,sysname,sysDescr,hardware,os,locations.location,ip AS ip FROM `devices` LEFT JOIN locations ON devices.location_id = locations.id LEFT JOIN devices_attribs AS `DA` ON devices.device_id = DA.device_id AND `DA`.attrib_type='override_Oxidized_disable' WHERE `disabled`='0' AND `ignore` = 0 AND (DA.attrib_value = 'false' OR DA.attrib_value IS NULL) AND (`type` NOT IN ( $device_types ) AND `os` NOT IN ( $device_os )) $sql " , $params ) as $device ) {
api: Allow more complete overrides on Oxidized output (#8633)
This patch provides the ability to override most aspects of the configuration output provided to Oxidized. This is now backwards compatible with any current groups defined previously but allows for extra features such as group overrides in Oxidized as well as IP changes (for hosts that are treated differently inside Oxidized), as well as ProxyHost additions. Of course, this goes further and allows for any flag that can be defined within Oxidized to be mapped in the config.
Examples
1) Define a group for these hosts
```php
$config['oxidized']['maps']['group']['sysname'][] = array('regex' => '/^(foo|bar)/', 'group' => 'myGroup');
$config['oxidized']['maps']['group']['sysname'][] = array('regex' => '/^(baz)/', 'group' => 'anotherGroup');
```
2) Provide a proxy host for these hosts to bounce through
```php
$config['oxidized']['maps']['ssh_proxy']['sysname'][] = array('regex' => '/foo/', 'ssh_proxy' => 'mySshProxyHost');
$config['oxidized']['maps']['ssh_proxy']['sysname'][] = array('regex' => '/bar/', 'ssh_proxy' => 'anotherSshProxyHost');
```
3) Allow overrides of IP addresses so the external DNS is not used for connections
```php
$config['oxidized']['maps']['ip']['sysname'][] = array('regex' => '/baz/', 'ip' => '10.10.0.23');
$config['oxidized']['maps']['ip']['sysname'][] = array('regex' => '/lala/', 'ip' => '192.168.0.243');
```
As already mentioned, this doesn't stop with the above examples.
DO NOT DELETE THIS TEXT
#### Please note
> Please read this information carefully. You can run `./scripts/pre-commit.php` to check your code before submitting.
- [x] Have you followed our [code guidelines?](http://docs.librenms.org/Developing/Code-Guidelines/)
#### Testers
If you would like to test this pull request then please run: `./scripts/github-apply <pr_id>`, i.e `./scripts/github-apply 5926`
2018-05-24 07:00:39 +10:00
// Convert from packed value to human value
$device [ 'ip' ] = inet6_ntop ( $device [ 'ip' ]);
// Pre-populate the group with the default
if ( $config [ 'oxidized' ][ 'group_support' ] === true && ! empty ( $config [ 'oxidized' ][ 'default_group' ])) {
$device [ 'group' ] = $config [ 'oxidized' ][ 'default_group' ];
}
foreach ( $config [ 'oxidized' ][ 'maps' ] as $maps_column => $maps ) {
// Based on Oxidized group support we can apply groups by setting group_support to true
if ( $maps_column == " group " && ( ! isset ( $config [ 'oxidized' ][ 'group_support' ]) or $config [ 'oxidized' ][ 'group_support' ] !== true )) {
continue ;
2017-10-15 13:37:20 -05:00
}
2018-07-09 14:23:38 -05:00
api: Allow more complete overrides on Oxidized output (#8633)
This patch provides the ability to override most aspects of the configuration output provided to Oxidized. This is now backwards compatible with any current groups defined previously but allows for extra features such as group overrides in Oxidized as well as IP changes (for hosts that are treated differently inside Oxidized), as well as ProxyHost additions. Of course, this goes further and allows for any flag that can be defined within Oxidized to be mapped in the config.
Examples
1) Define a group for these hosts
```php
$config['oxidized']['maps']['group']['sysname'][] = array('regex' => '/^(foo|bar)/', 'group' => 'myGroup');
$config['oxidized']['maps']['group']['sysname'][] = array('regex' => '/^(baz)/', 'group' => 'anotherGroup');
```
2) Provide a proxy host for these hosts to bounce through
```php
$config['oxidized']['maps']['ssh_proxy']['sysname'][] = array('regex' => '/foo/', 'ssh_proxy' => 'mySshProxyHost');
$config['oxidized']['maps']['ssh_proxy']['sysname'][] = array('regex' => '/bar/', 'ssh_proxy' => 'anotherSshProxyHost');
```
3) Allow overrides of IP addresses so the external DNS is not used for connections
```php
$config['oxidized']['maps']['ip']['sysname'][] = array('regex' => '/baz/', 'ip' => '10.10.0.23');
$config['oxidized']['maps']['ip']['sysname'][] = array('regex' => '/lala/', 'ip' => '192.168.0.243');
```
As already mentioned, this doesn't stop with the above examples.
DO NOT DELETE THIS TEXT
#### Please note
> Please read this information carefully. You can run `./scripts/pre-commit.php` to check your code before submitting.
- [x] Have you followed our [code guidelines?](http://docs.librenms.org/Developing/Code-Guidelines/)
#### Testers
If you would like to test this pull request then please run: `./scripts/github-apply <pr_id>`, i.e `./scripts/github-apply 5926`
2018-05-24 07:00:39 +10:00
foreach ( $maps as $field_type => $fields ) {
foreach ( $fields as $field ) {
if ( isset ( $field [ 'regex' ]) && preg_match ( $field [ 'regex' ] . 'i' , $device [ $field_type ])) {
$device [ $maps_column ] = $field [ $maps_column ];
2016-03-05 00:48:51 +00:00
break ;
api: Allow more complete overrides on Oxidized output (#8633)
This patch provides the ability to override most aspects of the configuration output provided to Oxidized. This is now backwards compatible with any current groups defined previously but allows for extra features such as group overrides in Oxidized as well as IP changes (for hosts that are treated differently inside Oxidized), as well as ProxyHost additions. Of course, this goes further and allows for any flag that can be defined within Oxidized to be mapped in the config.
Examples
1) Define a group for these hosts
```php
$config['oxidized']['maps']['group']['sysname'][] = array('regex' => '/^(foo|bar)/', 'group' => 'myGroup');
$config['oxidized']['maps']['group']['sysname'][] = array('regex' => '/^(baz)/', 'group' => 'anotherGroup');
```
2) Provide a proxy host for these hosts to bounce through
```php
$config['oxidized']['maps']['ssh_proxy']['sysname'][] = array('regex' => '/foo/', 'ssh_proxy' => 'mySshProxyHost');
$config['oxidized']['maps']['ssh_proxy']['sysname'][] = array('regex' => '/bar/', 'ssh_proxy' => 'anotherSshProxyHost');
```
3) Allow overrides of IP addresses so the external DNS is not used for connections
```php
$config['oxidized']['maps']['ip']['sysname'][] = array('regex' => '/baz/', 'ip' => '10.10.0.23');
$config['oxidized']['maps']['ip']['sysname'][] = array('regex' => '/lala/', 'ip' => '192.168.0.243');
```
As already mentioned, this doesn't stop with the above examples.
DO NOT DELETE THIS TEXT
#### Please note
> Please read this information carefully. You can run `./scripts/pre-commit.php` to check your code before submitting.
- [x] Have you followed our [code guidelines?](http://docs.librenms.org/Developing/Code-Guidelines/)
#### Testers
If you would like to test this pull request then please run: `./scripts/github-apply <pr_id>`, i.e `./scripts/github-apply 5926`
2018-05-24 07:00:39 +10:00
} elseif ( isset ( $field [ 'match' ]) && $field [ 'match' ] == $device [ $field_type ]) {
$device [ $maps_column ] = $field [ $maps_column ];
2016-01-10 19:45:54 +00:00
break ;
}
}
}
}
api: Allow more complete overrides on Oxidized output (#8633)
This patch provides the ability to override most aspects of the configuration output provided to Oxidized. This is now backwards compatible with any current groups defined previously but allows for extra features such as group overrides in Oxidized as well as IP changes (for hosts that are treated differently inside Oxidized), as well as ProxyHost additions. Of course, this goes further and allows for any flag that can be defined within Oxidized to be mapped in the config.
Examples
1) Define a group for these hosts
```php
$config['oxidized']['maps']['group']['sysname'][] = array('regex' => '/^(foo|bar)/', 'group' => 'myGroup');
$config['oxidized']['maps']['group']['sysname'][] = array('regex' => '/^(baz)/', 'group' => 'anotherGroup');
```
2) Provide a proxy host for these hosts to bounce through
```php
$config['oxidized']['maps']['ssh_proxy']['sysname'][] = array('regex' => '/foo/', 'ssh_proxy' => 'mySshProxyHost');
$config['oxidized']['maps']['ssh_proxy']['sysname'][] = array('regex' => '/bar/', 'ssh_proxy' => 'anotherSshProxyHost');
```
3) Allow overrides of IP addresses so the external DNS is not used for connections
```php
$config['oxidized']['maps']['ip']['sysname'][] = array('regex' => '/baz/', 'ip' => '10.10.0.23');
$config['oxidized']['maps']['ip']['sysname'][] = array('regex' => '/lala/', 'ip' => '192.168.0.243');
```
As already mentioned, this doesn't stop with the above examples.
DO NOT DELETE THIS TEXT
#### Please note
> Please read this information carefully. You can run `./scripts/pre-commit.php` to check your code before submitting.
- [x] Have you followed our [code guidelines?](http://docs.librenms.org/Developing/Code-Guidelines/)
#### Testers
If you would like to test this pull request then please run: `./scripts/github-apply <pr_id>`, i.e `./scripts/github-apply 5926`
2018-05-24 07:00:39 +10:00
2018-05-25 04:08:44 +01:00
// We remap certain device OS' that have different names with Oxidized models
$models = [
'arista_eos' => 'eos' ,
'vyos' => 'vyatta' ,
2019-04-07 15:00:20 +01:00
'slms' => 'zhoneolt' ,
2018-05-25 04:08:44 +01:00
];
$device [ 'os' ] = str_replace ( array_keys ( $models ), array_values ( $models ), $device [ 'os' ]);
2016-01-10 19:45:54 +00:00
unset ( $device [ 'location' ]);
2017-10-15 13:37:20 -05:00
unset ( $device [ 'sysname' ]);
2018-07-09 21:25:26 +02:00
unset ( $device [ 'sysDescr' ]);
unset ( $device [ 'hardware' ]);
2015-07-13 20:10:26 +02:00
$devices [] = $device ;
}
$app -> response -> headers -> set ( 'Content-Type' , 'application/json' );
echo _json_encode ( $devices );
2015-05-07 00:32:58 +01:00
}
2015-07-02 23:19:46 +01:00
2016-08-18 20:28:22 -05:00
function list_bills ()
{
2015-07-02 23:19:46 +01:00
global $config ;
$app = \Slim\Slim :: getInstance ();
2015-08-03 15:02:35 +00:00
$router = $app -> router () -> getCurrentRoute () -> getParams ();
2017-11-29 09:42:52 +00:00
2015-07-02 23:19:46 +01:00
$bills = array ();
$bill_id = mres ( $router [ 'bill_id' ]);
$bill_ref = mres ( $_GET [ 'ref' ]);
$bill_custid = mres ( $_GET [ 'custid' ]);
2018-02-09 21:14:55 +00:00
$period = $_GET [ 'period' ];
2017-11-29 09:42:52 +00:00
$param = array ();
2018-01-21 20:56:57 +01:00
2015-07-02 23:19:46 +01:00
if ( ! empty ( $bill_custid )) {
2017-11-29 09:42:52 +00:00
$sql .= '`bill_custid` = ?' ;
$param [] = $bill_custid ;
2016-08-18 20:28:22 -05:00
} elseif ( ! empty ( $bill_ref )) {
2017-11-29 09:42:52 +00:00
$sql .= '`bill_ref` = ?' ;
$param [] = $bill_ref ;
2016-08-18 20:28:22 -05:00
} elseif ( is_numeric ( $bill_id )) {
2017-11-29 09:42:52 +00:00
$sql .= '`bill_id` = ?' ;
$param [] = $bill_id ;
2017-12-07 22:56:31 +00:00
} else {
$sql = '1' ;
2015-07-02 23:19:46 +01:00
}
2018-09-11 07:51:35 -05:00
if ( ! LegacyAuth :: user () -> hasGlobalRead ()) {
2017-11-29 09:42:52 +00:00
$sql .= ' AND `bill_id` IN (SELECT `bill_id` FROM `bill_perms` WHERE `user_id` = ?)' ;
2018-09-11 07:51:35 -05:00
$param [] = LegacyAuth :: id ();
2015-07-02 23:19:46 +01:00
}
2018-03-19 16:40:17 -05:00
2018-02-09 21:14:55 +00:00
if ( $period === 'previous' ) {
$select = " SELECT bills.bill_name, bills.bill_notes, bill_history.*, bill_history.traf_total as total_data, bill_history.traf_in as total_data_in, bill_history.traf_out as total_data_out " ;
$query = ' FROM `bills`
INNER JOIN ( SELECT bill_id , MAX ( bill_hist_id ) AS bill_hist_id FROM bill_history WHERE bill_dateto < NOW () AND bill_dateto > subdate ( NOW (), 40 ) GROUP BY bill_id ) qLastBills ON bills . bill_id = qLastBills . bill_id
INNER JOIN bill_history ON qLastBills . bill_hist_id = bill_history . bill_hist_id
' ;
} else {
$select = " SELECT bills.*,
IF ( bills . bill_type = 'CDR' , bill_cdr , bill_quota ) AS bill_allowed
" ;
$query = " FROM `bills` \n " ;
}
2015-07-02 23:19:46 +01:00
2018-02-09 21:14:55 +00:00
foreach ( dbFetchRows ( " $select $query WHERE $sql ORDER BY `bill_name` " , $param ) as $bill ) {
2015-07-02 23:19:46 +01:00
$rate_data = $bill ;
2015-08-04 06:54:58 +00:00
$allowed = '' ;
$used = '' ;
$percent = '' ;
$overuse = '' ;
2015-07-02 23:19:46 +01:00
if ( $bill [ 'bill_type' ] == " cdr " ) {
$allowed = format_si ( $bill [ 'bill_cdr' ]) . " bps " ;
$used = format_si ( $rate_data [ 'rate_95th' ]) . " bps " ;
2016-08-18 20:28:22 -05:00
$percent = round (( $rate_data [ 'rate_95th' ] / $bill [ 'bill_cdr' ]) * 100 , 2 );
2015-07-02 23:19:46 +01:00
$overuse = $rate_data [ 'rate_95th' ] - $bill [ 'bill_cdr' ];
$overuse = (( $overuse <= 0 ) ? " - " : format_si ( $overuse ));
2016-08-18 20:28:22 -05:00
} elseif ( $bill [ 'bill_type' ] == " quota " ) {
2015-07-02 23:19:46 +01:00
$allowed = format_bytes_billing ( $bill [ 'bill_quota' ]);
$used = format_bytes_billing ( $rate_data [ 'total_data' ]);
2016-08-18 20:28:22 -05:00
$percent = round (( $rate_data [ 'total_data' ] / ( $bill [ 'bill_quota' ])) * 100 , 2 );
2015-07-02 23:19:46 +01:00
$overuse = $rate_data [ 'total_data' ] - $bill [ 'bill_quota' ];
2015-08-04 06:54:58 +00:00
$overuse = (( $overuse <= 0 ) ? " - " : format_bytes_billing ( $overuse ));
2015-07-02 23:19:46 +01:00
}
$bill [ 'allowed' ] = $allowed ;
$bill [ 'used' ] = $used ;
$bill [ 'percent' ] = $percent ;
$bill [ 'overuse' ] = $overuse ;
2017-03-04 07:40:15 +11:00
$bill [ 'ports' ] = dbFetchRows ( " SELECT `D`.`device_id`,`P`.`port_id`,`P`.`ifName` FROM `bill_ports` AS `B`, `ports` AS `P`, `devices` AS `D` WHERE `B`.`bill_id` = ? AND `P`.`port_id` = `B`.`port_id` AND `D`.`device_id` = `P`.`device_id` " , array ( $bill [ " bill_id " ]));
2015-07-02 23:19:46 +01:00
$bills [] = $bill ;
}
2017-11-29 09:42:52 +00:00
api_success ( $bills , 'bills' );
2015-07-02 23:19:46 +01:00
}
2015-12-08 22:53:52 +00:00
2018-02-20 14:57:56 +00:00
function get_bill_graph ()
2018-02-09 21:14:55 +00:00
{
global $config ;
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
$bill_id = mres ( $router [ 'bill_id' ]);
2018-02-20 14:57:56 +00:00
$graph_type = $router [ 'graph_type' ];
2018-09-11 07:51:35 -05:00
if ( ! LegacyAuth :: user () -> hasGlobalRead ()) {
2018-02-20 14:57:56 +00:00
check_bill_permission ( $bill_id );
}
2018-03-19 16:40:17 -05:00
2018-02-20 14:57:56 +00:00
if ( $graph_type == 'monthly' ) {
$graph_type = 'historicmonthly' ;
}
$vars = array ();
$vars [ 'type' ] = " bill_ $graph_type " ;
$vars [ 'id' ] = $bill_id ;
$vars [ 'width' ] = $_GET [ 'width' ] ? : 1075 ;
$vars [ 'height' ] = $_GET [ 'height' ] ? : 300 ;
$app -> response -> headers -> set ( 'Content-Type' , 'image/png' );
2019-04-11 23:26:42 -05:00
include 'includes/html/graphs/graph.inc.php' ;
2018-02-20 14:57:56 +00:00
}
function get_bill_graphdata ()
{
global $config ;
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
$bill_id = mres ( $router [ 'bill_id' ]);
$graph_type = $router [ 'graph_type' ];
2018-09-11 07:51:35 -05:00
if ( ! LegacyAuth :: user () -> hasGlobalRead ()) {
2018-02-09 21:14:55 +00:00
check_bill_permission ( $bill_id );
}
2018-02-20 14:57:56 +00:00
if ( $graph_type == 'bits' ) {
$from = ( isset ( $_GET [ 'from' ]) ? $_GET [ 'from' ] : time () - 60 * 60 * 24 );
$to = ( isset ( $_GET [ 'to' ]) ? $_GET [ 'to' ] : time ());
$reducefactor = $_GET [ 'reducefactor' ];
$graph_data = getBillingBitsGraphData ( $bill_id , $from , $to , $reducefactor );
2018-03-19 16:40:17 -05:00
} elseif ( $graph_type == 'monthly' ) {
2018-02-20 14:57:56 +00:00
$graph_data = getHistoricTransferGraphData ( $bill_id );
}
2018-03-19 16:40:17 -05:00
2018-02-20 14:57:56 +00:00
if ( ! isset ( $graph_data )) {
api_error ( 400 , " Unsupported graph type $graph_type " );
} else {
api_success ( $graph_data , 'graph_data' );
}
}
function get_bill_history ()
{
global $config ;
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
$bill_id = mres ( $router [ 'bill_id' ]);
2018-09-11 07:51:35 -05:00
if ( ! LegacyAuth :: user () -> hasGlobalRead ()) {
2018-02-20 14:57:56 +00:00
check_bill_permission ( $bill_id );
}
2018-02-09 21:14:55 +00:00
$result = [];
foreach ( dbFetchRows ( 'SELECT * FROM `bill_history` WHERE `bill_id` = ? ORDER BY `bill_datefrom` DESC LIMIT 24' , array ( $bill_id )) as $history ) {
$result [] = $history ;
}
2018-02-20 14:57:56 +00:00
2018-02-09 21:14:55 +00:00
api_success ( $result , 'bill_history' );
}
2018-02-20 14:57:56 +00:00
function get_bill_history_graph ()
{
global $config ;
2018-03-19 16:40:17 -05:00
2018-02-20 14:57:56 +00:00
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
$bill_id = mres ( $router [ 'bill_id' ]);
$bill_hist_id = mres ( $router [ 'bill_hist_id' ]);
$graph_type = $router [ 'graph_type' ];
2018-09-11 07:51:35 -05:00
if ( ! LegacyAuth :: user () -> hasGlobalRead ()) {
2018-02-20 14:57:56 +00:00
check_bill_permission ( $bill_id );
}
2018-03-19 16:40:17 -05:00
2018-02-20 14:57:56 +00:00
$vars = array ();
switch ( $graph_type ) {
case 'bits' :
$graph_type = 'historicbits' ;
$vars [ 'reducefactor' ] = $_GET [ 'reducefactor' ];
break ;
2018-03-19 16:40:17 -05:00
2018-02-20 14:57:56 +00:00
case 'day' :
case 'hour' :
$vars [ 'imgtype' ] = $graph_type ;
$graph_type = 'historictransfer' ;
break ;
2018-03-19 16:40:17 -05:00
2018-02-20 14:57:56 +00:00
default :
api_error ( 400 , " Unknown Graph Type $graph_type " );
break ;
}
global $dur ; // Needed for callback within graph code
$vars [ 'type' ] = " bill_ $graph_type " ;
$vars [ 'id' ] = $bill_id ;
$vars [ 'bill_hist_id' ] = $bill_hist_id ;
$vars [ 'width' ] = $_GET [ 'width' ] ? : 1075 ;
$vars [ 'height' ] = $_GET [ 'height' ] ? : 300 ;
$app -> response -> headers -> set ( 'Content-Type' , 'image/png' );
2019-04-11 23:26:42 -05:00
include 'includes/html/graphs/graph.inc.php' ;
2018-02-20 14:57:56 +00:00
}
function get_bill_history_graphdata ()
{
global $config ;
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
$bill_id = mres ( $router [ 'bill_id' ]);
$bill_hist_id = mres ( $router [ 'bill_hist_id' ]);
$graph_type = $router [ 'graph_type' ];
2018-09-11 07:51:35 -05:00
if ( ! LegacyAuth :: user () -> hasGlobalRead ()) {
2018-02-20 14:57:56 +00:00
check_bill_permission ( $bill_id );
}
2018-03-19 16:40:17 -05:00
2018-02-20 14:57:56 +00:00
switch ( $graph_type ) {
case 'bits' :
$reducefactor = $_GET [ 'reducefactor' ];
$graph_data = getBillingHistoryBitsGraphData ( $bill_id , $bill_hist_id , $reducefactor );
break ;
case 'day' :
case 'hour' :
$graph_data = getBillingBandwidthGraphData ( $bill_id , $bill_hist_id , null , null , $graph_type );
break ;
}
2018-03-19 16:40:17 -05:00
2018-02-20 14:57:56 +00:00
if ( ! isset ( $graph_data )) {
api_error ( 400 , " Unsupported graph type $graph_type " );
} else {
api_success ( $graph_data , 'graph_data' );
}
}
2018-02-27 20:51:29 +01:00
function delete_bill ()
{
check_is_admin ();
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
$bill_id = ( int ) $router [ 'id' ];
if ( $bill_id < 1 ) {
api_error ( 400 , 'Could not remove bill with id ' . $bill_id . '. Invalid id' );
}
$res = dbDelete ( 'bills' , '`bill_id` = ? LIMIT 1' , [ $bill_id ]);
if ( $res == 1 ) {
dbDelete ( 'bill_ports' , '`bill_id` = ? ' , [ $bill_id ]);
dbDelete ( 'bill_data' , '`bill_id` = ? ' , [ $bill_id ]);
dbDelete ( 'bill_history' , '`bill_id` = ? ' , [ $bill_id ]);
dbDelete ( 'bill_history' , '`bill_id` = ? ' , [ $bill_id ]);
dbDelete ( 'bill_perms' , '`bill_id` = ? ' , [ $bill_id ]);
api_success_noresult ( 200 , 'Bill has been removed' );
}
api_error ( 400 , 'Could not remove bill with id ' . $bill_id );
}
function check_bill_key_value ( $bill_key , $bill_value )
{
$return_value = null ;
$bill_types = [ 'quota' , 'cdr' ];
switch ( $bill_key ) {
case " bill_type " :
if ( in_array ( $bill_value , $bill_types )) {
$return_value = mres ( $bill_value );
} else {
api_error ( 400 , " Invalid value for $bill_key : $bill_value . Allowed: quota,cdr " );
}
break ;
case " bill_cdr " :
if ( is_numeric ( $bill_value )) {
$return_value = mres ( $bill_value );
} else {
api_error ( 400 , " Invalid value for $bill_key . Must be numeric. " );
}
break ;
case " bill_day " :
if ( $bill_value > 0 && $bill_value <= 31 ) {
$return_value = mres ( $bill_value );
} else {
api_error ( 400 , " Invalid value for $bill_key . range: 1-31 " );
}
break ;
case " bill_quota " :
if ( is_numeric ( $bill_value )) {
$return_value = mres ( $bill_value );
} else {
api_error ( 400 , " Invalid value for $bill_key . Must be numeric " );
}
break ;
default :
$return_value = mres ( $bill_value );
break ;
}
return $return_value ;
}
function create_edit_bill ()
{
check_is_admin ();
$data = json_decode ( file_get_contents ( 'php://input' ), true );
if ( ! $data ) {
api_error ( 500 , 'Invalid JSON data' );
}
//check ports
$ports_add = null ;
if ( array_key_exists ( 'ports' , $data )) {
$ports_add = [];
$ports = $data [ 'ports' ];
foreach ( $ports as $port_id ) {
$result = dbFetchRows ( 'SELECT port_id FROM `ports` WHERE `port_id` = ? LIMIT 1' , [ $port_id ]);
$result = $result [ 0 ];
if ( ! is_array ( $result ) || ! array_key_exists ( 'port_id' , $result )) {
api_error ( 500 , 'Port ' . $port_id . ' does not exists' );
}
$ports_add [] = $port_id ;
}
}
$bill = [];
//find existing bill for update
$bill_id = ( int ) $data [ 'bill_id' ];
$bills = dbFetchRows ( " SELECT * FROM `bills` WHERE `bill_id` = $bill_id LIMIT 1 " );
// update existing bill
if ( is_array ( $bills ) && count ( $bills ) == 1 ) {
$bill = $bills [ 0 ];
foreach ( $data as $bill_key => $bill_value ) {
$bill [ $bill_key ] = check_bill_key_value ( $bill_key , $bill_value );
}
$update_data = [
'bill_name' => $bill [ 'bill_name' ],
'bill_type' => $bill [ 'bill_type' ],
'bill_cdr' => $bill [ 'bill_cdr' ],
'bill_day' => $bill [ 'bill_day' ],
'bill_quota' => $bill [ 'bill_quota' ],
'bill_custid' => $bill [ 'bill_custid' ],
'bill_ref' => $bill [ 'bill_ref' ],
'bill_notes' => $bill [ 'bill_notes' ]
];
$update = dbUpdate ( $update_data , 'bills' , 'bill_id=?' , array ( $bill_id ));
if ( $update === false || $update < 0 ) {
api_error ( 500 , 'Failed to update existing bill' );
}
} else {
// create new bill
if ( array_key_exists ( 'bill_id' , $data )) {
api_error ( 500 , 'Argument bill_id is not allowed on bill create (auto assigned)' );
}
$bill_keys = [
'bill_name' ,
'bill_type' ,
'bill_cdr' ,
'bill_day' ,
'bill_quota' ,
'bill_custid' ,
'bill_ref' ,
'bill_notes'
];
if ( $data [ 'bill_type' ] == 'quota' ) {
$data [ 'bill_cdr' ] = 0 ;
}
if ( $data [ 'bill_type' ] == 'cdr' ) {
$data [ 'bill_quota' ] = 0 ;
}
$missing_keys = '' ;
$missing = array_diff_key ( array_flip ( $bill_keys ), $data );
if ( count ( $missing ) > 0 ) {
foreach ( $missing as $missing_key => $dummy ) {
$missing_keys .= " $missing_key " ;
}
api_error ( 500 , 'Missing parameters: ' . $missing_keys );
}
foreach ( $bill_keys as $bill_key ) {
$bill [ $bill_key ] = check_bill_key_value ( $bill_key , $data [ $bill_key ]);
}
$bill_id = dbInsert (
[
'bill_name' => $bill [ 'bill_name' ],
'bill_type' => $bill [ 'bill_type' ],
'bill_cdr' => $bill [ 'bill_cdr' ],
'bill_day' => $bill [ 'bill_day' ],
'bill_quota' => $bill [ 'bill_quota' ],
'bill_custid' => $bill [ 'bill_custid' ],
'bill_ref' => $bill [ 'bill_ref' ],
'bill_notes' => $bill [ 'bill_notes' ]
],
'bills'
);
if ( $bill_id === null ) {
api_error ( 500 , 'Failed to create new bill' );
}
}
// set previously checked ports
if ( is_array ( $ports_add )) {
dbDelete ( 'bill_ports' , " `bill_id` = $bill_id " );
if ( count ( $ports_add ) > 0 ) {
foreach ( $ports_add as $port_id ) {
dbInsert ([ 'bill_id' => $bill_id , 'port_id' => $port_id , 'bill_port_autoadded' => 0 ], 'bill_ports' );
}
}
}
api_success ( $bill_id , 'bill_id' );
}
2016-08-18 20:28:22 -05:00
function update_device ()
{
2017-11-29 09:42:52 +00:00
check_is_admin ();
2015-12-08 22:53:52 +00:00
global $config ;
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
$hostname = $router [ 'hostname' ];
// use hostname as device_id if it's all digits
$device_id = ctype_digit ( $hostname ) ? $hostname : getidbyname ( $hostname );
$data = json_decode ( file_get_contents ( 'php://input' ), true );
2016-11-26 20:55:24 +00:00
$bad_fields = array ( 'device_id' , 'hostname' );
2015-12-08 22:53:52 +00:00
if ( empty ( $data [ 'field' ])) {
2017-11-29 09:42:52 +00:00
api_error ( 400 , 'Device field to patch has not been supplied' );
2016-08-18 20:28:22 -05:00
} elseif ( in_array ( $data [ 'field' ], $bad_fields )) {
2017-11-29 09:42:52 +00:00
api_error ( 500 , 'Device field is not allowed to be updated' );
}
if ( is_array ( $data [ 'field' ]) && is_array ( $data [ 'data' ])) {
foreach ( $data [ 'field' ] as $tmp_field ) {
if ( in_array ( $tmp_field , $bad_fields )) {
api_error ( 500 , 'Device field is not allowed to be updated' );
2016-11-26 20:55:24 +00:00
}
2017-11-29 09:42:52 +00:00
}
if ( count ( $data [ 'field' ]) == count ( $data [ 'data' ])) {
for ( $x = 0 ; $x < count ( $data [ 'field' ]); $x ++ ) {
$update [ mres ( $data [ 'field' ][ $x ])] = mres ( $data [ 'data' ][ $x ]);
}
if ( dbUpdate ( $update , 'devices' , '`device_id`=?' , array ( $device_id )) >= 0 ) {
api_success_noresult ( 200 , 'Device fields have been updated' );
} else {
api_error ( 500 , 'Device fields failed to be updated' );
2016-11-26 20:55:24 +00:00
}
2016-08-18 20:28:22 -05:00
} else {
2017-11-29 09:42:52 +00:00
api_error ( 500 , 'Device fields failed to be updated as the number of fields (' . count ( $data [ 'field' ]) . ') does not match the supplied data (' . count ( $data [ 'data' ]) . ')' );
2015-12-08 22:53:52 +00:00
}
2017-11-29 09:42:52 +00:00
} elseif ( dbUpdate ( array ( mres ( $data [ 'field' ]) => mres ( $data [ 'data' ])), 'devices' , '`device_id`=?' , array ( $device_id )) >= 0 ) {
api_success_noresult ( 200 , 'Device ' . mres ( $data [ 'field' ]) . ' field has been updated' );
} else {
api_error ( 500 , 'Device ' . mres ( $data [ 'field' ]) . ' field failed to be updated' );
2015-12-08 22:53:52 +00:00
}
}
2015-12-12 12:58:07 +00:00
2017-12-19 09:32:31 +00:00
function rename_device ()
{
check_is_admin ();
global $config ;
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
$hostname = $router [ 'hostname' ];
$device_id = ctype_digit ( $hostname ) ? $hostname : getidbyname ( $hostname );
$new_hostname = $router [ 'new_hostname' ];
$new_device = getidbyname ( $new_hostname );
if ( empty ( $new_hostname )) {
api_error ( 500 , 'Missing new hostname' );
} elseif ( $new_device ) {
api_error ( 500 , 'Device failed to rename, new hostname already exists' );
} else {
if ( renamehost ( $device_id , $new_hostname , 'api' ) == '' ) {
api_success_noresult ( 200 , 'Device has been renamed' );
} else {
2018-01-13 04:26:57 +00:00
api_error ( 500 , 'Device failed to be renamed' );
2017-12-19 09:32:31 +00:00
}
}
}
2016-08-18 20:28:22 -05:00
function get_device_groups ()
{
2015-12-12 12:58:07 +00:00
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
$status = 'error' ;
$code = 404 ;
$hostname = $router [ 'hostname' ];
// use hostname as device_id if it's all digits
$device_id = ctype_digit ( $hostname ) ? $hostname : getidbyname ( $hostname );
if ( is_numeric ( $device_id )) {
2016-08-18 20:28:22 -05:00
$groups = GetGroupsFromDevice ( $device_id , 1 );
} else {
2015-12-12 12:58:07 +00:00
$groups = GetDeviceGroups ();
}
if ( empty ( $groups )) {
2017-11-29 09:42:52 +00:00
api_error ( 404 , 'No device groups found' );
2015-12-12 12:58:07 +00:00
}
2017-11-29 09:42:52 +00:00
api_success ( $groups , 'groups' , 'Found ' . count ( $groups ) . ' device groups' );
2015-12-12 12:58:07 +00:00
}
2015-12-12 13:47:44 +00:00
2016-08-18 20:28:22 -05:00
function get_devices_by_group ()
{
2017-11-29 09:42:52 +00:00
check_is_read ();
2015-12-12 13:47:44 +00:00
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
2017-02-22 10:37:40 +01:00
$name = urldecode ( $router [ 'name' ]);
$devices = array ();
$full = $_GET [ 'full' ];
2015-12-12 13:47:44 +00:00
if ( empty ( $name )) {
2017-11-29 09:42:52 +00:00
api_error ( 400 , 'No device group name provided' );
}
$group_id = dbFetchCell ( " SELECT `id` FROM `device_groups` WHERE `name`=? " , array ( $name ));
$devices = GetDevicesFromGroup ( $group_id , true , $full );
if ( empty ( $devices )) {
api_error ( 404 , 'No devices found in group ' . $name );
2015-12-12 13:47:44 +00:00
}
2017-11-29 09:42:52 +00:00
api_success ( $devices , 'devices' );
2015-12-12 13:47:44 +00:00
}
2016-04-22 13:41:38 +00:00
2018-01-18 18:43:15 +03:00
function list_vrf ()
{
$app = \Slim\Slim :: getInstance ();
$sql = '' ;
$sql_params = array ();
$hostname = $_GET [ 'hostname' ];
$vrfname = $_GET [ 'vrfname' ];
$device_id = ctype_digit ( $hostname ) ? $hostname : getidbyname ( $hostname );
if ( is_numeric ( $device_id )) {
check_device_permission ( $device_id );
$sql = " AND `devices`.`device_id`=? " ;
$sql_params = array ( $device_id );
}
if ( ! empty ( $vrfname )) {
$sql = " AND `vrfs`.`vrf_name`=? " ;
$sql_params = array ( $vrfname );
}
2018-09-11 07:51:35 -05:00
if ( ! LegacyAuth :: user () -> hasGlobalRead ()) {
2018-01-18 18:43:15 +03:00
$sql .= " AND `vrfs`.`device_id` IN (SELECT device_id FROM devices_perms WHERE user_id = ?) " ;
2018-09-11 07:51:35 -05:00
$sql_params [] = LegacyAuth :: id ();
2018-01-18 18:43:15 +03:00
}
$vrfs = array ();
foreach ( dbFetchRows ( " SELECT `vrfs`.* FROM `vrfs` LEFT JOIN `devices` ON `vrfs`.`device_id` = `devices`.`device_id` WHERE `vrfs`.`vrf_name` IS NOT NULL $sql " , $sql_params ) as $vrf ) {
$host_id = get_vm_parent_id ( $device );
$device [ 'ip' ] = inet6_ntop ( $device [ 'ip' ]);
if ( is_numeric ( $host_id )) {
$device [ 'parent_id' ] = $host_id ;
}
$vrfs [] = $vrf ;
}
$total_vrfs = count ( $vrfs );
if ( $total_vrfs == 0 ) {
api_error ( 404 , 'VRFs do not exist' );
}
api_success ( $vrfs , 'vrfs' );
}
function get_vrf ()
{
check_is_read ();
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
$vrfId = $router [ 'id' ];
if ( ! is_numeric ( $vrfId )) {
api_error ( 400 , 'Invalid id has been provided' );
}
$vrf = dbFetchRows ( " SELECT * FROM `vrfs` WHERE `vrf_id` IS NOT NULL AND `vrf_id` = ? " , array ( $vrfId ));
$vrf_count = count ( $vrf );
if ( $vrf_count == 0 ) {
api_error ( 404 , " VRF $vrfId does not exist " );
}
api_success ( $vrf , 'vrf' );
}
2016-08-18 20:28:22 -05:00
function list_ipsec ()
{
2017-11-29 09:42:52 +00:00
check_is_read ();
2016-04-22 13:41:38 +00:00
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
$hostname = $router [ 'hostname' ];
// use hostname as device_id if it's all digits
$device_id = ctype_digit ( $hostname ) ? $hostname : getidbyname ( $hostname );
if ( ! is_numeric ( $device_id )) {
2017-11-29 09:42:52 +00:00
api_error ( 400 , " No valid hostname or device ID provided " );
2016-04-22 13:41:38 +00:00
}
2017-11-29 09:42:52 +00:00
$ipsec = dbFetchRows ( " SELECT `D`.`hostname`, `I`.* FROM `ipsec_tunnels` AS `I`, `devices` AS `D` WHERE `I`.`device_id`=? AND `D`.`device_id` = `I`.`device_id` " , array ( $device_id ));
api_success ( $ipsec , 'ipsec' );
2016-04-22 13:41:38 +00:00
}
2016-06-27 14:48:07 +01:00
2018-01-18 18:43:15 +03:00
function list_vlans ()
{
$app = \Slim\Slim :: getInstance ();
$sql = '' ;
$sql_params = array ();
$hostname = $_GET [ 'hostname' ] ? : '' ;
$device_id = ctype_digit ( $hostname ) ? $hostname : getidbyname ( $hostname );
if ( is_numeric ( $device_id )) {
check_device_permission ( $device_id );
$sql = " AND `devices`.`device_id` = ? " ;
$sql_params [] = $device_id ;
}
2018-09-11 07:51:35 -05:00
if ( ! LegacyAuth :: user () -> hasGlobalRead ()) {
2018-01-18 18:43:15 +03:00
$sql .= " AND `vlans`.`device_id` IN (SELECT device_id FROM devices_perms WHERE user_id = ?) " ;
2018-09-11 07:51:35 -05:00
$sql_params [] = LegacyAuth :: id ();
2018-01-18 18:43:15 +03:00
}
$vlans = array ();
foreach ( dbFetchRows ( " SELECT `vlans`.* FROM `vlans` LEFT JOIN `devices` ON `vlans`.`device_id` = `devices`.`device_id` WHERE `vlans`.`vlan_vlan` IS NOT NULL $sql " , $sql_params ) as $vlan ) {
$host_id = get_vm_parent_id ( $device );
$device [ 'ip' ] = inet6_ntop ( $device [ 'ip' ]);
if ( is_numeric ( $host_id )) {
$device [ 'parent_id' ] = $host_id ;
}
$vlans [] = $vlan ;
}
$vlans_count = count ( $vlans );
if ( $vlans_count == 0 ) {
api_error ( 404 , 'VLANs do not exist' );
}
api_success ( $vlans , 'vlans' );
}
2018-11-19 17:36:23 +03:00
function list_links ()
{
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
$sql = '' ;
$sql_params = array ();
$hostname = $router [ 'hostname' ];
$device_id = ctype_digit ( $hostname ) ? $hostname : getidbyname ( $hostname );
if ( is_numeric ( $device_id )) {
check_device_permission ( $device_id );
$sql = " AND `links`.`local_device_id`=? " ;
$sql_params = array ( $device_id );
}
if ( ! LegacyAuth :: user () -> hasGlobalRead ()) {
$sql .= " AND `links`.`local_device_id` IN (SELECT device_id FROM devices_perms WHERE user_id = ?) " ;
$sql_params [] = LegacyAuth :: id ();
}
$links = array ();
foreach ( dbFetchRows ( " SELECT `links`.* FROM `links` LEFT JOIN `devices` ON `links`.`local_device_id` = `devices`.`device_id` WHERE `links`.`id` IS NOT NULL $sql " , $sql_params ) as $link ) {
$host_id = get_vm_parent_id ( $device );
$device [ 'ip' ] = inet6_ntop ( $device [ 'ip' ]);
if ( is_numeric ( $host_id )) {
$device [ 'parent_id' ] = $host_id ;
}
$links [] = $link ;
}
$total_links = count ( $links );
if ( $total_links == 0 ) {
api_error ( 404 , 'Links do not exist' );
}
api_success ( $links , 'links' );
}
function get_link ()
{
check_is_read ();
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
$linkId = $router [ 'id' ];
if ( ! is_numeric ( $linkId )) {
api_error ( 400 , 'Invalid id has been provided' );
}
$link = dbFetchRows ( " SELECT * FROM `links` WHERE `id` IS NOT NULL AND `id` = ? " , array ( $linkId ));
$link_count = count ( $link );
if ( $link_count == 0 ) {
api_error ( 404 , " Link $linkId does not exist " );
}
api_success ( $link , 'link' );
}
2019-04-09 08:51:01 -04:00
function get_fdb ()
{
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
$hostname = $router [ 'hostname' ];
if ( empty ( $hostname )) {
api_error ( 500 , 'No hostname has been provided' );
}
$device_id = ctype_digit ( $hostname ) ? $hostname : getidbyname ( $hostname );
$device = null ;
if ( $device_id ) {
// save the current details for returning to the client on successful delete
$device = device_by_id_cache ( $device_id );
}
if ( ! $device ) {
api_error ( 404 , " Device $hostname not found " );
}
check_device_permission ( $device_id );
$fdb = \App\Models\PortsFdb :: find ( $device_id );
api_success ( $fdb , 'ports_fdb' );
}
function list_fdb ()
{
check_is_read ();
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
$mac = $router [ 'mac' ];
if ( empty ( $mac )) {
$fdb = \App\Models\PortsFdb :: hasAccess ( Auth :: user ()) -> get ();
} else {
$fdb = \App\Models\PortsFdb :: find ( $mac );
}
$total_fdb = $fdb -> count ();
if ( $total_fdb == 0 ) {
api_error ( 404 , 'Fdb do not exist' );
}
api_success ( $fdb , 'ports_fdb' );
}
2019-03-19 09:22:39 -04:00
function list_sensors ()
{
check_is_read ();
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
$sensors = \App\Models\Sensor :: hasAccess ( Auth :: user ()) -> get ();
$total_sensors = $sensors -> count ();
if ( $total_sensors == 0 ) {
api_error ( 404 , 'Sensors do not exist' );
}
api_success ( $sensors , 'sensors' );
}
2018-01-18 18:43:15 +03:00
function list_ip_addresses ()
{
check_is_read ();
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
$ipv4_addresses = array ();
$ipv6_addresses = array ();
$ipv4_addresses = dbFetchRows ( " SELECT * FROM `ipv4_addresses` " );
$ipv6_addresses = dbFetchRows ( " SELECT * FROM `ipv6_addresses` " );
$ip_addresses_count = count ( array_merge ( $ipv4_addresses , $ipv6_addresses ));
if ( $ip_addresses_count == 0 ) {
api_error ( 404 , 'IP addresses do not exist' );
}
api_success ( array_merge ( $ipv4_addresses , $ipv6_addresses ), 'ip_addresses' );
}
function list_ip_networks ()
{
check_is_read ();
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
$ipv4_networks = array ();
$ipv6_networks = array ();
$ipv4_networks = dbFetchRows ( " SELECT * FROM `ipv4_networks` " );
$ipv6_networks = dbFetchRows ( " SELECT * FROM `ipv6_networks` " );
$ip_networks_count = count ( array_merge ( $ipv4_networks , $ipv6_networks ));
if ( $ip_networks_count == 0 ) {
api_error ( 404 , 'IP networks do not exist' );
}
api_success ( array_merge ( $ipv4_networks , $ipv6_networks ), 'ip_networks' );
}
2016-08-18 20:28:22 -05:00
function list_arp ()
{
2017-11-29 09:42:52 +00:00
check_is_read ();
2016-06-27 14:48:07 +01:00
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
$ip = $router [ 'ip' ];
2019-04-10 16:33:25 -05:00
$hostname = $_GET [ 'device' ];
2017-03-13 02:17:09 +00:00
$total = 0 ;
2016-06-27 14:48:07 +01:00
if ( empty ( $ip )) {
2017-11-29 09:42:52 +00:00
api_error ( 400 , " No valid IP provided " );
2017-04-07 15:05:46 -05:00
} elseif ( $ip === " all " && empty ( $hostname )) {
2017-11-29 09:42:52 +00:00
api_error ( 400 , " Device argument is required when requesting all entries " );
}
if ( $ip === " all " ) {
$device_id = ctype_digit ( $hostname ) ? $hostname : getidbyname ( $hostname );
2019-04-10 16:33:25 -05:00
$arp = dbFetchRows ( " SELECT `ipv4_mac`.* FROM `ipv4_mac` LEFT JOIN `ports` ON `ipv4_mac`.`port_id` = `ports`.`port_id` WHERE `ports`.`device_id` = ? " , [ $device_id ]);
2017-11-29 09:42:52 +00:00
} elseif ( str_contains ( $ip , '/' )) {
2019-04-10 16:33:25 -05:00
try {
$ip = new IPv4 ( $ip );
$arp = dbFetchRows (
'SELECT * FROM `ipv4_mac` WHERE (inet_aton(`ipv4_address`) & ?) = ?' ,
[ ip2long ( $ip -> getNetmask ()), ip2long ( $ip -> getNetworkAddress ())]
);
} catch ( InvalidIpException $e ) {
api_error ( 400 , " Invalid Network Address " );
}
2016-08-18 20:28:22 -05:00
} else {
2019-04-10 16:33:25 -05:00
$arp = dbFetchRows ( " SELECT * FROM `ipv4_mac` WHERE `ipv4_address`=? " , [ $ip ]);
2016-06-27 14:48:07 +01:00
}
2017-11-29 09:42:52 +00:00
api_success ( $arp , 'arp' );
2016-06-27 14:48:07 +01:00
}
2017-06-28 07:00:46 +01:00
2016-08-26 09:19:05 +02:00
function list_services ()
{
2017-11-29 09:42:52 +00:00
check_is_read ();
2016-08-26 09:26:52 +02:00
global $config ;
2016-08-26 09:19:05 +02:00
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
2016-08-26 11:45:25 +02:00
$services = array ();
2017-06-28 07:00:46 +01:00
$where = array ();
$params = array ();
2017-02-07 02:46:09 -07:00
2017-06-28 07:00:46 +01:00
// Filter by State
2016-08-26 09:26:52 +02:00
if ( isset ( $_GET [ 'state' ])) {
2017-06-28 07:00:46 +01:00
$where [] = '`service_status`=?' ;
$params [] = $_GET [ 'state' ];
$where [] = " `service_disabled`='0' " ;
$where [] = " `service_ignore`='0' " ;
2017-02-07 02:46:09 -07:00
2016-08-26 09:26:52 +02:00
if ( ! is_numeric ( $_GET [ 'state' ])) {
2017-11-29 09:42:52 +00:00
api_error ( 400 , " No valid service state provided, valid option is 0=Ok, 1=Warning, 2=Critical " );
2016-08-26 09:26:52 +02:00
}
}
2017-02-07 02:46:09 -07:00
2017-06-28 07:00:46 +01:00
//Filter by Type
if ( isset ( $_GET [ 'type' ])) {
$where [] = '`service_type` LIKE ?' ;
$params [] = $_GET [ 'type' ];
}
2017-08-08 14:14:58 -05:00
2017-06-28 07:00:46 +01:00
//GET by Host
2016-08-26 10:11:20 +02:00
if ( isset ( $router [ 'hostname' ])) {
2016-08-26 09:26:52 +02:00
$hostname = $router [ 'hostname' ];
$device_id = ctype_digit ( $hostname ) ? $hostname : getidbyname ( $hostname );
2017-06-28 07:00:46 +01:00
$where [] = '`device_id` = ?' ;
$params [] = $device_id ;
2017-02-07 02:46:09 -07:00
2016-08-26 09:26:52 +02:00
if ( ! is_numeric ( $device_id )) {
2017-11-29 09:42:52 +00:00
api_error ( 500 , " No valid hostname or device id provided " );
2016-08-26 09:26:52 +02:00
}
}
2017-06-28 07:00:46 +01:00
$query = 'SELECT * FROM `services`' ;
2017-02-07 02:46:09 -07:00
2017-06-28 07:00:46 +01:00
if ( ! empty ( $where )) {
$query .= ' WHERE ' . implode ( ' AND ' , $where );
2016-08-26 09:26:52 +02:00
}
2017-06-28 07:00:46 +01:00
$query .= ' ORDER BY `service_ip`' ;
$services = array ( dbFetchRows ( $query , $params )); // double array for backwards compat :(
2017-11-29 09:42:52 +00:00
api_success ( $services , 'services' );
2016-08-26 09:19:05 +02:00
}
2017-07-27 19:56:38 +01:00
function list_logs ()
{
2017-11-29 09:42:52 +00:00
check_is_read ();
2019-01-10 00:52:00 -06:00
2017-07-27 19:56:38 +01:00
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
$type = $app -> router () -> getCurrentRoute () -> getName ();
$hostname = $router [ 'hostname' ];
$device_id = ctype_digit ( $hostname ) ? $hostname : getidbyname ( $hostname );
if ( $type === 'list_eventlog' ) {
$table = 'eventlog' ;
2019-01-10 00:52:00 -06:00
$select = '`eventlog`.`device_id` as `host`, `eventlog`.*' ; // inject host for backward compat
2017-07-27 19:56:38 +01:00
$timestamp = 'datetime' ;
} elseif ( $type === 'list_syslog' ) {
$table = 'syslog' ;
$timestamp = 'timestamp' ;
} elseif ( $type === 'list_alertlog' ) {
$table = 'alert_log' ;
$timestamp = 'time_logged' ;
} elseif ( $type === 'list_authlog' ) {
$table = 'authlog' ;
$timestamp = 'datetime' ;
} else {
$table = 'eventlog' ;
$timestamp = 'datetime' ;
}
2019-01-10 00:52:00 -06:00
$start = ( int ) $_GET [ 'start' ] ? : 0 ;
$limit = ( int ) $_GET [ 'limit' ] ? : 50 ;
$from = ( int ) $_GET [ 'from' ];
$to = ( int ) $_GET [ 'to' ];
2017-07-27 19:56:38 +01:00
$count_query = 'SELECT COUNT(*)' ;
2019-01-10 00:52:00 -06:00
$full_query = " SELECT `devices`.`hostname`, `devices`.`sysName`, " ;
$full_query .= isset ( $select ) ? $select : " ` $table `.* " ;
2017-07-27 19:56:38 +01:00
$param = array ();
$query = " FROM $table LEFT JOIN `devices` ON ` $table `.`device_id`=`devices`.`device_id` WHERE 1 " ;
if ( is_numeric ( $device_id )) {
$query .= " AND `devices`.`device_id` = ? " ;
$param [] = $device_id ;
}
if ( $from ) {
$query .= " AND $timestamp >= ? " ;
$param [] = $from ;
}
if ( $to ) {
$query .= " AND $timestamp <= ? " ;
$param [] = $to ;
}
$count_query = $count_query . $query ;
$count = dbFetchCell ( $count_query , $param );
$full_query = $full_query . $query . " ORDER BY $timestamp ASC LIMIT $start , $limit " ;
$logs = dbFetchRows ( $full_query , $param );
2019-02-07 09:42:16 -06:00
if ( $type === 'list_alertlog' ) {
foreach ( $logs as $index => $log ) {
$logs [ $index ][ 'details' ] = json_decode ( gzuncompress ( $log [ 'details' ]), true );
}
}
2017-11-29 09:42:52 +00:00
api_success ( $logs , 'logs' , null , 200 , null , array ( 'total' => $count ));
2017-07-27 19:56:38 +01:00
}
2017-11-14 15:15:12 +00:00
function validate_column_list ( $columns , $tableName )
{
global $config ;
$column_names = explode ( ',' , $columns );
$db_schema = Symfony\Component\Yaml\Yaml :: parse ( file_get_contents ( $config [ 'install_dir' ] . '/misc/db_schema.yaml' ));
$valid_columns = array_column ( $db_schema [ $tableName ][ 'Columns' ], 'Field' );
$invalid_columns = array_diff ( array_map ( 'trim' , $column_names ), $valid_columns );
if ( count ( $invalid_columns ) > 0 ) {
$output = array (
'status' => 'error' ,
'message' => 'Invalid columns: ' . join ( ',' , $invalid_columns ),
);
$app = \Slim\Slim :: getInstance ();
$app -> response -> setStatus ( 400 ); // Bad request
$app -> response -> headers -> set ( 'Content-Type' , 'application/json' );
echo _json_encode ( $output );
$app -> stop ();
}
}
2018-01-21 20:56:57 +01:00
function add_service_for_host ()
{
global $config ;
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
$hostname = $router [ 'hostname' ];
// use hostname as device_id if it's all digits
$device_id = ctype_digit ( $hostname ) ? $hostname : getidbyname ( $hostname );
check_device_permission ( $device_id );
$data = json_decode ( file_get_contents ( 'php://input' ), true );
$missing_fields = array ();
// Check if some required fields are empty
if ( empty ( $data [ 'type' ])) {
$missing_fields [] = 'type' ;
}
if ( empty ( $data [ 'ip' ])) {
$missing_fields [] = 'ip' ;
}
// Print error if required fields are missing
if ( ! empty ( $missing_fields )) {
api_error ( 400 , sprintf ( " Service field%s %s missing: %s. " , (( sizeof ( $missing_fields ) > 1 ) ? 's' : '' ), (( sizeof ( $missing_fields ) > 1 ) ? 'are' : 'is' ), implode ( ', ' , $missing_fields )));
}
if ( ! filter_var ( $data [ 'ip' ], FILTER_VALIDATE_IP )) {
api_error ( 400 , 'service_ip is not a valid IP address.' );
}
// Check if service type exists
if ( ! in_array ( $data [ 'type' ], list_available_services ())) {
api_error ( 400 , " The service " . $data [ 'type' ] . " does not exist. \n Available service types: " . implode ( ', ' , list_available_services ()));
}
// Get parameters
$service_type = $data [ 'type' ];
$service_ip = $data [ 'ip' ];
$service_desc = $data [ 'desc' ] ? mres ( $data [ 'desc' ]) : '' ;
$service_param = $data [ 'param' ] ? mres ( $data [ 'param' ]) : '' ;
$service_ignore = $data [ 'ignore' ] ? true : false ; // Default false
// Set the service
$service_id = add_service ( $device_id , $service_type , $service_desc , $service_ip , $service_param , ( int ) $service_ignore );
if ( $service_id != false ) {
api_success_noresult ( 201 , " Service $service_type has been added to device $hostname (# $service_id ) " );
} else {
api_error ( 500 , 'Failed to add the service' );
}
}
2018-05-19 00:36:06 -04:00
/**
* Display Librenms Instance Info
*/
function server_info ()
{
$versions = version_info ();
api_success ([
$versions
], 'system' );
}