Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

SysLogger

by Rex(Wrecks) (Curate)
on Aug 02, 2001 at 05:23 UTC ( #101556=sourcecode: print w/ replies, xml ) Need Help??

Category: Networking Code
Author/Contact Info Rex(Wrecks) rex555@hotmail.com
Description: Hey all, first post so go easy on me :) I do a lot of automation to test hardware and software, I constantly fight the proprietary logging issue in a lot of code being written by fellow Automation Daemons. Since I work with a lot of hardware, specifically network gear, I wrote this utilty that can be used on both *nix and Win32 (I usually create a standalone exe for Windows) to send messages to a Syslog daemon. I have not used Perl for that long yet so I am sure there are things you can improve on.
#!/usr/bin/perl
###############################################################
# Script for logging automation to a Syslog server            #
# For public use, creator takes no responsibilities for       #
# any damages, misconfigs or loss of sanity resulting from    #
# these script(s)                                             #
###############################################################

use Net::Syslog ;
use Getopt::Std ;
use Net::Ping ;

#Set an array with legal Priority values
@priVals = ("emerg", "alert", "crit", "err", "warning", "notice", "inf
+o", "debug") ;
$priLen = $#priVals ;

getopts('m:p:t:') ;
unless ( defined $opt_p && defined $opt_t )
{
   die ("Missing Options!\n Required:\n\n  -p <proirity of message>\n 
+   Can be emerg, alert, crit, err, warning, notice, info, debug\n\n  
+-t <text of Message to syslog>\n    Note: If you use spaces in your m
+essage, surround the message in quotes\n\n  -m <machine name or IP>\n
+    Note: this is optional, default is 127.0.0.1\n") ;
}

if ( !defined $opt_m )
{
   $opt_m = "127.0.0.1" ;
}

$pingResult = Net::Ping->new('icmp') ;
die ("\nSysLoggerErr:1\nCannot resolve $opt_m\n") unless $pingResult->
+ping($opt_m) ;
$pingResult->close() ;

PRI: for ( $i ; $i <= $priLen ; $i++ )
{
   last PRI if $opt_p eq $priVals[$i] ;
   if ( $i == 7 )
   {
      die ("Not a valid Priority, check caps as this is case sensitive
+!\n") ;
   }
   next PRI if $opt_p ne $priVals[$i] ;
}

$syslog = new Net::Syslog ( SyslogHost=>$opt_m, Facility=>'syslog', Pr
+iority=>$opt_p ) ;
$syslog->send("$opt_t\n") ;

exit (0) ;

Comment on SysLogger
Download Code
(crazyinsomniac) Re: SysLogger
by crazyinsomniac (Prior) on Aug 02, 2001 at 09:26 UTC
    No real improvements, just a matter of style
    (Dammit Jim, this is not C ;-) yeah i know, TIMTOWTDI)
    #!/usr/bin/perl -w ############################################################### # Script for logging automation to a Syslog server # # For public use, creator takes no responsibilities for # # any damages, misconfigs or loss of sanity resulting from # # these script(s) # ############################################################### use strict; use Net::Ping; use Net::Syslog; use Getopt::Std; use vars qw($opt_p $opt_t $opt_m); #Set an array with legal Priority values my @priVals = qw(emerg alert crit err warning notice info debug); getopts('m:p:t:'); # I prefer &getopts('f?h:u:p:', \%O) because it doesn't # set additional global vars like described in pod unless ( defined $opt_p && defined $opt_t ) { die(<<' THEOPTIONS'); Missing Options! Required: -p <proirity of message> Can be emerg, alert, crit, err, warning, notice, info, debug -t <text of Message to syslog> Note: If you use spaces in your message, surround the message in q +uotes -m <machine name or IP> Note: this is optional, default is 127.0.0.1 THEOPTIONS } $opt_m = "127.0.0.1" unless defined $opt_m; my $pingResult = Net::Ping->new('icmp'); die ("\nSysLoggerErr:1\nCannot resolve $opt_m\n") unless $pingResult->ping($opt_m); $pingResult->close() ; for my $priV (@priVals) { last if $opt_p eq $priV; die ("Not a valid Priority, check caps as this is case sensitive!\ +n") if $priV eq 'debug'; next if $opt_p ne $priV; } my $syslog = new Net::Syslog(SyslogHost => $opt_m, Facility => 'syslog', Priority => $opt_p); $syslog->send("$opt_t\n") ; exit;
    update:
    <<' THEOPTIONS' is called a heredoc, just do a search on this site and you'll find plenty of info (and you can always query the faq from perldoc)

    f? was just an example from xml_pimp and f? does nothing special, it just mean there is a switch called f, and one called ? (which is help in xml_pimp)

    As for the info, it's all documentation for Getopt::Std

     
    ___crazyinsomniac_______________________________________
    Disclaimer: Don't blame. It came from inside the void

    perl -e "$q=$_;map({chr unpack qq;H*;,$_}split(q;;,q*H*));print;$q/$q;"

      Hmmm, I like the "C" sytle :) It helps me read all of my code as I write C as well. But I'm sure I could argue about that all day.

      Where do I get info on what you suggested with the getopts line? Specificly, what does the f? do?
      I'm also curious to look up the trick that you used with THEOPTIONS...interesting.

      I really like perl, and the comunities seem very helpful as well.
      Thanks

      Rex(Wrecks)
      =========
      "Nothing is sure but death and taxes" I say combine the two and its death to all taxes!

Back to Code Catacombs

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://101556]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2014-12-19 11:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (79 votes), past polls