monitor_tools.inc.php 27 KB
Newer Older
1
2
3
<?php

/*
4
5
	Copyright (c) 2007-2011, Till Brehm, projektfarm Gmbh and Oliver Vogel www.muv.com
	All rights reserved.
6

7
8
	Redistribution and use in source and binary forms, with or without modification,
	are permitted provided that the following conditions are met:
9
10

 * Redistributions of source code must retain the above copyright notice,
11
	this list of conditions and the following disclaimer.
12
 * Redistributions in binary form must reproduce the above copyright notice,
13
14
	this list of conditions and the following disclaimer in the documentation
	and/or other materials provided with the distribution.
15
 * Neither the name of ISPConfig nor the names of its contributors
16
17
18
19
20
21
22
23
24
25
26
27
28
	may be used to endorse or promote products derived from this software without
	specific prior written permission.

	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
	ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
	WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
	IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
	INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
	BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
	DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
	OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
	NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
	EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30
31
32
33
34
35
36
37
 */

class monitor_tools {

	//** Get distribution identifier
	//** IMPORTANT!
	//   This is the same code as in install/lib/install.lib.php
	//   So if you change it here, you also have to change it in there!
	//   Please do not forget to remove the swriteln(); - lines here at this file
38
	function get_distname() {
39

40
41
42
43
	$distname = '';
	$distver = '';
	$distid = '';
	$distbaseid = '';
44

45
46
	//** Debian or Ubuntu
	if(file_exists('/etc/debian_version')) {
Thom's avatar
Thom committed
47

48
49
		// Check if this is Ubuntu and not Debian
		if (strstr(trim(file_get_contents('/etc/issue')), 'Ubuntu') || (is_file('/etc/os-release') && stristr(file_get_contents('/etc/os-release'), 'Ubuntu'))) {
Thom's avatar
Thom committed
50

51
			$issue = file_get_contents('/etc/issue');
Thom's avatar
Thom committed
52

53
54
55
			// Use content of /etc/issue file
			if(strstr($issue,'Ubuntu')) {
				if (strstr(trim($issue), 'LTS')) {
tbrehm's avatar
tbrehm committed
56
57
58
59
60
					$lts=" LTS";
				} else {
					$lts="";
				}

61
62
63
				$distname = 'Ubuntu';
				$distid = 'debian40';
				$distbaseid = 'debian';
64
				$ver = explode(' ', $issue);
65
66
				$ver = array_filter($ver);
				$ver = next($ver);
67
				$mainver = explode('.', $ver);
68
69
				$mainver = array_filter($mainver);
				$mainver = current($mainver).'.'.next($mainver);
70
			// Use content of /etc/os-release file
71
			} else {
72
73
74
75
76
77
				$os_release = file_get_contents('/etc/os-release');
				if (strstr(trim($os_release), 'LTS')) {
					$lts = " LTS";
				} else {
					$lts = "";
				}
Thom's avatar
Thom committed
78

79
				$distname = 'Ubuntu';
80
81
				$distid = 'debian40';
				$distbaseid = 'debian';
82
83
84
85
86
87
88
89

				preg_match("/.*VERSION=\"(.*)\".*/ui", $os_release, $ver);
				$ver = str_replace("LTS", "", $ver[1]);
				$ver = explode(" ", $ver, 2);
				$ver = reset($ver);
				$mainver = $ver;
			}
			switch ($mainver){
90
91
92
93
			case "20.04":
				$relname = "(Focal Fossa)";
				$distconfid = 'ubuntu2004';
				break;
94
95
96
97
			case "18.04":
				$relname = "(Bionic Beaver)";
				$distconfid = 'ubuntu1804';
				break;
98
99
100
101
			case "17.10":
				$relname = "(Artful Aardvark)";
				$distconfid = 'ubuntu1710';
				break;
102
103
104
105
			case "17.04":
				$relname = "(Zesty Zapus)";
				$distconfid = 'ubuntu1604';
				break;
106
107
108
109
			case "16.10":
				$relname = "(Yakkety Yak)";
				$distconfid = 'ubuntu1604';
				break;
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
			case "16.04":
				$relname = "(Xenial Xerus)";
				$distconfid = 'ubuntu1604';
				break;
			case "15.10":
				$relname = "(Wily Werewolf)";
				break;
			case "15.04":
				$relname = "(Vivid Vervet)";
				break;
			case "14.10":
				$relname = "(Utopic Unicorn)";
				break;
			case "14.04":
				$relname = "(Trusty Tahr)";
				break;
			case "13.10":
				$relname = "(Saucy Salamander)";
				break;
			case "13.04":
				$relname = "(Raring Ringtail)";
				break;
			case "12.10":
				$relname = "(Quantal Quetzal)";
				break;
			case "12.04":
				$relname = "(Precise Pangolin)";
				break;
			case "11.10":
				$relname = "(Oneiric Ocelot)";
				break;
			case "11.14":
				$relname = "(Natty Narwhal)";
				break;
			case "10.10":
				$relname = "(Maverick Meerkat)";
				break;
			case "10.04":
				$relname = "(Lucid Lynx)";
				break;
			case "9.10":
				$relname = "(Karmic Koala)";
				break;
			case "9.04":
				$relname = "(Jaunty Jackpole)";
				break;
			case "8.10":
				$relname = "(Intrepid Ibex)";
				break;
			case "8.04":
				$relname = "(Hardy Heron)";
				break;
			case "7.10":
				$relname = "(Gutsy Gibbon)";
				break;
			case "7.04":
				$relname = "(Feisty Fawn)";
				break;
			case "6.10":
				$relname = "(Edgy Eft)";
				break;
			case "6.06":
				$relname = "(Dapper Drake)";
				break;
			case "5.10":
				$relname = "(Breezy Badger)";
				break;
			case "5.04":
				$relname = "(Hoary Hedgehog)";
				break;
			case "4.10":
				$relname = "(Warty Warthog)";
				break;
			default:
				$relname = "UNKNOWN";
185
			}
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
			$distver = $ver.$lts." ".$relname;
		} elseif(trim(file_get_contents('/etc/debian_version')) == '4.0') {
			$distname = 'Debian';
			$distver = '4.0';
			$distid = 'debian40';
			$distbaseid = 'debian';
		} elseif(strstr(trim(file_get_contents('/etc/debian_version')), '5.0')) {
			$distname = 'Debian';
			$distver = 'Lenny';
			$distid = 'debian40';
			$distbaseid = 'debian';
		} elseif(strstr(trim(file_get_contents('/etc/debian_version')), '6.0') || trim(file_get_contents('/etc/debian_version')) == 'squeeze/sid') {
			$distname = 'Debian';
			$distver = 'Squeeze/Sid';
			$distid = 'debian60';
			$distbaseid = 'debian';
		} 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') {
			$distname = 'Debian';
			$distver = 'Wheezy/Sid';
			$distid = 'debian60';
			$distbaseid = 'debian';
207
		} elseif(substr(trim(file_get_contents('/etc/debian_version')),0,1) == '8') {
208
209
210
211
			$distname = 'Debian';
			$distver = 'Jessie';
			$distid = 'debian60';
			$distbaseid = 'debian';
212
		} elseif(substr(trim(file_get_contents('/etc/debian_version')),0,1) == '9') {
Till Brehm's avatar
Till Brehm committed
213
214
215
216
217
			$distname = 'Debian';
			$distver = 'Stretch';
			$distconfid = 'debian90';
			$distid = 'debian60';
			$distbaseid = 'debian';
218
219
220
221
222
223
		} elseif(substr(trim(file_get_contents('/etc/debian_version')),0,2) == '10') {
			$distname = 'Debian';
			$distver = 'Buster';
			$distconfid = 'debian100';
			$distid = 'debian60';
			$distbaseid = 'debian';
224
225
226
227
228
229
		} elseif(substr(trim(file_get_contents('/etc/debian_version')),0,2) == '11') {
			$distname = 'Debian';
			$distver = 'Bullseye';
			$distconfid = 'debian110';
			$distid = 'debian60';
			$distbaseid = 'debian';
230
231
232
233
234
235
		} elseif(strstr(trim(file_get_contents('/etc/debian_version')), '/sid')) {
			$distname = 'Debian';
			$distver = 'Testing';
			$distid = 'debian60';
			$distconfid = 'debiantesting';
			$distbaseid = 'debian';
236
237
238
		} else {
			$distname = 'Debian';
			$distver = 'Unknown';
239
240
			$distid = 'debian60';
			$distconfid = 'debian100';
241
			$distbaseid = 'debian';
242
		}
243
	}
244

245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
	//** OpenSuSE
	elseif(file_exists('/etc/SuSE-release')) {
		if(stristr(file_get_contents('/etc/SuSE-release'), '11.0')) {
			$distname = 'openSUSE';
			$distver = '11.0';
			$distid = 'opensuse110';
			$distbaseid = 'opensuse';
		} elseif(stristr(file_get_contents('/etc/SuSE-release'), '11.1')) {
			$distname = 'openSUSE';
			$distver = '11.1';
			$distid = 'opensuse110';
			$distbaseid = 'opensuse';
		} elseif(stristr(file_get_contents('/etc/SuSE-release'), '11.2')) {
			$distname = 'openSUSE';
			$distver = '11.2';
			$distid = 'opensuse112';
			$distbaseid = 'opensuse';
262
263
		} elseif(stristr(file_get_contents('/etc/os-release'), 'opensuse')) {
			$content = file_get_contents('/etc/os-release');
264
265
			preg_match_all('/NAME=\"([\w ]+)\"/m', $content, $name);
			preg_match_all('/VERSION_ID=\"([0-9]{1,2})\.?([0-9]{0,2})\.?([0-9]*).$/m', $content, $version);
266
			$distname = is_array($name) ? $name[1][0] : 'openSUSE';
267
			$distver = is_array($version) ? implode('.', array_filter(array($version[1][0],$version[2][0],$version[3][0]),'strlen')) : 'Unknown';
268
269
			$distid = 'opensuse112';
			$distbaseid = 'opensuse';
270
271
272
273
274
		}  else {
			$distname = 'openSUSE';
			$distver = 'Unknown';
			$distid = 'opensuse112';
			$distbaseid = 'opensuse';
275
		}
276
	}
277
278


279
280
	//** RHEL (including compatible clones) & Fedora
	elseif(file_exists('/etc/redhat-release') && file_exists('/etc/os-release')) {
Michael Seevogel's avatar
Michael Seevogel committed
281

282
		$content = file_get_contents('/etc/os-release');
283

284
		preg_match('/(?<=PRETTY_NAME=\").+?(?=\")/', $content, $prettyname);
285
		preg_match('/(?<=NAME=\").+?(?=\")/', $content, $name);
Michael Seevogel's avatar
Michael Seevogel committed
286
		preg_match('/(?<=VERSION=\").+?(?=\")/', $content, $version);
287
		preg_match('/(?<=VERSION_ID=\").+?(?=\")/', $content, $versionid);
288

289
		if(stristr($prettyname[0], 'Fedora 32 (Thirty Two)')) {
Jozef Sroka's avatar
Jozef Sroka committed
290
291
292
293
			$distname = 'Fedora';
			$distver = '32';
			$distid = 'fedora32';
			$distbaseid = 'fedora';
294
		} elseif(stristr($prettyname[0], 'Fedora 33 (Thirty Three)')) {
Jozef Sroka's avatar
Jozef Sroka committed
295
296
297
298
			$distname = 'Fedora';
			$distver = '33';
			$distid = 'fedora33';
			$distbaseid = 'fedora';
299
300
                //** RHEL 7 and compatible clones 
		} elseif(preg_match('/^(?:7|7\.[0-9]{1,2})$/', $versionid[0])) {
301
			preg_match_all('/([0-9]{1,2})\.?([0-9]{0,2})\.?([0-9]*)/', file_get_contents('/etc/redhat-release'), $centos7_version);
Michael Seevogel's avatar
Michael Seevogel committed
302
303
304
305
			$distname = $name[0];
			$distver = is_array($centos7_version)? implode('.', array_filter(array($centos7_version[1][0],$centos7_version[2][0],$centos7_version[3][0]),'strlen')) : $version[0];
			$distid = 'centos72';
			$distbaseid = 'fedora';
306
307
		//** RHEL 8 and compatible clones
		} elseif(preg_match('/^(?:8|8\.[0-9]{1,2})$/', $versionid[0])) {
Michael Seevogel's avatar
Michael Seevogel committed
308
309
310
			$distname = $name[0];
			$distver = $version[0];
			$distid = 'centos80';
311
312
313
314
315
316
			$distbaseid = 'fedora';
		} else {
			$distname = 'Redhat';
			$distver = 'Unknown';
			$distid = 'fedora9';
			$distbaseid = 'fedora';
317
		}
318
319
        //** CentOS 6
        } elseif(file_exists('/etc/redhat-release') && !file_exists('/etc/os-release') && !file_exists('/etc/els-release')) {
320
321
322
323

                $content = file_get_contents('/etc/redhat-release');

                if(stristr($content, 'CentOS Linux release 6') || stristr($content, 'CentOS release 6')) {
324
325
                        preg_match_all('/(6\.?([0-9]{0,2})\.?(\s)?([a-zA-Z()]+))$/', $content, $centos6_version);
                        $distname = 'CentOS Linux';
Michael Seevogel's avatar
Michael Seevogel committed
326
                        $distver = $centos6_version[0][0] ? $centos6_version[0][0] : '6';
327
328
                        $distid = 'centos53';
                        $distbaseid = 'fedora';
329
                } else {
330
331
332
333
334
                        $distname = 'Redhat';
                        $distver = 'Unknown';
                        $distid = 'fedora9';
                        $distbaseid = 'fedora';
                }
335
336
        //** CentOS 6 Extended Lifecycle Support by CloudLinux
        } elseif(file_exists('/etc/redhat-release') && file_exists('/etc/els-release') && !file_exists('/etc/os-release')) {
337

338
339
340
341
342
                $content = file_get_contents('/etc/els-release');

                if(stristr($content, 'CentOS Linux release 6') || stristr($content, 'CentOS release 6')) {
                        preg_match_all('/(6)\.?([0-9]{0,2})?\.?\s([a-zA-Z(), ]+)?$/', $content, $centos6_version);
                        $distname = 'CentOS Linux';
Michael Seevogel's avatar
Michael Seevogel committed
343
                        $distver = $centos6_version[0][0] ? $centos6_version[0][0] : '6';
344
345
346
347
348
349
350
351
352
                        $distid = 'centos53';
                        $distbaseid = 'fedora';
                } else {
                        $distname = 'Redhat';
                        $distver = 'Unknown';
                        $distid = 'fedora9';
                        $distbaseid = 'fedora';
                }
        }
353

354

355
356
	//** Gentoo
	elseif(file_exists('/etc/gentoo-release')) {
357

358
		$content = file_get_contents('/etc/gentoo-release');
359

360
361
362
363
364
365
366
367
368
		preg_match_all('/([0-9]{1,2})/', $content, $version);
		$distname = 'Gentoo';
		$distver = $version[0][0].$version[0][1];
		$distid = 'gentoo';
		$distbaseid = 'gentoo';

	} else {
		die('Unrecognized GNU/Linux distribution');
	}
Thom's avatar
Thom committed
369

370
371
	// Set $distconfid to distid, if no different id for the config is defined
	if(!isset($distconfid)) $distconfid = $distid;
372

373
	return array('name' => $distname, 'version' => $distver, 'id' => $distid, 'confid' => $distconfid, 'baseid' => $distbaseid);
374
375
	}

