-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathuptimerobot.class.php
211 lines (188 loc) · 6.98 KB
/
uptimerobot.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
class UptimeRobot
{
private $base_uri = 'http://api.uptimerobot.com/';
private $apiKey;
private $format = "json";
private $json_encap = "jsonUptimeRobotApi()";
/**
* Public constructor function
*
* @param mixed $apiKey optional
* @return UptimeRobot
*/
public function __construct($apiKey = null)
{
$this->apiKey = $apiKey;
}
/**
* Returns the API key
*
*/
public function getApiKey()
{
return $this->apiKey;
}
/**
* Sets the API key
*
* @param string $apiKey required
*/
public function setApiKey($apiKey)
{
if (empty($apiKey)) {
throw new Exception('Value not specified: apiKey', 1);
}
$this->apiKey = $apiKey;
}
/**
* Gets output format of API calls
*
*/
public function getFormat()
{
return $this->format;
}
/**
* Sets output format of API calls
*
* @param mixed $format required
*/
public function setFormat($format)
{
if (empty($format)) {
throw new Exception('Value not specified: format', 1);
}
$this->format = $format;
}
/**
* Returns the result of the API calls
*
* @param mixed $url required
*/
private function __fetch($url)
{
if (empty($url)) {
throw new Exception('Value not specified: url', 1);
}
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 10);
$file_contents = curl_exec ($ch);
curl_close ($ch);
switch ($this->format) {
case "xml":
return $file_contents;
default:
return substr ($file_contents, strlen($this->json_encap) - 1, strlen ($file_contents) - strlen($this->json_encap));
}
return false;
}
/**
* This is a Swiss-Army knife type of a method for getting any information on monitors
*
* @param array $monitors optional (if not used, will return all monitors in an account.
* Else, it is possible to define any number of monitors with their IDs like: monitors=15830-32696-83920)
* @param bool $logs optional (defines if the logs of each monitor will be returned. Should be set to 1 for getting the logs. Default is 0)
* @param bool $alertContacts optional (defines if the notified alert contacts of each notification will be returned.
* Should be set to 1 for getting them. Default is 0. Requires logs to be set to 1)
*/
public function getMonitors($monitors = array(), $logs = 0, $alertContacts = 0)
{
if (empty($this->apiKey)) {
throw new Exception('Property not set: apiKey', 2);
}
$url = "{$this->base_uri}/getMonitors?apiKey={$this->apiKey}";
if (!empty($monitors)) $url .= "&monitors=" . implode('-', $monitors);
$url .= "&logs=$logs&alertContacts=$alertContacts&format={$this->format}";
return $this->__fetch($url);
}
/**
* New monitors of any type can be created using this method
*
* @param array $params
*
* $params can have the following keys:
* name - required
* uri - required
* type - required
* subtype - optional (required for port monitoring)
* port - optional (required for port monitoring)
* keyword_type - optional (required for keyword monitoring)
* keyword_value - optional (required for keyword monitoring)
*/
public function newMonitor($params = array())
{
if (empty($params['name']) || empty($params['uri']) || empty($params['type'])) {
throw new Exception('Required key "name", "uri" or "type" not specified', 3);
} else {
extract($params);
}
if (empty($this->apiKey)) {
throw new Exception('Property not set: apiKey', 2);
}
$url = "{$this->base_uri}/newMonitor?apiKey={$this->apiKey}&monitorFriendlyName=$name&monitorURL=$uri&monitorType=$type";
if (isset($subtype)) $url .= "&monitorSubType=$subtype";
if (isset($port)) $url .= "&monitorPort=$port";
if (isset($keyword_type)) $url .= "&monitorKeywordType=$keyword_type";
if (isset($keyword_value)) $url .= '&monitorKeywordValue='. urlencode($keyword_value);
$url .= "&format={$this->format}";
return $this->__fetch($url);
}
/**
* Monitors can be edited using this method.
*
* Important: The type of a monitor can not be edited (like changing a HTTP monitor into a Port monitor).
* For such cases, deleting the monitor and re-creating a new one is adviced.
*
* @param string $monitorId required
* @param array $params required
*
* $params can have the following keys:
* name - required
* uri - required
* type - required
* subtype - optional (required for port monitoring)
* port - optional (required for port monitoring)
* keyword_type - optional (required for keyword monitoring)
* keyword_value - optional (required for keyword monitoring)
*/
public function editMonitor($monitorId, $params = array())
{
if (empty($params)) {
throw new Exception('Value not specified: params', 1);
} else {
extract($params);
}
if (empty($this->apiKey)) {
throw new Exception('Property not set: apiKey', 2);
}
$url = "{$this->base_uri}/editMonitor?apiKey={$this->apiKey}&monitorID=$monitorId";
if (isset($name)) $url .= "&monitorFriendlyName=". urlencode($name);
if (isset($uri)) $url .= "&monitorURL=$uri";
if (isset($type)) $url .= "&monitorType=$type";
if (isset($subtype)) $url .= "&monitorSubType=$subtype";
if (isset($port)) $url .= "&monitorPort=$port";
if (isset($keyword_type)) $url .= "&monitorKeywordType=$keyword_type";
if (isset($keyword_value)) $url .= '&monitorKeywordValue='. urlencode($keyword_value);
$url .= "&format={$this->format}";
return $this->__fetch($url);
}
/**
* Monitors can be deleted using this method.
*
* @param string $monitorId required
*/
public function deleteMonitor($monitorId)
{
if (empty($monitorId)) {
throw new Exception('Value not specified: monitorId', 1);
}
if (empty($this->apiKey)) {
throw new Exception('Property not set: apiKey', 2);
}
$url = "{$this->base_uri}/deleteMonitor?apiKey={$this->apiKey}&monitorID=$monitorId&format={$this->format}";
return $this->__fetch($url);
}
}