* @copyright (C) 2006 - 2012 Adam Armstrong (as Observium)
* @copyright (C) 2013 LibreNMS Group
*/
use LibreNMS\Config;
/**
* Compare $t with the value of $vars[$v], if that exists
* @param string $v Name of the var to test
* @param string $t Value to compare $vars[$v] to
* @return boolean true, if values are the same, false if $vars[$v] is unset or values differ
*/
function var_eq($v, $t)
{
global $vars;
if (isset($vars[$v]) && $vars[$v] == $t) {
return true;
}
return false;
}
/**
* Get the value of $vars[$v], if it exists
* @param string $v Name of the var to get
* @return string|boolean The value of $vars[$v] if it exists, false if it does not exist
*/
function var_get($v)
{
global $vars;
if (isset($vars[$v])) {
return $vars[$v];
}
return false;
}
function data_uri($file, $mime)
{
$contents = file_get_contents($file);
$base64 = base64_encode($contents);
return ('data:' . $mime . ';base64,' . $base64);
}//end data_uri()
/**
* Convert string to nice case, mostly used for applications
*
* @param $item
* @return mixed|string
*/
function nicecase($item)
{
return \LibreNMS\Util\StringHelpers::niceCase($item);
}
function toner2colour($descr, $percent)
{
$colour = get_percentage_colours(100 - $percent);
if (substr($descr, -1) == 'C' || stripos($descr, 'cyan') !== false) {
$colour['left'] = '55D6D3';
$colour['right'] = '33B4B1';
}
if (substr($descr, -1) == 'M' || stripos($descr, 'magenta') !== false) {
$colour['left'] = 'F24AC8';
$colour['right'] = 'D028A6';
}
if (substr($descr, -1) == 'Y' || stripos($descr, 'yellow') !== false
|| stripos($descr, 'giallo') !== false
|| stripos($descr, 'gul') !== false
) {
$colour['left'] = 'FFF200';
$colour['right'] = 'DDD000';
}
if (substr($descr, -1) == 'K' || stripos($descr, 'black') !== false
|| stripos($descr, 'nero') !== false
) {
$colour['left'] = '000000';
$colour['right'] = '222222';
}
return $colour;
}//end toner2colour()
/**
* Find all links in some text and turn them into html links.
*
* @param string $text
* @return string
*/
function linkify($text)
{
$regex = "#(http|https|ftp|ftps)://[a-z0-9\-.]*[a-z0-9\-]+(/\S*)?#i";
return preg_replace($regex, '$0', $text);
}
function generate_link($text, $vars, $new_vars = array())
{
return '' . $text . '';
}//end generate_link()
function generate_url($vars, $new_vars = [])
{
return \LibreNMS\Util\Url::generate($vars, $new_vars);
}
function escape_quotes($text)
{
return str_replace('"', "\'", str_replace("'", "\'", $text));
}//end escape_quotes()
function generate_overlib_content($graph_array, $text)
{
$overlib_content = '
";
}
}
// fetches disks for a system
function get_disks($device)
{
return dbFetchRows('SELECT * FROM `ucd_diskio` WHERE device_id = ? ORDER BY diskio_descr', array($device));
}
/**
* Get the fail2ban jails for a device... just requires the device ID
* an empty return means either no jails or fail2ban is not in use
* @param $device_id
* @return array
*/
function get_fail2ban_jails($device_id)
{
$options = array(
'filter' => array(
'type' => array('=', 'fail2ban'),
),
);
$component = new LibreNMS\Component();
$f2bc = $component->getComponents($device_id, $options);
if (isset($f2bc[$device_id])) {
$id = $component->getFirstComponentID($f2bc, $device_id);
return json_decode($f2bc[$device_id][$id]['jails']);
}
return array();
}
/**
* Get the Postgres databases for a device... just requires the device ID
* an empty return means Postres is not in use
* @param $device_id
* @return array
*/
function get_postgres_databases($device_id)
{
$options = array(
'filter' => array(
'type' => array('=', 'postgres'),
),
);
$component = new LibreNMS\Component();
$pgc = $component->getComponents($device_id, $options);
if (isset($pgc[$device_id])) {
$id = $component->getFirstComponentID($pgc, $device_id);
return json_decode($pgc[$device_id][$id]['databases']);
}
return array();
}
/**
* Get all application data from the collected
* rrd files.
*
* @param array $device device for which we get the rrd's
* @param int $app_id application id on the device
* @param string $category which category of seafile graphs are searched
* @return array list of entry data
*/
function get_arrays_with_application($device, $app_id, $app_name, $category = null)
{
$entries = array();
if ($category) {
$pattern = sprintf('%s/%s-%s-%s-%s-*.rrd', get_rrd_dir($device['hostname']), 'app', $app_name, $app_id, $category);
} else {
$pattern = sprintf('%s/%s-%s-%s-*.rrd', get_rrd_dir($device['hostname']), 'app', $app_name, $app_id);
}
foreach (glob($pattern) as $rrd) {
$filename = basename($rrd, '.rrd');
list(,,, $entry) = explode("-", $filename, 4);
if ($entry) {
array_push($entries, $entry);
}
}
return $entries;
}
/**
* Get all certificate names from the collected
* rrd files.
*
* @param array $device device for which we get the rrd's
* @param int $app_id application id on the device
* @return array list of certificate names
*/
function get_domains_with_certificates($device, $app_id)
{
$app_name = 'certificate';
return get_arrays_with_application($device, $app_id, $app_name);
}
/**
* Get all seafile data from the collected
* rrd files.
*
* @param array $device device for which we get the rrd's
* @param int $app_id application id on the device
* @param string $category which category of seafile graphs are searched
* @return array list of seafile data
*/
function get_arrays_with_seafile($device, $app_id, $category)
{
$app_name = 'seafile';
return get_arrays_with_application($device, $app_id, $app_name, $category);
}
/**
* Get all mdadm arrays from the collected
* rrd files.
*
* @param array $device device for which we get the rrd's
* @param int $app_id application id on the device
* @return array list of raid-arrays
*/
function get_arrays_with_mdadm($device, $app_id)
{
$app_name = 'mdadm';
return get_arrays_with_application($device, $app_id, $app_name);
}
/**
* Get all disks (disk serial numbers) from the collected
* rrd files.
*
* @param array $device device for which we get the rrd's
* @param int $app_id application id on the device
* @return array list of disks
*/
function get_disks_with_smart($device, $app_id)
{
$app_name = 'smart';
return get_arrays_with_application($device, $app_id, $app_name);
}
/**
* Gets all dashboards the user can access
* adds in the keys:
* username - the username of the owner of each dashboard
* default - the default dashboard for the logged in user
*
* @param int $user_id optionally get list for another user
* @return array list of dashboards
*/
function get_dashboards($user_id = null)
{
$user = is_null($user_id) ? Auth::user() : \App\Models\User::find($user_id);
$default = get_user_pref('dashboard');
return \App\Models\Dashboard::allAvailable($user)->with('user')->get()->map(function ($dashboard) use ($default) {
$dash = $dashboard->toArray();
$dash['username'] = $dashboard->user ? $dashboard->user->username : '';
$dash['default'] = $default == $dashboard->dashboard_id;
return $dash;
})->keyBy('dashboard_id')->all();
}
/**
* Return stacked graphs information
*
* @param string $transparency value of desired transparency applied to rrdtool options (values 01 - 99)
* @return array containing transparency and stacked setup
*/
function generate_stacked_graphs($transparency = '88')
{
if (Config::get('webui.graph_stacked') == true) {
return array('transparency' => $transparency, 'stacked' => '1');
} else {
return array('transparency' => '', 'stacked' => '-1');
}
}
/**
* Parse AT time spec, does not handle the entire spec.
* @param string $time
* @return int
*/
function parse_at_time($time)
{
if (is_numeric($time)) {
return $time < 0 ? time() + $time : intval($time);
}
if (preg_match('/^[+-]\d+[hdmy]$/', $time)) {
$units = [
'm' => 60,
'h' => 3600,
'd' => 86400,
'y' => 31557600,
];
$value = substr($time, 1, -1);
$unit = substr($time, -1);
$offset = ($time[0] == '-' ? -1 : 1) * $units[$unit] * $value;
return time() + $offset;
}
return (int)strtotime($time);
}
/**
* Get the ZFS pools for a device... just requires the device ID
* an empty return means ZFS is not in use or there are currently no pools
* @param $device_id
* @return array
*/
function get_zfs_pools($device_id)
{
$options=array(
'filter' => array(
'type' => array('=', 'zfs'),
),
);
$component=new LibreNMS\Component();
$zfsc=$component->getComponents($device_id, $options);
if (isset($zfsc[$device_id])) {
$id = $component->getFirstComponentID($zfsc, $device_id);
return json_decode($zfsc[$device_id][$id]['pools']);
}
return array();
}
/**
* Get the ports for a device... just requires the device ID
* an empty return means portsactivity is not in use or there are currently no ports
* @param $device_id
* @return array
*/
function get_portactivity_ports($device_id)
{
$options=array(
'filter' => array(
'type' => array('=', 'portsactivity'),
),
);
$component=new LibreNMS\Component();
$portsc=$component->getComponents($device_id, $options);
if (isset($portsc[$device_id])) {
$id = $component->getFirstComponentID($portsc, $device_id);
return json_decode($portsc[$device_id][$id]['ports']);
}
return array();
}
/**
* 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
* @param array device
* @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;
}
/**
* Returns state generic label from value with optional text
*/
function get_state_label($sensor)
{
$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 = ? ', array($sensor['sensor_id'], $sensor['sensor_current']));
switch ($state_translation['state_generic_value']) {
case 0: // OK
$state_text = $state_translation['state_descr'] ?: "OK";
$state_label = "label-success";
break;
case 1: // Warning
$state_text = $state_translation['state_descr'] ?: "Warning";
$state_label = "label-warning";
break;
case 2: // Critical
$state_text = $state_translation['state_descr'] ?: "Critical";
$state_label = "label-danger";
break;
case 3: // Unknown
default:
$state_text = $state_translation['state_descr'] ?: "Unknown";
$state_label = "label-default";
}
return "$state_text";
}
/**
* Get sensor label and state color
* @param array $sensor
* @param string $type sensors or wireless
* @return string
*/
function get_sensor_label_color($sensor, $type = 'sensors')
{
$label_style = "label-success";
if (is_null($sensor)) {
return "label-unknown";
}
if (!is_null($sensor['sensor_limit_warn']) && $sensor['sensor_current'] > $sensor['sensor_limit_warn']) {
$label_style = "label-warning";
}
if (!is_null($sensor['sensor_limit_low_warn']) && $sensor['sensor_current'] < $sensor['sensor_limit_low_warn']) {
$label_style = "label-warning";
}
if (!is_null($sensor['sensor_limit']) && $sensor['sensor_current'] > $sensor['sensor_limit']) {
$label_style = "label-danger";
}
if (!is_null($sensor['sensor_limit_low']) && $sensor['sensor_current'] < $sensor['sensor_limit_low']) {
$label_style = "label-danger";
}
$unit = __("$type.{$sensor['sensor_class']}.unit");
if ($sensor['sensor_class'] == 'runtime') {
$sensor['sensor_current'] = formatUptime($sensor['sensor_current'] * 60, 'short');
return "".trim($sensor['sensor_current'])."";
}
return "".trim(format_si($sensor['sensor_current']).$unit)."";
}
/**
* @params int unix time
* @params int seconds
* @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
* @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
* @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);
}
return $nfsen_hostname;
}
/**
* @params string hostname
* @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;
}
}
}