Marius Cramer
2015-08-06 37b29231e47a0c4458dc1c15d98588f16f07e1e2
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_system_update extends cronjob {
b1a6a5 32
MC 33     // job schedule
34     protected $_schedule = '0 * * * *';
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
7c2179 56         $app->uses('getconf');
MC 57         $server_config = $app->getconf->get_server_config($conf['server_id'], 'server');
58         if($server_config['monitor_system_updates'] == 'n') return;
59         
b1a6a5 60         /* used for all monitor cronjobs */
MC 61         $app->load('monitor_tools');
62         $this->_tools = new monitor_tools();
63         /* end global section for monitor cronjobs */
64
5de2af 65         /* the id of the server as int */
M 66         $server_id = intval($conf['server_id']);
67
68         /** The type of the data */
b1a6a5 69
MC 70
5de2af 71         $type = 'system_update';
M 72
73         /* This monitoring is only available on Debian or Ubuntu */
74         if (file_exists('/etc/debian_version')) {
75
76             /*
77              * first update the "apt database"
78              */
96c95a 79             shell_exec('while fuser /var/lib/apt/lists/lock >/dev/null 2>&1 ; do sleep 2; done; apt-get update');
5de2af 80
M 81             /*
82              * Then test the upgrade.
83              * if there is any output, then there is a needed update
84              */
96c95a 85             $aptData = shell_exec('while fuser /var/lib/dpkg/lock >/dev/null 2>&1 || fuser /var/lib/apt/lists/lock >/dev/null 2>&1 ; do sleep 2; done; apt-get -s -qq dist-upgrade');
5de2af 86             if ($aptData == '') {
M 87                 /* There is nothing to update! */
88                 $state = 'ok';
89             } else {
90                 /*
91                  * There is something to update! this is in most cases not critical, so we can
92                  * do a system-update once a month or so...
93                  */
94                 $state = 'info';
95             }
96
97             /*
98              * Fetch the output
99              */
100             $data['output'] = $aptData;
101         } elseif (file_exists('/etc/gentoo-release')) {
102
103             /*
104              * first update the portage tree
105              */
106
107             // 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)
108             $do_update = true;
109             if (file_exists('/usr/portage/metadata/timestamp.chk')) {
110                 $datetime = file_get_contents('/usr/portage/metadata/timestamp.chk');
111                 $datetime = trim($datetime);
112
113                 $dstamp = strtotime($datetime);
114                 if ($dstamp) {
115                     $checkat = $dstamp + 14400; // + 4hours
116                     if (mktime() < $checkat) {
117                         $do_update = false;
118                     }
119                 }
120             }
121
122             if ($do_update) {
123                 shell_exec('emerge --sync --quiet');
124             }
125
126             /*
127              * Then test the upgrade.
128              * if there is any output, then there is a needed update
129              */
130             $emergeData = shell_exec('glsa-check -t affected');
131             if ($emergeData == '') {
132                 /* There is nothing to update! */
133                 $state = 'ok';
134                 $data['output'] = 'No unapplied GLSA\'s found on the system.';
135             } else {
136                 /* There is something to update! */
137                 $state = 'info';
138                 $data['output'] = shell_exec('glsa-check -pv --nocolor affected 2>/dev/null');
139             }
140         } elseif (file_exists('/etc/SuSE-release')) {
141
142             /*
143              * update and find the upgrade.
144              * if there is any output, then there is a needed update
145              */
146             $aptData = shell_exec('zypper -q lu');
147             if ($aptData == '') {
148                 /* There is nothing to update! */
149                 $state = 'ok';
150             } else {
151                 /*
152                  * There is something to update! this is in most cases not critical, so we can
153                  * do a system-update once a month or so...
154                  */
155                 $state = 'info';
156             }
157
158             /*
159              * Fetch the output
160              */
161             $data['output'] = shell_exec('zypper lu');
162         } else {
163             /*
164              * It is not Debian/Ubuntu, so there is no data and no state
165              *
166              * no_state, NOT unknown, because "unknown" is shown as state
167              * inside the GUI. no_state is hidden.
168              *
169              * We have to write NO DATA inside the DB, because the GUI
170              * could not know, if there is any dat, or not...
171              */
172             $state = 'no_state';
173             $data['output'] = '';
174         }
175
176         $res = array();
177         $res['server_id'] = $server_id;
178         $res['type'] = $type;
179         $res['data'] = $data;
180         $res['state'] = $state;
181
182         //* Ensure that output is encoded so that it does not break the serialize
183         //$res['data']['output'] = htmlentities($res['data']['output']);
b1a6a5 184         $res['data']['output'] = htmlentities($res['data']['output'], ENT_QUOTES, 'UTF-8');
5de2af 185
M 186         /*
187          * Insert the data into the database
188          */
189         $sql = 'REPLACE INTO monitor_data (server_id, type, created, data, state) ' .
cc7a82 190             'VALUES (?, ?, UNIX_TIMESTAMP(), ?, ?)';
MC 191         $app->dbmaster->query($sql, $res['server_id'], $res['type'], serialize($res['data']), $res['state']);
5de2af 192
M 193         /* The new data is written, now we can delete the old one */
194         $this->_tools->delOldRecords($res['type'], $res['server_id']);
b1a6a5 195
MC 196         parent::onRunJob();
197     }
198
199     /* this function is optional if it contains no custom code */
200     public function onAfterRun() {
201         global $app;
202
203         parent::onAfterRun();
204     }
5de2af 205
M 206 }
207
208 ?>