-
Notifications
You must be signed in to change notification settings - Fork 0
/
uptime.php
113 lines (83 loc) · 1.5 KB
/
uptime.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
<?php
if (PHP_SAPI !== 'cli') {
die('Must be from command line');
}
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
define('ROOT_PATH', realpath(dirname(__FILE__)).'/');
$host = isset($argv[1]) ? $argv[1] : false;
$port = isset($argv[2]) ? $argv[2] : 80;
$email = isset($argv[3]) ? $argv[3] : false;
if($host === false) {
die('No host');
}
try {
check_status($host, $port);
} catch (ErrorException $e) {
handle_failure($host.$port);
}
function check_status($host, $port = 80)
{
$waitTimeoutInSeconds = 1;
$fp = @fsockopen($host,$port,$errCode,$errStr,$waitTimeoutInSeconds);
if ($fp)
{
handle_success($host.$port);
fclose($fp);
}
else
{
handle_failure($host.$port);
}
}
function send_email($subject, $message = "")
{
global $email;
if(!$email) {
return false;
}
return mail ( $email, $subject, $message);
}
function handle_failure($file)
{
if(lock_exists($file))
{
// do nothing
}
else
{
if(create_lock($file))
{
send_email("Downtime Detected for $file");
}
else
{
// could not create lock file
}
}
}
function handle_success($file)
{
if(lock_exists($file))
{
if(remove_lock($file))
{
send_email("Uptime Detected for $file");
} else {
// could not remove lock
}
}
}
function remove_lock($file)
{
return unlink($file.".lock");
}
function create_lock($file)
{
return fopen($file.".lock", "w");
}
function lock_exists($file)
{
return file_exists(ROOT_PATH.$file.".lock");
}