php script not showing results

Templates, scripts for templates, scripts and requests for templates.

Moderators: Developers, Moderators

Post Reply
yedascientist
Posts: 10
Joined: Fri Oct 27, 2023 12:23 pm

php script not showing results

Post by yedascientist »

Hi All,

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;
}
User avatar
TheWitness
Developer
Posts: 17047
Joined: Tue May 14, 2002 5:08 pm
Location: MI, USA
Contact:

Re: php script not showing results

Post by TheWitness »

Look at the script ss_hstats.php. You no longer set the http_headers, etc. Just call cli_check.php be it script server or otherwise.

Code: Select all

error_reporting(0);

if (!isset($called_by_script_server)) {
    include_once(__DIR__ . '/../include/cli_check.php');

    array_shift($_SERVER['argv']);

    print call_user_func_array('ss_hstats', $_SERVER['argv']);
}
True understanding begins only when we realize how little we truly understand...

Life is an adventure, let yours begin with Cacti!

Author of dozens of Cacti plugins and customization's. Advocate of LAMP, MariaDB, IBM Spectrum LSF and the world of batch. Creator of IBM Spectrum RTM, author of quite a bit of unpublished work and most of Cacti's bugs.
_________________
Official Cacti Documentation
GitHub Repository with Supported Plugins
Percona Device Packages (no support)
Interesting Device Packages


For those wondering, I'm still here, but lost in the shadows. Yearning for less bugs. Who want's a Cacti 1.3/2.0? Streams anyone?
yedascientist
Posts: 10
Joined: Fri Oct 27, 2023 12:23 pm

Re: php script not showing results

Post by yedascientist »

Thanks TheWitness,

here is my script output, this device is not having standard snmp oids, i am customizing to get utilization graphs
xxxx# php -q olt_interface_traffic.php Nxx_xxx_xxx_SME xxxxxx 2 161 500 "" "" "" "" "" "" "" index
4096
8192
12288
16384
20480
24576
28672
32768
40960
45056
49152
53248
57344
61440
65536
69632
73728
77824
81920
86016
90112
94208
98304
102400
36864
xxxx# php -q olt_interface_traffic.php Nxx_xxx_xxx_SME xxxxxx 2 161 500 "" "" "" "" "" "" "" query tejasStoctetsRx
4096!909335154
8192!42240
12288!0
16384!0
20480!0
24576!0
28672!0
32768!0
40960!0
45056!0
49152!0
53248!0
57344!0
61440!0
65536!0
69632!0
73728!0
77824!0
81920!0
86016!0
90112!0
94208!0
98304!0
102400!0
36864!124224
xxxx# php -q olt_interface_traffic.php Nxx_xxx_xxx_SME xxxxxx 2 161 500 "" "" "" "" "" "" "" get tejasStoctetsRx 1
934843648

xxxxx# php -q olt_interface_traffic.php Nxx_xxx_xxx_SME xxxxxx 2 161 500 "" "" "" "" "" "" "" get tejasStoctetsTx 1
.1.3.6.1.4.1.8255.1.2.1.2.37.1.1.482.10000.1.1.1
1349417418
how can i pass oid to get values against index, cacti is passing index number against get <query_field> index, however in this case i want to pass oid created as per index

here is my code

Code: Select all

# 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";
}
User avatar
TheWitness
Developer
Posts: 17047
Joined: Tue May 14, 2002 5:08 pm
Location: MI, USA
Contact:

Re: php script not showing results

Post by TheWitness »

Look at the Poller Cache for some clues. Its pretty much templated. You will see something like:

Code: Select all

get metric index
For example:

Code: Select all

 /var/www/html/cacti/scripts/ss_host_cpu.php ss_host_cpu '192.168.11.105' '42' '2:161:1000:1:10:public::::::' 'get' 'usage' '4000' 
 
The metric is 'usage', and the 'index' is '4000'.
True understanding begins only when we realize how little we truly understand...

Life is an adventure, let yours begin with Cacti!