376
	// this function remains in the tools class, because it is used by cron AND rescue
377
	public function monitorServices() {
vogelor's avatar
vogelor committed
378
		global $app;
379
380
		global $conf;

381
		/** the id of the server as int */
382
383


384
385
		$server_id = intval($conf['server_id']);

386
		/**  get the "active" Services of the server from the DB */
387
		$services = $app->db->queryOneRecord('SELECT * FROM server WHERE server_id = ?', $server_id);
388
		/*
389
390
391
		 * If the DB is down, we have to set the db to "yes".
		 * If we don't do this, then the monitor will NOT monitor, that the db is down and so the
		 * rescue-module can not try to rescue the db
392
		 */
393
394
		if ($services == null) {
			$services['db_server'] = 1;
395
396
		}

397
398
		/* The type of the Monitor-data */
		$type = 'services';
399

400
401
402
403
		/** the State of the monitoring */
		/* ok, if ALL active services are running,
		 * error, if not
		 * There is no other state!
404
405
406
		 */
		$state = 'ok';

407
408
409
410
411
		/* Monitor Webserver */
		$data['webserver'] = -1; // unknown - not needed
		if ($services['web_server'] == 1) {
			if ($this->_checkTcp('localhost', 80)) {
				$data['webserver'] = 1;
412
			} else {
413
414
				$data['webserver'] = 0;
				$state = 'error'; // because service is down
415
416
417
			}
		}

418
419
420
421
422
423
424
425
		/* Monitor FTP-Server */
		$data['ftpserver'] = -1; // unknown - not needed
		if ($services['file_server'] == 1) {
			if ($this->_checkFtp('localhost', 21)) {
				$data['ftpserver'] = 1;
			} else {
				$data['ftpserver'] = 0;
				$state = 'error'; // because service is down
426
427
428
			}
		}

429
430
431
432
433
434
435
436
437
438
		/* Monitor SMTP-Server */
		$data['smtpserver'] = -1; // unknown - not needed
		if ($services['mail_server'] == 1) {
			if ($this->_checkTcp('localhost', 25)) {
				$data['smtpserver'] = 1;
			} else {
				$data['smtpserver'] = 0;
				$state = 'error'; // because service is down
			}
		}
439

440
441
442
443
444
445
446
447
448
449
		/* Monitor POP3-Server */
		$data['pop3server'] = -1; // unknown - not needed
		if ($services['mail_server'] == 1) {
			if ($this->_checkTcp('localhost', 110)) {
				$data['pop3server'] = 1;
			} else {
				$data['pop3server'] = 0;
				$state = 'error'; // because service is down
			}
		}
450

451
452
453
454
455
456
457
458
459
460
		/* Monitor IMAP-Server */
		$data['imapserver'] = -1; // unknown - not needed
		if ($services['mail_server'] == 1) {
			if ($this->_checkTcp('localhost', 143)) {
				$data['imapserver'] = 1;
			} else {
				$data['imapserver'] = 0;
				$state = 'error'; // because service is down
			}
		}
461

462
463
464
465
466
467
468
469
470
471
		/* Monitor BIND-Server */
		$data['bindserver'] = -1; // unknown - not needed
		if ($services['dns_server'] == 1) {
			if ($this->_checkUdp('localhost', 53)) {
				$data['bindserver'] = 1;
			} else {
				$data['bindserver'] = 0;
				$state = 'error'; // because service is down
			}
		}
472

473
474
475
		/* Monitor MySQL Server */
		$data['mysqlserver'] = -1; // unknown - not needed
		if ($services['db_server'] == 1) {
476
                       if ($this->_checkTcp($conf['db_host'], $conf['db_port'])) {
477
478
479
480
481
482
				$data['mysqlserver'] = 1;
			} else {
				$data['mysqlserver'] = 0;
				$state = 'error'; // because service is down
			}
		}
Florian Schaal's avatar
Florian Schaal committed
483
/*
484
485
486
487
488
		$data['mongodbserver'] = -1;
		if ($this->_checkTcp('localhost', 27017)) {
			$data['mongodbserver'] = 1;
		} else {
			$data['mongodbserver'] = 0;
Florian Schaal's avatar
Florian Schaal committed
489
*/
490
491
			//$state = 'error'; // because service is down
			/* TODO!!! check if this is a mongodbserver at all, otherwise it will always throw an error state!!! */
Florian Schaal's avatar
Florian Schaal committed
492
//		}
493
494
495
496
497
498
499
500
501
502

		/*
		 * Return the Result
		 */
		$res['server_id'] = $server_id;
		$res['type'] = $type;
		$res['data'] = $data;
		$res['state'] = $state;
		return $res;
	}
