prod_monitor.install 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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_uninstall().
  152. */
  153. function prod_monitor_uninstall() {
  154. // This beats multiple variable_del() calls.
  155. db_delete('variable')->condition('name', 'prod_monitor\_%', 'LIKE')->execute();
  156. }
  157. /**
  158. * Increase the size of the settings field for table prod_monitor_sites.
  159. */
  160. function prod_monitor_update_7100() {
  161. // Note http://drupal.org/node/150220 about not using hook_schema() here!
  162. db_change_field(
  163. 'prod_monitor_sites',
  164. 'settings',
  165. 'settings',
  166. array(
  167. 'description' => 'All settings related to the site.',
  168. 'type' => 'text',
  169. 'size' => 'medium',
  170. 'default' => NULL,
  171. )
  172. );
  173. }
  174. /**
  175. * Update xmlrpc settings for all sites.
  176. */
  177. function prod_monitor_update_7101(&$sandbox) {
  178. $prefix = '_prod_check_';
  179. $ret = array();
  180. // Update 5 sites at a time
  181. if (!isset($sandbox['progress'])) {
  182. $sandbox['progress'] = 0;
  183. $sandbox['current_site'] = 0;
  184. $sandbox['max'] = db_query('SELECT COUNT(DISTINCT id) FROM {prod_monitor_sites}')->fetchField();
  185. }
  186. $sites = db_select('prod_monitor_sites', 'pms')
  187. ->fields('pms', array('id', 'settings'))
  188. ->condition('id', $sandbox['current_site'], '>')
  189. ->range(0, 5)
  190. ->orderBy('id', 'ASC')
  191. ->execute();
  192. foreach ($sites as $site) {
  193. $change = FALSE;
  194. $site->settings = unserialize($site->settings);
  195. // Adjust functions
  196. foreach ($site->settings['functions'] as $set => &$data) {
  197. foreach ($data['functions'] as $function => $title) {
  198. if (stripos($function, $prefix) === FALSE) {
  199. // Can't 'rename' keys, so we remove them and add a new one.
  200. unset($data['functions'][$function]);
  201. $data['functions'][$prefix . $function] = $title;
  202. $change = TRUE;
  203. }
  204. }
  205. }
  206. // Prevent any chance of the next loop from going kaka cuckoo.
  207. // See warning at http://php.net/manual/en/control-structures.foreach.php
  208. unset($data, $function);
  209. // Adjust checks
  210. foreach ($site->settings['checks'] as $set => &$calls) {
  211. foreach ($calls as $key => &$function) {
  212. if (stripos($function, $prefix) === FALSE) {
  213. $function = $prefix . $function;
  214. $change = TRUE;
  215. }
  216. }
  217. }
  218. // Only update record if there were changes. Added to prevent loss of data
  219. // when (accidentally) running this update twice.
  220. if ($change) {
  221. $site->settings = serialize($site->settings);
  222. db_update('prod_monitor_sites')
  223. ->fields(array('settings' => $site->settings))
  224. ->condition('id', $site->id)
  225. ->execute();
  226. $msg = t('Successfully updated all remote site settings.');
  227. }
  228. else {
  229. $msg = t('No remote site settings found that needed an update.');
  230. }
  231. $sandbox['progress']++;
  232. $sandbox['current_site'] = $site->id;
  233. }
  234. $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
  235. return $msg;
  236. }
  237. /**
  238. * Add new table to store performance data.
  239. */
  240. function prod_monitor_update_7102() {
  241. $schema = prod_monitor_schema();
  242. db_create_table('prod_monitor_site_performance', $schema['prod_monitor_site_performance']);
  243. }
  244. /**
  245. * Update sitekey field in database to allow longer D7 values.
  246. */
  247. function prod_check_update_7103() {
  248. // Note http://drupal.org/node/150220 about not using hook_schema() here!
  249. db_change_field(
  250. 'prod_monitor_site_modules',
  251. 'sitekey',
  252. 'sitekey',
  253. array(
  254. 'description' => 'The unique key for the site.',
  255. 'type' => 'varchar',
  256. 'length' => 128,
  257. 'not null' => TRUE,
  258. 'default' => '',
  259. )
  260. );
  261. }
  262. /**
  263. * Add new dbconnect_path setting to database to prevent warnings.
  264. */
  265. function prod_check_update_7104() {
  266. $table = 'prod_monitor_sites';
  267. // Fetch all settings.
  268. $result = db_select($table, 'psm')
  269. ->fields('psm', array('id', 'settings'))
  270. ->execute();
  271. // Update all sites to add new setting.
  272. foreach ($result as $site) {
  273. $settings = unserialize($site->settings);
  274. $settings['dbconnect_path'] = '';
  275. $site->settings = serialize($settings);
  276. // Write to DB.
  277. drupal_write_record($table, $site, array('id'));
  278. }
  279. }