root / plugins / mysql / mysql-table-size @ 17f78427
Historique | Voir | Annoter | Télécharger (1,84 ko)
| 1 |
#!/usr/local/bin/php |
|---|---|
| 2 |
<?php |
| 3 |
/* |
| 4 |
* Munin plugin mysql_table_size |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
$dbname = getenv('dbname');
|
| 9 |
$tables = getenv('tables');
|
| 10 |
$dsn = getenv('dsn');
|
| 11 |
$username = getenv('mysqluser');
|
| 12 |
$password = getenv('mysqlpassword');
|
| 13 |
$table_list = explode(' ',$tables);
|
| 14 |
$text = ""; |
| 15 |
//print_r($table_list); |
| 16 |
|
| 17 |
if($argc > 1 && $argv[1] == 'authconf') {
|
| 18 |
exit('yes');
|
| 19 |
} |
| 20 |
|
| 21 |
foreach ($table_list as $_str_schema_table) {
|
| 22 |
$_ary_schema_table = explode('.',$_str_schema_table);
|
| 23 |
$schema = $_ary_schema_table[0]; |
| 24 |
$table = $_ary_schema_table[1]; |
| 25 |
$text .= <<<EOT |
| 26 |
$table.label $schema.$table\n |
| 27 |
EOT; |
| 28 |
} |
| 29 |
|
| 30 |
if($argc > 1 && $argv[1] == 'config') {
|
| 31 |
$text = <<<EOT |
| 32 |
graph_title $dbname Table Data Size |
| 33 |
graph_category db |
| 34 |
graph_vlabel Size |
| 35 |
graph_args --base 1024 -l 0 |
| 36 |
$text |
| 37 |
EOT; |
| 38 |
die($text); |
| 39 |
} |
| 40 |
|
| 41 |
$dbh = new PDO($dsn,$username,$password); |
| 42 |
$mysql = new MysqlTblSize($dbh); |
| 43 |
|
| 44 |
foreach ($table_list as $_str_schema_table) {
|
| 45 |
$_ary_schema_table = explode('.',$_str_schema_table);
|
| 46 |
$schema = $_ary_schema_table[0]; |
| 47 |
$table = $_ary_schema_table[1]; |
| 48 |
|
| 49 |
$row = $mysql->getTableSize($schema,$table); |
| 50 |
$data_length = $row['data_length']; |
| 51 |
echo "$table.value $data_length\n"; |
| 52 |
} |
| 53 |
|
| 54 |
class MysqlTblSize {
|
| 55 |
|
| 56 |
private $dbh; |
| 57 |
|
| 58 |
public function __construct($dbh) {
|
| 59 |
$this->dbh = $dbh; |
| 60 |
} |
| 61 |
|
| 62 |
public function getTableSize($schema,$table) {
|
| 63 |
$bind = array($schema,$table); |
| 64 |
$sql = <<<EOT |
| 65 |
select data_length |
| 66 |
from information_schema.tables |
| 67 |
where table_schema = ? and table_name = ? |
| 68 |
EOT; |
| 69 |
$stmt = $this->dbh->prepare($sql); |
| 70 |
$stmt->execute($bind); |
| 71 |
return $row = $stmt->fetch(PDO::FETCH_ASSOC); |
| 72 |
} |
| 73 |
|
| 74 |
public function getSchemaSize($schema) {
|
| 75 |
$bind = array($schema); |
| 76 |
$sql = <<<EOT |
| 77 |
select sum( data_length ) sum_data_length |
| 78 |
from information_schema.tables |
| 79 |
where table_schema = ? |
| 80 |
EOT; |
| 81 |
$stmt = $this->dbh->prepare($sql); |
| 82 |
$stmt->execute($bind); |
| 83 |
return $row = $stmt->fetch(PDO::FETCH_ASSOC); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
} |
