Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / mysql / mysql-schema-size @ 17f78427

Historique | Voir | Annoter | Télécharger (1,47 ko)

1
#!/usr/local/bin/php
2
<?php
3
/*
4
 * Munin plugin mysql_schema_size
5
 */
6

    
7
$dbname   = getenv('dbname');
8
$dsn      = getenv('dsn');
9
$username = getenv('mysqluser');
10
$password = getenv('mysqlpassword');
11
$text = "";
12

    
13
if($argc > 1 && $argv[1] == 'authconf') {
14
  exit('yes');
15
}
16

    
17
$dbh = new PDO($dsn,$username,$password);
18
$mysql = new MysqlTblSize($dbh);
19

    
20
$stmt = $mysql->getSchemaList();
21
$schema_list = array();
22
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
23
  $schema = $row['table_schema'];
24
  $text .= "$schema.label $schema\n";
25
  $schema_list[] = $schema;
26
}
27

    
28
if($argc > 1 && $argv[1] == 'config') {
29
  $text = <<<EOT
30
graph_title $dbname Schema Size
31
graph_category db
32
graph_vlabel Size
33
graph_args --base 1024 -l 0
34
$text
35
EOT;
36
  die($text);
37
}
38

    
39
$stmt = $mysql->getSchemaSize();
40
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
41
  $data_length = $row['sum_data_length'];
42
  $schema      = $row['table_schema'];
43
  echo "$schema.value $data_length\n";
44
}
45

    
46
class MysqlTblSize {
47

    
48
  private $dbh;
49

    
50
  public function __construct($dbh) {
51
    $this->dbh = $dbh;
52
  }
53

    
54
  public function getSchemaList() {
55
    $sql = <<<EOT
56
select distinct(table_schema)
57
from information_schema.tables
58
EOT;
59
    $stmt = $this->dbh->prepare($sql);
60
    $stmt->execute();
61
    return $stmt;
62
  }
63

    
64
  public function getSchemaSize() {
65
    $sql = <<<EOT
66
select sum( data_length ) sum_data_length,table_schema
67
from information_schema.tables
68
group by table_schema
69
EOT;
70
    $stmt = $this->dbh->prepare($sql);
71
    $stmt->execute();
72
    return $stmt;
73
  }
74

    
75

    
76
}