cacti命令行进入mysql_在Cacti中查询mysql,数据库硬盘占用量,php的script query

在cacti中使用 php脚本查询MySQL中数据库磁盘占用量

今天先写了php的script,好久没写过边查边写,痛苦啊。还好完成了历史6个小时,希望以后可以加快。

cacti 使用的模板后面写好了再放上来

/*

* 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 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 \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 的下载地址:请点这里

相关阅读:

0b1331709591d260c1c78e86d0c51c18.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值