项目需要,监控mysql占用磁盘量,并在cacti上显示出来。
由于不是mysql里每个database都要监控,并且数量和名称也不是固定的,所以就不能像snmp或者使用script/command的方式获取固定的内容了。的使用到index,并让用户来选择。
ps:前面写了几篇,比较凌乱,而且也有错误的地方,毕竟写了那么多字也懒的删了。
再写一篇吧
本次详细写了一个cacti能使用的script query的制作方法,语言采用php。cacti的web页面必须使用mysql,so使用php不存在还要安装依赖或者库什么的问题。
方法1:在数据库所在机,写个脚本或者程序,查看mysql的存储目录,并上传到cacti上
分析1:由于cacti是采用轮训的机制,与cacti接口比较麻烦,放弃。
方法2:在mysql机器上定制一个mib文件来扩展snmp的内容,使mysql的信息能够在cacti上使用snmp查询的方式来获取到。
分析2:定制mib,就必须写一个守护程序来定时更新snmp里的数据,如果写不好安全性和稳定性有问题。再一个做运维嘛小东西写写,服务还是由开发来写吧 :)【这种方法最靠谱,但难度大】
方法3:使用script在cacti上直接获取mysql的信息
分析3:效率低,可能有网络异常。需要开放监控账户的权限,由于本次项目mysql全部是监听内网网口,安全性没问题,在同一个局域网,网络问题不大,再一个开发快。【采用此方式】
一、数据采集方法
使用mysql的内部语句来统计数据库的大小,可能与磁盘占用量有差别,但也基本显示出了磁盘占用情况。
使用的查询语句:
select sum(DATA_LENGTH)+sum(INDEX_LENGTH) from information_schema.tables where table_schema='数据库名';
另外账户还必须有show databases;的权限
二、采集过程
1、首先查询出mysql需要统计的数据库,并列出相关信息(就是数据库的描述了)
2、选择需要统计的数据库,使用模板创建图形,和轮训获取数据并保存
3、在需要的时候来看过往的数据库大小的变化了
三、实现过程
完成这个任务,需要2个文件:
描述功能和方法的xml文件;
实现具体功能php文件;
先创建一个xml来让cacti知道如何获取mysql里的数据库名(由于数据库没有索引值,这里索引和描述都为数据库名)
文件名:flashapp_mysql_space.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> <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>
xml中详细描述了,如何获取索引、如何查询索引对应的相关信息、以及如何获取具体的数据
<fields> ..... </fields> 这部分,描述了使用给cacti输入和保存的数据的内容,有个类型input和output,input是在device添加Data Query后创建图表时出现【选择列表】时出现的信息,outpu为创建图表后rrd文件保存的数据。
其他部分:
<name> | 不解释 |
<script_path> | 也不解释了吧,其中|path_php_binary| 等为cacti的变量,大家可以看手册 |
<arg_prepend> | 为flashapp_mysql_space.php 后面跟的参数多个用空格空开,我们这里主要主机的ip就可以了 |
<arg_index> | 获取索引的关键字,这里也就是指使用 flashapp_mysql_space.php |host_hostname| index 来获取索引,如果你不是index,是list或者什么的自己改吧(别和系统的冲突就好) |
<arg_query> | 何获取其他信息使用的关键字,比如每个表格的描述信息,其实也就是下面fields中input信息的获取方法实际命令为: flashapp_mysql_space.php |host_hostname| query <类型是input的query_name> |
<arg_get> | 获取output使用的关键字,实际命令 flashapp_mysql_space.php |host_hostname| get <query_name> <index> 注意输出不能有换行符 |
<arg_num_indexes> | 索引数量,也可以没有,cacti会自己统计 |
<output_delimeter> | 在使用query时,索引和数据之间的分隔符 |
<index_order> | 索引排序依据,可使用fields里面的input类型的名字,也可以是多个,用冒号【:】分隔. |
<index_order_type> | 排序方法 numeric:1,2,3,10,20,31 alphabetic:1,10,2,20,3,31 |
fields->name | |
fields->direction | input output |
fields->query_name | script 查询此项时使用的关键字 如本例的hrDataBasesIndex,是个input,query_name为desc,所以命令为: flashapp_mysql_space.php |host_hostname| query desc 如果是hrDataBasesSize 那就是: flashapp_mysql_space.php |host_hostname| get space <索引> |
ok,到这步应该都知道怎么写你自己的xml文件了吧,对了<interface>,这个名字随便起,一般用<query></query>
接下来该写php文件来获取数据了
先用命令行方式来看下执行结果,程序见最后
获取索引
[root@localhost ~]# php flashapp_mysql_space.php 192.168.1.84 index
cacti
cacti1
cacti2
test1
[root@localhost ~]#
获取索引数
[root@localhost ~]# php flashapp_mysql_space.php 192.168.1.84 num_index 4 [root@localhost ~]#
获取选择列表的内容(由于数据库木有索引,所以索引和描述相同)
[root@localhost ~]# php flashapp_mysql_space.php 192.168.1.84 query index cacti:cacti cacti1:cacti1 cacti2:cacti1 test1:test1 [root@localhost ~]# php flashapp_mysql_space.php 192.168.1.84 query desc cacti:cacti cacti1:cacti1 cacti2:cacti1 test1:test1 [root@localhost ~]#
获取数据库大小
[root@localhost ~]# php flashapp_mysql_space.php 192.168.1.84 get space cacti 664589[root@localhost ~]# [root@localhost ~]# php flashapp_mysql_space.php 192.168.1.84 get space cacti1 804581[root@localhost ~]#
今天太晚,睡觉明天继续写如何配置到cacti里
文件:flashapp_mysql_space.php
<?php
/*
* flashapp_mysql_space.php
* -------------------------------------------------
* enable cacti to read mysql database size
* Originally by tongyuan at flashapp dot cn - 2013/12/25
*
*
* 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 mysqluser /show databases/ and /select/ permissions .
*
* examle:
* grant select,show databases on *.* \
* to monitor@'192.168.1.0/255.255.255.0' identified by 'monitor';
*
*/
# all of host use the same account and password
# you can change it by youself.
$username = "monitor";
$password = "monitor";
# enable or disable debug mode
# use debug mode in cli
# 0 - disable
# 1 - enable
$debug=0;
# this is cacti's header ,not use for this script.
# i am not sure for that.
# deactivate http headers
$no_http_headers = true;
# include some cacti files for ease of use
include(dirname(__FILE__) . "/../include/global.php");
include(dirname(__FILE__) . "/../lib/snmp.php");
$host = $_SERVER["argv"][1];
//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;
}
}
/*DEBUG*/if ($debug) {echo "all dataBase Number is :";print_r($dataBaseNum);echo "\n";}
/*DEBUG*/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"; exit(0); }
if ($cmd == "query" || $cmd =="get" ) {
$arg=$_SERVER["argv"][3];
if ($dataBaseNum == 0) {exit(0);}
// 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") {
$dataBaseSpace=array();
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"; //return NULL if database is empty,so change to zero.
}
/*DEBUG*/if ($debug) {echo "All dataBase space is :";print_r($dataBaseSpace);echo "\n";}
$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");
}
}
?>
转载于:https://blog.51cto.com/axe999/1344881