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

dmorley's avatar
cleanup  
dmorley committed
6
$dbh = pg_connect("dbname=$pgdb user=$pguser password=$pgpass");
7 8
$dbh || die('Error in connection: ' . pg_last_error());

9 10 11
$domain = isset($_GET['domain']) ? $_GET['domain'] : null;
$sql    = "SELECT pingdomurl FROM pods WHERE domain = $1";
$result = pg_query_params($dbh, $sql, [$domain]);
12 13
$result || die('Error in SQL query: ' . pg_last_error());

14
$apikey = pg_fetch_all($result);
15 16 17
$upti   = curl_init();
$key    = $apikey[0]['pingdomurl'];
$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'];
18
curl_setopt($upti, CURLOPT_URL, 'https://api.uptimerobot.com/v2/getMonitors');
dmorley's avatar
cleanup  
dmorley committed
19
curl_setopt($upti, CURLOPT_HEADER, 0);
dmorley's avatar
dmorley committed
20 21
curl_setopt($upti, CURLOPT_POST, 1);
curl_setopt($upti, CURLOPT_POSTFIELDS, http_build_query($data));
dmorley's avatar
cleanup  
dmorley committed
22 23
curl_setopt($upti, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($upti, CURLOPT_CONNECTTIMEOUT, 8);
24 25 26 27 28
$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);
29 30 31 32 33 34
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>';
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
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';
}
50
echo 'Status: ' . $live;
dmorley's avatar
cleanup  
dmorley committed
51 52
pg_free_result($result);
pg_close($dbh);