prod_monitor.module 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. <?php
  2. /**
  3. * Our own definition of the core requirements states. These can be found in
  4. * includes/install.inc and are only available in hook_install(). That's why
  5. * we redefine them here (yes, it's double!). It's nicer than including the
  6. * install.inc file...
  7. * Let's see if this will pose problems...
  8. */
  9. define('PROD_MONITOR_REQUIREMENT_INFO', -1);
  10. define('PROD_MONITOR_REQUIREMENT_OK', 0);
  11. define('PROD_MONITOR_REQUIREMENT_WARNING', 1);
  12. define('PROD_MONITOR_REQUIREMENT_ERROR', 2);
  13. /**
  14. * We do the same here for the update module constants: redefine them so that we
  15. * do not need to run the update module entirely!
  16. */
  17. define('UPDATE_DEFAULT_URL', 'http://updates.drupal.org/release-history');
  18. define('UPDATE_NOT_SECURE', 1);
  19. define('UPDATE_REVOKED', 2);
  20. define('UPDATE_NOT_SUPPORTED', 3);
  21. define('UPDATE_NOT_CURRENT', 4);
  22. define('UPDATE_CURRENT', 5);
  23. define('UPDATE_NOT_CHECKED', -1);
  24. define('UPDATE_UNKNOWN', -2);
  25. define('UPDATE_NOT_FETCHED', -3);
  26. define('UPDATE_FETCH_PENDING', -4);
  27. define('UPDATE_MAX_FETCH_ATTEMPTS', 2);
  28. define('UPDATE_MAX_FETCH_TIME', 5);
  29. /**
  30. * Implementation of hook_help().
  31. */
  32. function prod_monitor_help($path, $arg) {
  33. $output = '';
  34. switch ($path) {
  35. case 'admin/help#prod_monitor':
  36. $output .= '<p>'.t('Production monitor is a module that can connect to the <strong>Production check</strong> module using <strong>XMLRPC</strong> and an <strong>API key</strong>. It will retrieve all specified data from the remote site to create a satus page and monitoring facility in a central place.').'<br />';
  37. $output .= t('You can add multiple sites and configure per site what data you wish (not) to monitor, allowing you to setup a central Drupal site that will monitor all of your sites that have the <em>Production check</em> module with <em>XMLRPC</em> enabled.').'<br />';
  38. $output .= t('The <strong>data retrieval</strong> mechanism can be called <strong>manually</strong> and is integrated with the <strong>cron</strong>, so you get a fresh update of data each cron run.').'</p>';
  39. break;
  40. case 'admin/reports/prod-monitor':
  41. $output .= '<p><strong>'.t('Site overview table').'</strong><br />';
  42. $output .= t('The overview table gives you an overview of what sites you have added together with their status. The status will be the highest error detected in the retrieved data set.').'<br />';
  43. $output .= t('The per site functions <strong>View</strong>, <strong>Edit</strong>, <strong>Fetch data</strong>, <strong>Flush</strong> and <strong>Delete</strong> should be self explanatory.').'</p>';
  44. // No break!
  45. case 'admin/reports/prod-monitor/site/%/edit':
  46. $output .= '<p><strong>'.t('Website URL & API key').'</strong><br />';
  47. $output .= t('To add a site, enter it\'s <strong>full url</strong>, including the protocol, but omitting the <em>xmlrpc.php</em> part and the <strong>API key</strong> that you have configured for it using the <strong>Production check</strong> module. Now click the <strong>Get settings</strong> button.').'<br />';
  48. $output .= t('All of the checks that the <em>Production check</em> module can perform are fetched from the remote site and presented as an array of checkboxes. Finally you can configure what exactly you wish to monitor for this site, then hit the <strong>Add site</strong> button.').'<br />';
  49. $output .= t('Each time you edit a site, the settings are fetched from the remote server so that any new checks that might have been added to the <em>Production check</em> module there are always up to date in the monitoring section.').'<br />'; $output .= t('<strong>Fetch data immediately</strong> does exactly what it says and fetches all the configured data from the remote site and will direct you to the report page.').'</p>';
  50. break;
  51. case 'admin/reports/prod-monitor/site/%':
  52. case 'admin/reports/prod-monitor/site/%/view':
  53. $output .= '<p>'.t('This is an overview of all checks performed by the <em>Production check</em> module and their status <strong>on the remote site</strong>. You can click the links inside the report to jump to the module\'s settings page, or to go to the project page of a module, in case you need to download it for installation.').'</p>';
  54. break;
  55. }
  56. return $output;
  57. }
  58. /**
  59. * Implementation of hook_permission()
  60. */
  61. function prod_monitor_permission() {
  62. return array(
  63. 'access production monitor' => array(
  64. 'title' => t('Administer the Production Monitor module'),
  65. 'description' => t('Perform adiminister tasks for the Production Monitor module'),
  66. ),
  67. );
  68. }
  69. /**
  70. * Implementation of hook_menu().
  71. * Note: do not use t() in this hook! Translation is handled by core!
  72. */
  73. function prod_monitor_menu() {
  74. $items = array();
  75. $items['admin/reports/prod-monitor'] = array(
  76. 'title' => 'Production monitor',
  77. 'description' => 'Setup the Production monitor.',
  78. 'page callback' => 'drupal_get_form',
  79. 'page arguments' => array('prod_monitor_overview_form'),
  80. 'access callback' => 'user_access',
  81. 'access arguments' => array('access production monitor'),
  82. 'type' => MENU_NORMAL_ITEM,
  83. 'file' => 'includes/prod_monitor.admin.inc',
  84. );
  85. // This hook_menu() thing, still can't fully see the logic in it. However,
  86. // this here is what I want to achieve. It would be nice to see the /view/ bit
  87. // in the path dissapear, that would finish it entirely. I'll settle for this
  88. // now, caused me enough headache already ;-)
  89. // The actual callback used by the default primary & secondary tabs,
  90. // they trickle upwards seeking for a callback to end up here.
  91. $items['admin/reports/prod-monitor/site/%'] = array(
  92. 'title' => 'View',
  93. 'description' => 'View the Production monitor report page.',
  94. 'page callback' => 'prod_monitor_status',
  95. 'page arguments' => array(4),
  96. 'access callback' => 'user_access',
  97. 'access arguments' => array('access production monitor'),
  98. 'type' => MENU_NORMAL_ITEM,
  99. 'file' => 'includes/prod_monitor.admin.inc',
  100. 'weight' => 10,
  101. );
  102. // Default primary tab (callback for this is it's parent path).
  103. $items['admin/reports/prod-monitor/site/%/view'] = array(
  104. 'title' => 'View',
  105. 'type' => MENU_DEFAULT_LOCAL_TASK,
  106. 'weight' => 0,
  107. );
  108. // Default secondary (sub) tab (callback for this is it's parent path).
  109. $items['admin/reports/prod-monitor/site/%/view/status'] = array(
  110. 'title' => 'Status',
  111. 'type' => MENU_DEFAULT_LOCAL_TASK,
  112. 'weight' => 0,
  113. );
  114. // Performance secondary (sub) tab.
  115. $items['admin/reports/prod-monitor/site/%prod_monitor_perf/view/performance'] = array(
  116. 'title' => 'Performance',
  117. 'description' => t('View the performance data for this site.'),
  118. 'page callback' => 'prod_monitor_performance',
  119. 'page arguments' => array(4),
  120. 'access callback' => 'user_access',
  121. 'access arguments' => array('access production monitor'),
  122. 'type' => MENU_LOCAL_TASK,
  123. 'file' => 'includes/prod_monitor.admin.inc',
  124. 'weight' => 12,
  125. );
  126. // Updates secondary (sub) tab.
  127. $items['admin/reports/prod-monitor/site/%prod_monitor/view/updates'] = array(
  128. 'title' => 'Updates',
  129. 'description' => 'View the Production monitor modules update page.',
  130. 'page callback' => 'prod_monitor_updates',
  131. 'page arguments' => array(4),
  132. 'access callback' => 'user_access',
  133. 'access arguments' => array('access production monitor'),
  134. 'type' => MENU_LOCAL_TASK,
  135. 'file' => 'includes/prod_monitor.admin.inc',
  136. 'weight' => 15,
  137. );
  138. $items['admin/reports/prod-monitor/site/%/update-check'] = array(
  139. 'title' => 'Updates',
  140. 'description' => 'Refresh Production monitor modules update page.',
  141. 'page callback' => 'prod_monitor_updates_check',
  142. 'page arguments' => array(4),
  143. 'access callback' => 'user_access',
  144. 'access arguments' => array('access production monitor'),
  145. 'type' => MENU_CALLBACK,
  146. 'file' => 'includes/prod_monitor.admin.inc',
  147. );
  148. $items['admin/reports/prod-monitor/site/%/edit'] = array(
  149. 'title' => 'Edit',
  150. 'description' => 'Edit website',
  151. 'page callback' => 'drupal_get_form',
  152. 'page arguments' => array('prod_monitor_overview_form', 4),
  153. 'access callback' => 'user_access',
  154. 'access arguments' => array('access production monitor'),
  155. 'type' => MENU_LOCAL_TASK,
  156. 'file' => 'includes/prod_monitor.admin.inc',
  157. 'weight' => 20,
  158. );
  159. $items['admin/reports/prod-monitor/site/%/flush'] = array(
  160. 'title' => 'Flush',
  161. 'description' => "Flush website's data.",
  162. 'page callback' => 'drupal_get_form',
  163. 'page arguments' => array('prod_monitor_flush_form', 4),
  164. 'access callback' => 'user_access',
  165. 'access arguments' => array('access production monitor'),
  166. 'type' => MENU_LOCAL_TASK,
  167. 'file' => 'includes/prod_monitor.admin.inc',
  168. 'weight' => 30,
  169. );
  170. $items['admin/reports/prod-monitor/site/%/fetch'] = array(
  171. 'title' => 'Fetch',
  172. 'description' => "Fetch website's data.",
  173. 'page callback' => 'prod_monitor_fetch_data',
  174. 'page arguments' => array(4),
  175. 'access callback' => 'user_access',
  176. 'access arguments' => array('access production monitor'),
  177. 'type' => MENU_LOCAL_TASK,
  178. 'file' => 'includes/prod_monitor.admin.inc',
  179. 'weight' => 35,
  180. );
  181. $items['admin/reports/prod-monitor/site/%/delete'] = array(
  182. 'title' => 'Delete',
  183. 'description' => 'Delete website',
  184. 'page callback' => 'drupal_get_form',
  185. 'page arguments' => array('prod_monitor_delete_form', 4),
  186. 'access callback' => 'user_access',
  187. 'access arguments' => array('access production monitor'),
  188. 'type' => MENU_LOCAL_TASK,
  189. 'file' => 'includes/prod_monitor.admin.inc',
  190. 'weight' => 40,
  191. );
  192. return $items;
  193. }
  194. /**
  195. * Implementation of hook_load()
  196. */
  197. function prod_monitor_load($id) {
  198. // Get module data.
  199. $modules = _prod_monitor_get_site_modules($id);
  200. // Hide tab if no module data found.
  201. if (!isset($modules['projects']) || empty($modules['projects'])) {
  202. $modules = FALSE;
  203. }
  204. return $modules;
  205. }
  206. /**
  207. * Implementation of hook_load()
  208. */
  209. function prod_monitor_perf_load($id) {
  210. $data = array();
  211. $data['id'] = $id;
  212. // Get performance data.
  213. $data['data'] = _prod_monitor_get_performance_data($id);
  214. // Hide tab if no module data found.
  215. if (empty($data['data'])) {
  216. return FALSE;
  217. }
  218. return $data;
  219. }
  220. /**
  221. * Implementation of hook_theme()
  222. */
  223. function prod_monitor_theme() {
  224. return array(
  225. 'prod_monitor_update_report' => array(
  226. 'variables' => array('id' => NULL, 'last' => NULL, 'data' => NULL),
  227. 'file' => 'includes/prod_monitor.theme.inc',
  228. ),
  229. 'prod_monitor_update_status_label' => array(
  230. 'variables' => array('status' => NULL),
  231. 'file' => 'includes/prod_monitor.theme.inc',
  232. ),
  233. 'prod_monitor_update_version' => array(
  234. 'variables' => array('version' => NULL, 'tag' => NULL, 'class' => NULL),
  235. 'file' => 'includes/prod_monitor.theme.inc',
  236. ),
  237. 'prod_monitor_status_report' => array(
  238. 'variables' => array('requirements' => NULL),
  239. 'file' => 'includes/prod_monitor.theme.inc',
  240. ),
  241. 'prod_monitor_performance' => array(
  242. 'render element' => 'elements',
  243. 'template' => 'templates/prod-monitor-performance',
  244. ),
  245. );
  246. }
  247. /**
  248. * Implementation of theme_preprocess_hook()
  249. */
  250. function template_preprocess_prod_monitor_performance(&$vars) {
  251. $vars['data'] = $vars['elements']['#data'];
  252. // This array holds all graphs per module, per timestamp and per unit (MB, ms,
  253. // ...).
  254. $vars['graphs'] = array();
  255. foreach ($vars['data'] as $module => $data_set) {
  256. // Counters to make sure we only add columns once.
  257. $count = $i = 0;
  258. foreach ($data_set as $time => $params) {
  259. // Store title for this modules graphs.
  260. $vars['graphs'][$module]['title'] = $params['title'];
  261. if (is_array($params['data'])) {
  262. // Count all rows.
  263. $count = count($params['data']);
  264. foreach ($params['data'] as $title => $row) {
  265. // Rows without a specified unit.
  266. if (!isset($row[1])) {
  267. $row[1] = '';
  268. }
  269. if ($i < $count) {
  270. // Setup the columns for the graph in such a way that they are
  271. // organised by unit so we can draw one graph per unit.
  272. $vars['graphs'][$module][$row[1]]['cols'][] = $title;
  273. $i++;
  274. }
  275. // Cast empty strings to 0 and store the data per timestamp and unit so
  276. // that we can draw one graph per unit.
  277. $vars['graphs'][$module][$row[1]]['rows'][$time][] = (int) $row[0];
  278. }
  279. }
  280. else {
  281. $vars['graphs'][$module]['message'] = $params['data'];
  282. }
  283. }
  284. if (count($vars['graphs'][$module]) > 2) {
  285. unset($vars['graphs'][$module]['message']);
  286. }
  287. }
  288. }
  289. /**
  290. * Implementation of hook_cron()
  291. */
  292. function prod_monitor_cron() {
  293. if (variable_get('prod_monitor_cron_running', FALSE)) {
  294. watchdog('prod_monitor', 'Last cron run was not properly terminated!', array(), WATCHDOG_ERROR);
  295. }
  296. $sites = _prod_monitor_get_sites(variable_get('prod_monitor_cron_start_at', 0));
  297. // Indicate we're running.
  298. variable_set('prod_monitor_cron_running', TRUE);
  299. $cron_start = REQUEST_TIME;
  300. // 180 seconds run max!
  301. $time_limit = 180;
  302. $elapsed = $process = 0;
  303. foreach ($sites as $id => $site_info) {
  304. $elapsed = time() - $cron_start;
  305. if ($elapsed < $time_limit) {
  306. //TODO: add module status update check here.
  307. _prod_monitor_retrieve_data($id, $site_info);
  308. $process++;
  309. }
  310. else {
  311. // Time's up! Start with this site next time.
  312. variable_set('prod_monitor_cron_start_at', $id);
  313. break;
  314. }
  315. }
  316. // If all was processed, make sure we start from the top next time
  317. if ($process >= count($sites)) {
  318. variable_set('prod_monitor_cron_start_at', 0);
  319. }
  320. watchdog('prod_monitor', '!count sites updated successfully in !time seconds.', array('!count' => $process, '!time' => $elapsed), WATCHDOG_NOTICE);
  321. // Indicate we've stopped.
  322. variable_del('prod_monitor_cron_running');
  323. }
  324. /**
  325. * Retrieve settings form from Prod check using XMLRPC
  326. */
  327. function _prod_monitor_retrieve_functions($url, $api_key, $msg = TRUE) {
  328. $url = rtrim($url, '/') . '/xmlrpc.php';
  329. $functions = xmlrpc($url, array('prod_check.get_settings' => array($api_key)));
  330. if (!$functions) {
  331. drupal_set_message(
  332. t('Failed to retrieve settings form from !link, please verify the given URL and try again!',
  333. array('!link' => l('remote site', $url, array('attributes' => array('title' => t('remote site')))))
  334. ),
  335. 'error'
  336. );
  337. }
  338. else if ($msg) {
  339. drupal_set_message(t('Settings form updated, please adjust your settings.'));
  340. }
  341. return $functions;
  342. }
  343. /**
  344. * Retrieve data form from Prod check using XMLRPC and store it in the database.
  345. *
  346. * @param $id id of the site the data is being fetched for
  347. * @param $site_info associative array containing api_key and checks to execute
  348. * @param $msg wether or not to give feedback to the user of the action
  349. */
  350. function _prod_monitor_retrieve_data($id, $site_info, $msg = FALSE) {
  351. $url = rtrim($site_info['url'], '/') . '/xmlrpc.php';
  352. $api_key = $site_info['settings']['api_key'];
  353. $checks = $site_info['settings']['checks'];
  354. // Do requests.
  355. $data = xmlrpc($url, array('prod_check.get_data' => array($api_key, $checks)));
  356. if (!$data) {
  357. watchdog('prod_monitor', 'Could not retrieve settings data for %link', array('%link' => $site_info['url']), WATCHDOG_ERROR);
  358. if ($msg) {
  359. drupal_set_message(
  360. t('Data for %link not successfully fetched. Errors have been !link.',
  361. array(
  362. '%link' => $site_info['url'],
  363. '!link' => l(t('logged'), 'admin/reports/dblog'),
  364. )
  365. ), 'error'
  366. );
  367. }
  368. }
  369. else {
  370. // Extract the module list data to be stored in a different table
  371. $module_list = array();
  372. if (isset($data['prod_mon']['prod_check_module_list'])) {
  373. $module_list = $data['prod_mon']['prod_check_module_list'];
  374. unset($data['prod_mon']['prod_check_module_list']);
  375. }
  376. // Extract the performance data to be stored in a different table
  377. $perf_data = array();
  378. if (isset($data['perf_data'])) {
  379. $perf_data = $data['perf_data'];
  380. unset($data['perf_data']);
  381. }
  382. // Store site data
  383. $site = new stdClass();
  384. $site->id = $id;
  385. $site->data = serialize($data);
  386. $site->lastupdate = REQUEST_TIME;
  387. $result = drupal_write_record('prod_monitor_sites', $site, array('id'));
  388. // TODO: pour this into a function, it's thrice the same!
  389. if (!$result) {
  390. watchdog('prod_monitor', 'Could not update data for %link', array('%link' => $site_info['url']), WATCHDOG_ERROR);
  391. if ($msg) {
  392. drupal_set_message(
  393. t('Data for %link not successfully saved. Errors have been !link.',
  394. array(
  395. '%link' => $site_info['url'],
  396. '!link' => l(t('logged'), 'admin/reports/dblog'),
  397. )
  398. ), 'error'
  399. );
  400. }
  401. }
  402. else {
  403. if ($msg) {
  404. drupal_set_message(t('Data for %link successfully updated.', array('%link' => $site_info['url'])));
  405. }
  406. // Store module data if there is an update.
  407. if (!empty($module_list)) {
  408. // Check if data present, so we can update.
  409. $modules = _prod_monitor_get_site_modules($id, TRUE);
  410. $update = array();
  411. if (!empty($modules)) {
  412. $update = array('id');
  413. }
  414. $modules = new stdClass();
  415. $modules->id = $id;
  416. $modules->projects = serialize($module_list['projects']);
  417. $modules->sitekey = $module_list['site_key'];
  418. $modules->lastfetch = $module_list['last_update'];
  419. $result = drupal_write_record('prod_monitor_site_modules', $modules, $update);
  420. // TODO: pour this into a function, it's thrice the same!
  421. if (!$result) {
  422. watchdog('prod_monitor', 'Could not update module data for %link', array('%link' => $site_info['url']), WATCHDOG_ERROR);
  423. if ($msg) {
  424. drupal_set_message(
  425. t('Module data for %link not successfully saved. Errors have been !link.',
  426. array(
  427. '%link' => $site_info['url'],
  428. '!link' => l(t('logged'), 'admin/reports/dblog'),
  429. )
  430. ), 'error'
  431. );
  432. }
  433. }
  434. else if ($msg) {
  435. drupal_set_message(t('Module data for %link successfully updated.', array('%link' => $site_info['url'])));
  436. }
  437. }
  438. if (!empty($perf_data)) {
  439. foreach ($perf_data as $module => $module_data) {
  440. $performance = new stdClass();
  441. $performance->id = $id;
  442. $performance->module = $module;
  443. $performance->data = serialize($module_data);
  444. $performance->fetched = time();
  445. $result = drupal_write_record('prod_monitor_site_performance', $performance);
  446. // TODO: pour this into a function, it's thrice the same!
  447. if (!$result) {
  448. watchdog('prod_monitor', 'Could not update performance data for %link', array('%link' => $site_info['url']), WATCHDOG_ERROR);
  449. if ($msg) {
  450. drupal_set_message(
  451. t('Performance data for %link not successfully saved. Errors have been !link.',
  452. array(
  453. '%link' => $site_info['url'],
  454. '!link' => l(t('logged'), 'admin/reports/dblog'),
  455. )
  456. ), 'error'
  457. );
  458. }
  459. }
  460. else if ($msg) {
  461. drupal_set_message(t('Performance data for %link successfully updated.', array('%link' => $site_info['url'])));
  462. }
  463. }
  464. }
  465. }
  466. }
  467. }
  468. /**
  469. * Helper function to get all sites.
  470. */
  471. function _prod_monitor_get_sites($start_id = FALSE) {
  472. if ($start_id) {
  473. // When called from hook_cron
  474. $result = db_query("SELECT * FROM {prod_monitor_sites} WHERE id >= :start_id ORDER BY id ASC", array(':start_id' => $start_id));
  475. }
  476. else {
  477. $result = db_query("SELECT * FROM {prod_monitor_sites} ORDER BY added DESC");
  478. }
  479. $sites = array();
  480. foreach ($result as $row) {
  481. $id = $row->id;
  482. $row->data = unserialize($row->data);
  483. // Get highest error level
  484. $status = -1;
  485. if (!empty($row->data)) {
  486. foreach ($row->data as $set => $checks) {
  487. foreach ($checks as $check => $results) {
  488. $status = ($results['severity'] > $status) ? $results['severity'] : $status;
  489. }
  490. }
  491. $data_status = TRUE;
  492. }
  493. else {
  494. $data_status = FALSE;
  495. }
  496. switch ($status) {
  497. case 0: $status = 'ok';
  498. break;
  499. case 1: $status = 'warning';
  500. break;
  501. case 2: $status = 'error';
  502. break;
  503. default: $status = '';
  504. }
  505. $sites[$id]['url'] = $row->url;
  506. $sites[$id]['settings'] = unserialize($row->settings);
  507. $sites[$id]['data'] = $data_status;
  508. $sites[$id]['status'] = $status;
  509. $sites[$id]['added'] = format_date($row->added, 'small');
  510. $sites[$id]['lastupdate'] = (empty($row->lastupdate)) ? FALSE : format_date($row->lastupdate, 'small');
  511. }
  512. return $sites;
  513. }
  514. /**
  515. * Helper function to get a site by ID.
  516. *
  517. * @param $id
  518. * int site id.
  519. * @param $all
  520. * Boolean whether or not to return all fields or just the url and settings.
  521. */
  522. function _prod_monitor_get_site($id, $all = FALSE) {
  523. if (!$all) {
  524. $site = db_query("SELECT url, settings FROM {prod_monitor_sites} WHERE id = :id", array(':id' => $id))->fetchAssoc();
  525. }
  526. else {
  527. $site = db_query("SELECT * FROM {prod_monitor_sites} WHERE id = :id", array(':id' => $id))->fetchAssoc();
  528. }
  529. if (!empty($site)) {
  530. $site['settings'] = unserialize($site['settings']);
  531. if ($all) {
  532. $site['data'] = unserialize($site['data']);
  533. }
  534. }
  535. return $site;
  536. }
  537. /**
  538. * Helper function to get a site's modules by ID.
  539. *
  540. * @param $id
  541. * int site id.
  542. * @param $exists
  543. * Boolean wether to return just the ID (to check if there is module info)
  544. * or all fields.
  545. */
  546. function _prod_monitor_get_site_modules($id, $exists = FALSE) {
  547. if (!$exists) {
  548. $modules = db_query('SELECT * FROM {prod_monitor_site_modules} WHERE id = :id', array (':id' => $id))->fetchAssoc();
  549. }
  550. else {
  551. $modules = db_query('SELECT id FROM {prod_monitor_site_modules} WHERE id = :id', array (':id' => $id))->fetchAssoc();
  552. }
  553. if (!empty($modules) && !$exists) {
  554. $modules['projects'] = unserialize($modules['projects']);
  555. $modules['available'] = unserialize($modules['available']);
  556. }
  557. return $modules;
  558. }
  559. /**
  560. * Helper function to get the module status of a site by ID.
  561. *
  562. * @param $id
  563. * int site id.
  564. */
  565. function _prod_monitor_get_update_status($id) {
  566. return db_query('SELECT updates FROM {prod_monitor_site_modules} WHERE id = :id', array(':id' => $id))->fetchField();
  567. }
  568. /**
  569. * Helper function to get the performance data of a site by ID.
  570. *
  571. * @param $id
  572. * int site id.
  573. */
  574. function _prod_monitor_get_performance_data($id) {
  575. $result = db_query('SELECT module, data, fetched FROM {prod_monitor_site_performance} WHERE id = :id', array(':id' => $id));
  576. $data = array();
  577. foreach ($result as $row) {
  578. $data[$row->module][$row->fetched] = unserialize($row->data);
  579. }
  580. return $data;
  581. }
  582. /**
  583. * Helper function to flush data for a site.
  584. * Added for easy implementation of Drush functionality.
  585. */
  586. function _prod_monitor_flush_data($id) {
  587. $site = new stdClass();
  588. $site->id = $id;
  589. // Setting data to NULL would be preferred, but then drupal_write_record
  590. // fails!
  591. $site->data = serialize(array());
  592. $site->lastupdate = 0;
  593. return drupal_write_record('prod_monitor_sites', $site, array('id'));
  594. }
  595. /**
  596. * Helper function to delete a site.
  597. * Added for easy implementation of Drush functionality.
  598. */
  599. function _prod_monitor_delete_site($id) {
  600. $txn = db_transaction();
  601. // If anyone has an idea on how to do this on one single query, like we did in
  602. // the D6 version, drop us a line!
  603. try {
  604. $query = db_delete('prod_monitor_sites')
  605. ->condition('id', $id)
  606. ->execute();
  607. $query = db_delete('prod_monitor_site_modules')
  608. ->condition('id', $id)
  609. ->execute();
  610. return TRUE;
  611. }
  612. catch (Exception $e) {
  613. $txn->rollback();
  614. watchdog_exception('prod_monitor', $e);
  615. }
  616. return FALSE;
  617. }
  618. /**
  619. * Helper function for Drush to show proper info when deleting a site.
  620. */
  621. function _prod_monitor_get_url($id) {
  622. $url = db_query('SELECT url FROM {prod_monitor_sites} WHERE id = :id', array(':id' => $id))->fetchField();
  623. return _prod_monitor_sanitize_url(rtrim($url, '/'));
  624. }
  625. /**
  626. * Remove (optional) password from URL.
  627. */
  628. function _prod_monitor_sanitize_url($url) {
  629. return preg_replace('/(:\/\/[^:]+:)[^@]+(@)/', "$1...$2", $url);
  630. }