1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10:
11: namespace OpenCloud\CloudMonitoring\Resource;
12:
13: use OpenCloud\CloudMonitoring\Exception;
14:
15: 16: 17:
18: class Check extends AbstractResource
19: {
20: 21: 22:
23: private $id;
24:
25: 26: 27:
28: private $type;
29:
30: 31: 32:
33: private $details;
34:
35: 36: 37:
38: private $disabled;
39:
40: 41: 42:
43: private $label;
44:
45: 46: 47:
48: private $period;
49:
50: 51: 52:
53: private $timeout;
54:
55: 56: 57: 58: 59: 60:
61: private $monitoring_zones_poll;
62:
63: 64: 65: 66: 67: 68:
69: private $target_alias;
70:
71: 72: 73: 74: 75:
76: private $target_hostname;
77:
78: 79: 80: 81:
82: private $target_resolver;
83:
84: protected static $json_name = false;
85: protected static $json_collection_name = 'values';
86: protected static $url_resource = 'checks';
87:
88: protected static $emptyObject = array(
89: 'type',
90: 'details',
91: 'disabled',
92: 'label',
93: 'period',
94: 'timeout',
95: 'monitoring_zones_poll',
96: 'target_alias',
97: 'target_hostname',
98: 'target_resolver'
99: );
100:
101: protected static $requiredKeys = array('type');
102:
103: protected $associatedResources = array('CheckType' => 'CheckType');
104:
105: protected $dataPointParams = array(
106: 'from',
107: 'to',
108: 'points',
109: 'resolution',
110: 'select'
111: );
112:
113: public function testUrl($debug = false)
114: {
115: $params = array();
116: if ($debug === true) {
117: $params['debug'] = 'true';
118: }
119: return $this->getParent()->url('test-check', $params);
120: }
121:
122: public function testExistingUrl($debug = false)
123: {
124: return $this->getUrl()->addPath('test');
125: }
126:
127: public function getMetrics()
128: {
129: return $this->getService()->resourceList('Metric', null, $this);
130: }
131:
132: public function getMetric($info = null)
133: {
134: return $this->getService()->resource('Metric', $info, $this);
135: }
136:
137: 138: 139: 140: 141: 142: 143: 144:
145: public function fetchDataPoints($metricName, array $options = array())
146: {
147: $metric = $this->getService()->resource('Metric', null, $this);
148:
149: $url = clone $metric->getUrl();
150: $url->addPath($metricName)->addPath('plot');
151:
152: $parts = array();
153:
154:
155: foreach (array('to', 'from', 'points') as $param) {
156: if (isset($options[$param])) {
157: $parts[$param] = $options[$param];
158: }
159: }
160:
161: if (!isset($parts['to'])) {
162: throw new Exception\MetricException(sprintf(
163: 'Please specify a "to" value'
164: ));
165: }
166:
167: if (!isset($parts['from'])) {
168: throw new Exception\MetricException(sprintf(
169: 'Please specify a "from" value'
170: ));
171: }
172:
173: if (isset($options['resolution'])) {
174: $allowedResolutions = array('FULL', 'MIN5', 'MIN20', 'MIN60', 'MIN240', 'MIN1440');
175: if (!in_array($options['resolution'], $allowedResolutions)) {
176: throw new Exception\MetricException(sprintf(
177: '%s is an invalid resolution type. Please use one of the following: %s',
178: $options['resolution'],
179: implode(', ', $allowedResolutions)
180: ));
181: }
182: $parts['resolution'] = $options['resolution'];
183: }
184:
185: if (isset($options['select'])) {
186: $allowedStats = array('average', 'variance', 'min', 'max');
187: if (!in_array($options['select'], $allowedStats)) {
188: throw new Exception\MetricException(sprintf(
189: '%s is an invalid stat type. Please use one of the following: %s',
190: $options['select'],
191: implode(', ', $allowedStats)
192: ));
193: }
194: $parts['select'] = $options['select'];
195: }
196:
197: if (!isset($parts['points']) && !isset($parts['resolution'])) {
198: throw new Exception\MetricException(sprintf(
199: 'Please specify at least one point or resolution value'
200: ));
201: }
202:
203: $url->setQuery($parts);
204:
205: return $this->getService()->resourceList('Metric', $url, $this);
206: }
207:
208: }