Marius Cramer
2015-08-06 37b29231e47a0c4458dc1c15d98588f16f07e1e2
commit | author | age
21c641 1 <?php
FS 2
3 /**
b1a6a5 4  Copyright (c) 2013, Marius Cramer, pixcept KG
MC 5  Copyright (c) 2013, Florian Schaal, info@schaal-24.de
6  All rights reserved.
21c641 7
b1a6a5 8  Redistribution and use in source and binary forms, with or without modification,
MC 9  are permitted provided that the following conditions are met:
21c641 10
b1a6a5 11  * Redistributions of source code must retain the above copyright notice,
MC 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.
21c641 19
b1a6a5 20  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
MC 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
21c641 32
FS 33 class cronjob_monitor_database_size extends cronjob {
b1a6a5 34
MC 35     // job schedule
36     protected $_schedule = '*/5 * * * *';
37     protected $_run_at_new = true;
38
39     private $_tools = null;
40
41
42
43     /**
44      * this function is optional if it contains no custom code
45      */
46     public function onPrepare() {
47         global $app;
48
49         parent::onPrepare();
50     }
51
52
53
54     /**
55      * this function is optional if it contains no custom code
56      */
57     public function onBeforeRun() {
58         global $app;
59
60         return parent::onBeforeRun();
61     }
62
63     public function onRunJob() {
64         global $app, $conf;
65
66         /* used for all monitor cronjobs */
67         $app->load('monitor_tools');
68         $this->_tools = new monitor_tools();
69         /* end global section for monitor cronjobs */
70
21c641 71         /* the id of the server as int */
FS 72         $server_id = intval($conf['server_id']);
73
74         /** The type of the data */
75         $type = 'database_size';
76
77         /** The state of the database-usage */
78         $state = 'ok';
79
0543b2 80         /** Fetch the data of all databases into an array */
cc7a82 81         $databases = $app->db->queryAllRecords("SELECT database_name, sys_groupid FROM web_database WHERE server_id = ? GROUP BY sys_groupid, database_name ASC", $server_id);
0543b2 82
F 83         if(is_array($databases) && !empty($databases)) {
84
21c641 85             $data = array();
0543b2 86
F 87             for ($i = 0; $i < sizeof($databases); $i++) {
88                 $data[$i]['database_name']= $databases[$i]['database_name'];
89                 $data[$i]['size'] = $app->db->getDatabaseSize($databases[$i]['database_name']);
90                 $data[$i]['sys_groupid'] = $databases[$i]['sys_groupid'];
21c641 91             }
0543b2 92
F 93             $res = array();
94             $res['server_id'] = $server_id;
95             $res['type'] = $type;
96             $res['data'] = $data;
97             $res['state'] = $state;
98
99             //* Insert the data into the database
100             $sql = 'REPLACE INTO monitor_data (server_id, type, created, data, state) ' .
cc7a82 101                 'VALUES (?, ?, UNIX_TIMESTAMP(), ?, ?)';
MC 102             $app->dbmaster->query($sql, $res['server_id'], $res['type'], serialize($res['data']), $res['state']);
0543b2 103
F 104             //* The new data is written, now we can delete the old one
105             $this->_tools->delOldRecords($res['type'], $res['server_id']);
21c641 106         }
b1a6a5 107
MC 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     }
21c641 117
FS 118 }
119
120 ?>