[icinga-checkins] icinga.org: icinga-reports/mluebben/default: Make changes on debugging modul

git at icinga.org git at icinga.org
Wed Jun 29 21:34:20 CEST 2011


Module: icinga-reports
Branch: mluebben/default
Commit: bf76aaaf2f5fe6e53e34fe22800aad769f72750e
URL:    https://git.icinga.org/?p=icinga-reports.git;a=commit;h=bf76aaaf2f5fe6e53e34fe22800aad769f72750e

Author: Michael Luebben <michael_luebben at web.de>
Date:   Wed Jun 29 21:33:04 2011 +0200

Make changes on debugging modul

---

 AvailReportParser/Modules/Debug/Debugger.pm   |   65 +++++++++++++-----------
 AvailReportParser/etc/default.ini             |   10 +++-
 AvailReportParser/importServiceAvailReport.pl |   50 +++++++++++++-------
 3 files changed, 76 insertions(+), 49 deletions(-)

diff --git a/AvailReportParser/Modules/Debug/Debugger.pm b/AvailReportParser/Modules/Debug/Debugger.pm
index 409c117..11a2101 100755
--- a/AvailReportParser/Modules/Debug/Debugger.pm
+++ b/AvailReportParser/Modules/Debug/Debugger.pm
@@ -12,7 +12,7 @@ Debugger.pm
 
 =head1 Version
 
-Version 0.0.2
+Version 0.1.2
 
 =head1 DESCRIPTION
 
@@ -22,17 +22,17 @@ Write debugging information into a file.
 
   use Debugger;
   
-  $Debug = new Debugger($debugFile, $enable);
+  $Debug = new Debugger($debugFile, $enable, $logLevel);
   $Debug->addMessage($debugMessage);
-  $Debug->writeDebugFile();
+  $Debug->close();
 
 =head1 EXAMPLE
 
   use Debugger;
   
-  $Debug = new Debugger("/tmp/debug.log", "1");
+  $Debug = new Debugger("/tmp/debug.log", "1", "2");
   $Debug->addMessage("Hello world");
-  $Debug->writeDebugFile();
+  $Debug->close();
 
 =over
 
