monitor_core_module.inc.php 48.5 KB
Newer Older
1
2
<?php
/*
3
Copyright (c) 2007-2010, Till Brehm, projektfarm Gmbh and Oliver Vogel www.muv.com
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
All rights reserved.

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

    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
    * Neither the name of ISPConfig nor the names of its contributors
      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.
*/
redray's avatar
redray committed
29

30
class monitor_core_module {
31
	var $interval = 5; // do the monitoring every 5 minutes
32

33
34
35
36
	var $module_name = 'monitor_core_module';
	var $class_name = 'monitor_core_module';
	/* No actions at this time. maybe later... */
	var $actions_available = array();
37

38
	//* This function is called during ispconfig installation to determine
39
40
41
	//  if a symlink shall be created for this plugin.
	function onInstall() {
		global $conf;
42

43
		return true;
44

45
	}
46

47
	/*
48
        This function is called when the module is loaded
49
50
51
	*/
	function onLoad() {
		global $app;
52

53
		/*
54
         * Do the monitor every n minutes and write the result to the db
55
56
57
58
59
60
61
62
		*/
		$min = date('i');
		if (($min % $this->interval) == 0) {
			$this->doMonitor();
		}
	}

	/*
63
64
     This function is called when a change in one of the registered tables is detected.
     The function then raises the events for the plugins.
65
66
	*/
	function process($tablename, $action, $data) {
67
		// not needed
68
69
70
71
	} // end function

	//** Get distribution identifier
	//** IMPORTANT!
72
73
	//   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!
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
	//   Please do not forget to remove the swriteln(); - lines here at this file
	function get_distname() {

		$distname = '';
		$distver = '';
		$distid = '';
		$distbaseid = '';

		//** Debian or Ubuntu
		if(file_exists('/etc/debian_version')) {

			if(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';
98
				$distid = 'debian60';
99
100
101
102
103
104
105
106
107
108
				$distbaseid = 'debian';
			}  else {
				$distname = 'Debian';
				$distver = 'Unknown';
				$distid = 'debian40';
				$distbaseid = 'debian';
			}
		}

		//** OpenSuSE
109
		elseif(file_exists('/etc/SuSE-release')) {
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
			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.1';
				$distid = 'opensuse110';
				$distbaseid = 'opensuse';
			}  else {
				$distname = 'openSUSE';
				$distver = 'Unknown';
				$distid = 'opensuse110';
				$distbaseid = 'opensuse';
			}
		}


		//** Redhat
135
		elseif(file_exists('/etc/redhat-release')) {
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

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

			if(stristr($content,'Fedora release 9 (Sulphur)')) {
				$distname = 'Fedora';
				$distver = '9';
				$distid = 'fedora9';
				$distbaseid = 'fedora';
			} elseif(stristr($content,'Fedora release 10 (Cambridge)')) {
				$distname = 'Fedora';
				$distver = '10';
				$distid = 'fedora9';
				$distbaseid = 'fedora';
			} elseif(stristr($content,'Fedora release 10')) {
				$distname = 'Fedora';
				$distver = '11';
				$distid = 'fedora9';
				$distbaseid = 'fedora';
			} elseif(stristr($content,'CentOS release 5.2 (Final)')) {
				$distname = 'CentOS';
				$distver = '5.2';
				$distid = 'centos52';
				$distbaseid = 'fedora';
			} elseif(stristr($content,'CentOS release 5.3 (Final)')) {
				$distname = 'CentOS';
				$distver = '5.3';
				$distid = 'centos53';
				$distbaseid = 'fedora';
			} else {
				$distname = 'Redhat';
				$distver = 'Unknown';
				$distid = 'fedora9';
				$distbaseid = 'fedora';
			}
		}

		//** Gentoo
173
		elseif(file_exists('/etc/gentoo-release')) {
174
175
176
177
178
179
180
181
182
183

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

			preg_match_all('/([0-9]{1,2})/', $content, $version);
			$distname = 'Gentoo';
			$distver = $version[0][0].$version[0][1];
			$distid = 'gentoo';
			$distbaseid = 'gentoo';

		} else {
184
			die('Unrecognized GNU/Linux distribution');
185
186
187
188
189
190
191
		}

