I am trying to polling interface traffic with customized oids, here index is not standard, I am getting query_index values by quering objectName, its giving me interface names, interface names are converted to query_index value however showing U result
command line snmpget for concatenating OID is working
example:-
root@tejagponnms:/var/www/html/cacti/scripts# php -q olt_interface_traffic.php xxx_WTT_xxx_SME xxxx 1 161 500 "" "" "" "" "" "" "" get tejasStoctetsRx 2
.1.3.6.1.4.1.8255.1.2.1.2.37.1.1.480.10000.1.1.2 ---> this is concetinated OID
U --> result
command line output
root@tejagponnms:/var/www/html/cacti/scripts# snmpget -v2c -c xxxx xxx_WTT_xxx_SME.1.3.6.1.4.1.8255.1.2.1.2.37.1.1.480.10000.1.1.2
SNMPv2-SMI::enterprises.8255.1.2.1.2.37.1.1.480.10000.1.1.2 = Counter64: 47616
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.4.1.8255.1.2.1.2.37.1.1.7",
"ifOperStatus" => ".1.3.6.1.4.1.8255.1.2.1.2.37.1.1.3",
"ifType" => ".1.3.6.1.4.1.8255.1.2.1.2.37.1.1.4",
"objectName" => ".1.3.6.1.4.1.8255.1.2.1.2.37.1.1.1001",
"lCTName" => ".1.3.6.1.4.1.8255.1.2.1.2.37.1.1.1002",
"tejasStoctetsRx" => ".1.3.6.1.4.1.8255.1.2.1.2.37.1.1.480",
"tejasStoctetsTx" => ".1.3.6.1.4.1.8255.1.2.1.2.37.1.1.482",
);
$xml_delimiter = "!";
# All required input parameters
$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 the number of SNMP retries from global settings
$snmp_retries = read_config_option("snmp_retries");
# -------------------------------------------------------------------------
# Script MUST respond to index queries
if ($cmd == "index") {
# Retrieve all indices from the 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));
# Print each index as a separate line
foreach ($return_arr as $index) {
print $index . "\n";
}
}
# -------------------------------------------------------------------------
# Script MUST respond to query requests
elseif ($cmd == "query" && isset($query_field)) {
$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));
foreach ($arr_index as $i => $index) {
print $index . $xml_delimiter . $arr[$i] . "\n";
}
}
elseif ($cmd == "get" && isset($query_field) && isset($query_index)) {
// Perform SNMP walk on the specified query_field OID
// appending query_index to query_field is not working, hence changed logic
// oids["objectName"] gives me interfaces, spliting values by Port_Eth and replacing - with . to get query_index value
// then concating query_field oid and query_index value
$result = reindex(cacti_snmp_walk($hostname, $snmp_community,
$oids["objectName"], $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));
if (is_array($result)) {
// Calculate the position to retrieve
$position = $query_index - 1;
if (isset($result[$position])) {
$value = $result[$position];
// Split $value by "Port_Eth-"
$parts = explode("Port_Eth-", $value);
if (count($parts) === 2) {
// Replace "-" with "." in the second part
$modifiedValue = str_replace("-", ".", $parts[1]);
#echo $modifiedValue . "\n";
#echo $oids[$query_field]. "\n";
$full_oid = $oids[$query_field] . "." . $modifiedValue;
echo $full_oid . "\n";
print(cacti_snmp_get($hostname, $snmp_community,
$full_oid, $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 {
echo "Invalid format in the SNMP walk result for position $position.\n";
}
} else {
echo "Invalid query_index: $query_index. It does not correspond to a valid position in the SNMP walk result.\n";
}
} else {
// Handle the case where the SNMP walk result is not an array
echo "SNMP walk did not return an array.\n";
}
} 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();
foreach ($arr as $entry) {
$return_arr[] = $entry["value"];
}
return $return_arr;
}