showstats.php 2.12 KB
Newer Older
1 2
<?php
//* Copyright (c) 2011-2016, David Morley. This file is licensed under the Affero General Public License version 3 or later. See the COPYRIGHT file. */
3 4 5 6 7
$debug = 1;

// Other parameters.
$_domain = $_GET['domain'] ?? null;

8
require_once __DIR__ . '/config.php';
9

dmorley's avatar
dmorley committed
10
$dbh = pg_connect("dbname=$pgdb user=$pguser password=$pgpass");
11 12
$dbh || die('Error in connection: ' . pg_last_error());

dmorley's avatar
dmorley committed
13
$sql    = 'SELECT stats_apikey FROM pods WHERE domain = $1';
14
$result = pg_query_params($dbh, $sql, [$_domain]);
15 16
$result || die('Error in SQL query: ' . pg_last_error());

17
$apikey = pg_fetch_all($result);
18
$upti   = curl_init();
dmorley's avatar
dmorley committed
19
$key    = $apikey[0]['stats_apikey'];
20
$data   = ['all_time_uptime_ratio' => 1, 'format' => 'json', 'custom_uptime_ratios' => '7-30-60-90', 'response_times' => 1, 'response_times_average' => 86400, 'api_key' => $key, 'callback' => 'jsonpUptimeRobot'];
21
curl_setopt($upti, CURLOPT_URL, 'https://api.uptimerobot.com/v2/getMonitors');
dmorley's avatar
dmorley committed
22
curl_setopt($upti, CURLOPT_HEADER, 0);
dmorley's avatar
dmorley committed
23 24
curl_setopt($upti, CURLOPT_POST, 1);
curl_setopt($upti, CURLOPT_POSTFIELDS, http_build_query($data));
dmorley's avatar
dmorley committed
25 26
curl_setopt($upti, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($upti, CURLOPT_CONNECTTIMEOUT, 8);
27 28 29 30 31
$uptimerobot = curl_exec($upti);
curl_close($upti);
$json_encap = 'jsonpUptimeRobot()';
$up2        = substr($uptimerobot, strlen($json_encap) - 1, strlen($uptimerobot) - strlen($json_encap));
$uptr       = json_decode($up2);
32 33 34 35 36 37
echo '<b>UptimeRobot Json displayed in Human</b><br><br>';
echo 'Name: ' . $uptr->monitors[0]->friendly_name . '<br>';
echo 'Url: ' . $uptr->monitors[0]->url . '<br>';
echo 'Interval: ' . $uptr->monitors[0]->interval . 'ms<br>';
echo 'Uptime: ' . $uptr->monitors[0]->all_time_uptime_ratio . '%<br>';
echo 'Response Time: ' . round($uptr->monitors[0]->average_response_time) . 'ms<br>';
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
if ($uptr->monitors[0]->status == 2) {
  $live = 'Up';
}
if ($uptr->monitors[0]->status == 0) {
  $live = 'Paused';
}
if ($uptr->monitors[0]->status == 1) {
  $live = 'Not Checked Yet';
}
if ($uptr->monitors[0]->status == 8) {
  $live = 'Seems Down';
}
if ($uptr->monitors[0]->status == 9) {
  $live = 'Down';
}
53
echo 'Status: ' . $live;
dmorley's avatar
dmorley committed
54 55
pg_free_result($result);
pg_close($dbh);