		return array('name' => $distname, 'version' => $distver, 'id' => $distid, 'baseid' => $distbaseid);
	}


	/*
192
193
    This method is called every n minutes, when the module ist loaded.
    The method then does a system-monitoring
194
195
196
197
	*/
	// TODO: what monitoring is done should be a config-var
	function doMonitor() {
		/* Calls the single Monitoring steps */
198
		$this->monitorHDQuota();
199
200
		$this->monitorServer();
		$this->monitorOSVer();
201
		$this->monitorIspCVer();
202
203
204
205
		$this->monitorDiskUsage();
		$this->monitorMemUsage();
		$this->monitorCpu();
		$this->monitorServices();
206
207
		$this->monitorOpenVzHost();
		$this->monitorOpenVzUserBeancounter();
208
209
210
211
212
213
214
215
216
217
218
219
		$this->monitorMailLog();
		$this->monitorMailWarnLog();
		$this->monitorMailErrLog();
		$this->monitorMessagesLog();
		$this->monitorISPCCronLog();
		$this->monitorFreshClamLog();
		$this->monitorClamAvLog();
		$this->monitorIspConfigLog();
		$this->monitorSystemUpdate();
		$this->monitorMailQueue();
		$this->monitorRaid();
		$this->monitorRkHunter();
220
		$this->monitorFail2ban();
221
222
		$this->monitorSysLog();
	}
223
224
225
226
227
228
229
230
231
	
	function monitorHDQuota() {
		global $app;
		global $conf;
		
		/* Initialize data array */
		$data = array();

		/* the id of the server as int */
232
		$server_id = intval($conf['server_id']);
233
234
235

		/** The type of the data */
		$type = 'harddisk_quota';
236

237
238
239
240
		/** The state of the harddisk_quota. */
		$state = 'ok';
		
		/** Fetch the data for all users*/
241
		$dfData = shell_exec('repquota -asu');
242
243
244
245
246
247
248
249
250
251
252
253

		// split into array
		$df = explode("\n", $dfData);

		/*
         * ignore the first 5 lines, process the rest
		*/
		for($i=5; $i <= sizeof($df); $i++) {
			if ($df[$i] != '') {
				/*
                 * Make a array of the data
				*/
254
				$s = preg_split ('/[\s]+/', $df[$i]);
255
256
257
258
259
260
261
262
				$username = $s[0];
				$data['user'][$username]['used'] = $s[2];
				$data['user'][$username]['soft'] = $s[3];
				$data['user'][$username]['hard'] = $s[4];
			}
		}
		
		/** Fetch the data for all users*/
263
		$dfData = shell_exec('repquota -asg');
264
265
266
267
268
269
270
271
272
273
274
275

		// split into array
		$df = explode("\n", $dfData);

		/*
         * ignore the first 5 lines, process the rest
		*/
		for($i=5; $i <= sizeof($df); $i++) {
			if ($df[$i] != '') {
				/*
                 * Make a array of the data
				*/
276
				$s = preg_split ('/[\s]+/', $df[$i]);
277
278
279
280
281
282
283
284
285
286
				$groupname = $s[0];
				$data['group'][$groupname]['used'] = $s[1];
				$data['group'][$groupname]['soft'] = $s[2];
				$data['group'][$groupname]['hard'] = $s[3];
			}
		}

		/*
        Insert the data into the database
		*/
287
288
289
		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
				'VALUES ('.
				$server_id . ', ' .
290
				"'" . $app->dbmaster->quote($type) . "', " .
291
				time() . ', ' .
292
293
				"'" . $app->dbmaster->quote(serialize($data)) . "', " .
				"'" . $state . "'" .
294
				')';
295
296
297
298
299
300
		$app->dbmaster->query($sql);

		/* The new data is written, now we can delete the old one */
		$this->_delOldRecords($type, 4);
	}
	
