monitor_core_module.inc.php 22.1 KB
Newer Older
1
<?php
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
  Copyright (c) 2007-2011, Till Brehm, projektfarm Gmbh and Oliver Vogel www.muv.com
  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
30

31
class monitor_core_module {
32

33
	var $interval = 5; // do the monitoring every 5 minutes
34
35
36
37
	var $module_name = 'monitor_core_module';
	var $class_name = 'monitor_core_module';
	/* No actions at this time. maybe later... */
	var $actions_available = array();
38
	/** The Tools */
39
40
41
42
43
44
45
	private $_tools = null;

	/**
	 * This function is called during ispconfig installation to determine
	 * if a symlink shall be created for this plugin.
	 */
	public function onInstall() {
46
47
48
		global $conf;
		return true;
	}
49

50
51
52
53
	/**
	 * This function is called when the module is loaded
	 */
	public function onLoad() {
54
		global $app;
55

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

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

73
74
75
76
	/**
	 * This method is called every n minutes, when the module ist loaded.
	 * The method then does a system-monitoring
	 */
77
	// TODO: what monitoring is done should be a config-var
78
	private function _doMonitor() {
vogelor's avatar
vogelor committed
79
		global $app;
80
		/*
81
82
83
84
		 * We need the tools in almost every method, so initialize them once...
		 */
		$app->load('monitor_tools');
		$this->_tools = new monitor_tools();
85

86
		/*
87
88
		 * Calls the single Monitoring steps 
		 */
latham's avatar
latham committed
89
		$this->_monitorEmailQuota();
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
		$this->_monitorHDQuota();
		$this->_monitorServer();
		$this->_monitorOsVer();
		$this->_monitorIspcVer();
		$this->_monitorDiskUsage();
		$this->_monitorMemUsage();
		$this->_monitorCpu();
		$this->_monitorServices();
		if (@file_exists('/proc/user_beancounters')) {
			$this->_monitorOpenVzHost();
			$this->_monitorOpenVzUserBeancounter();
		}
		$this->_monitorMailLog();
		$this->_monitorMailWarnLog();
		$this->_monitorMailErrLog();
		$this->_monitorMessagesLog();
		$this->_monitorISPCCronLog();
		$this->_monitorFreshClamLog();
		$this->_monitorClamAvLog();
		$this->_monitorIspConfigLog();
		$this->_monitorSystemUpdate();
		$this->_monitorMailQueue();
		$this->_monitorRaid();
		$this->_monitorRkHunter();
		$this->_monitorFail2ban();
		$this->_monitorSysLog();
	}

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
    private function _monitorEmailQuota() {
        global $app;

        /*
         * First we get the Monitoring-data from the tools
         */
        $res = $this->_tools->monitorEmailQuota();

        /*
         * Insert the data into the database
         */
        $sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
                'VALUES (' .
                $res['server_id'] . ', ' .
                "'" . $app->dbmaster->quote($res['type']) . "', " .
                'UNIX_TIMESTAMP(), ' .
                "'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
                "'" . $res['state'] . "'" .
                ')';
        $app->dbmaster->query($sql);

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

143
144
	private function _monitorHDQuota() {
		global $app;
145
146

		/*
147
148
149
		 * First we get the Monitoring-data from the tools
		 */
		$res = $this->_tools->monitorHDQuota();
150

151
152
153
		/*
		 * Insert the data into the database
		 */
154
		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
155
156
157
				'VALUES (' .
				$res['server_id'] . ', ' .
				"'" . $app->dbmaster->quote($res['type']) . "', " .
158
				'UNIX_TIMESTAMP(), ' .
159
160
				"'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
				"'" . $res['state'] . "'" .
161
				')';
162
163
164
		$app->dbmaster->query($sql);

		/* The new data is written, now we can delete the old one */
vogelor's avatar
vogelor committed
165
		$this->_delOldRecords($res['type'], $res['server_id']);
166
	}
167

168
169
	private function _monitorServer() {
		global $app;
170

171
		/*
172
173
174
		 * First we get the Monitoring-data from the tools
		 */
		$res = $this->_tools->monitorServer();
175

176
		/*
177
178
		 * Insert the data into the database
		 */
179
		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
180
181
182
				'VALUES (' .
				$res['server_id'] . ', ' .
				"'" . $app->dbmaster->quote($res['type']) . "', " .
183
				'UNIX_TIMESTAMP(), ' .
184
185
				"'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
				"'" . $res['state'] . "'" .
186
				')';
187
188
189
		$app->dbmaster->query($sql);

		/* The new data is written, now we can delete the old one */
vogelor's avatar
vogelor committed
190
		$this->_delOldRecords($res['type'], $res['server_id']);
191
192
	}

193
	private function _monitorOsVer() {
194
		global $app;
195

196
		/*
197
198
199
		 * First we get the Monitoring-data from the tools
		 */
		$res = $this->_tools->monitorOsVer();
200

201
		/*
202
203
		 * Insert the data into the database
		 */
204
		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
205
206
207
				'VALUES (' .
				$res['server_id'] . ', ' .
				"'" . $app->dbmaster->quote($res['type']) . "', " .
208
				'UNIX_TIMESTAMP(), ' .
209
210
				"'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
				"'" . $res['state'] . "'" .
211
				')';
212
213
214
		$app->dbmaster->query($sql);

		/* The new data is written, now we can delete the old one */
vogelor's avatar
vogelor committed
215
		$this->_delOldRecords($res['type'], $res['server_id']);
216
	}
217

218
	private function _monitorIspcVer() {
219
220
221
		global $app;

		/*
222
223
224
		 * First we get the Monitoring-data from the tools
		 */
		$res = $this->_tools->monitorIspcVer();
225

226
		/*
227
228
		 * Insert the data into the database
		 */
229
		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
230
231
232
				'VALUES (' .
				$res['server_id'] . ', ' .
				"'" . $app->dbmaster->quote($res['type']) . "', " .
233
				'UNIX_TIMESTAMP(), ' .
234
235
				"'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
				"'" . $res['state'] . "'" .
236
				')';
237
238
239
		$app->dbmaster->query($sql);

		/* The new data is written, now we can delete the old one */
vogelor's avatar
vogelor committed
240
		$this->_delOldRecords($res['type'], $res['server_id']);
241
242
	}

243
	private function _monitorDiskUsage() {
244
		global $app;
245

246
		/*
247
248
249
		 * First we get the Monitoring-data from the tools
		 */
		$res = $this->_tools->monitorDiskUsage();
250

251
		/*
252
253
		 * Insert the data into the database
		 */
254
		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
255
256
257
				'VALUES (' .
				$res['server_id'] . ', ' .
				"'" . $app->dbmaster->quote($res['type']) . "', " .
258
				'UNIX_TIMESTAMP(), ' .
259
260
				"'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
				"'" . $res['state'] . "'" .
261
				')';
262
263
264
		$app->dbmaster->query($sql);

		/* The new data is written, now we can delete the old one */
vogelor's avatar
vogelor committed
265
		$this->_delOldRecords($res['type'], $res['server_id']);
266
267
	}

268
	private function _monitorMemUsage() {
269
270
		global $app;
		/*
271
272
273
		 * First we get the Monitoring-data from the tools
		 */
		$res = $this->_tools->monitorMemUsage();
274

275
		/*
276
277
		 * Insert the data into the database
		 */
278
		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
279
280
281
				'VALUES (' .
				$res['server_id'] . ', ' .
				"'" . $app->dbmaster->quote($res['type']) . "', " .
282
				'UNIX_TIMESTAMP(), ' .
283
284
				"'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
				"'" . $res['state'] . "'" .
285
				')';
286
287
288
		$app->dbmaster->query($sql);

		/* The new data is written, now we can delete the old one */
vogelor's avatar
vogelor committed
289
		$this->_delOldRecords($res['type'], $res['server_id']);
290
291
	}

292
	private function _monitorCpu() {
293
294
		global $app;
		/*
295
296
297
		 * First we get the Monitoring-data from the tools
		 */
		$res = $this->_tools->monitorCpu();
298

299
		/*
300
301
		 * Insert the data into the database
		 */
302
		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
303
304
305
				'VALUES (' .
				$res['server_id'] . ', ' .
				"'" . $app->dbmaster->quote($res['type']) . "', " .
306
				'UNIX_TIMESTAMP(), ' .
307
308
				"'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
				"'" . $res['state'] . "'" .
309
				')';
310
311
312
		$app->dbmaster->query($sql);

		/* The new data is written, now we can delete the old one */
vogelor's avatar
vogelor committed
313
		$this->_delOldRecords($res['type'], $res['server_id']);
314
315
	}

316
	private function _monitorServices() {
317
		global $app;
318

319
320
321
322
		/*
		 * First we get the Monitoring-data from the tools
		 */
		$res = $this->_tools->monitorServices();
323

324
		/*
325
326
		 * Insert the data into the database
		 */
327
		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
328
329
330
				'VALUES (' .
				$res['server_id'] . ', ' .
				"'" . $app->dbmaster->quote($res['type']) . "', " .
331
				'UNIX_TIMESTAMP(), ' .
332
333
				"'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
				"'" . $res['state'] . "'" .
334
				')';
335
336
337
		$app->dbmaster->query($sql);

		/* The new data is written, now we can delete the old one */
vogelor's avatar
vogelor committed
338
		$this->_delOldRecords($res['type'], $res['server_id']);
339
340
	}

341
	private function _monitorOpenVzHost() {
342
		global $app;
343

344
		/*
345
346
347
		 * First we get the Monitoring-data from the tools
		 */
		$res = $this->_tools->monitorOpenVzHost();
348

349
		/*
350
351
		 * Insert the data into the database
		 */
352
		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
353
354
355
				'VALUES (' .
				$res['server_id'] . ', ' .
				"'" . $app->dbmaster->quote($res['type']) . "', " .
356
				'UNIX_TIMESTAMP(), ' .
357
358
				"'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
				"'" . $res['state'] . "'" .
359
				')';
360
361
362
		$app->dbmaster->query($sql);

		/* The new data is written, now we can delete the old one */
vogelor's avatar
vogelor committed
363
		$this->_delOldRecords($res['type'], $res['server_id']);
364
365
	}

366
	private function _monitorOpenVzUserBeancounter() {
367
		global $app;
368

369
		/*
370
371
372
		 * First we get the Monitoring-data from the tools
		 */
		$res = $this->_tools->monitorOpenVzUserBeancounter();
373

374
		/*
375
376
		 * Insert the data into the database
		 */
377
		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
378
379
380
				'VALUES (' .
				$res['server_id'] . ', ' .
				"'" . $app->dbmaster->quote($res['type']) . "', " .
381
				'UNIX_TIMESTAMP(), ' .
382
383
				"'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
				"'" . $res['state'] . "'" .
384
				')';
385
386
387
		$app->dbmaster->query($sql);

		/* The new data is written, now we can delete the old one */
vogelor's avatar
vogelor committed
388
		$this->_delOldRecords($res['type'], $res['server_id']);
389
390
	}

391
	private function _monitorSystemUpdate() {
392
		/*
393
394
		 *  This monitoring is expensive, so do it only once an hour
		 */
395
		$min = @date('i');
396
397
		if ($min != 0)
			return;
398

399
		/*
400
401
		 * OK - here we go...
		 */
402
		global $app;
403

404
405
406
407
		/*
		 * First we get the Monitoring-data from the tools
		 */
		$res = $this->_tools->monitorSystemUpdate();
408

409
		/*
410
411
		 * Insert the data into the database
		 */
412
		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
413
414
415
				'VALUES (' .
				$res['server_id'] . ', ' .
				"'" . $app->dbmaster->quote($res['type']) . "', " .
416
				'UNIX_TIMESTAMP(), ' .
417
418
				"'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
				"'" . $res['state'] . "'" .
419
				')';
420
421
422
		$app->dbmaster->query($sql);

		/* The new data is written, now we can delete the old one */
vogelor's avatar
vogelor committed
423
		$this->_delOldRecords($res['type'], $res['server_id']);
424
425
	}

426
	private function _monitorMailQueue() {
427
		global $app;
428

429
		/*
430
431
432
		 * First we get the Monitoring-data from the tools
		 */
		$res = $this->_tools->monitorMailQueue();
433

434
		/*
435
436
		 * Insert the data into the database
		 */
437
		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
438
439
440
				'VALUES (' .
				$res['server_id'] . ', ' .
				"'" . $app->dbmaster->quote($res['type']) . "', " .
441
				'UNIX_TIMESTAMP(), ' .
442
443
				"'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
				"'" . $res['state'] . "'" .
444
				')';
445
446
447
		$app->dbmaster->query($sql);

		/* The new data is written, now we can delete the old one */
vogelor's avatar
vogelor committed
448
		$this->_delOldRecords($res['type'], $res['server_id']);
449
450
	}

451
	private function _monitorRaid() {
452
		global $app;
453

454
		/*
455
456
457
		 * First we get the Monitoring-data from the tools
		 */
		$res = $this->_tools->monitorRaid();
458

459
		/*
460
461
		 * Insert the data into the database
		 */
462
		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
463
464
465
				'VALUES (' .
				$res['server_id'] . ', ' .
				"'" . $app->dbmaster->quote($res['type']) . "', " .
466
				'UNIX_TIMESTAMP(), ' .
467
468
				"'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
				"'" . $res['state'] . "'" .
469
				')';
470
471
472
		$app->dbmaster->query($sql);

		/* The new data is written, now we can delete the old one */
vogelor's avatar
vogelor committed
473
		$this->_delOldRecords($res['type'], $res['server_id']);
474
475
	}

476
	private function _monitorRkHunter() {
477
		/*
478
479
		 *  This monitoring is expensive, so do it only once a day
		 */
480
481
		$min = @date('i');
		$hour = @date('H');
482
483
484
485
486
		if (!($min == 0 && $hour == 23))
			return;
		/*
		 * OK . here we go...
		 */
487
		global $app;
488

489
490
491
492
		/*
		 * First we get the Monitoring-data from the tools
		 */
		$res = $this->_tools->monitorRkHunter();
493

494
		/*
495
496
		 * Insert the data into the database
		 */
497
		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
498
499
500
				'VALUES (' .
				$res['server_id'] . ', ' .
				"'" . $app->dbmaster->quote($res['type']) . "', " .
501
				'UNIX_TIMESTAMP(), ' .
502
503
				"'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
				"'" . $res['state'] . "'" .
504
				')';
505
506
507
		$app->dbmaster->query($sql);

		/* The new data is written, now we can delete the old one */
vogelor's avatar
vogelor committed
508
		$this->_delOldRecords($res['type'], $res['server_id']);
509
510
	}

511
	private function _monitorFail2ban() {
512
		global $app;
513

514
		/*
515
516
517
		 * First we get the Monitoring-data from the tools
		 */
		$res = $this->_tools->monitorFail2ban();
518

519
520
521
		/*
		 * Insert the data into the database
		 */
522
		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
523
524
525
				'VALUES (' .
				$res['server_id'] . ', ' .
				"'" . $app->dbmaster->quote($res['type']) . "', " .
526
				'UNIX_TIMESTAMP(), ' .
527
528
				"'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
				"'" . $res['state'] . "'" .
529
				')';
530
531
532
		$app->dbmaster->query($sql);

		/* The new data is written, now we can delete the old one */
vogelor's avatar
vogelor committed
533
		$this->_delOldRecords($res['type'], $res['server_id']);
534
535
	}

536
	private function _monitorSysLog() {
537
		global $app;
538

539
		/*
540
541
542
		 * First we get the Monitoring-data from the tools
		 */
		$res = $this->_tools->monitorSysLog();
543

544
		/*
545
546
		 * Insert the data into the database
		 */
547
		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
548
549
550
				'VALUES (' .
				$res['server_id'] . ', ' .
				"'" . $app->dbmaster->quote($res['type']) . "', " .
551
				'UNIX_TIMESTAMP(), ' .
552
553
				"'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
				"'" . $res['state'] . "'" .
554
				')';
555
556
557
		$app->dbmaster->query($sql);

		/* The new data is written, now we can delete the old one */
vogelor's avatar
vogelor committed
558
		$this->_delOldRecords($res['type'], $res['server_id']);
559
560
	}

561
	private function _monitorMailLog() {
562
		global $app;
563

564
		/*
565
566
567
		 * First we get the Monitoring-data from the tools
		 */
		$res = $this->_tools->monitorMailLog();
568

569
		/*
570
571
		 * Insert the data into the database
		 */
572
		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
573
574
575
				'VALUES (' .
				$res['server_id'] . ', ' .
				"'" . $app->dbmaster->quote($res['type']) . "', " .
576
				'UNIX_TIMESTAMP(), ' .
577
578
				"'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
				"'" . $res['state'] . "'" .
579
				')';
580
581
582
		$app->dbmaster->query($sql);

		/* The new data is written, now we can delete the old one */
vogelor's avatar
vogelor committed
583
		$this->_delOldRecords($res['type'], $res['server_id']);
584
585
	}

586
	private function _monitorMailWarnLog() {
587
		global $app;
588

589
		/*
590
591
592
		 * First we get the Monitoring-data from the tools
		 */
		$res = $this->_tools->monitorMailWarnLog();
593

594
		/*
595
596
		 * Insert the data into the database
		 */
597
		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
598
599
600
				'VALUES (' .
				$res['server_id'] . ', ' .
				"'" . $app->dbmaster->quote($res['type']) . "', " .
601
				'UNIX_TIMESTAMP(), ' .
602
603
				"'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
				"'" . $res['state'] . "'" .
604
				')';
605
606
607
		$app->dbmaster->query($sql);

		/* The new data is written, now we can delete the old one */
vogelor's avatar
vogelor committed
608
		$this->_delOldRecords($res['type'], $res['server_id']);
609
610
	}

611
	private function _monitorMailErrLog() {
612
		global $app;
613

614
		/*
615
616
617
		 * First we get the Monitoring-data from the tools
		 */
		$res = $this->_tools->monitorMailErrLog();
618

619
		/*
620
621
		 * Insert the data into the database
		 */
622
		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
623
624
625
				'VALUES (' .
				$res['server_id'] . ', ' .
				"'" . $app->dbmaster->quote($res['type']) . "', " .
626
				'UNIX_TIMESTAMP(), ' .
627
628
				"'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
				"'" . $res['state'] . "'" .
629
				')';
630
631
632
		$app->dbmaster->query($sql);

		/* The new data is written, now we can delete the old one */
vogelor's avatar
vogelor committed
633
		$this->_delOldRecords($res['type'], $res['server_id']);
634
	}
635

636
	private function _monitorMessagesLog() {
637
		global $app;
638

639
		/*
640
641
642
		 * First we get the Monitoring-data from the tools
		 */
		$res = $this->_tools->monitorMessagesLog();
643

644
		/*
645
646
		 * Insert the data into the database
		 */
647
		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
648
649
650
				'VALUES (' .
				$res['server_id'] . ', ' .
				"'" . $app->dbmaster->quote($res['type']) . "', " .
651
				'UNIX_TIMESTAMP(), ' .
652
653
				"'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
				"'" . $res['state'] . "'" .
654
				')';
655
656
657
		$app->dbmaster->query($sql);

		/* The new data is written, now we can delete the old one */
vogelor's avatar
vogelor committed
658
		$this->_delOldRecords($res['type'], $res['server_id']);
659
660
	}

661
	private function _monitorISPCCronLog() {
662
		global $app;
663

664
		/*
665
666
667
		 * First we get the Monitoring-data from the tools
		 */
		$res = $this->_tools->monitorISPCCronLog();
668

669
		/*
670
671
		 * Insert the data into the database
		 */
672
		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
673
674
675
				'VALUES (' .
				$res['server_id'] . ', ' .
				"'" . $app->dbmaster->quote($res['type']) . "', " .
676
				'UNIX_TIMESTAMP(), ' .
677
678
				"'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
				"'" . $res['state'] . "'" .
679
				')';
680
681
682
		$app->dbmaster->query($sql);

		/* The new data is written, now we can delete the old one */
vogelor's avatar
vogelor committed
683
		$this->_delOldRecords($res['type'], $res['server_id']);
684
685
	}

686
	private function _monitorFreshClamLog() {
687
		global $app;
688

689
		/*
690
691
692
		 * First we get the Monitoring-data from the tools
		 */
		$res = $this->_tools->monitorFreshClamLog();
693

694
		/*
695
696
		 * Insert the data into the database
		 */
697
		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
698
699
700
				'VALUES (' .
				$res['server_id'] . ', ' .
				"'" . $app->dbmaster->quote($res['type']) . "', " .
701
				'UNIX_TIMESTAMP(), ' .
702
703
				"'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
				"'" . $res['state'] . "'" .
704
				')';
705
706
707
		$app->dbmaster->query($sql);

		/* The new data is written, now we can delete the old one */
vogelor's avatar
vogelor committed
708
		$this->_delOldRecords($res['type'], $res['server_id']);
709
710
	}

711
	private function _monitorClamAvLog() {
712
713
		global $app;

714
		/*
715
716
717
		 * First we get the Monitoring-data from the tools
		 */
		$res = $this->_tools->monitorClamAvLog();
718

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

		/* The new data is written, now we can delete the old one */
vogelor's avatar
vogelor committed
733
		$this->_delOldRecords($res['type'], $res['server_id']);
734
735
	}

736
	private function _monitorIspConfigLog() {
737
		global $app;
738

739
		/*
740
741
742
		 * First we get the Monitoring-data from the tools
		 */
		$res = $this->_tools->monitorIspConfigLog();
743

744
745
746
		/*
		 * Insert the data into the database
		 */
747
		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
748
749
750
				'VALUES (' .
				$res['server_id'] . ', ' .
				"'" . $app->dbmaster->quote($res['type']) . "', " .
751
				'UNIX_TIMESTAMP(), ' .
752
753
				"'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
				"'" . $res['state'] . "'" .
754
				')';
755
756
757
		$app->dbmaster->query($sql);

		/* The new data is written, now we can delete the old one */
vogelor's avatar
vogelor committed
758
		$this->_delOldRecords($res['type'], $res['server_id']);
759
760
	}

761
762
763
764
765
	/**
	 * 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...
766
	 */
767
	private function _delOldRecords($type, $serverId) {
768
769
770
		global $app;

		$now = time();
771
772
773
774
775
776
777
778
779
		$old = $now - (4 * 60); // 4 minutes
		/*
		 * 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!
		 */
780
781
		$sql = 'DELETE FROM monitor_data ' .
				'WHERE ' .
782
783
784
				'  type =' . "'" . $app->dbmaster->quote($type) . "' " .
				'AND ' .
				'  created < ' . $old . ' ' .
785
				'AND ' .
786
				'  server_id = ' . $serverId;
787
788
789
790
		$app->dbmaster->query($sql);
	}

}
791

792
?>