-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwheresitup.php
137 lines (119 loc) · 4.1 KB
/
wheresitup.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
define('clientID', "506a121ea7ec61e60605dc4d");
define('token', "99cd47cda39ddb8e91a2cb0606dc7a6a");
/*
* 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.
* - When the check fails it immediately requests a second check against that URL,
* including Traceroute, Ping, and DNS lookups. While the results of these lookups
* aren't available in the echo'd result, the link to them is. This should help
* diagnose problems.
*
* 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 ]} <URL> <check source> [accept redirects]\n";
echo "Example: {$argv[ 0 ]} http://example.com newyork yes\n";
exit( 3 );
}
$uri = $argv[ 1 ];
$server = $argv[ 2 ];
$followRedirects = false;
if( isset( $argv[ 3 ] ) and $argv[ 3 ] === "yes" )
{
$followRedirects = true;
}
$jobID = submitJob( $uri, $server, array( 'http' ) );
$done = false;
$attemptCount = 0;
do
{
sleep( 5 );
unset( $ch );
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, "http://api.wheresitup.com/v3/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 );
if( $results[ 'return' ][ 'summary' ][ $argv[ 2 ] ][ 'http' ] == "in progress" )
{
$attemptCount++;
}
else
{
if( $followRedirects )
{
$key = count($results['return']['summary'][$server]['http']) - 1;
}
else
{
$key = 0;
}
//Redirects will push data into later elements
if( isset( $results[ 'return' ][ 'summary' ][ $server ][ 'http' ][ $key ][ 'responseCode' ] ) && $results[ 'return' ][ 'summary' ][ $server ][ 'http' ][ $key ][ 'responseCode' ] == 200 )
{
echo "Success\n";
exit( 0 );
}
else
{
if( isset( $results[ 'return' ][ 'summary' ][ $server ][ 'http' ][ $key ][ 'responseCode' ] ) )
{
echo "Expected Status Code 200, received: " . $results['return']['summary'][$server]['http'][$key]['responseCode'] . "\n";
}
else
{
echo "Expected Status Code 200, none received\n";
}
echo implode( "\n", $results[ 'return' ][ 'raw' ][ 'results' ][ $server ][ 'http' ][ 0 ][ 'full' ] );
echo "\n\nJob ID: $jobID\n";
$followUpID = submitJob( $uri, $server, array( 'http', 'trace', 'dig', 'ping') );
echo "Detailed follow up document: http://wheresitup.com/results/$followUpID\n";
exit( 2 );
}
$done = true;
}
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://api.wheresitup.com/v3/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_RETURNTRANSFER, 1 );
$ret = curl_exec( $ch );
$results = json_decode( $ret, true );
if( is_null( $results ) )
{
echo "API Call error: $ret \n";
exit( 3 );
}
if( !isset( $results[ 'jobID' ] ) )
{
echo "API Call error: $ret \n";
exit( 3 );
}
return $results[ 'jobID' ];
}