503

504
	public function _getLogData($log, $max_lines = 100) {
505
506
507
508
		global $conf;

		$dist = '';
		$logfile = '';
509
		$journalmatch = '';
510
511
512
513
514
515
516
517
518
519
520
521

		if (@is_file('/etc/debian_version')) {
			$dist = 'debian';
		} elseif (@is_file('/etc/redhat-release')) {
			$dist = 'redhat';
		} elseif (@is_file('/etc/SuSE-release')) {
			$dist = 'suse';
		} elseif (@is_file('/etc/gentoo-release')) {
			$dist = 'gentoo';
		}

		switch ($log) {
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
		case 'log_mail':
			if ($dist == 'debian') {
				$logfile = '/var/log/mail.log';
			} elseif ($dist == 'redhat') {
				$logfile = '/var/log/maillog';
			} elseif ($dist == 'suse') {
				$logfile = '/var/log/mail.info';
			} elseif ($dist == 'gentoo') {
				$logfile = '/var/log/maillog';
			}
			break;
		case 'log_mail_warn':
			if ($dist == 'debian') {
				$logfile = '/var/log/mail.warn';
			} elseif ($dist == 'redhat') {
				$logfile = '/var/log/maillog';
			} elseif ($dist == 'suse') {
				$logfile = '/var/log/mail.warn';
			} elseif ($dist == 'gentoo') {
				$logfile = '/var/log/maillog';
			}
			break;
		case 'log_mail_err':
			if ($dist == 'debian') {
				$logfile = '/var/log/mail.err';
			} elseif ($dist == 'redhat') {
				$logfile = '/var/log/maillog';
			} elseif ($dist == 'suse') {
				$logfile = '/var/log/mail.err';
			} elseif ($dist == 'gentoo') {
				$logfile = '/var/log/maillog';
			}
			break;
		case 'log_messages':
			if ($dist == 'debian') {
				$logfile = '/var/log/syslog';
			} elseif ($dist == 'redhat') {
559
				$journalmatch = ' ';
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
			} elseif ($dist == 'suse') {
				$logfile = '/var/log/messages';
			} elseif ($dist == 'gentoo') {
				$logfile = '/var/log/messages';
			}
			break;
		case 'log_ispc_cron':
			if ($dist == 'debian') {
				$logfile = $conf['ispconfig_log_dir'] . '/cron.log';
			} elseif ($dist == 'redhat') {
				$logfile = $conf['ispconfig_log_dir'] . '/cron.log';
			} elseif ($dist == 'suse') {
				$logfile = $conf['ispconfig_log_dir'] . '/cron.log';
			} elseif ($dist == 'gentoo') {
				$logfile = '/var/log/cron';
			}
			break;
Thom's avatar
Thom committed
577
		case 'log_letsencrypt':
Jesse Norell's avatar
Jesse Norell committed
578
				$check_files = array();
579
580
581
				if(file_exists($conf['ispconfig_log_dir'].'/acme.log')) {
					$check_files[] = $conf['ispconfig_log_dir'].'/acme.log';
				}
582
				if(file_exists('/root/.acme.sh/acme.sh') && file_exists('/root/.acme.sh/acme.sh.log')) {
583
584
585
586
587
588
					$check_files[] = '/root/.acme.sh/acme.sh.log';
				}
				if(file_exists('/usr/local/ispconfig/server/scripts/acme.sh') && file_exists('/usr/local/ispconfig/server/scripts/acme.sh.log')) {
					$check_files[] = '/usr/local/ispconfig/server/scripts/acme.sh.log';
				}
				if(file_exists('/var/log/letsencrypt/letsencrypt.log')) {
Jesse Norell's avatar
Jesse Norell committed
589
					$check_files[] = '/var/log/letsencrypt/letsencrypt.log';
590
591
592
593
594
595
596
597
598
599
				}
				$logfile = '';
				$newest = 0;

				foreach($check_files as $file) {
					$mtime = filemtime($file);
					if($mtime > $newest) {
						$newest = $mtime;
						$logfile = $file;
					}
Thom's avatar
Thom committed
600
				}
601
				unset($check_files);
Thom's avatar
Thom committed
602
			break;
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
		case 'log_freshclam':
			if ($dist == 'debian') {
				$logfile = '/var/log/clamav/freshclam.log';
			} elseif ($dist == 'redhat') {
				$logfile = (is_file('/var/log/clamav/freshclam.log') ? '/var/log/clamav/freshclam.log' : '/var/log/freshclam.log');
			} elseif ($dist == 'suse') {
				$logfile = '/var/log/freshclam.log';
			} elseif ($dist == 'gentoo') {
				$logfile = '/var/log/clamav/freshclam.log';
			}
			break;
		case 'log_clamav':
			if ($dist == 'debian') {
				$logfile = '/var/log/clamav/clamav.log';
			} elseif ($dist == 'redhat') {
				$logfile = (is_file('/var/log/clamav/clamd.log') ? '/var/log/clamav/clamd.log' : '/var/log/maillog');
			} elseif ($dist == 'suse') {
				$logfile = '/var/log/clamd.log';
			} elseif ($dist == 'gentoo') {
				$logfile = '/var/log/clamav/clamd.log';
			}
			break;
		case 'log_fail2ban':
			if ($dist == 'debian') {
				$logfile = '/var/log/fail2ban.log';
			} elseif ($dist == 'redhat') {
				$logfile = '/var/log/fail2ban.log';
			} elseif ($dist == 'suse') {
				$logfile = '/var/log/fail2ban.log';
			} elseif ($dist == 'gentoo') {
				$logfile = '/var/log/fail2ban.log';
			}
			break;
		case 'log_mongodb':
			$logfile = '/var/log/mongodb/mongodb.log';
			break;
		case 'log_ispconfig':
			if ($dist == 'debian') {
				$logfile = $conf['ispconfig_log_dir'] . '/ispconfig.log';
			} elseif ($dist == 'redhat') {
				$logfile = $conf['ispconfig_log_dir'] . '/ispconfig.log';
			} elseif ($dist == 'suse') {
				$logfile = $conf['ispconfig_log_dir'] . '/ispconfig.log';
			} elseif ($dist == 'gentoo') {
				$logfile = $conf['ispconfig_log_dir'] . '/ispconfig.log';
			}
			break;
		default:
			$logfile = '';
			break;
653
654
655
656
657
658
659
660
		}

		// Getting the logfile content
		if ($logfile != '') {
			if (stristr($logfile, ';') or substr($logfile, 0, 9) != '/var/log/' or stristr($logfile, '..')) {
				$log = 'Logfile path error.';
			} else {
				if (is_readable($logfile)) {
661
					$log = $this->_getOutputFromExecCommand('tail -n '.intval($max_lines).' ' . escapeshellarg($logfile));
662
663
664
665
				} else {
					$log = 'Unable to read ' . $logfile;
				}
			}
666
		} else {
667
			if($journalmatch != ''){
668
				$log = $this->_getOutputFromExecCommand('journalctl -n '.intval($max_lines).' --no-pager ' . escapeshellcmd($journalmatch));
669
			}else{
670
				$log = 'Unable to read logfile';
671
672
			}

673
674
675
676
677
		}

		return $log;
	}

