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