2007-10-26 22:44:33 +00:00
|
|
|
#!/usr/bin/perl
|
|
|
|
# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html
|
|
|
|
|
|
|
|
package xCAT::MsgUtils;
|
|
|
|
|
|
|
|
use strict;
|
2007-12-11 14:18:45 +00:00
|
|
|
use Sys::Syslog;
|
2007-10-26 22:44:33 +00:00
|
|
|
use locale;
|
|
|
|
use Socket;
|
|
|
|
use File::Path;
|
|
|
|
|
2007-12-11 14:18:45 +00:00
|
|
|
$::NOK = -1;
|
|
|
|
$::OK = 0;
|
2007-10-26 22:44:33 +00:00
|
|
|
|
|
|
|
#--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
=head1 xCAT::MsgUtils
|
|
|
|
|
|
|
|
|
|
|
|
=head2 Package Description
|
|
|
|
|
|
|
|
|
2008-02-18 15:57:25 +00:00
|
|
|
This program module file, supports the xcat messaging and logging
|
2007-10-26 22:44:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
=head2 Package Dependancies
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use Fcntl qw(:flock);
|
|
|
|
use File::Basename;
|
|
|
|
use File::Find;
|
|
|
|
use File::Path; # Provides mkpath()
|
|
|
|
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#--------------------------------------------------------------------------------
|
|
|
|
|
2008-02-18 15:57:25 +00:00
|
|
|
=head1 Subroutines
|
2007-10-26 22:44:33 +00:00
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
=head3 message
|
|
|
|
|
2008-02-18 15:57:25 +00:00
|
|
|
Display a msg STDOUT,STDERR or return to callback function.
|
2007-10-26 22:44:33 +00:00
|
|
|
If callback routine is provide, the message will be returned to the callback
|
2007-12-11 14:18:45 +00:00
|
|
|
routine.
|
|
|
|
|
|
|
|
If callback routime is not provide, the message is displayed to STDOUT or
|
|
|
|
STDERR.
|
2007-10-26 22:44:33 +00:00
|
|
|
|
2008-02-18 15:57:25 +00:00
|
|
|
|
2007-10-26 22:44:33 +00:00
|
|
|
Arguments:
|
|
|
|
The arguments of the message() function are:
|
|
|
|
|
2008-02-18 15:57:25 +00:00
|
|
|
|
2007-12-11 14:18:45 +00:00
|
|
|
If address of the callback is provided,
|
2008-02-18 15:57:25 +00:00
|
|
|
then the message will be returned either
|
2007-12-11 14:18:45 +00:00
|
|
|
as data to the client's callback routine or to the
|
|
|
|
xcat daemon or Client.pm ( bypass) for display/logging.
|
2008-02-18 15:57:25 +00:00
|
|
|
See flags below.
|
2007-12-11 14:18:45 +00:00
|
|
|
|
|
|
|
If address of the callback is not provide, then
|
2008-02-18 15:57:25 +00:00
|
|
|
the message will be displayed to STDERR or STDOUT or
|
2007-12-11 14:18:45 +00:00
|
|
|
added to SYSLOG. See flags below.
|
|
|
|
|
|
|
|
For compatibility with existing code, the message routine will
|
|
|
|
move the data into the appropriate callback structure, if required.
|
|
|
|
See example below, if the input to the message routine
|
2008-02-18 15:57:25 +00:00
|
|
|
has the "data" structure filled in for an error message, then
|
|
|
|
the message routine will move the $rsp->{data}->[0] to
|
2007-12-11 14:18:45 +00:00
|
|
|
$rsp->{error}->[0]. This will allow xcatd/Client.pm will process
|
|
|
|
all but "data" messages.
|
|
|
|
|
2008-02-18 15:57:25 +00:00
|
|
|
The current client code should not have to change.
|
2007-12-11 14:18:45 +00:00
|
|
|
|
|
|
|
my %rsp;
|
|
|
|
$rsp->{data}->[0] = "Job did not run. \n";
|
|
|
|
xCAT::MsgUtils->message("E", $rsp, $::CALLBACK);
|
|
|
|
|
|
|
|
Here the message routine will move $rsp->{data}->[0] to
|
|
|
|
$rsp->{error}->[0], to match the "E"message code.
|
|
|
|
Note the message
|
2008-02-18 15:57:25 +00:00
|
|
|
routine will only check for the data to either exist in
|
|
|
|
$rsp->{error}->[0] already, or to exist in $rsp->{data}->[0].
|
|
|
|
|
2007-12-11 14:18:45 +00:00
|
|
|
Here's the meaning of the 1st character, if a callback specified:
|
|
|
|
|
2008-02-18 15:57:25 +00:00
|
|
|
D - DATA this is returned to the client callback routine
|
|
|
|
E - error this is displayed/logged by daemon/Client.pm.
|
2007-12-11 14:18:45 +00:00
|
|
|
I - informational this is displayed/logged by daemon/Client.pm.
|
2008-02-18 15:57:25 +00:00
|
|
|
S - Message will be logged to syslog ( severe error)
|
2007-12-11 14:18:45 +00:00
|
|
|
syslog facily (local4) and priority (err) will be used.
|
|
|
|
See /etc/syslog.conf file for the destination of the
|
|
|
|
messages.
|
|
|
|
Note S can be combined with other flags for example
|
|
|
|
SE logs message to syslog to also display the
|
2008-02-18 15:57:25 +00:00
|
|
|
message by daemon/ Client.pm.
|
2007-12-11 14:18:45 +00:00
|
|
|
V - verbose. This flag is not valid, the calling routine
|
|
|
|
should check for verbose mode before calling the message
|
|
|
|
routine and only use the I flag for the message.
|
2008-02-18 15:57:25 +00:00
|
|
|
If V flag is detected, it will be changed to an I flag.
|
|
|
|
W - warning this is displayed/logged by daemon/Client.pm.
|
2007-12-11 14:18:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
Here's the meaning of the 1st character, if no callback specified:
|
|
|
|
|
2008-02-18 15:57:25 +00:00
|
|
|
D - DATA goes to STDOUT
|
2007-12-11 14:18:45 +00:00
|
|
|
E - error. This type of message will be sent to STDERR.
|
2008-02-18 15:57:25 +00:00
|
|
|
I - informational goes to STDOUT
|
|
|
|
S - Message will be logged to syslog ( severe error)
|
2007-12-11 14:18:45 +00:00
|
|
|
Note S can be combined with other flags for example
|
2008-02-18 15:57:25 +00:00
|
|
|
SE logs message to syslog and is sent to STDERR.
|
2007-12-11 14:18:45 +00:00
|
|
|
V - verbose. This flag is not valid, the calling routine
|
|
|
|
should check for verbose mode before calling the message
|
|
|
|
routine and only use the I flag for the message.
|
2008-02-18 15:57:25 +00:00
|
|
|
If V flag is detected, it will be changed to an I flag.
|
|
|
|
W - warning goes to STDOUT.
|
2007-10-26 22:44:33 +00:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
none
|
|
|
|
|
|
|
|
Error:
|
2007-12-11 14:18:45 +00:00
|
|
|
none
|
2007-10-26 22:44:33 +00:00
|
|
|
|
|
|
|
Example:
|
2007-12-11 14:18:45 +00:00
|
|
|
|
2008-02-18 15:57:25 +00:00
|
|
|
Use with no callback
|
2007-10-26 22:44:33 +00:00
|
|
|
xCAT::MsgUtils->message('E', "Operation $value1 failed\n");
|
2007-12-11 14:18:45 +00:00
|
|
|
xCAT::MsgUtils->message('S', "Host $host not responding\n");
|
|
|
|
xCAT::MsgUtils->message('SI', "Host $host not responding\n");
|
|
|
|
|
2008-02-18 15:57:25 +00:00
|
|
|
Use with callback
|
2007-12-11 14:18:45 +00:00
|
|
|
my %rsp;
|
|
|
|
$rsp->{data}->[0] = "Job did not run. \n";
|
|
|
|
xCAT::MsgUtils->message("D", $rsp, $::CALLBACK);
|
|
|
|
|
|
|
|
my %rsp;
|
|
|
|
$rsp->{error}->[0] = "No hosts in node list\n";
|
|
|
|
xCAT::MsgUtils->message("E", $rsp, $::CALLBACK);
|
|
|
|
|
|
|
|
|
|
|
|
my %rsp;
|
|
|
|
$rsp->{info}->[0] = "No hosts in node list\n";
|
|
|
|
xCAT::MsgUtils->message("I", $rsp, $::CALLBACK);
|
|
|
|
|
|
|
|
|
|
|
|
my %rsp;
|
|
|
|
$rsp->{warning}->[0] = "No hosts in node list\n";
|
|
|
|
xCAT::MsgUtils->message("W", $rsp, $::CALLBACK);
|
|
|
|
|
|
|
|
my %rsp;
|
|
|
|
$rsp->{error}->[0] = "Host not responding\n";
|
|
|
|
xCAT::MsgUtils->message("S", $rsp, $::CALLBACK);
|
|
|
|
|
|
|
|
|
|
|
|
my %rsp;
|
|
|
|
$rsp->{error}->[0] = "Host not responding\n";
|
|
|
|
xCAT::MsgUtils->message("SE", $rsp, $::CALLBACK);
|
|
|
|
|
|
|
|
my %rsp;
|
|
|
|
$rsp->{info}->[0] = "Host not responding\n";
|
|
|
|
xCAT::MsgUtils->message("SI", $rsp, $::CALLBACK);
|
2007-10-26 22:44:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
Comments:
|
2008-02-18 15:57:25 +00:00
|
|
|
|
2007-12-11 14:18:45 +00:00
|
|
|
|
2007-10-26 22:44:33 +00:00
|
|
|
Returns:
|
|
|
|
none
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
sub message
|
|
|
|
{
|
|
|
|
|
|
|
|
# Process the arguments
|
|
|
|
shift; # get rid of the class name
|
2007-12-11 14:18:45 +00:00
|
|
|
my $sev = shift;
|
|
|
|
my $rsp = shift;
|
2007-11-26 18:41:46 +00:00
|
|
|
my $call_back = shift; # optional
|
2008-02-18 15:57:25 +00:00
|
|
|
my $exitcode = shift; # optional
|
2007-10-26 22:44:33 +00:00
|
|
|
|
2007-12-11 14:18:45 +00:00
|
|
|
# should be I, D, E, S, W
|
|
|
|
# or S(I, D, E, S, W)
|
2007-10-26 22:44:33 +00:00
|
|
|
|
|
|
|
my $stdouterrf = \*STDOUT;
|
|
|
|
my $stdouterrd = '';
|
2007-12-11 14:18:45 +00:00
|
|
|
if ($sev =~ /[E]/)
|
2007-10-26 22:44:33 +00:00
|
|
|
{
|
|
|
|
$stdouterrf = \*STDERR;
|
|
|
|
$stdouterrd = '1>&2';
|
|
|
|
}
|
|
|
|
|
2007-12-11 14:18:45 +00:00
|
|
|
if ($sev eq 'V')
|
|
|
|
{ # verbose should have been handled in calling routine
|
|
|
|
$sev = "I";
|
|
|
|
}
|
|
|
|
if ($sev eq 'SV')
|
|
|
|
{ # verbose should have been handled in calling routine
|
|
|
|
$sev = "SI";
|
2007-10-26 22:44:33 +00:00
|
|
|
}
|
|
|
|
|
2008-02-18 15:57:25 +00:00
|
|
|
# Check that correct structure is filled in. If the data is not in the
|
2007-12-11 14:18:45 +00:00
|
|
|
# structure corresponding to the $sev, then look for it in "data"
|
2008-02-21 21:10:35 +00:00
|
|
|
#TODO: this is not really right for a few reasons: 1) all the fields in the
|
2008-02-18 15:57:25 +00:00
|
|
|
# response structure are arrays, so can handle multiple lines of text. We
|
|
|
|
# should not just be check the 0th element. 2) a cmd may have both error
|
|
|
|
# text and data text. 3) this message() function should just take in a plain
|
|
|
|
# string and put it in the correct place based on the severity.
|
|
|
|
if ($call_back) { # callback routine provided
|
|
|
|
my $sevkey;
|
2008-02-21 21:10:35 +00:00
|
|
|
if ($sev =~ /D/) { $sevkey = 'data'; }
|
|
|
|
elsif ($sev =~ /I/) { $sevkey = 'info'; }
|
|
|
|
elsif ($sev =~ /W/) { $sevkey = 'warning'; }
|
|
|
|
elsif ($sev =~ /E/) {
|
2008-02-18 15:57:25 +00:00
|
|
|
$sevkey = 'error';
|
|
|
|
if (!defined($exitcode)) { $exitcode = 1; } # default to something non-zero
|
|
|
|
}
|
2008-02-21 21:10:35 +00:00
|
|
|
else { print "Internal Error: Invalid severity passed to MsgUtils::message().\n"; return 1; }
|
|
|
|
|
|
|
|
if ($sevkey ne 'data') {
|
2008-02-18 15:57:25 +00:00
|
|
|
if (!defined ($rsp->{$sevkey}) || !scalar(@{$rsp->{$sevkey}})) { # did not pass the text in in the severity-specific field
|
|
|
|
if (defined ($rsp->{data}) && scalar(@{$rsp->{data}})) {
|
2008-02-21 21:10:35 +00:00
|
|
|
push @{$rsp->{$sevkey}}, @{$rsp->{data}}; # assume they passed in the text in the data field instead
|
|
|
|
@{$rsp->{data}} = (); # clear out the data field
|
2008-02-18 15:57:25 +00:00
|
|
|
}
|
2007-12-11 14:18:45 +00:00
|
|
|
}
|
2008-02-18 15:57:25 +00:00
|
|
|
}
|
|
|
|
if (!defined ($rsp->{$sevkey}) || !scalar(@{$rsp->{$sevkey}})) { return; } # if still nothing in the array, there is nothing to print out
|
2007-10-26 22:44:33 +00:00
|
|
|
|
2008-02-21 21:10:35 +00:00
|
|
|
if ($sev ne 'S') { # if sev is anything but only-syslog, print the msg
|
|
|
|
if ($exitcode) { push @{$rsp->{errorcode}}, $exitcode; }
|
2008-02-18 15:57:25 +00:00
|
|
|
$call_back->($rsp); # send message to daemon/Client.pm
|
2008-02-21 21:10:35 +00:00
|
|
|
@{$rsp->{$sevkey}} = (); # clear out the rsp structure in case they use it again
|
|
|
|
@{$rsp->{data}} = ();
|
|
|
|
@{$rsp->{errorcode}} = ();
|
2007-12-11 14:18:45 +00:00
|
|
|
}
|
2008-02-21 21:10:35 +00:00
|
|
|
# No need to support syslog writing when they specified a callback
|
2007-10-26 22:44:33 +00:00
|
|
|
}
|
2008-02-21 21:10:35 +00:00
|
|
|
|
2007-12-11 14:18:45 +00:00
|
|
|
else # no callback provided
|
2007-10-26 22:44:33 +00:00
|
|
|
{
|
2008-02-21 21:10:35 +00:00
|
|
|
if ($sev ne 'S') # it is not syslog only
|
2007-12-11 14:18:45 +00:00
|
|
|
{
|
|
|
|
print $stdouterrf $rsp; # print the message
|
|
|
|
}
|
2008-02-21 21:10:35 +00:00
|
|
|
if ($sev =~ /S/) {
|
|
|
|
# If they want this msg to also go to syslog, do that now
|
|
|
|
openlog("xCAT", '', 'local4');
|
|
|
|
syslog("err", $rsp);
|
|
|
|
closelog();
|
|
|
|
}
|
2007-10-26 22:44:33 +00:00
|
|
|
}
|
2008-02-21 21:10:35 +00:00
|
|
|
return;
|
2007-10-26 22:44:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|
|
|
|
|