2011年11月7日星期一

修改cacti脚本, 增加磁盘使用百分比

最近在部署cacti用来monitor各数据库服务器, 想要设置一个磁盘空间报警, 当分区满90的时候发alert mail, 但是看了现在已经设置的monitor disk space的data source, 里面并没有类似于used_percent项, 又懒得再去找其他的template, 变自己修改已经有的脚本来实现.

首先找到对应的data query:

我们知道了这个data query对应的xml文件, 于是找到这个xml文件, 编辑, 在field中增加一项:
<fields>
               <hrStorageIndex>
                       <name>Index</name>
                       <direction>input</direction>
                       <query_name>index</query_name>
               </hrStorageIndex>
......                
               <hrStorageUsedPercent>
                       <name>Used Percent</name>
                       <direction>output</direction>
                       <query_name>used_percent</query_name>
               </hrStorageUsedPercent>

从这个文件中我们也可以看出, 所用的php文件:
<script_path>|path_cacti|/scripts/ss_host_disk.php</script_path>

对应编辑这个php文件(不会php, 只能看着改了):
if ($cmd == "index") {
$return_arr = ss_host_disk_reindex(cacti_snmp_walk($hostname, $snmp_community, $oids["index"], $snmp_version, $snmp_auth_username, $snmp_auth_password, $snmp_auth_protocol, $snmp_priv_passphrase, $snmp_priv_protocol, $snmp_context, $snmp_port, $snmp_timeout, $ping_retries, $max_oids, SNMP_POLLER));

for ($i=0;($i<sizeof($return_arr));$i++) {
print $return_arr[$i] . "\n";
}

}elseif ($cmd == "num_indexes") {
$return_arr = ss_host_disk_reindex(cacti_snmp_walk($hostname, $snmp_community, $oids["index"], $snmp_version, $snmp_auth_username, $snmp_auth_password, $snmp_auth_protocol, $snmp_priv_passphrase, $snmp_priv_protocol, $snmp_context, $snmp_port, $snmp_timeout, $ping_retries, $max_oids, SNMP_POLLER));

print sizeof($return_arr);

}elseif ($cmd == "query") {
$arg = $arg1;

$arr_index = ss_host_disk_reindex(cacti_snmp_walk($hostname, $snmp_community, $oids["index"], $snmp_version, $snmp_auth_username, $snmp_auth_password, $snmp_auth_protocol, $snmp_priv_passphrase, $snmp_priv_protocol, $snmp_context, $snmp_port, $snmp_timeout, $ping_retries, $max_oids, SNMP_POLLER));
$arr = ss_host_disk_reindex(cacti_snmp_walk($hostname, $snmp_community, $oids[$arg], $snmp_version, $snmp_auth_username, $snmp_auth_password, $snmp_auth_protocol, $snmp_priv_passphrase, $snmp_priv_protocol, $snmp_context, $snmp_port, $snmp_timeout, $ping_retries, $max_oids, SNMP_POLLER));

for ($i=0;($i<sizeof($arr_index));$i++) {
print $arr_index[$i] . "!" . $arr[$i] . "\n";
}
}elseif ($cmd == "get") {
$arg = $arg1;
$index = $arg2;

if ($arg == "used_percent")
{
   $sau_used = preg_replace("/[^0-9]/i", "", db_fetch_cell("select field_value from host_snmp_cache where host_id=$host_id and field_name='hrStorageAllocationUnits' and snmp_index='$index'"));
$snmp_used = cacti_snmp_get($hostname, $snmp_community, $oids["used"] . ".$index", $snmp_version, $snmp_auth_username, $snmp_auth_password, $snmp_auth_protocol,$snmp_priv_passphrase,$snmp_priv_protocol, $snmp_context, $snmp_port, $snmp_timeout, $ping_retries, SNMP_POLLER);
if($snmp_data_used<0){
$used = (abs($snmp_used) + 2147483647) * $sau_used;
} else {
$used = $snmp_used * $sau_used;
}

$sau_total = preg_replace("/[^0-9]/i", "", db_fetch_cell("select field_value from host_snmp_cache where host_id=$host_id and field_name='hrStorageAllocationUnits' and snmp_index='$index'"));
$snmp_total = cacti_snmp_get($hostname, $snmp_community, $oids["total"] . ".$index", $snmp_version, $snmp_auth_username, $snmp_auth_password, $snmp_auth_protocol,$snmp_priv_passphrase,$snmp_priv_protocol, $snmp_context, $snmp_port, $snmp_timeout, $ping_retries, SNMP_POLLER);
if($snmp_total<0){
$total = (abs($snmp_total) + 2147483647) * $sau_total;
} else {
$total = $snmp_total * $sau_total;
}

return number_format($used * 100 / $total, 2);
}

if (($arg == "total") || ($arg == "used")) {
$sau = preg_replace("/[^0-9]/i", "", db_fetch_cell("select field_value from host_snmp_cache where host_id=$host_id and field_name='hrStorageAllocationUnits' and snmp_index='$index'"));
$snmp_data = cacti_snmp_get($hostname, $snmp_community, $oids[$arg] . ".$index", $snmp_version, $snmp_auth_username, $snmp_auth_password, $snmp_auth_protocol,$snmp_priv_passphrase,$snmp_priv_protocol, $snmp_context, $snmp_port, $snmp_timeout, $ping_retries, SNMP_POLLER);
if($snmp_data<0){
return (abs($snmp_data) + 2147483647) * $sau;
} else {
return $snmp_data * $sau;
}
}else{
return cacti_snmp_get($hostname, $snmp_community, $oids[$arg] . ".$index", $snmp_version, $snmp_auth_username, $snmp_auth_password, $snmp_auth_protocol,$snmp_priv_passphrase,$snmp_priv_protocol, $snmp_context, $snmp_port, $snmp_timeout, $ping_retries, SNMP_POLLER);
}
}

接下来就可以到cacti中操作了:
在data template中增加一个:

在data query中关联graph template以及 date template的地方:

然后修改graph template(其实应该没有必要这么加, 因为这个百分比相对于磁盘大小来说实在太小了, 在同一张图里面根本显示不出来, 只是我现在没研究透, 就全加了):

然后我们就可以添加threshold了.

没有评论: