Files
librenms-librenms/includes/common.php
T

185 lines
4.1 KiB
PHP
Raw Normal View History

2009-09-07 11:07:59 +00:00
<?php
2009-05-07 13:47:51 +00:00
## Common Functions
function get_port_by_id($port_id)
{
2011-03-23 09:54:56 +00:00
if (is_numeric($port_id))
{
$port = mysql_fetch_assoc(mysql_query("SELECT * FROM `ports` WHERE `interface_id` = '".$port_id."'"));
}
if (is_array($port))
{
return $port;
} else {
return FALSE;
}
2010-08-01 14:17:06 +00:00
}
2010-08-01 14:17:06 +00:00
function get_application_by_id($application_id)
{
2011-03-23 09:54:56 +00:00
if (is_numeric($application_id))
{
$application = mysql_fetch_assoc(mysql_query("SELECT * FROM `applications` WHERE `app_id` = '".$application_id."'"));
}
if (is_array($application))
{
return $application;
} else {
return FALSE;
}
2010-08-01 14:17:06 +00:00
}
function get_sensor_by_id($sensor_id)
{
2011-03-23 09:54:56 +00:00
if (is_numeric($sensor_id))
{
$sensor = mysql_fetch_assoc(mysql_query("SELECT * FROM `sensors` WHERE `sensor_id` = '".$sensor_id."'"));
}
if (is_array($sensor))
{
return $sensor;
} else {
return FALSE;
}
2010-08-01 14:17:06 +00:00
}
2011-03-11 18:03:49 +00:00
function get_device_id_by_interface_id($interface_id)
{
2011-03-23 09:54:56 +00:00
if (is_numeric($interface_id))
{
$device_id = mysql_result(mysql_query("SELECT `device_id` FROM `ports` WHERE `interface_id` = '".$interface_id."'"),0);
}
if (is_numeric($device_id))
{
return $device_id;
} else {
return FALSE;
}
}
function ifclass($ifOperStatus, $ifAdminStatus)
{
2011-03-23 09:54:56 +00:00
$ifclass = "interface-upup";
2011-03-11 18:03:49 +00:00
2011-03-23 09:54:56 +00:00
if ($ifAdminStatus == "down") { $ifclass = "interface-admindown"; }
if ($ifAdminStatus == "up" && $ifOperStatus== "down") { $ifclass = "interface-updown"; }
if ($ifAdminStatus == "up" && $ifOperStatus== "up") { $ifclass = "interface-upup"; }
2011-03-11 18:03:49 +00:00
2011-03-23 09:54:56 +00:00
return $ifclass;
}
function device_by_id_cache($device_id)
{
2011-03-23 09:54:56 +00:00
global $device_cache;
2011-03-11 18:03:49 +00:00
2011-03-23 09:54:56 +00:00
if (is_array($device_cache[$device_id]))
{
$device = $device_cache[$device_id];
} else {
$device = mysql_fetch_assoc(mysql_query("SELECT * FROM `devices` WHERE `device_id` = '".$device_id."'"));
$device_cache[$device_id] = $device;
}
2011-03-11 18:03:49 +00:00
2011-03-23 09:54:56 +00:00
return $device;
}
2011-03-11 18:03:49 +00:00
function truncate($substring, $max = 50, $rep = '...')
{
2011-03-23 09:54:56 +00:00
if (strlen($substring) < 1){ $string = $rep; } else { $string = $substring; }
$leave = $max - strlen ($rep);
if (strlen($string) > $max){ return substr_replace($string, $rep, $leave); } else { return $string; }
2009-10-28 13:49:37 +00:00
}
2011-03-11 18:03:49 +00:00
function mres($string)
{ // short function wrapper because the real one is stupidly long and ugly. aestetics.
2011-03-23 09:54:56 +00:00
return mysql_real_escape_string($string);
2009-06-19 10:43:02 +00:00
}
2011-03-11 18:03:49 +00:00
function getifhost($id)
{
2011-03-23 09:54:56 +00:00
$sql = mysql_query("SELECT `device_id` from `ports` WHERE `interface_id` = '$id'");
$result = @mysql_result($sql, 0);
2011-03-11 18:03:49 +00:00
2011-03-23 09:54:56 +00:00
return $result;
2009-05-11 13:43:59 +00:00
}
2011-03-11 18:03:49 +00:00
function gethostbyid($id)
{
2011-03-23 09:54:56 +00:00
$sql = mysql_query("SELECT `hostname` FROM `devices` WHERE `device_id` = '$id'");
$result = @mysql_result($sql, 0);
2011-03-11 18:03:49 +00:00
2011-03-23 09:54:56 +00:00
return $result;
2009-05-07 13:47:51 +00:00
}
function strgen ($length = 16)
{
2011-03-23 09:54:56 +00:00
$entropy = array(0,1,2,3,4,5,6,7,8,9,'a','A','b','B','c','C','d','D','e',
'E','f','F','g','G','h','H','i','I','j','J','k','K','l','L','m','M','n',
'N','o','O','p','P','q','Q','r','R','s','S','t','T','u','U','v','V','w',
'W','x','X','y','Y','z','Z');
$string = "";
2011-03-11 18:03:49 +00:00
2011-03-23 09:54:56 +00:00
for ($i=0; $i<$length; $i++)
{
$key = mt_rand(0,61);
$string .= $entropy[$key];
}
2011-03-11 18:03:49 +00:00
2011-03-23 09:54:56 +00:00
return $string;
2009-05-07 13:47:51 +00:00
}
2011-03-11 18:03:49 +00:00
function getpeerhost($id)
{
2011-03-23 09:54:56 +00:00
$sql = mysql_query("SELECT `device_id` from `bgpPeers` WHERE `bgpPeer_id` = '$id'");
$result = @mysql_result($sql, 0);
2011-03-11 18:03:49 +00:00
2011-03-23 09:54:56 +00:00
return $result;
2009-05-07 13:47:51 +00:00
}
2011-03-11 18:03:49 +00:00
function getifindexbyid($id)
{
2011-03-23 09:54:56 +00:00
$sql = mysql_query("SELECT `ifIndex` FROM `ports` WHERE `interface_id` = '$id'");
$result = @mysql_result($sql, 0);
2011-03-11 18:03:49 +00:00
2011-03-23 09:54:56 +00:00
return $result;
2009-05-07 13:47:51 +00:00
}
2011-03-11 18:03:49 +00:00
function getifbyid($id)
{
2011-03-23 09:54:56 +00:00
$sql = mysql_query("SELECT * FROM `ports` WHERE `interface_id` = '$id'");
$result = @mysql_fetch_array($sql);
2011-03-11 18:03:49 +00:00
2011-03-23 09:54:56 +00:00
return $result;
}
2011-03-11 18:03:49 +00:00
function getifdescrbyid($id)
{
2011-03-23 09:54:56 +00:00
$sql = mysql_query("SELECT `ifDescr` FROM `ports` WHERE `interface_id` = '$id'");
$result = @mysql_result($sql, 0);
2011-03-11 18:03:49 +00:00
2011-03-23 09:54:56 +00:00
return $result;
2009-05-07 13:47:51 +00:00
}
2011-03-11 18:03:49 +00:00
function getidbyname($domain)
{
2011-03-23 09:54:56 +00:00
$sql = mysql_query("SELECT `device_id` FROM `devices` WHERE `hostname` = '$domain'");
$result = @mysql_result($sql, 0);
2011-03-11 18:03:49 +00:00
2011-03-23 09:54:56 +00:00
return $result;
2009-05-07 13:47:51 +00:00
}
2011-03-11 18:03:49 +00:00
function gethostosbyid($id)
{
2011-03-23 09:54:56 +00:00
$sql = mysql_query("SELECT `os` FROM `devices` WHERE `device_id` = '$id'");
$result = @mysql_result($sql, 0);
2011-03-11 18:03:49 +00:00
2011-03-23 09:54:56 +00:00
return $result;
2009-05-07 13:47:51 +00:00
}
2011-03-15 15:39:57 +00:00
function safename($name)
{
2011-03-23 09:54:56 +00:00
return preg_replace('/[^a-zA-Z0-9,._\-]/', '_', $name);
}
2009-05-07 13:47:51 +00:00
2011-03-23 09:54:56 +00:00
?>