Good morning all....
I have been learning Perl for about 2 months now and I guess I am getting there as much as I can however I am really stuck. I have a Perl script called postEvent.pl which uses a package called event.pm. PostEvent.pl depends on a meithod inside event.pm called isSuccess to tell the user if the message sent was a success.
For some reason I keep getting....
Problems sending message to Tivoli
Which comes from postEvent.pl code logic here...
if ($event->isSuccess()) {
&info("Message sent to Tivoli, $flags{'m'}");
} else {
&error("Problems sending message to Tivoli, ", $event->getStatusMsg);
}
The problem is that the message is in fact a success, for some reason its just not getting the message that it was a success from the event.pm package and I am not sure why. I am listed both sets of source code here in hopes that somebody can tell me what I am doing wrong and show me. I have been stuck on this for 2 days now ;-(
Thank you.
Here is the source for postEvent.pl
#!/lcl/bin/perl -d
use Env;
use strict;
use Sys::Hostname;
use Getopt::Std;
my ($rtn, $help);
my %flags = ('t' => '', # Event class
's' => '', # Severity
'h' => hostname(), # hostname
'p' => '', # Program
'b' => '', # Business System
'm' => '', # Message
'g' => '', # on-call group
'i' => '', # instructions
'n' => 'Shell_API',
'c' => '', # confirm an event with an email to the on-call team.
'd' => '', # debug mode
'u' => '', # specify a specific URL to post events
'?' => '' # help
);
getopts('t:s:h:p:b:m:g:i:n:c:d:u', \%flags);
## Make sure GFS::Event is setup on this system
##
my $rtn = eval { require GFS::Event };
my $event = eval { return GFS::Event->new() };
if ($flags{'u'}) { $event->setURL($flags{'u'}); }
if ($help) { help();exit; }
&debug("Command line parameters: ", join (",", @ARGV)) if $flags{'d'};
&debug("Beginning $0, ", join (",", @ARGV)) if $flags{'d'};
my $event = GFS::Event->new();
if (! defined $event) {
&error("Could not create event object, $@");
}
if ($flags{'c'}) { $event->setConfirmEventOn(); }
$event->postEvent(
errorType => $flags{'t'},
errorSeverity => $flags{'s'},
errorGroup => $flags{'g'},
errorProgram => $flags{'p'},
errorSystem => $flags{'b'},
errorMessage => "$flags{'m'}",
errorHost => $flags{'h'},
errorInstructions => $flags{'i'},
errorNumber => "$flags{'n'}",
errorSubsource => "$0",
errorSource => "$0"
);
if ($event->isSuccess()) {
&info("Message sent to Tivoli, $flags{'m'}");
} else {
&error("Problems sending message to Tivoli, ", $event->getStatusMsg);
}
sub help {
my $usage = "Mandatory, these command line options must be specified.\n";
$usage .= "\t-t <Error Type>\n";
$usage .= "\t-s <Severity Code>\n";
$usage .= "\t-g <On-Call Group>\n";
$usage .= "\t-m <Message>\n\n";
$usage .= "\t-i <Operator's Instruction>\n";
$usage .= "\t-n <errorNumber>\n";
$usage .= "\t-c, no parameter. If specified, an email notification will be generated in addition to the Tivoli post.\n";
$usage .= "\t-p <Program>\n";
$usage .= "\t-b <Business_System>\n";
$usage .= "\t-d, no parameter. If specified, set log level to debug.\n";
&info($usage);
return;
}
sub debug {
if (! $flags{'d'}) { return }
my $field;
print STDOUT "[" . localtime() . "] [DEBUG] ";
foreach $field (@_) { print STDOUT $field }
print "\n";
}
sub info {
my $field;
print STDOUT "[" . localtime() . "] [INFO] ";
foreach $field (@_) { print STDOUT $field }
print "\n";
}
sub warn {
my $field;
print STDOUT "[" . localtime() . "] [WARN] ";
foreach $field (@_) { print STDOUT $field }
print "\n";
}
sub error {
my $field;
print STDOUT "[" . localtime() . "] [ERROR] ";
foreach $field (@_) { print STDOUT $field }
print "\n";
exit 5;
}
Here is the source for event.pm
package GFS::Event;
our $VERSION = '1.1.0';
## Core
use strict;
use Sys::Hostname;
use Date::Format;
## Location of postzmsg
use constant POSTZ_HOME => "/lcl/apps/esm/bin";
## Config file for postzmsg
use constant POSTZ_CONFIG => "/lcl/apps/esm/bin/eif.conf";
sub new {
my $this = {};
my $proto = shift;
my $class = ref($proto) || $proto;
bless $this, $class;
my %args = @_;
$this->{'STATUS'} = "";
$this->{'POSTZ_HOME'} = $args{'postz'} || POSTZ_HOME;
$this->{'POSTZ_CONFIG'} = $args{'postz_config'} || POSTZ_CONFIG;
return $this;
}
sub postEvent {
my $this = shift;
my %parms = @_;
my $severity = $parms{'errorSeverity'} || $parms{'err_severity'} || '';
my $message = $parms{'errorMessage'} || $parms{'err_msg'} || '';
# Removes all front white space
$message =~ s/^\s+//;
# Replaces sequence of white space with a single space
$message =~ s/\s+/ /g;
# Removes all end white space
$message =~ s/\s+$//;
# Remove carriage returns anywhere in the string
$message =~ s/^M$//;
# Remove Line Feeds
$message =~ s/\n//g;
my $type = $parms{'errorType'} || $parms{'err_type'} || '';
my $source = $parms{'errorSource'} || $parms{'err_source'} || '';
my $group = $parms{'errorGroup'} || $parms{'err_group'} || '';
my $instructions = $parms{'errorInstructions'} || $parms{'err_inst'};
if (! $instructions) { $instructions= "Please call ${group} as appropriate for severity (${severity})."; }
# Removes all front white space
$instructions =~ s/^\s+//;
# Replaces sequence of white space with a single space
$instructions =~ s/\s+/ /g;
# Removes all end white space
$instructions =~ s/\s+$//;
# Remove carriage returns anywhere in the string
$instructions =~ s/^M$//;
# Remove Line Feeds
$instructions =~ s/\n//g;
my $host = $parms{'errorHost'} || $parms{'err_host'} || '';
my $system = $parms{'errorSystem'} || $parms{'err_system'} || '';
my $subSource = $parms{'errorSubSource'} || $parms{'err_subsource'} || '';
my $program = $parms{'errorProgram'} || $parms{'err_prog'} || '';
$this->{'STATUS'} = system ("clear");
print "######################################################################################################################################## \n";
print "\n";
print "Output being sent to postzmsg....";
print "\n";
print ("$this->{'POSTZ_HOME'}/postzmsg -f $this->{'POSTZ_CONFIG'} -r ${severity} -m '${message}' OnCallGroup=${group} ISOC_Instructions='${instructions}' Node=${host} SubSource=${subSource} System=${system}
Program=${program} ${type} ${source} ");
print "\n";
print "######################################################################################################################################## \n";
print "\n";
$this->{'STATUS'} = system("$this->{'POSTZ_HOME'}/postzmsg -f $this->{'POSTZ_CONFIG'} -r ${severity} -m '${message}' OnCallGroup=${group} ISOC_Instructions='${instructions}' Node=${host} SubSource=${subS
ource} System=${system} Program=${program} ${type} ${source}");
return $this->{'STATUS'};
}
## Mostly legacy getters/setters for backwards compatibility
sub setLogFile { my $this = shift; $this->{'log_file'} = shift; }
sub setStatus { my $this = shift; $this->{'STATUS'} = shift; }
sub setStatusMsg { my $this = shift; $this->{'STATUS_MSG'} = shift; }
sub setConfirmEventOn { my $this = shift; $this->{'confirm_event'} = "TRUE"; }
sub setConfirmEventOff { my $this = shift; $this->{'confirm_event'} = ""; }
sub confirmEvent { my $this = shift; return $this->{'confirm_event'}; }
sub getLogFile { my $this = shift; return $this->{'log_file'}; }
sub getStatus { my $this = shift; return $this->{'STATUS'}; }
sub getStatusMsg { my $this = shift; return $this->{'STATUS_MSG'}; }
sub getContentMsg { my $this = shift; return $this->{'CONTENT_MSG'}; }
sub getFromAddr { my $this=shift; return $this->{'email_from_addr'}; }
sub getSuppDistList { my $this=shift; return $this->{'email_distribution_list'}; }
sub getOncallGrp { my $this=shift; return $this->{'default_oncall_group'}; }
sub getURL { my $this=shift; return $this->{'URL'} }
sub setURL { my $this=shift; $this->{'URL'} = shift;}
sub printURL { my $this=shift; return join ("\n", split /\|/, $this->{'URL'}) }
sub isSuccess { my $this = shift; return $this->{'STATUS'} }
## -----------------------------sendErrortoTivoli-------------------------------- ##
## Legacy method used to reformat a hashmap into the postEvent call.
##
sub sendError2Tivoli {
my $class = shift;
my $postEvent = shift;
my %tivEvent = %{$postEvent};
my $event = GFS::Event->new();
$event->setConfirmEventOff();
if (! $tivEvent{'err_host'}) { $tivEvent{'err_host'} = hostname(); }
$event->postEvent(
errorType => $tivEvent{'err_type'},
errorSeverity => $tivEvent{'err_severity'},
errorGroup => $tivEvent{'err_group'},
errorProgram => $tivEvent{'err_prog'},
errorSystem => $tivEvent{'err_system'},
err_msg => $tivEvent{'err_msg'},
errorHost => $tivEvent{'err_host'},
errorInstructions => $tivEvent{'err_inst'},
errorNumber => "$tivEvent{err_n}",
errorSubsource => $tivEvent{'subsource'}
);
return $event->getStatusMsg();
}
1;
##
sub sendError2Tivoli {
my $class = shift;
my $postEvent = shift;
my %tivEvent = %{$postEvent};
my $event = GFS::Event->new();
$event->setConfirmEventOff();
if (! $tivEvent{'err_host'}) { $tivEvent{'err_host'} = hostname(); }
$event->postEvent(
errorType => $tivEvent{'err_type'},
errorSeverity => $tivEvent{'err_severity'},
errorGroup => $tivEvent{'err_group'},
errorProgram => $tivEvent{'err_prog'},
errorSystem => $tivEvent{'err_system'},
err_msg => $tivEvent{'err_msg'},
errorHost => $tivEvent{'err_host'},
errorInstructions => $tivEvent{'err_inst'},
errorNumber => "$tivEvent{err_n}",
errorSubsource => $tivEvent{'subsource'}
);
return $event->getStatusMsg();
}
1;
__END__
# Documentation...