2008-03-23 21:32:54 +00:00
< ? php
2012-05-25 12:24:34 +00:00
// FIXME : use db functions properly
2015-07-13 20:10:26 +02:00
// $device_id_host = @dbFetchCell("SELECT device_id FROM devices WHERE `hostname` = '".mres($entry['host'])."' OR `sysName` = '".mres($entry['host'])."'");
// $device_id_ip = @dbFetchCell("SELECT device_id FROM ipv4_addresses AS A, ports AS I WHERE A.ipv4_address = '" . $entry['host']."' AND I.port_id = A.port_id");
2011-09-02 06:56:21 +00:00
2015-07-13 20:10:26 +02:00
2016-08-28 12:32:58 -05:00
function get_cache ( $host , $value )
{
2015-07-13 20:10:26 +02:00
global $dev_cache ;
if ( ! isset ( $dev_cache [ $host ][ $value ])) {
switch ( $value ) {
2016-08-28 12:32:58 -05:00
case 'device_id' :
// Try by hostname
$ip = inet_pton ( $host );
if ( inet_ntop ( $ip ) === false ) {
$dev_cache [ $host ][ 'device_id' ] = dbFetchCell ( 'SELECT `device_id` FROM devices WHERE `hostname` = ? OR `sysName` = ?' , array ( $host , $host ));
} else {
$dev_cache [ $host ][ 'device_id' ] = dbFetchCell ( 'SELECT `device_id` FROM devices WHERE `hostname` = ? OR `sysName` = ? OR `ip` = ?' , array ( $host , $host , $ip ));
}
// If failed, try by IP
if ( ! is_numeric ( $dev_cache [ $host ][ 'device_id' ])) {
$dev_cache [ $host ][ 'device_id' ] = dbFetchCell ( 'SELECT `device_id` FROM `ipv4_addresses` AS A, `ports` AS I WHERE A.ipv4_address = ? AND I.port_id = A.port_id' , array ( $host ));
}
break ;
2015-07-13 20:10:26 +02:00
2016-08-28 12:32:58 -05:00
case 'os' :
$dev_cache [ $host ][ 'os' ] = dbFetchCell ( 'SELECT `os` FROM devices WHERE `device_id` = ?' , array ( get_cache ( $host , 'device_id' )));
break ;
2015-07-13 20:10:26 +02:00
2016-08-28 12:32:58 -05:00
case 'version' :
$dev_cache [ $host ][ 'version' ] = dbFetchCell ( 'SELECT `version` FROM devices WHERE `device_id`= ?' , array ( get_cache ( $host , 'device_id' )));
break ;
2015-07-13 20:10:26 +02:00
2016-08-28 12:32:58 -05:00
default :
return null ;
2015-07-13 20:10:26 +02:00
} //end switch
} //end if
return $dev_cache [ $host ][ $value ];
} //end get_cache()
2016-08-28 12:32:58 -05:00
function process_syslog ( $entry , $update )
{
2015-07-13 20:10:26 +02:00
global $config , $dev_cache ;
foreach ( $config [ 'syslog_filter' ] as $bi ) {
if ( strpos ( $entry [ 'msg' ], $bi ) !== false ) {
return $entry ;
2011-09-20 09:55:11 +00:00
}
2011-05-26 21:27:40 +00:00
}
2011-09-02 06:56:21 +00:00
2015-08-21 16:02:59 +02:00
$entry [ 'host' ] = preg_replace ( " /^::ffff:/ " , " " , $entry [ 'host' ]);
2015-07-13 20:10:26 +02:00
$entry [ 'device_id' ] = get_cache ( $entry [ 'host' ], 'device_id' );
if ( $entry [ 'device_id' ]) {
$os = get_cache ( $entry [ 'host' ], 'os' );
if ( in_array ( $os , array ( 'ios' , 'iosxe' , 'catos' ))) {
2016-02-27 16:00:06 -06:00
// multipart message
2016-08-28 12:32:58 -05:00
if ( strpos ( $entry [ 'msg' ], ':' ) !== false ) {
2016-02-27 16:00:06 -06:00
$matches = array ();
2016-04-11 19:57:49 -05:00
$timestamp_prefix = '([\*\.]?[A-Z][a-z]{2} \d\d? \d\d:\d\d:\d\d(.\d\d\d)?( [A-Z]{3})?: )?' ;
2016-03-09 08:20:51 -06:00
$program_match = '(?<program>%?[A-Za-z\d\-_]+(:[A-Z]* %[A-Z\d\-_]+)?)' ;
$message_match = '(?<msg>.*)' ;
2016-08-28 12:32:58 -05:00
if ( preg_match ( '/^' . $timestamp_prefix . $program_match . ': ?' . $message_match . '/' , $entry [ 'msg' ], $matches )) {
2016-02-27 16:00:06 -06:00
$entry [ 'program' ] = $matches [ 'program' ];
$entry [ 'msg' ] = $matches [ 'msg' ];
}
unset ( $matches );
2016-08-28 12:32:58 -05:00
} else {
2016-02-27 16:00:06 -06:00
// if this looks like a program (no groups of 2 or more lowercase letters), move it to program
2016-03-02 23:48:07 -06:00
if ( ! preg_match ( '/[(a-z)]{2,}/' , $entry [ 'msg' ])) {
2016-02-27 16:00:06 -06:00
$entry [ 'program' ] = $entry [ 'msg' ];
unset ( $entry [ 'msg' ]);
2016-08-28 12:32:58 -05:00
}
2015-07-13 20:10:26 +02:00
}
2016-08-28 12:32:58 -05:00
} elseif ( $os == 'linux' and get_cache ( $entry [ 'host' ], 'version' ) == 'Point' ) {
2015-07-13 20:10:26 +02:00
// Cisco WAP200 and similar
$matches = array ();
if ( preg_match ( '#Log: \[(?P<program>.*)\] - (?P<msg>.*)#' , $entry [ 'msg' ], $matches )) {
$entry [ 'msg' ] = $matches [ 'msg' ];
$entry [ 'program' ] = $matches [ 'program' ];
}
unset ( $matches );
2016-08-28 12:32:58 -05:00
} elseif ( $os == 'linux' ) {
2015-07-13 20:10:26 +02:00
$matches = array ();
2016-03-05 12:12:00 +01:00
// pam_krb5(sshd:auth): authentication failure; logname=root uid=0 euid=0 tty=ssh ruser= rhost=123.213.132.231
2015-07-13 20:10:26 +02:00
// pam_krb5[sshd:auth]: authentication failure; logname=root uid=0 euid=0 tty=ssh ruser= rhost=123.213.132.231
2016-03-05 12:12:00 +01:00
if ( preg_match ( '#^(?P<program>([^(:]+\([^)]+\)|[^\[:]+\[[^\]]+\])) ?: ?(?P<msg>.*)$#' , $entry [ 'msg' ], $matches )) {
2015-07-13 20:10:26 +02:00
$entry [ 'msg' ] = $matches [ 'msg' ];
$entry [ 'program' ] = $matches [ 'program' ];
} // SYSLOG CONNECTION BROKEN; FD='6', SERVER='AF_INET(123.213.132.231:514)', time_reopen='60'
// 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'], ':')) {
// $entry['program'] = substr($entry['msg'], 0, $pos);
// $entry['msg'] = substr($entry['msg'], $pos+1);
// }
// fallback, better than nothing...
2016-08-28 12:32:58 -05:00
elseif ( empty ( $entry [ 'program' ]) and ! empty ( $entry [ 'facility' ])) {
2015-07-13 20:10:26 +02:00
$entry [ 'program' ] = $entry [ 'facility' ];
}
unset ( $matches );
2016-08-28 12:32:58 -05:00
} elseif ( $os == 'procurve' ) {
2016-03-04 16:14:47 +01:00
$matches = array ();
if ( preg_match ( '/^(?P<program>[A-Za-z]+): {2}(?P<msg>.*)/' , $entry [ 'msg' ], $matches )) {
$entry [ 'msg' ] = $matches [ 'msg' ] . " [ " . $entry [ 'program' ] . " ] " ;
$entry [ 'program' ] = $matches [ 'program' ];
}
unset ( $matches );
2015-07-13 20:10:26 +02:00
} //end if
if ( ! isset ( $entry [ 'program' ])) {
$entry [ 'program' ] = $entry [ 'msg' ];
unset ( $entry [ 'msg' ]);
}
$entry [ 'program' ] = strtoupper ( $entry [ 'program' ]);
2016-03-05 11:06:29 +01:00
$entry = array_map ( 'trim' , $entry );
2015-07-13 20:10:26 +02:00
if ( $update ) {
dbInsert (
array (
'device_id' => $entry [ 'device_id' ],
'program' => $entry [ 'program' ],
'facility' => $entry [ 'facility' ],
'priority' => $entry [ 'priority' ],
'level' => $entry [ 'level' ],
'tag' => $entry [ 'tag' ],
'msg' => $entry [ 'msg' ],
'timestamp' => $entry [ 'timestamp' ],
),
'syslog'
);
}
unset ( $os );
} //end if
return $entry ;
} //end process_syslog()