prod_monitor.install 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <?php
  2. /**
  3. * Implementation of hook_schema().
  4. */
  5. function prod_monitor_schema() {
  6. return array(
  7. 'prod_monitor_sites' => array(
  8. 'description' => 'Holds all sites and data monitored by Production monitor.',
  9. 'fields' => array(
  10. 'id' => array(
  11. 'description' => 'The primary identifier for a site.',
  12. 'type' => 'serial',
  13. 'unsigned' => TRUE,
  14. 'not null' => TRUE,
  15. ),
  16. 'url' => array(
  17. 'description' => 'URL of the website to monitor.',
  18. 'type' => 'text',
  19. 'size' => 'normal',
  20. 'default' => NULL,
  21. ),
  22. 'settings' => array(
  23. 'description' => 'All settings related to the site.',
  24. 'type' => 'text',
  25. 'size' => 'medium',
  26. 'default' => NULL,
  27. ),
  28. 'data' => array(
  29. 'description' => 'All data collected through XMLRPC in serialized form.',
  30. 'type' => 'text',
  31. 'size' => 'medium',
  32. 'default' => NULL,
  33. ),
  34. 'added' => array(
  35. 'description' => 'The Unix timestamp when the site was added.',
  36. 'type' => 'int',
  37. 'not null' => TRUE,
  38. 'default' => 0
  39. ),
  40. 'lastupdate' => array(
  41. 'description' => 'The Unix timestamp when the data was most recently updated.',
  42. 'type' => 'int',
  43. 'not null' => TRUE,
  44. 'default' => 0,
  45. ),
  46. ),
  47. 'primary key' => array('id'),
  48. ),
  49. 'prod_monitor_site_modules' => array(
  50. 'description' => 'Holds all retrieved module data for a specific site.',
  51. 'fields' => array(
  52. 'id' => array(
  53. 'description' => 'The primary identifier for a site.',
  54. 'type' => 'int',
  55. 'unsigned' => TRUE,
  56. 'not null' => TRUE,
  57. ),
  58. 'projects' => array(
  59. 'description' => 'All modules installed on the remote site.',
  60. 'type' => 'text',
  61. 'size' => 'medium',
  62. 'default' => NULL,
  63. ),
  64. 'sitekey' => array(
  65. 'description' => 'The unique key for the site.',
  66. 'type' => 'varchar',
  67. 'length' => 128,
  68. 'not null' => TRUE,
  69. 'default' => '',
  70. ),
  71. 'lastfetch' => array(
  72. 'description' => 'The Unix timestamp when the data was most recently retrieved from the remote site.',
  73. 'type' => 'int',
  74. 'not null' => TRUE,
  75. 'default' => 0,
  76. ),
  77. 'available' => array(
  78. 'description' => 'All module updates available for the remote site.',
  79. 'type' => 'text',
  80. 'size' => 'medium',
  81. 'default' => NULL,
  82. ),
  83. 'updates' => array(
  84. 'description' => '0 = unknown, 1 = no updates, 2 = regular updates, 3 = security updates.',
  85. 'type' => 'int',
  86. 'size' => 'tiny',
  87. 'unsigned' => TRUE,
  88. 'not null' => TRUE,
  89. 'default' => 0,
  90. ),
  91. 'lastupdate' => array(
  92. 'description' => 'The Unix timestamp of the most recent module update check.',
  93. 'type' => 'int',
  94. 'not null' => TRUE,
  95. 'default' => 0,
  96. ),
  97. ),
  98. 'foreign keys' => array(
  99. 'prod_monitor_sites' => array(
  100. 'table' => 'prod_monitor_sites',
  101. 'columns' => array('id' => 'id'),
  102. ),
  103. ),
  104. 'primary key' => array('id'),
  105. ),
  106. 'prod_monitor_site_performance' => array(
  107. 'description' => 'Holds all retrieved performance data for a specific site.',
  108. 'fields' => array(
  109. 'id' => array(
  110. 'description' => 'The primary identifier for a site.',
  111. 'type' => 'int',
  112. 'unsigned' => TRUE,
  113. 'not null' => TRUE,
  114. ),
  115. 'module' => array(
  116. 'description' => 'The module that reported the performance data.',
  117. 'type' => 'varchar',
  118. 'length' => 255,
  119. 'not null' => TRUE,
  120. ),
  121. 'data' => array(
  122. 'description' => 'The actual performance data.',
  123. 'type' => 'text',
  124. 'size' => 'medium',
  125. 'default' => NULL,
  126. ),
  127. 'annotation' => array(
  128. 'description' => 'A short annotation to this specific dataset.',
  129. 'type' => 'varchar',
  130. 'length' => 255,
  131. 'default' => NULL,
  132. ),
  133. 'fetched' => array(
  134. 'description' => 'The Unix timestamp when the data was retrieved from the remote site.',
  135. 'type' => 'int',
  136. 'not null' => TRUE,
  137. 'default' => 0,
  138. ),
  139. ),
  140. 'foreign keys' => array(
  141. 'prod_monitor_sites' => array(
  142. 'table' => 'prod_monitor_sites',
  143. 'columns' => array('id' => 'id'),
  144. ),
  145. ),
  146. 'primary key' => array('id', 'module', 'fetched'),
  147. ),
  148. );
  149. }
  150. /**
  151. * Implementation of hook_requirements().
  152. */
  153. function prod_monitor_requirements($phase) {
  154. $requirements = array();
  155. switch ($phase) {
  156. case 'install':
  157. if (module_exists('update')) {
  158. $requirements['prod_monitor_update'] = array(
  159. 'title' => t('Production monitor'),
  160. 'value' => t('Update manager enabled.'),
  161. 'severity' => REQUIREMENT_ERROR,
  162. 'description' => t('You have enabled <em>Update manager</em>. You have to disable this module before enabling Production monitor!'),
  163. );
  164. }
  165. break;
  166. }
  167. return $requirements;
  168. }
  169. /**
  170. * Implementation of hook_uninstall().
  171. */
  172. function prod_monitor_uninstall() {
  173. // This beats multiple variable_del() calls.
  174. db_delete('variable')->condition('name', 'prod_monitor\_%', 'LIKE')->execute();
  175. }
  176. /**
  177. * Increase the size of the settings field for table prod_monitor_sites.
  178. */
  179. function prod_monitor_update_7100() {
  180. // Note http://drupal.org/node/150220 about not using hook_schema() here!
  181. db_change_field(
  182. 'prod_monitor_sites',
  183. 'settings',
  184. 'settings',
  185. array(
  186. 'description' => 'All settings related to the site.',
  187. 'type' => 'text',
  188. 'size' => 'medium',
  189. 'default' => NULL,
  190. )
  191. );
  192. }
  193. /**
  194. * Update xmlrpc settings for all sites.
  195. */
  196. function prod_monitor_update_7101(&$sandbox) {
  197. $prefix = '_prod_check_';
  198. $ret = array();
  199. // Update 5 sites at a time
  200. if (!isset($sandbox['progress'])) {
  201. $sandbox['progress'] = 0;
  202. $sandbox['current_site'] = 0;
  203. $sandbox['max'] = db_query('SELECT COUNT(DISTINCT id) FROM {prod_monitor_sites}')->fetchField();
  204. }
  205. $sites = db_select('prod_monitor_sites', 'pms')
  206. ->fields('pms', array('id', 'settings'))
  207. ->condition('id', $sandbox['current_site'], '>')
  208. ->range(0, 5)
  209. ->orderBy('id', 'ASC')
  210. ->execute();
  211. foreach ($sites as $site) {
  212. $change = FALSE;
  213. $site->settings = unserialize($site->settings);
  214. // Adjust functions
  215. foreach ($site->settings['functions'] as $set => &$data) {
  216. foreach ($data['functions'] as $function => $title) {
  217. if (stripos($function, $prefix) === FALSE) {
  218. // Can't 'rename' keys, so we remove them and add a new one.
  219. unset($data['functions'][$function]);
  220. $data['functions'][$prefix . $function] = $title;
  221. $change = TRUE;
  222. }
  223. }
  224. }
  225. // Prevent any chance of the next loop from going kaka cuckoo.
  226. // See warning at http://php.net/manual/en/control-structures.foreach.php
  227. unset($data, $function);
  228. // Adjust checks
  229. foreach ($site->settings['checks'] as $set => &$calls) {
  230. foreach ($calls as $key => &$function) {
  231. if (stripos($function, $prefix) === FALSE) {
  232. $function = $prefix . $function;
  233. $change = TRUE;
  234. }
  235. }
  236. }
  237. // Only update record if there were changes. Added to prevent loss of data
  238. // when (accidentally) running this update twice.
  239. if ($change) {
  240. $site->settings = serialize($site->settings);
  241. db_update('prod_monitor_sites')
  242. ->fields(array('settings' => $site->settings))
  243. ->condition('id', $site->id)
  244. ->execute();
  245. $msg = t('Successfully updated all remote site settings.');
  246. }
  247. else {
  248. $msg = t('No remote site settings found that needed an update.');
  249. }
  250. $sandbox['progress']++;
  251. $sandbox['current_site'] = $site->id;
  252. }
  253. $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
  254. return $msg;
  255. }
  256. /**
  257. * Add new table to store performance data.
  258. */
  259. function prod_monitor_update_7102() {
  260. $schema = prod_monitor_schema();
  261. db_create_table('prod_monitor_site_performance', $schema['prod_monitor_site_performance']);
  262. }
  263. /**
  264. * Update sitekey field in database to allow longer D7 values.
  265. */
  266. function prod_check_update_7103() {
  267. // Note http://drupal.org/node/150220 about not using hook_schema() here!
  268. db_change_field(
  269. 'prod_monitor_site_modules',
  270. 'sitekey',
  271. 'sitekey',
  272. array(
  273. 'description' => 'The unique key for the site.',
  274. 'type' => 'varchar',
  275. 'length' => 128,
  276. 'not null' => TRUE,
  277. 'default' => '',
  278. )
  279. );
  280. }