-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathupdate-uptimerobot-heartbeat-urls.php
executable file
·80 lines (75 loc) · 3.52 KB
/
update-uptimerobot-heartbeat-urls.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
<?php
/***********************************************************************************************************
*
* Sporks, the learning, scriptable Discord bot!
*
* Copyright 2019 Craig Edwards <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
* Sporks UptimeRobot Heartbeat Ping
*
* This script should be run every minute under crontab, and uses UptimeRobot's API to retrieve the
* heartbeat url for each monitor associated with a discord shard. It will find these by requesting all
* monitors which contain the string "Discord Bot Shard " in their name, and are of type 5 (heartbeat).
* Once they are retrieved it will iterate them and match them to a regular expression by friendly name,
* extracting the shard ID from the name. It then does two things:
*
* 1) Update the database to store the last uptimerobot heartbeat url to be pinged under infobot_shard_status
*
* 2) If the shard is both online and connected, ping the endpoint with a simple GET request to let
* uptimerobot know this shard is alive
*
* If this script does not ping one of the uptimerobot heartbeat endpoints for 4 minutes (two heartbeats)
* then uptimerobot will POST to a discord webhook which causes an embed message in #status on the official
* discord server.
*
***********************************************************************************************************/
$settings = json_decode(file_get_contents("config.json"));
$conn = mysqli_connect($settings->dbhost, $settings->dbuser, $settings->dbpass);
if (!$conn) {
die("Can't connect to database, check config.json\n");
}
mysqli_select_db($conn, $settings->dbname);
$payload = [
'api_key' => $settings->utr_readonly_key,
'format' => 'json',
'logs' => '0',
'types' => '5',
'search' => 'Discord Bot Shard ',
];
$context = [
'http' => [
'method' => 'POST',
'ignore_errors' => true,
'header' => "Content-Type: application/x-www-form-urlencoded\r\nCache-Control: no-cache",
'content' => http_build_query($payload),
]
];
$response = @file_get_contents("https://api.uptimerobot.com/v2/getMonitors", false, stream_context_create($context));
$monitors = json_decode($response);
foreach($monitors->monitors as $monitor) {
if (preg_match("/^Discord Bot Shard (\d+)$/", $monitor->friendly_name, $m)) {
$shard_id = $m[1];
$url = $monitor->url;
$shardinfo = mysqli_fetch_object(mysqli_query($conn, "SELECT * FROM infobot_shard_status WHERE id = " . mysqli_real_escape_string($conn, $m[1])));
if ($shardinfo) {
mysqli_query($conn, "UPDATE infobot_shard_status SET uptimerobot_heartbeat = '" . mysqli_real_escape_string($conn, $monitor->url) . "' WHERE id = " . mysqli_real_escape_string($conn, $m[1]));
if ($shardinfo->connected && $shardinfo->online && time() - date('Z') - strtotime($shardinfo->updated) < 90) {
$utr_response = file_get_contents($monitor->url);
mysqli_query($conn, "UPDATE infobot_shard_status SET uptimerobot_response = '" . mysqli_real_escape_string($conn, $utr_response) . "' WHERE id = " . mysqli_real_escape_string($conn, $m[1]));
}
}
}
}