Welcome to the nuBuilder Forums!

Register and log in to access exclusive forums and content available only to registered users.

error PHP snmpget

Questions related to customising nuBuilder Forte with JavaScript or PHP.
Post Reply
kknm
Posts: 366
Joined: Sat Apr 11, 2020 12:03 am
Has thanked: 3 times
Been thanked: 4 times
Contact:

error PHP snmpget

Unread post by kknm »

Updated Ubuntu 18.04 to 20.04
Previously the SNMP procedure worked correctly:
*************************************************

Code: Select all

$param = json_decode(base64_decode('#param#')); // retrieve the parameters
$host = $param-ip;

$pCountTotal = '1.3.6.1.2.1.43.10.2.1.4.1.1';
$phHostName_HP = '1.3.6.1.2.1.1.5.0';
$place = '1.3.6.1.2.1.1.6.0';
$capacity = '1.3.6.1.2.1.43.11.1.1.8.1.1';
$ostatok = '1.3.6.1.2.1.43.11.1.1.9.1.1';
$cyan = '1.3.6.1.2.1.43.11.1.1.8.1.2';
$mgent = '1.3.6.1.2.1.43.11.1.1.8.1.3';
$yell = '1.3.6.1.2.1.43.11.1.1.8.1.4';
$manuf = '1.3.6.1.2.1.1.1.0';
$ost_c = '1.3.6.1.2.1.43.11.1.1.9.1.2';
$ost_m = '1.3.6.1.2.1.43.11.1.1.9.1.3';
$ost_y = '1.3.6.1.2.1.43.11.1.1.9.1.4';
//$host='192.168.1.134';
$community='public';
//$community_write='private';
//snmp_set_quick_print (0); #Changing the output format to full
$tot=@snmpget($host,$community, $pCountTotal);

if ($tot) {
    $page = explode(':', $tot);
    $js = 'parent.$('#obz_str').val($page[1]).change(); ';
    nuJavascriptCallback($js);
    }else{
    $js = 'parent.$('#obz_str').val(0).change(); ';
    nuJavascriptCallback($js);
}
Now the error:
***********************

Code: Select all

Procedure SNMP

/var/www/kknmkomp/core/nucommon.php(1408) : eval()'d code
Call to undefined function snmpget()

Traced from...

(line:104) /var/www/kknmkomp/core/nuapi.php - nuRunPHPHidden

(line:396) /var/www/kknmkomp/core/nucommon.php - nuRunPHP

(line:427) /var/www/kknmkomp/core/nucommon.php - nuEval

(line:1408) /var/www/kknmkomp/core/nucommon.php - eval
Where to look for an error in Ubuntu or in nuBuilder ver.V.4.5-2023.09.18.02?
kev1n
nuBuilder Team
Posts: 4292
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 444 times
Contact:

Re: error PHP snmpget

Unread post by kev1n »

I asked ChatGPT:

The error "Call to undefined function snmpget()" indicates that the `snmpget()` function is not available in your PHP environment. This typically occurs because the SNMP (Simple Network Management Protocol) functions are not enabled or the required SNMP extension is not installed.

Here's a step-by-step guide to resolve this issue:

### 1. Verify PHP Version
First, ensure you are using a version of PHP that supports the SNMP functions. SNMP support is available in PHP 4.3.0 and later.

### 2. Check PHP Configuration
Ensure that the SNMP extension is enabled in your PHP configuration. The SNMP functions are part of the `php-snmp` extension. You can check this in your `php.ini` file.

- Windows: Open your `php.ini` file and look for the line:

Code: Select all

;extension=php_snmp.dll
Remove the semicolon (`;`) to uncomment the line:

Code: Select all

extension=php_snmp.dll
- Linux/Unix: Open your `php.ini` file and look for the line:

Code: Select all

  ;extension=snmp.so
  
Remove the semicolon (`;`) to uncomment the line:

Code: Select all

  extension=snmp.so
  
### 3. Install SNMP Extension
If the extension is not present in your `php.ini` file, you might need to install it.

- On Windows:
- Ensure you have the `php_snmp.dll` file in your PHP extensions directory.
- Add the following line to your `php.ini` file:

Code: Select all

    extension=php_snmp.dll
    
- On Linux/Unix:
- Install the SNMP extension using your package manager. For example, on Ubuntu or Debian, you can install it using:

Code: Select all

    sudo apt-get install php-snmp
    
For CentOS or RHEL:

Code: Select all

    sudo yum install php-snmp
    
- After installation, restart your web server:

Code: Select all

    sudo systemctl restart apache2  # for Apache
    sudo systemctl restart nginx    # for Nginx
    
### 4. Verify Extension is Loaded
You can verify that the SNMP extension is loaded by creating a PHP file (e.g., `phpinfo.php`) with the following content:

Code: Select all

<?php
phpinfo();
?>
Access this file through your web browser. Search for "snmp" in the output to confirm that the SNMP extension is enabled.

### 5. Testing `snmpget()`
After ensuring the extension is enabled, test the `snmpget()` function in a PHP script:

Code: Select all

<?php
$ip = "127.0.0.1";
$community = "public";
$oid = "1.3.6.1.2.1.1.1.0";  // Example OID

$result = snmpget($ip, $community, $oid);

if ($result === false) {
    echo "SNMP get failed.";
} else {
    echo "SNMP get succeeded: $result";
}
?>
### Summary
The steps to resolve the "Call to undefined function snmpget()" error involve ensuring the SNMP extension is installed and enabled in your PHP environment. Verify your PHP version, update your `php.ini` configuration, install the necessary packages, and check that the extension is correctly loaded. Following these steps should enable you to use the `snmpget()` function without issues.
kknm
Posts: 366
Joined: Sat Apr 11, 2020 12:03 am
Has thanked: 3 times
Been thanked: 4 times
Contact:

Re: error PHP snmpget

Unread post by kknm »

Very strange...
When updating I did not change the PHP7.4 and apache version. Unfortunately, I can no longer check whether the php-snmp module was enabled, but in php.ini extension=snmp.so was commented out. Now I installed php-snmp, php8.1-snmp was installed automatically, but php7.4-snmp cannot be installed due to dependencies. Removing the comment in php.ini does nothing.
kev1n
nuBuilder Team
Posts: 4292
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 444 times
Contact:

Re: error PHP snmpget

Unread post by kev1n »

That issue isn't directly related to nuBuilder itself, but rather to the PHP version, dependencies, and settings. It might be more suitable to ask your question on platforms like Stack Overflow, where there are numerous experts available to assist.
kknm
Posts: 366
Joined: Sat Apr 11, 2020 12:03 am
Has thanked: 3 times
Been thanked: 4 times
Contact:

Re: error PHP snmpget

Unread post by kknm »

I solved the problem using the usual
snmp and shell_exec.

$tot=shell_exec(snmpwalk -v2c -c public '.$host.' '.$pCountTotal)
Post Reply