678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
	private function _getOutputFromExecCommand ($command) {
		$log = '';
		$fd = popen($command, 'r');
		if ($fd) {
			$n = 0;
			while (!feof($fd)) {
				$log .= fgets($fd, 4096);
				$n++;
				if ($n > 1000)
					break;
			}
			fclose($fd);
		}
		return $log;
	}

694
	private function _checkTcp($host, $port) {
695
		/* Try to open a connection */
696
697
698
		$fp = @fsockopen($host, $port, $errno, $errstr, 2);

		if ($fp) {
699
			/*
700
701
			 * We got a connection, this means, everything is O.K.
			 * But maybe we are able to do more deep testing?
702
			 */
703
704
705
706
707
708
			if ($port == 80) {
				/*
				 * Port 80 means, testing APACHE
				 * So we can do a deepter test and try to get data over this connection.
				 * (if apache hangs, we get a connection but a timeout by trying to GET the data!)
				 */
709
710
711
712
713
714
715
				// fwrite($fp, "GET / HTTP/1.0\r\n\r\n");
				$out = "GET / HTTP/1.1\r\n";
				$out .= "Host: localhost\r\n";
				$out .= "User-Agent: Mozilla/5.0 (ISPConfig monitor)\r\n";
				$out .= "Accept: application/xml,application/xhtml+xml,text/html\r\n";
				$out .= "Connection: Close\r\n\r\n";
				fwrite($fp, $out);
716
717
718
719
720
721
				stream_set_timeout($fp, 5); // Timeout after 5 seconds
				$res = fread($fp, 10);  // try to get 10 bytes (enough to test!)
				$info = stream_get_meta_data($fp);
				if ($info['timed_out']) {
					return false; // Apache was not able to send data over this connection
				}
722
			}
723
724
725
726
727

			/* The connection is no longer needed */
			fclose($fp);
			/* We are able to establish a connection */
			return true;
728
		} else {
729
730
			/* We are NOT able to establish a connection */
			return false;
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
		}
	}

	private function _checkUdp($host, $port) {

		$fp = @fsockopen('udp://' . $host, $port, $errno, $errstr, 2);

		if ($fp) {
			fclose($fp);
			return true;
		} else {
			return false;
		}
	}

	private function _checkFtp($host, $port) {

		$conn_id = @ftp_connect($host, $port);

		if ($conn_id) {
			@ftp_close($conn_id);
			return true;
		} else {
			return false;
		}
	}
