在cacti中查詢mysql,數據庫硬盤佔用量,php的script query

在cacti中使用 php腳本查詢mysql中數據庫磁盤佔用量

今天先寫了php的script,好久沒寫過邊查邊寫,痛苦啊。還好完成了歷史6個小時,希望以後可以加快。

cacti 使用的模板後面寫好了再放上來

<?php
/*
 * flashapp_mysql_space.php
 * -------------------------------------------------
 * enable cacti to read mysql database size
 * Originally by tongyuan at flashapp dot cn - 2013/12/24
 *
 * usage:
 * flashapp_mysql_space.php <index|num_nidex|get> db_host db_user db_password
 *
 * mysql user must have permissions to do this.
 * give myuser /show databases/ and /select/ permissions  .
 *
 * examle:
 * grant select,process,super,show databases on *.* \
 *   to monitor@'192.168.11.0/255.255.255.0' identified by 'monitor';
 *
 */
$debug=0;
if ($_SERVER["argc"] != 5 ){
        echo "Error. Wrong parameter count or paramenter wrong .\n";
        echo "Usage: " . $_SERVER["argv"][0] ;
        echo " index|num_index|get <dbhost> <dbuser> <dbpasswd>\n\n";
        exit(1);
}
$cmd        = $_SERVER["argv"][1];
$host       = $_SERVER["argv"][2];
$username   = $_SERVER["argv"][3];
$password   = $_SERVER["argv"][4];
//get all dabase name and count,expect system database.
if (@mysql_connect($host,$username,$password)) {
    $alldataBase = @mysql_query("show databases");
    $dataBaseNum = 0;
    $dataBase=array();
    while ($row=@mysql_fetch_array($alldataBase)) {
        if ( $row[0] != "information_schema"
                && $row[0] != "mysql"
                && $row[0] != "performance_schema"
                && $row[0] != "test" ) {
            $dataBase[]=$row[0];
            $dataBaseNum+=1;
        }
    }
    if ($debug) {echo "all dataBase Number is :";print_r($dataBaseNum);echo "\n";}
    if ($debug) {echo "all dataBase is :";print_r($dataBase);echo "\n";}
      
    // output index
    if ($cmd == 'index') { foreach ($dataBase as $d){echo $d."\n";} exit(0); }
    // output index_number
    if ($cmd == "num_index" ) { echo $dataBaseNum . "\n";}
      
    // get databses space
    if ($dataBaseNum == 0) {exit(0) ; }
    foreach ($dataBase as $row){
        $resault=@mysql_query("select sum(DATA_LENGTH)+sum(INDEX_LENGTH) \
                            from information_schema.tables  where table_schema='".$row."'");
          
        $space=@mysql_fetch_row($resault)[0];
        $dataBaseSpace[] = $space?$space:"0";
    }
    if ($debug) {echo "All dataBase space is :";print_r($dataBaseSpace);echo "\n";}
    // output database space
    if ($cmd == "get") {
        foreach ($dataBase as $key => $value) {
            echo $value . ":" .$dataBaseSpace[$key] . "\n";
        }
    }  
      
} else {
    die("Can't connected to Server!\n");
}
?>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章