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 .
*/
2016-08-18 20:28:22 -05:00
function authToken ( \Slim\Route $route )
{
2015-07-13 20:10:26 +02:00
$app = \Slim\Slim :: getInstance ();
$token = $app -> request -> headers -> get ( 'X-Auth-Token' );
if ( isset ( $token ) && ! empty ( $token )) {
2016-03-10 08:17:09 +01:00
if ( ! function_exists ( 'get_user' )) {
$username = dbFetchCell ( 'SELECT `U`.`username` FROM `api_tokens` AS AT JOIN `users` AS U ON `AT`.`user_id`=`U`.`user_id` WHERE `AT`.`token_hash`=?' , array ( $token ));
2016-08-18 20:28:22 -05:00
} else {
2016-03-10 08:17:09 +01:00
$username = get_user ( dbFetchCell ( 'SELECT `AT`.`user_id` FROM `api_tokens` AS AT WHERE `AT`.`token_hash`=?' , array ( $token )));
}
2015-07-13 20:10:26 +02:00
if ( ! empty ( $username )) {
$authenticated = true ;
2016-08-18 20:28:22 -05:00
} else {
2015-07-13 20:10:26 +02:00
$authenticated = false ;
}
2016-08-18 20:28:22 -05:00
} else {
2015-07-13 20:10:26 +02:00
$authenticated = false ;
}
if ( $authenticated === false ) {
$app -> response -> setStatus ( 401 );
$output = array (
'status' => 'error' ,
'message' => 'API Token is missing or invalid; please supply a valid token' ,
);
echo _json_encode ( $output );
$app -> stop ();
}
2014-07-07 00:15:04 +01:00
}
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' ];
$vars = array ();
$vars [ 'port' ] = urldecode ( $router [ 'ifname' ]);
$vars [ 'type' ] = $router [ 'type' ] ? : 'port_bits' ;
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' ;
2015-12-19 19:10:18 +00: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`.`hostname`=? AND `P`.` $port `=? " , array ( $hostname , $vars [ 'port' ]));
2015-07-13 20:10:26 +02:00
$app -> response -> headers -> set ( 'Content-Type' , 'image/png' );
include 'includes/graphs/graph.inc.php' ;
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
global $config ;
$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 ));
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 ]);
}
}
}
2015-07-13 20:10:26 +02:00
$output = array (
'status' => 'ok' ,
2016-10-15 00:42:54 +01:00
'port' => $port ,
2015-07-13 20:10:26 +02:00
);
$app -> response -> headers -> set ( 'Content-Type' , 'application/json' );
echo _json_encode ( $output );
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' ];
$vars = array ();
$vars [ 'type' ] = $router [ 'type' ] ? : 'device_uptime' ;
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 );
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 ));
$app -> response -> headers -> set ( 'Content-Type' , 'image/png' );
include 'includes/graphs/graph.inc.php' ;
2014-07-08 14:39:19 +01:00
}
2014-07-13 21:28:26 +01:00
2015-07-13 20:10:26 +02:00
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 ();
$app -> response -> headers -> set ( 'Content-Type' , 'application/json' );
$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 ) {
$app -> response -> setStatus ( 404 );
$output = array (
'status' => 'error' ,
'message' => " Device $hostname does not exist " ,
);
echo _json_encode ( $output );
$app -> stop ();
2016-08-18 20:28:22 -05:00
} else {
2015-07-13 20:10:26 +02:00
$output = array (
'status' => 'ok' ,
'devices' => array ( $device ),
);
echo _json_encode ( $output );
}
2014-09-27 15:25:43 +10:00
}
2015-07-13 20:10:26 +02: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
global $config ;
$app = \Slim\Slim :: getInstance ();
$order = $_GET [ 'order' ];
$type = $_GET [ 'type' ];
2015-08-23 11:57:56 +00:00
$query = mres ( $_GET [ 'query' ]);
$param = array ();
$join = '' ;
2015-07-13 20:10:26 +02:00
if ( empty ( $order )) {
$order = 'hostname' ;
}
if ( stristr ( $order , ' desc' ) === false && stristr ( $order , ' asc' ) === false ) {
2015-11-03 16:50:06 +00:00
$order = '`' . $order . '` ASC' ;
2015-07-13 20:10:26 +02:00
}
if ( $type == 'all' || empty ( $type )) {
$sql = '1' ;
2016-08-18 20:28:22 -05:00
} elseif ( $type == 'ignored' ) {
2015-11-03 16:50:06 +00:00
$sql = " `ignore`='1' AND `disabled`='0' " ;
2016-08-18 20:28:22 -05:00
} elseif ( $type == 'up' ) {
2015-11-03 16:50:06 +00:00
$sql = " `status`='1' AND `ignore`='0' AND `disabled`='0' " ;
2016-08-18 20:28:22 -05:00
} elseif ( $type == 'down' ) {
2015-11-03 16:50:06 +00:00
$sql = " `status`='0' AND `ignore`='0' AND `disabled`='0' " ;
2016-08-18 20:28:22 -05:00
} elseif ( $type == 'disabled' ) {
2015-11-03 16:50:06 +00:00
$sql = " `disabled`='1' " ;
2016-08-18 20:28:22 -05:00
} elseif ( $type == 'mac' ) {
2015-08-23 11:57:56 +00:00
$join = " LEFT JOIN `ports` ON `devices`.`device_id`=`ports`.`device_id` LEFT JOIN `ipv4_mac` ON `ports`.`port_id`=`ipv4_mac`.`port_id` " ;
$sql = " `ipv4_mac`.`mac_address`=? " ;
$param [] = $query ;
2016-08-18 20:28:22 -05:00
} elseif ( $type == 'ipv4' ) {
2015-08-23 11:57:56 +00:00
$join = " LEFT JOIN `ports` ON `devices`.`device_id`=`ports`.`device_id` LEFT JOIN `ipv4_addresses` ON `ports`.`port_id`=`ipv4_addresses`.`port_id` " ;
$sql = " `ipv4_addresses`.`ipv4_address`=? " ;
$param [] = $query ;
2016-08-18 20:28:22 -05:00
} elseif ( $type == 'ipv6' ) {
2015-08-23 11:57:56 +00:00
$join = " LEFT JOIN `ports` ON `devices`.`device_id`=`ports`.`device_id` LEFT JOIN `ipv6_addresses` ON `ports`.`port_id`=`ipv6_addresses`.`port_id` " ;
$sql = " `ipv6_addresses`.`ipv6_address`=? OR `ipv6_addresses`.`ipv6_compressed`=? " ;
$param = array ( $query , $query );
2016-08-18 20:28:22 -05:00
} else {
2015-07-13 20:10:26 +02:00
$sql = '1' ;
}
$devices = array ();
2015-08-23 11:57:56 +00:00
foreach ( dbFetchRows ( " SELECT * FROM `devices` $join WHERE $sql ORDER by $order " , $param ) as $device ) {
2016-01-20 18:19:15 +00:00
$device [ 'ip' ] = inet6_ntop ( $device [ 'ip' ]);
2015-07-13 20:10:26 +02:00
$devices [] = $device ;
}
2015-08-23 11:57:56 +00:00
$count = count ( $devices );
2015-07-13 20:10:26 +02:00
$output = array (
'status' => 'ok' ,
2015-08-23 11:57:56 +00:00
'count' => $count ,
2015-07-13 20:10:26 +02:00
'devices' => $devices ,
);
$app -> response -> headers -> set ( 'Content-Type' , 'application/json' );
echo _json_encode ( $output );
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 ()
{
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 ;
$app = \Slim\Slim :: getInstance ();
$data = json_decode ( file_get_contents ( 'php://input' ), true );
// Default status & code to error and change it if we need to.
$status = 'error' ;
$code = 500 ;
// keep scrutinizer from complaining about snmpver not being set for all execution paths
$snmpver = 'v2c' ;
if ( empty ( $data )) {
$message = 'No information has been provided to add this new device' ;
2016-08-18 20:28:22 -05:00
} elseif ( empty ( $data [ 'hostname' ])) {
2015-07-13 20:10:26 +02:00
$message = 'Missing the device hostname' ;
}
$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 ;
$force_add = $data [ 'force_add' ] ? mres ( $data [ 'force_add' ]) : 0 ;
if ( $data [ 'version' ] == 'v1' || $data [ 'version' ] == 'v2c' ) {
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' ]),
);
array_push ( $config [ 'snmp' ][ 'v3' ], $v3 );
$snmpver = 'v3' ;
2016-08-18 20:28:22 -05:00
} else {
2015-07-13 20:10:26 +02:00
$code = 400 ;
$status = 'error' ;
$message = " You haven't specified an SNMP version to use " ;
}
if ( empty ( $message )) {
2016-08-07 12:16:40 -05:00
try {
$device_id = addHost ( $hostname , $snmpver , $port , $transport , $poller_group , $force_add );
2015-07-13 20:10:26 +02:00
$code = 201 ;
$status = 'ok' ;
2016-08-07 12:16:40 -05:00
$message = " Device $hostname ( $device_id ) has been added successfully " ;
} catch ( Exception $e ) {
$message = $e -> getMessage ();
2015-07-13 20:10:26 +02:00
}
}
$app -> response -> setStatus ( $code );
$output = array (
'status' => $status ,
'message' => $message ,
2014-07-16 01:02:55 +01:00
);
2015-07-13 20:10:26 +02:00
$app -> response -> headers -> set ( 'Content-Type' , 'application/json' );
echo _json_encode ( $output );
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 ()
{
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' ];
// Default status to error and change it if we need to.
$status = 'error' ;
$code = 500 ;
if ( empty ( $hostname ) || $config [ 'api_demo' ] == 1 ) {
$message = 'No hostname has been provided to delete' ;
if ( $config [ 'api_demo' ] == 1 ) {
$message = " This feature isn \ 't available in the demo " ;
}
$output = array (
'status' => $status ,
'message' => $message ,
);
2016-08-18 20:28:22 -05:00
} else {
2015-07-13 20:10:26 +02: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 );
}
if ( $device ) {
$response = delete_device ( $device_id );
if ( empty ( $response )) {
// FIXME: Need to provide better diagnostics out of delete_device
$output = array (
'status' => $status ,
'message' => 'Device deletion failed' ,
);
2016-08-18 20:28:22 -05:00
} else {
2015-07-13 20:10:26 +02:00
// deletion succeeded - include old device details in response
$code = 200 ;
$status = 'ok' ;
$output = array (
'status' => $status ,
'message' => $response ,
'devices' => array ( $device ),
);
}
2016-08-18 20:28:22 -05:00
} else {
2015-07-13 20:10:26 +02:00
// no device matching the name
$code = 404 ;
$output = array (
'status' => $status ,
'message' => " Device $hostname not found " ,
);
}
2014-09-17 21:03:02 +01:00
}
2014-09-27 16:32:55 +10:00
2015-07-13 20:10:26 +02:00
$app -> response -> setStatus ( $code );
$app -> response -> headers -> set ( 'Content-Type' , 'application/json' );
echo _json_encode ( $output );
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
global $config ;
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' ];
2015-07-13 20:10:26 +02:00
$code = 500 ;
if ( empty ( $hostname )) {
$output = $output = array (
'status' => 'error' ,
'message' => 'No hostname has been provided' ,
);
2016-08-18 20:28:22 -05:00
} else {
2015-07-13 20:10:26 +02:00
include_once '../includes/functions.php' ;
2014-10-01 20:53:15 +01:00
$device_id = ctype_digit ( $hostname ) ? $hostname : getidbyname ( $hostname );
2015-07-13 20:10:26 +02:00
$device = null ;
2014-10-01 20:53:15 +01:00
if ( $device_id ) {
// save the current details for returning to the client on successful delete
$device = device_by_id_cache ( $device_id );
}
2015-07-13 20:10:26 +02:00
2014-10-01 20:53:15 +01:00
if ( $device ) {
2015-07-13 20:10:26 +02:00
$vlans = dbFetchRows ( 'SELECT vlan_vlan,vlan_domain,vlan_name,vlan_type,vlan_mtu FROM vlans WHERE `device_id` = ?' , array ( $device_id ));
2014-10-01 20:53:15 +01:00
$total_vlans = count ( $vlans );
2015-07-13 20:10:26 +02:00
$code = 200 ;
$output = array (
'status' => 'ok' ,
'count' => $total_vlans ,
'vlans' => $vlans ,
);
2016-08-18 20:28:22 -05:00
} else {
2015-07-13 20:10:26 +02:00
$code = 404 ;
$output = array (
'status' => 'error' , " Device $hostname not found "
);
2014-10-01 20:53:15 +01:00
}
2014-10-01 20:49:34 +01:00
}
2015-07-13 20:10:26 +02:00
2014-10-01 21:12:21 +01:00
$app -> response -> setStatus ( $code );
$app -> response -> headers -> set ( 'Content-Type' , 'application/json' );
2014-10-01 20:53:15 +01:00
echo _json_encode ( $output );
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 ()
{
2014-10-26 15:48:51 +00:00
global $config ;
2015-07-13 20:10:26 +02:00
$app = \Slim\Slim :: getInstance ();
$code = 500 ;
$status = 'error' ;
$message = 'Error retrieving bgpPeers' ;
$sql = '' ;
2014-10-29 21:30:35 +00:00
$sql_params = array ();
2015-07-13 20:10:26 +02:00
$hostname = $_GET [ 'hostname' ];
$device_id = ctype_digit ( $hostname ) ? $hostname : getidbyname ( $hostname );
if ( is_numeric ( $device_id )) {
$sql = ' AND `device_id`=?' ;
2014-10-26 15:48:51 +00:00
$sql_params = array ( $device_id );
}
2015-07-13 20:10:26 +02:00
$bgp_sessions = dbFetchRows ( " SELECT * FROM bgpPeers WHERE `bgpPeerState` IS NOT NULL AND `bgpPeerState` != '' $sql " , $sql_params );
2014-10-26 15:48:51 +00:00
$total_bgp_sessions = count ( $bgp_sessions );
2015-07-13 20:10:26 +02:00
if ( is_numeric ( $total_bgp_sessions )) {
$code = 200 ;
$status = 'ok' ;
2014-10-29 21:30:35 +00:00
$message = '' ;
}
2015-07-13 20:10:26 +02:00
$output = array (
'status' => " $status " ,
'err-msg' => $message ,
'count' => $total_bgp_sessions ,
'bgp_sessions' => $bgp_sessions ,
);
2014-10-26 15:48:51 +00:00
$app -> response -> setStatus ( $code );
$app -> response -> headers -> set ( 'Content-Type' , 'application/json' );
echo _json_encode ( $output );
}
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 ()
{
2015-07-13 20:10:26 +02:00
global $config ;
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
$group = $router [ 'group' ];
$vars = array ();
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' ;
2016-06-24 14:56:45 -07:00
$ports = get_ports_from_type ( explode ( ',' , $group ));
2015-07-13 20:10:26 +02:00
$if_list = '' ;
$seperator = '' ;
foreach ( $ports as $port ) {
$if_list .= $seperator . $port [ 'port_id' ];
$seperator = ',' ;
}
unset ( $seperator );
$vars [ 'type' ] = 'multiport_bits_separate' ;
$vars [ 'id' ] = $if_list ;
$app -> response -> headers -> set ( 'Content-Type' , 'image/png' );
include 'includes/graphs/graph.inc.php' ;
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
global $config ;
$code = 200 ;
$status = 'ok' ;
$message = '' ;
$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 );
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
$output = array (
'status' => " $status " ,
'err-msg' => $message ,
'count' => count ( $components [ $device_id ]),
'components' => $components [ $device_id ],
);
$app -> response -> setStatus ( $code );
$app -> response -> headers -> set ( 'Content-Type' , 'application/json' );
echo _json_encode ( $output );
}
2016-08-18 20:28:22 -05:00
function add_components ()
{
2016-03-30 16:39:39 +10:00
global $config ;
$code = 200 ;
$status = 'ok' ;
$message = '' ;
$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
$output = array (
'status' => " $status " ,
'err-msg' => $message ,
'count' => count ( $component ),
'components' => $component ,
);
$app -> response -> setStatus ( $code );
$app -> response -> headers -> set ( 'Content-Type' , 'application/json' );
echo _json_encode ( $output );
}
2016-08-18 20:28:22 -05:00
function edit_components ()
{
2016-03-30 16:39:39 +10:00
global $config ;
$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
2016-08-18 20:28:22 -05:00
if ( $COMPONENT -> setComponentPrefs ( $device_id , $data )) {
2016-03-30 16:39:39 +10:00
// Edit Success.
$code = 200 ;
$status = 'ok' ;
$message = '' ;
2016-08-18 20:28:22 -05:00
} else {
2016-03-30 16:39:39 +10:00
// Edit Failure.
$code = 500 ;
$status = 'error' ;
$message = 'Components could not be edited.' ;
}
$output = array (
'status' => " $status " ,
'err-msg' => $message ,
'count' => count ( $data ),
);
$app -> response -> setStatus ( $code );
$app -> response -> headers -> set ( 'Content-Type' , 'application/json' );
echo _json_encode ( $output );
}
2016-08-18 20:28:22 -05:00
function delete_components ()
{
2016-03-30 16:39:39 +10:00
global $config ;
$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 )) {
// Edit Success.
$code = 200 ;
$status = 'ok' ;
$message = '' ;
2016-08-18 20:28:22 -05:00
} else {
2016-03-30 16:39:39 +10:00
// Edit Failure.
$code = 500 ;
$status = 'error' ;
$message = 'Components could not be deleted.' ;
}
$output = array (
'status' => " $status " ,
'err-msg' => $message ,
);
$app -> response -> setStatus ( $code );
$app -> response -> headers -> set ( 'Content-Type' , 'application/json' );
echo _json_encode ( $output );
}
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
$code = 200 ;
$status = 'ok' ;
$message = '' ;
$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 );
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
2014-11-29 15:43:19 +00:00
$total_graphs = count ( $graphs );
2015-07-13 20:10:26 +02:00
$output = array (
'status' => " $status " ,
'err-msg' => $message ,
'count' => $total_graphs ,
'graphs' => $graphs ,
);
2014-11-29 15:43:19 +00:00
$app -> response -> setStatus ( $code );
$app -> response -> headers -> set ( 'Content-Type' , 'application/json' );
echo _json_encode ( $output );
}
2014-12-01 20:27:50 +00:00
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function get_port_graphs ()
{
2014-11-30 15:45:10 +00:00
global $config ;
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' ;
}
// 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 );
$ports = dbFetchRows ( " SELECT $columns FROM `ports` WHERE `device_id` = ? AND `deleted` = '0' ORDER BY `ifIndex` ASC " , array ( $device_id ));
2014-11-30 15:45:10 +00:00
$total_ports = count ( $ports );
2015-07-13 20:10:26 +02:00
$output = array (
'status' => 'ok' ,
'err-msg' => '' ,
'count' => $total_ports ,
'ports' => $ports ,
);
2014-11-30 15:45:10 +00:00
$app -> response -> setStatus ( '200' );
$app -> response -> headers -> set ( 'Content-Type' , 'application/json' );
echo _json_encode ( $output );
}
2014-12-02 23:43:11 +00:00
2016-08-18 20:28:22 -05:00
function get_port_stack ()
{
2016-04-05 13:03:17 -07: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 );
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 ));
}
2016-04-05 13:03:17 -07:00
$total_mappings = count ( $mappings );
$output = array (
'status' => 'ok' ,
'err-msg' => '' ,
'count' => $total_mappings ,
'mappings' => $mappings ,
);
$app -> response -> setStatus ( '200' );
$app -> response -> headers -> set ( 'Content-Type' , 'application/json' );
echo _json_encode ( $output );
}
2016-08-18 20:28:22 -05:00
function list_alert_rules ()
{
2014-12-16 20:39:58 +00:00
global $config ;
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 );
2014-12-16 20:39:58 +00:00
$total_rules = count ( $rules );
2015-07-13 20:10:26 +02:00
$output = array (
'status' => 'ok' ,
'err-msg' => '' ,
'count' => $total_rules ,
'rules' => $rules ,
);
2014-12-16 20:39:58 +00:00
$app -> response -> setStatus ( '200' );
$app -> response -> headers -> set ( 'Content-Type' , 'application/json' );
echo _json_encode ( $output );
}
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function list_alerts ()
{
2014-12-16 20:39:58 +00:00
global $config ;
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-10-08 15:15:44 +02:00
if ( isset ( $_GET [ 'state' ])) {
$param = array ( mres ( $_GET [ 'state' ]));
2016-08-18 20:28:22 -05:00
} else {
2014-12-16 20:39:58 +00:00
$param = array ( '1' );
}
2015-07-13 20:10:26 +02:00
2014-12-16 20:58:31 +00:00
$sql = '' ;
2015-07-13 20:10:26 +02:00
if ( isset ( $router [ 'id' ]) && $router [ 'id' ] > 0 ) {
2014-12-16 20:39:58 +00:00
$alert_id = mres ( $router [ 'id' ]);
2015-07-13 20:10:26 +02:00
$sql = 'AND id=?' ;
array_push ( $param , $alert_id );
2014-12-16 20:39:58 +00:00
}
2015-07-13 20:10:26 +02:00
2016-09-14 15:29:46 +02:00
$alerts = dbFetchRows ( " 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 (?) $sql " , $param );
2014-12-16 20:39:58 +00:00
$total_alerts = count ( $alerts );
2015-07-13 20:10:26 +02:00
$output = array (
'status' => 'ok' ,
'err-msg' => '' ,
'count' => $total_alerts ,
'alerts' => $alerts ,
);
2014-12-16 20:39:58 +00:00
$app -> response -> setStatus ( '200' );
$app -> response -> headers -> set ( 'Content-Type' , 'application/json' );
echo _json_encode ( $output );
}
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function add_edit_rule ()
{
2014-12-16 20:39:58 +00:00
global $config ;
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 );
2015-07-13 20:10:26 +02:00
$status = 'error' ;
2014-12-16 20:39:58 +00:00
$message = '' ;
2015-07-13 20:10:26 +02:00
$code = 500 ;
2014-12-16 20:39:58 +00:00
$rule_id = mres ( $data [ 'rule_id' ]);
$device_id = mres ( $data [ 'device_id' ]);
2015-07-13 20:10:26 +02:00
if ( empty ( $device_id ) && ! isset ( $rule_id )) {
2014-12-16 20:39:58 +00:00
$message = 'Missing the device id or global device id (-1)' ;
2016-08-18 20:28:22 -05:00
} elseif ( $device_id == 0 ) {
2014-12-16 20:39:58 +00:00
$device_id = '-1' ;
}
$rule = $data [ 'rule' ];
2015-07-13 20:10:26 +02:00
if ( empty ( $rule )) {
2014-12-16 20:39:58 +00:00
$message = 'Missing the alert rule' ;
}
2015-07-13 20:10:26 +02:00
2015-07-01 21:35:37 +01:00
$name = mres ( $data [ 'name' ]);
if ( empty ( $name )) {
$message = 'Missing the alert rule name' ;
}
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 )) {
2014-12-16 20:39:58 +00:00
$message = 'Missing the severity' ;
}
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' ]);
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 ;
}
2015-07-13 20:10:26 +02:00
$extra = array (
'mute' => $mute ,
'count' => $count ,
'delay' => $delay_sec ,
);
2014-12-16 20:39:58 +00:00
$extra_json = json_encode ( $extra );
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 ) {
$message = 'Addition failed : Name has already been used' ;
}
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 ) {
2015-10-16 16:18:18 +02:00
$message = 'Edition failed : Name has already been used' ;
}
2015-07-01 21:35:37 +01:00
}
2015-07-13 20:10:26 +02:00
if ( empty ( $message )) {
if ( is_numeric ( $rule_id )) {
if ( dbUpdate ( array ( 'name' => $name , 'rule' => $rule , 'severity' => $severity , 'disabled' => $disabled , 'extra' => $extra_json ), 'alert_rules' , 'id=?' , array ( $rule_id )) >= 0 ) {
2014-12-16 20:39:58 +00:00
$status = 'ok' ;
2015-07-13 20:10:26 +02:00
$code = 200 ;
2016-08-18 20:28:22 -05:00
} else {
2014-12-16 20:39:58 +00:00
$message = 'Failed to update existing alert rule' ;
}
2016-08-18 20:28:22 -05:00
} elseif ( dbInsert ( array ( 'name' => $name , 'device_id' => $device_id , 'rule' => $rule , 'severity' => $severity , 'disabled' => $disabled , 'extra' => $extra_json ), 'alert_rules' )) {
2014-12-16 20:39:58 +00:00
$status = 'ok' ;
2015-07-13 20:10:26 +02:00
$code = 200 ;
2016-08-18 20:28:22 -05:00
} else {
2014-12-16 20:39:58 +00:00
$message = 'Failed to create new alert rule' ;
}
}
2015-07-13 20:10:26 +02:00
$output = array (
'status' => $status ,
'err-msg' => $message ,
);
2014-12-16 20:39:58 +00:00
$app -> response -> setStatus ( $code );
$app -> response -> headers -> set ( 'Content-Type' , 'application/json' );
echo _json_encode ( $output );
}
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function delete_rule ()
{
2014-12-16 20:39:58 +00:00
global $config ;
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
$status = 'error' ;
2014-12-16 20:39:58 +00:00
$err_msg = '' ;
2014-12-16 20:58:31 +00:00
$message = '' ;
2015-07-13 20:10:26 +02:00
$code = 500 ;
if ( is_numeric ( $rule_id )) {
2014-12-16 20:39:58 +00:00
$status = 'ok' ;
2015-07-13 20:10:26 +02:00
$code = 200 ;
if ( dbDelete ( 'alert_rules' , '`id` = ? LIMIT 1' , array ( $rule_id ))) {
2014-12-16 20:39:58 +00:00
$message = 'Alert rule has been removed' ;
2016-08-18 20:28:22 -05:00
} else {
2014-12-16 20:39:58 +00:00
$message = 'No alert rule by that ID' ;
}
2016-08-18 20:28:22 -05:00
} else {
2014-12-16 20:39:58 +00:00
$err_msg = 'Invalid rule id has been provided' ;
}
2015-07-13 20:10:26 +02:00
$output = array (
'status' => $status ,
'err-msg' => $err_msg ,
'message' => $message ,
);
2014-12-16 20:39:58 +00:00
$app -> response -> setStatus ( $code );
$app -> response -> headers -> set ( 'Content-Type' , 'application/json' );
echo _json_encode ( $output );
}
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function ack_alert ()
{
2014-12-16 20:39:58 +00:00
global $config ;
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' ]);
2015-07-13 20:10:26 +02:00
$status = 'error' ;
$err_msg = '' ;
$message = '' ;
$code = 500 ;
if ( is_numeric ( $alert_id )) {
2014-12-16 20:39:58 +00:00
$status = 'ok' ;
2015-07-13 20:10:26 +02:00
$code = 200 ;
if ( dbUpdate ( array ( 'state' => 2 ), 'alerts' , '`id` = ? LIMIT 1' , array ( $alert_id ))) {
2016-01-07 23:02:18 +01:00
$message = 'Alert has been acknowledged' ;
2016-08-18 20:28:22 -05:00
} else {
2014-12-16 20:39:58 +00:00
$message = 'No alert by that ID' ;
}
2016-08-18 20:28:22 -05:00
} else {
2014-12-16 20:39:58 +00:00
$err_msg = 'Invalid alert has been provided' ;
}
2015-07-13 20:10:26 +02:00
$output = array (
'status' => $status ,
'err-msg' => $err_msg ,
'message' => $message ,
);
2014-12-16 20:39:58 +00:00
$app -> response -> setStatus ( $code );
$app -> response -> headers -> set ( 'Content-Type' , 'application/json' );
echo _json_encode ( $output );
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 ()
{
2015-10-08 16:45:36 +02:00
global $config ;
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
$alert_id = mres ( $router [ 'id' ]);
$status = 'error' ;
$err_msg = '' ;
$message = '' ;
$code = 500 ;
if ( is_numeric ( $alert_id )) {
$status = 'ok' ;
$code = 200 ;
if ( dbUpdate ( array ( 'state' => 1 ), 'alerts' , '`id` = ? LIMIT 1' , array ( $alert_id ))) {
$message = 'Alert has been unmuted' ;
2016-08-18 20:28:22 -05:00
} else {
2015-10-08 16:45:36 +02:00
$message = 'No alert by that ID' ;
}
2016-08-18 20:28:22 -05:00
} else {
2015-10-08 16:45:36 +02:00
$err_msg = 'Invalid alert has been provided' ;
}
$output = array (
'status' => $status ,
'err-msg' => $err_msg ,
'message' => $message ,
);
$app -> response -> setStatus ( $code );
$app -> response -> headers -> set ( 'Content-Type' , 'application/json' );
echo _json_encode ( $output );
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 ();
$status = 'error' ;
$err_msg = '' ;
$code = 500 ;
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 );
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 )) {
2015-07-13 20:10:26 +02:00
$err_msg = 'Invalid device provided' ;
2015-03-23 15:30:09 +00:00
$total_inv = 0 ;
$inventory = array ();
2016-08-18 20:28:22 -05:00
} else {
2015-07-13 20:10:26 +02:00
$inventory = dbFetchRows ( " SELECT * FROM `entPhysical` WHERE 1 $sql " , $params );
$code = 200 ;
$status = 'ok' ;
2015-03-23 15:15:05 +00:00
$total_inv = count ( $inventory );
}
2015-07-13 20:10:26 +02:00
$output = array (
'status' => $status ,
'err-msg' => $err_msg ,
'count' => $total_inv ,
'inventory' => $inventory ,
);
2015-03-23 15:15:05 +00:00
$app -> response -> setStatus ( $code );
$app -> response -> headers -> set ( 'Content-Type' , 'application/json' );
echo _json_encode ( $output );
}
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 ()
{
2015-12-02 20:22:38 +00:00
global $config ;
2015-07-13 20:10:26 +02:00
$app = \Slim\Slim :: getInstance ();
$app -> response -> headers -> set ( 'Content-Type' , 'application/json' );
$devices = array ();
2015-12-02 20:22:38 +00:00
$device_types = " ' " . implode ( " ',' " , $config [ 'oxidized' ][ 'ignore_types' ]) . " ' " ;
$device_os = " ' " . implode ( " ',' " , $config [ 'oxidized' ][ 'ignore_os' ]) . " ' " ;
2016-01-10 19:45:54 +00:00
foreach ( dbFetchRows ( " SELECT hostname,os,location FROM `devices` 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 )) " ) as $device ) {
if ( $config [ 'oxidized' ][ 'group_support' ] == " true " ) {
foreach ( $config [ 'oxidized' ][ 'group' ][ 'hostname' ] as $host_group ) {
if ( preg_match ( $host_group [ 'regex' ] . 'i' , $device [ 'hostname' ])) {
$device [ 'group' ] = $host_group [ 'group' ];
break ;
}
}
2016-03-05 00:48:51 +00:00
if ( empty ( $device [ 'group' ])) {
foreach ( $config [ 'oxidized' ][ 'group' ][ 'os' ] as $host_group ) {
if ( $host_group [ 'match' ] === $device [ 'os' ]) {
$device [ 'group' ] = $host_group [ 'group' ];
break ;
}
}
}
2016-01-10 19:45:54 +00:00
if ( empty ( $device [ 'group' ])) {
foreach ( $config [ 'oxidized' ][ 'group' ][ 'location' ] as $host_group ) {
if ( preg_match ( $host_group [ 'regex' ] . 'i' , $device [ 'location' ])) {
$device [ 'group' ] = $host_group [ 'group' ];
break ;
}
}
}
if ( empty ( $device [ 'group' ]) && ! empty ( $config [ 'oxidized' ][ 'default_group' ])) {
$device [ 'group' ] = $config [ 'oxidized' ][ 'default_group' ];
}
}
unset ( $device [ 'location' ]);
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 ();
2015-07-02 23:19:46 +01:00
$status = 'ok' ;
$err_msg = '' ;
$message = '' ;
$code = 200 ;
$bills = array ();
$bill_id = mres ( $router [ 'bill_id' ]);
$bill_ref = mres ( $_GET [ 'ref' ]);
$bill_custid = mres ( $_GET [ 'custid' ]);
if ( ! empty ( $bill_custid )) {
$sql = '`bill_custid` = ?' ;
$param = array ( $bill_custid );
2016-08-18 20:28:22 -05:00
} elseif ( ! empty ( $bill_ref )) {
2015-07-02 23:19:46 +01:00
$sql = '`bill_ref` = ?' ;
$param = array ( $bill_ref );
2016-08-18 20:28:22 -05:00
} elseif ( is_numeric ( $bill_id )) {
2015-10-26 19:55:23 +00:00
$sql = '`bills`.`bill_id` = ?' ;
2015-07-02 23:19:46 +01:00
$param = array ( $bill_id );
2016-08-18 20:28:22 -05:00
} else {
2015-07-02 23:19:46 +01:00
$sql = '' ;
$param = array ();
}
if ( count ( $param ) >= 1 ) {
$sql = " WHERE $sql " ;
}
2016-08-18 20:28:22 -05:00
foreach ( dbFetchRows ( " SELECT `bills`.*,COUNT(port_id) AS `ports_total` FROM `bills` LEFT JOIN `bill_ports` ON `bill_ports`.`bill_id`=`bills`.`bill_id` $sql GROUP BY `bill_name`,`bill_ref` 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 ;
$bills [] = $bill ;
}
$count = count ( $bills );
2015-08-04 06:54:58 +00:00
$output = array (
'status' => $status ,
'message' => $message ,
'err-msg' => $err_msg ,
'count' => $count ,
'bills' => $bills
);
$app -> response -> setStatus ( $code );
2015-07-02 23:19:46 +01:00
$app -> response -> headers -> set ( 'Content-Type' , 'application/json' );
echo _json_encode ( $output );
}
2015-12-08 22:53:52 +00:00
2016-08-18 20:28:22 -05:00
function update_device ()
{
2015-12-08 22:53:52 +00:00
global $config ;
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
$status = 'error' ;
$code = 500 ;
$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' ])) {
$message = 'Device field to patch has not been supplied' ;
2016-08-18 20:28:22 -05:00
} elseif ( in_array ( $data [ 'field' ], $bad_fields )) {
2015-12-08 22:53:52 +00:00
$message = 'Device field is not allowed to be updated' ;
2016-08-18 20:28:22 -05:00
} else {
2016-11-26 20:55:24 +00:00
if ( is_array ( $data [ 'field' ]) && is_array ( $data [ 'data' ])) {
foreach ( $data [ 'field' ] as $tmp_field ) {
if ( in_array ( $tmp_field , $bad_fields )) {
$message = 'Device field is not allowed to be updated' ;
}
}
if ( $message == '' && 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 ) {
$status = 'ok' ;
$code = 200 ;
$message = 'Device fields have been updated' ;
} else {
$message = 'Device fields failed to be updated' ;
}
} elseif ( $message == '' ) {
$message = 'Device fields failed to be updated as the number of fields (' . count ( $data [ 'field' ]) . ') does not match the supplied data (' . count ( $data [ 'data' ]) . ')' ;
}
} elseif ( dbUpdate ( array ( mres ( $data [ 'field' ]) => mres ( $data [ 'data' ])), 'devices' , '`device_id`=?' , array ( $device_id )) >= 0 ) {
2015-12-08 22:53:52 +00:00
$status = 'ok' ;
$message = 'Device ' . mres ( $data [ 'field' ]) . ' field has been updated' ;
$code = 200 ;
2016-08-18 20:28:22 -05:00
} else {
2015-12-08 22:53:52 +00:00
$message = 'Device ' . mres ( $data [ 'field' ]) . ' field failed to be updated' ;
}
}
$output = array (
'status' => $status ,
'message' => $message ,
);
$app -> response -> setStatus ( $code );
$app -> response -> headers -> set ( 'Content-Type' , 'application/json' );
echo _json_encode ( $output );
}
2015-12-12 12:58:07 +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 )) {
$message = 'No device groups found' ;
2016-08-18 20:28:22 -05:00
} else {
2015-12-12 12:58:07 +00:00
$status = 'ok' ;
$code = 200 ;
$message = 'Found ' . count ( $groups ) . ' device groups' ;
}
$output = array (
'status' => $status ,
'message' => $message ,
'count' => count ( $groups ),
'groups' => $groups ,
);
$app -> response -> setStatus ( $code );
$app -> response -> headers -> set ( 'Content-Type' , 'application/json' );
echo _json_encode ( $output );
}
2015-12-12 13:47:44 +00:00
2016-08-18 20:28:22 -05:00
function get_devices_by_group ()
{
2015-12-12 13:47:44 +00:00
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
$status = 'error' ;
$code = 404 ;
2015-12-12 14:21:49 +00:00
$count = 0 ;
2015-12-12 13:47:44 +00:00
$name = urldecode ( $router [ 'name' ]);
$devices = array ();
if ( empty ( $name )) {
$message = 'No device group name provided' ;
2016-08-18 20:28:22 -05:00
} else {
$group_id = dbFetchCell ( " SELECT `id` FROM `device_groups` WHERE `name`=? " , array ( $name ));
2016-06-02 12:37:03 -05:00
$devices = GetDevicesFromGroup ( $group_id , true );
2015-12-12 13:47:44 +00:00
$count = count ( $devices );
if ( empty ( $devices )) {
$message = 'No devices found in group ' . $name ;
2016-08-18 20:28:22 -05:00
} else {
2015-12-12 13:47:44 +00:00
$message = " Found $count in group $name " ;
2016-08-27 15:46:54 +02:00
$status = 'ok' ;
2015-12-12 13:47:44 +00:00
$code = 200 ;
}
}
$output = array (
'status' => $status ,
'message' => $message ,
'count' => $count ,
'devices' => $devices ,
);
$app -> response -> setStatus ( $code );
$app -> response -> headers -> set ( 'Content-Type' , 'application/json' );
echo _json_encode ( $output );
}
2016-04-22 13:41:38 +00:00
2016-08-18 20:28:22 -05:00
function list_ipsec ()
{
2016-04-22 13:41:38 +00:00
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
$status = 'error' ;
$code = 404 ;
$message = '' ;
$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 )) {
$message = " No valid hostname or device ID provided " ;
2016-08-18 20:28:22 -05:00
} else {
2016-04-22 14:42:33 +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 ));
2016-04-22 13:41:38 +00:00
$total = count ( $ipsec );
$status = 'ok' ;
$code = 200 ;
}
$output = array (
'status' => $status ,
'err-msg' => $message ,
'count' => $total ,
'ipsec' => $ipsec ,
);
$app -> response -> setStatus ( $code );
$app -> response -> headers -> set ( 'Content-Type' , 'application/json' );
echo _json_encode ( $output );
}
2016-06-27 14:48:07 +01:00
2016-08-18 20:28:22 -05:00
function list_arp ()
{
2016-06-27 14:48:07 +01:00
$app = \Slim\Slim :: getInstance ();
$router = $app -> router () -> getCurrentRoute () -> getParams ();
$status = 'error' ;
$code = 404 ;
$message = '' ;
$ip = $router [ 'ip' ];
if ( empty ( $ip )) {
$message = " No valid IP provided " ;
2016-08-18 20:28:22 -05:00
} else {
2016-06-27 14:48:07 +01:00
$code = 200 ;
$status = 'ok' ;
if ( $ip === " all " ) {
$hostname = mres ( $_GET [ 'device' ]);
$device_id = ctype_digit ( $hostname ) ? $hostname : getidbyname ( $hostname );
$arp = dbFetchRows ( " SELECT `ipv4_mac`.* FROM `ipv4_mac` LEFT JOIN `ports` ON `ipv4_mac`.`port_id` = `ports`.`port_id` WHERE `ports`.`device_id` = ? " , array ( $device_id ));
2016-08-18 20:28:22 -05:00
} else {
2016-06-27 14:48:07 +01:00
$arp = dbFetchRows ( " SELECT * FROM `ipv4_mac` WHERE `ipv4_address`=? " , array ( $ip ));
}
$total = count ( $arp );
}
$output = array (
'status' => $status ,
'err-msg' => $message ,
'count' => $total ,
'arp' => $arp ,
);
$app -> response -> setStatus ( $code );
$app -> response -> headers -> set ( 'Content-Type' , 'application/json' );
echo _json_encode ( $output );
}
2016-08-26 09:19:05 +02:00
function list_services ()
{
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 09:26:52 +02:00
$status = 'ok' ;
2016-08-26 09:19:05 +02:00
$code = 200 ;
$message = '' ;
2016-08-26 11:45:25 +02:00
$host_par = array ();
$sql_param = array ();
$services = array ();
$where = '' ;
$devicewhere = '' ;
2016-08-26 09:26:52 +02:00
// Filter BY STATE
if ( isset ( $_GET [ 'state' ])) {
$where = " AND S.service_status= ? AND S.service_disabled='0' AND S.service_ignore='0' " ;
$host_par [] = $_GET [ 'state' ];
if ( ! is_numeric ( $_GET [ 'state' ])) {
$status = 'error' ;
$message = " No valid service state provided, valid option is 0=Ok, 1=Warning, 2=Critical " ;
}
}
// 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 );
$where .= " AND S.device_id = ? " ;
$host_par [] = $device_id ;
if ( ! is_numeric ( $device_id )) {
$status = 'error' ;
$message = " No valid hostname or device id provided " ;
}
}
// DEVICE
$host_sql = 'SELECT * FROM devices AS D, services AS S WHERE D.device_id = S.device_id ' . $where . ' GROUP BY D.hostname ORDER BY D.hostname' ;
// SERVICE
foreach ( dbFetchRows ( $host_sql , $host_par ) as $device ) {
2016-08-26 10:11:20 +02:00
$device_id = $device [ 'device_id' ];
$sql_param [ 0 ] = $device_id ;
2016-08-26 09:26:52 +02:00
// FILTER BY TYPE
if ( isset ( $_GET [ 'type' ])) {
$devicewhere = " AND `service_type` LIKE ? " ;
$sql_param [ 1 ] = $_GET [ 'type' ];
}
2016-10-25 12:10:42 +02:00
$services [] = dbFetchRows ( " SELECT * FROM `services` WHERE `device_id` = ? " . $devicewhere , $sql_param );
2016-08-26 09:26:52 +02:00
}
2016-08-26 09:19:05 +02:00
$count = count ( $services );
2016-08-26 09:26:52 +02:00
$output = array (
2016-08-26 09:19:05 +02:00
'status' => $status ,
2016-08-26 09:26:52 +02:00
'err-msg' => $message ,
2016-08-26 09:19:05 +02:00
'count' => $count ,
'services' => $services ,
);
2016-08-26 09:26:52 +02:00
$app -> response -> setStatus ( $code );
2016-08-26 09:19:05 +02:00
$app -> response -> headers -> set ( 'Content-Type' , 'application/json' );
echo _json_encode ( $output );
}