Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate SNMP traps with PHP

I'm desperately searching for a way to generate SNMP traps from PHP. I know the build in methods to use snmpget but I was not able to figure out how to send SNMP traps.

Does anybody know a class / code snippet for it? Searching the web did not bring up anything other than using exec to call cli tools which is definately no option for me.

I suspect that it would be neccessary to use socket_create and corresponding functionality to generate the UDP package manually...

like image 986
glutorange Avatar asked Sep 29 '10 20:09

glutorange


3 Answers

As far as I know, there is no native way for generating traps/informs with php. Even the SNMP extension only permits get and set requests. So the only (quick) way to accomplish this is to call an external tool like net-snmp. The proper command line would be something like

snmptrap -v 1 -c public manager enterprises.spider test-hub 3 0 '' interfaces.iftable.ifentry.ifindex.1 i 1

will send a generic linkUp trap to manager, for interface 1 (taken from the manpage). To execute this from php the net-snmp binaries should be on the path of the system and you could either call exec, shell_exec or proc_open.

Obvisouly you also can send the trap by yourself by encoding it as raw byte array and sending it over an UDP socket, but then you had to implement a BER encoder and a SNMP packet encoder all by yourself which I don't recommend. For your reference, you would need those informations:

like image 172
jek Avatar answered Nov 06 '22 11:11

jek


There are no core SNMP trap libraries. Or even any core libraries that will help you package an SNMP udp packet. I did however find this abandoned project. http://code.google.com/p/php-snmp/ which provides most of what you would need to send a simple trap.

A little more active but a lot more complex seems to be http://www.activexperts.com/network-component/howto/snmpts/php/

like image 22
reconbot Avatar answered Nov 06 '22 12:11

reconbot


For anybody searching for such a library these days (in 2019), I found https://github.com/FreeDSx/SNMP which supports sending SNMPv1 and SNMPv2 traps (including inform requests).

like image 1
kguest Avatar answered Nov 06 '22 12:11

kguest