I am using SNMP traps to pass information via snmp from a module on one system to a module on another system. Specifically, I am using SNMPv2 Notify messages, as described on this page:
Now, for reasons that are not at all clear to me, snmptrapd expects that the command that it invokes when it processes a trap will be a shell script.
I do not wish to use a shell script; I want to have snmptrapd invoke a dedicated C program that I will provide. This C program will accept the data that is being sent via the trap, and will forward that data via some IPC mechanism (I haven't decided yet...probably a TCP socket) to its ultimate destination.
I am correctly building the SNMP trap message in my C module, and sending it. The snmptrapd is properly processing it and invoking an appropriate command. When that command is the demo handler script as described on the net-snmp page (and reproduced here), everything works as expected and the output is correct (a text file in /tmp, for now).
When I replace that script with a dedicated C program, the result is not as desired; I am not picking up the trap information although the program is being invoked.
Specifically, here is the demo trap handler script I am using, from the net-snmp site:
echo "executing script" >/tmp/mytstfile
while read oid val
if [ "$vars" = "" ]
vars="$oid = $val"
vars="$vars, $oid = $val"
echo trap: $1 tag1 $host tag2 $ip tag3 $vars >>/tmp/mytstfile
And here is my C program, which I would like to have do the same thing (output the same data, though the format might be different):
int main(int argc, char *argv)
fprintf(fp,"arg count is %s\n");
arg = argv[nn];
while(argv[nn] != NULL)
//while(nn < 5)
fprintf(fp,"arg is %s\n",argv[nn]);
Now, here is the text from /tmp/mytstfile after I invoke a trap with the shell script as the command:
trap: tag1 dadsbox.homegroup tag2 UDP: [192.168.0.2]:60925 tag3 DISMAN-EVENT-MIB::sysUpTimeInstance = 74695283 ==spacer==, SNMPv2-MIB::snmpTrapOID.0 = ExacqSCAN-MIB::exacqnotiftrap ==spacer==, ExacqSCAN-MIB::scanLastData.1 = "abc=def" ==spacer==
And here is the text from /tmp/mytstfile after I invoke a trap with the C program as the command:
arg count is
arg is /home/jiml/scan_snmp/testscript
Note in the C code that there is a commented out while statement, which should have the effect of wandering off into random memory if I invoke it when the actual argument count is 0 or one. Well, when I invoke it, I do get the command name listed as the first argument and the subsequent "arguments" are clearly environment variables and associated values.
So, the problem here is quite obviously that I do not know where to pick up the argument list that is passed to a shell script when it is invoked.
Could anyone here tell me where that argument list is kept? Perhaps a code fragment that will pick it up?