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;
|
|
|
|
|
2009-08-03 13:07:29 +00:00
|
|
|
# if AIX - make sure we include perl 5.8.2 in INC path.
|
|
|
|
# Needed to find perl dependencies shipped in deps tarball.
|
2012-08-08 17:27:50 +00:00
|
|
|
if ($^O =~ /^aix/i) {
|
|
|
|
unshift(@INC, qw(/usr/opt/perl5/lib/5.8.2/aix-thread-multi /usr/opt/perl5/lib/5.8.2 /usr/opt/perl5/lib/site_perl/5.8.2/aix-thread-multi /usr/opt/perl5/lib/site_perl/5.8.2));
|
2009-08-03 13:07:29 +00:00
|
|
|
}
|
|
|
|
|
2007-10-26 22:44:33 +00:00
|
|
|
use strict;
|
2008-08-12 17:21:08 +00:00
|
|
|
use Sys::Syslog qw (:DEFAULT setlogsock);
|
2010-04-13 13:22:53 +00:00
|
|
|
use xCAT::Utils;
|
2008-05-07 11:34:17 +00:00
|
|
|
#use locale;
|
2007-10-26 22:44:33 +00:00
|
|
|
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
|
|
|
|
|
2009-01-07 14:07:21 +00:00
|
|
|
Display a msg STDOUT,STDERR,log a msg and/or return to callback function.
|
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.
|
2009-01-07 14:07:21 +00:00
|
|
|
|
|
|
|
If logging (L) is requested, the message structure
|
|
|
|
must be a simple string. The routine will convert
|
|
|
|
it to the appropriate callback structure, if a callback
|
|
|
|
is provided.
|
|
|
|
Note for logging xCAT:MsgUtils->start_logging and
|
|
|
|
xCAT:MstUtils->stop_logging must be used to
|
|
|
|
open and close the log.
|
2007-12-11 14:18:45 +00:00
|
|
|
|
|
|
|
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
|
2009-02-05 18:56:50 +00:00
|
|
|
N - Node Data this is returned to the client callback routine
|
2008-02-18 15:57:25 +00:00
|
|
|
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.
|
2009-01-07 14:07:21 +00:00
|
|
|
L - Log error to xCAT Log on the local machine.
|
|
|
|
Routine must have setup log by calling
|
|
|
|
MsgUtils->start_log routine which returns
|
|
|
|
$::LOG_FILE_HANDLE. Log is closed with
|
|
|
|
MsgUtils->stop_log routine. Note can be combined with
|
|
|
|
other flags:
|
|
|
|
LS - Log to xCAT Log and Syslog
|
|
|
|
LSE/LSI - Log to xCAT Log and Syslog and display
|
|
|
|
if this option is used the message must be a simple
|
|
|
|
string. The message routine will format for callback
|
|
|
|
based on the (D,E,I,W) flag.
|
|
|
|
|
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
|
2009-02-05 18:56:50 +00:00
|
|
|
N - Node informational goes to STDOUT
|
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
|
|
|
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.
|
2010-03-03 18:26:57 +00:00
|
|
|
SA logs message to syslog and to the auditlog DB table
|
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
|
2009-01-07 14:07:21 +00:00
|
|
|
|
2007-12-11 14:18:45 +00:00
|
|
|
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.
|
2009-01-07 14:07:21 +00:00
|
|
|
L - log goes to /var/log/xcat/<logname>
|
|
|
|
( see MsgUtils->start_log)
|
|
|
|
Routine must have setup log by calling
|
|
|
|
MsgUtils->start_log routine which returns
|
|
|
|
$::LOG_FILE_HANDLE. Log is closed with
|
|
|
|
MsgUtils->stop_log routine. Note can be combined with
|
|
|
|
other flags:
|
|
|
|
LS - Log to xCAT Log and Syslog
|
|
|
|
LSE/LSI - Log to xCAT Log and Syslog and display
|
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
|
2009-01-07 14:07:21 +00:00
|
|
|
# Message to STDOUT
|
|
|
|
xCAT::MsgUtils->message('I', "Operation $value1 succeeded\n");
|
2009-02-05 18:56:50 +00:00
|
|
|
xCAT::MsgUtils->message('N', "Node:$node failed\n");
|
2010-03-03 18:26:57 +00:00
|
|
|
|
2009-01-07 14:07:21 +00:00
|
|
|
# Message to STDERR
|
2007-10-26 22:44:33 +00:00
|
|
|
xCAT::MsgUtils->message('E', "Operation $value1 failed\n");
|
2010-03-03 18:26:57 +00:00
|
|
|
|
2009-01-07 14:07:21 +00:00
|
|
|
# Message to Syslog
|
2007-12-11 14:18:45 +00:00
|
|
|
xCAT::MsgUtils->message('S', "Host $host not responding\n");
|
2010-03-03 18:26:57 +00:00
|
|
|
|
|
|
|
# Message to Syslog and auditlog table
|
|
|
|
# see tabdump -d auditlog
|
|
|
|
my $rsp = {};
|
|
|
|
$rsp->{syslogdata}->[0] = "$host not responding\n"; # for syslog
|
|
|
|
# the next data is for auditlog table, audittime added below
|
|
|
|
$rsp->{userid} ->[0] = $user;
|
|
|
|
$rsp->{clientname} -> [0] = $client;
|
|
|
|
$rsp->{clienttype} -> [0] = $clienttype;
|
|
|
|
$rsp->{command} -> [0] = $command;
|
|
|
|
$rsp->{noderange} -> [0] = $noderange;
|
|
|
|
$rsp->{args} -> [0] = $arguments;
|
|
|
|
$rsp->{status} -> [0] = $status;
|
|
|
|
xCAT::MsgUtils->message('SA', $rsp);
|
|
|
|
|
2009-01-07 14:07:21 +00:00
|
|
|
# Message to Log and Syslog
|
|
|
|
xCAT::MsgUtils->message('LS', "Host $host not responding\n");
|
2010-03-03 18:26:57 +00:00
|
|
|
|
2009-01-07 14:07:21 +00:00
|
|
|
# Message to Log
|
|
|
|
xCAT::MsgUtils->message('L', "Host $host not responding\n");
|
2007-12-11 14:18:45 +00:00
|
|
|
|
2008-02-18 15:57:25 +00:00
|
|
|
Use with callback
|
2009-01-07 14:07:21 +00:00
|
|
|
# Message to callback
|
2009-02-05 18:56:50 +00:00
|
|
|
my $rsp = {};
|
2007-12-11 14:18:45 +00:00
|
|
|
$rsp->{data}->[0] = "Job did not run. \n";
|
|
|
|
xCAT::MsgUtils->message("D", $rsp, $::CALLBACK);
|
|
|
|
|
2009-02-05 18:56:50 +00:00
|
|
|
my $rsp = {};
|
2007-12-11 14:18:45 +00:00
|
|
|
$rsp->{error}->[0] = "No hosts in node list\n";
|
|
|
|
xCAT::MsgUtils->message("E", $rsp, $::CALLBACK);
|
|
|
|
|
2009-02-05 18:56:50 +00:00
|
|
|
my $rsp = {};
|
|
|
|
$rsp->{node}->[0]->{name}->[0] ="mynode";
|
|
|
|
$rsp->{node}->[0]->{data}->[0] ="mydata";
|
|
|
|
xCAT::MsgUtils->message("N", $rsp, $callback);
|
2007-12-11 14:18:45 +00:00
|
|
|
|
2009-02-05 18:56:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
my $rsp = {};
|
2007-12-11 14:18:45 +00:00
|
|
|
$rsp->{info}->[0] = "No hosts in node list\n";
|
|
|
|
xCAT::MsgUtils->message("I", $rsp, $::CALLBACK);
|
|
|
|
|
|
|
|
|
2009-02-05 18:56:50 +00:00
|
|
|
my $rsp = {};
|
2007-12-11 14:18:45 +00:00
|
|
|
$rsp->{warning}->[0] = "No hosts in node list\n";
|
|
|
|
xCAT::MsgUtils->message("W", $rsp, $::CALLBACK);
|
|
|
|
|
2009-02-05 18:56:50 +00:00
|
|
|
my $rsp = {};
|
2007-12-11 14:18:45 +00:00
|
|
|
$rsp->{error}->[0] = "Host not responding\n";
|
|
|
|
xCAT::MsgUtils->message("S", $rsp, $::CALLBACK);
|
|
|
|
|
|
|
|
|
2009-01-07 14:07:21 +00:00
|
|
|
# Message to Syslog and callback
|
2009-02-05 18:56:50 +00:00
|
|
|
my $rsp = {};
|
2007-12-11 14:18:45 +00:00
|
|
|
$rsp->{error}->[0] = "Host not responding\n";
|
|
|
|
xCAT::MsgUtils->message("SE", $rsp, $::CALLBACK);
|
|
|
|
|
2009-01-07 14:07:21 +00:00
|
|
|
# Message to Syslog and callback
|
2009-02-05 18:56:50 +00:00
|
|
|
my $rsp = {};
|
2007-12-11 14:18:45 +00:00
|
|
|
$rsp->{info}->[0] = "Host not responding\n";
|
|
|
|
xCAT::MsgUtils->message("SI", $rsp, $::CALLBACK);
|
2007-10-26 22:44:33 +00:00
|
|
|
|
2009-01-07 14:07:21 +00:00
|
|
|
# Message to Log, Syslog and callback
|
|
|
|
my $msg;
|
|
|
|
$msg = "Host not responding\n";
|
|
|
|
xCAT::MsgUtils->message("LSI", $msg, $::CALLBACK);
|
|
|
|
|
|
|
|
# Message to Log and callback
|
|
|
|
my $msg;
|
|
|
|
$msg = "Host not responding\n";
|
|
|
|
xCAT::MsgUtils->message("LI", $msg, $::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:
|
2009-01-07 14:07:21 +00:00
|
|
|
1 for internal error ( invalid input to the routine)
|
2007-10-26 22:44:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=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
|
|
|
|
2010-03-03 18:26:57 +00:00
|
|
|
# should be I, D, E, S, SA ,LS, W , L,N
|
2009-02-05 18:56:50 +00:00
|
|
|
# or S(I, D, E, S, W, L,N)
|
2010-03-17 16:41:42 +00:00
|
|
|
#
|
|
|
|
# if new SA option need to split syslog messages from auditlog entry
|
|
|
|
#
|
|
|
|
my $newrsp;
|
|
|
|
if ($sev eq 'SA')
|
|
|
|
{ # if SA then need to pull first entry from $rsp
|
|
|
|
# for syslog, to preserve old interface
|
|
|
|
$newrsp = $rsp;
|
|
|
|
$rsp = $newrsp->{syslogdata}->[0];
|
|
|
|
}
|
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';
|
|
|
|
}
|
|
|
|
|
2009-01-07 14:07:21 +00:00
|
|
|
# check for logging
|
|
|
|
my $logging = 0;
|
|
|
|
if ($sev =~ /[L]/)
|
|
|
|
{
|
|
|
|
|
|
|
|
# no log opened, we have an error
|
|
|
|
if (!defined($::LOG_FILE_HANDLE))
|
|
|
|
{
|
|
|
|
if ($call_back)
|
|
|
|
{
|
|
|
|
|
|
|
|
# build callback structure
|
|
|
|
my $newrsp;
|
|
|
|
my $sevkey = 'error';
|
2010-03-03 18:26:57 +00:00
|
|
|
my $err =
|
2009-01-07 14:07:21 +00:00
|
|
|
"Logging requested without setting up log by calling xCAT:MsgUtils->start_logging.\n";
|
|
|
|
push @{$newrsp->{$sevkey}}, $err;
|
|
|
|
push @{$newrsp->{errorcode}}, "1";
|
|
|
|
$call_back->($newrsp); # send message to daemon/Client.pm
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
print
|
|
|
|
"Logging requested without setting up log by calling xCAT:MsgUtils->start_logging.\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
|
|
|
|
$logging = 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2009-01-07 14:07:21 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# if a callback routine is provided
|
|
|
|
#
|
2008-04-08 19:03:46 +00:00
|
|
|
if ($call_back)
|
|
|
|
{ # callback routine provided
|
|
|
|
my $sevkey;
|
|
|
|
if ($sev =~ /D/) { $sevkey = 'data'; }
|
2009-02-05 18:56:50 +00:00
|
|
|
elsif ($sev =~ /N/) { $sevkey = 'node'; }
|
2008-04-08 19:03:46 +00:00
|
|
|
elsif ($sev =~ /I/) { $sevkey = 'info'; }
|
|
|
|
elsif ($sev =~ /W/) { $sevkey = 'warning'; }
|
|
|
|
elsif ($sev =~ /E/)
|
|
|
|
{
|
|
|
|
$sevkey = 'error';
|
|
|
|
if (!defined($exitcode))
|
|
|
|
{
|
|
|
|
$exitcode = 1;
|
|
|
|
} # default to something non-zero
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-01-07 14:07:21 +00:00
|
|
|
|
|
|
|
# build callback structure
|
|
|
|
my $newrsp;
|
|
|
|
my $sevkey = 'error';
|
2010-03-03 18:26:57 +00:00
|
|
|
my $err =
|
2009-01-07 14:07:21 +00:00
|
|
|
"Invalid or no severity code passed to MsgUtils::message().\n";
|
|
|
|
push @{$newrsp->{$sevkey}}, $err;
|
|
|
|
push @{$newrsp->{errorcode}}, "1";
|
|
|
|
$call_back->($newrsp); # send message to daemon/Client.pm
|
2008-04-08 19:03:46 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-01-07 14:07:21 +00:00
|
|
|
# check if logging to xCAT log, must be handled
|
|
|
|
# separately because message data is a simple string
|
|
|
|
#
|
|
|
|
if (!$logging)
|
2008-04-08 19:03:46 +00:00
|
|
|
{
|
2009-01-07 14:07:21 +00:00
|
|
|
if ($sevkey ne 'data')
|
|
|
|
{
|
|
|
|
if (!defined($rsp->{$sevkey}) || !scalar(@{$rsp->{$sevkey}}))
|
|
|
|
{ # did not pass the text in in the severity-specific field
|
|
|
|
# so fix it
|
|
|
|
if (defined($rsp->{data}) && scalar(@{$rsp->{data}}))
|
|
|
|
{
|
|
|
|
push @{$rsp->{$sevkey}}, @{$rsp->{data}};
|
|
|
|
|
|
|
|
# assume they passed
|
|
|
|
# in the text in the data field instead
|
|
|
|
@{$rsp->{data}} = (); # clear out the data field
|
|
|
|
}
|
2008-04-08 19:03:46 +00:00
|
|
|
}
|
2007-12-11 14:18:45 +00:00
|
|
|
}
|
2008-04-08 19:03:46 +00:00
|
|
|
|
2009-01-07 14:07:21 +00:00
|
|
|
# if still nothing in the array, there is nothing to print out
|
|
|
|
if (!defined($rsp->{$sevkey}) || !scalar(@{$rsp->{$sevkey}}))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($exitcode)
|
|
|
|
{
|
|
|
|
push @{$rsp->{errorcode}}, $exitcode;
|
|
|
|
}
|
2008-04-08 19:03:46 +00:00
|
|
|
$call_back->($rsp); # send message to daemon/Client.pm
|
|
|
|
@{$rsp->{$sevkey}} =
|
|
|
|
(); # clear out the rsp structure in case they use it again
|
|
|
|
@{$rsp->{data}} = ();
|
2008-02-21 21:10:35 +00:00
|
|
|
@{$rsp->{errorcode}} = ();
|
2007-12-11 14:18:45 +00:00
|
|
|
}
|
2009-01-07 14:07:21 +00:00
|
|
|
else # logging
|
|
|
|
{
|
|
|
|
|
|
|
|
# write to log
|
|
|
|
print $::LOG_FILE_HANDLE $rsp;
|
|
|
|
|
|
|
|
# build callback structure
|
|
|
|
my $newrsp;
|
|
|
|
push @{$newrsp->{$sevkey}}, $rsp;
|
|
|
|
if ($exitcode)
|
|
|
|
{
|
|
|
|
push @{$newrsp->{errorcode}}, $exitcode;
|
|
|
|
}
|
|
|
|
$call_back->($newrsp); # send message to daemon/Client.pm
|
2008-04-08 19:03:46 +00:00
|
|
|
|
2009-01-07 14:07:21 +00:00
|
|
|
}
|
2007-10-26 22:44:33 +00:00
|
|
|
}
|
2008-02-21 21:10:35 +00:00
|
|
|
|
2009-01-07 14:07:21 +00:00
|
|
|
else # no callback provided
|
2007-10-26 22:44:33 +00:00
|
|
|
{
|
2009-01-07 14:07:21 +00:00
|
|
|
if ($logging)
|
|
|
|
{ # print to local xcat log
|
|
|
|
print $::LOG_FILE_HANDLE $rsp;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ # print to stdout
|
|
|
|
|
2010-03-03 18:26:57 +00:00
|
|
|
print $stdouterrf $rsp . "\n"; # print the message
|
2007-12-11 14:18:45 +00:00
|
|
|
}
|
2009-01-07 14:07:21 +00:00
|
|
|
}
|
2008-04-08 19:03:46 +00:00
|
|
|
|
2009-01-07 14:07:21 +00:00
|
|
|
# is syslog requested
|
2010-03-03 18:26:57 +00:00
|
|
|
|
2009-01-07 14:07:21 +00:00
|
|
|
if ($sev =~ /S/)
|
|
|
|
{
|
|
|
|
|
|
|
|
# If they want this msg to also go to syslog, do that now
|
|
|
|
eval {
|
|
|
|
openlog("xCAT", '', 'local4');
|
|
|
|
setlogsock(["tcp", "unix", "stream"]);
|
2010-09-24 11:48:46 +00:00
|
|
|
if ($sev eq 'SE') {
|
|
|
|
syslog("err", $rsp);
|
|
|
|
} else {
|
|
|
|
syslog("info", $rsp);
|
|
|
|
}
|
2009-01-07 14:07:21 +00:00
|
|
|
closelog();
|
|
|
|
};
|
|
|
|
my $errstr = $@;
|
|
|
|
if ($errstr)
|
|
|
|
{
|
|
|
|
print $stdouterrf
|
|
|
|
"Unable to log $rsp to syslog because of $errstr\n";
|
2008-04-08 19:03:46 +00:00
|
|
|
}
|
2007-10-26 22:44:33 +00:00
|
|
|
}
|
2010-03-03 18:26:57 +00:00
|
|
|
|
2010-03-10 18:57:42 +00:00
|
|
|
# if write to auditlog table requested, if not on service node
|
|
|
|
if (xCAT::Utils->isMN()){
|
|
|
|
if ($sev eq 'SA')
|
|
|
|
{
|
2011-03-31 16:00:04 +00:00
|
|
|
require xCAT::Table;
|
2010-03-03 18:26:57 +00:00
|
|
|
my $auditlogentry;
|
2010-04-06 14:48:26 +00:00
|
|
|
my $tab = xCAT::Table->new("auditlog");
|
2010-03-03 18:26:57 +00:00
|
|
|
if ($tab)
|
|
|
|
{
|
|
|
|
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) =
|
|
|
|
localtime(time);
|
2011-03-29 14:55:43 +00:00
|
|
|
|
|
|
|
my $currtime = sprintf("%04d-%02d-%02d %02d:%02d:%02d",
|
|
|
|
$year + 1900, $mon + 1, $mday,
|
|
|
|
$hour, $min, $sec);
|
2010-03-03 18:26:57 +00:00
|
|
|
|
|
|
|
$auditlogentry->{audittime} = $currtime;
|
|
|
|
$auditlogentry->{userid} = $newrsp->{userid}->[0];
|
|
|
|
$auditlogentry->{clientname} = $newrsp->{clientname}->[0];
|
|
|
|
$auditlogentry->{clienttype} = $newrsp->{clienttype}->[0];
|
|
|
|
$auditlogentry->{command} = $newrsp->{command}->[0];
|
|
|
|
$auditlogentry->{noderange} = $newrsp->{noderange}->[0];
|
|
|
|
$auditlogentry->{args} = $newrsp->{args}->[0];
|
|
|
|
$auditlogentry->{status} = $newrsp->{status}->[0];
|
|
|
|
|
|
|
|
my @ret = $tab->setAttribs(undef, $auditlogentry);
|
|
|
|
if (@ret > 1)
|
|
|
|
{
|
|
|
|
print $stdouterrf "Unable to open auditlog\n";
|
|
|
|
eval {
|
|
|
|
openlog("xCAT", '', 'local4');
|
|
|
|
setlogsock(["tcp", "unix", "stream"]);
|
|
|
|
syslog("err", "Unable to write to auditlog");
|
|
|
|
closelog();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-04-13 13:22:53 +00:00
|
|
|
my $DBname = xCAT::Utils->get_DBName;
|
|
|
|
if (!($DBname =~ /^SQLITE/)) {
|
|
|
|
$tab->commit;
|
|
|
|
}
|
2010-03-03 18:26:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ # error
|
|
|
|
print $stdouterrf "Unable to open auditlog\n";
|
|
|
|
eval {
|
|
|
|
openlog("xCAT", '', 'local4');
|
|
|
|
setlogsock(["tcp", "unix", "stream"]);
|
|
|
|
syslog("err", "Unable to open auditlog");
|
|
|
|
closelog();
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
2010-03-10 18:57:42 +00:00
|
|
|
}
|
2010-03-03 18:26:57 +00:00
|
|
|
}
|
2008-02-21 21:10:35 +00:00
|
|
|
return;
|
2007-10-26 22:44:33 +00:00
|
|
|
}
|
|
|
|
|
2009-01-07 14:07:21 +00:00
|
|
|
#--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
=head2 xCAT Logging Routines
|
|
|
|
To use xCAT Logging follow the following sample
|
|
|
|
|
|
|
|
my $rc=xCAT::MsgUtils->start_logging("mylogname"); # create/open log
|
|
|
|
.
|
|
|
|
.
|
|
|
|
.
|
|
|
|
# Message to Log and callback
|
|
|
|
my $msg;
|
|
|
|
$msg = "Host not responding\n";
|
|
|
|
xCAT::MsgUtils->message("LI", $msg, $::CALLBACK);
|
|
|
|
.
|
|
|
|
.
|
|
|
|
# Message to Log
|
|
|
|
my $msg;
|
|
|
|
$msg = "Host not responding\n";
|
|
|
|
xCAT::MsgUtils->message("L", $msg);
|
|
|
|
.
|
|
|
|
|
|
|
|
my $rc=xCAT::MsgUtils->stop_logging(); # close log
|
|
|
|
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
=head3 start_logging
|
|
|
|
|
|
|
|
Start logging messages to a logfile. Return the log file handle so it
|
|
|
|
can be used for updates and to close the file when done logging
|
|
|
|
using stop_logging.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
$logfilename ( just name, path is by default /var/log/xcat)
|
|
|
|
Returns:
|
|
|
|
$::LOG_FILE_HANDLE
|
|
|
|
Globals:
|
|
|
|
$::LOG_FILE_HANDLE
|
|
|
|
Error:
|
|
|
|
$::NOK
|
|
|
|
Example:
|
|
|
|
xCAT:Utils->start_logging("logname");
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
sub start_logging
|
|
|
|
{
|
|
|
|
my ($class, $logfilename) = @_;
|
|
|
|
my ($cmd, $rc);
|
|
|
|
my $xCATLogDir = "/var/log/xcat/";
|
|
|
|
|
|
|
|
my $logfile = $xCATLogDir;
|
|
|
|
$logfile .= $logfilename;
|
|
|
|
xCAT::MsgUtils->backup_logfile($logfile);
|
|
|
|
|
|
|
|
# create the log directory if it's not already there
|
|
|
|
if (!-d $xCATLogDir)
|
|
|
|
{
|
|
|
|
$cmd = "mkdir -m 644 -p $xCATLogDir";
|
|
|
|
$rc = system("$cmd");
|
|
|
|
if ($rc >> 8)
|
|
|
|
{
|
|
|
|
xCAT::MsgUtils->message('SE', "Error running $cmd.\n");
|
|
|
|
return ($::NOK);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# open the log file
|
|
|
|
unless (open(LOGFILE, ">>$logfile"))
|
|
|
|
{
|
|
|
|
|
|
|
|
# Cannot open file
|
|
|
|
xCAT::MsgUtils->message('SE', "Error opening $logfile.\n");
|
|
|
|
return ($::NOK);
|
|
|
|
}
|
|
|
|
|
|
|
|
$::LOG_FILE_HANDLE = \*LOGFILE;
|
|
|
|
$::LOG_FILE_NAME = $logfile;
|
|
|
|
|
2011-12-04 13:04:58 +00:00
|
|
|
# Make the file to be unbuffered
|
|
|
|
binmode( $::LOG_FILE_HANDLE, ":unix" );
|
2009-01-07 14:07:21 +00:00
|
|
|
# Print the program name and date to the top of the logfile
|
|
|
|
my $sdate = `/bin/date`;
|
|
|
|
chomp $sdate;
|
|
|
|
my $program = $0;
|
|
|
|
xCAT::MsgUtils->message('L', "$program:logging started $sdate.\n");
|
|
|
|
|
|
|
|
return ($::LOG_FILE_HANDLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
#--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
=head3 stop_logging
|
|
|
|
|
|
|
|
Turn off message logging. Routine expects to have a file handle
|
|
|
|
passed in via the global $::LOG_FILE_HANDLE.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
$::OK
|
|
|
|
Globals:
|
|
|
|
$::LOG_FILE_HANDLE
|
|
|
|
Error:
|
|
|
|
none
|
|
|
|
Example:
|
|
|
|
MsgUtils->stop_logging();
|
|
|
|
Comments:
|
|
|
|
closes the logfile and undefines $::LOG_FILE_HANDLE
|
|
|
|
even on error.
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
sub stop_logging
|
|
|
|
{
|
|
|
|
my ($class) = @_;
|
|
|
|
if (defined($::LOG_FILE_HANDLE))
|
|
|
|
{
|
|
|
|
|
|
|
|
# Print the date at the bottom of the logfile
|
|
|
|
my $sdate = `/bin/date`;
|
|
|
|
chomp $sdate;
|
|
|
|
my $program = $0;
|
|
|
|
xCAT::MsgUtils->message('L', "$program:logging stopped $sdate.\n");
|
|
|
|
|
|
|
|
close($::LOG_FILE_HANDLE);
|
|
|
|
undef $::LOG_FILE_HANDLE;
|
|
|
|
}
|
|
|
|
return $::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
=head3 backup_logfile
|
|
|
|
|
|
|
|
Backup the current logfile. Move logfile to logfile.1.
|
|
|
|
Shift all other logfiles
|
|
|
|
(logfile.[1-3]) up one number. The original logfile.4 is removed as in a FIFO.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
$logfile ( full path)
|
|
|
|
Returns:
|
|
|
|
$::OK
|
|
|
|
Error:
|
|
|
|
undefined
|
|
|
|
Example:
|
|
|
|
xCAT::MsgUtils->backup_logfile($logfile);
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
sub backup_logfile
|
|
|
|
{
|
|
|
|
my ($class, $logfile) = @_;
|
|
|
|
|
|
|
|
my ($logfile1) = $logfile . ".1";
|
|
|
|
my ($logfile2) = $logfile . ".2";
|
|
|
|
my ($logfile3) = $logfile . ".3";
|
|
|
|
my ($logfile4) = $logfile . ".4";
|
|
|
|
|
|
|
|
if (-f $logfile)
|
|
|
|
{
|
|
|
|
rename($logfile3, $logfile4) if (-f $logfile3);
|
|
|
|
rename($logfile2, $logfile3) if (-f $logfile2);
|
|
|
|
rename($logfile1, $logfile2) if (-f $logfile1);
|
|
|
|
rename($logfile, $logfile1);
|
|
|
|
}
|
|
|
|
return $::OK;
|
|
|
|
}
|
|
|
|
|
2012-06-21 09:43:16 +00:00
|
|
|
sub verbose_message
|
|
|
|
{
|
|
|
|
shift;
|
|
|
|
my $req = shift;
|
|
|
|
my $data = shift;
|
|
|
|
if (!defined($req->{verbose})) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
my ($sec,$min,$hour,$mday,$mon,$yr,$wday,$yday,$dst) = localtime(time);
|
|
|
|
my $time = sprintf "%04d%02d%02d.%02d:%02d:%02d", $yr+1900,$mon+1,$mday,$hour,$min,$sec;
|
|
|
|
$data = "$time ($$) ".$data;
|
|
|
|
if (defined($req->{callback})) {
|
|
|
|
my %rsp;
|
|
|
|
$rsp{data} = [$data];
|
|
|
|
xCAT::MsgUtils->message("I", \%rsp, $req->{callback});
|
|
|
|
} else {
|
|
|
|
xCAT::MsgUtils->message("I", $data);
|
|
|
|
}
|
|
|
|
}
|
2007-10-26 22:44:33 +00:00
|
|
|
1;
|
|
|
|
|