Marius Burkard
2016-04-20 4569cae57f127afd093794310ccd290d2d9fdf36
commit | author | age
5de2af 1 <?php
M 2
3 /*
4 Copyright (c) 2013, Marius Cramer, pixcept KG
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without modification,
8 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       this list of conditions and the following disclaimer in the documentation
14       and/or other materials provided with the distribution.
15     * Neither the name of ISPConfig nor the names of its contributors
16       may be used to endorse or promote products derived from this software without
17       specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
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.
29 */
30
31 class cronjob_monitor_clamav_log extends cronjob {
b1a6a5 32
MC 33     // job schedule
34     protected $_schedule = '*/5 * * * *';
35     protected $_run_at_new = true;
36
37     private $_tools = null;
38
39     /* this function is optional if it contains no custom code */
40     public function onPrepare() {
41         global $app;
42
43         parent::onPrepare();
44     }
45
46     /* this function is optional if it contains no custom code */
47     public function onBeforeRun() {
48         global $app;
49
50         return parent::onBeforeRun();
51     }
52
53     public function onRunJob() {
54         global $app, $conf;
55
56         /* used for all monitor cronjobs */
57         $app->load('monitor_tools');
58         $this->_tools = new monitor_tools();
59         /* end global section for monitor cronjobs */
60
5de2af 61         /* the id of the server as int */
M 62         $server_id = intval($conf['server_id']);
63
64         /** The type of the data */
b1a6a5 65
MC 66
5de2af 67         $type = 'log_clamav';
M 68
69         /* Get the data of the log */
70         $data = $this->_tools->_getLogData($type);
71
72         // Todo: the state should be calculated.
73         $state = 'ok';
74
75         $res = array();
76         $res['server_id'] = $server_id;
77         $res['type'] = $type;
78         $res['data'] = $data;
79         $res['state'] = $state;
80
81         /*
82          * Insert the data into the database
83          */
84         $sql = 'REPLACE INTO monitor_data (server_id, type, created, data, state) ' .
cc7a82 85             'VALUES (?, ?, UNIX_TIMESTAMP(), ?, ?)';
MC 86         $app->dbmaster->query($sql, $res['server_id'], $res['type'], serialize($res['data']), $res['state']);
5de2af 87
M 88         /* The new data is written, now we can delete the old one */
89         $this->_tools->delOldRecords($res['type'], $res['server_id']);
90
91
92         /** The type of the data */
93         $type = 'log_freshclam';
94
95         /* Get the data of the log */
96         $data = $this->_tools->_getLogData($type);
97
98         /* Get the data from the LAST log-Entry.
99          * if there can be found:
100          * WARNING: Your ClamAV installation is OUTDATED!
101          * then the clamav is outdated. This is a warning!
102          */
103         $state = 'ok';
104
105         $tmp = explode("\n", $data);
106         $lastLog = array();
107         if ($tmp[sizeof($tmp) - 1] == '') {
108             /* the log ends with an empty line remove this */
109             array_pop($tmp);
110         }
111         if (strpos($tmp[sizeof($tmp) - 1], '-------------') !== false) {
112             /* the log ends with "-----..." remove this */
113             array_pop($tmp);
114         }
115         for ($i = sizeof($tmp) - 1; $i > 0; $i--) {
116             if (strpos($tmp[$i], '---------') === false) {
117                 /* no delimiter found, so add this to the last-log */
118                 $lastLog[] = $tmp[$i];
119             } else {
120                 /* delimiter found, so there is no more line left! */
121                 break;
122             }
123         }
124
125         /*
126          * Now we have the last log in the array.
127          * Check if the outdated-string is found...
128          */
d73810 129         $clamav_outdated_warning = false;
TB 130         $clamav_bytecode_updated = false;
5de2af 131         foreach ($lastLog as $line) {
277af4 132             if (stristr($line,"Can't download daily.cvd from")) {
d73810 133                 $clamav_outdated_warning = true;
5de2af 134             }
d73810 135             if(stristr($line,'main.cld is up to date')) {
TB 136                 $clamav_bytecode_updated = true;
137             }
138         }
139         
140         //* Warn when clamav is outdated and main.cld update failed.
141         if($clamav_outdated_warning == true && $clamav_bytecode_updated == false) {
94a38c 142             $state = $this->_tools->_setState($state, 'info');
5de2af 143         }
M 144
145         $res = array();
146         $res['server_id'] = $server_id;
147         $res['type'] = $type;
148         $res['data'] = $data;
149         $res['state'] = $state;
150
151         /*
152          * Insert the data into the database
153          */
154         $sql = 'REPLACE INTO monitor_data (server_id, type, created, data, state) ' .
cc7a82 155             'VALUES (?, ?, UNIX_TIMESTAMP(), ?, ?)';
MC 156         $app->dbmaster->query($sql, $res['server_id'], $res['type'], serialize($res['data']), $res['state']);
5de2af 157
M 158         /* The new data is written, now we can delete the old one */
159         $this->_tools->delOldRecords($res['type'], $res['server_id']);
160
b1a6a5 161
MC 162         parent::onRunJob();
163     }
164
165     /* this function is optional if it contains no custom code */
166     public function onAfterRun() {
167         global $app;
168
169         parent::onAfterRun();
170     }
5de2af 171
M 172 }
173
174 ?>