synchro_exec.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. // Copyright (C) 2010-2012 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * Import web service
  20. *
  21. * @copyright Copyright (C) 2010-2012 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  23. */
  24. //
  25. // Known limitations
  26. // - reconciliation is made on the first column
  27. //
  28. // Known issues
  29. // - ALMOST impossible to troubleshoot when an external key has a wrong value
  30. // - no character escaping in the xml output (yes !?!?!)
  31. // - not outputing xml when a wrong input is given (class, attribute names)
  32. //
  33. if (!defined('__DIR__')) define('__DIR__', dirname(__FILE__));
  34. require_once(__DIR__.'/../approot.inc.php');
  35. require_once(APPROOT.'/application/application.inc.php');
  36. require_once(APPROOT.'/application/webpage.class.inc.php');
  37. require_once(APPROOT.'/application/csvpage.class.inc.php');
  38. require_once(APPROOT.'/application/clipage.class.inc.php');
  39. require_once(APPROOT.'/application/startup.inc.php');
  40. function UsageAndExit($oP)
  41. {
  42. global $aPageParams;
  43. $bModeCLI = utils::IsModeCLI();
  44. if ($bModeCLI)
  45. {
  46. $oP->p("USAGE:\n");
  47. $oP->p("php -q synchro_exec.php --auth_user=<login> --auth_pwd=<password> --data_sources=<comma_separated_list_of_data_sources> [max_chunk_size=<limit the count of replica loaded in a single pass>]\n");
  48. }
  49. else
  50. {
  51. $oP->p("The parameter 'data_sources' is mandatory, and must contain a comma separated list of data sources\n");
  52. }
  53. $oP->output();
  54. exit -2;
  55. }
  56. function ReadMandatoryParam($oP, $sParam, $sSanitizationFilter = 'parameter')
  57. {
  58. $sValue = utils::ReadParam($sParam, null, true /* Allow CLI */, $sSanitizationFilter);
  59. if (is_null($sValue))
  60. {
  61. $oP->p("ERROR: Missing argument '$sParam'\n");
  62. UsageAndExit($oP);
  63. }
  64. return trim($sValue);
  65. }
  66. /////////////////////////////////
  67. // Main program
  68. if (utils::IsModeCLI())
  69. {
  70. $oP = new CLIPage(Dict::S("TitleSynchroExecution"));
  71. }
  72. else
  73. {
  74. $oP = new WebPage(Dict::S("TitleSynchroExecution"));
  75. }
  76. try
  77. {
  78. utils::UseParamFile();
  79. }
  80. catch(Exception $e)
  81. {
  82. $oP->p("Error: ".$e->GetMessage());
  83. $oP->output();
  84. exit -2;
  85. }
  86. if (utils::IsModeCLI())
  87. {
  88. // Next steps:
  89. // specific arguments: 'csvfile'
  90. //
  91. $sAuthUser = ReadMandatoryParam($oP, 'auth_user', 'raw_data');
  92. $sAuthPwd = ReadMandatoryParam($oP, 'auth_pwd', 'raw_data');
  93. $sDataSourcesList = ReadMandatoryParam($oP, 'data_sources', 'raw_data'); // May contain commas
  94. if (UserRights::CheckCredentials($sAuthUser, $sAuthPwd))
  95. {
  96. UserRights::Login($sAuthUser); // Login & set the user's language
  97. }
  98. else
  99. {
  100. $oP->p("Access restricted or wrong credentials ('$sAuthUser')");
  101. $oP->output();
  102. exit -1;
  103. }
  104. }
  105. else
  106. {
  107. require_once(APPROOT.'/application/loginwebpage.class.inc.php');
  108. LoginWebPage::DoLogin(); // Check user rights and prompt if needed
  109. $sDataSourcesList = utils::ReadParam('data_sources', null, true, 'raw_data');
  110. if ($sDataSourcesList == null)
  111. {
  112. UsageAndExit($oP);
  113. }
  114. }
  115. $bSimulate = (utils::ReadParam('simulate', '0', true) == '1');
  116. foreach(explode(',', $sDataSourcesList) as $iSDS)
  117. {
  118. $oSynchroDataSource = MetaModel::GetObject('SynchroDataSource', $iSDS, false);
  119. if ($oSynchroDataSource == null)
  120. {
  121. $oP->p("ERROR: The data source (id=$iSDS) does not exist. Exiting...");
  122. $oP->output();
  123. exit -3;
  124. }
  125. else
  126. {
  127. if ($bSimulate)
  128. {
  129. CMDBSource::Query('START TRANSACTION');
  130. }
  131. try
  132. {
  133. $oSynchroExec = new SynchroExecution($oSynchroDataSource);
  134. $oStatLog = $oSynchroExec->Process();
  135. if ($bSimulate)
  136. {
  137. CMDBSource::Query('ROLLBACK');
  138. }
  139. foreach ($oStatLog->GetTraces() as $sMessage)
  140. {
  141. $oP->p('#'.$sMessage);
  142. }
  143. if ($oStatLog->Get('status') == 'error')
  144. {
  145. $oP->p("ERROR: ".$oStatLog->Get('last_error'));
  146. }
  147. $oP->p("Replicas: ".$oStatLog->Get('stats_nb_replica_total'));
  148. $oP->p("Replicas touched since last synchro: ".$oStatLog->Get('stats_nb_replica_seen'));
  149. $oP->p("Objects deleted: ".$oStatLog->Get('stats_nb_obj_deleted'));
  150. $oP->p("Objects deletion errors: ".$oStatLog->Get('stats_nb_obj_deleted_errors'));
  151. $oP->p("Objects obsoleted: ".$oStatLog->Get('stats_nb_obj_obsoleted'));
  152. $oP->p("Objects obsolescence errors: ".$oStatLog->Get('stats_nb_obj_obsoleted_errors'));
  153. $oP->p("Objects created: ".$oStatLog->Get('stats_nb_obj_created')." (".$oStatLog->Get('stats_nb_obj_created_warnings')." warnings)");
  154. $oP->p("Objects creation errors: ".$oStatLog->Get('stats_nb_obj_created_errors'));
  155. $oP->p("Objects updated: ".$oStatLog->Get('stats_nb_obj_updated')." (".$oStatLog->Get('stats_nb_obj_updated_warnings')." warnings)");
  156. $oP->p("Objects update errors: ".$oStatLog->Get('stats_nb_obj_updated_errors'));
  157. $oP->p("Objects reconciled (updated): ".$oStatLog->Get('stats_nb_obj_new_updated')." (".$oStatLog->Get('stats_nb_obj_new_updated_warnings')." warnings)");
  158. $oP->p("Objects reconciled (unchanged): ".$oStatLog->Get('stats_nb_obj_new_unchanged')." (".$oStatLog->Get('stats_nb_obj_new_updated_warnings')." warnings)");
  159. $oP->p("Objects reconciliation errors: ".$oStatLog->Get('stats_nb_replica_reconciled_errors'));
  160. $oP->p("Replica disappeared, no action taken: ".$oStatLog->Get('stats_nb_replica_disappeared_no_action'));
  161. }
  162. catch(Exception $e)
  163. {
  164. $oP->add($e->getMessage());
  165. if ($bSimulate)
  166. {
  167. CMDBSource::Query('ROLLBACK');
  168. }
  169. }
  170. }
  171. }
  172. $oP->output();
  173. ?>