commit | author | age
|
dead5c
|
1 |
<?php |
V |
2 |
|
|
3 |
/* |
996bad
|
4 |
Copyright (c) 2007-2011, Till Brehm, projektfarm Gmbh and Oliver Vogel www.muv.com |
M |
5 |
All rights reserved. |
dead5c
|
6 |
|
996bad
|
7 |
Redistribution and use in source and binary forms, with or without modification, |
M |
8 |
are permitted provided that the following conditions are met: |
dead5c
|
9 |
|
V |
10 |
* Redistributions of source code must retain the above copyright notice, |
996bad
|
11 |
this list of conditions and the following disclaimer. |
dead5c
|
12 |
* Redistributions in binary form must reproduce the above copyright notice, |
996bad
|
13 |
this list of conditions and the following disclaimer in the documentation |
M |
14 |
and/or other materials provided with the distribution. |
dead5c
|
15 |
* Neither the name of ISPConfig nor the names of its contributors |
996bad
|
16 |
may be used to endorse or promote products derived from this software without |
M |
17 |
specific prior written permission. |
dead5c
|
18 |
|
996bad
|
19 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
M |
20 |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
21 |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
|
22 |
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
|
23 |
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
|
24 |
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
25 |
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
|
26 |
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
27 |
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
|
28 |
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
dead5c
|
29 |
*/ |
V |
30 |
|
|
31 |
class monitor_tools { |
|
32 |
|
|
33 |
//** Get distribution identifier |
|
34 |
//** IMPORTANT! |
|
35 |
// This is the same code as in install/lib/install.lib.php |
|
36 |
// So if you change it here, you also have to change it in there! |
|
37 |
// Please do not forget to remove the swriteln(); - lines here at this file |
|
38 |
public function get_distname() { |
|
39 |
|
|
40 |
$distname = ''; |
|
41 |
$distver = ''; |
|
42 |
$distid = ''; |
|
43 |
$distbaseid = ''; |
|
44 |
|
|
45 |
//** Debian or Ubuntu |
|
46 |
if (file_exists('/etc/debian_version')) { |
f35123
|
47 |
if (strstr(trim(file_get_contents('/etc/issue')), 'Ubuntu')) { |
T |
48 |
if (strstr(trim(file_get_contents('/etc/issue')), 'LTS')) { |
|
49 |
$lts=" LTS"; |
|
50 |
} else { |
|
51 |
$lts=""; |
|
52 |
} |
|
53 |
|
d281bb
|
54 |
$issue=file_get_contents('/etc/issue'); |
C |
55 |
$distname = 'Ubuntu'; |
|
56 |
$distid = 'debian40'; |
|
57 |
$distbaseid = 'debian'; |
b1a6a5
|
58 |
$ver = explode(' ', $issue); |
d281bb
|
59 |
$ver = array_filter($ver); |
C |
60 |
$ver = next($ver); |
b1a6a5
|
61 |
$mainver = explode('.', $ver); |
d281bb
|
62 |
$mainver = array_filter($mainver); |
C |
63 |
$mainver = current($mainver).'.'.next($mainver); |
|
64 |
switch ($mainver){ |
bc04c3
|
65 |
case "14.10": |
TB |
66 |
$relname = "(Utopic Unicorn)"; |
|
67 |
break; |
be2cbb
|
68 |
case "14.04": |
TB |
69 |
$relname = "(Trusty Tahr)"; |
|
70 |
break; |
|
71 |
case "13.10": |
|
72 |
$relname = "(Saucy Salamander)"; |
|
73 |
break; |
|
74 |
case "13.04": |
|
75 |
$relname = "(Raring Ringtail)"; |
|
76 |
break; |
d281bb
|
77 |
case "12.10": |
C |
78 |
$relname = "(Quantal Quetzal)"; |
b1a6a5
|
79 |
break; |
d281bb
|
80 |
case "12.04": |
C |
81 |
$relname = "(Precise Pangolin)"; |
b1a6a5
|
82 |
break; |
d281bb
|
83 |
case "11.10": |
C |
84 |
$relname = "(Oneiric Ocelot)"; |
b1a6a5
|
85 |
break; |
d281bb
|
86 |
case "11.14": |
C |
87 |
$relname = "(Natty Narwhal)"; |
b1a6a5
|
88 |
break; |
d281bb
|
89 |
case "10.10": |
C |
90 |
$relname = "(Maverick Meerkat)"; |
b1a6a5
|
91 |
break; |
d281bb
|
92 |
case "10.04": |
C |
93 |
$relname = "(Lucid Lynx)"; |
b1a6a5
|
94 |
break; |
d281bb
|
95 |
case "9.10": |
C |
96 |
$relname = "(Karmic Koala)"; |
b1a6a5
|
97 |
break; |
d281bb
|
98 |
case "9.04": |
C |
99 |
$relname = "(Jaunty Jackpole)"; |
b1a6a5
|
100 |
break; |
d281bb
|
101 |
case "8.10": |
b1a6a5
|
102 |
$relname = "(Intrepid Ibex)"; |
MC |
103 |
break; |
d281bb
|
104 |
case "8.04": |
C |
105 |
$relname = "(Hardy Heron)"; |
b1a6a5
|
106 |
break; |
d281bb
|
107 |
case "7.10": |
C |
108 |
$relname = "(Gutsy Gibbon)"; |
b1a6a5
|
109 |
break; |
d281bb
|
110 |
case "7.04": |
C |
111 |
$relname = "(Feisty Fawn)"; |
b1a6a5
|
112 |
break; |
d281bb
|
113 |
case "6.10": |
C |
114 |
$relname = "(Edgy Eft)"; |
b1a6a5
|
115 |
break; |
d281bb
|
116 |
case "6.06": |
C |
117 |
$relname = "(Dapper Drake)"; |
b1a6a5
|
118 |
break; |
d281bb
|
119 |
case "5.10": |
C |
120 |
$relname = "(Breezy Badger)"; |
b1a6a5
|
121 |
break; |
d281bb
|
122 |
case "5.04": |
C |
123 |
$relname = "(Hoary Hedgehog)"; |
b1a6a5
|
124 |
break; |
d281bb
|
125 |
case "4.10": |
C |
126 |
$relname = "(Warty Warthog)"; |
b1a6a5
|
127 |
break; |
d281bb
|
128 |
default: |
C |
129 |
$relname = "UNKNOWN"; |
|
130 |
} |
|
131 |
$distver = $ver.$lts." ".$relname; |
|
132 |
} elseif(trim(file_get_contents('/etc/debian_version')) == '4.0') { |
dead5c
|
133 |
$distname = 'Debian'; |
V |
134 |
$distver = '4.0'; |
|
135 |
$distid = 'debian40'; |
|
136 |
$distbaseid = 'debian'; |
|
137 |
} elseif (strstr(trim(file_get_contents('/etc/debian_version')), '5.0')) { |
|
138 |
$distname = 'Debian'; |
|
139 |
$distver = 'Lenny'; |
|
140 |
$distid = 'debian40'; |
|
141 |
$distbaseid = 'debian'; |
|
142 |
} elseif (strstr(trim(file_get_contents('/etc/debian_version')), '6.0') || trim(file_get_contents('/etc/debian_version')) == 'squeeze/sid') { |
|
143 |
$distname = 'Debian'; |
|
144 |
$distver = 'Squeeze/Sid'; |
|
145 |
$distid = 'debian60'; |
|
146 |
$distbaseid = 'debian'; |
82ff62
|
147 |
} elseif (strstr(trim(file_get_contents('/etc/debian_version')), '7.0') || substr(trim(file_get_contents('/etc/debian_version')),0,2) == '7.' || trim(file_get_contents('/etc/debian_version')) == 'wheezy/sid') { |
996bad
|
148 |
$distname = 'Debian'; |
M |
149 |
$distver = 'Wheezy/Sid'; |
d6aef1
|
150 |
$distid = 'debian60'; |
996bad
|
151 |
$distbaseid = 'debian'; |
dead5c
|
152 |
} else { |
V |
153 |
$distname = 'Debian'; |
|
154 |
$distver = 'Unknown'; |
|
155 |
$distid = 'debian40'; |
|
156 |
$distbaseid = 'debian'; |
|
157 |
} |
|
158 |
} |
|
159 |
|
|
160 |
//** OpenSuSE |
|
161 |
elseif (file_exists('/etc/SuSE-release')) { |
|
162 |
if (stristr(file_get_contents('/etc/SuSE-release'), '11.0')) { |
|
163 |
$distname = 'openSUSE'; |
|
164 |
$distver = '11.0'; |
|
165 |
$distid = 'opensuse110'; |
|
166 |
$distbaseid = 'opensuse'; |
|
167 |
} elseif (stristr(file_get_contents('/etc/SuSE-release'), '11.1')) { |
|
168 |
$distname = 'openSUSE'; |
|
169 |
$distver = '11.1'; |
|
170 |
$distid = 'opensuse110'; |
|
171 |
$distbaseid = 'opensuse'; |
|
172 |
} elseif (stristr(file_get_contents('/etc/SuSE-release'), '11.2')) { |
|
173 |
$distname = 'openSUSE'; |
|
174 |
$distver = '11.1'; |
|
175 |
$distid = 'opensuse110'; |
|
176 |
$distbaseid = 'opensuse'; |
|
177 |
} else { |
|
178 |
$distname = 'openSUSE'; |
|
179 |
$distver = 'Unknown'; |
|
180 |
$distid = 'opensuse110'; |
|
181 |
$distbaseid = 'opensuse'; |
|
182 |
} |
|
183 |
} |
|
184 |
|
|
185 |
|
|
186 |
//** Redhat |
|
187 |
elseif (file_exists('/etc/redhat-release')) { |
|
188 |
|
|
189 |
$content = file_get_contents('/etc/redhat-release'); |
|
190 |
|
|
191 |
if (stristr($content, 'Fedora release 9 (Sulphur)')) { |
|
192 |
$distname = 'Fedora'; |
|
193 |
$distver = '9'; |
|
194 |
$distid = 'fedora9'; |
|
195 |
$distbaseid = 'fedora'; |
|
196 |
} elseif (stristr($content, 'Fedora release 10 (Cambridge)')) { |
|
197 |
$distname = 'Fedora'; |
|
198 |
$distver = '10'; |
|
199 |
$distid = 'fedora9'; |
|
200 |
$distbaseid = 'fedora'; |
|
201 |
} elseif (stristr($content, 'Fedora release 10')) { |
|
202 |
$distname = 'Fedora'; |
|
203 |
$distver = '11'; |
|
204 |
$distid = 'fedora9'; |
|
205 |
$distbaseid = 'fedora'; |
|
206 |
} elseif (stristr($content, 'CentOS release 5.2 (Final)')) { |
|
207 |
$distname = 'CentOS'; |
|
208 |
$distver = '5.2'; |
|
209 |
$distid = 'centos52'; |
|
210 |
$distbaseid = 'fedora'; |
|
211 |
} elseif (stristr($content, 'CentOS release 5.3 (Final)')) { |
|
212 |
$distname = 'CentOS'; |
|
213 |
$distver = '5.3'; |
|
214 |
$distid = 'centos53'; |
|
215 |
$distbaseid = 'fedora'; |
be2cbb
|
216 |
} elseif(stristr($content, 'CentOS Linux release 6')) { |
TB |
217 |
$distname = 'CentOS'; |
|
218 |
$distver = 'Unknown'; |
|
219 |
$distid = 'centos53'; |
|
220 |
$distbaseid = 'fedora'; |
|
221 |
} elseif(stristr($content, 'CentOS Linux release 7')) { |
|
222 |
$distname = 'CentOS'; |
|
223 |
$distver = 'Unknown'; |
|
224 |
$distid = 'centos53'; |
|
225 |
$distbaseid = 'fedora'; |
dead5c
|
226 |
} else { |
V |
227 |
$distname = 'Redhat'; |
|
228 |
$distver = 'Unknown'; |
|
229 |
$distid = 'fedora9'; |
|
230 |
$distbaseid = 'fedora'; |
|
231 |
} |
|
232 |
} |
|
233 |
|
|
234 |
//** Gentoo |
|
235 |
elseif (file_exists('/etc/gentoo-release')) { |
|
236 |
|
|
237 |
$content = file_get_contents('/etc/gentoo-release'); |
|
238 |
|
|
239 |
preg_match_all('/([0-9]{1,2})/', $content, $version); |
|
240 |
$distname = 'Gentoo'; |
|
241 |
$distver = $version[0][0] . $version[0][1]; |
|
242 |
$distid = 'gentoo'; |
|
243 |
$distbaseid = 'gentoo'; |
|
244 |
} else { |
|
245 |
die('Unrecognized GNU/Linux distribution'); |
|
246 |
} |
|
247 |
|
|
248 |
return array('name' => $distname, 'version' => $distver, 'id' => $distid, 'baseid' => $distbaseid); |
|
249 |
} |
|
250 |
|
b1a6a5
|
251 |
// this function remains in the tools class, because it is used by cron AND rescue |
dead5c
|
252 |
public function monitorServices() { |
V |
253 |
global $app; |
|
254 |
global $conf; |
|
255 |
|
|
256 |
/** the id of the server as int */ |
b1a6a5
|
257 |
|
MC |
258 |
|
dead5c
|
259 |
$server_id = intval($conf['server_id']); |
V |
260 |
|
eed6b3
|
261 |
/** get the "active" Services of the server from the DB */ |
039b46
|
262 |
$services = $app->db->queryOneRecord('SELECT * FROM server WHERE server_id = ' . $server_id); |
eed6b3
|
263 |
/* |
V |
264 |
* If the DB is down, we have to set the db to "yes". |
|
265 |
* If we don't do this, then the monitor will NOT monitor, that the db is down and so the |
|
266 |
* rescue-module can not try to rescue the db |
|
267 |
*/ |
|
268 |
if ($services == null) { |
|
269 |
$services['db_server'] = 1; |
|
270 |
} |
dead5c
|
271 |
|
V |
272 |
/* The type of the Monitor-data */ |
|
273 |
$type = 'services'; |
|
274 |
|
|
275 |
/** the State of the monitoring */ |
|
276 |
/* ok, if ALL active services are running, |
|
277 |
* error, if not |
|
278 |
* There is no other state! |
|
279 |
*/ |
|
280 |
$state = 'ok'; |
|
281 |
|
|
282 |
/* Monitor Webserver */ |
|
283 |
$data['webserver'] = -1; // unknown - not needed |
|
284 |
if ($services['web_server'] == 1) { |
|
285 |
if ($this->_checkTcp('localhost', 80)) { |
|
286 |
$data['webserver'] = 1; |
|
287 |
} else { |
|
288 |
$data['webserver'] = 0; |
|
289 |
$state = 'error'; // because service is down |
|
290 |
} |
|
291 |
} |
|
292 |
|
|
293 |
/* Monitor FTP-Server */ |
|
294 |
$data['ftpserver'] = -1; // unknown - not needed |
|
295 |
if ($services['file_server'] == 1) { |
|
296 |
if ($this->_checkFtp('localhost', 21)) { |
|
297 |
$data['ftpserver'] = 1; |
|
298 |
} else { |
|
299 |
$data['ftpserver'] = 0; |
|
300 |
$state = 'error'; // because service is down |
|
301 |
} |
|
302 |
} |
|
303 |
|
|
304 |
/* Monitor SMTP-Server */ |
|
305 |
$data['smtpserver'] = -1; // unknown - not needed |
|
306 |
if ($services['mail_server'] == 1) { |
|
307 |
if ($this->_checkTcp('localhost', 25)) { |
|
308 |
$data['smtpserver'] = 1; |
|
309 |
} else { |
|
310 |
$data['smtpserver'] = 0; |
|
311 |
$state = 'error'; // because service is down |
|
312 |
} |
|
313 |
} |
|
314 |
|
|
315 |
/* Monitor POP3-Server */ |
|
316 |
$data['pop3server'] = -1; // unknown - not needed |
|
317 |
if ($services['mail_server'] == 1) { |
|
318 |
if ($this->_checkTcp('localhost', 110)) { |
|
319 |
$data['pop3server'] = 1; |
|
320 |
} else { |
|
321 |
$data['pop3server'] = 0; |
|
322 |
$state = 'error'; // because service is down |
|
323 |
} |
|
324 |
} |
|
325 |
|
|
326 |
/* Monitor IMAP-Server */ |
|
327 |
$data['imapserver'] = -1; // unknown - not needed |
|
328 |
if ($services['mail_server'] == 1) { |
|
329 |
if ($this->_checkTcp('localhost', 143)) { |
|
330 |
$data['imapserver'] = 1; |
|
331 |
} else { |
|
332 |
$data['imapserver'] = 0; |
|
333 |
$state = 'error'; // because service is down |
|
334 |
} |
|
335 |
} |
|
336 |
|
|
337 |
/* Monitor BIND-Server */ |
|
338 |
$data['bindserver'] = -1; // unknown - not needed |
|
339 |
if ($services['dns_server'] == 1) { |
dfcd55
|
340 |
if ($this->_checkUdp('localhost', 53)) { |
dead5c
|
341 |
$data['bindserver'] = 1; |
V |
342 |
} else { |
|
343 |
$data['bindserver'] = 0; |
|
344 |
$state = 'error'; // because service is down |
|
345 |
} |
|
346 |
} |
|
347 |
|
|
348 |
/* Monitor MySQL Server */ |
|
349 |
$data['mysqlserver'] = -1; // unknown - not needed |
|
350 |
if ($services['db_server'] == 1) { |
|
351 |
if ($this->_checkTcp('localhost', 3306)) { |
|
352 |
$data['mysqlserver'] = 1; |
|
353 |
} else { |
|
354 |
$data['mysqlserver'] = 0; |
|
355 |
$state = 'error'; // because service is down |
|
356 |
} |
|
357 |
} |
b1a6a5
|
358 |
$data['mongodbserver'] = -1; |
MC |
359 |
if ($this->_checkTcp('localhost', 27017)) { |
|
360 |
$data['mongodbserver'] = 1; |
|
361 |
} else { |
|
362 |
$data['mongodbserver'] = 0; |
|
363 |
//$state = 'error'; // because service is down |
|
364 |
/* TODO!!! check if this is a mongodbserver at all, otherwise it will always throw an error state!!! */ |
|
365 |
} |
def897
|
366 |
|
dead5c
|
367 |
/* |
V |
368 |
* Return the Result |
|
369 |
*/ |
|
370 |
$res['server_id'] = $server_id; |
|
371 |
$res['type'] = $type; |
|
372 |
$res['data'] = $data; |
|
373 |
$res['state'] = $state; |
|
374 |
return $res; |
|
375 |
} |
b1a6a5
|
376 |
|
dead5c
|
377 |
public function _getLogData($log) { |
V |
378 |
global $conf; |
|
379 |
|
|
380 |
$dist = ''; |
|
381 |
$logfile = ''; |
|
382 |
|
|
383 |
if (@is_file('/etc/debian_version')) { |
|
384 |
$dist = 'debian'; |
|
385 |
} elseif (@is_file('/etc/redhat-release')) { |
|
386 |
$dist = 'redhat'; |
|
387 |
} elseif (@is_file('/etc/SuSE-release')) { |
|
388 |
$dist = 'suse'; |
|
389 |
} elseif (@is_file('/etc/gentoo-release')) { |
|
390 |
$dist = 'gentoo'; |
|
391 |
} |
|
392 |
|
|
393 |
switch ($log) { |
b1a6a5
|
394 |
case 'log_mail': |
MC |
395 |
if ($dist == 'debian') { |
|
396 |
$logfile = '/var/log/mail.log'; |
|
397 |
} elseif ($dist == 'redhat') { |
|
398 |
$logfile = '/var/log/maillog'; |
|
399 |
} elseif ($dist == 'suse') { |
|
400 |
$logfile = '/var/log/mail.info'; |
|
401 |
} elseif ($dist == 'gentoo') { |
|
402 |
$logfile = '/var/log/maillog'; |
|
403 |
} |
|
404 |
break; |
|
405 |
case 'log_mail_warn': |
|
406 |
if ($dist == 'debian') { |
|
407 |
$logfile = '/var/log/mail.warn'; |
|
408 |
} elseif ($dist == 'redhat') { |
|
409 |
$logfile = '/var/log/maillog'; |
|
410 |
} elseif ($dist == 'suse') { |
|
411 |
$logfile = '/var/log/mail.warn'; |
|
412 |
} elseif ($dist == 'gentoo') { |
|
413 |
$logfile = '/var/log/maillog'; |
|
414 |
} |
|
415 |
break; |
|
416 |
case 'log_mail_err': |
|
417 |
if ($dist == 'debian') { |
|
418 |
$logfile = '/var/log/mail.err'; |
|
419 |
} elseif ($dist == 'redhat') { |
|
420 |
$logfile = '/var/log/maillog'; |
|
421 |
} elseif ($dist == 'suse') { |
|
422 |
$logfile = '/var/log/mail.err'; |
|
423 |
} elseif ($dist == 'gentoo') { |
|
424 |
$logfile = '/var/log/maillog'; |
|
425 |
} |
|
426 |
break; |
|
427 |
case 'log_messages': |
|
428 |
if ($dist == 'debian') { |
|
429 |
$logfile = '/var/log/syslog'; |
|
430 |
} elseif ($dist == 'redhat') { |
|
431 |
$logfile = '/var/log/messages'; |
|
432 |
} elseif ($dist == 'suse') { |
|
433 |
$logfile = '/var/log/messages'; |
|
434 |
} elseif ($dist == 'gentoo') { |
|
435 |
$logfile = '/var/log/messages'; |
|
436 |
} |
|
437 |
break; |
|
438 |
case 'log_ispc_cron': |
|
439 |
if ($dist == 'debian') { |
|
440 |
$logfile = $conf['ispconfig_log_dir'] . '/cron.log'; |
|
441 |
} elseif ($dist == 'redhat') { |
|
442 |
$logfile = $conf['ispconfig_log_dir'] . '/cron.log'; |
|
443 |
} elseif ($dist == 'suse') { |
|
444 |
$logfile = $conf['ispconfig_log_dir'] . '/cron.log'; |
|
445 |
} elseif ($dist == 'gentoo') { |
|
446 |
$logfile = '/var/log/cron'; |
|
447 |
} |
|
448 |
break; |
|
449 |
case 'log_freshclam': |
|
450 |
if ($dist == 'debian') { |
|
451 |
$logfile = '/var/log/clamav/freshclam.log'; |
|
452 |
} elseif ($dist == 'redhat') { |
|
453 |
$logfile = (is_file('/var/log/clamav/freshclam.log') ? '/var/log/clamav/freshclam.log' : '/var/log/freshclam.log'); |
|
454 |
} elseif ($dist == 'suse') { |
|
455 |
$logfile = '/var/log/freshclam.log'; |
|
456 |
} elseif ($dist == 'gentoo') { |
|
457 |
$logfile = '/var/log/clamav/freshclam.log'; |
|
458 |
} |
|
459 |
break; |
|
460 |
case 'log_clamav': |
|
461 |
if ($dist == 'debian') { |
|
462 |
$logfile = '/var/log/clamav/clamav.log'; |
|
463 |
} elseif ($dist == 'redhat') { |
|
464 |
$logfile = (is_file('/var/log/clamav/clamd.log') ? '/var/log/clamav/clamd.log' : '/var/log/maillog'); |
|
465 |
} elseif ($dist == 'suse') { |
|
466 |
$logfile = '/var/log/clamd.log'; |
|
467 |
} elseif ($dist == 'gentoo') { |
|
468 |
$logfile = '/var/log/clamav/clamd.log'; |
|
469 |
} |
|
470 |
break; |
|
471 |
case 'log_fail2ban': |
|
472 |
if ($dist == 'debian') { |
|
473 |
$logfile = '/var/log/fail2ban.log'; |
|
474 |
} elseif ($dist == 'redhat') { |
|
475 |
$logfile = '/var/log/fail2ban.log'; |
|
476 |
} elseif ($dist == 'suse') { |
|
477 |
$logfile = '/var/log/fail2ban.log'; |
|
478 |
} elseif ($dist == 'gentoo') { |
|
479 |
$logfile = '/var/log/fail2ban.log'; |
|
480 |
} |
|
481 |
break; |
|
482 |
case 'log_mongodb': |
|
483 |
$logfile = '/var/log/mongodb/mongodb.log'; |
|
484 |
break; |
|
485 |
case 'log_ispconfig': |
|
486 |
if ($dist == 'debian') { |
|
487 |
$logfile = $conf['ispconfig_log_dir'] . '/ispconfig.log'; |
|
488 |
} elseif ($dist == 'redhat') { |
|
489 |
$logfile = $conf['ispconfig_log_dir'] . '/ispconfig.log'; |
|
490 |
} elseif ($dist == 'suse') { |
|
491 |
$logfile = $conf['ispconfig_log_dir'] . '/ispconfig.log'; |
|
492 |
} elseif ($dist == 'gentoo') { |
|
493 |
$logfile = $conf['ispconfig_log_dir'] . '/ispconfig.log'; |
|
494 |
} |
|
495 |
break; |
|
496 |
default: |
|
497 |
$logfile = ''; |
|
498 |
break; |
dead5c
|
499 |
} |
V |
500 |
|
|
501 |
// Getting the logfile content |
|
502 |
if ($logfile != '') { |
|
503 |
$logfile = escapeshellcmd($logfile); |
|
504 |
if (stristr($logfile, ';') or substr($logfile, 0, 9) != '/var/log/' or stristr($logfile, '..')) { |
|
505 |
$log = 'Logfile path error.'; |
|
506 |
} else { |
|
507 |
$log = ''; |
|
508 |
if (is_readable($logfile)) { |
|
509 |
$fd = popen('tail -n 100 ' . $logfile, 'r'); |
|
510 |
if ($fd) { |
|
511 |
while (!feof($fd)) { |
|
512 |
$log .= fgets($fd, 4096); |
|
513 |
$n++; |
|
514 |
if ($n > 1000) |
|
515 |
break; |
|
516 |
} |
|
517 |
fclose($fd); |
|
518 |
} |
|
519 |
} else { |
|
520 |
$log = 'Unable to read ' . $logfile; |
|
521 |
} |
|
522 |
} |
|
523 |
} |
|
524 |
|
|
525 |
return $log; |
|
526 |
} |
|
527 |
|
|
528 |
private function _checkTcp($host, $port) { |
e5c5af
|
529 |
/* Try to open a connection */ |
dead5c
|
530 |
$fp = @fsockopen($host, $port, $errno, $errstr, 2); |
V |
531 |
|
|
532 |
if ($fp) { |
8bbcc1
|
533 |
/* |
e5c5af
|
534 |
* We got a connection, this means, everything is O.K. |
V |
535 |
* But maybe we are able to do more deep testing? |
8bbcc1
|
536 |
*/ |
e5c5af
|
537 |
if ($port == 80) { |
V |
538 |
/* |
|
539 |
* Port 80 means, testing APACHE |
|
540 |
* So we can do a deepter test and try to get data over this connection. |
|
541 |
* (if apache hangs, we get a connection but a timeout by trying to GET the data!) |
|
542 |
*/ |
8cf78b
|
543 |
// fwrite($fp, "GET / HTTP/1.0\r\n\r\n"); |
T |
544 |
$out = "GET / HTTP/1.1\r\n"; |
|
545 |
$out .= "Host: localhost\r\n"; |
|
546 |
$out .= "User-Agent: Mozilla/5.0 (ISPConfig monitor)\r\n"; |
|
547 |
$out .= "Accept: application/xml,application/xhtml+xml,text/html\r\n"; |
|
548 |
$out .= "Connection: Close\r\n\r\n"; |
|
549 |
fwrite($fp, $out); |
e5c5af
|
550 |
stream_set_timeout($fp, 5); // Timeout after 5 seconds |
V |
551 |
$res = fread($fp, 10); // try to get 10 bytes (enough to test!) |
|
552 |
$info = stream_get_meta_data($fp); |
|
553 |
if ($info['timed_out']) { |
|
554 |
return false; // Apache was not able to send data over this connection |
|
555 |
} |
eed6b3
|
556 |
} |
e5c5af
|
557 |
|
V |
558 |
/* The connection is no longer needed */ |
|
559 |
fclose($fp); |
|
560 |
/* We are able to establish a connection */ |
|
561 |
return true; |
dead5c
|
562 |
} else { |
e5c5af
|
563 |
/* We are NOT able to establish a connection */ |
V |
564 |
return false; |
dead5c
|
565 |
} |
V |
566 |
} |
|
567 |
|
|
568 |
private function _checkUdp($host, $port) { |
|
569 |
|
|
570 |
$fp = @fsockopen('udp://' . $host, $port, $errno, $errstr, 2); |
|
571 |
|
|
572 |
if ($fp) { |
|
573 |
fclose($fp); |
|
574 |
return true; |
|
575 |
} else { |
|
576 |
return false; |
|
577 |
} |
|
578 |
} |
|
579 |
|
|
580 |
private function _checkFtp($host, $port) { |
|
581 |
|
|
582 |
$conn_id = @ftp_connect($host, $port); |
|
583 |
|
|
584 |
if ($conn_id) { |
|
585 |
@ftp_close($conn_id); |
|
586 |
return true; |
|
587 |
} else { |
|
588 |
return false; |
|
589 |
} |
|
590 |
} |
eed6b3
|
591 |
|
e5c5af
|
592 |
/** |
V |
593 |
* Set the state to the given level (or higher, but not lesser). |
|
594 |
* * If the actual state is critical and you call the method with ok, |
|
595 |
* then the state is critical. |
|
596 |
* |
|
597 |
* * If the actual state is critical and you call the method with error, |
|
598 |
* then the state is error. |
|
599 |
*/ |
5de2af
|
600 |
public function _setState($oldState, $newState) { |
e5c5af
|
601 |
/* |
V |
602 |
* Calculate the weight of the old state |
|
603 |
*/ |
|
604 |
switch ($oldState) { |
b1a6a5
|
605 |
case 'no_state': $oldInt = 0; |
MC |
606 |
break; |
|
607 |
case 'ok': $oldInt = 1; |
|
608 |
break; |
|
609 |
case 'unknown': $oldInt = 2; |
|
610 |
break; |
|
611 |
case 'info': $oldInt = 3; |
|
612 |
break; |
|
613 |
case 'warning': $oldInt = 4; |
|
614 |
break; |
|
615 |
case 'critical': $oldInt = 5; |
|
616 |
break; |
|
617 |
case 'error': $oldInt = 6; |
|
618 |
break; |
e5c5af
|
619 |
} |
V |
620 |
/* |
|
621 |
* Calculate the weight of the new state |
|
622 |
*/ |
|
623 |
switch ($newState) { |
b1a6a5
|
624 |
case 'no_state': $newInt = 0; |
MC |
625 |
break; |
|
626 |
case 'ok': $newInt = 1; |
|
627 |
break; |
|
628 |
case 'unknown': $newInt = 2; |
|
629 |
break; |
|
630 |
case 'info': $newInt = 3; |
|
631 |
break; |
|
632 |
case 'warning': $newInt = 4; |
|
633 |
break; |
|
634 |
case 'critical': $newInt = 5; |
|
635 |
break; |
|
636 |
case 'error': $newInt = 6; |
|
637 |
break; |
e5c5af
|
638 |
} |
V |
639 |
|
|
640 |
/* |
|
641 |
* Set to the higher level |
|
642 |
*/ |
|
643 |
if ($newInt > $oldInt) { |
|
644 |
return $newState; |
|
645 |
} else { |
|
646 |
return $oldState; |
|
647 |
} |
|
648 |
} |
dead5c
|
649 |
|
5de2af
|
650 |
/** |
M |
651 |
* Deletes Records older than 4 minutes. |
|
652 |
* The monitor writes new data every 5 minutes or longer (4 hour, 1 day). |
|
653 |
* So if i delete all Date older than 4 minutes i can be sure, that all old data |
|
654 |
* are deleted... |
|
655 |
*/ |
|
656 |
public function delOldRecords($type, $serverId) { |
|
657 |
global $app; |
|
658 |
|
|
659 |
// $now = time(); |
|
660 |
// $old = $now - (4 * 60); // 4 minutes |
|
661 |
$old = 'UNIX_TIMESTAMP() - 240'; |
|
662 |
|
|
663 |
/* |
|
664 |
* ATTENTION if i do NOT pay attention of the server id, i delete all data (of the type) |
|
665 |
* of ALL servers. This means, if i have a multiserver-environment and a server has a |
|
666 |
* time not synced with the others (for example, all server has 11:00 and ONE server has |
|
667 |
* 10:45) then the actual data of this server (with the time-stamp 10:45) get lost |
|
668 |
* even though it is the NEWEST data of this server. To avoid this i HAVE to include |
|
669 |
* the server-id! |
|
670 |
*/ |
|
671 |
$sql = 'DELETE FROM monitor_data ' . |
b1a6a5
|
672 |
'WHERE ' . |
MC |
673 |
' type =' . "'" . $app->dbmaster->quote($type) . "' " . |
|
674 |
'AND ' . |
|
675 |
' created < ' . $old . ' ' . |
|
676 |
'AND ' . |
|
677 |
' server_id = ' . $serverId; |
5de2af
|
678 |
$app->dbmaster->query($sql); |
dead5c
|
679 |
} |
V |
680 |
|
a20430
|
681 |
public function send_notification_email($template, $placeholders, $recipients) { |
FS |
682 |
global $conf; |
|
683 |
|
|
684 |
if(!is_array($recipients) || count($recipients) < 1) return false; |
|
685 |
if(!is_array($placeholders)) $placeholders = array(); |
|
686 |
|
|
687 |
if(file_exists($conf['rootpath'].'/conf-custom/mail/' . $template . '_'.$conf['language'].'.txt')) { |
|
688 |
$lines = file($conf['rootpath'].'/conf-custom/mail/' . $template . '_'.$conf['language'].'.txt'); |
|
689 |
} elseif(file_exists($conf['rootpath'].'/conf-custom/mail/' . $template . '_en.txt')) { |
|
690 |
$lines = file($conf['rootpath'].'/conf-custom/mail/' . $template . '_en.txt'); |
|
691 |
} elseif(file_exists($conf['rootpath'].'/conf/mail/' . $template . '_'.$conf['language'].'.txt')) { |
|
692 |
$lines = file($conf['rootpath'].'/conf/mail/' . $template . '_'.$conf['language'].'.txt'); |
|
693 |
} else { |
|
694 |
$lines = file($conf['rootpath'].'/conf/mail/' . $template . '_en.txt'); |
|
695 |
} |
|
696 |
|
|
697 |
//* get mail headers, subject and body |
|
698 |
$mailHeaders = ''; |
|
699 |
$mailBody = ''; |
|
700 |
$mailSubject = ''; |
|
701 |
$inHeader = true; |
|
702 |
for($l = 0; $l < count($lines); $l++) { |
|
703 |
if($lines[$l] == '') { |
|
704 |
$inHeader = false; |
|
705 |
continue; |
|
706 |
} |
|
707 |
if($inHeader == true) { |
|
708 |
$parts = explode(':', $lines[$l], 2); |
|
709 |
if(strtolower($parts[0]) == 'subject') $mailSubject = trim($parts[1]); |
|
710 |
unset($parts); |
|
711 |
$mailHeaders .= trim($lines[$l]) . "\n"; |
|
712 |
} else { |
|
713 |
$mailBody .= trim($lines[$l]) . "\n"; |
|
714 |
} |
|
715 |
} |
|
716 |
$mailBody = trim($mailBody); |
|
717 |
|
|
718 |
//* Replace placeholders |
|
719 |
$mailHeaders = strtr($mailHeaders, $placeholders); |
|
720 |
$mailSubject = strtr($mailSubject, $placeholders); |
|
721 |
$mailBody = strtr($mailBody, $placeholders); |
|
722 |
|
|
723 |
for($r = 0; $r < count($recipients); $r++) { |
|
724 |
mail($recipients[$r], $mailSubject, $mailBody, $mailHeaders); |
|
725 |
} |
|
726 |
|
|
727 |
unset($mailSubject); |
|
728 |
unset($mailHeaders); |
|
729 |
unset($mailBody); |
|
730 |
unset($lines); |
|
731 |
|
|
732 |
return true; |
|
733 |
} |
5de2af
|
734 |
|
dead5c
|
735 |
} |
V |
736 |
|
2332b2
|
737 |
?> |