-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaemon.php
More file actions
102 lines (91 loc) · 1.93 KB
/
Copy pathdaemon.php
File metadata and controls
102 lines (91 loc) · 1.93 KB
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
#!/usr/bin/php
<?php
use classes\SiteChecker;
use classes\CurlRequest;
use classes\responseBodyNotEmpty;
spl_autoload_register(function ($class) {
require_once __DIR__ . '/' . str_replace('\\', '/', $class). '.php';
});
$pid = pcntl_fork();
if ($pid === -1) {
die('Could not fork!');
} elseif ($pid) {
exit;
} else {
// this is the child process
posix_setsid();
$parameters = [
'logfile' => false,
'interval' => 10,
'iterations' => 10,
'email' => false,
'url' => ''
];
$inputParameters = $argv;
foreach($inputParameters as $index=>$parameter)
{
if(0 == $index)
{
continue;
}
$tmpArr = explode('=', $parameter);
$parameters[$tmpArr[0]] = $tmpArr[1];
}
if(!$parameters['url'])
{
exit('need url');
}
$c = new CurlRequest();
$c->setTimeLimit(20);
$checker = new SiteChecker($c);
$checker->addBodyChecker(new responseBodyNotEmpty());
$prevStatus = true;
$iteration = 0;
$logfile = $parameters['logfile'];
$email = $parameters['email'];
if($logfile)
{
file_put_contents($logfile,'');
}
while(1) {
$iteration++;
$url = $parameters['url'];
$status = $checker->isSiteWorking($url);
$errors = [];
if(!$status)
{
$errors = $checker->getErrors();
}
if($prevStatus != $status)
{
$message = '';
if(count($errors) && $iteration == $parameters['iterations'])
{
$message = 'Site ' . $url . ' is down, reasons: ' . "\n";
foreach($errors as $error)
{
$message .= $error . "\n";
}
}
if(!count($errors))
{
$message = 'Site '. $url . ' in normal status' . "\n";
}
if($message)
{
if($logfile)
{
$content = file_get_contents($logfile);
file_put_contents($logfile, date('Y-m-d H:i:s') . "\n" . $content . "\n" . $message);
}
if($email)
{
$subject = 'the subject';
mail($email, 'Watcher\'s report', $message);
}
$prevStatus = $status;
}
}
sleep($parameters['interval']);
}
}