      1: <?php
      2: /**
      3:  * Copyright 2012-2014 Rackspace US, Inc.
      4:  *
      5:  * Licensed under the Apache License, Version 2.0 (the "License");
      6:  * you may not use this file except in compliance with the License.
      7:  * You may obtain a copy of the License at
      8:  *
      9:  * http://www.apache.org/licenses/LICENSE-2.0
     10:  *
     11:  * Unless required by applicable law or agreed to in writing, software
     12:  * distributed under the License is distributed on an "AS IS" BASIS,
     13:  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14:  * See the License for the specific language governing permissions and
     15:  * limitations under the License.
     16:  */
     17:
     18: namespace OpenCloud\CloudMonitoring\Resource;
     19:
     20: use OpenCloud\CloudMonitoring\Exception;
     21:
     22: /**
     23:  * Check class.
     24:  */
     25: class Check extends AbstractResource
     26: {
     27:     /**
     28:      * @var string
     29:      */
     30:     private $id;
     31:
     32:     /**
     33:      * @var string|CheckType The type of check.
     34:      */
     35:     private $type;
     36:
     37:     /**
     38:      * @var array Details specific to the check type.
     39:      */
     40:     private $details;
     41:
     42:     /**
     43:      * @var bool Disables the check.
     44:      */
     45:     private $disabled;
     46:
     47:     /**
     48:      * @var string A friendly label for a check.
     49:      */
     50:     private $label;
     51:
     52:     /**
     53:      * @var int The period in seconds for a check. The value must be greater than the minimum period set on your account.
     54:      */
     55:     private $period;
     56:
     57:     /**
     58:      * @var int The timeout in seconds for a check. This has to be less than the period.
     59:      */
     60:     private $timeout;
     61:
     62:     /**
     63:      * For remote checks only. List of monitoring zones to poll from. Note: This argument is only required for remote
     64:      * (non-agent) checks.
     65:      *
     66:      * @var array
     67:      */
     68:     private $monitoring_zones_poll;
     69:
     70:     /**
     71:      * For remote checks only. A key in the entity's 'ip_addresses' hash used to resolve this check to an IP address.
     72:      * This parameter is mutually exclusive with target_hostname.
     73:      *
     74:      * @var string
     75:      */
     76:     private $target_alias;
     77:
     78:     /**
     79:      * For remote checks only. The hostname this check should target. This parameter is mutually exclusive with target_alias.
     80:      *
     81:      * @var string
     82:      */
     83:     private $target_hostname;
     84:
     85:     /**
     86:      * For remote checks only. Determines how to resolve the check target.
     87:      * @var string
     88:      */
     89:     private $target_resolver;
     90:
     91:     protected static $json_name = false;
     92:     protected static $json_collection_name = 'values';
     93:     protected static $url_resource = 'checks';
     94:
     95:     protected static $emptyObject = array(
     96:         'type',
     97:         'details',
     98:         'disabled',
     99:         'label',
    100:         'period',
    101:         'timeout',
    102:         'monitoring_zones_poll',
    103:         'target_alias',
    104:         'target_hostname',
    105:         'target_resolver'
    106:     );
    107:
    108:     protected static $requiredKeys = array('type');
    109:
    110:     protected $associatedResources = array('CheckType' => 'CheckType');
    111:
    112:     protected $dataPointParams = array(
    113:         'from',
    114:         'to',
    115:         'points',
    116:         'resolution',
    117:         'select'
    118:     );
    119:
    120:     public function testUrl($debug = false)
    121:     {
    122:         $params = array();
    123:         if ($debug === true) {
    124:             $params['debug'] = 'true';
    125:         }
    126:
    127:         return $this->getParent()->url('test-check', $params);
    128:     }
    129:
    130:     public function testExistingUrl($debug = false)
    131:     {
    132:         return $this->getUrl()->addPath('test');
    133:     }
    134:
    135:     public function getMetrics()
    136:     {
    137:         return $this->getService()->resourceList('Metric', null, $this);
    138:     }
    139:
    140:     public function getMetric($info = null)
    141:     {
    142:         return $this->getService()->resource('Metric', $info, $this);
    143:     }
    144:
    145:     /**
    146:      * Fetch particular data points.
    147:      *
    148:      * @param string $metricName
    149:      * @param array  $options
    150:      * @return mixed
    151:      * @throws \OpenCloud\CloudMonitoring\Exception\MetricException
    152:      */
    153:     public function fetchDataPoints($metricName, array $options = array())
    154:     {
    155:         $metric = $this->getService()->resource('Metric', null, $this);
    156:
    157:         $url = clone $metric->getUrl();
    158:         $url->addPath($metricName)->addPath('plot');
    159:
    160:         $parts = array();
    161:
    162:         // Timestamps
    163:         foreach (array('to', 'from', 'points') as $param) {
    164:             if (isset($options[$param])) {
    165:                 $parts[$param] = $options[$param];
    166:             }
    167:         }
    168:
    169:         if (!isset($parts['to'])) {
    170:             throw new Exception\MetricException(sprintf(
    171:                 'Please specify a "to" value'
    172:             ));
    173:         }
    174:
    175:         if (!isset($parts['from'])) {
    176:             throw new Exception\MetricException(sprintf(
    177:                 'Please specify a "from" value'
    178:             ));
    179:         }
    180:
    181:         if (isset($options['resolution'])) {
    182:             $allowedResolutions = array('FULL', 'MIN5', 'MIN20', 'MIN60', 'MIN240', 'MIN1440');
    183:             if (!in_array($options['resolution'], $allowedResolutions)) {
    184:                 throw new Exception\MetricException(sprintf(
    185:                     '%s is an invalid resolution type. Please use one of the following: %s',
    186:                     $options['resolution'],
    187:                     implode(', ', $allowedResolutions)
    188:                 ));
    189:             }
    190:             $parts['resolution'] = $options['resolution'];
    191:         }
    192:
    193:         if (isset($options['select'])) {
    194:             $allowedStats = array('average', 'variance', 'min', 'max');
    195:             if (!in_array($options['select'], $allowedStats)) {
    196:                 throw new Exception\MetricException(sprintf(
    197:                     '%s is an invalid stat type. Please use one of the following: %s',
    198:                     $options['select'],
    199:                     implode(', ', $allowedStats)
    200:                 ));
    201:             }
    202:             $parts['select'] = $options['select'];
    203:         }
    204:
    205:         if (!isset($parts['points']) && !isset($parts['resolution'])) {
    206:             throw new Exception\MetricException(sprintf(
    207:                 'Please specify at least one point or resolution value'
    208:             ));
    209:         }
    210:
    211:         $url->setQuery($parts);
    212:
    213:         return $this->getService()->resourceList('Metric', $url, $this);
    214:     }
    215: }
    216: