diff --git a/includes/unix-graphing.php b/attic/unix-graphing.php similarity index 100% rename from includes/unix-graphing.php rename to attic/unix-graphing.php diff --git a/discovery.php b/discovery.php index 6d744be71f..1f31350654 100755 --- a/discovery.php +++ b/discovery.php @@ -95,12 +95,7 @@ if (file_exists('.svn')) { list(,$dbu_rev) = preg_split('/: /',@shell_exec('svn info database-update.sql|grep ^Revision')); - $device_query = mysql_query("SELECT revision FROM `dbSchema`"); - if ($rev = @mysql_fetch_assoc($device_query)) - { - $db_rev = $rev['revision']; - } - else + if ($db_rev = @dbFetchCell("SELECT revision FROM `dbSchema`")) {} else { $db_rev = 0; } @@ -142,19 +137,18 @@ if (file_exists('.svn')) shell_exec("scripts/update-sql.php database-update.sql"); if ($db_rev == 0) { - mysql_query("INSERT INTO dbSchema VALUES ($dbu_rev)"); + dbInsert(array('revision' => $dbu_rev), 'dbSchema'); } else { - mysql_query("UPDATE dbSchema set revision=$dbu_rev"); + dbUpdate(array('revision' => $dbu_rev), 'dbSchema'); } } } $discovered_devices = 0; -$device_query = mysql_query("SELECT * FROM `devices` WHERE status = 1 AND disabled = 0 $where ORDER BY device_id DESC"); -while ($device = mysql_fetch_assoc($device_query)) +foreach (dbFetch("SELECT * FROM `devices` WHERE status = 1 AND disabled = 0 $where ORDER BY device_id DESC") as $device) { discover_device($device, $options); } @@ -173,8 +167,8 @@ function discover_device($device, $options) echo($device['hostname'] . " ".$device['device_id']." ".$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']); + dbUpdate(array('os' => $device['os']), 'devices', '`device_id` = ?', array($device['device_id'])); echo("OS lowercased."); } 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); - $update_query = "UPDATE `devices` SET "; - $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); + dbUpdate(array('last_discovered' => array('NOW()'), 'type' => $device['type'], 'last_discovered_timetaken' => $device_time), 'devices', '`device_id` = ?', array($device['device_id'])); echo("Discovered in $device_time seconds\n"); @@ -236,8 +226,7 @@ $proctime = substr($run, 0, 5); if ($discovered_devices) { - mysql_query("INSERT INTO `perf_times` (`type`, `doing`, `start`, `duration`, `devices`) - VALUES ('discover', '$doing', '$start', '$proctime', '$discovered_devices')"); + dbInsert(array('type' => 'discover', 'doing' => $doing, 'start' => $start, 'duration' => $proctime, 'devices' => $discovered_devices), 'perf_times'); } $string = $argv[0] . " $doing " . date("F j, Y, G:i") . " - $discovered_devices devices discovered in $proctime secs"; diff --git a/includes/common.php b/includes/common.php index 649cad1066..6ef37cfed1 100644 --- a/includes/common.php +++ b/includes/common.php @@ -33,7 +33,7 @@ function get_port_by_id($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)) { @@ -47,7 +47,7 @@ function get_application_by_id($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)) { @@ -61,7 +61,7 @@ function get_sensor_by_id($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)) { @@ -75,7 +75,7 @@ function get_device_id_by_interface_id($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)) { @@ -104,7 +104,7 @@ function device_by_id_cache($device_id) { $device = $device_cache[$device_id]; } 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')) { $device['real_location'] = $device['location']; @@ -130,18 +130,12 @@ function mres($string) function getifhost($id) { - $sql = mysql_query("SELECT `device_id` from `ports` WHERE `interface_id` = '$id'"); - $result = @mysql_result($sql, 0); - - return $result; + return dbFetchCell("SELECT `device_id` from `ports` WHERE `interface_id` = ?", array($id)); } function gethostbyid($id) { - $sql = mysql_query("SELECT `hostname` FROM `devices` WHERE `device_id` = '$id'"); - $result = @mysql_result($sql, 0); - - return $result; + return dbFetchCell("SELECT `hostname` FROM `devices` WHERE `device_id` = ?", array($id)); } function strgen ($length = 16) @@ -163,58 +157,37 @@ function strgen ($length = 16) function getpeerhost($id) { - $sql = mysql_query("SELECT `device_id` from `bgpPeers` WHERE `bgpPeer_id` = '$id'"); - $result = @mysql_result($sql, 0); - - return $result; + return dbFetchCell("SELECT `device_id` from `bgpPeers` WHERE `bgpPeer_id` = ?", array($id)); } function getifindexbyid($id) { - $sql = mysql_query("SELECT `ifIndex` FROM `ports` WHERE `interface_id` = '$id'"); - $result = @mysql_result($sql, 0); - - return $result; + return dbFetchCell("SELECT `ifIndex` FROM `ports` WHERE `interface_id` = ?", array($id)); } function get_port_by_ifIndex($device, $ifIndex) { - $sql = mysql_query("SELECT * FROM `ports` WHERE `device_id` = '".$device['device_id']."' AND `ifIndex` = '$ifIndex'"); - $result = @mysql_fetch_assoc($sql); - - return $result; + return dbFetchCell("SELECT * FROM `ports` WHERE `device_id` = ? AND `ifIndex` = ?", array($device['device_id'], $ifIndex)); } function getifbyid($id) { - $sql = mysql_query("SELECT * FROM `ports` WHERE `interface_id` = '$id'"); - $result = @mysql_fetch_assoc($sql); - - return $result; + return dbFetchCell("SELECT * FROM `ports` WHERE `interface_id` = ?", array($id)); } function getifdescrbyid($id) { - $sql = mysql_query("SELECT `ifDescr` FROM `ports` WHERE `interface_id` = '$id'"); - $result = @mysql_result($sql, 0); - - return $result; + return dbFetchCell("SELECT `ifDescr` FROM `ports` WHERE `interface_id` = ?", array($id)); } function getidbyname($domain) { - $sql = mysql_query("SELECT `device_id` FROM `devices` WHERE `hostname` = '$domain'"); - $result = @mysql_result($sql, 0); - - return $result; + return dbFetchCell("SELECT `device_id` FROM `devices` WHERE `hostname` = ?", mres($domain)); } function gethostosbyid($id) { - $sql = mysql_query("SELECT `os` FROM `devices` WHERE `device_id` = '$id'"); - $result = @mysql_result($sql, 0); - - return $result; + return dbFetchCell("SELECT `os` FROM `devices` WHERE `device_id` = ?", array($id)); } function safename($name) @@ -234,27 +207,21 @@ function zeropad($num, $length = 2) 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 (mysql_result(mysql_query($count_sql),0)) + if (dbFetchCell("SELECT COUNT(*) FROM devices_attribs WHERE `device_id` = ? AND `attrib_type` = ?", array($device['device_id'],$attrib_type))) { - $update_sql = "UPDATE devices_attribs SET attrib_value = '$attrib_value' WHERE `device_id` = '" . mres($device['device_id']) . "' AND `attrib_type` = '$attrib_type'"; - mysql_query($update_sql); + $return = dbUpdate(array('attrib_value' => $attrib_value), 'devices_attribs', 'device_id=? and attrib_type=?', array($device['device_id'], $attrib_type)); } else { - $insert_sql = "INSERT INTO devices_attribs (`device_id`, `attrib_type`, `attrib_value`) VALUES ('" . mres($device['device_id'])."', '$attrib_type', '$attrib_value')"; - mysql_query($insert_sql); + $return = dbInsert(array('device_id' => $device['device_id'], 'attrib_type' => $attrib_type, 'attrib_value' => $attrib_value), 'devices_attribs'); } - - return mysql_affected_rows(); + return $return; } function get_dev_attribs($device) { $attribs = array(); - $sql = "SELECT * FROM devices_attribs WHERE `device_id` = '" . mres($device) . "'"; - $query = mysql_query($sql); - while($entry = mysql_fetch_assoc($query)) + foreach(dbFetch("SELECT * FROM devices_attribs WHERE `device_id` = ?", array($device)) as $entry) { $attribs[$entry['attrib_type']] = $entry['attrib_value']; } @@ -263,8 +230,7 @@ function get_dev_attribs($device) 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 = mysql_fetch_assoc(mysql_query($sql))) + if ($row = dbFetchRow("SELECT attrib_value FROM devices_attribs WHERE `device_id` = ? AND `attrib_type` = ?", array($device['device_id'], $attrib_type))) { return $row['attrib_value']; } @@ -276,8 +242,7 @@ function get_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 mysql_query($sql); + return dbDelete('devices_attribs', "`device_id` = ? AND `attrib_type` = ?", array($device['device_id'], $attrib_type)); } function formatRates($rate) diff --git a/includes/functions.php b/includes/functions.php index c8ccb2aa34..3d2a5b553a 100755 --- a/includes/functions.php +++ b/includes/functions.php @@ -109,9 +109,10 @@ function interface_errors($rrd_file, $period = '-1d') // Returns the last in/out function getImage($host) { + ## FIXME why not pass $device? (my shitty ancient code here!) + global $config; - $sql = "SELECT * FROM `devices` WHERE `device_id` = '$host'"; - $data = mysql_fetch_assoc(mysql_query($sql)); + $data = dbFetchRow("SELECT * FROM `devices` WHERE `device_id` = ?", array($host)); $type = strtolower($data['os']); 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; - $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"); - 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'); } @@ -148,17 +149,18 @@ function delete_port($int_id) { 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")); - mysql_query("DELETE from `adjacencies` WHERE `interface_id` = '$int_id'"); - mysql_query("DELETE from `links` WHERE `local_interface_id` = '$int_id'"); - mysql_query("DELETE from `links` WHERE `remote_interface_id` = '$int_id'"); - mysql_query("DELETE from `ipaddr` WHERE `interface_id` = '$int_id'"); - mysql_query("DELETE from `ip6adjacencies` WHERE `interface_id` = '$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'"); - mysql_query("DELETE from `pseudowires` WHERE `interface_id` = '$int_id'"); - mysql_query("DELETE FROM `ports` WHERE `interface_id` = '$int_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)); + + $interface_tables = array('adjacencies', 'ipaddr', 'ip6adjacencies', 'ip6addr', 'mac_accounting', 'bill_ports', 'pseudowires', 'ports'); + + foreach($interface_tables as $table) { + dbDelete($table, "`interface_id` = ?", array($int_id)); + } + + dbDelete('links', "`local_interface_id` = ?", array($int_id)); + dbDelete('links', "`remote_interface_id` = ?", array($int_id)); + dbDelete('bill_ports', "`port_id` = ?", array($int_id)); + unlink(trim($config['rrd_dir'])."/".trim($interface['hostname'])."/".$interface['ifIndex'].".rrd"); } @@ -166,34 +168,25 @@ function delete_device($id) { global $config; - $host = dbFetchCell("SELECT hostname FROM devices WHERE device_id = '$id'"); - mysql_query("DELETE FROM `devices` WHERE `device_id` = '$id'"); - $int_query = mysql_query("SELECT * FROM `ports` WHERE `device_id` = '$id'"); - while ($int_data = mysql_fetch_assoc($int_query)) + $host = dbFetchCell("SELECT hostname FROM devices WHERE device_id = ?", array($id)); + + dbDelete('devices', "`device_id` = ?", array($id)); + + foreach (dbFetch("SELECT * FROM `ports` WHERE `device_id` = ?", array($id)) as $int_data) { $int_if = $int_data['ifDescr']; $int_id = $int_data['interface_id']; delete_port($int_id); $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"); $ret = "Removed Device $host\n"; @@ -395,10 +388,7 @@ function isDomainResolves($domain) function hoststatus($id) { - $sql = mysql_query("SELECT `status` FROM `devices` WHERE `device_id` = '$id'"); - $result = @mysql_result($sql, 0); - - return $result; + return dbFetchCell("SELECT `status` FROM `devices` WHERE `device_id` = ?", array($id)); } 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 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); } - $event_query = "INSERT INTO eventlog (host, reference, type, datetime, message) VALUES (" . ($device['device_id'] ? $device['device_id'] : "NULL"); - $event_query .= ", '" . ($reference ? $reference : "NULL") . "', '" . ($type ? $type : "NULL") . "', NOW(), '" . mres($text) . "')"; - if ($debug) { echo($event_query . "\n"); } - mysql_query($event_query); + $insert = array('host' => ($device['device_id'] ? $device['device_id'] : "NULL"), + 'reference' => ($reference ? $reference : "NULL"), + 'type' => ($type ? $type : "NULL"), + 'datetime' => array("NOW()"), + 'message' => $text); + + return $dbInsert($insert, 'eventlog'); + } function notify($device,$title,$message) diff --git a/includes/services.inc.php b/includes/services.inc.php index 5ba02fdd80..c270e85c27 100644 --- a/includes/services.inc.php +++ b/includes/services.inc.php @@ -1,14 +1,20 @@ $id, 'service_ip' => $hostname, 'service_type' => $service, 'service_desc' => "auto discovered: $service", 'service_param' => "", 'service_ignore' => "0"); + + return $dbInsert($insert, 'services'); + } diff --git a/includes/static-config.php b/includes/static-config.php index 264e74eb75..965f9bc74a 100644 --- a/includes/static-config.php +++ b/includes/static-config.php @@ -914,15 +914,6 @@ if (!isset($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("