Code: Select all
<interface>
<name>Get Interface Traffic Information</name>
<script_path>|path_php_binary| -q |path_cacti|/scripts/query_interface_traffic-MIO.php</script_path>
<arg_prepend>|host_hostname| |host_snmp_community| |host_snmp_version| |host_snmp_port| |host_snmp_timeout| |host_max_oids| "|host_snmp_username|" "|host_snmp_password|" "|host_snmp_auth_protocol|" "|host_snmp_priv_passphrase|" "|query_ifIP|" "|host_snmp_priv_protocol|" "|host_snmp_context|"</arg_prepend>
<arg_index>index</arg_index>
<arg_query>query</arg_query>
<arg_get>get</arg_get>
<output_delimeter>!</output_delimeter>
<index_order>ifIndex</index_order>
<index_order_type>numeric</index_order_type>
<index_title_format>|chosen_order_field|</index_title_format>
<!-- Valores que se pasan después del query-->
<fields>
<ifIndex>
<name>Index</name>
<direction>input</direction>
<query_name>index</query_name>
</ifIndex>
<ifIP>
<name>IP Address</name>
<method>walk</method>
<source>OID/REGEXP:^\.?1\.3\.6\.1\.2\.1\.4\.20\.1\.2\.([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}).*</source>
<direction>input</direction>
<oid>.1.3.6.1.2.1.4.20.1.2</oid>
<query_name>ip</query_name>
</ifIP>
<ifstatus>
<name>Status</name>
<direction>input</direction>
<query_name>ifstatus</query_name>
</ifstatus>
<ifdescription>
<name>Description</name>
<direction>input</direction>
<query_name>ifdescription</query_name>
</ifdescription>
<ifname>
<name>Name</name>
<direction>input</direction>
<query_name>ifname</query_name>
</ifname>
<ifalias>
<name>Alias</name>
<direction>input</direction>
<query_name>ifalias</query_name>
</ifalias>
<iftype>
<name>Type</name>
<direction>input</direction>
<query_name>iftype</query_name>
</iftype>
<ifspeed>
<name>Speed</name>
<direction>input</direction>
<query_name>ifspeed</query_name>
</ifspeed>
<ifHWaddress>
<name>HWaddress</name>
<direction>input</direction>
<query_name>ifHWaddress</query_name>
</ifHWaddress>
<ifInOctets>
<name>InOctets</name>
<direction>input</direction>
<query_name>ifInOctets</query_name>
</ifInOctets>
<ifOutOctets>
<name>OutOctets</name>
<direction>input</direction>
<query_name>ifOutOctets</query_name>
</ifOutOctets>
<ifInUcastPkts>
<name>UtilizationIn</name>
<direction>output</direction>
<query_name>ifUtilizationIn</query_name>
</ifInUcastPkts>
<ifOutUcastPkts>
<name>UtilizationOut</name>
<direction>output</direction>
<query_name>ifUtilizationOut</query_name>
</ifOutUcastPkts>
<ifInErrors>
<name>InErrors</name>
<direction>output</direction>
<query_name>ifErrorsIn</query_name>
</ifInErrors>
<ifOutErrors>
<name>OutErrors</name>
<direction>output</direction>
<query_name>ifErrorsOut</query_name>
</ifOutErrors>
<ifVlanTrunkPortDynamicStatus>
<name>vlanTrunkPortDynamicStatus</name>
<direction>input</direction>
<query_name>ifVlanTrunkPortDynamicStatus</query_name>
</ifVlanTrunkPortDynamicStatus>
<ifInUnknownProtos>
<name>Saturation</name>
<direction>output</direction>
<query_name>ifSaturation</query_name>
</ifInUnknownProtos>
</fields>
</interface>
Code: Select all
<?php
/* do NOT run this script through a web browser */
if (!isset($_SERVER["argv"][0]) || isset($_SERVER['REQUEST_METHOD']) || isset($_SERVER['REMOTE_ADDR'])) {
die("<br><strong>This script is only meant to run at the command line.</strong>");
}
# 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");
# define all OIDs we need for further processing
$oids = array(
"index" => ".1.3.6.1.2.1.2.2.1.1",
"ifstatus" => ".1.3.6.1.2.1.2.2.1.8",
"ifdescription" => ".1.3.6.1.2.1.2.2.1.2",
"ifname" => ".1.3.6.1.2.1.31.1.1.1.1",
"ifalias" => ".1.3.6.1.2.1.31.1.1.1.18",
"iftype" => ".1.3.6.1.2.1.2.2.1.3",
"ifspeed" => ".1.3.6.1.2.1.2.2.1.5",
"ifHWaddress" => ".1.3.6.1.2.1.2.2.1.6",
"ifInOctets" => ".1.3.6.1.2.1.2.2.1.10",
"ifOutOctets" => ".1.3.6.1.2.1.2.2.1.16",
"ifInErrors" => ".1.3.6.1.2.1.2.2.1.14",
"ifOutErrors" => ".1.3.6.1.2.1.2.2.1.20",
"ifVlanTrunkPortDynamicStatus" => ".1.3.6.1.4.1.9.9.46.1.6.1.1.14",
"ifVlanTrunkPortDynamicStatus" => ".1.3.6.1.2.1.4.20.1.2",
);
$xml_delimiter = "!";
# all required input parms
$hostname = $_SERVER["argv"][1];
$snmp_community = $_SERVER["argv"][2];
$snmp_version = $_SERVER["argv"][3];
$snmp_port = $_SERVER["argv"][4];
$snmp_timeout = $_SERVER["argv"][5];
$max_oids = $_SERVER["argv"][6];
# required for SNMP V3
$snmp_auth_username = $_SERVER["argv"][7];
$snmp_auth_password = $_SERVER["argv"][8];
$snmp_auth_protocol = $_SERVER["argv"][9];
$snmp_priv_passphrase = $_SERVER["argv"][10];
$snmp_priv_protocol = $_SERVER["argv"][11];
$snmp_context = $_SERVER["argv"][12];
$cmd = $_SERVER["argv"][13];
if (isset($_SERVER["argv"][14])) { $query_field = $_SERVER["argv"][14]; };
if (isset($_SERVER["argv"][15])) { $query_index = $_SERVER["argv"][15]; };
# get number of snmp retries from global settings
$snmp_retries = read_config_option("snmp_retries");
# -------------------------------------------------------------------------
# main code starts here
# -------------------------------------------------------------------------
# -------------------------------------------------------------------------
# script MUST respond to index queries
# the command for this is defined within the XML file as
# <arg_index>index</arg_index>
# you may replace the string "index" both in the XML and here
# -------------------------------------------------------------------------
# php -q <script> <parms> index
# will list all indices of the target values
# e.g. in case of interfaces
# it has to respond with the list of interface indices
# -------------------------------------------------------------------------
if ($cmd == "index") {
# retrieve all indices from target
$return_arr = 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, $snmp_retries, $max_oids, SNMP_POLLER));
# and print each index as a separate line
for ($i=0;($i<sizeof($return_arr));$i++) {
print $return_arr[$i] . "\n";
}
# -------------------------------------------------------------------------
# script MUST respond to query requests
# the command for this is defined within the XML file as
# <arg_query>query</arg_query>
# you may replace the string "query" both in the XML and here
# -------------------------------------------------------------------------
# php -q <script> <parms> query <function>
# where <function> is a parameter that tells this script,
# which target value should be retrieved
# e.g. in case of interfaces, <function> = ifdescription
# it has to respond with the list of
# interface indices along with the description of the interface
# -------------------------------------------------------------------------
}elseif ($cmd == "query" && isset($query_field)) {
//$f = fopen("/tmp/file.txt", "w");
//fwrite($f, "$cmd . $query_field ");
//fclose($f);
if ($query_field == "ifUtilizationIn"){
$arr_index = 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, $snmp_retries, $max_oids, SNMP_POLLER));
$arr1 = reindex(cacti_snmp_walk($hostname, $snmp_community,
$oids["ifInOctets"], $snmp_version, $snmp_auth_username,
$snmp_auth_password, $snmp_auth_protocol, $snmp_priv_passphrase, $snmp_priv_protocol,
$snmp_context, $snmp_port, $snmp_timeout, $snmp_retries, $max_oids, SNMP_POLLER));
$arr2 = reindex(cacti_snmp_walk($hostname, $snmp_community,
$oids["ifspeed"], $snmp_version, $snmp_auth_username,
$snmp_auth_password, $snmp_auth_protocol, $snmp_priv_passphrase, $snmp_priv_protocol,
$snmp_context, $snmp_port, $snmp_timeout, $snmp_retries, $max_oids, SNMP_POLLER));
for ($i=0;($i<sizeof($arr_index));$i++) {
$arr = ($arr1[$i]/$arr2[$i]) * 100;
print $arr_index[$i] . $xml_delimiter . round($arr,0) . "\n";
}
} elseif ($query_field == "ifUtilizationOut") {
$arr_index = 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, $snmp_retries, $max_oids, SNMP_POLLER));
$arr1 = reindex(cacti_snmp_walk($hostname, $snmp_community,
$oids["ifOutOctets"], $snmp_version, $snmp_auth_username,
$snmp_auth_password, $snmp_auth_protocol, $snmp_priv_passphrase, $snmp_priv_protocol,
$snmp_context, $snmp_port, $snmp_timeout, $snmp_retries, $max_oids, SNMP_POLLER));
$arr2 = reindex(cacti_snmp_walk($hostname, $snmp_community,
$oids["ifspeed"], $snmp_version, $snmp_auth_username,
$snmp_auth_password, $snmp_auth_protocol, $snmp_priv_passphrase, $snmp_priv_protocol,
$snmp_context, $snmp_port, $snmp_timeout, $snmp_retries, $max_oids, SNMP_POLLER));
for ($i=0;($i<sizeof($arr_index));$i++) {
$arr = ($arr1[$i]/$arr2[$i]) * 100;
print $arr_index[$i] . $xml_delimiter . round($arr,0) . "\n";
}
} elseif ($query_field == "ifErrorsIn") {
$arr_index = 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, $snmp_retries, $max_oids, SNMP_POLLER));
$arr1 = reindex(cacti_snmp_walk($hostname, $snmp_community,
$oids["ifInErrors"], $snmp_version, $snmp_auth_username,
$snmp_auth_password, $snmp_auth_protocol, $snmp_priv_passphrase, $snmp_priv_protocol,
$snmp_context, $snmp_port, $snmp_timeout, $snmp_retries, $max_oids, SNMP_POLLER));
$arr2 = reindex(cacti_snmp_walk($hostname, $snmp_community,
$oids["ifInOctets"], $snmp_version, $snmp_auth_username,
$snmp_auth_password, $snmp_auth_protocol, $snmp_priv_passphrase, $snmp_priv_protocol,
$snmp_context, $snmp_port, $snmp_timeout, $snmp_retries, $max_oids, SNMP_POLLER));
for ($i=0;($i<sizeof($arr_index));$i++) {
$arr = ($arr1[$i]/$arr2[$i]) * 100;
print $arr_index[$i] . $xml_delimiter . round($arr,0) . "\n";
}
} elseif ($query_field == "ifErrorsOut") {
$arr_index = 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, $snmp_retries, $max_oids, SNMP_POLLER));
$arr1 = reindex(cacti_snmp_walk($hostname, $snmp_community,
$oids["ifOutErrors"], $snmp_version, $snmp_auth_username,
$snmp_auth_password, $snmp_auth_protocol, $snmp_priv_passphrase, $snmp_priv_protocol,
$snmp_context, $snmp_port, $snmp_timeout, $snmp_retries, $max_oids, SNMP_POLLER));
$arr2 = reindex(cacti_snmp_walk($hostname, $snmp_community,
$oids["ifOutOctets"], $snmp_version, $snmp_auth_username,
$snmp_auth_password, $snmp_auth_protocol, $snmp_priv_passphrase, $snmp_priv_protocol,
$snmp_context, $snmp_port, $snmp_timeout, $snmp_retries, $max_oids, SNMP_POLLER));
for ($i=0;($i<sizeof($arr_index));$i++) {
$arr = ($arr1[$i]/$arr2[$i]) * 100;
print $arr_index[$i] . $xml_delimiter . round($arr,0) . "\n";
}
} elseif ($query_field == "ifSaturation") {
$arr_index = 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, $snmp_retries, $max_oids, SNMP_POLLER));
$arr1 = reindex(cacti_snmp_walk($hostname, $snmp_community,
$oids["ifOutErrors"], $snmp_version, $snmp_auth_username,
$snmp_auth_password, $snmp_auth_protocol, $snmp_priv_passphrase, $snmp_priv_protocol,
$snmp_context, $snmp_port, $snmp_timeout, $snmp_retries, $max_oids, SNMP_POLLER));
$arr2 = reindex(cacti_snmp_walk($hostname, $snmp_community,
$oids["ifOutOctets"], $snmp_version, $snmp_auth_username,
$snmp_auth_password, $snmp_auth_protocol, $snmp_priv_passphrase, $snmp_priv_protocol,
$snmp_context, $snmp_port, $snmp_timeout, $snmp_retries, $max_oids, SNMP_POLLER));
for ($i=0;($i<sizeof($arr_index));$i++) {
$arr = ($arr1[$i]/$arr2[$i]) * 100;
print $arr_index[$i] . $xml_delimiter . round($arr,0) . "\n";
}
} else {
$arr_index = 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, $snmp_retries, $max_oids, SNMP_POLLER));
$arr = reindex(cacti_snmp_walk($hostname, $snmp_community,
$oids[$query_field], $snmp_version, $snmp_auth_username,
$snmp_auth_password, $snmp_auth_protocol, $snmp_priv_passphrase, $snmp_priv_protocol,
$snmp_context, $snmp_port, $snmp_timeout, $snmp_retries, $max_oids, SNMP_POLLER));
for ($i=0;($i<sizeof($arr_index));$i++) {
print $arr_index[$i] . $xml_delimiter . $arr[$i] . "\n";
}
}
# -------------------------------------------------------------------------
# script MUST respond to get requests
# the command for this is defined within the XML file as
# <arg_get>get</arg_get>
# you may replace the string "get" both in the XML and here
# -------------------------------------------------------------------------
# php -q <script> <parms> get <function> <index>
# where <function> is a parameter that tells this script,
# which target value should be retrieved
# and <index> is the index that should be queried
# e.g. in case of interfaces, <function> = ifdescription
# <index> = 1
# it has to respond with
# the description of the interface for interface #1
# -------------------------------------------------------------------------
}elseif ($cmd == "get" && isset($query_field) && isset($query_index)) {
//Obtenemos veocidad (comun para todos los casos)
$Speed = (cacti_snmp_get($hostname, $snmp_community,
$oids["ifspeed"] . ".$query_index", $snmp_version, $snmp_auth_username,
$snmp_auth_password, $snmp_auth_protocol, $snmp_priv_passphrase, $snmp_priv_protocol,
$snmp_context, $snmp_port, $snmp_timeout, $snmp_retries, $max_oids, SNMP_POLLER));
$InOct = (cacti_snmp_get($hostname, $snmp_community,
$oids["ifInOctets"] . ".$query_index", $snmp_version, $snmp_auth_username,
$snmp_auth_password, $snmp_auth_protocol, $snmp_priv_passphrase, $snmp_priv_protocol,
$snmp_context, $snmp_port, $snmp_timeout, $snmp_retries, $max_oids, SNMP_POLLER));
$OutOct = (cacti_snmp_get($hostname, $snmp_community,
$oids["ifOutOctets"] . ".$query_index", $snmp_version, $snmp_auth_username,
$snmp_auth_password, $snmp_auth_protocol, $snmp_priv_passphrase, $snmp_priv_protocol,
$snmp_context, $snmp_port, $snmp_timeout, $snmp_retries, $max_oids, SNMP_POLLER));
$InErrors = rand(5000000,7000000);
$OutErrors = rand(7000000,9000000);
//Obtenemos lo 4 valores para calculos
$tmpPath="/var/www/html/cacti/tmp/".$hostname."_".$query_index;
if ($query_field == "ifUtilizationIn"){
$fileOctIn = $tmpPath."_OctIn.tmp";
if (file_exists($fileOctIn)) {
$fpOctIn = fopen($fileOctIn, "r");
$LastOctIn = fgets($fpOctIn);
fclose($fpOctIn);
} else $LastOctIn = 0;
if($Speed>0){
$UtilizationIn = (($InOct-$LastOctIn)/$Speed) * 100;
}else{
$UtilizationIn =0;
}
print "$UtilizationIn";
if ($UtilizationIn >= 75) {
$saturation1 = 1;
} else {
$saturation1 = 0;
}
$fpOctIn = fopen($tmpPath."_OctIn.tmp", "w");
fwrite($fpOctIn, $InOct);
fclose($fpOctIn);
$fpUtilIn = fopen($tmpPath."_UtilizationIn.tmp", "w");
fwrite($fpUtilIn, $saturation1);
fclose($fpUtilIn);
} elseif ($query_field == "ifUtilizationOut") {
$fileOctOut = $tmpPath."_OctOut.tmp";
if (file_exists($fileOctOut)) {
$fpOctOut = fopen($fileOctOut, "r");
$LastOctOut = fgets($fpOctOut);
fclose($fpOctOut);
} else $LastOctOut = 0;
if($Speed>0){
$UtilizationOut = (($OutOct-$LastOctOut)/$Speed) * 100;
}else{
$UtilizationOut =0;
}
print "$UtilizationOut";
if ($UtilizationOut >= 75) {
$saturation2 = 1;
} else {
$saturation2 = 0;
}
$fpOctOut = fopen($tmpPath."_OctOut.tmp", "w");
fwrite($fpOctOut, $OutOct);
fclose($fpOctOut);
$fpUtilOut = fopen($tmpPath."_UtilizationOut.tmp", "w");
fwrite($fpUtilOut, $saturation2);
fclose($fpUtilOut);
} elseif ($query_field == "ifErrorsIn") {
$fileErrIn = $tmpPath."_ErrIn.tmp";
if (file_exists($fileErrIn)) {
$fpErrIn = fopen($fileErrIn, "r");
$LastErrIn = fgets($fpErrIn);
fclose($fpErrIn);
} else $LastErrIn = 0;
if($InOct == 0) $ErrorsIn = 0;
else $ErrorsIn = (($InErrors-$LastErrIn)/$InOct) * 100;
print "$ErrorsIn";
if ($ErrorsIn >= 25) {
$saturation3 = 1;
} else {
$saturation3 = 0;
}
$fpErrIn = fopen($tmpPath."_ErrIn.tmp", "w");
fwrite($fpErrIn, $InErrors);
fclose($fpErrIn);
$fpErrorsIn = fopen($tmpPath."_SaturErrorsIn.tmp", "w");
fwrite($fpErrorsIn, $saturation3);
fclose($fpErrorsIn);
} elseif ($query_field == "ifErrorsOut") {
$fileErrOut = $tmpPath."_ErrOut.tmp";
if (file_exists($fileErrOut)) {
$fpErrOut = fopen($tmpPath."_ErrOut.tmp", "r");
$LastErrOut = fgets($fpErrOut);
fclose($fpErrOut);
} else $LastErrOut = 0;
if($OutOct == 0) $ErrorsOut = 0;
else $ErrorsOut = (($OutErrors-$LastErrOut)/$OutOct) * 100;
print $ErrorsOut;
if ($ErrorsOut >= 25) {
$saturation4 = 1;
} else {
$saturation4 = 0;
}
$fpErrOut = fopen($tmpPath."_ErrOut.tmp", "w");
fwrite($fpErrOut, $OutErrors);
fclose($fpErrOut);
$fpErrorsOut = fopen($tmpPath."_SaturErrorsOut.tmp", "w");
fwrite($fpErrorsOut, $saturation4);
fclose($fpErrorsOut);
} elseif ($query_field == "ifSaturation") {
$fileUtilIn = $tmpPath."_UtilizationIn.tmp";
if (file_exists($fileUtilIn)) {
$fpUtilIn = fopen($fileUtilIn, "r");
$LastUtilIn = fgets($fpUtilIn);
fclose($fpUtilIn);
} else $LastUtilIn = 0;
$fileUtilOut = $tmpPath."_UtilizationOut.tmp";
if (file_exists($fileUtilOut)) {
$fpUtilOut = fopen($fileUtilOut, "r");
$LastUtilOut = fgets($fpUtilOut);
fclose($fpUtilOut);
} else $LastUtilOut = 0;
$fileErrorsIn = $tmpPath."_SaturErrorsIn.tmp";
if (file_exists($fileErrorsIn)) {
$fpErrorsIn = fopen($fileErrorsIn, "r");
$LastErrorsIn = fgets($fpErrorsIn);
fclose($fpErrorsIn);
} else $LastErrorsIn = 0;
$fileErrorsOut = $tmpPath."_SaturErrorsOut.tmp";
if (file_exists($fileErrorsOut)) {
$fpErrorsOut = fopen($fileErrorsOut, "r");
$LastErrorsOut = fgets($fpErrorsOut);
fclose($fpErrorsOut);
} else $LastErrorsOut = 0;
if ($LastUtilIn == 1 || $LastUtilOut == 1 || $LastErrorsIn == 1 || $LastErrorsOut == 1 ) {
$saturation = 1;
} else $saturation = 0;
print "$saturation";
} else {
print (cacti_snmp_get($hostname, $snmp_community,
$oids[$query_field] . ".$query_index", $snmp_version, $snmp_auth_username,
$snmp_auth_password, $snmp_auth_protocol, $snmp_priv_passphrase, $snmp_priv_protocol,
$snmp_context, $snmp_port, $snmp_timeout, $snmp_retries, $max_oids, SNMP_POLLER));
}
# -------------------------------------------------------------------------
} else {
print "Invalid use of script query, required parameters:\n\n";
print " <hostname> <community> <version> <snmp_port> <timeout>
<max_oids> <auth_user> <auth_passphrase> <auth_proto>
<priv_passphrase> <priv_proto> <context> <cmd>\n";
}
function reindex($arr) {
$return_arr = array();
for ($i=0;($i<sizeof($arr));$i++) {
$return_arr[$i] = $arr[$i]["value"];
}
return $return_arr;
}
?>