@@ -52,13 +52,14 @@ B</tmp/debug.log contains:>
 # Constructor
 sub new {
 	my $pkg = shift;
-	my ($debugFile, $enabled) = @_; 
+	my ($debugFile, $enabled, $logLevel) = @_; 
 	my $self = {};
 
 	bless $self, $pkg;
 	
 	$self->{enabled} = $enabled;
 	$self->{debugFile} = $debugFile;
+	$self->{logLevel} = $logLevel;
 	
 	$self->begin;
 	
@@ -74,7 +75,7 @@ sub begin {
 		my $HiResTime = sprintf("%.5f", time());
 
 		$self->{startTime} = $HiResTime;
-		$self->{message} = $HiResTime." ---------------===== Begin debugging =====---------------\n";
+		$self->writeToFile($HiResTime." ---------------===== Begin debugging =====---------------\n");
 	}
 	
 	return 0;
@@ -99,28 +100,17 @@ Add a message into the debug file
 sub addMessage {
 	my $self = shift;
 	my $message = shift;
+	my $logLevel = shift;
 
 	if ($self->{enabled} == 1) {
-		my $HiResTime = sprintf("%.5f", time());
-				
-		$self->{message} .= $HiResTime." ".$message."\n";	
+		if ($logLevel <= $self->{logLevel}) {
+			my $HiResTime = sprintf("%.5f", time());
+					
+			$self->writeToFile($HiResTime." ".$message."\n");
+		}
 	}
 	
-	return 0;
-}
-
-# Internal method for the method writeDebugFile();
-# End a new debug file with a new start entry
-sub end {
-	my $self = shift;
-	
-	if ($self->{enabled} == 1) {
-		my $HiResTime = sprintf("%.5f", time());
 	
-		$self->{executionTime} = $HiResTime - $self->{startTime};
-		$self->{message} .= $HiResTime." Execution time: ".$self->{executionTime}."s\n";
-		$self->{message} .= $HiResTime." ----------------===== End debugging =====----------------\n\n";
-	}
 	
 	return 0;
 }
@@ -145,7 +135,7 @@ sub getExecutionTime() {
 	my $self = shift;
 	
 	if (length($self->{executionTime}) == 0) {
-		$self->addMessage("WARN: For using the method getExecutionTime(), use first the methode writeDebugFile() to calculate the execution time!");
+		$self->addMessage("WARN: For using the method getExecutionTime(), use first the methode close() to calculate the execution time!");
 		return -1;
 	} else {
 		return $self->{executionTime};
@@ -153,6 +143,18 @@ sub getExecutionTime() {
 }
 
 
+# Internal method;
+# Write new message to file
+sub writeToFile() {
+	my $self = shift;
+	my $message = shift;
+	
+	open(debugFile, ">>$self->{debugFile}");
+		print debugFile $message;
+	close(debugFile);
+}
+
+
 =pod
 
 =over
@@ -168,15 +170,18 @@ Close and write a debug file
   $Debug->writeDebugFile;
 
 =cut
-sub writeDebugFile {
+sub close {
 	my $self = shift;
+	my $message;
 	
 	if ($self->{enabled} == 1) {
-		$self->end();
+		my $HiResTime = sprintf("%.5f", time());
+	
+		$self->{executionTime} = $HiResTime - $self->{startTime};
+		$message = $HiResTime." Execution time: ".$self->{executionTime}."s\n";
+		$message .= $HiResTime." ----------------===== End debugging =====----------------\n\n";
 		
-		open(debugFile, ">>$self->{debugFile}");
-			print debugFile $self->{message};
-		close(debugFile);
+		$self->writeToFile($message);
 	}
 	
 	return 0;
diff --git a/AvailReportParser/etc/default.ini b/AvailReportParser/etc/default.ini
index 19ab55c..7620263 100755
--- a/AvailReportParser/etc/default.ini
+++ b/AvailReportParser/etc/default.ini
@@ -1,5 +1,5 @@
 # Configuration file for availability parser
-# Version 1.8
+# Version 1.9
 
 # ===== Basic Authentification =====
 HOST = localhost
@@ -19,11 +19,17 @@ DBUSER = icinga
 DBPASS = icinga
 
 # ===== Parsing Option =====
+# Use HTML or JSON for parsing
 PARSERTYPE = JSON
 
 # ===== Debugging =====
 # Enable debuggging (0=off / 1=on)
-DEBUG = 1
+DEBUG = 0
+
+# Log level for debugging
+# 0 = Normal (default)
+# 1 = More information about methods, variables etc.
+LOGLEVEL = 1
 
 # Path to debug file
 DEBUGFILE = /tmp/report_avail.log
diff --git a/AvailReportParser/importServiceAvailReport.pl b/AvailReportParser/importServiceAvailReport.pl
index 46d91d2..0184a28 100755
--- a/AvailReportParser/importServiceAvailReport.pl
+++ b/AvailReportParser/importServiceAvailReport.pl
@@ -7,7 +7,7 @@
  
 =head1 Version
 
-Version 0.1.8
+Version 0.1.9
 
 =head1 SYNOPSIS
 
@@ -170,51 +170,66 @@ my $Config = new Configuration($optConfigFile);
 
 
 # Init debugger
-my $Debug = new Debugger($Config->getParameter('DEBUGFILE'), $Config->getParameter('DEBUG'));
+my $Debug = new Debugger($Config->getParameter('DEBUGFILE'), $Config->getParameter('DEBUG'), $Config->getParameter('LOGLEVEL'));
 
 
 # Set parameters for url
 $ReportAvail->setUrlParameter('host',$optHostname);
+$Debug->addMessage("LOGLEVEL 2: Parser::Availability->setUrlParameter('host', ".$optHostname.")", 2);
+
 $ReportAvail->setUrlParameter('service',$optServicesDesc);
+$Debug->addMessage("LOGLEVEL 2: Parser::Availability->setUrlParameter('service', ".$optServicesDesc.")", 2);
+
 $setUrlResult = $ReportAvail->setUrlParameter('timeperiod',$optTimeperiod);
+$Debug->addMessage("LOGLEVEL 2: Parser::Availability->setUrlParameter('timeperiod', ".$optTimeperiod.")", 2);
 if ($setUrlResult == -1) {
-	$Debug->addMessage("ERROR: ".$ReportAvail->getErrorMessage());
-	$Debug->writeDebugFile();
+	$Debug->addMessage("ERROR: ".$ReportAvail->getErrorMessage(), 1);
+	$Debug->close();
 	exit 3;
 }
+
 $setUrlResult = $ReportAvail->setUrlParameter('rpttimeperiod',$optRptTimeperiod);
+$Debug->addMessage("LOGLEVEL 2: Parser::Availability->setUrlParameter('rpttimeperiod', ".$optRptTimeperiod.")", 2);
 
 
 # Get url for availibility report
 $ReportAvail->setUrlPath($Config->getParameter('URLPATH'));
+$Debug->addMessage("LOGLEVEL 2: Parser::Availability->setUrlPath(".$Config->getParameter('URLPATH').")", 2);
+
 $ReportAvail->setParserType($Config->getParameter('PARSERTYPE'));
+$Debug->addMessage("LOGLEVEL 2: Parser::Availability->setUrlPath(".$Config->getParameter('PARSERTYPE').")", 2);
+
 my $url = $ReportAvail->getUrl();
-$Debug->addMessage("Create url: ".$url);
+$Debug->addMessage("INFO: Create url: ".$url, 1);
 
 
 # Get availalibility report from Icinga/Nagios
 my $Browser = LWP::UserAgent->new;
+$Debug->addMessage("LOGLEVEL 2: LWP::UserAgent->new", 2);
+
 $Browser->credentials(
 	$Config->getParameter('HOST').":".$Config->getParameter('PORT'),
 	$Config->getParameter('REALM'),
 	$Config->getParameter('USERNAME') => $Config->getParameter('PASSWORD')
 );
+$Debug->addMessage("LOGLEVEL 2: Browser->credentials(".$Config->getParameter('HOST').":".$Config->getParameter('PORT').", ".$Config->getParameter('REALM').", ".$Config->getParameter('USERNAME')." => xxxxxxx)", 2);
+
 my $response = $Browser->get($url);
 if ($response->is_success != 1) {
-	$Debug->addMessage("ERROR: ".$response->status_line);
-	$Debug->writeDebugFile();
+	$Debug->addMessage("ERROR: ".$response->status_line, 1);
+	$Debug->close();
 	exit 3;
 } else {
-	$Debug->addMessage("INFO: Response status ".$response->status_line);
+	$Debug->addMessage("INFO: Response status ".$response->status_line, 1);
 }
 
 
 # Parse HTML content
 my $parseResult = $ReportAvail->parseContent($response->content);
 if ($parseResult == -1) {
-	$Debug->addMessage("ERROR: ".$ReportAvail->getErrorMessage());
+	$Debug->addMessage("ERROR: ".$ReportAvail->getErrorMessage(), 1);
 } else {
-	$Debug->addMessage("INFO: ".$Config->getParameter('PARSERTYPE')." parsing successully!");
+	$Debug->addMessage("INFO: ".$Config->getParameter('PARSERTYPE')." parsing successully!", 1);
 
 
 	# +------------------------------------------------------+
@@ -227,16 +242,17 @@ if ($parseResult == -1) {
 	my $dbPass = $Config->getParameter('DBPASS');
 	
 	my $DBHandler = new DBHandler($dbHost, $dbName, $dbUser, $dbPass);
+	$Debug->addMessage("LOGLEVEL 2: new DBHandler(".$dbHost.", ".$dbName.", ".$dbUser.", xxxxxxxx)", 2);
 	if ($DBHandler == -1) {
-		$Debug->addMessage("ERROR: Can't connect to database!");
+		$Debug->addMessage("ERROR: Can't connect to database!", 1);
 	} else {
-		$Debug->addMessage("INFO: Connect to database successful!");
+		$Debug->addMessage("INFO: Connect to database successful!", 1);
 		
 		# Init report
 		$reportID = $DBHandler->createNewReport($optHostname, $optServicesDesc, $optReportDesc, $optTimeperiod, $optRptTimeperiod, $optConfigFile, "0", $statusCode ,$message);
 		if ($reportID == -1) {
-			$Debug->addMessage("ERROR: ".$DBHandler->getErrorMessage);
-			$Debug->writeDebugFile();
+			$Debug->addMessage("ERROR: ".$DBHandler->getErrorMessage, 1);
+			$Debug->close();
 			exit 3;
 		} else {
 		
@@ -518,12 +534,12 @@ if ($parseResult == -1) {
 				$DBHandler->insertAvailEventLogEntry($reportID,$allEventLogEntries->{$index});
 				++$index;
 			}
-			$Debug->addMessage("INFO: Report successfully created with ID ".$reportID." and parsed data imported!");
+			$Debug->addMessage("INFO: Report successfully created with ID ".$reportID." and parsed data imported!",1);
 		}
-		$Debug->writeDebugFile();
+		$Debug->close();
 		$DBHandler->finishReport($reportID, $Debug->getExecutionTime(), $statusCode, "Report successfully created with ID ".$reportID." and parsed data imported!");
 	}
 }
 
-# Close and write debug file
+# Exit
 exit 0;
\ No newline at end of file





More information about the icinga-checkins mailing list