You are here

function _prod_monitor_retrieve_data in Production check & Production monitor 7

Same name and namespace in other branches
  1. 6 prod_monitor/prod_monitor.module \_prod_monitor_retrieve_data()

Retrieve data form from Prod check using XMLRPC and store it in the database.

Parameters

$id id of the site the data is being fetched for:

$site_info associative array containing api_key and checks to execute:

$msg wether or not to give feedback to the user of the action:

6 calls to _prod_monitor_retrieve_data()
drush_prod_monitor_fetch in prod_monitor/prod_monitor.drush.inc
Fetch data callback.
prod_monitor_cron in prod_monitor/prod_monitor.module
Implementation of hook_cron()
prod_monitor_fetch_all_data_batcher in prod_monitor/includes/prod_monitor.admin.inc
Batch fetching of all site info.
prod_monitor_fetch_data in prod_monitor/includes/prod_monitor.admin.inc
Callback to fetch site data
prod_monitor_overview_form_submit in prod_monitor/includes/prod_monitor.admin.inc
Submit function

... See full list

File

prod_monitor/prod_monitor.module, line 413

Code

function _prod_monitor_retrieve_data($id, $site_info, $msg = FALSE) {
  $url = rtrim($site_info['url'], '/') . '/xmlrpc.php';
  $api_key = $site_info['settings']['api_key'];
  $checks = $site_info['settings']['checks'];

  // Do requests.
  $data = xmlrpc($url, array(
    'prod_check.get_data' => array(
      $api_key,
      $checks,
    ),
  ));
  if (!$data) {
    watchdog('prod_monitor', 'Could not retrieve settings data for %link', array(
      '%link' => $site_info['url'],
    ), WATCHDOG_ERROR);
    if ($msg) {
      drupal_set_message(t('Data for %link not successfully fetched. Errors have been !link.', array(
        '%link' => $site_info['url'],
        '!link' => l(t('logged'), 'admin/reports/dblog'),
      )), 'error');
    }
  }
  else {

    // Extract the module list data to be stored in a different table
    $module_list = array();
    if (isset($data['prod_mon']['prod_check_module_list'])) {
      $module_list = $data['prod_mon']['prod_check_module_list'];
      unset($data['prod_mon']['prod_check_module_list']);
    }

    // Extract the performance data to be stored in a different table
    $perf_data = array();
    if (isset($data['perf_data'])) {
      $perf_data = $data['perf_data'];
      unset($data['perf_data']);
    }

    // Store site data
    $site = new stdClass();
    $site->id = $id;
    $site->data = serialize($data);
    $site->lastupdate = REQUEST_TIME;
    $result = drupal_write_record('prod_monitor_sites', $site, array(
      'id',
    ));

    // TODO: pour this into a function, it's thrice the same!
    if (!$result) {
      watchdog('prod_monitor', 'Could not update data for %link', array(
        '%link' => $site_info['url'],
      ), WATCHDOG_ERROR);
      if ($msg) {
        drupal_set_message(t('Data for %link not successfully saved. Errors have been !link.', array(
          '%link' => $site_info['url'],
          '!link' => l(t('logged'), 'admin/reports/dblog'),
        )), 'error');
      }
    }
    else {
      if ($msg) {
        drupal_set_message(t('Data for %link successfully updated.', array(
          '%link' => $site_info['url'],
        )));
      }

      // Store module data if there is an update.
      if (!empty($module_list)) {

        // Check if data present, so we can update.
        $modules = _prod_monitor_get_site_modules($id, TRUE);
        $update = array();
        if (!empty($modules)) {
          $update = array(
            'id',
          );
        }
        $modules = new stdClass();
        $modules->id = $id;
        $modules->projects = serialize($module_list['projects']);
        $modules->sitekey = $module_list['site_key'];
        $modules->lastfetch = $module_list['last_update'];
        $result = drupal_write_record('prod_monitor_site_modules', $modules, $update);

        // TODO: pour this into a function, it's thrice the same!
        if (!$result) {
          watchdog('prod_monitor', 'Could not update module data for %link', array(
            '%link' => $site_info['url'],
          ), WATCHDOG_ERROR);
          if ($msg) {
            drupal_set_message(t('Module data for %link not successfully saved. Errors have been !link.', array(
              '%link' => $site_info['url'],
              '!link' => l(t('logged'), 'admin/reports/dblog'),
            )), 'error');
          }
        }
        elseif ($msg) {
          drupal_set_message(t('Module data for %link successfully updated.', array(
            '%link' => $site_info['url'],
          )));
        }
      }
      if (!empty($perf_data)) {
        foreach ($perf_data as $module => $module_data) {
          $performance = new stdClass();
          $performance->id = $id;
          $performance->module = $module;
          $performance->data = serialize($module_data);
          $performance->fetched = time();
          $result = drupal_write_record('prod_monitor_site_performance', $performance);

          // TODO: pour this into a function, it's thrice the same!
          if (!$result) {
            watchdog('prod_monitor', 'Could not update performance data for %link', array(
              '%link' => $site_info['url'],
            ), WATCHDOG_ERROR);
            if ($msg) {
              drupal_set_message(t('Performance data for %link not successfully saved. Errors have been !link.', array(
                '%link' => $site_info['url'],
                '!link' => l(t('logged'), 'admin/reports/dblog'),
              )), 'error');
            }
          }
          elseif ($msg) {
            drupal_set_message(t('Performance data for %link successfully updated.', array(
              '%link' => $site_info['url'],
            )));
          }
        }
      }
    }
  }
}