cacti 中查询mysql数据库占用磁盘大小并返回的php修改了,

但在cacti中配置了模板,可以在device中创建表格并且可以生成data source的条目,但始终没有返回数据 不知道是什么问题,暂时没有办法解决。

尝试过用script query 和script_server的方式,由于还是不了解也不知道是对cacti的了解不够还是什么原因 始终没有返回值


哪位大神给指点下


贴这里记录下,因为之前通过snmpwalk的方式进行制作模板已经成功,没办法用自定义mib的方式进行实现吧,后面再分享

data query中引用的xml文件


<interface>
    <name>get mysql databases</name>
    <script_path>|path_php_binary| -q |path_cacti|/scripts/flashapp_mysql_space.php</script_path>
    <arg_prepend>|host_hostname|</arg_prepend>
    <arg_index>index</arg_index>
    <arg_query>query</arg_query>
    <arg_get>get</arg_get>
    <arg_num_indexes>num_index</arg_num_indexes>
    <output_delimeter>:</output_delimeter>
    <index_order>hrDataBasesIndex</index_order>
    <index_order_type>numeric</index_order_type>
    <index_title_format>|chosen_order_field|</index_title_format>
    <fields>
        <hrDataBasesIndex>
            <name>DataBasesIndex</name>
            <direction>input</direction>
            <query_name>index</query_name>
        </hrDataBasesIndex>
        <hrDataBasesDescr>
            <name>Description</name>
            <direction>input</direction>
            <query_name>desc</query_name>
        </hrDataBasesDescr>
        <hrDataBasesSize>
            <name>Total Size</name>
            <direction>output</direction>
            <query_name>space</query_name>
        </hrDataBasesSize>
                                                                                                     
    </fields>
</interface>



对应的php查询文件也做了修改调整了 参数 和输出方法

<?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 db_host  <index|num_nidex>
 * flashapp_mysql_spqce.php db_host query index|desc
 * flashapp_mysql_spqce.php db_host get space database_name
 *
 * 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 " <dbhost> <dbuser> <dbpasswd> <cmd>\n\n";
//         echo "cmd : index|num_index|get \n\n\n";
//         exit(1);
// }
$host       = $_SERVER["argv"][1];
//default user and password is monitor
$username   = "monitor";
$password   = "monitor";
//get all dabase name and count,expect system database.
if ($_SERVER["argc"] >= 3  ) {
                                                                         
    $cmd=$_SERVER["argv"][2];
    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";}
                                                                             
    if ($cmd == "query" || $cmd =="get" ) {
        $arg=$_SERVER["argv"][3];
        // 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";
        }
/*DEBUG*/if ($debug) {echo "All dataBase space is :";print_r($dataBaseSpace);echo "\n";}
        // output database space
        if ($cmd == "query" && ($arg == "index" || $arg == "desc")) {
            foreach ($dataBase as $key => $value) {
                echo $value . ":" .$value . "\n";
            }
            exit(0);
        }
                                                                             
        if ($cmd == "get" && $arg == "space") {
            $get_database=$_SERVER["argv"][4];
        foreach ($dataBase as $key => $value) {
            if ($value == $get_database){
                echo $dataBaseSpace[$key];
            }
        }
        }
    } 
    } else {
        die("Can't connected to Server!\n");
    }
}
?>