mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
replace mysql_ with db* (update with caution, it works for me but...)
git-svn-id: http://www.observium.org/svn/observer/trunk@2282 61d68cd4-352d-0410-923a-c4978735b2b8
This commit is contained in:
@@ -95,12 +95,7 @@ if (file_exists('.svn'))
|
|||||||
{
|
{
|
||||||
list(,$dbu_rev) = preg_split('/: /',@shell_exec('svn info database-update.sql|grep ^Revision'));
|
list(,$dbu_rev) = preg_split('/: /',@shell_exec('svn info database-update.sql|grep ^Revision'));
|
||||||
|
|
||||||
$device_query = mysql_query("SELECT revision FROM `dbSchema`");
|
if ($db_rev = @dbFetchCell("SELECT revision FROM `dbSchema`")) {} else
|
||||||
if ($rev = @mysql_fetch_assoc($device_query))
|
|
||||||
{
|
|
||||||
$db_rev = $rev['revision'];
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
$db_rev = 0;
|
$db_rev = 0;
|
||||||
}
|
}
|
||||||
@@ -142,19 +137,18 @@ if (file_exists('.svn'))
|
|||||||
shell_exec("scripts/update-sql.php database-update.sql");
|
shell_exec("scripts/update-sql.php database-update.sql");
|
||||||
if ($db_rev == 0)
|
if ($db_rev == 0)
|
||||||
{
|
{
|
||||||
mysql_query("INSERT INTO dbSchema VALUES ($dbu_rev)");
|
dbInsert(array('revision' => $dbu_rev), 'dbSchema');
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
mysql_query("UPDATE dbSchema set revision=$dbu_rev");
|
dbUpdate(array('revision' => $dbu_rev), 'dbSchema');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$discovered_devices = 0;
|
$discovered_devices = 0;
|
||||||
|
|
||||||
$device_query = mysql_query("SELECT * FROM `devices` WHERE status = 1 AND disabled = 0 $where ORDER BY device_id DESC");
|
foreach (dbFetch("SELECT * FROM `devices` WHERE status = 1 AND disabled = 0 $where ORDER BY device_id DESC") as $device)
|
||||||
while ($device = mysql_fetch_assoc($device_query))
|
|
||||||
{
|
{
|
||||||
discover_device($device, $options);
|
discover_device($device, $options);
|
||||||
}
|
}
|
||||||
@@ -173,8 +167,8 @@ function discover_device($device, $options)
|
|||||||
echo($device['hostname'] . " ".$device['device_id']." ".$device['os']." ");
|
echo($device['hostname'] . " ".$device['device_id']." ".$device['os']." ");
|
||||||
if ($device['os'] != strtolower($device['os']))
|
if ($device['os'] != strtolower($device['os']))
|
||||||
{
|
{
|
||||||
mysql_query("UPDATE `devices` SET `os` = '".strtolower($device['os'])."' WHERE device_id = '".$device['device_id']."'");
|
|
||||||
$device['os'] = strtolower($device['os']);
|
$device['os'] = strtolower($device['os']);
|
||||||
|
dbUpdate(array('os' => $device['os']), 'devices', '`device_id` = ?', array($device['device_id']));
|
||||||
echo("OS lowercased.");
|
echo("OS lowercased.");
|
||||||
}
|
}
|
||||||
if ($config['os'][$device['os']]['group'])
|
if ($config['os'][$device['os']]['group'])
|
||||||
@@ -218,11 +212,7 @@ function discover_device($device, $options)
|
|||||||
|
|
||||||
$device_end = utime(); $device_run = $device_end - $device_start; $device_time = substr($device_run, 0, 5);
|
$device_end = utime(); $device_run = $device_end - $device_start; $device_time = substr($device_run, 0, 5);
|
||||||
|
|
||||||
$update_query = "UPDATE `devices` SET ";
|
dbUpdate(array('last_discovered' => array('NOW()'), 'type' => $device['type'], 'last_discovered_timetaken' => $device_time), 'devices', '`device_id` = ?', array($device['device_id']));
|
||||||
$update_query .= " `last_discovered` = NOW(), `type` = '" . $device['type'] . "'";
|
|
||||||
$update_query .= ", `last_discovered_timetaken` = '$device_time'";
|
|
||||||
$update_query .= " WHERE `device_id` = '" . $device['device_id'] . "'";
|
|
||||||
$update_result = mysql_query($update_query);
|
|
||||||
|
|
||||||
echo("Discovered in $device_time seconds\n");
|
echo("Discovered in $device_time seconds\n");
|
||||||
|
|
||||||
@@ -236,8 +226,7 @@ $proctime = substr($run, 0, 5);
|
|||||||
|
|
||||||
if ($discovered_devices)
|
if ($discovered_devices)
|
||||||
{
|
{
|
||||||
mysql_query("INSERT INTO `perf_times` (`type`, `doing`, `start`, `duration`, `devices`)
|
dbInsert(array('type' => 'discover', 'doing' => $doing, 'start' => $start, 'duration' => $proctime, 'devices' => $discovered_devices), 'perf_times');
|
||||||
VALUES ('discover', '$doing', '$start', '$proctime', '$discovered_devices')");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$string = $argv[0] . " $doing " . date("F j, Y, G:i") . " - $discovered_devices devices discovered in $proctime secs";
|
$string = $argv[0] . " $doing " . date("F j, Y, G:i") . " - $discovered_devices devices discovered in $proctime secs";
|
||||||
|
@@ -33,7 +33,7 @@ function get_port_by_id($port_id)
|
|||||||
{
|
{
|
||||||
if (is_numeric($port_id))
|
if (is_numeric($port_id))
|
||||||
{
|
{
|
||||||
$port = mysql_fetch_assoc(mysql_query("SELECT * FROM `ports` WHERE `interface_id` = '".$port_id."'"));
|
$port = dbFetchRow("SELECT * FROM `ports` WHERE `interface_id` = ?", array($port_id));
|
||||||
}
|
}
|
||||||
if (is_array($port))
|
if (is_array($port))
|
||||||
{
|
{
|
||||||
@@ -47,7 +47,7 @@ function get_application_by_id($application_id)
|
|||||||
{
|
{
|
||||||
if (is_numeric($application_id))
|
if (is_numeric($application_id))
|
||||||
{
|
{
|
||||||
$application = mysql_fetch_assoc(mysql_query("SELECT * FROM `applications` WHERE `app_id` = '".$application_id."'"));
|
$application = dbFetchRow("SELECT * FROM `applications` WHERE `app_id` = ?", array($application_id));
|
||||||
}
|
}
|
||||||
if (is_array($application))
|
if (is_array($application))
|
||||||
{
|
{
|
||||||
@@ -61,7 +61,7 @@ function get_sensor_by_id($sensor_id)
|
|||||||
{
|
{
|
||||||
if (is_numeric($sensor_id))
|
if (is_numeric($sensor_id))
|
||||||
{
|
{
|
||||||
$sensor = mysql_fetch_assoc(mysql_query("SELECT * FROM `sensors` WHERE `sensor_id` = '".$sensor_id."'"));
|
$sensor = dbFetchRow("SELECT * FROM `sensors` WHERE `sensor_id` = ?", array($sensor_id));
|
||||||
}
|
}
|
||||||
if (is_array($sensor))
|
if (is_array($sensor))
|
||||||
{
|
{
|
||||||
@@ -75,7 +75,7 @@ function get_device_id_by_interface_id($interface_id)
|
|||||||
{
|
{
|
||||||
if (is_numeric($interface_id))
|
if (is_numeric($interface_id))
|
||||||
{
|
{
|
||||||
$device_id = mysql_result(mysql_query("SELECT `device_id` FROM `ports` WHERE `interface_id` = '".$interface_id."'"),0);
|
$device_id = dbFetchCell("SELECT `device_id` FROM `ports` WHERE `interface_id` = ?", array($interface_id));
|
||||||
}
|
}
|
||||||
if (is_numeric($device_id))
|
if (is_numeric($device_id))
|
||||||
{
|
{
|
||||||
@@ -104,7 +104,7 @@ function device_by_id_cache($device_id)
|
|||||||
{
|
{
|
||||||
$device = $device_cache[$device_id];
|
$device = $device_cache[$device_id];
|
||||||
} else {
|
} else {
|
||||||
$device = mysql_fetch_assoc(mysql_query("SELECT * FROM `devices` WHERE `device_id` = '".$device_id."'"));
|
$device = dbFetchRow("SELECT * FROM `devices` WHERE `device_id` = ?", array($device_id));
|
||||||
if (get_dev_attrib($device,'override_sysLocation_bool'))
|
if (get_dev_attrib($device,'override_sysLocation_bool'))
|
||||||
{
|
{
|
||||||
$device['real_location'] = $device['location'];
|
$device['real_location'] = $device['location'];
|
||||||
@@ -130,18 +130,12 @@ function mres($string)
|
|||||||
|
|
||||||
function getifhost($id)
|
function getifhost($id)
|
||||||
{
|
{
|
||||||
$sql = mysql_query("SELECT `device_id` from `ports` WHERE `interface_id` = '$id'");
|
return dbFetchCell("SELECT `device_id` from `ports` WHERE `interface_id` = ?", array($id));
|
||||||
$result = @mysql_result($sql, 0);
|
|
||||||
|
|
||||||
return $result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function gethostbyid($id)
|
function gethostbyid($id)
|
||||||
{
|
{
|
||||||
$sql = mysql_query("SELECT `hostname` FROM `devices` WHERE `device_id` = '$id'");
|
return dbFetchCell("SELECT `hostname` FROM `devices` WHERE `device_id` = ?", array($id));
|
||||||
$result = @mysql_result($sql, 0);
|
|
||||||
|
|
||||||
return $result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function strgen ($length = 16)
|
function strgen ($length = 16)
|
||||||
@@ -163,58 +157,37 @@ function strgen ($length = 16)
|
|||||||
|
|
||||||
function getpeerhost($id)
|
function getpeerhost($id)
|
||||||
{
|
{
|
||||||
$sql = mysql_query("SELECT `device_id` from `bgpPeers` WHERE `bgpPeer_id` = '$id'");
|
return dbFetchCell("SELECT `device_id` from `bgpPeers` WHERE `bgpPeer_id` = ?", array($id));
|
||||||
$result = @mysql_result($sql, 0);
|
|
||||||
|
|
||||||
return $result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getifindexbyid($id)
|
function getifindexbyid($id)
|
||||||
{
|
{
|
||||||
$sql = mysql_query("SELECT `ifIndex` FROM `ports` WHERE `interface_id` = '$id'");
|
return dbFetchCell("SELECT `ifIndex` FROM `ports` WHERE `interface_id` = ?", array($id));
|
||||||
$result = @mysql_result($sql, 0);
|
|
||||||
|
|
||||||
return $result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function get_port_by_ifIndex($device, $ifIndex)
|
function get_port_by_ifIndex($device, $ifIndex)
|
||||||
{
|
{
|
||||||
$sql = mysql_query("SELECT * FROM `ports` WHERE `device_id` = '".$device['device_id']."' AND `ifIndex` = '$ifIndex'");
|
return dbFetchCell("SELECT * FROM `ports` WHERE `device_id` = ? AND `ifIndex` = ?", array($device['device_id'], $ifIndex));
|
||||||
$result = @mysql_fetch_assoc($sql);
|
|
||||||
|
|
||||||
return $result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getifbyid($id)
|
function getifbyid($id)
|
||||||
{
|
{
|
||||||
$sql = mysql_query("SELECT * FROM `ports` WHERE `interface_id` = '$id'");
|
return dbFetchCell("SELECT * FROM `ports` WHERE `interface_id` = ?", array($id));
|
||||||
$result = @mysql_fetch_assoc($sql);
|
|
||||||
|
|
||||||
return $result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getifdescrbyid($id)
|
function getifdescrbyid($id)
|
||||||
{
|
{
|
||||||
$sql = mysql_query("SELECT `ifDescr` FROM `ports` WHERE `interface_id` = '$id'");
|
return dbFetchCell("SELECT `ifDescr` FROM `ports` WHERE `interface_id` = ?", array($id));
|
||||||
$result = @mysql_result($sql, 0);
|
|
||||||
|
|
||||||
return $result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getidbyname($domain)
|
function getidbyname($domain)
|
||||||
{
|
{
|
||||||
$sql = mysql_query("SELECT `device_id` FROM `devices` WHERE `hostname` = '$domain'");
|
return dbFetchCell("SELECT `device_id` FROM `devices` WHERE `hostname` = ?", mres($domain));
|
||||||
$result = @mysql_result($sql, 0);
|
|
||||||
|
|
||||||
return $result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function gethostosbyid($id)
|
function gethostosbyid($id)
|
||||||
{
|
{
|
||||||
$sql = mysql_query("SELECT `os` FROM `devices` WHERE `device_id` = '$id'");
|
return dbFetchCell("SELECT `os` FROM `devices` WHERE `device_id` = ?", array($id));
|
||||||
$result = @mysql_result($sql, 0);
|
|
||||||
|
|
||||||
return $result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function safename($name)
|
function safename($name)
|
||||||
@@ -234,27 +207,21 @@ function zeropad($num, $length = 2)
|
|||||||
|
|
||||||
function set_dev_attrib($device, $attrib_type, $attrib_value)
|
function set_dev_attrib($device, $attrib_type, $attrib_value)
|
||||||
{
|
{
|
||||||
$count_sql = "SELECT COUNT(*) FROM devices_attribs WHERE `device_id` = '" . mres($device['device_id']) . "' AND `attrib_type` = '$attrib_type'";
|
if (dbFetchCell("SELECT COUNT(*) FROM devices_attribs WHERE `device_id` = ? AND `attrib_type` = ?", array($device['device_id'],$attrib_type)))
|
||||||
if (mysql_result(mysql_query($count_sql),0))
|
|
||||||
{
|
{
|
||||||
$update_sql = "UPDATE devices_attribs SET attrib_value = '$attrib_value' WHERE `device_id` = '" . mres($device['device_id']) . "' AND `attrib_type` = '$attrib_type'";
|
$return = dbUpdate(array('attrib_value' => $attrib_value), 'devices_attribs', 'device_id=? and attrib_type=?', array($device['device_id'], $attrib_type));
|
||||||
mysql_query($update_sql);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
$insert_sql = "INSERT INTO devices_attribs (`device_id`, `attrib_type`, `attrib_value`) VALUES ('" . mres($device['device_id'])."', '$attrib_type', '$attrib_value')";
|
$return = dbInsert(array('device_id' => $device['device_id'], 'attrib_type' => $attrib_type, 'attrib_value' => $attrib_value), 'devices_attribs');
|
||||||
mysql_query($insert_sql);
|
|
||||||
}
|
}
|
||||||
|
return $return;
|
||||||
return mysql_affected_rows();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function get_dev_attribs($device)
|
function get_dev_attribs($device)
|
||||||
{
|
{
|
||||||
$attribs = array();
|
$attribs = array();
|
||||||
$sql = "SELECT * FROM devices_attribs WHERE `device_id` = '" . mres($device) . "'";
|
foreach(dbFetch("SELECT * FROM devices_attribs WHERE `device_id` = ?", array($device)) as $entry)
|
||||||
$query = mysql_query($sql);
|
|
||||||
while($entry = mysql_fetch_assoc($query))
|
|
||||||
{
|
{
|
||||||
$attribs[$entry['attrib_type']] = $entry['attrib_value'];
|
$attribs[$entry['attrib_type']] = $entry['attrib_value'];
|
||||||
}
|
}
|
||||||
@@ -263,8 +230,7 @@ function get_dev_attribs($device)
|
|||||||
|
|
||||||
function get_dev_attrib($device, $attrib_type)
|
function get_dev_attrib($device, $attrib_type)
|
||||||
{
|
{
|
||||||
$sql = "SELECT attrib_value FROM devices_attribs WHERE `device_id` = '" . mres($device['device_id']) . "' AND `attrib_type` = '$attrib_type'";
|
if ($row = dbFetchRow("SELECT attrib_value FROM devices_attribs WHERE `device_id` = ? AND `attrib_type` = ?", array($device['device_id'], $attrib_type)))
|
||||||
if ($row = mysql_fetch_assoc(mysql_query($sql)))
|
|
||||||
{
|
{
|
||||||
return $row['attrib_value'];
|
return $row['attrib_value'];
|
||||||
}
|
}
|
||||||
@@ -276,8 +242,7 @@ function get_dev_attrib($device, $attrib_type)
|
|||||||
|
|
||||||
function del_dev_attrib($device, $attrib_type)
|
function del_dev_attrib($device, $attrib_type)
|
||||||
{
|
{
|
||||||
$sql = "DELETE FROM devices_attribs WHERE `device_id` = '" . mres($device['device_id']) . "' AND `attrib_type` = '$attrib_type'";
|
return dbDelete('devices_attribs', "`device_id` = ? AND `attrib_type` = ?", array($device['device_id'], $attrib_type));
|
||||||
return mysql_query($sql);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function formatRates($rate)
|
function formatRates($rate)
|
||||||
|
@@ -109,9 +109,10 @@ function interface_errors($rrd_file, $period = '-1d') // Returns the last in/out
|
|||||||
|
|
||||||
function getImage($host)
|
function getImage($host)
|
||||||
{
|
{
|
||||||
|
## FIXME why not pass $device? (my shitty ancient code here!)
|
||||||
|
|
||||||
global $config;
|
global $config;
|
||||||
$sql = "SELECT * FROM `devices` WHERE `device_id` = '$host'";
|
$data = dbFetchRow("SELECT * FROM `devices` WHERE `device_id` = ?", array($host));
|
||||||
$data = mysql_fetch_assoc(mysql_query($sql));
|
|
||||||
$type = strtolower($data['os']);
|
$type = strtolower($data['os']);
|
||||||
if ($config['os'][$type]['icon'] && file_exists($config['html_dir'] . "/images/os/" . $config['os'][$type]['icon'] . ".png"))
|
if ($config['os'][$type]['icon'] && file_exists($config['html_dir'] . "/images/os/" . $config['os'][$type]['icon'] . ".png"))
|
||||||
{
|
{
|
||||||
@@ -138,9 +139,9 @@ function renamehost($id, $new, $source = 'console')
|
|||||||
{
|
{
|
||||||
global $config;
|
global $config;
|
||||||
|
|
||||||
$host = dbFetchCell("SELECT hostname FROM devices WHERE device_id = '$id'");
|
$host = dbFetchCell("SELECT `hostname` FROM `devices` WHERE `device_id` = ?", array($id));
|
||||||
rename($config['rrd_dir']."/$host",$config['rrd_dir']."/$new");
|
rename($config['rrd_dir']."/$host",$config['rrd_dir']."/$new");
|
||||||
mysql_query("UPDATE devices SET hostname = '$new' WHERE device_id = '$id'");
|
$return = dbUpdate(array('hostname' => $new), 'devices', 'device_id=?', array($id));
|
||||||
log_event("Hostname changed -> $new ($source)", $id, 'system');
|
log_event("Hostname changed -> $new ($source)", $id, 'system');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -148,17 +149,18 @@ function delete_port($int_id)
|
|||||||
{
|
{
|
||||||
global $config;
|
global $config;
|
||||||
|
|
||||||
$interface = mysql_fetch_assoc(mysql_query("SELECT * FROM `ports` AS P, `devices` AS D WHERE P.interface_id = '".$int_id."' AND D.device_id = P.device_id"));
|
$interface = dbFetchRow("SELECT * FROM `ports` AS P, `devices` AS D WHERE P.interface_id = ? AND D.device_id = P.device_id", array($int_id));
|
||||||
mysql_query("DELETE from `adjacencies` WHERE `interface_id` = '$int_id'");
|
|
||||||
mysql_query("DELETE from `links` WHERE `local_interface_id` = '$int_id'");
|
$interface_tables = array('adjacencies', 'ipaddr', 'ip6adjacencies', 'ip6addr', 'mac_accounting', 'bill_ports', 'pseudowires', 'ports');
|
||||||
mysql_query("DELETE from `links` WHERE `remote_interface_id` = '$int_id'");
|
|
||||||
mysql_query("DELETE from `ipaddr` WHERE `interface_id` = '$int_id'");
|
foreach($interface_tables as $table) {
|
||||||
mysql_query("DELETE from `ip6adjacencies` WHERE `interface_id` = '$int_id'");
|
dbDelete($table, "`interface_id` = ?", array($int_id));
|
||||||
mysql_query("DELETE from `ip6addr` WHERE `interface_id` = '$int_id'");
|
}
|
||||||
mysql_query("DELETE from `mac_accounting` WHERE `interface_id` = '$int_id'");
|
|
||||||
mysql_query("DELETE FROM `bill_ports` WHERE `port_id` = '$int_id'");
|
dbDelete('links', "`local_interface_id` = ?", array($int_id));
|
||||||
mysql_query("DELETE from `pseudowires` WHERE `interface_id` = '$int_id'");
|
dbDelete('links', "`remote_interface_id` = ?", array($int_id));
|
||||||
mysql_query("DELETE FROM `ports` WHERE `interface_id` = '$int_id'");
|
dbDelete('bill_ports', "`port_id` = ?", array($int_id));
|
||||||
|
|
||||||
unlink(trim($config['rrd_dir'])."/".trim($interface['hostname'])."/".$interface['ifIndex'].".rrd");
|
unlink(trim($config['rrd_dir'])."/".trim($interface['hostname'])."/".$interface['ifIndex'].".rrd");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -166,34 +168,25 @@ function delete_device($id)
|
|||||||
{
|
{
|
||||||
global $config;
|
global $config;
|
||||||
|
|
||||||
$host = dbFetchCell("SELECT hostname FROM devices WHERE device_id = '$id'");
|
$host = dbFetchCell("SELECT hostname FROM devices WHERE device_id = ?", array($id));
|
||||||
mysql_query("DELETE FROM `devices` WHERE `device_id` = '$id'");
|
|
||||||
$int_query = mysql_query("SELECT * FROM `ports` WHERE `device_id` = '$id'");
|
dbDelete('devices', "`device_id` = ?", array($id));
|
||||||
while ($int_data = mysql_fetch_assoc($int_query))
|
|
||||||
|
foreach (dbFetch("SELECT * FROM `ports` WHERE `device_id` = ?", array($id)) as $int_data)
|
||||||
{
|
{
|
||||||
$int_if = $int_data['ifDescr'];
|
$int_if = $int_data['ifDescr'];
|
||||||
$int_id = $int_data['interface_id'];
|
$int_id = $int_data['interface_id'];
|
||||||
delete_port($int_id);
|
delete_port($int_id);
|
||||||
$ret .= "Removed interface $int_id ($int_if)\n";
|
$ret .= "Removed interface $int_id ($int_if)\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$device_tables = array('entPhysical', 'devices_attribs', 'devices_perms', 'bgpPeers', 'vlans', 'vrfs', 'storage', 'alerts', 'eventlog',
|
||||||
|
'syslog', 'ports', 'services', 'alerts', 'toner', 'frequency', 'current', 'sensors');
|
||||||
|
|
||||||
|
foreach($device_tables as $table) {
|
||||||
|
dbDelete($table, "`device_id` = ?", array($id));
|
||||||
|
}
|
||||||
|
|
||||||
mysql_query("DELETE FROM `entPhysical` WHERE `device_id` = '$id'");
|
|
||||||
mysql_query("DELETE FROM `devices_attribs` WHERE `device_id` = '$id'");
|
|
||||||
mysql_query("DELETE FROM `devices_perms` WHERE `device_id` = '$id'");
|
|
||||||
mysql_query("DELETE FROM `bgpPeers` WHERE `device_id` = '$id'");
|
|
||||||
mysql_query("DELETE FROM `vlans` WHERE `device_id` = '$id'");
|
|
||||||
mysql_query("DELETE FROM `vrfs` WHERE `device_id` = '$id'");
|
|
||||||
mysql_query("DELETE FROM `storage` WHERE `device_id` = '$id'");
|
|
||||||
mysql_query("DELETE FROM `alerts` WHERE `device_id` = '$id'");
|
|
||||||
mysql_query("DELETE FROM `eventlog` WHERE `host` = '$id'");
|
|
||||||
mysql_query("DELETE FROM `syslog` WHERE `device_id` = '$id'");
|
|
||||||
mysql_query("DELETE FROM `ports` WHERE `device_id` = '$id'");
|
|
||||||
mysql_query("DELETE FROM `services` WHERE `device_id` = '$id'");
|
|
||||||
mysql_query("DELETE FROM `alerts` WHERE `device_id` = '$id'");
|
|
||||||
mysql_query("DELETE FROM `toner` WHERE `device_id` = '$id'");
|
|
||||||
mysql_query("DELETE FROM `frequency` WHERE `device_id` = '$id'");
|
|
||||||
mysql_query("DELETE FROM `current` WHERE `device_id` = '$id'");
|
|
||||||
mysql_query("DELETE FROM `sensors` WHERE `device_id` = '$id'");
|
|
||||||
shell_exec("rm -rf ".trim($config['rrd_dir'])."/$host");
|
shell_exec("rm -rf ".trim($config['rrd_dir'])."/$host");
|
||||||
|
|
||||||
$ret = "Removed Device $host\n";
|
$ret = "Removed Device $host\n";
|
||||||
@@ -395,10 +388,7 @@ function isDomainResolves($domain)
|
|||||||
|
|
||||||
function hoststatus($id)
|
function hoststatus($id)
|
||||||
{
|
{
|
||||||
$sql = mysql_query("SELECT `status` FROM `devices` WHERE `device_id` = '$id'");
|
return dbFetchCell("SELECT `status` FROM `devices` WHERE `device_id` = ?", array($id));
|
||||||
$result = @mysql_result($sql, 0);
|
|
||||||
|
|
||||||
return $result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function match_network($nets, $ip, $first=false)
|
function match_network($nets, $ip, $first=false)
|
||||||
@@ -478,14 +468,6 @@ function get_astext($asn)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# FIXME DEPRECATED -- only used in dead file includes/polling/interfaces.inc.php - if we no longer need that one, this can go too.
|
|
||||||
function eventlog($eventtext,$device_id = "", $interface_id = "")
|
|
||||||
{
|
|
||||||
$event_query = "INSERT INTO eventlog (host, interface, datetime, message) VALUES (" . ($device_id ? $device_id : "NULL");
|
|
||||||
$event_query .= ", " . ($interface_id ? $interface_id : "NULL") . ", NOW(), '" . mysql_escape_string($eventtext) . "')";
|
|
||||||
mysql_query($event_query);
|
|
||||||
}
|
|
||||||
|
|
||||||
# Use this function to write to the eventlog table
|
# Use this function to write to the eventlog table
|
||||||
function log_event($text, $device = NULL, $type = NULL, $reference = NULL)
|
function log_event($text, $device = NULL, $type = NULL, $reference = NULL)
|
||||||
{
|
{
|
||||||
@@ -493,10 +475,14 @@ function log_event($text, $device = NULL, $type = NULL, $reference = NULL)
|
|||||||
|
|
||||||
if (!is_array($device)) { $device = device_by_id_cache($device); }
|
if (!is_array($device)) { $device = device_by_id_cache($device); }
|
||||||
|
|
||||||
$event_query = "INSERT INTO eventlog (host, reference, type, datetime, message) VALUES (" . ($device['device_id'] ? $device['device_id'] : "NULL");
|
$insert = array('host' => ($device['device_id'] ? $device['device_id'] : "NULL"),
|
||||||
$event_query .= ", '" . ($reference ? $reference : "NULL") . "', '" . ($type ? $type : "NULL") . "', NOW(), '" . mres($text) . "')";
|
'reference' => ($reference ? $reference : "NULL"),
|
||||||
if ($debug) { echo($event_query . "\n"); }
|
'type' => ($type ? $type : "NULL"),
|
||||||
mysql_query($event_query);
|
'datetime' => array("NOW()"),
|
||||||
|
'message' => $text);
|
||||||
|
|
||||||
|
return $dbInsert($insert, 'eventlog');
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function notify($device,$title,$message)
|
function notify($device,$title,$message)
|
||||||
|
@@ -1,14 +1,20 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
## FIXME who wrote this? so ugly :)
|
||||||
|
|
||||||
function add_service($service) {
|
function add_service($service) {
|
||||||
global $id;
|
global $id;
|
||||||
global $hostname;
|
global $hostname;
|
||||||
echo("$service ");
|
echo("$service ");
|
||||||
$sql = "INSERT INTO `services` (`device_id`,`service_ip`,`service_type`,`service_desc`,`service_param`,`service_ignore`)
|
|
||||||
VALUES ('" . mres($id). "','" . mres($hostname) . "','" . mres($service) . "',
|
|
||||||
'" . mres("auto discovered: $service") . "','" . mres("") . "','0')";
|
|
||||||
|
|
||||||
$query = mysql_query($sql);
|
#$sql = "INSERT INTO `services` (`device_id`,`service_ip`,`service_type`,`service_desc`,`service_param`,`service_ignore`)
|
||||||
|
# VALUES ('" . mres($id). "','" . mres($hostname) . "','" . mres($service) . "',
|
||||||
|
# '" . mres("auto discovered: $service") . "','" . mres("") . "','0')";
|
||||||
|
|
||||||
|
$insert = array('device_id' => $id, 'service_ip' => $hostname, 'service_type' => $service, 'service_desc' => "auto discovered: $service", 'service_param' => "", 'service_ignore' => "0");
|
||||||
|
|
||||||
|
return $dbInsert($insert, 'services');
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -914,15 +914,6 @@ if (!isset($config['mibdir']))
|
|||||||
}
|
}
|
||||||
$config['mib_dir'] = $config['mibdir'];
|
$config['mib_dir'] = $config['mibdir'];
|
||||||
|
|
||||||
if (isset($config['enable_nagios']) && $config['enable_nagios']) {
|
|
||||||
$nagios_link = mysql_connect($config['nagios_db_host'], $config['nagios_db_user'], $config['nagios_db_pass']);
|
|
||||||
if (!$nagios_link) {
|
|
||||||
echo("<h2>Nagios MySQL Error</h2>");
|
|
||||||
die;
|
|
||||||
}
|
|
||||||
$nagios_db = mysql_select_db($config['nagios_db_name'], $nagios_link);
|
|
||||||
}
|
|
||||||
|
|
||||||
# If we're on SSL, let's properly detect it
|
# If we're on SSL, let's properly detect it
|
||||||
if (isset($_SERVER['HTTPS']))
|
if (isset($_SERVER['HTTPS']))
|
||||||
{
|
{
|
||||||
|
@@ -25,8 +25,7 @@ function process_syslog ($entry, $update)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
$device_id_ip = @dbFetchCell("SELECT device_id FROM ipv4_addresses AS A, ports AS I WHERE
|
$device_id_ip = @dbFetchCell("SELECT device_id FROM ipv4_addresses AS A, ports AS I WHERE A.ipv4_address = '" . $entry['host']."' AND I.interface_id = A.interface_id");
|
||||||
A.ipv4_address = '" . $entry['host']."' AND I.interface_id = A.interface_id");
|
|
||||||
if ($device_id_ip)
|
if ($device_id_ip)
|
||||||
{
|
{
|
||||||
$entry['device_id'] = $device_id_ip;
|
$entry['device_id'] = $device_id_ip;
|
||||||
@@ -87,10 +86,12 @@ function process_syslog ($entry, $update)
|
|||||||
# }
|
# }
|
||||||
}
|
}
|
||||||
$entry['program'] = strtoupper($entry['program']);
|
$entry['program'] = strtoupper($entry['program']);
|
||||||
$x = "UPDATE `syslog` set `device_id` = '".$entry['device_id']."', `program` = '".$entry['program']."', `msg` = '" . mres($entry['msg']) . "', processed = '1' WHERE `seq` = '" . $entry['seq'] . "'";
|
#$x = "UPDATE `syslog` set `device_id` = '".$entry['device_id']."', `program` = '".$entry['program']."', `msg` = '" . mres($entry['msg']) . "', processed = '1' WHERE `seq` = '" . $entry['seq'] . "'";
|
||||||
$x = "INSERT INTO `syslog` (`device_id`,`program`,`facility`,`priority`, `level`, `tag`, `msg`, `timestamp`) ";
|
|
||||||
$x .= "VALUES ('".$entry['device_id']."','".$entry['program']."','".$entry['facility']."','".$entry['priority']."', '".$entry['level']."', '".$entry['tag']."', '".$entry['msg']."','".$entry['timestamp']."')";
|
$insert_array = array('device_id' => $entry['device_id'], 'program' => $entry['program'], 'facility' => $entry['facility'], 'priority' => $entry['priority'],
|
||||||
if ($update && $entry['device_id']) { mysql_query($x); }
|
'level' => $entry['level'], 'tag' => $entry['tag'], 'msg' => $entry['msg'], 'timestamp' => $entry['timestamp']);
|
||||||
|
|
||||||
|
if ($update && $entry['device_id']) { $dbInsert($insert_array, 'syslog'); }
|
||||||
unset ($fix);
|
unset ($fix);
|
||||||
} else { print_r($entry); echo("D-$delete"); }
|
} else { print_r($entry); echo("D-$delete"); }
|
||||||
|
|
||||||
|
29
poller.php
29
poller.php
@@ -49,7 +49,7 @@ if (isset($options['i']) && $options['i'] && isset($options['n']))
|
|||||||
WHERE `disabled` = 0
|
WHERE `disabled` = 0
|
||||||
ORDER BY `device_id` ASC
|
ORDER BY `device_id` ASC
|
||||||
) temp
|
) temp
|
||||||
WHERE MOD(temp.rownum, '.$options['i'].') = '.$options['n'].';';
|
WHERE MOD(temp.rownum, '.mres($options['i']).') = '.mres($options['n']).';';
|
||||||
$doing = $options['n'] ."/".$options['i'];
|
$doing = $options['n'] ."/".$options['i'];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,15 +89,12 @@ echo("Starting polling run:\n\n");
|
|||||||
$polled_devices = 0;
|
$polled_devices = 0;
|
||||||
if(!isset($query))
|
if(!isset($query))
|
||||||
{
|
{
|
||||||
$device_query = mysql_query("SELECT `device_id` FROM `devices` WHERE `disabled` = 0 $where ORDER BY `device_id` ASC");
|
$query = "SELECT `device_id` FROM `devices` WHERE `disabled` = 0 $where ORDER BY `device_id` ASC";
|
||||||
} else {
|
|
||||||
$device_query = mysql_query($query);
|
|
||||||
}
|
}
|
||||||
print mysql_error();
|
|
||||||
|
|
||||||
while ($device = mysql_fetch_assoc($device_query))
|
foreach (dbFetch($query) as $device)
|
||||||
{
|
{
|
||||||
$device = mysql_fetch_assoc(mysql_query("SELECT * FROM `devices` WHERE `device_id` = '".$device['device_id']."'"));
|
$device = dbFetchRow("SELECT * FROM `devices` WHERE `device_id` = '".$device['device_id']."'");
|
||||||
|
|
||||||
poll_device($device, $options);
|
poll_device($device, $options);
|
||||||
|
|
||||||
@@ -147,8 +144,10 @@ function poll_device($device, $options) {
|
|||||||
{
|
{
|
||||||
$poll_update .= $poll_separator . "`status` = '$status'";
|
$poll_update .= $poll_separator . "`status` = '$status'";
|
||||||
$poll_separator = ", ";
|
$poll_separator = ", ";
|
||||||
mysql_query("UPDATE `devices` SET `status` = '".$status."' WHERE `device_id` = '".$device['device_id']."'");
|
|
||||||
mysql_query("INSERT INTO alerts (importance, device_id, message) VALUES ('0', '" . $device['device_id'] . "', 'Device is " .($status == '1' ? 'up' : 'down') . "')");
|
dbUpdate(array('status' => $status), 'devices', 'device_id=?', array($device['device_id']));
|
||||||
|
dbInsert(array('importance' => '0', 'device_id' => $device['device_id'], 'message' => "Device is " .($status == '1' ? 'up' : 'down')), 'alerts');
|
||||||
|
|
||||||
log_event('Device status changed to ' . ($status == '1' ? 'Up' : 'Down'), $device, ($status == '1' ? 'up' : 'down'));
|
log_event('Device status changed to ' . ($status == '1' ? 'Up' : 'Down'), $device, ($status == '1' ? 'up' : 'down'));
|
||||||
notify($device, "Device ".($status == '1' ? 'Up' : 'Down').": " . $device['hostname'], "Device ".($status == '1' ? 'up' : 'down').": " . $device['hostname'] . " at " . date($config['timestamp_format']));
|
notify($device, "Device ".($status == '1' ? 'Up' : 'Down').": " . $device['hostname'], "Device ".($status == '1' ? 'up' : 'down').": " . $device['hostname'] . " at " . date($config['timestamp_format']));
|
||||||
}
|
}
|
||||||
@@ -186,12 +185,11 @@ if (!$options['m'])
|
|||||||
### If there any don't match, they're added/deleted from the database.
|
### If there any don't match, they're added/deleted from the database.
|
||||||
### Ideally we should hold graphs for xx days/weeks/polls so that we don't needlessly hide information.
|
### Ideally we should hold graphs for xx days/weeks/polls so that we don't needlessly hide information.
|
||||||
|
|
||||||
$query = mysql_query("SELECT `graph` FROM `device_graphs` WHERE `device_id` = '".$device['device_id']."'");
|
foreach (dbFetch("SELECT `graph` FROM `device_graphs` WHERE `device_id` = ?", array($device['device_id'])) as $graph)
|
||||||
while ($graph = mysql_fetch_assoc($query))
|
|
||||||
{
|
{
|
||||||
if (!isset($graphs[$graph["graph"]]))
|
if (!isset($graphs[$graph["graph"]]))
|
||||||
{
|
{
|
||||||
mysql_query("DELETE FROM `device_graphs` WHERE `device_id` = '".$device['device_id']."' AND `graph` = '".$graph["graph"]."'");
|
dbDelete('device_graphs', "`device_id` = ? AND `graph` = ?", array($device['device_id'], $graph["graph"]));
|
||||||
} else {
|
} else {
|
||||||
$oldgraphs[$graph["graph"]] = TRUE;
|
$oldgraphs[$graph["graph"]] = TRUE;
|
||||||
}
|
}
|
||||||
@@ -201,7 +199,7 @@ if (!$options['m'])
|
|||||||
{
|
{
|
||||||
if (!isset($oldgraphs[$graph]))
|
if (!isset($oldgraphs[$graph]))
|
||||||
{
|
{
|
||||||
mysql_query("INSERT INTO `device_graphs` (`device_id`, `graph`) VALUES ('".$device['device_id']."','".$graph."')");
|
dbInsert(array('device_id' => $device['device_id'], 'graph' => $graph), 'device_graphs');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -217,7 +215,7 @@ if (!$options['m'])
|
|||||||
$device['db_update_query'] .= $device['db_update'];
|
$device['db_update_query'] .= $device['db_update'];
|
||||||
$device['db_update_query'] .= " WHERE `device_id` = '" . $device['device_id'] . "'";
|
$device['db_update_query'] .= " WHERE `device_id` = '" . $device['device_id'] . "'";
|
||||||
if ($debug) { echo("Updating " . $device['hostname'] . " - ".$device['db_update_query']." \n"); }
|
if ($debug) { echo("Updating " . $device['hostname'] . " - ".$device['db_update_query']." \n"); }
|
||||||
if (!mysql_query($device['db_update_query']))
|
if (!mysql_query($device['db_update_query'])) ## FIXME some work to be done to build the update array then dbUpdate()
|
||||||
{
|
{
|
||||||
echo("ERROR: " . mysql_error() . "\nSQL: ".$device['db_update_query']."\n");
|
echo("ERROR: " . mysql_error() . "\nSQL: ".$device['db_update_query']."\n");
|
||||||
}
|
}
|
||||||
@@ -233,8 +231,7 @@ if (!$options['m'])
|
|||||||
|
|
||||||
if ($polled_devices)
|
if ($polled_devices)
|
||||||
{
|
{
|
||||||
mysql_query("INSERT INTO `perf_times` (`type`, `doing`, `start`, `duration`, `devices`)
|
dbInsert(array('type' => 'poll', 'doing' => $doing, 'start' => $poller_start, 'duration' => $poller_time, 'device' => $polled_devices ), 'perf_times');
|
||||||
VALUES ('poll', '$doing', '$poller_start', '$poller_time', '$polled_devices')");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$string = $argv[0] . " $doing " . date("F j, Y, G:i") . " - $polled_devices devices polled in $poller_time secs";
|
$string = $argv[0] . " $doing " . date("F j, Y, G:i") . " - $polled_devices devices polled in $poller_time secs";
|
||||||
|
Reference in New Issue
Block a user