File: //var/opt/OV/bin/instrumentation/OvTrace.pm
###############################
#
# Version : 11.11.025
#
###############################
package OvTrace;
require Exporter;
use vars qw($VERSION @ISA @EXPORT);
# set the version for version checking
$VERSION = 0.01;
@ISA = qw(Exporter);
use Config;
sub GetInstallDir {
open (OVPATHEXE, "ovpath -instdir|") or warn("unable to run ovpath binary: \n");
$out = <OVPATHEXE>;
close OVPATHEXE;
chomp $out;
$out = $out . '/';
return $out;
}
sub new
{
my $class = shift;
my $self = {};
$self->{PROJECT} = "Infraspi";
$self->{POLICY} = shift;
$self->{CONSOLEOBJECT} = shift;
$self->{RULE} = shift;
$self->{SESSION} = shift;
bless ($self, $class);
return $self;
}
#WriteTraceLog function for Trace level messages.
#Implemented through SendConsoleMessage
sub WriteTraceLog {
my $self = shift;
my ($Debug, $strText, $MessageType) = @_;
if ( !$MessageType)
{
$MessageType = "normal";
}
# check for application type, "System" by default
my $PolicyName = $self->{POLICY}->Name();
my $Application = "OS";
if ( $PolicyName =~/VI-/ )
{
$Application = "Virtualization";
}
elsif ( $PolicyName =~/CI-/ )
{
$Application = "HA";
}
# check debug flag to see if messages goto console/logfile
# > 1 , messages writen to logfile
if ( $Debug > 1 )
{
$self->SendLogMessage ( $strText, $MessageType);
}
else
{
$self->SendConsoleMessage ($Application, 'Debug', '', '', "Trace message from ".$PolicyName.":\n$strText", $MessageType);
}
}
#SendConsoleMessage for sending messages to the console.
sub SendConsoleMessage {
my $self = shift;
my($strAppl, $strObj, $strGrp, $strSvc, $strText, $strSev) = @_;
$self->{CONSOLEOBJECT}->Application($strAppl);
$self->{CONSOLEOBJECT}->Object($strObj);
$self->{CONSOLEOBJECT}->MsgGrp($strGrp);
$self->{CONSOLEOBJECT}->ServiceId($strSvc);
$self->{CONSOLEOBJECT}->MsgText($strText);
$self->{CONSOLEOBJECT}->Severity($strSev);
$self->{CONSOLEOBJECT}->Send();
}
#SendLogMessage for sending messages to log file /var/opt/OV/log/Infraspi.txt.
sub SendLogMessage {
my( $self, $Message, $MessageType, $Project ) = @_;
my $OS = $^O;
my $Logtool = GetInstallDir()."bin/ovlogmsg";
if ( $OS =~ /WIN/i )
{
$Logtool = GetInstallDir()."bin/ovlogmsg.exe";
if ( $Config{"archname"} =~ /64/ )
{
$Logtool = GetInstallDir()."bin/win64/ovlogmsg.exe";
}
}
# Define error message type by default.
$_ = $MessageType;
#Define as error message by default
my $Flag = "-e";
SWITCH:
{
/ERROR/i && do {
$Flag = "-e";
last SWITCH;
};
/WARNING/i && do {
$Flag = "-w";
last SWITCH;
};
/NORMAL/i && do {
$Flag = "-i";
last SWITCH;
};
}
if ( !$Project )
{
$Project = $self->{PROJECT};
}
my $Policyname;
if ( ( $self->{POLICY} =~ /Discovery/) || ( $self->{POLICY} =~ /Collector/) || ( $self->{POLICY} =~ /VMEvent/) || ( $self->{POLICY} =~ /VI-VMware/) )
{
$Policyname = $self->{POLICY};
}
else
{
$Policyname = $self->{POLICY}->Name();
}
# run ovlogmsg utility to log messages, messages logged in /var/opt/OV/log/<PROJECT> file
`"$Logtool" -a $Project -c $Policyname -msg "$Message" -t $Flag`;
warn ("Error! ovlogmsg failed!\n") if ( $? == -1 )
}
sub SendOpcmsg {
my($self, $strAppl, $strObj, $strText, $strSev) = @_;
$strText =~ s/\n/ /g; # serialize message text - to remove newlines
$strText =~ s/"/'/g; # replace " by '
system("opcmsg severity=$strSev a=\"$strAppl\" o=\"$strObj\" msg_text=\"$strText\"");
}
# WriteErrorLog function for error conditions that cannot be resolved.
# Note that session variable alertstring contains error msg.
sub WriteErrorLog {
my $self = shift;
my @strText;
@strText=@_;
my $ErrorString .= "ERROR message from ".$self->{POLICY}->Name().":\n@strText";
$self->{SESSION}->Value('AlertString', $ErrorString );
$self->{RULE}->Status(1);
}
1;