在Cacti中查询mysql,数据库硬盘占用量,php的scr

在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");
}
?>

Cacti 的详细介绍请点这里
Cacti 的下载地址请点这里

相关阅读

RHEL6.4环境Cacti中spine安装

RHEL6.4中使用Cacti+Spine监控主机实现发送邮件报警

CentOS 5.5完整安装Cacti+Spine

CentOS 6下Cacti搭建文档

RHEL5.9下Cacti监控部署详解

CentOS 6.3 下Cacti 安装详解

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/73bff9f1cecb6dc7e1cbb7fd30c44bd6.html