2009-10-02 13:20:24 +00:00
< ? php
2012-05-25 10:34:01 +00:00
/**
2013-10-28 11:32:21 -07:00
* LibreNMS
2012-05-25 10:34:01 +00:00
*
2013-10-28 11:32:21 -07:00
* This file is part of LibreNMS
2012-05-25 10:34:01 +00:00
*
2013-10-28 11:32:21 -07:00
* @ author LibreNMS Contributors < librenms - project @ google . groups . com >
* @ copyright ( C ) 2006 - 2012 Adam Armstrong ( as Observium )
* @ copyright ( C ) 2013 LibreNMS Group
2012-05-25 10:34:01 +00:00
*/
2018-12-12 03:32:56 +01:00
use LibreNMS\Config ;
2022-11-03 15:07:17 -05:00
use LibreNMS\Enum\ImageFormat ;
2021-03-28 17:25:30 -05:00
use LibreNMS\Util\Number ;
use LibreNMS\Util\Rewrite ;
2017-11-18 11:33:03 +01:00
2015-09-01 10:14:15 +02:00
/**
* Compare $t with the value of $vars [ $v ], if that exists
2021-09-10 20:09:53 +02:00
*
2021-09-08 23:35:56 +02:00
* @ param string $v Name of the var to test
* @ param string $t Value to compare $vars [ $v ] to
2020-02-10 12:07:27 +01:00
* @ return bool true , if values are the same , false if $vars [ $v ]
2021-09-08 23:35:56 +02:00
* is unset or values differ
2015-09-01 10:14:15 +02:00
*/
2016-08-18 20:28:22 -05:00
function var_eq ( $v , $t )
{
2015-08-27 21:06:07 +02:00
global $vars ;
if ( isset ( $vars [ $v ]) && $vars [ $v ] == $t ) {
return true ;
}
return false ;
}
2015-09-01 10:14:15 +02:00
/**
* Get the value of $vars [ $v ], if it exists
2021-09-10 20:09:53 +02:00
*
2021-09-08 23:35:56 +02:00
* @ param string $v Name of the var to get
2015-09-01 10:14:15 +02:00
* @ return string | bool The value of $vars [ $v ] if it exists , false if it does not exist
*/
2016-08-18 20:28:22 -05:00
function var_get ( $v )
{
2015-08-27 21:06:07 +02:00
global $vars ;
if ( isset ( $vars [ $v ])) {
return $vars [ $v ];
}
return false ;
}
2016-08-18 20:28:22 -05:00
function toner2colour ( $descr , $percent )
{
2021-12-06 09:12:24 -06:00
$colour = \LibreNMS\Util\Color :: percentage ( 100 - $percent , null );
2012-05-25 10:34:01 +00:00
2012-04-14 23:28:23 +00:00
if ( substr ( $descr , - 1 ) == 'C' || stripos ( $descr , 'cyan' ) !== false ) {
2017-12-02 22:28:03 +02:00
$colour [ 'left' ] = '55D6D3' ;
2012-04-14 23:28:23 +00:00
$colour [ 'right' ] = '33B4B1' ;
2015-07-13 20:10:26 +02:00
}
2012-04-14 23:28:23 +00:00
if ( substr ( $descr , - 1 ) == 'M' || stripos ( $descr , 'magenta' ) !== false ) {
2017-12-02 22:28:03 +02:00
$colour [ 'left' ] = 'F24AC8' ;
2012-04-14 23:28:23 +00:00
$colour [ 'right' ] = 'D028A6' ;
2015-07-13 20:10:26 +02:00
}
2012-04-09 23:00:31 +00:00
if ( substr ( $descr , - 1 ) == 'Y' || stripos ( $descr , 'yellow' ) !== false
|| stripos ( $descr , 'giallo' ) !== false
|| stripos ( $descr , 'gul' ) !== false
2015-07-13 20:10:26 +02:00
) {
2017-12-02 22:28:03 +02:00
$colour [ 'left' ] = 'FFF200' ;
2012-04-14 23:28:23 +00:00
$colour [ 'right' ] = 'DDD000' ;
2015-07-13 20:10:26 +02:00
}
2012-04-09 23:00:31 +00:00
if ( substr ( $descr , - 1 ) == 'K' || stripos ( $descr , 'black' ) !== false
2012-04-14 23:28:23 +00:00
|| stripos ( $descr , 'nero' ) !== false
2012-04-09 23:00:31 +00:00
) {
2017-12-02 22:28:03 +02:00
$colour [ 'left' ] = '000000' ;
2012-04-14 23:28:23 +00:00
$colour [ 'right' ] = '222222' ;
2015-07-13 20:10:26 +02:00
}
2012-04-09 23:00:31 +00:00
2012-04-14 23:28:23 +00:00
return $colour ;
2012-04-17 15:27:17 +00:00
} //end toner2colour()
2012-04-09 23:00:31 +00:00
2016-08-18 20:28:22 -05:00
function generate_link ( $text , $vars , $new_vars = [])
{
2021-03-28 17:25:30 -05:00
return '<a href="' . \LibreNMS\Util\Url :: generate ( $vars , $new_vars ) . '">' . $text . '</a>' ;
2011-09-14 13:38:01 +00:00
} //end generate_link()
2016-08-18 20:28:22 -05:00
function escape_quotes ( $text )
{
2013-11-26 11:03:00 +10:00
return str_replace ( '"' , " \ ' " , str_replace ( " ' " , " \ ' " , $text ));
2011-09-14 13:38:01 +00:00
} //end escape_quotes()
2016-08-18 20:28:22 -05:00
function generate_overlib_content ( $graph_array , $text )
{
2017-12-02 22:28:03 +02:00
$overlib_content = '<div class=overlib><span class=overlib-text>' . $text . '</span><br />' ;
2011-09-17 19:14:44 +00:00
foreach ([ 'day' , 'week' , 'month' , 'year' ] as $period ) {
2019-06-23 00:29:12 -05:00
$graph_array [ 'from' ] = Config :: get ( " time. $period " );
2021-03-28 17:25:30 -05:00
$overlib_content .= escape_quotes ( \LibreNMS\Util\Url :: graphTag ( $graph_array ));
2011-09-17 19:14:44 +00:00
}
2015-07-13 20:10:26 +02:00
2013-11-19 08:57:43 +10:00
$overlib_content .= '</div>' ;
2011-09-17 19:14:44 +00:00
return $overlib_content ;
} //end generate_overlib_content()
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function generate_device_link ( $device , $text = null , $vars = [], $start = 0 , $end = 0 , $escape_text = 1 , $overlib = 1 )
{
2021-03-28 17:25:30 -05:00
$deviceModel = DeviceCache :: get (( int ) $device [ 'device_id' ]);
2015-07-13 20:10:26 +02:00
2021-03-28 17:25:30 -05:00
return \LibreNMS\Util\Url :: deviceLink ( $deviceModel , $text , $vars , $start , $end , $escape_text , $overlib );
2020-04-29 07:25:13 -05:00
}
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function bill_permitted ( $bill_id )
{
2019-07-29 16:32:37 -05:00
if ( Auth :: user () -> hasGlobalRead ()) {
2019-03-19 08:14:01 -05:00
return true ;
2010-07-31 21:08:35 +00:00
}
2015-07-13 20:10:26 +02:00
2019-07-29 16:32:37 -05:00
return \Permissions :: canAccessBill ( $bill_id , Auth :: id ());
2019-03-19 08:14:01 -05:00
}
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function port_permitted ( $port_id , $device_id = null )
{
2012-05-16 13:25:50 +00:00
if ( ! is_numeric ( $device_id )) {
$device_id = get_device_id_by_port_id ( $port_id );
}
2015-07-13 20:10:26 +02:00
2019-03-19 08:14:01 -05:00
if ( device_permitted ( $device_id )) {
return true ;
2010-04-24 22:43:25 +00:00
}
2015-07-13 20:10:26 +02:00
2019-07-29 16:32:37 -05:00
return \Permissions :: canAccessPort ( $port_id , Auth :: id ());
2019-03-19 08:14:01 -05:00
}
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function device_permitted ( $device_id )
{
2019-08-04 06:32:46 +02:00
if ( Auth :: user () && Auth :: user () -> hasGlobalRead ()) {
2019-03-19 08:14:01 -05:00
return true ;
2010-04-24 22:43:25 +00:00
}
2020-09-21 15:40:17 +02:00
2019-07-29 16:32:37 -05:00
return \Permissions :: canAccessDevice ( $device_id , Auth :: id ());
2019-03-19 08:14:01 -05:00
}
2015-07-13 20:10:26 +02:00
2020-03-01 15:50:45 +01:00
function alert_layout ( $severity )
{
switch ( $severity ) {
case 'critical' :
$icon = 'exclamation' ;
$color = 'danger' ;
$background = 'danger' ;
break ;
case 'warning' :
$icon = 'warning' ;
$color = 'warning' ;
$background = 'warning' ;
break ;
case 'ok' :
$icon = 'check' ;
$color = 'success' ;
$background = 'success' ;
break ;
default :
$icon = 'info' ;
$color = 'info' ;
$background = 'info' ;
}
2020-09-21 15:40:17 +02:00
2020-03-01 15:50:45 +01:00
return [ 'icon' => $icon ,
'icon_color' => $color ,
'background_color' => $background , ];
}
2015-07-13 20:10:26 +02:00
2018-08-29 15:25:16 +02:00
function generate_dynamic_graph_tag ( $args )
{
$urlargs = [];
$width = 0 ;
foreach ( $args as $key => $arg ) {
switch ( strtolower ( $key )) {
case 'width' :
$width = $arg ;
$value = '{{width}}' ;
break ;
case 'from' :
$value = '{{start}}' ;
break ;
case 'to' :
$value = '{{end}}' ;
break ;
default :
$value = $arg ;
break ;
}
$urlargs [] = $key . '=' . $value ;
}
return '<img style="width:' . $width . 'px;height:100%" class="graph img-responsive" data-src-template="graph.php?' . implode ( '&' , $urlargs ) . '" border="0" />' ;
} //end generate_dynamic_graph_tag()
function generate_dynamic_graph_js ( $args )
{
$from = ( is_numeric ( $args [ 'from' ]) ? $args [ 'from' ] : '(new Date()).getTime() / 1000 - 24*3600' );
$range = ( is_numeric ( $args [ 'to' ]) ? $args [ 'to' ] - $args [ 'from' ] : '24*3600' );
$output = ' < script src = " js/RrdGraphJS/q-5.0.2.min.js " ></ script >
< script src = " js/RrdGraphJS/moment-timezone-with-data.js " ></ script >
< script src = " js/RrdGraphJS/rrdGraphPng.js " ></ script >
< script type = " text/javascript " >
q . ready ( function (){
var graphs = [];
q ( \ ' . graph\ ' ) . forEach ( function ( item ){
graphs . push (
q ( item ) . rrdGraphPng ({
canvasPadding : 120 ,
initialStart : ' . $from . ' ,
initialRange : ' . $range . '
})
);
});
});
// needed for dynamic height
window . onload = function (){ window . dispatchEvent ( new Event ( \ ' resize\ ' )); }
</ script > ' ;
2020-09-21 15:40:17 +02:00
2018-08-29 15:25:16 +02:00
return $output ;
} //end generate_dynamic_graph_js()
2010-03-11 21:58:50 +00:00
2016-08-18 20:28:22 -05:00
function generate_graph_js_state ( $args )
{
2011-06-14 14:04:56 +00:00
// we are going to assume we know roughly what the graph url looks like here.
// TODO: Add sensible defaults
2017-12-02 22:28:03 +02:00
$from = ( is_numeric ( $args [ 'from' ]) ? $args [ 'from' ] : 0 );
$to = ( is_numeric ( $args [ 'to' ]) ? $args [ 'to' ] : 0 );
$width = ( is_numeric ( $args [ 'width' ]) ? $args [ 'width' ] : 0 );
2011-06-14 14:04:56 +00:00
$height = ( is_numeric ( $args [ 'height' ]) ? $args [ 'height' ] : 0 );
2022-08-30 12:55:37 -05:00
$legend = str_replace ( " ' " , '' , $args [ 'legend' ] ? ? '' );
2015-07-13 20:10:26 +02:00
2011-06-14 14:04:56 +00:00
$state = <<< STATE
< script type = " text/javascript " language = " JavaScript " >
document . graphFrom = $from ;
document . graphTo = $to ;
document . graphWidth = $width ;
document . graphHeight = $height ;
document . graphLegend = '$legend' ;
</ script >
STATE ;
return $state ;
} //end generate_graph_js_state()
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function print_percentage_bar ( $width , $height , $percent , $left_text , $left_colour , $left_background , $right_text , $right_colour , $right_background )
{
2021-01-02 00:47:41 +01:00
return \LibreNMS\Util\Html :: percentageBar ( $width , $height , $percent , $left_text , $right_text , null , null , [
Modernize mempools (#12282)
* mempools to modern module
quick hacky hrstorage port
* port ucd-snmp-mib to Mempools
* Populate DB for ucd
Prep for yaml
* initial yaml attempt
* more complex conversions
fixes to YamlDiscovery, make leading $ optional and allow mib::oid format
* walk full tables and skip values
normalize percentages above 100
* handle precent only ones (specify total as 100)
* Move default polling out of YamlMempoolsDiscovery
* fixes
* Update test data hrstorage should be correct.
* perc_warn for hrstorage
* Host Resources, record buffer, cached, and shared
* Host Resources is always better, don't do both HR and UCD
* fix unix, include warning levels
* variable size
* consolidate skip_values
* define mempools schema
* number instead of integer
* more schema refactor
* one more skip_values reference
* throw error for invalid oid translation
aos6
* a* and Cisco
* updated test data
* update almost all hrstorage data files
* b*
* c* with test data
use standard cache for hrStorage
* use cache for storage module too
* hand bsnmp properly
* bdcom
* exclude total oid from yaml so it is not polled
May add a way to ignore this behavior and poll it, but I don't know if that is needed.
* automatically handle percent only values
* ciscowlc
* only poll used or free if we have used, free, and total.
* fix skipping
* the dlinkoning
fix find value when value "name" is numeric
* support numeric oids
* dnos/ftos attempt
* I guess we can't filter on total > 0
* edgecos
* e*
* f WIP
* f*
* gwd (aka g*)
* h* + procurve
* i*
* j*
* m*
* support 0% used memory (however unlikely)
* n*
* CISCO-PROCESS-MIB memory, share cache with processors module
* ignore mempools with invalid total
* p*
* quanta
* r*
fix raisecom mibs terribly broken
* s-z
* style fixes
* Move VRP back to PHP and make it actually work
* fix zynos
* update schema
* Update Cisco processor data for description bug fixes
* fix comware processors
* comware mempools with memory size
default precision to 1
* sophos-xg updated data
* hrstorage use ram size for buffers, cache, and shared
* Show memory available instead of free in device overview
* UCD, use same rrd format, store available instead of free in the db.
* Calculate availability for HOST-RESOURCES-MIB
* Convert UCD to standard polling
* rename old rrd files
* initial graph work
* graph WIP
* Graph looking decent
* Graph looking decent for hr
* remove old ucd_graph code
* handle availability mempool
more graph cleanup
* color adjustments
* remove accidental free calculation
* Update test data and fix corner cases
* fis pfsense
* update schema
* add default value for mempool_class
* fix whitespace
* update schema
* update schema correctly
* one more time
* fortigate_1500d-sensors missing oids
* Update docs.
* fix indent
* add implements MempoolsDiscovery explicitly to OS
* remove ucd_memory graph references
remove unused device_memory graph
* remove unused functions
* set devices with mempools to rediscover to prevent/minimize gaps
* use a subquery
* add overview graph
* port health mempools table
* Update device mempool
* only show overview if multiple
* Don't override user set warn percentages in discovery
* fix missed usage
* fix style
* Safety check to not rename rrd files incorrectly if migration has not been run.
* Fix overview percent bar and represent available and used on the bar
* missed an item to convert to mempool_class
* percent on the wrong side
2020-11-23 15:35:35 -06:00
'left' => $left_background ,
'left_text' => $left_colour ,
'right' => $right_background ,
'right_text' => $right_colour ,
]);
2018-12-16 15:18:17 -06:00
}
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function generate_entity_link ( $type , $entity , $text = null , $graph_type = null )
{
2019-06-23 00:29:12 -05:00
global $entity_cache ;
2015-07-13 20:10:26 +02:00
2012-05-20 23:14:08 +00:00
if ( is_numeric ( $entity )) {
2012-05-16 16:24:43 +00:00
$entity = get_entity_by_id_cache ( $type , $entity );
}
2015-07-13 20:10:26 +02:00
2012-05-16 16:24:43 +00:00
switch ( $type ) {
2016-08-18 20:28:22 -05:00
case 'port' :
$link = generate_port_link ( $entity , $text , $graph_type );
break ;
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
case 'storage' :
if ( empty ( $text )) {
$text = $entity [ 'storage_descr' ];
}
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
$link = generate_link ( $text , [ 'page' => 'device' , 'device' => $entity [ 'device_id' ], 'tab' => 'health' , 'metric' => 'storage' ]);
break ;
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
default :
2017-12-02 22:28:03 +02:00
$link = $entity [ $type . '_id' ];
2012-05-16 16:24:43 +00:00
}
2015-07-13 20:10:26 +02:00
2012-05-16 16:24:43 +00:00
return $link ;
} //end generate_entity_link()
2015-07-13 20:10:26 +02:00
2019-08-12 09:46:36 -05:00
/**
* Extract type and subtype from a complex graph type , also makes sure variables are file name safe .
2021-09-10 20:09:53 +02:00
*
2021-09-08 23:35:56 +02:00
* @ param string $type
2019-08-12 09:46:36 -05:00
* @ return array [ type , subtype ]
*/
function extract_graph_type ( $type ) : array
{
preg_match ( '/^(?P<type>[A-Za-z0-9]+)_(?P<subtype>.+)/' , $type , $graphtype );
$type = basename ( $graphtype [ 'type' ]);
$subtype = basename ( $graphtype [ 'subtype' ]);
2020-09-21 15:40:17 +02:00
2019-08-12 09:46:36 -05:00
return [ $type , $subtype ];
}
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function generate_port_link ( $port , $text = null , $type = null , $overlib = 1 , $single_graph = 0 )
{
2015-02-22 16:45:40 +00:00
$graph_array = [];
2017-04-04 08:08:23 +01:00
2012-05-16 16:24:43 +00:00
if ( ! $text ) {
2021-05-13 17:03:10 +02:00
$text = Rewrite :: normalizeIfName ( $port [ 'label' ] ? ? $port [ 'ifName' ]);
2012-05-16 16:24:43 +00:00
}
2015-07-13 20:10:26 +02:00
2012-05-16 16:24:43 +00:00
if ( $type ) {
$port [ 'graph_type' ] = $type ;
}
2015-07-13 20:10:26 +02:00
2012-05-16 16:24:43 +00:00
if ( ! isset ( $port [ 'graph_type' ])) {
$port [ 'graph_type' ] = 'port_bits' ;
}
2015-07-13 20:10:26 +02:00
2012-05-16 16:24:43 +00:00
$class = ifclass ( $port [ 'ifOperStatus' ], $port [ 'ifAdminStatus' ]);
2015-07-13 20:10:26 +02:00
2012-05-16 16:24:43 +00:00
if ( ! isset ( $port [ 'hostname' ])) {
$port = array_merge ( $port , device_by_id_cache ( $port [ 'device_id' ]));
}
2015-07-13 20:10:26 +02:00
2022-08-30 12:55:37 -05:00
if ( ! isset ( $port [ 'label' ])) {
$port = cleanPort ( $port );
}
2021-03-28 17:25:30 -05:00
$content = '<div class=list-large>' . $port [ 'hostname' ] . ' - ' . Rewrite :: normalizeIfName ( addslashes ( \LibreNMS\Util\Clean :: html ( $port [ 'label' ], []))) . '</div>' ;
2022-02-17 21:57:51 +01:00
$content .= addslashes ( \LibreNMS\Util\Clean :: html ( $port [ 'ifAlias' ], [])) . '<br />' ;
2015-07-13 20:10:26 +02:00
2017-12-02 22:28:03 +02:00
$content .= " <div style= \ 'width: 850px \ '> " ;
$graph_array [ 'type' ] = $port [ 'graph_type' ];
2010-05-02 23:24:47 +00:00
$graph_array [ 'legend' ] = 'yes' ;
$graph_array [ 'height' ] = '100' ;
2017-12-02 22:28:03 +02:00
$graph_array [ 'width' ] = '340' ;
2019-06-23 00:29:12 -05:00
$graph_array [ 'to' ] = Config :: get ( 'time.now' );
$graph_array [ 'from' ] = Config :: get ( 'time.day' );
2017-12-02 22:28:03 +02:00
$graph_array [ 'id' ] = $port [ 'port_id' ];
2021-03-28 17:25:30 -05:00
$content .= \LibreNMS\Util\Url :: graphTag ( $graph_array );
2015-04-17 16:24:51 +01:00
if ( $single_graph == 0 ) {
2019-06-23 00:29:12 -05:00
$graph_array [ 'from' ] = Config :: get ( 'time.week' );
2021-03-28 17:25:30 -05:00
$content .= \LibreNMS\Util\Url :: graphTag ( $graph_array );
2019-06-23 00:29:12 -05:00
$graph_array [ 'from' ] = Config :: get ( 'time.month' );
2021-03-28 17:25:30 -05:00
$content .= \LibreNMS\Util\Url :: graphTag ( $graph_array );
2019-06-23 00:29:12 -05:00
$graph_array [ 'from' ] = Config :: get ( 'time.year' );
2021-03-28 17:25:30 -05:00
$content .= \LibreNMS\Util\Url :: graphTag ( $graph_array );
2015-04-17 16:24:51 +01:00
}
2015-07-13 20:10:26 +02:00
2010-05-02 23:24:47 +00:00
$content .= '</div>' ;
2015-07-13 20:10:26 +02:00
2012-05-16 16:24:43 +00:00
$url = generate_port_url ( $port );
2015-07-13 20:10:26 +02:00
2015-04-18 10:29:44 +01:00
if ( $overlib == 0 ) {
2015-04-17 16:24:51 +01:00
return $content ;
2016-08-18 20:28:22 -05:00
} elseif ( port_permitted ( $port [ 'port_id' ], $port [ 'device_id' ])) {
2021-03-28 17:25:30 -05:00
return \LibreNMS\Util\Url :: overlibLink ( $url , $text , $content , $class );
2016-08-18 20:28:22 -05:00
} else {
2021-03-28 17:25:30 -05:00
return Rewrite :: normalizeIfName ( $text );
2010-05-02 23:24:47 +00:00
}
2009-10-02 13:20:24 +00:00
} //end generate_port_link()
2015-07-13 20:10:26 +02:00
2018-12-12 03:32:56 +01:00
function generate_sensor_link ( $args , $text = null , $type = null )
{
if ( ! $text ) {
2020-10-05 03:45:41 +02:00
$text = $args [ 'sensor_descr' ];
2018-12-12 03:32:56 +01:00
}
if ( ! $type ) {
$args [ 'graph_type' ] = 'sensor_' . $args [ 'sensor_class' ];
} else {
$args [ 'graph_type' ] = 'sensor_' . $type ;
}
if ( ! isset ( $args [ 'hostname' ])) {
$args = array_merge ( $args , device_by_id_cache ( $args [ 'device_id' ]));
}
$content = '<div class=list-large>' . $text . '</div>' ;
$content .= " <div style= \ 'width: 850px \ '> " ;
$graph_array = [
'type' => $args [ 'graph_type' ],
'legend' => 'yes' ,
'height' => '100' ,
'width' => '340' ,
'to' => Config :: get ( 'time.now' ),
'from' => Config :: get ( 'time.day' ),
'id' => $args [ 'sensor_id' ],
];
2021-03-28 17:25:30 -05:00
$content .= \LibreNMS\Util\Url :: graphTag ( $graph_array );
2018-12-12 03:32:56 +01:00
$graph_array [ 'from' ] = Config :: get ( 'time.week' );
2021-03-28 17:25:30 -05:00
$content .= \LibreNMS\Util\Url :: graphTag ( $graph_array );
2018-12-12 03:32:56 +01:00
$graph_array [ 'from' ] = Config :: get ( 'time.month' );
2021-03-28 17:25:30 -05:00
$content .= \LibreNMS\Util\Url :: graphTag ( $graph_array );
2018-12-12 03:32:56 +01:00
$graph_array [ 'from' ] = Config :: get ( 'time.year' );
2021-03-28 17:25:30 -05:00
$content .= \LibreNMS\Util\Url :: graphTag ( $graph_array );
2018-12-12 03:32:56 +01:00
$content .= '</div>' ;
2021-03-28 17:25:30 -05:00
$url = \LibreNMS\Util\Url :: generate ([ 'page' => 'graphs' , 'id' => $args [ 'sensor_id' ], 'type' => $args [ 'graph_type' ], 'from' => \LibreNMS\Config :: get ( 'time.day' )], []);
2020-10-05 03:45:41 +02:00
2021-03-28 17:25:30 -05:00
return \LibreNMS\Util\Url :: overlibLink ( $url , $text , $content );
2018-12-12 03:32:56 +01:00
} //end generate_sensor_link()
2016-08-18 20:28:22 -05:00
function generate_port_url ( $port , $vars = [])
{
2021-03-28 17:25:30 -05:00
return \LibreNMS\Util\Url :: generate ([ 'page' => 'device' , 'device' => $port [ 'device_id' ], 'tab' => 'port' , 'port' => $port [ 'port_id' ]], $vars );
2011-05-17 19:21:20 +00:00
} //end generate_port_url()
2015-07-13 20:10:26 +02:00
2021-01-15 15:28:22 +01:00
function generate_sap_url ( $sap , $vars = [])
{
2022-01-09 10:33:32 +01:00
// Overwrite special QinQ sap identifiers
if ( $sap [ 'sapEncapValue' ] == '*' ) {
$sap [ 'sapEncapValue' ] = '4095' ;
}
2021-01-15 15:28:22 +01:00
return \LibreNMS\Util\Url :: graphPopup ([ 'device' => $sap [ 'device_id' ], 'page' => 'graphs' , 'type' => 'device_sap' , 'tab' => 'routing' , 'proto' => 'mpls' , 'view' => 'saps' , 'traffic_id' => $sap [ 'svc_oid' ] . '.' . $sap [ 'sapPortId' ] . '.' . $sap [ 'sapEncapValue' ]], $vars );
} //end generate_sap_url()
2016-08-18 20:28:22 -05:00
function generate_port_image ( $args )
{
2011-09-26 14:06:48 +00:00
if ( ! $args [ 'bg' ]) {
2017-03-02 12:07:55 -06:00
$args [ 'bg' ] = 'FFFFFF00' ;
2011-09-26 14:06:48 +00:00
}
2015-07-13 20:10:26 +02:00
2017-12-02 22:28:03 +02:00
return " <img src='graph.php?type= " . $args [ 'graph_type' ] . '&id=' . $args [ 'port_id' ] . '&from=' . $args [ 'from' ] . '&to=' . $args [ 'to' ] . '&width=' . $args [ 'width' ] . '&height=' . $args [ 'height' ] . '&bg=' . $args [ 'bg' ] . " '> " ;
2013-11-26 11:03:00 +10:00
} //end generate_port_image()
2015-07-13 20:10:26 +02:00
2021-01-13 07:23:47 -06:00
/**
* Create image to output text instead of a graph .
*
2021-09-08 23:35:56 +02:00
* @ param string $text
* @ param int [] $color
2021-01-13 07:23:47 -06:00
*/
2022-11-03 15:07:17 -05:00
function graph_error ( $text , $short = null , $color = [ 128 , 0 , 0 ])
2021-01-13 07:23:47 -06:00
{
2022-11-03 15:07:17 -05:00
header ( 'Content-Type: ' . ImageFormat :: forGraph () -> contentType ());
echo \LibreNMS\Util\Graph :: error ( $text , $short , 300 , null , $color );
2021-01-13 07:23:47 -06:00
}
2016-08-18 20:28:22 -05:00
function print_port_thumbnail ( $args )
{
2013-11-26 11:03:00 +10:00
echo generate_port_link ( $args , generate_port_image ( $args ));
2009-10-02 13:20:24 +00:00
} //end print_port_thumbnail()
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function print_optionbar_start ( $height = 0 , $width = 0 , $marginbottom = 5 )
{
2014-02-27 19:49:24 +00:00
echo '
2018-02-12 17:50:16 +02:00
< div class = " panel panel-default " >
< div class = " panel-heading " >
2014-02-27 19:49:24 +00:00
' ;
2009-10-02 13:20:24 +00:00
} //end print_optionbar_start()
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function print_optionbar_end ()
{
2018-02-12 17:50:16 +02:00
echo '
</ div >
</ div >
' ;
2009-10-02 13:20:24 +00:00
} //end print_optionbar_end()
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function devclass ( $device )
{
2011-03-16 18:28:52 +00:00
if ( isset ( $device [ 'status' ]) && $device [ 'status' ] == '0' ) {
$class = 'list-device-down' ;
2016-08-18 20:28:22 -05:00
} else {
2011-05-12 20:16:03 +00:00
$class = 'list-device' ;
2011-03-31 17:19:54 +00:00
}
2015-07-13 20:10:26 +02:00
2020-01-10 01:46:52 +01:00
if ( isset ( $device [ 'disable_notify' ]) && $device [ 'disable_notify' ] == '1' ) {
2010-07-17 22:53:53 +00:00
$class = 'list-device-ignored' ;
2015-03-01 17:06:38 +00:00
if ( isset ( $device [ 'status' ]) && $device [ 'status' ] == '1' ) {
2010-07-17 22:53:53 +00:00
$class = 'list-device-ignored-up' ;
2015-07-13 20:10:26 +02:00
}
}
2015-03-01 17:06:38 +00:00
if ( isset ( $device [ 'disabled' ]) && $device [ 'disabled' ] == '1' ) {
2011-03-16 18:28:52 +00:00
$class = 'list-device-disabled' ;
2015-07-13 20:10:26 +02:00
}
2010-07-17 22:53:53 +00:00
return $class ;
2015-07-13 20:10:26 +02:00
} //end devclass()
2016-08-18 20:28:22 -05:00
function getlocations ()
{
2019-07-29 16:32:37 -05:00
if ( Auth :: user () -> hasGlobalRead ()) {
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
return dbFetchRows ( 'SELECT id, location FROM locations ORDER BY location' );
2015-07-13 20:10:26 +02:00
}
2019-07-29 16:32:37 -05:00
return dbFetchRows ( 'SELECT id, L.location FROM devices AS D, locations AS L, devices_perms AS P WHERE D.device_id = P.device_id AND P.user_id = ? AND D.location_id = L.id ORDER BY location' , [ Auth :: id ()]);
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
}
2015-07-13 20:10:26 +02:00
2018-07-23 11:54:02 -05:00
/**
* Get the recursive file size and count for a directory
*
2021-09-08 23:35:56 +02:00
* @ param string $path
2018-07-23 11:54:02 -05:00
* @ return array [ size , file count ]
*/
2016-08-18 20:28:22 -05:00
function foldersize ( $path )
{
2017-12-02 22:28:03 +02:00
$total_size = 0 ;
2011-09-23 10:16:31 +00:00
$total_files = 0 ;
2015-07-13 20:10:26 +02:00
2018-07-23 11:54:02 -05:00
foreach ( glob ( rtrim ( $path , '/' ) . '/*' , GLOB_NOSORT ) as $item ) {
if ( is_dir ( $item )) {
[ $folder_size , $file_count ] = foldersize ( $item );
$total_size += $folder_size ;
$total_files += $file_count ;
2016-08-18 20:28:22 -05:00
} else {
2018-07-23 11:54:02 -05:00
$total_size += filesize ( $item );
2011-09-23 10:16:31 +00:00
$total_files ++ ;
}
2015-07-13 20:10:26 +02:00
}
2018-07-23 11:54:02 -05:00
return [ $total_size , $total_files ];
}
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function generate_ap_link ( $args , $text = null , $type = null )
{
2017-04-18 22:42:37 +01:00
$args = cleanPort ( $args );
2012-05-21 18:17:23 +00:00
if ( ! $text ) {
2021-03-28 17:25:30 -05:00
$text = Rewrite :: normalizeIfName ( $args [ 'label' ]);
2012-05-21 18:17:23 +00:00
}
2015-07-13 20:10:26 +02:00
2012-05-21 18:17:23 +00:00
if ( $type ) {
$args [ 'graph_type' ] = $type ;
}
2015-07-13 20:10:26 +02:00
2012-05-21 18:17:23 +00:00
if ( ! isset ( $args [ 'graph_type' ])) {
$args [ 'graph_type' ] = 'port_bits' ;
}
2015-07-13 20:10:26 +02:00
2012-05-21 18:17:23 +00:00
if ( ! isset ( $args [ 'hostname' ])) {
$args = array_merge ( $args , device_by_id_cache ( $args [ 'device_id' ]));
2015-07-13 20:10:26 +02:00
}
2021-03-28 17:25:30 -05:00
$content = '<div class=list-large>' . $args [ 'text' ] . ' - ' . Rewrite :: normalizeIfName ( $args [ 'label' ]) . '</div>' ;
2012-05-21 18:17:23 +00:00
if ( $args [ 'ifAlias' ]) {
2021-03-28 17:25:30 -05:00
$content .= \LibreNMS\Util\Clean :: html ( $args [ 'ifAlias' ], []) . '<br />' ;
2015-07-13 20:10:26 +02:00
}
2017-12-02 22:28:03 +02:00
$content .= " <div style= \ 'width: 850px \ '> " ;
$graph_array = [];
$graph_array [ 'type' ] = $args [ 'graph_type' ];
2012-05-21 18:17:23 +00:00
$graph_array [ 'legend' ] = 'yes' ;
$graph_array [ 'height' ] = '100' ;
2017-12-02 22:28:03 +02:00
$graph_array [ 'width' ] = '340' ;
2019-06-23 00:29:12 -05:00
$graph_array [ 'to' ] = Config :: get ( 'time.now' );
$graph_array [ 'from' ] = Config :: get ( 'time.day' );
2017-12-02 22:28:03 +02:00
$graph_array [ 'id' ] = $args [ 'accesspoint_id' ];
2021-03-28 17:25:30 -05:00
$content .= \LibreNMS\Util\Url :: graphTag ( $graph_array );
2019-06-23 00:29:12 -05:00
$graph_array [ 'from' ] = Config :: get ( 'time.week' );
2021-03-28 17:25:30 -05:00
$content .= \LibreNMS\Util\Url :: graphTag ( $graph_array );
2019-06-23 00:29:12 -05:00
$graph_array [ 'from' ] = Config :: get ( 'time.month' );
2021-03-28 17:25:30 -05:00
$content .= \LibreNMS\Util\Url :: graphTag ( $graph_array );
2019-06-23 00:29:12 -05:00
$graph_array [ 'from' ] = Config :: get ( 'time.year' );
2021-03-28 17:25:30 -05:00
$content .= \LibreNMS\Util\Url :: graphTag ( $graph_array );
2017-12-02 22:28:03 +02:00
$content .= '</div>' ;
2015-07-13 20:10:26 +02:00
2012-05-21 18:17:23 +00:00
$url = generate_ap_url ( $args );
if ( port_permitted ( $args [ 'interface_id' ], $args [ 'device_id' ])) {
2021-03-28 17:25:30 -05:00
return \LibreNMS\Util\Url :: overlibLink ( $url , $text , $content );
2016-08-18 20:28:22 -05:00
} else {
2021-03-28 17:25:30 -05:00
return Rewrite :: normalizeIfName ( $text );
2012-05-21 18:17:23 +00:00
}
2015-05-17 11:44:33 +00:00
} //end generate_ap_link()
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function generate_ap_url ( $ap , $vars = [])
{
2021-03-28 17:25:30 -05:00
return \LibreNMS\Util\Url :: generate ([ 'page' => 'device' , 'device' => $ap [ 'device_id' ], 'tab' => 'accesspoints' , 'ap' => $ap [ 'accesspoint_id' ]], $vars );
2012-05-21 18:17:23 +00:00
} //end generate_ap_url()
2015-07-13 20:10:26 +02:00
2013-11-26 14:35:23 +10:00
// Find all the files in the given directory that match the pattern
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function get_matching_files ( $dir , $match = '/\.php$/' )
{
2014-03-14 09:04:14 +10:00
$list = [];
2013-11-26 14:35:23 +10:00
if ( $handle = opendir ( $dir )) {
while ( false !== ( $file = readdir ( $handle ))) {
if ( $file != '.' && $file != '..' && preg_match ( $match , $file ) === 1 ) {
$list [] = $file ;
}
2015-07-13 20:10:26 +02:00
}
2013-11-26 14:35:23 +10:00
closedir ( $handle );
}
2015-07-13 20:10:26 +02:00
2013-11-26 14:35:23 +10:00
return $list ;
} //end get_matching_files()
2015-07-13 20:10:26 +02:00
2013-11-26 14:35:23 +10:00
// Include all the files in the given directory that match the pattern
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function include_matching_files ( $dir , $match = '/\.php$/' )
{
2013-11-26 14:35:23 +10:00
foreach ( get_matching_files ( $dir , $match ) as $file ) {
include_once $file ;
}
2015-01-22 21:22:37 +00:00
} //end include_matching_files()
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function generate_pagination ( $count , $limit , $page , $links = 2 )
{
2017-12-02 22:28:03 +02:00
$end_page = ceil ( $count / $limit );
$start = (( $page - $links ) > 0 ) ? ( $page - $links ) : 1 ;
$end = (( $page + $links ) < $end_page ) ? ( $page + $links ) : $end_page ;
$return = '<ul class="pagination">' ;
2014-10-29 18:30:42 +00:00
$link_class = ( $page == 1 ) ? 'disabled' : '' ;
2017-12-02 22:28:03 +02:00
$return .= " <li><a href='' onClick='changePage(1,event);'>«</a></li> " ;
$return .= " <li class=' $link_class '><a href='' onClick='changePage( $page - 1,event);'><</a></li> " ;
2015-07-13 20:10:26 +02:00
2014-10-29 18:30:42 +00:00
if ( $start > 1 ) {
$return .= " <li><a href='' onClick='changePage(1,event);'>1</a></li> " ;
$return .= " <li class='disabled'><span>...</span></li> " ;
}
for ( $x = $start ; $x <= $end ; $x ++ ) {
$link_class = ( $page == $x ) ? 'active' : '' ;
2017-12-02 22:28:03 +02:00
$return .= " <li class=' $link_class '><a href='' onClick='changePage( $x ,event);'> $x </a></li> " ;
2014-10-29 18:30:42 +00:00
}
if ( $end < $end_page ) {
$return .= " <li class='disabled'><span>...</span></li> " ;
$return .= " <li><a href='' onClick='changePage( $end_page ,event);'> $end_page </a></li> " ;
}
$link_class = ( $page == $end_page ) ? 'disabled' : '' ;
2017-12-02 22:28:03 +02:00
$return .= " <li class=' $link_class '><a href='' onClick='changePage( $page + 1,event);'>></a></li> " ;
$return .= " <li class=' $link_class '><a href='' onClick='changePage( $end_page ,event);'>»</a></li> " ;
$return .= '</ul>' ;
2020-09-21 15:40:17 +02:00
2014-10-29 18:30:42 +00:00
return $return ;
} //end generate_pagination()
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function demo_account ()
{
2015-02-16 23:45:28 +00:00
print_error ( " You are logged in as a demo account, this page isn't accessible to you " );
} //end demo_account()
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function get_client_ip ()
{
2015-03-18 19:14:51 +00:00
if ( isset ( $_SERVER [ 'HTTP_X_FORWARDED_FOR' ])) {
$client_ip = $_SERVER [ 'HTTP_X_FORWARDED_FOR' ];
2016-08-18 20:28:22 -05:00
} else {
2015-03-18 19:14:51 +00:00
$client_ip = $_SERVER [ 'REMOTE_ADDR' ];
}
2015-07-13 20:10:26 +02:00
2015-03-18 19:14:51 +00:00
return $client_ip ;
2015-04-08 02:46:03 +01:00
} //end get_client_ip()
2016-08-18 20:28:22 -05:00
function clean_bootgrid ( $string )
{
2015-04-13 15:32:39 +01:00
$output = str_replace ([ " \r " , " \n " ], '' , $string );
$output = addslashes ( $output );
2020-09-21 15:40:17 +02:00
2015-04-13 15:32:39 +01:00
return $output ;
} //end clean_bootgrid()
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function get_url ()
{
2015-05-17 03:20:28 +01:00
// http://stackoverflow.com/questions/2820723/how-to-get-base-url-with-php
// http://stackoverflow.com/users/184600/ma%C4%8Dek
return sprintf (
'%s://%s%s' ,
isset ( $_SERVER [ 'HTTPS' ]) && $_SERVER [ 'HTTPS' ] != 'off' ? 'https' : 'http' ,
$_SERVER [ 'SERVER_NAME' ],
$_SERVER [ 'REQUEST_URI' ]
);
} //end get_url()
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function alert_details ( $details )
{
2022-08-30 12:55:37 -05:00
if ( is_string ( $details )) {
2015-05-17 11:44:33 +00:00
$details = json_decode ( gzuncompress ( $details ), true );
2022-08-30 12:55:37 -05:00
} elseif ( ! is_array ( $details )) {
$details = [];
2015-05-17 11:44:33 +00:00
}
2015-07-13 20:10:26 +02:00
2021-10-03 01:12:18 +02:00
$max_row_length = 0 ;
$all_fault_detail = '' ;
2022-08-30 12:55:37 -05:00
foreach ( $details [ 'rule' ] ? ? [] as $o => $tmp_alerts ) {
2021-10-03 01:12:18 +02:00
$fault_detail = '' ;
2017-12-02 22:28:03 +02:00
$fallback = true ;
$fault_detail .= '#' . ( $o + 1 ) . ': ' ;
2022-08-30 12:55:37 -05:00
if ( isset ( $tmp_alerts [ 'bill_id' ])) {
2021-03-28 17:25:30 -05:00
$fault_detail .= '<a href="' . \LibreNMS\Util\Url :: generate ([ 'page' => 'bill' , 'bill_id' => $tmp_alerts [ 'bill_id' ]], []) . '">' . $tmp_alerts [ 'bill_name' ] . '</a>; ' ;
2017-12-02 22:28:03 +02:00
$fallback = false ;
2015-05-17 11:44:33 +00:00
}
2015-07-13 20:10:26 +02:00
2022-08-30 12:55:37 -05:00
if ( isset ( $tmp_alerts [ 'port_id' ])) {
2022-04-14 08:36:13 +03:00
if ( $tmp_alerts [ 'isisISAdjState' ]) {
$fault_detail .= 'Adjacent ' . $tmp_alerts [ 'isisISAdjIPAddrAddress' ];
$port = \App\Models\Port :: find ( $tmp_alerts [ 'port_id' ]);
$fault_detail .= ', Interface ' . \LibreNMS\Util\Url :: portLink ( $port );
} else {
$tmp_alerts = cleanPort ( $tmp_alerts );
$fault_detail .= generate_port_link ( $tmp_alerts ) . '; ' ;
}
2017-12-02 22:28:03 +02:00
$fallback = false ;
2015-07-13 20:10:26 +02:00
}
2022-08-30 12:55:37 -05:00
if ( isset ( $tmp_alerts [ 'accesspoint_id' ])) {
2017-02-15 07:55:03 +00:00
$fault_detail .= generate_ap_link ( $tmp_alerts , $tmp_alerts [ 'name' ]) . '; ' ;
2017-12-02 22:28:03 +02:00
$fallback = false ;
2017-02-15 07:55:03 +00:00
}
2022-08-30 12:55:37 -05:00
if ( isset ( $tmp_alerts [ 'sensor_id' ])) {
2021-02-25 19:00:47 +01:00
if ( $tmp_alerts [ 'sensor_class' ] == 'state' ) {
// Give more details for a state (textual form)
$details = 'State: ' . $tmp_alerts [ 'state_descr' ] . ' (numerical ' . $tmp_alerts [ 'sensor_current' ] . ')<br> ' ;
} else {
// Other sensors
$details = 'Value: ' . $tmp_alerts [ 'sensor_current' ] . ' (' . $tmp_alerts [ 'sensor_class' ] . ')<br> ' ;
}
2018-12-12 03:32:56 +01:00
$details_a = [];
if ( $tmp_alerts [ 'sensor_limit_low' ]) {
$details_a [] = 'low: ' . $tmp_alerts [ 'sensor_limit_low' ];
}
if ( $tmp_alerts [ 'sensor_limit_low_warn' ]) {
$details_a [] = 'low_warn: ' . $tmp_alerts [ 'sensor_limit_low_warn' ];
}
if ( $tmp_alerts [ 'sensor_limit_warn' ]) {
$details_a [] = 'high_warn: ' . $tmp_alerts [ 'sensor_limit_warn' ];
}
if ( $tmp_alerts [ 'sensor_limit' ]) {
$details_a [] = 'high: ' . $tmp_alerts [ 'sensor_limit' ];
}
$details .= implode ( ', ' , $details_a );
$fault_detail .= generate_sensor_link ( $tmp_alerts , $tmp_alerts [ 'name' ]) . '; <br>' . $details ;
$fallback = false ;
}
2020-02-01 00:59:56 +01:00
2022-08-30 12:55:37 -05:00
if ( isset ( $tmp_alerts [ 'bgpPeer_id' ])) {
2018-12-12 03:32:56 +01:00
// If we have a bgpPeer_id, we format the data accordingly
$fault_detail .= " BGP peer <a href=' " .
2021-03-28 17:25:30 -05:00
\LibreNMS\Util\Url :: generate ([
'page' => 'device' ,
2018-12-12 03:32:56 +01:00
'device' => $tmp_alerts [ 'device_id' ],
'tab' => 'routing' ,
2021-03-28 17:25:30 -05:00
'proto' => 'bgp' ,
]) .
2018-12-12 03:32:56 +01:00
" '> " . $tmp_alerts [ 'bgpPeerIdentifier' ] . '</a>' ;
$fault_detail .= ', AS' . $tmp_alerts [ 'bgpPeerRemoteAs' ];
$fault_detail .= ', State ' . $tmp_alerts [ 'bgpPeerState' ];
$fallback = false ;
}
2020-02-01 00:59:56 +01:00
2022-08-30 12:55:37 -05:00
if ( $tmp_alerts [ 'type' ] && isset ( $tmp_alerts [ 'label' ])) {
2015-08-15 16:01:43 +10:00
if ( $tmp_alerts [ 'error' ] == '' ) {
2017-12-02 22:28:03 +02:00
$fault_detail .= ' ' . $tmp_alerts [ 'type' ] . ' - ' . $tmp_alerts [ 'label' ] . '; ' ;
2016-08-18 20:28:22 -05:00
} else {
2017-12-02 22:28:03 +02:00
$fault_detail .= ' ' . $tmp_alerts [ 'type' ] . ' - ' . $tmp_alerts [ 'label' ] . ' - ' . $tmp_alerts [ 'error' ] . '; ' ;
2015-08-15 16:01:43 +10:00
}
2017-12-02 22:28:03 +02:00
$fallback = false ;
2015-08-15 16:01:43 +10:00
}
2020-02-01 00:59:56 +01:00
if ( in_array ( 'app_id' , array_keys ( $tmp_alerts ))) {
2021-03-28 17:25:30 -05:00
$fault_detail .= " <a href=' " .
\LibreNMS\Util\Url :: generate ([
'page' => 'device' ,
'device' => $tmp_alerts [ 'device_id' ],
'tab' => 'apps' ,
'app' => $tmp_alerts [ 'app_type' ],
]) . " '> " ;
2021-07-09 21:34:17 +02:00
$fault_detail .= $tmp_alerts [ 'app_type' ];
2020-02-01 00:59:56 +01:00
$fault_detail .= '</a>' ;
2021-07-11 20:03:54 +02:00
if ( $tmp_alerts [ 'app_status' ]) {
$fault_detail .= ' => ' . $tmp_alerts [ 'app_status' ];
}
if ( $tmp_alerts [ 'metric' ]) {
$fault_detail .= ' : ' . $tmp_alerts [ 'metric' ] . ' => ' . $tmp_alerts [ 'value' ];
}
2020-02-01 00:59:56 +01:00
$fallback = false ;
}
2015-05-17 11:44:33 +00:00
if ( $fallback === true ) {
2020-01-31 12:42:46 +01:00
$fault_detail_data = [];
2015-05-17 11:44:33 +00:00
foreach ( $tmp_alerts as $k => $v ) {
2020-01-31 12:42:46 +01:00
if ( in_array ( $k , [ 'device_id' , 'sysObjectID' , 'sysDescr' , 'location_id' ])) {
continue ;
}
2020-02-19 17:34:37 +01:00
if ( ! empty ( $v ) && str_i_contains ( $k , [ 'id' , 'desc' , 'msg' , 'last' ])) {
2020-01-31 12:42:46 +01:00
$fault_detail_data [] = " $k => ' $v ' " ;
2015-07-13 20:10:26 +02:00
}
}
2020-01-31 12:42:46 +01:00
$fault_detail .= count ( $fault_detail_data ) ? implode ( '<br>  ' , $fault_detail_data ) : '' ;
2015-07-13 20:10:26 +02:00
2015-05-17 11:44:33 +00:00
$fault_detail = rtrim ( $fault_detail , ', ' );
}
2015-07-13 20:10:26 +02:00
2015-05-17 11:44:33 +00:00
$fault_detail .= '<br>' ;
2021-10-03 01:12:18 +02:00
$max_row_length = strlen ( strip_tags ( $fault_detail )) > $max_row_length ? strlen ( strip_tags ( $fault_detail )) : $max_row_length ;
$all_fault_detail .= $fault_detail ;
2015-05-17 11:44:33 +00:00
} //end foreach
2015-07-13 20:10:26 +02:00
2021-10-03 01:12:18 +02:00
return [ $all_fault_detail , $max_row_length ];
2015-05-17 11:44:33 +00:00
} //end alert_details()
2015-07-23 17:48:22 +01:00
2016-08-18 20:28:22 -05:00
function dynamic_override_config ( $type , $name , $device )
{
$attrib_val = get_dev_attrib ( $device , $name );
2015-10-10 13:40:27 +00:00
if ( $attrib_val == 'true' ) {
$checked = 'checked' ;
2016-08-18 20:28:22 -05:00
} else {
2015-10-10 13:40:27 +00:00
$checked = '' ;
}
if ( $type == 'checkbox' ) {
2017-12-02 22:28:03 +02:00
return '<input type="checkbox" id="override_config" name="override_config" data-attrib="' . $name . '" data-device_id="' . $device [ 'device_id' ] . '" data-size="small" ' . $checked . '>' ;
2016-08-18 20:28:22 -05:00
} elseif ( $type == 'text' ) {
2017-12-02 22:28:03 +02:00
return '<input type="text" id="override_config_text" name="override_config_text" data-attrib="' . $name . '" data-device_id="' . $device [ 'device_id' ] . '" value="' . $attrib_val . '">' ;
2015-11-17 00:20:06 +00:00
}
2015-10-10 13:40:27 +00:00
} //end dynamic_override_config()
2015-10-12 21:28:31 +00:00
2016-06-24 14:56:45 -07:00
/**
* Return the rows from 'ports' for all ports of a certain type as parsed by port_descr_parser .
* One or an array of strings can be provided as an argument ; if an array is passed , all ports matching
* any of the types in the array are returned .
2021-09-10 20:09:53 +02:00
*
2016-06-24 14:56:45 -07:00
* @ param $types mixed String or strings matching 'port_descr_type' s .
* @ return array Rows from the ports table for matching ports .
*/
2016-08-18 20:28:22 -05:00
function get_ports_from_type ( $given_types )
{
2016-06-24 14:56:45 -07:00
// Make the arg an array if it isn't, so subsequent steps only have to handle arrays.
2016-08-18 20:28:22 -05:00
if ( ! is_array ( $given_types )) {
2016-06-24 14:56:45 -07:00
$given_types = [ $given_types ];
}
// Check the config for a '_descr' entry for each argument. This is how a 'custom_descr' entry can
// be key/valued to some other string that's actually searched for in the DB. Merge or append the
// configured value if it's an array or a string. Or append the argument itself if there's no matching
// entry in config.
2019-06-23 00:29:12 -05:00
$search_types = [];
2016-08-18 20:28:22 -05:00
foreach ( $given_types as $type ) {
2019-06-23 00:29:12 -05:00
if ( Config :: has ( $type . '_descr' )) {
$type_descr = Config :: get ( $type . '_descr' );
if ( is_array ( $type_descr )) {
$search_types = array_merge ( $search_types , $type_descr );
2016-08-18 20:28:22 -05:00
} else {
2019-06-23 00:29:12 -05:00
$search_types [] = $type_descr ;
2016-06-24 14:56:45 -07:00
}
2016-08-18 20:28:22 -05:00
} else {
2016-06-24 14:56:45 -07:00
$search_types [] = $type ;
}
}
// Using the full list of strings to search the DB for, build the 'where' portion of a query that
// compares 'port_descr_type' with entry in the list. Also, since '@' is the convential wildcard,
// replace it with '%' so it functions as a wildcard in the SQL query.
$type_where = ' (' ;
$or = '' ;
$type_param = [];
2016-08-18 20:28:22 -05:00
foreach ( $search_types as $type ) {
2016-06-24 14:56:45 -07:00
if ( ! empty ( $type )) {
2017-12-02 22:28:03 +02:00
$type = strtr ( $type , '@' , '%' );
$type_where .= " $or `port_descr_type` LIKE ? " ;
$or = 'OR' ;
$type_param [] = $type ;
2016-06-24 14:56:45 -07:00
}
}
2017-12-02 22:28:03 +02:00
$type_where .= ') ' ;
2016-06-24 14:56:45 -07:00
// Run the query with the generated 'where' and necessary parameters, and send it back.
$ports = dbFetchRows ( " SELECT * FROM `ports` as I, `devices` AS D WHERE $type_where AND I.device_id = D.device_id ORDER BY I.ifAlias " , $type_param );
2020-09-21 15:40:17 +02:00
2016-06-24 14:56:45 -07:00
return $ports ;
}
2016-08-12 00:53:25 +03:00
2016-09-26 22:27:27 +01:00
/**
* @ param $filename
* @ param $content
*/
function file_download ( $filename , $content )
{
$length = strlen ( $content );
header ( 'Content-Description: File Transfer' );
header ( 'Content-Type: text/plain' );
header ( " Content-Disposition: attachment; filename= $filename " );
header ( 'Content-Transfer-Encoding: binary' );
header ( 'Content-Length: ' . $length );
header ( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
header ( 'Expires: 0' );
header ( 'Pragma: public' );
echo $content ;
}
2016-10-26 06:54:48 +00:00
function get_rules_from_json ()
{
2019-06-23 00:29:12 -05:00
return json_decode ( file_get_contents ( Config :: get ( 'install_dir' ) . '/misc/alert_rules.json' ), true );
2016-10-26 06:54:48 +00:00
}
2017-01-09 21:09:02 +02:00
function search_oxidized_config ( $search_in_conf_textbox )
{
2020-07-22 03:34:42 +02:00
if ( ! Auth :: user () -> hasGlobalRead ()) {
return false ;
}
2019-06-23 00:29:12 -05:00
$oxidized_search_url = Config :: get ( 'oxidized.url' ) . '/nodes/conf_search?format=json' ;
2017-01-09 21:09:02 +02:00
$postdata = http_build_query (
2020-09-21 15:40:17 +02:00
[
2017-01-09 21:09:02 +02:00
'search_in_conf_textbox' => $search_in_conf_textbox ,
2020-09-21 15:40:17 +02:00
]
2017-01-09 21:09:02 +02:00
);
$opts = [ 'http' => [
2017-12-02 22:28:03 +02:00
'method' => 'POST' ,
'header' => 'Content-type: application/x-www-form-urlencoded' ,
2017-01-09 21:09:02 +02:00
'content' => $postdata ,
2020-09-21 15:40:17 +02:00
],
];
2017-12-02 22:28:03 +02:00
$context = stream_context_create ( $opts );
2020-05-19 14:35:32 -05:00
2020-01-27 11:31:43 +00:00
$nodes = json_decode ( file_get_contents ( $oxidized_search_url , false , $context ), true );
// Look up Oxidized node names to LibreNMS devices for a link
foreach ( $nodes as & $n ) {
$dev = device_by_name ( $n [ 'node' ]);
$n [ 'dev_id' ] = $dev ? $dev [ 'device_id' ] : false ;
}
2020-07-22 03:34:42 +02:00
/*
// Filter nodes we don't have access too
$nodes = array_filter ( $nodes , function ( $device ) {
return \Permissions :: canAccessDevice ( $device [ 'dev_id' ], Auth :: id ());
});
*/
2020-01-27 11:31:43 +00:00
return $nodes ;
2017-01-09 21:09:02 +02:00
}
2017-02-03 21:20:25 +00:00
/**
* @ param $data
* @ return bool | mixed
*/
function array_to_htmljson ( $data )
{
if ( is_array ( $data )) {
$data = htmlentities ( json_encode ( $data ));
2020-09-21 15:40:17 +02:00
2017-02-03 21:20:25 +00:00
return str_replace ( ',' , ',<br />' , $data );
} else {
return false ;
}
}
2017-02-13 00:41:05 +02:00
/**
2021-09-08 23:35:56 +02:00
* @ param int $eventlog_severity
2017-03-12 08:05:31 -05:00
* @ return string $eventlog_severity_icon
2017-02-13 00:41:05 +02:00
*/
function eventlog_severity ( $eventlog_severity )
{
switch ( $eventlog_severity ) {
case 1 :
2017-12-24 21:56:24 +02:00
return 'label-success' ; //OK
2017-02-13 00:41:05 +02:00
case 2 :
2017-12-24 21:56:24 +02:00
return 'label-info' ; //Informational
2017-02-13 00:41:05 +02:00
case 3 :
2017-12-24 21:56:24 +02:00
return 'label-primary' ; //Notice
2017-02-13 00:41:05 +02:00
case 4 :
2017-12-24 21:56:24 +02:00
return 'label-warning' ; //Warning
2017-02-13 00:41:05 +02:00
case 5 :
2017-12-24 21:56:24 +02:00
return 'label-danger' ; //Critical
2017-02-13 00:41:05 +02:00
default :
2017-12-24 21:56:24 +02:00
return 'label-default' ; //Unknown
2017-02-13 00:41:05 +02:00
}
} // end eventlog_severity
2017-02-22 12:08:18 +00:00
2017-02-27 23:32:13 +01:00
function get_oxidized_nodes_list ()
{
$context = stream_context_create ([
'http' => [
'header' => 'Accept: application/json' ,
2020-09-21 15:40:17 +02:00
],
2017-02-27 23:32:13 +01:00
]);
2019-06-23 00:29:12 -05:00
$data = json_decode ( file_get_contents ( Config :: get ( 'oxidized.url' ) . '/nodes?format=json' , false , $context ), true );
2017-02-27 23:32:13 +01:00
foreach ( $data as $object ) {
$device = device_by_name ( $object [ 'name' ]);
2018-10-17 18:12:25 +02:00
if ( ! device_permitted ( $device [ 'device_id' ])) {
//user cannot see this device, so let's skip it.
continue ;
}
2020-05-19 14:35:32 -05:00
2020-01-27 11:31:43 +00:00
echo ' < tr >
< td > ' . $device[' device_id '] . ' </ td >
< td > ' . $object[' name '] . ' </ td >
< td > ' . $device[' sysName '] . ' </ td >
< td > ' . $object[' status '] . ' </ td >
< td > ' . $object[' time '] . ' </ td >
< td > ' . $object[' model '] . ' </ td >
< td > ' . $object[' group '] . ' </ td >
< td ></ td >
2017-02-27 23:32:13 +01:00
</ tr > ' ;
}
}
2017-03-03 08:27:33 -06:00
2017-12-02 22:28:03 +02:00
/**
* Return stacked graphs information
*
2021-09-08 23:35:56 +02:00
* @ param string $transparency value of desired transparency applied to rrdtool options ( values 01 - 99 )
2017-12-02 22:28:03 +02:00
* @ return array containing transparency and stacked setup
*/
function generate_stacked_graphs ( $transparency = '88' )
{
if ( Config :: get ( 'webui.graph_stacked' ) == true ) {
return [ 'transparency' => $transparency , 'stacked' => '1' ];
} else {
return [ 'transparency' => '' , 'stacked' => '-1' ];
}
}
2017-12-30 05:42:51 -06:00
2018-02-10 16:18:53 +03:00
/**
* Returns the sysname of a device with a html line break prepended .
* if the device has an empty sysname it will return device ' s hostname instead
* And finally if the device has no hostname it will return an empty string
2021-09-10 20:09:53 +02:00
*
2018-04-24 21:20:21 -05:00
* @ param array device
2018-02-10 16:18:53 +03:00
* @ return string
*/
function get_device_name ( $device )
{
$ret_str = '' ;
if ( format_hostname ( $device ) !== $device [ 'sysName' ]) {
$ret_str = $device [ 'sysName' ];
} elseif ( $device [ 'hostname' ] !== $device [ 'ip' ]) {
$ret_str = $device [ 'hostname' ];
}
return $ret_str ;
}
2018-12-20 22:56:54 +01:00
/**
* Returns state generic label from value with optional text
*/
2019-06-27 00:30:10 -05:00
function get_state_label ( $sensor )
2018-12-20 22:56:54 +01:00
{
2019-06-27 00:30:10 -05:00
$state_translation = dbFetchRow ( 'SELECT * FROM state_translations as ST, sensors_to_state_indexes as SSI WHERE ST.state_index_id=SSI.state_index_id AND SSI.sensor_id = ? AND ST.state_value = ? ' , [ $sensor [ 'sensor_id' ], $sensor [ 'sensor_current' ]]);
switch ( $state_translation [ 'state_generic_value' ]) {
2018-12-20 22:56:54 +01:00
case 0 : // OK
2019-06-27 00:30:10 -05:00
$state_text = $state_translation [ 'state_descr' ] ? : 'OK' ;
2018-12-20 22:56:54 +01:00
$state_label = 'label-success' ;
break ;
case 1 : // Warning
2019-06-27 00:30:10 -05:00
$state_text = $state_translation [ 'state_descr' ] ? : 'Warning' ;
2018-12-20 22:56:54 +01:00
$state_label = 'label-warning' ;
break ;
case 2 : // Critical
2019-06-27 00:30:10 -05:00
$state_text = $state_translation [ 'state_descr' ] ? : 'Critical' ;
2018-12-20 22:56:54 +01:00
$state_label = 'label-danger' ;
break ;
2019-06-27 00:30:10 -05:00
case 3 : // Unknown
2018-12-20 22:56:54 +01:00
default :
2019-06-27 00:30:10 -05:00
$state_text = $state_translation [ 'state_descr' ] ? : 'Unknown' ;
2018-12-20 22:56:54 +01:00
$state_label = 'label-default' ;
}
2020-09-21 15:40:17 +02:00
2019-06-27 00:30:10 -05:00
return " <span class='label $state_label '> $state_text </span> " ;
2018-12-20 22:56:54 +01:00
}
/**
2019-06-27 00:30:10 -05:00
* Get sensor label and state color
2021-09-10 20:09:53 +02:00
*
2021-09-08 23:35:56 +02:00
* @ param array $sensor
* @ param string $type sensors or wireless
2019-07-02 07:11:58 -05:00
* @ return string
2018-12-20 22:56:54 +01:00
*/
2019-07-02 07:11:58 -05:00
function get_sensor_label_color ( $sensor , $type = 'sensors' )
2018-12-20 22:56:54 +01:00
{
2019-06-27 00:30:10 -05:00
$label_style = 'label-success' ;
2018-12-20 22:56:54 +01:00
if ( is_null ( $sensor )) {
return 'label-unknown' ;
}
if ( ! is_null ( $sensor [ 'sensor_limit_warn' ]) && $sensor [ 'sensor_current' ] > $sensor [ 'sensor_limit_warn' ]) {
2019-06-27 00:30:10 -05:00
$label_style = 'label-warning' ;
2018-12-20 22:56:54 +01:00
}
if ( ! is_null ( $sensor [ 'sensor_limit_low_warn' ]) && $sensor [ 'sensor_current' ] < $sensor [ 'sensor_limit_low_warn' ]) {
2019-06-27 00:30:10 -05:00
$label_style = 'label-warning' ;
2018-12-20 22:56:54 +01:00
}
if ( ! is_null ( $sensor [ 'sensor_limit' ]) && $sensor [ 'sensor_current' ] > $sensor [ 'sensor_limit' ]) {
2019-06-27 00:30:10 -05:00
$label_style = 'label-danger' ;
2018-12-20 22:56:54 +01:00
}
if ( ! is_null ( $sensor [ 'sensor_limit_low' ]) && $sensor [ 'sensor_current' ] < $sensor [ 'sensor_limit_low' ]) {
2019-06-27 00:30:10 -05:00
$label_style = 'label-danger' ;
2018-12-20 22:56:54 +01:00
}
2019-07-02 07:11:58 -05:00
$unit = __ ( " $type . { $sensor [ 'sensor_class' ] } .unit " );
2019-09-16 20:43:24 +02:00
if ( $sensor [ 'sensor_class' ] == 'runtime' ) {
2021-03-28 17:25:30 -05:00
$sensor [ 'sensor_current' ] = \LibreNMS\Util\Time :: formatInterval ( $sensor [ 'sensor_current' ] * 60 );
2020-09-21 15:40:17 +02:00
2019-09-16 20:43:24 +02:00
return " <span class='label $label_style '> " . trim ( $sensor [ 'sensor_current' ]) . '</span>' ;
2020-06-10 03:41:36 -05:00
}
if ( $sensor [ 'sensor_class' ] == 'frequency' && $sensor [ 'sensor_type' ] == 'openwrt' ) {
return " <span class='label $label_style '> " . trim ( $sensor [ 'sensor_current' ]) . ' ' . $unit . '</span>' ;
2019-09-16 20:43:24 +02:00
}
2020-09-21 15:40:17 +02:00
2022-10-25 15:31:02 +02:00
if ( in_array ( $sensor [ 'rrd_type' ], [ 'COUNTER' , 'DERIVE' , 'DCOUNTER' , 'DDERIVE' ])) {
//compute and display an approx rate for this sensor
return " <span class='label $label_style '> " . trim ( Number :: formatSi ( max ( 0 , $sensor [ 'sensor_current' ] - $sensor [ 'sensor_prev' ]) / Config :: get ( 'rrd.step' , 300 ), 2 , 3 , $unit )) . '</span>' ;
}
2021-12-13 15:30:57 +01:00
if ( $type == 'wireless' && $sensor [ 'sensor_class' ] == 'frequency' ) {
return " <span class='label $label_style '> " . trim ( Number :: formatSi ( $sensor [ 'sensor_current' ] * 1000000 , 2 , 3 , 'Hz' )) . '</span>' ;
}
if ( $type == 'wireless' && $sensor [ 'sensor_class' ] == 'distance' ) {
return " <span class='label $label_style '> " . trim ( Number :: formatSi ( $sensor [ 'sensor_current' ] * 1000 , 2 , 3 , 'm' )) . '</span>' ;
}
2021-03-28 17:25:30 -05:00
return " <span class='label $label_style '> " . trim ( Number :: formatSi ( $sensor [ 'sensor_current' ], 2 , 3 , $unit )) . '</span>' ;
2019-03-20 16:17:49 +01:00
}
2019-08-06 19:16:34 -05:00
/**
* @ params int unix time
* @ params int seconds
2021-09-10 20:09:53 +02:00
*
2019-08-06 19:16:34 -05:00
* @ return int
*
* Rounds down to the nearest interval .
*
* The first argument is required and it is the unix time being
* rounded down .
*
* The second value is the time interval . If not specified , it
* defaults to 300 , or 5 minutes .
*/
function lowest_time ( $time , $seconds = 300 )
{
return $time - ( $time % $seconds );
}
/**
* @ params int
2021-09-10 20:09:53 +02:00
*
2019-08-06 19:16:34 -05:00
* @ return string
*
* This returns the subpath for working with nfdump .
*
* 1 value is taken and that is a unix time stamp . It will be then be rounded
* off to the lowest five minutes earlier .
*
* The return string will be a path partial you can use with nfdump to tell it what
* file or range of files to use .
*
* Below ie a explanation of the layouts as taken from the NfSen config file .
* 0 no hierachy levels - flat layout - compatible with pre NfSen version
* 1 % Y /% m /% d year / month / day
* 2 % Y /% m /% d /% H year / month / day / hour
* 3 % Y /% W /% u year / week_of_year / day_of_week
* 4 % Y /% W /% u /% H year / week_of_year / day_of_week / hour
* 5 % Y /% j year / day - of - year
* 6 % Y /% j /% H year / day - of - year / hour
* 7 % Y -% m -% d year - month - day
* 8 % Y -% m -% d /% H year - month - day / hour
*/
function time_to_nfsen_subpath ( $time )
{
$time = lowest_time ( $time );
$layout = Config :: get ( 'nfsen_subdirlayout' );
if ( $layout == 0 ) {
return 'nfcapd.' . date ( 'YmdHi' , $time );
} elseif ( $layout == 1 ) {
return date ( 'Y\/m\/d\/\n\f\c\a\p\d\.YmdHi' , $time );
} elseif ( $layout == 2 ) {
return date ( 'Y\/m\/d\/H\/\n\f\c\a\p\d\.YmdHi' , $time );
} elseif ( $layout == 3 ) {
return date ( 'Y\/W\/w\/\n\f\c\a\p\d\.YmdHi' , $time );
} elseif ( $layout == 4 ) {
return date ( 'Y\/W\/w\/H\/\n\f\c\a\p\d\.YmdHi' , $time );
} elseif ( $layout == 5 ) {
return date ( 'Y\/z\/\n\f\c\a\p\d\.YmdHi' , $time );
} elseif ( $layout == 6 ) {
return date ( 'Y\/z\/H\/\n\f\c\a\p\d\.YmdHi' , $time );
} elseif ( $layout == 7 ) {
return date ( 'Y\-m\-d\/\n\f\c\a\p\d\.YmdHi' , $time );
} elseif ( $layout == 8 ) {
return date ( 'Y\-m\-d\/H\/\n\f\c\a\p\d\.YmdHi' , $time );
}
}
/**
* @ params string hostname
2021-09-10 20:09:53 +02:00
*
2019-08-06 19:16:34 -05:00
* @ return string
*
* Takes a hostname and transforms it to the name
* used by nfsen .
*/
function nfsen_hostname ( $hostname )
{
$nfsen_hostname = str_replace ( '.' , Config :: get ( 'nfsen_split_char' ), $hostname );
if ( ! is_null ( Config :: get ( 'nfsen_suffix' ))) {
$nfsen_hostname = str_replace ( Config :: get ( 'nfsen_suffix' ), '' , $nfsen_hostname );
}
2020-09-21 15:40:17 +02:00
2019-08-06 19:16:34 -05:00
return $nfsen_hostname ;
}
/**
* @ params string hostname
2021-09-10 20:09:53 +02:00
*
2019-08-06 19:16:34 -05:00
* @ return string
*
* Takes a hostname and returns the path to the nfsen
* live dir .
*/
function nfsen_live_dir ( $hostname )
{
$hostname = nfsen_hostname ( $hostname );
foreach ( Config :: get ( 'nfsen_base' ) as $base_dir ) {
if ( file_exists ( $base_dir ) && is_dir ( $base_dir )) {
return $base_dir . '/profiles-data/live/' . $hostname ;
}
}
}