Author of dozens of Cacti plugins and customization's. Advocate of LAMP, MariaDB, IBM Spectrum LSF and the world of batch. Creator of IBM Spectrum RTM, author of quite a bit of unpublished work and most of Cacti's bugs.
_________________
Official Cacti Documentation
GitHub Repository with Supported Plugins
Percona Device Packages (no support)
Interesting Device Packages


For those wondering, I'm still here, but lost in the shadows. Yearning for less bugs. Who want's a Cacti 1.3/2.0? Streams anyone?
yedascientist
Posts: 10
Joined: Fri Oct 27, 2023 12:23 pm

Re: php script not showing results

Post by yedascientist »

Hi TheWitness,
changed script logic, how able to see graphs and traffic, however getting these warning while graphing using script
2023-11-06 01:30:14 - SNMPAGENT WARNING: No notification receivers configured for event: cactiNotifyDeviceFailedPoll (CACTI-MIB), severity: medium
2023-11-06 01:30:13 - SPINE: Poller[Main Poller] PID[20012] PT[140595454859072] WARNING: Device[x.x.x.xE] polling sleeping while waiting for 1 Threads to End
2023-11-06 01:30:12 - SPINE: Poller[Main Poller] PID[20012] PT[140595454859072] WARNING: Device[x.x.x.xE] polling sleeping while waiting for 1 Threads to End
2023-11-06 01:30:12 - SPINE: Poller[Main Poller] PID[20012] PT[140595454859072] WARNING: Device[x.x.x.xE] polling sleeping while waiting for 1 Threads to End
2023-11-06 01:30:11 - SPINE: Poller[Main Poller] PID[20012] PT[140595454859072] WARNING: Device[x.x.x.xE] polling sleeping while waiting for 1 Threads to End
2023-11-06 01:30:11 - SPINE: Poller[Main Poller] PID[20012] PT[140595454859072] WARNING: Device[x.x.x.xE] polling sleeping while waiting for 1 Threads to End
2023-11-06 01:30:10 - SPINE: Poller[Main Poller] PID[20012] PT[140595454859072] WARNING: Device[x.x.x.xE] polling sleeping while waiting for 1 Threads to End
2023-11-06 01:30:10 - SPINE: Poller[Main Poller] PID[20012] PT[140595454859072] WARNING: Device[x.x.x.xE] polling sleeping while waiting for 1 Threads to End
2023-11-06 01:30:09 - SPINE: Poller[Main Poller] PID[20012] PT[140595454859072] WARNING: Device[x.x.x.xE] polling sleeping while waiting for 1 Threads to End
2023-11-06 01:30:09 - SPINE: Poller[Main Poller] PID[20012] PT[140595454859072] WARNING: Device[x.x.x.xE] polling sleeping while waiting for 1 Threads to End
2023-11-06 01:30:08 - SPINE: Poller[Main Poller] PID[20012] PT[140595454859072] WARNING: Device[x.x.x.xE] polling sleeping while waiting for 1 Threads to End
2023-11-06 01:30:08 - SPINE: Poller[Main Poller] PID[20012] PT[140595454859072] WARNING: Device[x.x.x.xE] polling sleeping while waiting for 1 Threads to End
2023-11-06 01:30:07 - SPINE: Poller[Main Poller] PID[20012] PT[140595454859072] WARNING: Device[x.x.x.xE] polling sleeping while waiting for 1 Threads to End
2023-11-06 01:30:07 - SPINE: Poller[Main Poller] PID[20012] PT[140595454859072] WARNING: Device[x.x.x.xE] polling sleeping while waiting for 1 Threads to End
2023-11-06 01:30:06 - SPINE: Poller[Main Poller] PID[20012] PT[140595454859072] WARNING: Device[x.x.x.xE] polling sleeping while waiting for 1 Threads to End
2023-11-06 01:30:06 - SPINE: Poller[Main Poller] PID[20012] PT[140595454859072] WARNING: Device[x.x.x.xE] polling sleeping while waiting for 1 Threads to End
2023-11-06 01:30:05 - SPINE: Poller[Main Poller] PID[20012] PT[140595454859072] WARNING: Device[x.x.x.xE] polling sleeping while waiting for 1 Threads to End
2023-11-06 01:30:05 - SPINE: Poller[Main Poller] PID[20012] PT[140595454859072] WARNING: Device[x.x.x.xE] polling sleeping while waiting for 1 Threads to End
2023-11-06 01:30:04 - SPINE: Poller[Main Poller] PID[20012] PT[140595454859072] WARNING: Device[x.x.x.xE] polling sleeping while waiting for 1 Threads to End
2023-11-06 01:30:04 - SPINE: Poller[Main Poller] PID[20012] PT[140595454859072] WARNING: Device[x.x.x.xE] polling sleeping while waiting for 1 Threads to End
2023-11-06 01:30:03 - SPINE: Poller[Main Poller] PID[20012] PT[140595454859072] WARNING: Device[x.x.x.xE] polling sleeping while waiting for 1 Threads to End
2023-11-06 01:30:03 - SPINE: Poller[Main Poller] PID[20012] PT[140595454859072] WARNING: Device[x.x.x.xE] polling sleeping while waiting for 1 Threads to End
2023-11-06 01:30:02 - SPINE: Poller[Main Poller] PID[20012] PT[140595454859072] WARNING: Device[x.x.x.xE] polling sleeping while waiting for 1 Threads to End
here is my updated script