757
758
759
760
761
762
763
764
765

	/**
	 * Set the state to the given level (or higher, but not lesser).
	 * * If the actual state is critical and you call the method with ok,
	 *   then the state is critical.
	 *
	 * * If the actual state is critical and you call the method with error,
	 *   then the state is error.
	 */
766
	public function _setState($oldState, $newState) {
767
768
769
770
		/*
		 * Calculate the weight of the old state
		 */
		switch ($oldState) {
771
772
773
774
775
776
777
778
779
780
781
782
783
784
		case 'no_state': $oldInt = 0;
			break;
		case 'ok': $oldInt = 1;
			break;
		case 'unknown': $oldInt = 2;
			break;
		case 'info': $oldInt = 3;
			break;
		case 'warning': $oldInt = 4;
			break;
		case 'critical': $oldInt = 5;
			break;
		case 'error': $oldInt = 6;
			break;
785
786
787
788
789
		}
		/*
		 * Calculate the weight of the new state
		 */
		switch ($newState) {
790
791
792
793
794
795
796
797
798
799
800
801
802
803
		case 'no_state': $newInt = 0;
			break;
		case 'ok': $newInt = 1;
			break;
		case 'unknown': $newInt = 2;
			break;
		case 'info': $newInt = 3;
			break;
		case 'warning': $newInt = 4;
			break;
		case 'critical': $newInt = 5;
			break;
		case 'error': $newInt = 6;
			break;
804
805
806
807
808
809
810
811
812
813
814
		}

		/*
		 * Set to the higher level
		 */
		if ($newInt > $oldInt) {
			return $newState;
		} else {
			return $oldState;
		}
	}
815

816
817
818
819
820
821
822
823
824
825
826
	/**
	 * Deletes Records older than 4 minutes.
	 * The monitor writes new data every 5 minutes or longer (4 hour, 1 day).
	 * So if i delete all Date older than 4 minutes i can be sure, that all old data
	 * are deleted...
	 */
	public function delOldRecords($type, $serverId) {
		global $app;

		// $now = time();
		// $old = $now - (4 * 60); // 4 minutes
827
		$old = 240; //seconds
828
829
830
831
832
833
834
835
836

		/*
		 * ATTENTION if i do NOT pay attention of the server id, i delete all data (of the type)
		 * of ALL servers. This means, if i have a multiserver-environment and a server has a
		 * time not synced with the others (for example, all server has 11:00 and ONE server has
		 * 10:45) then the actual data of this server (with the time-stamp 10:45) get lost
		 * even though it is the NEWEST data of this server. To avoid this i HAVE to include
		 * the server-id!
		 */
837
		$sql = 'DELETE FROM `monitor_data` WHERE `type` = ? AND `created` < UNIX_TIMESTAMP() - ? AND `server_id` = ?';
838
		$app->dbmaster->query($sql, $type, $old, $serverId);
839
840
	}

841
	public function send_notification_email($template, $placeholders, $recipients) {
Jesse Norell's avatar
Jesse Norell committed
842
		global $app, $conf;
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858

		if(!is_array($recipients) || count($recipients) < 1) return false;
		if(!is_array($placeholders)) $placeholders = array();

		if(file_exists($conf['rootpath'].'/conf-custom/mail/' . $template . '_'.$conf['language'].'.txt')) {
			$lines = file($conf['rootpath'].'/conf-custom/mail/' . $template . '_'.$conf['language'].'.txt');
		} elseif(file_exists($conf['rootpath'].'/conf-custom/mail/' . $template . '_en.txt')) {
			$lines = file($conf['rootpath'].'/conf-custom/mail/' . $template . '_en.txt');
		} elseif(file_exists($conf['rootpath'].'/conf/mail/' . $template . '_'.$conf['language'].'.txt')) {
			$lines = file($conf['rootpath'].'/conf/mail/' . $template . '_'.$conf['language'].'.txt');
		} else {
			$lines = file($conf['rootpath'].'/conf/mail/' . $template . '_en.txt');
		}

		//* get mail headers, subject and body
		$mailHeaders = '';
Jesse Norell's avatar
Jesse Norell committed
859
		$mailFrom = '';
860
861
862
863
		$mailBody = '';
		$mailSubject = '';
		$inHeader = true;
		for($l = 0; $l < count($lines); $l++) {
Olivier BOUMATI's avatar
Olivier BOUMATI committed
864
865
			/* Trim only in headers */
			if($inHeader && trim($lines[$l]) == '') {
866
867
868
869
870
				$inHeader = false;
				continue;
			}
			if($inHeader == true) {
				$parts = explode(':', $lines[$l], 2);
871
872
873
874
				if(strtolower($parts[0]) == 'subject') {
					$mailSubject = trim($parts[1]);
					continue;
				}
875
				if(strtolower($parts[0]) == 'from') {
Jesse Norell's avatar
Jesse Norell committed
876
877
878
					$mailFrom = trim($parts[1]);
					continue;
				}
879
				if(strtolower($parts[0]) == 'cc') {
Jesse Norell's avatar
Jesse Norell committed
880
881
882
883
884
					if (! in_array(trim($parts[1]), $recipients)) {
						$recipients[] = trim($parts[1]);
					}
					continue;
				}
885
886
887
888
889
890
891
892
893
894
				unset($parts);
				$mailHeaders .= trim($lines[$l]) . "\n";
			} else {
				$mailBody .= trim($lines[$l]) . "\n";
			}
		}
		$mailBody = trim($mailBody);

		//* Replace placeholders
		$mailHeaders = strtr($mailHeaders, $placeholders);
895
		$mailFrom = strtr($mailFrom, $placeholders);
896
897
898
899
		$mailSubject = strtr($mailSubject, $placeholders);
		$mailBody = strtr($mailBody, $placeholders);

		for($r = 0; $r < count($recipients); $r++) {
Jesse Norell's avatar
Jesse Norell committed
900
			$app->functions->mail($recipients[$r], $mailSubject, $mailBody, $mailFrom);
901
902
903
904
		}

		return true;
	}
905

906
907
}

908
?>