forked from totoCZ/Runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc.cron.php
135 lines (109 loc) · 2.46 KB
/
func.cron.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
<?php
require './lib/Stachl_Ping.php';
stream_context_set_default(
array(
'http' => array(
'method' => 'HEAD',
'user_agent' => 'Mozilla/5.0 (compatible; RuntimeBot)',
'timeout' => '10'
)
)
);
function getStatus($type, $domain, $port, $match = '') {
if($type == 'Ping') {
$ping = new Stachl_Ping($domain, 1);
$ping->ping();
$status = $ping->getAvg();
if($status) {
return round($status);
}
else {
return 9999;
}
}
if($type == 'HTTP GET' && $match == '') {
// optimize, haha
$type = 'HTTP';
}
if($port == 0) {
switch($type) {
case 'HTTP':
$port = 80;
break;
case 'HTTP GET':
$port = 80;
break;
case 'TCP':
break;
}
}
$starttime = microtime(true);
switch($type) {
case 'HTTP':
$status = httpHeadCheck( 'http://' . $domain . ':' . $port );
break;
case 'HTTP GET':
$status = httpGetCheck( 'http://' . $domain . ':' . $port, $match);
break;
case 'TCP':
$status = fsockCheck( 'tcp://' . $domain, $port );
break;
}
$stoptime = microtime(true);
if (!$status || $status == 0) {
$status = 9999;
} else {
$status = ($stoptime - $starttime) * 1000;
$status = round($status);
}
return $status;
}
function fsockCheck($domain, $port){
$fp = @fsockopen($domain, $port, $errno, $errstr, 10);
if(!$fp) {
return false;
}
fclose($fp);
return true;
}
function httpHeadCheck($url) {
$headers = @get_headers($url);
if (!$headers
&& $headers[0] != 'HTTP/1.1 200 OK'
&& $headers[0] != 'HTTP/1.0 200 OK') {
return false;
}
return true;
}
function httpGetCheck($url, $match) {
$ctxGet = stream_context_create(
array(
'http' => array(
'method' => 'GET',
'user_agent' => 'Mozilla/5.0 (compatible; RuntimeBot)',
'timeout' => '10'
)
)
);
$page = @file_get_contents($url, false, $ctxGet);
if(!$page) {
return false;
}
if (!preg_match('/'. $match .'/i', $page)) {
return false;
}
return true;
}
function alertService($service) {
$alert_email = dibi::query('select email from users where id = %i', $service->user_id)->fetchSingle();
$name = $service->hostname . ' (' . $service->info . ')';
require_once './lib/nette.min.php';
$mail = new Nette\Mail\Message;
$mail->setFrom('Runtime Cron <root@localhost.localdomain>')
->addTo($alert_email)
->setSubject('Your service ' . $name . ' is down!')
->setBody("Hi, your service ".$name." has stopped responding. Please check it on the Runtime website.")
->send();
return true;
}
?>