Rossi Milan Bob Rossi Milan Bob - 2 months ago 24
Linux Question

SNMP Trap In PHP/Linux

Is there anyone know how to send SNMP Trap using php? Or How to send SNMP traps in Linx? I used Net-SNMP to send Traps from the terminal. But it doesn't tell where exactly the MIB need to be placed and I am unable to send traps using 'snmptrap' command. I need to employ it in RedHat Linux server.


MIB search path: /home/user/.snmp/mibs:/usr/local/share/snmp/mibs
Cannot find module (MY-MIB): At line 1 in (none)
MY-MIB::clientrap: Unknown Object Identifier


This is the error I am getting. I am sure that I put the MIB file in the /home/user/.snmp/mibs and /usr/local/share/snmp/mibs as NET-SNMP suggests. Then I don't know how the error come. Is there any other MIB search path? I don't know.

Answer

It seems that there is no direct methods in PHP to send SNMP traps and so does the answers. So at last I came up with my own way, relied on Net-Snmp itself. What I really wanted was to send the Snmp trap from a php program. I was able to do it finally with the help of Net-Snmp library through the Linux terminal. Following are the steps.

  1. Install Net-Snmp : There are two ways that you can install net-Snmp package in your linux machine

    1) By downloading Net-Snmp the package from sourceforge.net as mentioned in http://www.net-snmp.org/wiki/index.php/Net-Snmp_on_Ubuntu.

    2) By Installing pysnmp module - This method seemed more simple. Also you can do it in two different ways.

    a) By manually downloading the library and extracting succeeded by running a python script to install
    b) By just using the command **sudo pip install pysnmp** , assuming you have installed the pip.
    

    both the methods are described here - https://pynet.twb-tech.com/blog/snmp/python-snmp-intro.html

    the reason why we install pysnmp is that the pysnmp package automatically installs the basic libraries of the NET-SNMP even though it is made for the python programs.

  2. confirm the installation of the NET-SNMP by typing the commands snmpd --version or snmpget or snmptrap etc.

  3. Find the MIB search path by using the command snmpget -Dparse-mibs 2>&1 | grep directory. It will display the paths where the Net-Snmp looks for the MIB files. Normally the results are :

    parse-mibs: Scanning directory /var/www/html/User/.snmp/mibs parse-mibs: cannot open MIB directory /var/www/html/User/.snmp/mibs parse-mibs: Scanning directory /usr/local/share/snmp/mibs

  4. Copy(Upload) the MIB file in '.txt' format to one of the MIB search path which is available. Normally it is

    /usr/local/share/snmp/mibs

  5. Send the trap using the command

    snmptrap -v 2c -c public 192.168.1.1:162 "" MIBNAME-MIB::trapname MIBNAME-MIB::message1 s "Hi" MIBNAME-MIB::message2 s "It works"

    note that '192.168.1.1:162' is the manager IP along with the port number. MIBNAME is the MIB module name given in the MIB file and trapname is the trapname in the MIB file.

Assumed that you have a working SNMP Manger machine in the given IP to receive your Snmp traps in the format given in the MIB file in SNMP Manager.