|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | +Copyright (c) 2013, Marius Cramer, pixcept KG |
| 5 | +Copyright (c) 2013, Florian Schaal, info@schaal-24.de |
| 6 | +All rights reserved. |
| 7 | +
|
| 8 | +Redistribution and use in source and binary forms, with or without modification, |
| 9 | +are permitted provided that the following conditions are met: |
| 10 | +
|
| 11 | + * Redistributions of source code must retain the above copyright notice, |
| 12 | + this list of conditions and the following disclaimer. |
| 13 | + * Redistributions in binary form must reproduce the above copyright notice, |
| 14 | + this list of conditions and the following disclaimer in the documentation |
| 15 | + and/or other materials provided with the distribution. |
| 16 | + * Neither the name of ISPConfig nor the names of its contributors |
| 17 | + may be used to endorse or promote products derived from this software without |
| 18 | + specific prior written permission. |
| 19 | +
|
| 20 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 21 | +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 22 | +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 23 | +IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 24 | +INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 25 | +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 27 | +OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 28 | +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 29 | +EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | +*/ |
| 31 | + |
| 32 | +class cronjob_monitor_database_size extends cronjob { |
| 33 | + |
| 34 | + // job schedule |
| 35 | + protected $_schedule = '*/5 * * * *'; |
| 36 | + protected $_run_at_new = true; |
| 37 | + |
| 38 | + private $_tools = null; |
| 39 | + |
| 40 | + /** |
| 41 | + * this function is optional if it contains no custom code |
| 42 | + */ |
| 43 | + public function onPrepare() { |
| 44 | + global $app; |
| 45 | + |
| 46 | + parent::onPrepare(); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * this function is optional if it contains no custom code |
| 51 | + */ |
| 52 | + public function onBeforeRun() { |
| 53 | + global $app; |
| 54 | + |
| 55 | + return parent::onBeforeRun(); |
| 56 | + } |
| 57 | + |
| 58 | + public function onRunJob() { |
| 59 | + global $app, $conf; |
| 60 | + |
| 61 | + /* used for all monitor cronjobs */ |
| 62 | + $app->load('monitor_tools'); |
| 63 | + $this->_tools = new monitor_tools(); |
| 64 | + /* end global section for monitor cronjobs */ |
| 65 | + |
| 66 | + /* the id of the server as int */ |
| 67 | + $server_id = intval($conf['server_id']); |
| 68 | + |
| 69 | + /** The type of the data */ |
| 70 | + $type = 'database_size'; |
| 71 | + |
| 72 | + /** The state of the database-usage */ |
| 73 | + $state = 'ok'; |
| 74 | + |
| 75 | + /** Fetch the data of ALL databases into an array */ |
| 76 | + $records = $app->db->queryAllRecords("SELECT database_name, sys_groupid FROM web_database WHERE server_id = $server_id"); |
| 77 | + if(is_array($records) && !empty($records)) { |
| 78 | + $data = array(); |
| 79 | + for ($i = 0; $i < sizeof($records); $i++) { |
| 80 | + $data[$i]['name'] = $records[$i]['database_name']; |
| 81 | + $data[$i]['size'] = $app->db->getDatabaseSize($data[$i]['name']); |
| 82 | + $data[$i]['client_id'] = $records[$i]['sys_groupid']; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + $res = array(); |
| 87 | + $res['server_id'] = $server_id; |
| 88 | + $res['type'] = $type; |
| 89 | + $res['data'] = $data; |
| 90 | + $res['state'] = $state; |
| 91 | + |
| 92 | + /* |
| 93 | + * Insert the data into the database |
| 94 | + */ |
| 95 | + $sql = 'REPLACE INTO monitor_data (server_id, type, created, data, state) ' . |
| 96 | + 'VALUES (' . |
| 97 | + $res['server_id'] . ', ' . |
| 98 | + "'" . $app->dbmaster->quote($res['type']) . "', " . |
| 99 | + 'UNIX_TIMESTAMP(), ' . |
| 100 | + "'" . $app->dbmaster->quote(serialize($res['data'])) . "', " . |
| 101 | + "'" . $res['state'] . "'" . |
| 102 | + ')'; |
| 103 | + $app->dbmaster->query($sql); |
| 104 | + |
| 105 | + /* The new data is written, now we can delete the old one */ |
| 106 | + $this->_tools->delOldRecords($res['type'], $res['server_id']); |
| 107 | + |
| 108 | + parent::onRunJob(); |
| 109 | + } |
| 110 | + |
| 111 | + /* this function is optional if it contains no custom code */ |
| 112 | + public function onAfterRun() { |
| 113 | + global $app; |
| 114 | + |
| 115 | + parent::onAfterRun(); |
| 116 | + } |
| 117 | + |
| 118 | +} |
| 119 | + |
| 120 | +?> |
0 commit comments