-
Notifications
You must be signed in to change notification settings - Fork 3
/
wheresitfast.php
122 lines (99 loc) · 3.53 KB
/
wheresitfast.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
<?php
define('clientID', "");
define('token', "");
/*
* You're clearly welcome to edit what comes later, but you shouldn't need to.
*
* A few notes:
* - The script uses a sleep(5) before submitting the job and checking the first
* time, and then before every subsequent check. This is a long time in computing
* worlds, but remember that the server has to do a lot of work. Dropping this to
* zero is a great way to get throttled.
* - http_build_query() wories about escaping for us
* - Designed to integrate with Nagios it uses return codes 0, 2, and 3. Which
* represent Success, Failure, and Unknown respectively. This simple implementaiton
* doesn't present warn.
* - Can also return simply the value in MS, useful for integration in other tools, like cacti
* Source: https://github.com/preinheimer/wiu-scripts
* Integrates with: http://api.wheresitup.com/
*/
if(!isset($argv[1]) OR !isset($argv[2]))
{
echo "Usage: {$argv[0]} <server name> <check source> [maximum acceptable time in ms]\n";
echo "Example: {$argv[0]} http://example.com newyork 2500\n";
echo "or\n";
echo "Example: {$argv[0]} http://example.com newyork\n";
exit(3);
}
$uri = $argv[1];
$server = $argv[2];
$maxTime = isset($argv[3]) ? $argv[3] : null;
$jobID = submitJob($uri, $server, array('fast'));
$done = false;
$attemptCount = 0;
do {
sleep(5);
unset($ch);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://adev.wheresitup.com/v2/retrieve/$jobID");
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Auth: Bearer " . clientID . ' ' . token));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$ret = curl_exec($ch);
$results = json_decode($ret, true);
//var_dump($results);
if (isset($results['return']['summary'][$server]['fast']['status']))
{
$status = $results['return']['summary'][$server]['fast']['status'];
$time = $results['return']['summary'][$server]['fast']['time'];
if ($status != "success")
{
echo "[{$jobID}] Page load failure\n";
return 2;
}
//Handle people who just want the time, not in nagios
if (is_null($maxTime))
{
echo $time;
return 0;
}
if ($time > $maxTime)
{
echo "[{$jobID}] Maxtime exceeded, limit was {$maxTime}ms, page took {$time}ms\n";
exit(2);
}
echo "Success\n";
exit(0);
}
if(++$attemptCount == 10)
{
//Something may be wrong with the API
echo "Where's it Up not responding!";
exit(3);
$done = true;
}
}while(!$done);
function submitJob($uri, $server, $services)
{
$ch = curl_init();
$data = http_build_query(array('services' => $services, 'sources' => array($server), 'uri' => $uri));
curl_setopt($ch, CURLOPT_URL, 'http://adev.wheresitup.com/v2/submit');
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Auth: Bearer " . clientID . ' ' . token));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
//curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$ret = curl_exec($ch);
$results = json_decode($ret, true);
if(is_null($results))
{
echo $data;
echo "API Call error: $ret \n";
exit(3);
}
if (!isset($results['jobID']))
{
echo "API Call error: $ret \n";
exit(3);
}
return $results['jobID'];
}