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 */ |
5512af
|
81 |
$databases = $app->db->queryAllRecords("SELECT database_id, database_name, sys_groupid, database_quota, quota_exceeded 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++) { |
5512af
|
88 |
$rec = $databases[$i]; |
MB |
89 |
|
|
90 |
$data[$i]['database_name']= $rec['database_name']; |
|
91 |
$data[$i]['size'] = $app->db->getDatabaseSize($rec['database_name']); |
|
92 |
$data[$i]['sys_groupid'] = $rec['sys_groupid']; |
|
93 |
|
|
94 |
$quota = $rec['database_quota'] * 1024 * 1024; |
e06b59
|
95 |
if(!is_numeric($quota)) continue; |
5512af
|
96 |
|
e06b59
|
97 |
if($quota < 1 || $quota > $data[$i]['size']) { |
MB |
98 |
print $rec['database_name'] . ' does not exceed quota qize: ' . $quota . ' > ' . $data[$i]['size'] . "\n"; |
5512af
|
99 |
if($rec['quota_exceeded'] == 'y') { |
MB |
100 |
$app->dbmaster->datalogUpdate('web_database', array('quota_exceeded' => 'n'), 'database_id', $rec['database_id']); |
|
101 |
} |
|
102 |
} elseif($rec['quota_exceeded'] == 'n') { |
e06b59
|
103 |
print $rec['database_name'] . ' exceeds quota qize: ' . $quota . ' < ' . $data[$i]['size'] . "\n"; |
MB |
104 |
$app->dbmaster->datalogUpdate('web_database', array('quota_exceeded' => 'y'), 'database_id', $rec['database_id']); |
5512af
|
105 |
} |
21c641
|
106 |
} |
0543b2
|
107 |
|
F |
108 |
$res = array(); |
|
109 |
$res['server_id'] = $server_id; |
|
110 |
$res['type'] = $type; |
|
111 |
$res['data'] = $data; |
|
112 |
$res['state'] = $state; |
|
113 |
|
|
114 |
//* Insert the data into the database |
|
115 |
$sql = 'REPLACE INTO monitor_data (server_id, type, created, data, state) ' . |
cc7a82
|
116 |
'VALUES (?, ?, UNIX_TIMESTAMP(), ?, ?)'; |
MC |
117 |
$app->dbmaster->query($sql, $res['server_id'], $res['type'], serialize($res['data']), $res['state']); |
0543b2
|
118 |
|
F |
119 |
//* The new data is written, now we can delete the old one |
|
120 |
$this->_tools->delOldRecords($res['type'], $res['server_id']); |
21c641
|
121 |
} |
b1a6a5
|
122 |
|
MC |
123 |
parent::onRunJob(); |
|
124 |
} |
|
125 |
|
|
126 |
/* this function is optional if it contains no custom code */ |
|
127 |
public function onAfterRun() { |
|
128 |
global $app; |
|
129 |
|
|
130 |
parent::onAfterRun(); |
|
131 |
} |
21c641
|
132 |
|
FS |
133 |
} |
|
134 |
|
|
135 |
?> |