301
302
303
	function monitorServer() {
		global $app;
		global $conf;
304

305
		/* the id of the server as int */
306
		$server_id = intval($conf['server_id']);
307

308
309
		/** The type of the data */
		$type = 'server_load';
310

311
		/*
312
        Fetch the data into a array
313
314
315
316
317
318
		*/
		$procUptime = shell_exec("cat /proc/uptime | cut -f1 -d' '");
		$data['up_days'] = floor($procUptime/86400);
		$data['up_hours'] = floor(($procUptime-$data['up_days']*86400)/3600);
		$data['up_minutes'] = floor(($procUptime-$data['up_days']*86400-$data['up_hours']*3600)/60);

319
		$data['uptime'] = shell_exec('uptime');
320

321
322
		$tmp = explode(',', $data['uptime'], 4);
		$tmpUser = explode(' ', trim($tmp[2]));
323
		$data['user_online'] = intval($tmpUser[0]);
324
325
326

		//* New Load Average code to fix "always zero" bug in non-english distros. NEEDS TESTING
		$loadTmp = shell_exec("cat /proc/loadavg | cut -f1-3 -d' '");
327
		$load = explode(' ', $loadTmp);
328
329
330
		$data['load_1'] = floatval(str_replace(',', '.', $load[0]));
		$data['load_5'] = floatval(str_replace(',', '.', $load[1]));
		$data['load_15'] = floatval(str_replace(',', '.', $load[2]));
331

332
333
334
335
336
337
		/** The state of the server-load. */
		$state = 'ok';
		if ($data['load_1'] > 20 ) $state = 'info';
		if ($data['load_1'] > 50 ) $state = 'warning';
		if ($data['load_1'] > 100 ) $state = 'critical';
		if ($data['load_1'] > 150 ) $state = 'error';
338

339
		/*
340
        Insert the data into the database
341
		*/
342
343
344
		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
				'VALUES ('.
				$server_id . ', ' .
345
				"'" . $app->dbmaster->quote($type) . "', " .
346
				time() . ', ' .
347
348
				"'" . $app->dbmaster->quote(serialize($data)) . "', " .
				"'" . $state . "'" .
349
				')';
350
351
352
		$app->dbmaster->query($sql);

		/* The new data is written, now we can delete the old one */
353
		$this->_delOldRecords($type, 4);
354
355
356
357
358
359
360
	}

	function monitorOsVer() {
		global $app;
		global $conf;

		/* the id of the server as int */
361
		$server_id = intval($conf['server_id']);
362
363
364

		/** The type of the data */
		$type = 'os_info';
365

366
367
368
369
370
371
372
373
374
375
376
377
378
379
		/*
        Fetch the data into a array
		*/
		$dist = $this->get_distname();

		$data['name'] = $dist['name'];
		$data['version'] = $dist['version'];

		/* the OS has no state. It is, what it is */
		$state = 'no_state';

		/*
        Insert the data into the database
		*/
380
381
382
		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
				'VALUES ('.
				$server_id . ', ' .
383
				"'" . $app->dbmaster->quote($type) . "', " .
384
				time() . ', ' .
385
386
				"'" . $app->dbmaster->quote(serialize($data)) . "', " .
				"'" . $state . "'" .
387
				')';
388
389
390
		$app->dbmaster->query($sql);

		/* The new data is written, now we can delete the old one */
391
		$this->_delOldRecords($type, 4);
392
	}
393

394

395
396
397
398
399
	function monitorIspcVer() {
		global $app;
		global $conf;

		/* the id of the server as int */
400
		$server_id = intval($conf['server_id']);
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416

		/** The type of the data */
		$type = 'ispc_info';

		/*
        Fetch the data into a array
		*/
		$data['name'] = ISPC_APP_TITLE;
		$data['version'] = ISPC_APP_VERSION;

		/* the ISPC-Version has no state. It is, what it is */
		$state = 'no_state';

		/*
        Insert the data into the database
		*/
417
418
419
		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
				'VALUES ('.
				$server_id . ', ' .
420
				"'" . $app->dbmaster->quote($type) . "', " .
421
				time() . ', ' .
422
423
				"'" . $app->dbmaster->quote(serialize($data)) . "', " .
				"'" . $state . "'" .
424
				')';
425
426
427
428
429
430
		$app->dbmaster->query($sql);

		/* The new data is written, now we can delete the old one */
		$this->_delOldRecords($type, 4);
	}