Code: Select all

<?php
#error_reporting(0);

#if (!isset($called_by_script_server)) {
#    include_once(__DIR__ . '/../include/cli_check.php');

#    array_shift($_SERVER['argv']);

#    print call_user_func_array('ss_hstats', $_SERVER['argv']);
#}
/* 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.9",
    "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];
#$cmd                    = $_SERVER["argv"][6];
$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");
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";
    }
}
// Define a mapping of common OID elements to their corresponding indexes
if ($cmd == "get" && isset($query_field) && isset($query_index)) {
    // Perform SNMP walk on the specified query_field OID
    $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)) {
        $index_number = (int) $query_index;
        $dynamic_mapping = [];

        // Create a dynamic mapping for the oid_index
        foreach ($result as $position => $value) {
            if (preg_match('/Port_Eth-(\d+-\d+-\d+-\d+)/', $value, $matches)) {
                $oid_index = str_replace('-', '.', $matches[1]);
                $oid_parts = explode('.', $oid_index);
                $index = (int) end($oid_parts);

                $dynamic_mapping[$index] = '.' . $oid_index;
            }
        }

        // Print the dynamic mapping
        #echo "Dynamic Mapping:\n";
        #print_r($dynamic_mapping);

        // Check if the oid_index exists in the dynamic mapping
        if (isset($dynamic_mapping[$index_number])) {
                //$base_oid = $oids["objectName"];
                $base_oid = '.1.3.6.1.4.1.8255.1.2.1.2.37.1.1.480';
            $full_oid = $base_oid . $dynamic_mapping[$index_number];
                #echo "Full OID: $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 query_index: $query_index. It does not correspond to a valid position in the SNMP walk result.\n";
        }
    } else {
        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";
}

User avatar
TheWitness
Developer
Posts: 17047
Joined: Tue May 14, 2002 5:08 pm
Location: MI, USA
Contact:

Re: php script not showing results

Post by TheWitness »

You changed it all wrong. Spend more time reviewing the scripts.
True understanding begins only when we realize how little we truly understand...

Life is an adventure, let yours begin with Cacti!

Author of dozens of Cacti plugins and customization's. Advocate of LAMP, MariaDB, IBM Spectrum LSF and the world of batch. Creator of IBM Spectrum RTM, author of quite a bit of unpublished work and most of Cacti's bugs.
_________________
Official Cacti Documentation
GitHub Repository with Supported Plugins
Percona Device Packages (no support)
Interesting Device Packages


For those wondering, I'm still here, but lost in the shadows. Yearning for less bugs. Who want's a Cacti 1.3/2.0? Streams anyone?
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest