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


In reply to daemonizing my code by arunvelusamy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.