|
| 1 | +<?php |
| 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 { |
| 32 | + |
| 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 | + |
| 56 | + $app->uses('getconf'); |
| 57 | + $server_config = $app->getconf->get_server_config($conf['server_id'], 'server'); |
| 58 | + if($server_config['monitor_system_updates'] == 'n') return; |
| 59 | + |
| 60 | + /* used for all monitor cronjobs */ |
| 61 | + $app->load('monitor_tools'); |
| 62 | + $this->_tools = new monitor_tools(); |
| 63 | + /* end global section for monitor cronjobs */ |
| 64 | + |
| 65 | + /* the id of the server as int */ |
| 66 | + $server_id = intval($conf['server_id']); |
| 67 | + |
| 68 | + /** The type of the data */ |
| 69 | + |
| 70 | + |
| 71 | + $type = 'system_update'; |
| 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 | + */ |
| 79 | + shell_exec('while fuser /var/lib/apt/lists/lock >/dev/null 2>&1 ; do sleep 2; done; apt-get update'); |
| 80 | + |
| 81 | + /* |
| 82 | + * Then test the upgrade. |
| 83 | + * if there is any output, then there is a needed update |
| 84 | + */ |
| 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'); |
| 86 | + if ($aptData == '') { |
| 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 | + |
| 163 | + } elseif(file_exists('/etc/redhat-release')) { |
| 164 | + /* |
| 165 | + * update and find the upgrade. |
| 166 | + * if there is any output, then there is a needed update |
| 167 | + */ |
| 168 | + |
| 169 | + /* try to figure out the default package manager first */ |
| 170 | + if(file_exists('/usr/bin/dnf') && (is_link('/usr/bin/yum'))) { |
| 171 | + $rhPkgMgr = 'dnf'; |
| 172 | + } elseif(file_exists('/usr/bin/dnf') && (!file_exists('/usr/bin/yum'))) { |
| 173 | + $rhPkgMgr = 'dnf'; |
| 174 | + } else { |
| 175 | + $rhPkgMgr = 'yum'; |
| 176 | + } |
| 177 | + |
| 178 | + $aptData = shell_exec($rhPkgMgr. ' -q list updates'); |
| 179 | + |
| 180 | + if ($aptData == '') { |
| 181 | + /* There is nothing to update! */ |
| 182 | + $state = 'ok'; |
| 183 | + } else { |
| 184 | + /* |
| 185 | + * There is something to update! this is in most cases not critical, so we can |
| 186 | + * do a system-update once a month or so... |
| 187 | + */ |
| 188 | + $state = 'info'; |
| 189 | + } |
| 190 | + |
| 191 | + /* |
| 192 | + * Fetch the output |
| 193 | + */ |
| 194 | + |
| 195 | + $data['output'] = shell_exec($rhPkgMgr. ' -q list updates'); |
| 196 | + |
| 197 | + } else { |
| 198 | + /* |
| 199 | + * It is not Debian/Ubuntu, so there is no data and no state |
| 200 | + * |
| 201 | + * no_state, NOT unknown, because "unknown" is shown as state |
| 202 | + * inside the GUI. no_state is hidden. |
| 203 | + * |
| 204 | + * We have to write NO DATA inside the DB, because the GUI |
| 205 | + * could not know, if there is any dat, or not... |
| 206 | + */ |
| 207 | + $state = 'no_state'; |
| 208 | + $data['output'] = ''; |
| 209 | + } |
| 210 | + |
| 211 | + $res = array(); |
| 212 | + $res['server_id'] = $server_id; |
| 213 | + $res['type'] = $type; |
| 214 | + $res['data'] = $data; |
| 215 | + $res['state'] = $state; |
| 216 | + |
| 217 | + //* Ensure that output is encoded so that it does not break the serialize |
| 218 | + //$res['data']['output'] = htmlentities($res['data']['output']); |
| 219 | + $res['data']['output'] = htmlentities($res['data']['output'], ENT_QUOTES, 'UTF-8'); |
| 220 | + |
| 221 | + /* |
| 222 | + * Insert the data into the database |
| 223 | + */ |
| 224 | + $sql = 'REPLACE INTO monitor_data (server_id, type, created, data, state) ' . |
| 225 | + 'VALUES (?, ?, UNIX_TIMESTAMP(), ?, ?)'; |
| 226 | + $app->dbmaster->query($sql, $res['server_id'], $res['type'], serialize($res['data']), $res['state']); |
| 227 | + |
| 228 | + /* The new data is written, now we can delete the old one */ |
| 229 | + $this->_tools->delOldRecords($res['type'], $res['server_id']); |
| 230 | + |
| 231 | + parent::onRunJob(); |
| 232 | + } |
| 233 | + |
| 234 | + /* this function is optional if it contains no custom code */ |
| 235 | + public function onAfterRun() { |
| 236 | + global $app; |
| 237 | + |
| 238 | + parent::onAfterRun(); |
| 239 | + } |
| 240 | + |
| 241 | +} |
| 242 | + |
| 243 | +?> |
0 commit comments