1 <?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
17
18 namespace OpenCloud\CloudMonitoring\Resource;
19
20 use OpenCloud\CloudMonitoring\Exception;
21
22 23 24
25 class Check extends AbstractResource
26 {
27 28 29
30 private $id;
31
32 33 34
35 private $type;
36
37 38 39
40 private $details;
41
42 43 44
45 private $disabled;
46
47 48 49
50 private $label;
51
52 53 54
55 private $period;
56
57 58 59
60 private $timeout;
61
62 63 64 65 66 67
68 private $monitoring_zones_poll;
69
70 71 72 73 74 75
76 private $target_alias;
77
78 79 80 81 82
83 private $target_hostname;
84
85 86 87 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 147 148 149 150 151 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
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('MetricDataPoint', $url, $this);
214 }
215 }
216