arunvelusamy has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,

I need to receive SNMP traps using a daemon and log it in a file. Can someone give me sample codes for daemonizing the program. I tried using Proc::Daemon but was unsuccessful. I am new to Perl.

#!/usr/local/bin/perl use strict; use Carp; use Proc::Daemon; use SNMP_Session "0.60"; use BER; use Socket; Proc::Daemon::Init; $session = SNMPv1_Session->open_trap_session ( ); while (($trap, $sender, $sender_port) = $session->receive_trap ( )) { chomp ($DATE=`/bin/date \'+%a %b %e %T\'`); # print STDERR "$DATE - " . inet_ntoa($sender) . " - port: $sen +der_port\n"; print_trap ($session, $trap); } sub print_trap ($$) { ($this, $trap) = @_; ($community, $ent, $agent, $gen, $spec, $dt, @bindings) = $this->d +ecode_trap_request ($trap); open(OUT, ">>output.txt") or die "Failed to open file"; print OUT " Community:\t".$community."\n"; print OUT " Enterprise:\t".BER::pretty_oid ($ent)."\n"; print OUT " Agent addr:\t".inet_ntoa ($agent)."\n"; print OUT " Generic ID:\t$gen\n"; print OUT " Specific ID:\t$spec\n"; print OUT " Uptime:\t".BER::pretty_uptime_value ($dt)."\n"; $prefix = " bindings:\t"; foreach $encoded_pair (@bindings) { ($oid, $value) = decode_by_template ($encoded_pair, "%{%O%@"); #next unless defined $oid; print OUT $prefix.BER::pretty_oid ($oid)." => ".pretty_print ( +$value)."\n"; $prefix = " "; } close (OUT) } 1;

Thanks,

Arun Velusamy

Replies are listed 'Best First'.
Re: daemonizing my code
by roboticus (Chancellor) on May 27, 2006 at 13:03 UTC
    arunvelusamy:

    In what way did it fail? Are you getting any error messages? Have you read How (Not) To Ask A Question?

    Offhand, random things I can think of:

  • Your firewall is blocking your port
  • You haven't got permissions to open the socket
  • --roboticus