-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.php
211 lines (172 loc) · 7.05 KB
/
monitor.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
/**
Site Monitor
Description
-----------------------------------------------------------------------------------------------
The script will check if one or more sites are online and will send you a notification.
It will stop after the maximum alerts is reached. At some point if the site is
still down the notifications will reset and you will start receiving them again.
When the site comes back online the flag file is deleted and you'll get another
notification. The notification emails are combined into one so you won't get multiple emails.
This script is useful when you have multiple domains or manage domains on behalf of your clients.
You want to be the first one to know when a website is down so you can quickly fix it.
Video Demo: http://www.youtube.com/watch?v=Sw93JUc55Tc
Usage:
-----------------------------------------------------------------------------------------------
1. Edit the config.php file and add your email(s) in APP_ALERT_EMAIL field.
Some carriers allow you to send an email to a specific email address which can be converted into text.
e.g. [email protected] for Telus customers.
2. Create a text file called: sites.txt in the same folder where this script resides.
You can check sites.sample.txt for some ideas. Each site has to be on its own line.
Comments are allowed and they should be prefixed by # (pound) sign. Empty lines are skipped.
3. Setup a cron job.
Running the script from console (cli) is the recommended approach as opposed to using lynx because
if your site is down the monitoring script won't get executed.
# Runs this script from a cron job every 5 minutes.
*/
// */5 0-23 * * * /usr/bin/php /path/to/monitor.php
/*
4. If you run into issues or want to suggest a feature submit a ticket at: https://github.com/orbisius/site-monitor/issues
5. If you are receiving multiple emails (more than the limit) please create a tmp folder in the same folder
where the script resides. Make sure it has write permissions.
@author Svetoslav Marinov (SLAVI) <slavi at orbisius.com>
@link http://orbisius.com
@copyright (c) 2013, Svetoslav (SLAVI) Marinov
@license LGPL
*/
// If the private config is present load it.
if (file_exists(dirname(__FILE__) . '/config_priv.php')) {
include_once(dirname(__FILE__) . '/config_priv.php');
} elseif (file_exists(dirname(__FILE__) . '/config.php')) {
include_once(dirname(__FILE__) . '/config.php');
}
$alert = 0;
$alert_online_sites = $alert_offline_sites = array();
if (!defined('APP_BASE_DIR')) {
define('APP_BASE_DIR', dirname(__FILE__));
}
if (!file_exists(APP_BASE_DIR . '/sites.txt')) {
die('Please create sites.txt in the current directory.' . "\n");
}
if (!defined('APP_ALERT_EMAIL')) {
define('APP_ALERT_EMAIL', 'admin@' . APP_HOST);
}
$sites = file(APP_BASE_DIR . '/sites.txt');
foreach ($sites as $site) {
// there could be comments or deactivated sites.
$site = preg_replace('#\s#si', '', $site);
$site = preg_replace('#\#.*#si', '', $site);
$site = trim($site);
if (empty($site)) {
continue;
}
// we need to save some space in the text/email
$site_clean = preg_replace('#^.+?:/+(?:www\.)?#si', '', $site);
if (!preg_match('#^https?://#si', $site)) {
$site = 'http://' . $site;
}
$alert_file = app_get_check_file($site);
if (!app_check_site($site)) { // site is down
if (file_exists($alert_file)) {
$check_times = file_get_contents($alert_file);
$check_times = empty($check_times) ? 0 : $check_times;
$check_times++;
$last_checked = filemtime($alert_file);
// Send a notification only if there is more than twice failed attempts
if ($check_times >= APP_ALERT_THRESHOLD
&& $check_times <= APP_ALERT_LIMIT) {
$alert++;
$alert_offline_sites[] = $site_clean;
file_put_contents($alert_file, $check_times);
} elseif (time() - $last_checked > APP_ALERT_RESET) { // should we resume alerts?
if (APP_ALERT_THRESHOLD == 1) {
$alert++;
$alert_offline_sites[] = $site_clean;
}
file_put_contents($alert_file, 1);
}
} else {
// no alert for the first time.
file_put_contents($alert_file, 1);
if (APP_ALERT_THRESHOLD == 1) {
$alert++;
$alert_offline_sites[] = $site_clean;
}
}
} else { // back online?
if (file_exists($alert_file)) {
$alert++;
unlink($alert_file);
$alert_online_sites[] = $site_clean;
}
}
} // foreach
if ($alert) { // create a short message so it fits in txt msg
//$message = "Checked on: " . date('r') . "\n";
$subject = 'alert: ';
$message = '';
if (!empty($alert_online_sites) && !empty($alert_offline_sites)) {
$subject .= 'Mixed';
} elseif (!empty ($alert_online_sites)) {
$subject .= 'Online';
} elseif (!empty ($alert_offline_sites)) {
$subject .= 'Offline';
}
if (!empty($alert_online_sites)) {
$message .= "\nOnline:" . join(",", $alert_online_sites);
}
if (!empty($alert_offline_sites)) {
$message .= "\nOffline:" . join(",", $alert_offline_sites);
}
mail(APP_ALERT_EMAIL, $subject, $message, "From: alerts@" . APP_HOST . "\r\n");
}
/**
* Generates the alert file based on the site name in TMP dir.
* the file will match the domain name: e.g. domain_com.txt
* @param string $site
* @return string
*/
function app_get_check_file($site) {
if (!is_dir(APP_TMP_DIR)) {
mkdir(APP_TMP_DIR, 0777, 1);
}
$alert_file = $site;
$alert_file = preg_replace('#^.+?:/+(?:www\.)?#si', '', $alert_file);
$alert_file = preg_replace('#[^\w-]#si', '_', $alert_file);
$alert_file = preg_replace('#_+#', '_', $alert_file);
$alert_file = trim($alert_file, ' _');
$alert_file = APP_TMP_DIR . '/' . $alert_file . '.txt';
return $alert_file;
}
/**
*
* @param string $url
* @return boolean
*/
function app_check_site($url) {
if (defined('APP_ALERT_USER_AGENT')) {
$agent = APP_ALERT_USER_AGENT;
} else {
$agent = "Mozilla/5.0 (compatible; OrbisiusSiteMonitor/1.0; +http://orbisius.com)";
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSLVERSION, 3);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$content = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if (($http_code >= 200 && $http_code <= 300) // (!empty($content) &&
|| $http_code == 301 || $http_code == 302 || $http_code == 307) { // allow redirects
return true;
} else {
return false;
}
}