Source for file epo_sync.php
Documentation is available at epo_sync.php
* Long description for file:
* LICENSE: This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published
* by the Free Software Foundation.
* @author Wolfram Strauss, Sean Boran (FreeNAC Core Team)
* @copyright 2006 FreeNAC
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License Version 2
* @link http://www.freenac.net
/*************************************
**************************************/
$EPO_VERSION =
4; // either 3 or 4
// Php weirdness: change to script dir, then look for includes
* Load settings and common functions
require_once "./funcs.inc.php";
$logger->setLogToStdOut(false);
$logger->setDebugLevel(0);
elseif( $EPO_VERSION ==
4 )
$logger->logit("Version $EPO_VERSION is not supported!", LOG_ERR);
// this timestamp is used as the sync date for data synced from the wsus db to the freenac db
$timestamp=
date('Y-m-d H:i:s');
$logger->logit("Starting EPO sync job");
$enabled=
v_sql_1_select("select value from config where name='epo_enabled'");
// warning!! this should run within a transaction ...
$logger->logit("Failed to empty all epo tables, logical status may be inconsistend!", LOG_ERR);
$logger->logit("Failed to sync systems list", LOG_ERR);
$logger->logit("Failed to sync versions list", LOG_ERR);
// ... end of suggested transaction
$logger->logit("EPO support not enabled", LOG_WARNING);
/*******************************
*******************************/
* This function converts the datetime retrieved from MSSQL into MySQL datetime format
$date=
$date_array['year'].
'-';
$date_array['mon'] <
10 ?
$date.=
'0'.
$date_array['mon'].
'-' :
$date.=
$date_array['mon'].
'-';
$date_array['mday'] <
10 ?
$date.=
'0'.
$date_array['mday'].
' ' :
$date.=
$date_array['mday'].
' ';
$date_array['hours'] <
10 ?
$date.=
'0'.
$date_array['hours'].
':' :
$date.=
$date_array['hours'].
':';
$date_array['minutes'] <
10 ?
$date.=
'0'.
$date_array['minutes'].
':' :
$date.=
$date_array['minutes'].
':';
$date_array['seconds'] <
10 ?
$date.=
'0'.
$date_array['seconds'] :
$date.=
$date_array['seconds'];
* Ensures that $string is mysql safe
* Connect to the EPO server
global $conf,$epo_dbuser,$epo_dbpass, $logger;
$logger->debug("Connecting to ".
$conf->epo_dbalias.
" ".
$conf->epo_db, 2);
$msconnect =
mssql_connect($conf->epo_dbalias, $epo_dbuser, $epo_dbpass);
$logger->logit("Cannot connect to EPO server ".
$conf->epo_dbalias.
":" .
mssql_get_last_message(), LOG_ERR);
$db =
mssql_select_db($conf->epo_db, $msconnect);
$logger->logit("Couldn't open database ".
$conf->epo_db.
" ".
mssql_get_last_message(), LOG_ERR);
* Returns the hostname part of an fqdn thus everything before the first dot
$dot_pos =
strpos($fqdn, '.');
$hostname =
substr($fqdn, 0, $dot_pos);
$logger->debug("Converting $fqdn to $hostname", 2);
* Look up a wsus hostname in the vmps table and return the vmps id if and only if there's exactly one entry
$query =
"select id from systems where mac = '$mac';";
$logger->debug("Executing $query", 3);
$logger->logit("Could not obtain vmps id for mac $mac, " .
mysql_error(), LOG_WARNING);
#$logger->logit("No vmps id for mac $mac found", LOG_WARNING);
$logger->logit("No vmps id for mac $mac found"); // don't flag as warning until 100% right, its flooding logcheck
$logger->debug("System with mac $mac matches vmps id $row[0]", 2);
$logger->logit("mac $mac is not unique in vmps", LOG_WARNING);
* Convert mac address obtain from epo into vmps format
$vmps_mac =
preg_replace('/(\w{4})(\w{4})(\w{4})/', '$1.$2.$3', $epo_mac);
$logger->debug("Mac format conversion: $epo_mac -> $vmps_mac", 2);
* Empty all epo tables to get ready for fresh sync
$logger->debug("Emptying tables", 1);
* Obtain list of systems managed by the epo server
global $logger, $timestamp, $epo_db_prefix;
$query =
"select l.nodename, l.lastupdate, l.agentversion, c.ostype, c.netaddress, c.ipaddress, c.freediskspace, p.enginever, p.datver, p. productversion, p.hotfix, c.domainname, c.ostype, c.osversion, c.osservicepackver, c.osbuildnum, c.username from dbo.${epo_db_prefix}leafnode l left join dbo.${epo_db_prefix}computerproperties c on l.autoid = c.parentid left join dbo.${epo_db_prefix}productproperties p on l.autoid = p.parentid where p.productcode = 'VIRUSCAN8600'";
$logger->debug("Executing $query", 3);
$result =
mssql_query($query);
$logger->logit("Failed to obtain systems from epo, " .
mssql_get_last_message(), LOG_ERR);
while( $sys_row =
mssql_fetch_assoc($result) )
// TODO: exception handling
// insert system into epo_systems
$query =
sprintf("insert into epo_systems values('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s') on duplicate key update lastsync=now();", $id, $hostname, validate($sys_row['domainname']), validate($sys_row['ipaddress']), $mac, validate($sys_row['agentversion']), convert_date($sys_row['lastupdate']), validate($sys_row['productversion']), validate($sys_row['enginever']), validate($sys_row['datver']), validate($sys_row['hotfix']), validate($sys_row['ostype']), validate($sys_row['osversion']), validate($sys_row['osservicepackver']), validate($sys_row['osbuildnum']), validate($sys_row['freediskspace']), validate($sys_row['username']), $timestamp);
$logger->debug("Executing: $query", 3);
$logger->logit("Could not insert system $hostname, " .
mysql_error(), LOG_WARNING);
* Obtain list of product actual product version checked into the epo server
global $logger, $timestamp, $EPO_VERSION;
$query =
"select type, version from dbo.latestupdates";
$logger->debug("Executing: $query", 3);
$result =
mssql_query($query);
$logger->logit("Failed to obtain product versions from epo, " .
mssql_get_last_message(), LOG_ERR);
while( $row =
mssql_fetch_assoc($result) )
$query =
sprintf("insert into epo_versions(product, version, hotfix, lastsync) values('%s', '%s', '%s', '%s') on duplicate key update lastsync=now();", validate($row['type']), validate($row['version']), '', $timestamp);
$logger->debug("Executing: $query", 3);
$logger->logit("Could not insert product " .
$row['type'] .
", " .
mysql_error(), LOG_WARNING);
elseif( $EPO_VERSION ==
4 )
$query =
"select productcode, productversion, hotfixversion from dbo.epomastercatalog";
$logger->debug("Executing: $query", 3);
$result =
mssql_query($query);
$logger->logit("Failed to obtain product versions from epo, " .
mssql_get_last_message(), LOG_ERR);
while( $row =
mssql_fetch_assoc($result) )
$query =
sprintf("insert into epo_versions(product, version, hotfix, lastsync) values('%s', '%s', '%s', '%s') on duplicate key update lastsync=now();", validate($row['productcode']), validate($row['productversion']), validate($row['hotfixversion']), $timestamp);
$logger->debug("Executing: $query", 3);
$logger->logit("Could not insert product " .
$row['productcode'] .
", " .
mysql_error(), LOG_WARNING);
$logger->logit("Wrong epo version $EPO_VERSION. The variable must have changed during execution -> very bad!", LOG_ERR);
// TODO: exception handling
$logger->logit("Done syncing EPO");
Documentation generated on Mon, 01 Dec 2008 01:10:27 +0100 by phpDocumentor 1.4.0