-
Notifications
You must be signed in to change notification settings - Fork 0
/
monkeybrainwave.php
79 lines (60 loc) · 2.02 KB
/
monkeybrainwave.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
<?php
class UptimeCheckFailureException extends Exception {}
class MonkeyBrainWave {
private $api = '';
private $warn = 4;
private $down = 9;
public function __construct() {
$this->load_config();
}
function load_config($configFile = './config.json') {
$jsonConfig = file_get_contents($configFile);
$config = json_decode($jsonConfig, true);
if (isset($config['api'])) {
$this->api = $config['api'];
}
if (isset($config['warn']) && is_int($config['warn'])) {
$this->warn = $config['warn'];
}
if (isset($config['outage']) && is_int($config['outage'])) {
$this->outage = $config['outage'];
}
}
function curl_get($url) {
$c = curl_init();
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_URL, $url);
$contents = curl_exec($c);
curl_close($c);
if ($contents) {
return $contents;
} else {
return false;
}
}
function fetch_status() {
$data = $this->curl_get($this->api);
$status = json_decode($data, true);
if ($status !== NULL) {
return $status;
}
throw new UptimeCheckFailureException("⚠️ Unable to fetch uptime status; no valid data returned from Monkeybrians API");
}
function check_neighborhood($neighborhood) {
$current_status = $this->fetch_status();
$outages = 0;
if (!isset($current_status[$neighborhood])) {
throw new UptimeCheckFailureException("⚠️ There is no outage information $neighborhood. Check spelling and case.");
}
if (isset($current_status[$neighborhood]['outage'])) {
$outages = ($current_status[$neighborhood]['outage']);
}
if ($outages > $this->down) {
throw new UptimeCheckFailureException("🙈 Likely internet service interruption: $neighborhood currently has $outages Monkeybrains outages");
}
if ($outages > $this->warn) {
throw new UptimeCheckFailureException("🙊 Possible internet service disruption: $neighborhood currently has $outages Monkeybrains outages");
}
return true;
}
}