synchro_exec.php 5.3 KB

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