snmptrapd traphandle到php脚本

我必须在PHP脚本中接收snmptraps,所以我所做的是:

snmptrapd.conf

traphandle 1.3.6.1.4.1.3.1.1 /usr/home/user/trap/l.php 

l.php

 #!/usr/local/bin/php <?php $flow = fopen("php://stderr","r"); while(!feof($flow)){ file_put_contents("out",fread($flow,1024)."\n", FILE_APPEND); } ?> 

然后我开始snmptrapd像这样:

 snmptrapd -Le -f 

并生成这样的陷阱:

 snmptrap -v 1 -c public localhost '' localhost 6 1 '' 

snmptrapd给了我这样的输出

 2012-01-16 14:38:49 127.0.0.1(via UDP: [127.0.0.1]:11478->[0.0.0.0]:0) TRAP, SNMP v1, community public SNMPv2-SMI::enterprises.3.1.1 Enterprise Specific Trap (1) Uptime: 70 days, 1:03:57.00 

所以它似乎工作…但问题是, l.php没有执行,或没有什么stderr – 我不明白。

请,我的错误在哪里?

php://stderr不是可以读取的stream。 它是PHP程序本身的STDERRpipe道,它是只写的。

您需要通过STDIN访问数据。 而不是使用php://封装来访问STDIOstream,这是已知的错误(参见手册 ),您应该使用特殊的常量STDOUTSTDERRSTDIN 。 例如,我可能会这样写你的PHP脚本:

 #!/usr/local/bin/php <?php // The file we will be writing to. It's best to specify a full path, to avoid // any confusion over the current working directory $outFile = '/var/myuser/snmp.out'; // First, we'll open our outfile... if (!$ofp = fopen($outFile, 'a')) exit("Oh No! I couldn't open my out file!"); // ...and write the current timestamp to it, so we are certain the script was // invoked. You can remove this if you want, once you are sure it is working fwrite($ofp, "Process started at ".date('Ymd H:i:s')."\n"); // Next we'll write all the trap data to the file // We could use stream_copy_to_stream() for this, but I don't know if it is // available on your server so I won't do it here fwrite($ofp,"Data:\n"); while (!feof(STDIN)) { fwrite($ofp, fread(STDIN, 1024)."\n"); } fwrite($ofp,"End Data\n"); // We don't actually need a closing PHP tag and in many cases it is better // to omit it, to avoid unexpected whitespace being output.