Skip to content

Commit e5685e7

Browse files
author
Marius Cramer
committed
Merge branch 'master' of /home/git/repositories/florian030/ispconfig3
2 parents bbc6570 + 21c641c commit e5685e7

File tree

3 files changed

+153
-2
lines changed

3 files changed

+153
-2
lines changed

interface/lib/classes/functions.inc.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,13 @@ public function intval($string, $force_numeric = false) {
276276
return intval($string);
277277
}
278278
}
279-
279+
280+
/**
281+
* Function to change bytes to kB, MB, GB or TB
282+
* @param int $size - size in bytes
283+
* @param int precicion - after-comma-numbers (default: 2)
284+
* @return string - formated bytes
285+
*/
280286
public function formatBytes($size, $precision = 2) {
281287
$base=log($size)/log(1024);
282288
$suffixes=array('','k','M','G','T');
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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+
?>

server/lib/classes/db_mysql.inc.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,31 @@ public function diffrec($record_old, $record_new) {
265265

266266
}
267267

268+
/**
269+
* Function to get the database-size
270+
* @param string $database_name
271+
* @return int - database-size in bytes
272+
*/
273+
public function getDatabaseSize($database_name) {
274+
global $app;
275+
include('lib/mysql_clientdb.conf');
276+
/* Connect to the database */
277+
$link = mysql_connect($clientdb_host, $clientdb_user, $clientdb_password);
278+
if (!$link) {
279+
$app->log('Unable to connect to the database'.mysql_error($link),LOGLEVEL_DEBUG);
280+
return;
281+
}
282+
/* Get database-size from information_schema */
283+
$result=mysql_query("SELECT SUM(data_length+index_length) FROM information_schema.TABLES WHERE table_schema='".$database_name."';",$link);
284+
$this->close;
285+
if (!$result) {
286+
$app->log('Unable to get the database-size'.mysql_error($link),LOGLEVEL_DEBUG);
287+
return;
288+
}
289+
$database_size = mysql_fetch_row($result);
290+
return $database_size[0];
291+
}
292+
268293
//** Function to fill the datalog with a full differential record.
269294
public function datalogSave($db_table, $action, $primary_field, $primary_id, $record_old, $record_new, $force_update = false) {
270295
global $app,$conf;
@@ -645,4 +670,4 @@ public function mapType($metaType,$typeValue) {
645670

646671
}
647672

648-
?>
673+
?>

0 commit comments

Comments
 (0)