more code cleanup (last commit for now)

git-svn-id: http://www.observium.org/svn/observer/trunk@2520 61d68cd4-352d-0410-923a-c4978735b2b8
This commit is contained in:
Tom Laermans
2011-09-20 14:37:54 +00:00
parent f87ed09e1b
commit 9a1bee1458
71 changed files with 296 additions and 293 deletions
+2 -2
View File
@@ -201,9 +201,9 @@ function device_by_id_cache($device_id, $refresh = '0')
function truncate($substring, $max = 50, $rep = '...')
{
if (strlen($substring) < 1){ $string = $rep; } else { $string = $substring; }
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; }
if (strlen($string) > $max) { return substr_replace($string, $rep, $leave); } else { return $string; }
}
function mres($string)
+23 -23
View File
@@ -27,7 +27,7 @@ function dbQuery($sql, $parameters = array()) {
$fullSql = dbMakeQuery($sql, $parameters);
if ($debug) { echo(" SQL[".$fullSql."] "); }
/*
if($this->logFile)
if ($this->logFile)
$time_start = microtime(true);
*/
@@ -35,13 +35,13 @@ function dbQuery($sql, $parameters = array()) {
$result = mysql_query($fullSql); // sets $this->result
/*
if($this->logFile) {
if ($this->logFile) {
$time_end = microtime(true);
fwrite($this->logFile, date('Y-m-d H:i:s') . "\n" . $fullSql . "\n" . number_format($time_end - $time_start, 8) . " seconds\n\n");
}
*/
if($result === false && (error_reporting() & 1)) {
if ($result === false && (error_reporting() & 1)) {
// aye. this gets triggers on duplicate Contact insert
//trigger_error('QDB - Error in query: ' . $fullSql . ' : ' . mysql_error(), E_USER_WARNING);
}
@@ -61,7 +61,7 @@ function dbInsert($data, $table) {
// it allows the method to work for those that would rather it (or expect it to)
// follow closer with SQL convention:
// insert into the TABLE this DATA
if(is_string($data) && is_array($table)) {
if (is_string($data) && is_array($table)) {
$tmp = $data;
$data = $table;
$table = $tmp;
@@ -73,12 +73,12 @@ function dbInsert($data, $table) {
$time_start = microtime(true);
dbBeginTransaction();
$result = dbQuery($sql, $data);
if($result) {
if ($result) {
$id = mysql_insert_id();
dbCommitTransaction();
#return $id;
} else {
if($table != 'Contact') {
if ($table != 'Contact') {
trigger_error('QDB - Insert failed.', E_USER_WARNING);
}
dbRollbackTransaction();
@@ -108,7 +108,7 @@ function dbUpdate($data, $table, $where = null, $parameters = array()) {
// it allows the method to work for those that would rather it (or expect it to)
// follow closer with SQL convention:
// update the TABLE with this DATA
if(is_string($data) && is_array($table)) {
if (is_string($data) && is_array($table)) {
$tmp = $data;
$data = $table;
$table = $tmp;
@@ -123,13 +123,13 @@ function dbUpdate($data, $table, $where = null, $parameters = array()) {
}
$sql = substr($sql, 0, -1); // strip off last comma
if($where) {
if ($where) {
$sql .= ' WHERE ' . $where;
$data = array_merge($data, $parameters);
}
$time_start = microtime(true);
if(dbQuery($sql, $data)) {
if (dbQuery($sql, $data)) {
$return = mysql_affected_rows();
} else {
#echo("$fullSql");
@@ -146,10 +146,10 @@ function dbUpdate($data, $table, $where = null, $parameters = array()) {
function dbDelete($table, $where = null, $parameters = array()) {
$sql = 'DELETE FROM `' . $table.'`';
if($where) {
if ($where) {
$sql .= ' WHERE ' . $where;
}
if(dbQuery($sql, $parameters)) {
if (dbQuery($sql, $parameters)) {
return mysql_affected_rows();
} else {
return false;
@@ -166,7 +166,7 @@ function dbFetchRows($sql, $parameters = array()) {
$time_start = microtime(true);
$result = dbQuery($sql, $parameters);
if(mysql_num_rows($result) > 0) {
if (mysql_num_rows($result) > 0) {
$rows = array();
while ($row = mysql_fetch_assoc($result)) {
$rows[] = $row;
@@ -194,7 +194,7 @@ function dbFetch($sql, $parameters = array()) {
/*
// for now, don't do the iterator thing
$result = dbQuery($sql, $parameters);
if($result) {
if ($result) {
// return new iterator
return new dbIterator($result);
} else {
@@ -212,7 +212,7 @@ function dbFetchRow($sql = null, $parameters = array()) {
$time_start = microtime(true);
$result = dbQuery($sql, $parameters);
if($result) {
if ($result) {
$row = mysql_fetch_assoc($result);
mysql_free_result($result);
$time_end = microtime(true);
@@ -236,7 +236,7 @@ function dbFetchCell($sql, $parameters = array()) {
$time_start = microtime(true);
$row = dbFetchRow($sql, $parameters);
if($row) {
if ($row) {
return array_shift($row); // shift first field off first row
}
$time_end = microtime(true);
@@ -276,7 +276,7 @@ function dbFetchKeyValue($sql, $parameters = array()) {
$data = array();
foreach(dbFetch($sql, $parameters) as $row) {
$key = array_shift($row);
if(sizeof($row) == 1) { // if there were only 2 fields in the result
if (sizeof($row) == 1) { // if there were only 2 fields in the result
// use the second for the value
$data[ $key ] = array_shift($row);
} else { // if more than 2 fields were fetched
@@ -301,7 +301,7 @@ function dbMakeQuery($sql, $parameters) {
$questionParams = array();
$namedParams = array();
foreach($parameters as $key => $value) {
if(is_numeric($key)) {
if (is_numeric($key)) {
$questionParams[] = $value;
} else {
$namedParams[ ':' . $key ] = $value;
@@ -320,9 +320,9 @@ function dbMakeQuery($sql, $parameters) {
$query .= $result[ $i ];
$j = $i+1;
if(array_key_exists($j, $result)) {
if (array_key_exists($j, $result)) {
$test = $result[ $j ];
if($test == '?') {
if ($test == '?') {
$query .= array_shift($questionParams);
} else {
$query .= $namedParams[ $test ];
@@ -341,7 +341,7 @@ function dbPrepareData($data) {
// don't quote or esc if value is an array, we treat it
// as a "decorator" that tells us not to escape the
// value contained in the array
if(is_array($value) && !is_object($value)) {
if (is_array($value) && !is_object($value)) {
$escape = false;
$value = array_shift($value);
}
@@ -349,7 +349,7 @@ function dbPrepareData($data) {
// that are aliases, or part of other tables through joins
//if(!in_array($key, $columns)) // skip invalid fields
// continue;
if($escape) {
if ($escape) {
$values[$key] = "'" . mysql_real_escape_string($value) . "'";
} else
$values[$key] = $value;
@@ -365,7 +365,7 @@ function dbPrepareData($data) {
function dbPlaceHolders($values) {
$data = array();
foreach($values as $key => $value) {
if(is_numeric($key))
if (is_numeric($key))
$data[] = '?';
else
$data[] = ':' . $key;
@@ -410,7 +410,7 @@ class dbIterator implements Iterator {
public function next() {
$this->i++;
$a = mysql_data_seek($this->result, $this->i);
if($a === false) {
if ($a === false) {
$this->i = 0;
}
return $a;
+1 -1
View File
@@ -71,7 +71,7 @@ if (is_array($oids))
if (is_numeric($entry['entPhySensorPrecision']) && $entry['entPhySensorPrecision'] > "0") { $divisor = $divisor . str_pad('', $entry['entPhySensorPrecision'], "0"); }
$current = $current * $multiplier / $divisor;
if ($type == "temperature") { if ($current > "200"){ $thisisnotbullshit = FALSE; } $descr = preg_replace("/[T|t]emperature[|s]/", "", $descr); }
if ($type == "temperature") { if ($current > "200") { $thisisnotbullshit = FALSE; } $descr = preg_replace("/[T|t]emperature[|s]/", "", $descr); }
#echo($descr . "|" . $index . "|" .$current . "|" . $multiplier . "|" . $divisor ."|" . $entry['entPhySensorScale'] . "|" . $entry['entPhySensorPrecision'] . "\n");
+4 -4
View File
@@ -134,14 +134,14 @@ function getImage($host)
{
$image = '<img src="'.$config['base_url'].'/images/os/'.$config['os'][$type]['icon'].'.gif" />';
} else {
if (file_exists($config['html_dir'] . "/images/os/$type" . ".png")){ $image = '<img src="'.$config['base_url'].'/images/os/'.$type.'.png" />';
} elseif (file_exists($config['html_dir'] . "/images/os/$type" . ".gif")){ $image = '<img src="'.$config['base_url'].'/images/os/'.$type.'.gif" />'; }
if (file_exists($config['html_dir'] . "/images/os/$type" . ".png")) { $image = '<img src="'.$config['base_url'].'/images/os/'.$type.'.png" />';
} elseif (file_exists($config['html_dir'] . "/images/os/$type" . ".gif")) { $image = '<img src="'.$config['base_url'].'/images/os/'.$type.'.gif" />'; }
if ($type == "linux")
{
$features = strtolower(trim($data['features']));
list($distro) = explode(" ", $features);
if (file_exists($config['html_dir'] . "/images/os/$distro" . ".png")){ $image = '<img src="'.$config['base_url'].'/images/os/'.$distro.'.png" />';
} elseif (file_exists($config['html_dir'] . "/images/os/$distro" . ".gif")){ $image = '<img src="'.$config['base_url'].'/images/os/'.$distro.'.gif" />'; }
if (file_exists($config['html_dir'] . "/images/os/$distro" . ".png")) { $image = '<img src="'.$config['base_url'].'/images/os/'.$distro.'.png" />';
} elseif (file_exists($config['html_dir'] . "/images/os/$distro" . ".gif")) { $image = '<img src="'.$config['base_url'].'/images/os/'.$distro.'.gif" />'; }
}
}
+1 -1
View File
@@ -8,7 +8,7 @@
#Cisco Systems, Inc. WS-C4003 Cisco Catalyst Operating System Software, Version 6.4(13) Copyright (c) 1995-2004 by Cisco Systems, Inc.
#Cisco Systems, Inc. WS-C4006 Cisco Catalyst Operating System Software, Version 6.3(9) Copyright (c) 1995-2002 by Cisco Systems, Inc.
if (strstr($ciscomodel, "OID")){ unset($ciscomodel); }
if (strstr($ciscomodel, "OID")) { unset($ciscomodel); }
if (!strstr($ciscomodel, " ") && strlen($ciscomodel) >= '3')
{
+2 -2
View File
@@ -640,7 +640,7 @@ function snmp_cache_port_oids($oids, $port, $device, $array, $mib=0)
$x=0;
$values = explode("\n", $data);
#echo("Caching: ifIndex $port\n");
foreach ($oids as $oid){
foreach ($oids as $oid) {
if (!strstr($values[$x], "at this OID"))
{
$array[$port][$oid] = $values[$x];
@@ -686,7 +686,7 @@ function snmp_cache_portIfIndex($device, $array)
{
$entry = str_replace("CISCO-STACK-MIB::portIfIndex.", "", $entry);
list($slotport, $ifIndex) = explode(" ", $entry);
if ($slotport && $ifIndex){
if ($slotport && $ifIndex) {
$array[$ifIndex]['portIfIndex'] = $slotport;
$array[$slotport]['ifIndex'] = $ifIndex;
}
+15 -15
View File
@@ -6,11 +6,11 @@
# $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");
function get_cache($host, $value){
function get_cache($host, $value) {
global $dev_cache;
if (!isset($dev_cache[$host][$value])){
switch($value){
if (!isset($dev_cache[$host][$value])) {
switch($value) {
case 'device_id':
//Try by hostname
$dev_cache[$host]['device_id'] = dbFetchCell('SELECT `device_id` FROM devices WHERE `hostname` = ? OR `sysName` = ?', array($host, $host));
@@ -39,7 +39,7 @@ function process_syslog ($entry, $update) {
global $dev_cache;
foreach($config['syslog_filter'] as $bi)
if (strpos($entry['msg'], $bi) !== FALSE){
if (strpos($entry['msg'], $bi) !== FALSE) {
print_r($entry);
echo('D-'.$bi);
return $entry;
@@ -49,9 +49,9 @@ function process_syslog ($entry, $update) {
if ($entry['device_id']) {
$os = get_cache($entry['host'], 'os');
if (in_array($os, array('ios', 'iosxe', 'catos'))){
if (in_array($os, array('ios', 'iosxe', 'catos'))) {
$matches = array();
# if (preg_match('#%(?P<program>.*):( ?)(?P<msg>.*)#', $entry['msg'], $matches)){
# if (preg_match('#%(?P<program>.*):( ?)(?P<msg>.*)#', $entry['msg'], $matches)) {
# $entry['msg'] = $matches['msg'];
# $entry['program'] = $matches['program'];
# }
@@ -86,29 +86,29 @@ function process_syslog ($entry, $update) {
if (!$entry['msg']) { $entry['msg'] = $entry['program']; unset ($entry['program']); }
} elseif($os == 'linux' and get_cache($entry['host'], 'version') == 'Point'){
} elseif($os == 'linux' and get_cache($entry['host'], 'version') == 'Point') {
//Cisco WAP200 and similar
$matches = array();
if (preg_match('#Log: \[(?P<program>.*)\] - (?P<msg>.*)#', $entry['msg'], $matches)){
if (preg_match('#Log: \[(?P<program>.*)\] - (?P<msg>.*)#', $entry['msg'], $matches)) {
$entry['msg'] = $matches['msg'];
$entry['program'] = $matches['program'];
}
unset($matches);
} elseif($os == 'linux'){
} elseif($os == 'linux') {
$matches = array();
//User_CommonName/123.213.132.231:39872 VERIFY OK: depth=1, /C=PL/ST=Malopolska/O=VLO/CN=v-lo.krakow.pl/emailAddress=root@v-lo.krakow.pl
if ($entry['facility'] == 'daemon' and preg_match('#/([0-9]{1,3}\.){3}[0-9]{1,3}:[0-9]{4,} ([A-Z]([A-Za-z])+( ?)){2,}:#', $entry['msg'])){
if ($entry['facility'] == 'daemon' and preg_match('#/([0-9]{1,3}\.) {3}[0-9]{1,3}:[0-9]{4,} ([A-Z]([A-Za-z])+( ?)) {2,}:#', $entry['msg'])) {
$entry['program'] = 'OpenVPN';
}
//pop3-login: Login: user=<username>, method=PLAIN, rip=123.213.132.231, lip=123.213.132.231, TLS
//POP3(username): Disconnected: Logged out top=0/0, retr=0/0, del=0/1, size=2802
elseif($entry['facility'] == 'mail' and preg_match('#^(((pop3|imap)\-login)|((POP3|IMAP)\(.*\))):', $entry['msg'])){
elseif($entry['facility'] == 'mail' and preg_match('#^(((pop3|imap)\-login)|((POP3|IMAP)\(.*\))):', $entry['msg'])) {
$entry['program'] = 'Dovecot';
}
//pam_krb5(sshd:auth): authentication failure; logname=root uid=0 euid=0 tty=ssh ruser= rhost=123.213.132.231
//pam_krb5[sshd:auth]: authentication failure; logname=root uid=0 euid=0 tty=ssh ruser= rhost=123.213.132.231
elseif(preg_match('#^(?P<program>(.*((\(|\[).*(\)|\])))):(?P<msg>.*)$#', $entry['msg'], $matches)){
elseif(preg_match('#^(?P<program>(.*((\(|\[).*(\)|\])))):(?P<msg>.*)$#', $entry['msg'], $matches)) {
$entry['msg'] = $matches['msg'];
$entry['program'] = $matches['program'];
}
@@ -117,18 +117,18 @@ function process_syslog ($entry, $update) {
//pam_krb5: authentication failure; logname=root uid=0 euid=0 tty=ssh ruser= rhost=123.213.132.231
## Disabled because broke this:
//diskio.c: don't know how to handle 10 request
#elseif($pos = strpos($entry['msg'], ';') or $pos = strpos($entry['msg'], ':')){
#elseif($pos = strpos($entry['msg'], ';') or $pos = strpos($entry['msg'], ':')) {
# $entry['program'] = substr($entry['msg'], 0, $pos);
# $entry['msg'] = substr($entry['msg'], $pos+1);
#}
//fallback, better than nothing...
elseif(empty($entry['program']) and !empty($entry['facility'])){
elseif(empty($entry['program']) and !empty($entry['facility'])) {
$entry['program'] = $entry['facility'];
}
unset($matches);
}
if (!isset($entry['program'])){
if (!isset($entry['program'])) {
$entry['program'] = $entry['msg'];
unset($entry['msg']);
}