共计 2675 个字符,预计需要花费 7 分钟才能阅读完成。
在 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 安装 http://www.linuxidc.com/Linux/2013-11/92797.htm
RHEL6.4 中使用 Cacti+Spine 监控主机实现发送邮件报警 http://www.linuxidc.com/Linux/2013-11/92795.htm
CentOS 5.5 完整安装 Cacti+Spine http://www.linuxidc.com/Linux/2011-12/49701.htm
CentOS 6 下 Cacti 搭建文档 http://www.linuxidc.com/Linux/2013-06/86595.htm
RHEL5.9 下 Cacti 监控部署详解 http://www.linuxidc.com/Linux/2013-06/85427.htm
CentOS 6.3 下 Cacti 安装详解 http://www.linuxidc.com/Linux/2013-05/84279.htm
CentOS Linux 下快速安装配置 Cacti 中文版 http://www.linuxidc.com/Linux/2013-03/81627.htm