Source for file epo_sync.php

Documentation is available at epo_sync.php

  1. #!/usr/bin/php
  2. <?php
  3. /**
  4.  * enterprise/epo_sync
  5.  *
  6.  * Long description for file:
  7.  * 
  8.  * PHP version 5
  9.  *
  10.  * LICENSE: This program is free software; you can redistribute it and/or
  11.  * modify it under the terms of the GNU General Public License as published
  12.  * by the Free Software Foundation.
  13.  *
  14.  * @package                     FreeNAC
  15.  * @author                      Wolfram Strauss, Sean Boran (FreeNAC Core Team)
  16.  * @copyright                   2006 FreeNAC
  17.  * @license                     http://www.gnu.org/copyleft/gpl.html   GNU Public License Version 2
  18.  * @version                     CVS: $Id:$
  19.  * @link                        http://www.freenac.net
  20.  *
  21.  */
  22.  
  23.  
  24.  
  25.  
  26. /*************************************
  27.         MAIN
  28. **************************************/
  29.  
  30. $EPO_VERSION 4;    // either 3 or 4
  31.  
  32.  
  33. // Php weirdness: change to script dir, then look for includes
  34. chdir(dirname(__FILE__));
  35. set_include_path("../:./");
  36.  
  37.  
  38. /**
  39. * Load settings and common functions
  40. */
  41. require_once "./funcs.inc.php";     
  42.  
  43. global $logger;
  44. $logger->setLogToStdOut(false);
  45. $output=true;
  46. $logger->setDebugLevel(0);
  47.  
  48. #  epo version checking
  49. if$EPO_VERSION == )
  50. {
  51.     $epo_db_prefix '';
  52. }
  53. elseif$EPO_VERSION == )
  54. {
  55.     $epo_db_prefix 'EPO';
  56. }
  57. else
  58. {
  59.     $logger->logit("Version $EPO_VERSION is not supported!"LOG_ERR);
  60.     exit;
  61. }
  62.  
  63. // this timestamp is used as the sync date for data synced from the wsus db to the freenac db
  64. $timestamp=date('Y-m-d H:i:s');
  65. $logger->logit("Starting EPO sync job");
  66.  
  67. db_connect();    // vmps db connection (mysql)
  68. $enabled=v_sql_1_select("select value from config where name='epo_enabled'");
  69.  
  70. if$enabled )
  71. {
  72.     dbepo_connect();    // epo db connection (mssql)
  73.  
  74.     // warning!! this should run within a transaction ...
  75.     
  76.     if!empty_tables() )
  77.     {
  78.         $logger->logit("Failed to empty all epo tables, logical status may be inconsistend!"LOG_ERR);
  79.         cleanup();
  80.     }
  81.     
  82.     if!get_systems() )
  83.     {
  84.         $logger->logit("Failed to sync systems list"LOG_ERR);
  85.         cleanup;
  86.     }
  87.     
  88.     if!get_versions() )
  89.     {
  90.         $logger->logit("Failed to sync versions list"LOG_ERR);
  91.         cleanup;
  92.     }
  93.     
  94.     // ... end of suggested transaction
  95.     
  96.     cleanup();
  97.         
  98. }
  99. else
  100. {
  101.     $logger->logit("EPO support not enabled"LOG_WARNING);
  102. }
  103.  
  104.  
  105.  
  106. /*******************************
  107.     FUNCTIONS
  108. *******************************/
  109.     
  110.  
  111. /**
  112. *  This function converts the datetime retrieved from MSSQL into MySQL datetime format
  113. */
  114. function convert_date($date)
  115. {
  116.    $date_array=getdate(strtotime($date));
  117.    $date=$date_array['year'].'-';
  118.    $date_array['mon'10 $date.='0'.$date_array['mon'].'-' $date.=$date_array['mon'].'-';
  119.    $date_array['mday'10 $date.='0'.$date_array['mday'].' ' $date.=$date_array['mday'].' ';
  120.    $date_array['hours'10 $date.='0'.$date_array['hours'].':' $date.=$date_array['hours'].':';
  121.    $date_array['minutes'10 $date.='0'.$date_array['minutes'].':' $date.=$date_array['minutes'].':';
  122.    $date_array['seconds'10 $date.='0'.$date_array['seconds'$date.=$date_array['seconds'];
  123.    return $date;
  124. }
  125.  
  126.  
  127. /**
  128. * Ensures that $string is mysql safe
  129. */
  130. function validate($string)
  131. {
  132.    rtrim($string,' ');
  133.    if (get_magic_quotes_gpc()) {
  134.       $value=stripslashes($string);
  135.    }
  136.    if (!is_numeric($string)) {
  137.       $stringmysql_real_escape_string($string);
  138.    }
  139.    return $string;
  140. }
  141.  
  142.  
  143. /**
  144. * Connect to the EPO server
  145. */
  146. function dbepo_connect()
  147. {
  148.    global $conf,$epo_dbuser,$epo_dbpass$logger;
  149.    $logger->debug("Connecting to ".$conf->epo_dbalias." ".$conf->epo_db2);
  150.    $msconnect mssql_connect($conf->epo_dbalias$epo_dbuser$epo_dbpass);
  151.    if !$msconnect )
  152.    {
  153.      $logger->logit("Cannot connect to EPO server ".$conf->epo_dbalias.":" mssql_get_last_message()LOG_ERR);
  154.      return false;
  155.    }
  156.    $db mssql_select_db($conf->epo_db$msconnect);
  157.    if !$db )
  158.    {
  159.       $logger->logit("Couldn't open database ".$conf->epo_db." ".mssql_get_last_message()LOG_ERR);
  160.       return false;
  161.    }
  162. }
  163.  
  164.  
  165. /**
  166. * Returns the hostname part of an fqdn thus everything before the first dot
  167. */
  168. function get_hostname($fqdn)
  169. {
  170.     global $logger;
  171.     
  172.     $dot_pos strpos($fqdn'.');
  173.     $hostname;
  174.     if$dot_pos )
  175.     {
  176.         $hostname substr($fqdn0$dot_pos);
  177.     }
  178.     else
  179.     {
  180.         $hostname $fqdn;
  181.     }
  182.     
  183.     $logger->debug("Converting $fqdn to $hostname"2);
  184.     
  185.     return strtolower($hostname);
  186. }
  187.  
  188. /**
  189. * Look up a wsus hostname in the vmps table and return the vmps id if and only if there's exactly one entry
  190. */
  191. function get_vmps_id($mac)
  192. {
  193.     global $logger;
  194.     
  195.     $query "select id from systems where mac = '$mac';";
  196.     $logger->debug("Executing $query"3);
  197.     $result mysql_query($query);
  198.     if!$result )
  199.     {
  200.         $logger->logit("Could not obtain vmps id for mac $macmysql_error()LOG_WARNING);
  201.         return false;
  202.     }
  203.     $num_rows mysql_num_rows($result);    //TODO: exception handling
  204.     if$num_rows == )
  205.     {
  206.         #$logger->logit("No vmps id for mac $mac found", LOG_WARNING);
  207.         $logger->logit("No vmps id for mac $mac found")// don't flag as warning until 100% right, its flooding logcheck
  208.         return false;
  209.     }
  210.     elseif$num_rows == )
  211.     {
  212.         $row mysql_fetch_row($result);    //TODO: exception handling
  213.         $logger->debug("System with mac $mac matches vmps id $row[0]"2);
  214.         return $row[0];    
  215.     }
  216.     else
  217.     {
  218.         $logger->logit("mac $mac is not unique in vmps"LOG_WARNING);
  219.         return false;
  220.     }
  221. }
  222.  
  223.  
  224. /**
  225. * Convert mac address obtain from epo into vmps format
  226. */
  227. function convert_mac($epo_mac)
  228. {
  229.     global $logger;
  230.     
  231.     $vmps_mac preg_replace('/(\w{4})(\w{4})(\w{4})/''$1.$2.$3'$epo_mac);
  232.     
  233.     $logger->debug("Mac format conversion: $epo_mac -> $vmps_mac"2);
  234.     
  235.     return $vmps_mac;
  236. }
  237.  
  238. /**
  239. * Empty all epo tables to get ready for fresh sync
  240. */
  241. function empty_tables()
  242. {
  243.     global $logger;
  244.  
  245.     $logger->debug("Emptying tables"1);
  246.     if!mysql_query('truncate table epo_systems;') ) {
  247.         $logger->logit("Could not empty epo_systems, " mysql_error()LOG_ERR);
  248.         return false;
  249.     }
  250.     if!mysql_query('truncate table epo_versions;') ) {
  251.         $logger->logit("Could not empty epo_versions, " mysql_error()LOG_ERR);
  252.         return false;
  253.     }
  254.     
  255.         
  256.     return true;
  257. }
  258.  
  259.  
  260. /**
  261. * Obtain list of systems managed by the epo server
  262. */
  263. function get_systems()
  264. {
  265.     global $logger$timestamp$epo_db_prefix;
  266.  
  267.     $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'";
  268.     
  269.     $logger->debug("Executing $query"3);
  270.     $result mssql_query($query);
  271.     if!$result )
  272.     {
  273.         $logger->logit("Failed to obtain systems from epo, " mssql_get_last_message()LOG_ERR);
  274.         return false;
  275.     }
  276.     
  277.     while$sys_row mssql_fetch_assoc($result) )
  278.     {
  279.         // TODO: exception handling
  280.         $hostname get_hostname($sys_row['nodename']);
  281.         $mac convert_mac($sys_row['netaddress']);
  282.         $id get_vmps_id($mac);
  283.         if!$id )
  284.         {
  285.             continue;
  286.         }
  287.         
  288.         // insert system into epo_systems
  289.         $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$hostnamevalidate($sys_row['domainname'])validate($sys_row['ipaddress'])$macvalidate($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);
  290.         
  291.         $logger->debug("Executing: $query"3);
  292.         if!mysql_query($query) )
  293.         {
  294.             $logger->logit("Could not insert system $hostnamemysql_error()LOG_WARNING);
  295.             continue;
  296.         }
  297.         
  298.     }
  299.     
  300.     return true;
  301. }
  302.  
  303.  
  304. /**
  305. * Obtain list of product actual product version checked into the epo server
  306. */
  307. function get_versions()
  308. {
  309.     global $logger$timestamp$EPO_VERSION;
  310.     
  311.     if$EPO_VERSION == )
  312.     {
  313.         $query "select type, version from dbo.latestupdates";
  314.         
  315.         $logger->debug("Executing: $query"3);
  316.         $result mssql_query($query);
  317.         
  318.         if!$result )
  319.         {
  320.             $logger->logit("Failed to obtain product versions from epo, " mssql_get_last_message()LOG_ERR);
  321.             return false;
  322.         }
  323.     
  324.         while$row mssql_fetch_assoc($result) )
  325.         {
  326.             $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);
  327.         
  328.             $logger->debug("Executing: $query"3);
  329.             if!mysql_query($query) )
  330.             {
  331.                 $logger->logit("Could not insert product " $row['type'", " mysql_error()LOG_WARNING);
  332.                 continue;
  333.             }
  334.         }
  335.     }
  336.     elseif$EPO_VERSION == )
  337.     {
  338.         $query "select productcode, productversion, hotfixversion from dbo.epomastercatalog";
  339.         
  340.         $logger->debug("Executing: $query"3);
  341.         $result mssql_query($query);
  342.         
  343.         if!$result )
  344.         {
  345.             $logger->logit("Failed to obtain product versions from epo, " mssql_get_last_message()LOG_ERR);
  346.             return false;
  347.         }
  348.     
  349.         while$row mssql_fetch_assoc($result) )
  350.         {
  351.             $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);
  352.         
  353.             $logger->debug("Executing: $query"3);
  354.             if!mysql_query($query) )
  355.             {
  356.                 $logger->logit("Could not insert product " $row['productcode'",  " mysql_error()LOG_WARNING);
  357.                 continue;
  358.             }
  359.         }
  360.     }
  361.     else
  362.     {
  363.         $logger->logit("Wrong epo version $EPO_VERSION. The variable must have changed during execution -> very bad!"LOG_ERR);
  364.         exit(1);
  365.     }
  366.     
  367.     return true;
  368. }
  369.  
  370.  
  371. /**
  372. *
  373. */
  374. function cleanup($exit_code 0)
  375. {
  376.     global $logger;
  377.  
  378.     // TODO: exception handling    
  379.     
  380.     mssql_close();
  381.     mysql_close();
  382.     
  383.     $logger->logit("Done syncing EPO");
  384.     
  385.     exit($exit_code);
  386. }
  387.     
  388.     
  389. ?>

Documentation generated on Mon, 01 Dec 2008 01:10:27 +0100 by phpDocumentor 1.4.0