431
432
433
	function monitorDiskUsage() {
		global $app;
		global $conf;
434

435
		/* the id of the server as int */
436
		$server_id = intval($conf['server_id']);
437

438
439
		/** The type of the data */
		$type = 'disk_usage';
440

441
442
		/** The state of the disk-usage */
		$state = 'ok';
443

444
		/** Fetch the data of ALL devices into a array (needed for monitoring!)*/
445
		$dfData = shell_exec('df -hT');
446

447
448
		// split into array
		$df = explode("\n", $dfData);
449

450
		/*
451
         * ignore the first line, process the rest
452
453
454
455
		*/
		for($i=1; $i <= sizeof($df); $i++) {
			if ($df[$i] != '') {
				/*
456
                 * Make an array of the data
457
				*/
458
				$s = preg_split ('/[\s]+/', $df[$i]);
459
460
461
462
463
464
465
466
				$data[$i]['fs'] = $s[0];
				$data[$i]['type'] = $s[1];
				$data[$i]['size'] = $s[2];
				$data[$i]['used'] = $s[3];
				$data[$i]['available'] = $s[4];
				$data[$i]['percent'] = $s[5];
				$data[$i]['mounted'] = $s[6];
				/*
467
                 * calculate the state
468
469
470
				*/
				$usePercent = floatval($data[$i]['percent']);

471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
				//* We don't want to check some filesystem which have no sensible filling levels
				switch($data[$i]['type']) {
					case 'iso9660':
					case 'cramfs':
					case 'udf':
					case 'tmpfs':
					case 'devtmpfs':
					case 'udev':
						break;
					default: 
						if ($usePercent > 75) $state = $this->_setState($state, 'info');
						if ($usePercent > 80) $state = $this->_setState($state, 'warning');
						if ($usePercent > 90) $state = $this->_setState($state, 'critical');
						if ($usePercent > 95) $state = $this->_setState($state, 'error');
						break;
486
				}
487
488
			}
		}
489
490


491
		/*
492
        Insert the data into the database
493
		*/
494
495
496
		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
				'VALUES ('.
				$server_id . ', ' .
497
				"'" . $app->dbmaster->quote($type) . "', " .
498
				time() . ', ' .
499
500
				"'" . $app->dbmaster->quote(serialize($data)) . "', " .
				"'" . $state . "'" .
501
				')';
502
503
504
		$app->dbmaster->query($sql);

		/* The new data is written, now we can delete the old one */
505
		$this->_delOldRecords($type, 4);
506
507
508
509
510
511
512
513
	}


	function monitorMemUsage() {
		global $app;
		global $conf;

		/* the id of the server as int */
514
		$server_id = intval($conf['server_id']);
515
516
517
518
519

		/** The type of the data */
		$type = 'mem_usage';

		/*
520
        Fetch the data into a array
521
		*/
522
		$miData = shell_exec('cat /proc/meminfo');
523

524
		$memInfo = explode("\n", $miData);
525

526
		foreach($memInfo as $line) {
527
			$part = preg_split('/:/', $line);
528
			$key = trim($part[0]);
529
			$tmp = explode(' ', trim($part[1]));
530
531
532
533
			$value = 0;
			if ($tmp[1] == 'kB') $value = $tmp[0] * 1024;
			$data[$key] = $value;
		}
534

535
		/*
536
537
         * actually this info has no state.
         * maybe someone knows better...???...
538
539
		*/
		$state = 'no_state';
540

541
		/*
542
        Insert the data into the database
543
		*/
544
545
546
		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
				'VALUES ('.
				$server_id . ', ' .
547
				"'" . $app->dbmaster->quote($type) . "', " .
548
				time() . ', ' .
549
550
				"'" . $app->dbmaster->quote(serialize($data)) . "', " .
				"'" . $state . "'" .
551
				')';
552
553
554
		$app->dbmaster->query($sql);

		/* The new data is written, now we can delete the old one */
555
		$this->_delOldRecords($type, 4);
556
557
558
559
560
561
562
563
	}


	function monitorCpu() {
		global $app;
		global $conf;

		/* the id of the server as int */
564
		$server_id = intval($conf['server_id']);
565
566
567
568
569

		/** The type of the data */
		$type = 'cpu_info';

		/*
570
        Fetch the data into a array
571
		*/
572
573
574
575
576
577
578
579
580
581
582
583
584
		if(file_exists('/proc/cpuinfo')) {
			$cpuData = shell_exec('cat /proc/cpuinfo');
			$cpuInfo = explode("\n", $cpuData);
			$processor = 0;

			foreach($cpuInfo as $line) {

				$part = preg_split('/:/', $line);
				$key = trim($part[0]);
				$value = trim($part[1]);
				if($key == 'processor') $processor = intval($value);
				if($key != '') $data[$key.' '.$processor] = $value;
			}
585

586
587
588
589
590
591
592
593
594
595
596
597
598
599
			/* the cpu has no state. It is, what it is */
			$state = 'no_state';
		} else {
			/*
             * It is not Linux, so there is no data and no state
             *
             * no_state, NOT unknown, because "unknown" is shown as state
             * inside the GUI. no_state is hidden.
             *
             * We have to write NO DATA inside the DB, because the GUI
             * could not know, if there is any dat, or not...
			*/
			$state = 'no_state';
			$data['output']= '';
600
		}
601
602


603
		/*
604
        Insert the data into the database
605
		*/
606
607
608
		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
				'VALUES ('.
				$server_id . ', ' .
609
				"'" . $app->dbmaster->quote($type) . "', " .
610
				time() . ', ' .
611
612
				"'" . $app->dbmaster->quote(serialize($data)) . "', " .
				"'" . $state . "'" .
613
				')';
614
615
616
		$app->dbmaster->query($sql);

		/* The new data is written, now we can delete the old one */
617
		$this->_delOldRecords($type, 4);
618
619
620
621
622
623
624
625
	}


	function monitorServices() {
		global $app;
		global $conf;

		/** the id of the server as int */
626
		$server_id = intval($conf['server_id']);
627
628

		/** get the "active" Services of the server from the DB */
629
		$services = $app->dbmaster->queryOneRecord('SELECT * FROM server WHERE server_id = ' . $server_id);
630
631
632
633
634

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

		/** the State of the monitoring */
635
		/* ok, if ALL active services are running,
636
637
         * error, if not
         * There is no other state!
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
		*/
		$state = 'ok';

		/* Monitor Webserver */
		$data['webserver'] = -1; // unknown - not needed
		if ($services['web_server'] == 1) {
			if($this->_checkTcp('localhost', 80)) {
				$data['webserver'] = 1;
			} else {
				$data['webserver'] = 0;
				$state = 'error'; // because service is down
			}
		}

		/* 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
			}
		}

		/* 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
			}
		}

		/* 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
			}
		}

		/* 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
			}
		}

		/* Monitor BIND-Server */
		$data['bindserver'] = -1; // unknown - not needed
		if ($services['dns_server'] == 1) {
			if($this->_checkTcp('localhost', 53)) {
				$data['bindserver'] = 1;
			} else {
				$data['bindserver'] = 0;
				$state = 'error'; // because service is down
			}
		}

707
		/* Monitor MySQL Server */
708
709
710
711
712
713
714
715
716
717
718
719
		$data['mysqlserver'] = -1; // unknown - not needed
		if ($services['db_server'] == 1) {
			if($this->_checkTcp('localhost', 3306)) {
				$data['mysqlserver'] = 1;
			} else {
				$data['mysqlserver'] = 0;
				$state = 'error'; // because service is down
			}
		}


		/*
720
        Insert the data into the database
721
		*/
722
723
724
		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
				'VALUES ('.
				$server_id . ', ' .
725
				"'" . $app->dbmaster->quote($type) . "', " .
726
				time() . ', ' .
727
728
				"'" . $app->dbmaster->quote(serialize($data)) . "', " .
				"'" . $state . "'" .
729
				')';
730
731
732
		$app->dbmaster->query($sql);

		/* The new data is written, now we can delete the old one */
733
734
735
736
737
738
739
740
741
		$this->_delOldRecords($type, 4);
	}


	function monitorOpenVzHost() {
		global $app;
		global $conf;

		/* the id of the server as int */
742
		$server_id = intval($conf['server_id']);
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759

		/** The type of the data */
		$type = 'openvz_veinfo';

		/*
        Fetch the data into a array
		*/
		$app->load(openvz_tools);
		$openVzTools = new openvz_tools();
		$data = $openVzTools->getOpenVzVeInfo();

		/* the VE-Info has no state. It is, what it is */
		$state = 'no_state';

		/*
        Insert the data into the database
		*/
760
761
762
		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
				'VALUES ('.
				$server_id . ', ' .
763
				"'" . $app->dbmaster->quote($type) . "', " .
764
				time() . ', ' .
765
766
				"'" . $app->dbmaster->quote(serialize($data)) . "', " .
				"'" . $state . "'" .
767
				')';
768
769
770
771
772
773
774
775
776
777
778
		$app->dbmaster->query($sql);

		/* The new data is written, now we can delete the old one */
		$this->_delOldRecords($type, 4);
	}

	function monitorOpenVzUserBeancounter() {
		global $app;
		global $conf;

		/* the id of the server as int */
779
		$server_id = intval($conf['server_id']);
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807

		/** The type of the data */
		$type = 'openvz_beancounter';

		/*
        Fetch the data into a array
		*/
		$app->load(openvz_tools);
		$openVzTools = new openvz_tools();
		$data = $openVzTools->getOpenVzVeBeanCounter();

		/* calculate the state of the beancounter */
		if ($data == '') {
			$state = 'no_state';
		}
		else {
			$state = 'ok';

			/* transfer this output-string into a array */
			$test = explode("\n", $data);

			/* the first list of the output is not needed */
			array_shift($test);

			/* now process all items of the rest */
			foreach ($test as $item) {
				/*
			     * eliminate all doubled spaces and spaces at the beginning and end
808
				*/
809
810
811
812
813
814
815
				while (strpos($item, '  ') !== false) {
					$item = str_replace('  ', ' ', $item);
				}
				$item = trim($item);

				/*
			     * The failcounter is the LAST
816
				*/
817
818
819
820
821
822
823
824
825
826
827
828
829
830
				if ($item != '') {
					$tmp = explode(' ', $item);
					$failCounter = $tmp[sizeof($tmp)-1];
					if ($failCounter > 0 ) $state = 'info';
					if ($failCounter > 50 ) $state = 'warning';
					if ($failCounter > 200 ) $state = 'critical';
					if ($failCounter > 10000 ) $state = 'error';
				}
			}
		}

		/*
        Insert the data into the database
		*/
831
832
833
		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
				'VALUES ('.
				$server_id . ', ' .
834
				"'" . $app->dbmaster->quote($type) . "', " .
835
				time() . ', ' .
836
837
				"'" . $app->dbmaster->quote(serialize($data)) . "', " .
				"'" . $state . "'" .
838
				')';
839
840
841
842
		$app->dbmaster->query($sql);

		/* The new data is written, now we can delete the old one */
		$this->_delOldRecords($type, 4);
843
844
845
846
847
	}


	function monitorSystemUpdate() {
		/*
848
         *  This monitoring is expensive, so do it only once an hour
849
850
851
		*/
		$min = date('i');
		if ($min != 0) return;
852

853
		/*
854
         * OK - here we go...
855
856
857
		*/
		global $app;
		global $conf;
858

859
		/* the id of the server as int */
860
		$server_id = intval($conf['server_id']);
861

862
863
		/** The type of the data */
		$type = 'system_update';
864

865
		/* This monitoring is only available on Debian or Ubuntu */
866
		if(file_exists('/etc/debian_version')) {
vogelor's avatar
vogelor committed
867

868
			/*
869
             * first update the "apt database"
870
871
			*/
			shell_exec('apt-get update');
vogelor's avatar
vogelor committed
872

873
			/*
vogelor's avatar
vogelor committed
874
875
             * Then test the upgrade.
             * if there is any output, then there is a needed update
876
877
878
879
880
881
882
			*/
			$aptData = shell_exec('apt-get -s -qq dist-upgrade');
			if ($aptData == '') {
				/* There is nothing to update! */
				$state = 'ok';
			}
			else {
883
884
885
886
887
				/*
				 * There is something to update! this is in most cases not critical, so we can
				 * do a system-update once a month or so...
				*/
				$state = 'info';
888
889
890
			}

			/*
vogelor's avatar
vogelor committed
891
             * Fetch the output
892
893
894
			*/
			$data['output'] = shell_exec('apt-get -s -q dist-upgrade');
		}
895
		elseif (file_exists('/etc/gentoo-release')) {
896
897

			/*
wyrie's avatar
wyrie committed
898
        	 * first update the portage tree
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
			*/

			// In keeping with gentoo's rsync policy, don't update to frequently (every four hours - taken from http://www.gentoo.org/doc/en/source_mirrors.xml)
			$do_update = true;
			if (file_exists('/usr/portage/metadata/timestamp.chk')) {
				$datetime = file_get_contents('/usr/portage/metadata/timestamp.chk');
				$datetime = trim($datetime);

				$dstamp = strtotime($datetime);
				if ($dstamp) {
					$checkat = $dstamp + 14400; // + 4hours
					if (mktime() < $checkat) {
						$do_update = false;
					}
				}
			}

			if ($do_update) {
				shell_exec('emerge --sync --quiet');
			}

			/*
wyrie's avatar
wyrie committed
921
922
             * Then test the upgrade.
             * if there is any output, then there is a needed update
923
924
925
926
927
928
929
930
931
932
933
934
			*/
			$emergeData = shell_exec('emerge -puDNt --color n --nospinner --quiet world');
			if ($emergeData == '') {
				/* There is nothing to update! */
				$state = 'ok';
			}
			else {
				/* There is something to update! */
				$state = 'warning';
			}

			/*
wyrie's avatar
wyrie committed
935
             * Fetch the output
936
937
938
939
940
			*/
			$data['output'] = shell_exec('emerge -pvuDNt --color n --nospinner world');
		}
		else {
			/*
941
             * It is not Debian/Ubuntu, so there is no data and no state
vogelor's avatar
vogelor committed
942
943
944
945
946
947
             *
             * no_state, NOT unknown, because "unknown" is shown as state
             * inside the GUI. no_state is hidden.
             *
             * We have to write NO DATA inside the DB, because the GUI
             * could not know, if there is any dat, or not...
948
949
950
951
			*/
			$state = 'no_state';
			$data['output']= '';
		}
952

953
		/*
954
         * Insert the data into the database
955
		*/
956
957
958
		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
				'VALUES ('.
				$server_id . ', ' .
959
				"'" . $app->dbmaster->quote($type) . "', " .
960
				time() . ', ' .
961
962
				"'" . $app->dbmaster->quote(serialize($data)) . "', " .
				"'" . $state . "'" .
963
				')';
964
965
966
		$app->dbmaster->query($sql);

		/* The new data is written, now we can delete the old one */
967
		$this->_delOldRecords($type, 4);
968
969
970
971
972
973
974
	}

	function monitorMailQueue() {
		global $app;
		global $conf;

		/* the id of the server as int */
975
		$server_id = intval($conf['server_id']);
976
977
978
979
980
981
982
983

		/** The type of the data */
		$type = 'mailq';

		/* Get the data from the mailq */
		$data['output'] = shell_exec('mailq');

		/*
984
         *  The last line has more informations
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
		*/
		$tmp = explode("\n", $data['output']);
		$more = $tmp[sizeof($tmp) - 1];
		$this->_getIntArray($more);
		$data['bytes'] = $res[0];
		$data['requests'] = $res[1];

		/** The state of the mailq. */
		$state = 'ok';
		if ($data['requests'] > 2000 ) $state = 'info';
		if ($data['requests'] > 5000 ) $state = 'warning';
		if ($data['requests'] > 8000 ) $state = 'critical';
		if ($data['requests'] > 10000 ) $state = 'error';

		/*
1000
         * Insert the data into the database
For faster browsing, not all history is shown. View entire blame