Perl... Logging to console and Log file

Hi friends,

Kindly help me regarding the issue Im facing with the below perl
script,

I have a below script which send the message to Console.

I need to modify this script it send message to a Log file.

Please find the below steps which i performed to send a message to
log.

Orginal Script:
===============
return -1 on error
# 0 pkg is starting or running and switchable
# 1 pkg is starting or running and not switchable
# 2 pkg is not running
_show_pkg() {
local pkg=${1:-ALL} #i.e 'GENPKG1,GENPKG3' or 'ALL' or
'GENPKG1' or ...
local mode=${2:-silent} #i.e 'normal'

clustat -flx | perl -MXML::Twig -e'
my %S = (started => q/UP/, recoverable => q/FAILED/, pending
=> q/FAILED/, stopped => q/DOWN/, failed => q/FAILED/, disabled => q/
DOWN/ );
my [at] A = split /,/, $ARGV[0];
my ($ret,$cs) = (0,0);
my $twig = new XML::Twig(TwigRoots => {node => 1, group => 1},
TwigHandlers => { node => sub {
my ($t,$n) = [at] _;
$cs = $ret = 1 unless ($n->att(rgmanager) eq q/1/ and $n->att(state)
eq q/1/);
},
group => sub {
my ($t,$g) = [at] _;
my $pkg = (split /:/,$g->att(name))[1];
return unless (q/ALL/ eq $A[0] or grep /$pkg/, [at] A);
my $owner = "on ".substr($g->att(owner),0,-2) if $g->att(state_str) eq
q/started/;
unless ($ARGV[1] eq q/silent/) {
print("State of $pkg is $S{$g->att(state_str)}");
print(" $owner and ",($g->att(restarts)>0 or $cs)?"not":" ","
switchable") if $owner;
print "\n";
}
$ret = 1 if $g->att(restarts)>0;
$ret = 2 unless $g->att(state_str) eq q/started/;
}});
print(STDERR "Cannot open XML::Twig\n"), exit -1 unless
defined $twig; $twig->safe_parse(STDIN); exit $ret;
' ${pkg} ${mode}
}

Note ===> Steps

1. The output of the above script will display below information in
Screen ( console), it
will only display GENPKG{1..5} information on screen.

[root [at] incciozr0846 hbha]# show_pkg
State of GENPKG1 is FAILED
State of GENPKG2 is FAILED
State of GENPKG3 is FAILED
State of GENPKG4 is FAILED
State of GENPKG5 is FAILED
[root [at] incciozr0846 hbha]#

2. I want to send the same message to log file, So I adopted the
script as shown below.

my $file1 = "/tmp/pmon/usbs_manage_ha.log";
open FILE, ">>$file1" or die "unable to open $file1 $!";

unless ($ARGV[1] eq q/silent/) {
print("State of $pkg is $S{$g->att(state_str)}");
print FILE ("State of $pkg is $S{$g->att(state_str)} \n ");
print(" $owner and ",($g->att(restarts)>0 or $cs)?"not":" ","
switchable") if $owner ;
print FILE (" $owner and ",($g->att(restarts)>0 or $cs)?"not":"
"," switchable \n" ) if $owner ;
print "\n";
}
close FILE or die;

3) Kindly note, Now im able to write to $file1 ("/tmp/pmon/
usbs_manage_ha.log"), but its continiously writing to the
$file1 in loop,

< extract of messgae in log >


#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#* #*#*#*#*#*#*#*#*#*#*
#H: show_pkg called by: bash[2208] at Sat Jan 19 03:46:41 CET 2002
State of GENPKG1 is FAILED
State of GENPKG2 is FAILED
State of GENPKG3 is FAILED
State of GENPKG4 is FAILED
State of GENPKG5 is FAILED
#H: show_pkg (ret = 2) terminate at Sat Jan 19 03:46:41 CET 2002
State of GENPKG5 is FAILED
State of GENPKG1 is FAILED
State of GENPKG2 is FAILED
State of GENPKG3 is FAILED
State of GENPKG4 is FAILED
State of GENPKG5 is FAILED
State of GENPKG1 is FAILED
State of GENPKG2 is FAILED
State of GENPKG3 is FAILED
State of GENPKG4 is FAILED
State of GENPKG5 is FAILED
State of GENPKG1 is FAILED
State of GENPKG2 is FAILED
State of GENPKG3 is FAILED


4) Please provide your inputs to get rid of this loop,




--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
vasanth [ Do, 13 Januar 2011 09:15 ] [ ID #2053127 ]

Re: Perl... Logging to console and Log file

--00504502d1b5dc7e490499b77f44
Content-Type: text/plain; charset=ISO-8859-1

On Jan 13, 2011 4:19 AM, "vasanth" <mailme.vasanthan [at] gmail.com> wrote:
>
> Hi friends,
>
> Kindly help me regarding the issue Im facing with the below perl
> script,
>
> I have a below script which send the message to Console.
>
> I need to modify this script it send message to a Log file.
>
> Please find the below steps which i performed to send a message to
> log.
>
> Orginal Script:
> ===============
> return -1 on error
> # 0 pkg is starting or running and switchable
> # 1 pkg is starting or running and not switchable
> # 2 pkg is not running
> _show_pkg() {
> local pkg=${1:-ALL} #i.e 'GENPKG1,GENPKG3' or 'ALL' or
> 'GENPKG1' or ...
> local mode=${2:-silent} #i.e 'normal'
>
> clustat -flx | perl -MXML::Twig -e'
> my %S = (started => q/UP/, recoverable => q/FAILED/, pending
> => q/FAILED/, stopped => q/DOWN/, failed => q/FAILED/, disabled => q/
> DOWN/ );
> my [at] A = split /,/, $ARGV[0];
> my ($ret,$cs) = (0,0);
> my $twig = new XML::Twig(TwigRoots => {node => 1, group => 1},
> TwigHandlers => { node => sub {
> my ($t,$n) = [at] _;
> $cs = $ret = 1 unless ($n->att(rgmanager) eq q/1/ and $n->att(state)
> eq q/1/);
> },
> group => sub {
> my ($t,$g) = [at] _;
> my $pkg = (split /:/,$g->att(name))[1];
> return unless (q/ALL/ eq $A[0] or grep /$pkg/, [at] A);
> my $owner = "on ".substr($g->att(owner),0,-2) if $g->att(state_str) eq
> q/started/;
> unless ($ARGV[1] eq q/silent/) {
> print("State of $pkg is $S{$g->att(state_str)}");
> print(" $owner and ",($g->att(restarts)>0 or $cs)?"not":" ","
> switchable") if $owner;
> print "\n";
> }
> $ret = 1 if $g->att(restarts)>0;
> $ret = 2 unless $g->att(state_str) eq q/started/;
> }});
> print(STDERR "Cannot open XML::Twig\n"), exit -1 unless
> defined $twig; $twig->safe_parse(STDIN); exit $ret;
> ' ${pkg} ${mode}
> }
>
> Note ===> Steps
>
> 1. The output of the above script will display below information in
> Screen ( console), it
> will only display GENPKG{1..5} information on screen.
>
> [root [at] incciozr0846 hbha]# show_pkg
> State of GENPKG1 is FAILED
> State of GENPKG2 is FAILED
> State of GENPKG3 is FAILED
> State of GENPKG4 is FAILED
> State of GENPKG5 is FAILED
> [root [at] incciozr0846 hbha]#
>
> 2. I want to send the same message to log file, So I adopted the
> script as shown below.
>
> my $file1 = "/tmp/pmon/usbs_manage_ha.log";
> open FILE, ">>$file1" or die "unable to open $file1 $!";
>
> unless ($ARGV[1] eq q/silent/) {
> print("State of $pkg is $S{$g->att(state_str)}");
> print FILE ("State of $pkg is $S{$g->att(state_str)} \n ");
> print(" $owner and ",($g->att(restarts)>0 or $cs)?"not":" ","
> switchable") if $owner ;
> print FILE (" $owner and ",($g->att(restarts)>0 or $cs)?"not":"
> "," switchable \n" ) if $owner ;
> print "\n";
> }
> close FILE or die;
>
> 3) Kindly note, Now im able to write to $file1 ("/tmp/pmon/
> usbs_manage_ha.log"), but its continiously writing to the
> $file1 in loop,
>
> < extract of messgae in log >
>
>
>
#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#* #*#*#*#*#*#*#*#*#*#*
> #H: show_pkg called by: bash[2208] at Sat Jan 19 03:46:41 CET 2002
> State of GENPKG1 is FAILED
> State of GENPKG2 is FAILED
> State of GENPKG3 is FAILED
> State of GENPKG4 is FAILED
> State of GENPKG5 is FAILED
> #H: show_pkg (ret = 2) terminate at Sat Jan 19 03:46:41 CET 2002
> State of GENPKG5 is FAILED
> State of GENPKG1 is FAILED
> State of GENPKG2 is FAILED
> State of GENPKG3 is FAILED
> State of GENPKG4 is FAILED
> State of GENPKG5 is FAILED
> State of GENPKG1 is FAILED
> State of GENPKG2 is FAILED
> State of GENPKG3 is FAILED
> State of GENPKG4 is FAILED
> State of GENPKG5 is FAILED
> State of GENPKG1 is FAILED
> State of GENPKG2 is FAILED
> State of GENPKG3 is FAILED
>
>
> 4) Please provide your inputs to get rid of this loop,
>

If the original script worked, why not use tee from the command line?

--00504502d1b5dc7e490499b77f44--
Shawn Wilson [ Do, 13 Januar 2011 11:08 ] [ ID #2053129 ]

Re: Perl... Logging to console and Log file

--0016367b5e70ee61360499ba0740
Content-Type: text/plain; charset=ISO-8859-1

Thu, Jan 13, 2011 at 2:15 AM, vasanth <mailme.vasanthan [at] gmail.com> wrote:

> Kindly help me regarding the issue Im facing with the below perl
> script,
>
> I have a below script which send the message to Console.
>
> I need to modify this script it send message to a Log file.
>
> Please find the below steps which i performed to send a message to
> log.
>

Take a look at Log::Log4perl<http://search.cpan.org/%7Emschilli/Log-Log4perl-1.31/lib/Log/Log4perl.pm>(
http://search.cpan.org/~mschilli/Log-Log4perl-1.31/lib/Log/L og4perl.pm). It
handles multiple outputs seamlessly.

--
Robert Wohlfarth

--0016367b5e70ee61360499ba0740--
Robert Wohlfarth [ Do, 13 Januar 2011 14:09 ] [ ID #2053133 ]
Perl » gmane.comp.lang.perl.beginners » Perl... Logging to console and Log file

Vorheriges Thema: Perl OOP concept in real time scenario
Nächstes Thema: Return value from function