Ejemplo n.º 1
0
 /**
  * validation method - may be overriden by sub-classes, but parent method 
  * should still be invoked. returns TRUE if db options are valid, FALSE 
  * otherwise
  * @return boolean
  */
 protected function validate()
 {
     if ($valid = parent::validate()) {
         $table = 'test_' . $this->getTableName(rand());
         if ($this->mysql(sprintf('CREATE TABLE %s (id int)%s', $table, isset($this->options['db_mysql_engine']) ? ' ENGINE=' . $this->options['db_mysql_engine'] : '')) !== NULL) {
             // try to load some data into the table
             $fp = fopen($tmp = '/tmp/' . $table, 'w');
             fwrite($fp, rand() . "\n" . rand());
             fclose($fp);
             $query = sprintf("LOAD DATA LOCAL INFILE '%s' INTO TABLE %s", $tmp, $table);
             $imported = $this->mysql($query);
             unlink($tmp);
             $this->mysql('drop table ' . $table);
             $dropped = $this->mysql('desc ' . $table);
             if ($dropped !== NULL) {
                 print_msg(sprintf('Unable to drop test table %s - check that user has DROP TABLE permissions', $table), isset($this->options['verbose']), __FILE__, __LINE__, TRUE);
                 $valid = FALSE;
             } else {
                 if ($imported !== NULL) {
                     print_msg(sprintf('Validated MySQL connection by creating and importing data to a temporary table %s - table has been removed', $table), isset($this->options['verbose']), __FILE__, __LINE__);
                 } else {
                     $valid = FALSE;
                     print_msg(sprintf('MySQL connection successful, but unable to import data using LOAD DATA LOCAL INFILE. Check these permissions for this user'), isset($this->options['verbose']), __FILE__, __LINE__, TRUE);
                 }
             }
         } else {
             $valid = FALSE;
             print_msg(sprintf('MySQL connection failed - %s', $this->mysql() !== NULL ? 'user cannot create tables' : 'unable to connect to server'), isset($this->options['verbose']), __FILE__, __LINE__, TRUE);
         }
     }
     return $valid;
 }
 /**
  * validation method - may be overriden by sub-classes, but parent method 
  * should still be invoked. returns TRUE if db options are valid, FALSE 
  * otherwise
  * @return boolean
  */
 protected function validate()
 {
     if ($valid = parent::validate()) {
         if (!isset($this->options['db_name'])) {
             $valid = FALSE;
             print_msg('--db_name argument is required for --db bigquery', isset($this->options['verbose']), __FILE__, __LINE__, TRUE);
         } else {
             if ($this->bq() !== NULL) {
                 $valid = TRUE;
                 print_msg('BigQuery connection successful', isset($this->options['verbose']), __FILE__, __LINE__);
             } else {
                 print_msg('BigQuery connection failed', isset($this->options['verbose']), __FILE__, __LINE__, TRUE);
             }
         }
     }
     return $valid;
 }
 /**
  * validation method - may be overriden by sub-classes, but parent method 
  * should still be invoked. returns TRUE if db options are valid, FALSE 
  * otherwise
  * @return boolean
  */
 protected function validate()
 {
     if ($valid = parent::validate()) {
         if (isset($this->options['db_host'])) {
             if ($valid = ch_curl($this->getUrl(), 'HEAD', $this->getHeaders(), NULL, $this->getAuth())) {
                 print_msg('Successfully validated callback using HEAD request', isset($this->options['verbose']), __FILE__, __LINE__);
             } else {
                 print_msg('Unable to validate callback using HEAD request', isset($this->options['verbose']), __FILE__, __LINE__, TRUE);
             }
         } else {
             $valid = FALSE;
             print_msg('--db_host argument is required for --db callback', isset($this->options['verbose']), __FILE__, __LINE__, TRUE);
         }
     }
     return $valid;
 }
 /**
  * validation method - may be overriden by sub-classes, but parent method 
  * should still be invoked. returns TRUE if db options are valid, FALSE 
  * otherwise
  * @return boolean
  */
 protected function validate()
 {
     if (!isset($this->valid) && parent::validate()) {
         $this->valid = TRUE;
         $validate = array('db_librato_color' => array('color' => TRUE), 'db_librato_display_max' => array('min' => 0), 'db_librato_display_min' => array('min' => 0), 'db_librato_period' => array('min' => 0), 'db_librato_type' => array('option' => array('counter', 'gauge')), 'db_user' => array('required' => TRUE), 'db_pswd' => array('required' => TRUE));
         if ($validated = validate_options($this->options, $validate)) {
             $this->valid = FALSE;
             foreach ($validated as $param => $err) {
                 print_msg(sprintf('--%s is not valid: %s', $param, $err), isset($this->options['verbose']), __FILE__, __LINE__, TRUE);
             }
         } else {
             $valueCol = 'db_librato_value';
             if (!isset($this->options['db_librato_value'])) {
                 $valueCol = 'db_librato_count';
                 if (!isset($this->options['db_librato_count']) || !isset($this->options['db_librato_sum'])) {
                     $this->valid = FALSE;
                     print_msg(sprintf('If --db_librato_value is not set, both --db_librato_count and --db_librato_sum MUST be specified'), isset($this->options['verbose']), __FILE__, __LINE__, TRUE);
                 } else {
                     if (count($this->options['db_librato_count']) != count($this->options['db_librato_sum'])) {
                         $this->valid = FALSE;
                         print_msg(sprintf('--db_librato_count and --db_librato_sum must be repeated the same number of times'), isset($this->options['verbose']), __FILE__, __LINE__, TRUE);
                     } else {
                         if (isset($this->options['db_librato_name']) && count($this->options['db_librato_name']) != count($this->options['db_librato_count'])) {
                             $this->valid = FALSE;
                             print_msg(sprintf('--db_librato_name and --db_librato_count must be repeated the same number of times'), isset($this->options['verbose']), __FILE__, __LINE__, TRUE);
                         }
                     }
                 }
             } else {
                 if (isset($this->options['db_librato_name']) && count($this->options['db_librato_name']) != count($this->options['db_librato_value'])) {
                     $this->valid = FALSE;
                     print_msg(sprintf('--db_librato_name and --db_librato_value must be repeated the same number of times'), isset($this->options['verbose']), __FILE__, __LINE__, TRUE);
                 } else {
                     if (isset($this->options['db_librato_count']) || isset($this->options['db_librato_sum']) || isset($this->options['db_librato_max']) || isset($this->options['db_librato_min']) || isset($this->options['db_librato_sum_squares'])) {
                         $this->valid = FALSE;
                         print_msg(sprintf('--db_librato_value cannot be set with --db_librato_count, --db_librato_sum, --db_librato_max, --db_librato_min or --db_librato_sum_squares because these parameters are mutually exclusive'), isset($this->options['verbose']), __FILE__, __LINE__, TRUE);
                     }
                 }
             }
             if ($this->valid) {
                 foreach (array('db_librato_aggregate', 'db_librato_color', 'db_librato_count', 'db_librato_description', 'db_librato_display_max', 'db_librato_display_min', 'db_librato_display_name', 'db_librato_display_units_long', 'db_librato_display_units_short', 'db_librato_display_stacked', 'db_librato_display_transform', 'db_librato_max', 'db_librato_min', 'db_librato_measure_time', 'db_librato_period', 'db_librato_source', 'db_librato_sum', 'db_librato_summarize_function', 'db_librato_sum_squares', 'db_librato_type') as $param) {
                     if (isset($this->options[$param]) && count($this->options[$param]) != count($this->options[$valueCol]) && count($this->options[$param]) != 1) {
                         $this->valid = FALSE;
                         print_msg(sprintf('--%s can only be set once or %d times (once for each --%s)', $param, count($this->options[$valueCol]), $valueCol), isset($this->options['verbose']), __FILE__, __LINE__, TRUE);
                     }
                 }
             }
         }
         if ($this->valid) {
             // validate credentials using GET request
             $curl = ch_curl(self::LIBRATO_METRICS_API_URL, 'GET', NULL, NULL, sprintf('%s:%s', $this->options['db_user'], $this->options['db_pswd']), '200-299', TRUE);
             $this->valid = ($response = json_decode($curl, TRUE)) ? TRUE : FALSE;
             if ($curl === NULL) {
                 print_msg(sprintf('Librato API GET request to %s failed', self::LIBRATO_METRICS_API_URL), isset($this->options['verbose']), __FILE__, __LINE__, TRUE);
             } else {
                 if ($curl === FALSE) {
                     print_msg(sprintf('Librato API GET request to %s resulted in non 200 response code - API credentials may be invalid', self::LIBRATO_METRICS_API_URL), isset($this->options['verbose']), __FILE__, __LINE__, TRUE);
                 } else {
                     if ($curl && !$response) {
                         print_msg(sprintf('Librato API GET request to %s successful, but body did not contain valid JSON', self::LIBRATO_METRICS_API_URL), isset($this->options['verbose']), __FILE__, __LINE__, TRUE);
                     } else {
                         print_msg(sprintf('Librato API GET request to %s successful. There are %d existing metrics', self::LIBRATO_METRICS_API_URL, count($response['metrics'])), isset($this->options['verbose']), __FILE__, __LINE__);
                     }
                 }
             }
         }
     }
     return $this->valid;
 }
 /**
  * validation method - may be overriden by sub-classes, but parent method 
  * should still be invoked. returns TRUE if db options are valid, FALSE 
  * otherwise
  * @return boolean
  */
 protected function validate()
 {
     if ($valid = parent::validate()) {
         $table = 'test_' . $this->getTableName(rand());
         if ($this->psql('create table ' . $table . ' (id int)') !== NULL) {
             // try to load some data into the table
             $fp = fopen($tmp = '/tmp/' . $table, 'w');
             fwrite($fp, rand() . "\n" . rand());
             fclose($fp);
             $query = sprintf("\\COPY %s FROM '%s' CSV", $table, $tmp);
             $imported = $this->psql($query);
             unlink($tmp);
             $this->psql('drop table ' . $table);
             $dropped = $this->psql('\\d+ ' . $table);
             if ($dropped !== NULL) {
                 print_msg(sprintf('Unable to drop test table %s - check that user has DROP TABLE permissions', $table), isset($this->options['verbose']), __FILE__, __LINE__, TRUE);
                 $valid = FALSE;
             } else {
                 if ($imported !== NULL) {
                     print_msg(sprintf('Validated PostgreSQL connection by creating and importing data to a temporary table %s - table has been removed', $table), isset($this->options['verbose']), __FILE__, __LINE__);
                 } else {
                     $valid = FALSE;
                     print_msg(sprintf('PostgreSQL connection successful, but unable to import data using COPY. Check these permissions for this user'), isset($this->options['verbose']), __FILE__, __LINE__, TRUE);
                 }
             }
         } else {
             $valid = FALSE;
             print_msg(sprintf('PostgreSQL connection failed - %s', $this->psql() !== NULL ? 'user cannot create tables' : 'unable to connect to server'), isset($this->options['verbose']), __FILE__, __LINE__, TRUE);
         }
     }
     return $valid;
 }