[icinga-checkins] icinga.org: icinga-reports/mluebben/default: Add prefix parameter for tables

git at icinga.org git at icinga.org
Sat Jul 16 16:22:32 CEST 2011


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

Author: Michael Luebben <mluebben at destiny.(none)>
Date:   Sat Jul 16 16:22:10 2011 +0200

Add prefix parameter for tables

---

 AvailReportParser/Modules/Config/Configuration.pm |    4 ++
 AvailReportParser/Modules/Database/DBHandler.pm   |   49 ++++++++++++++-------
 AvailReportParser/etc/default.ini                 |   10 +++--
 AvailReportParser/importAvailReport.pl            |    2 +-
 4 files changed, 44 insertions(+), 21 deletions(-)

diff --git a/AvailReportParser/Modules/Config/Configuration.pm b/AvailReportParser/Modules/Config/Configuration.pm
index 92e8536..2ba4cce 100755
--- a/AvailReportParser/Modules/Config/Configuration.pm
+++ b/AvailReportParser/Modules/Config/Configuration.pm
@@ -107,6 +107,10 @@ sub getParameter {
 	
 	if (length($self->{config}->{$parameter}) != 0) {
 		$result = $self->{config}->{$parameter};
+	} else {	
+		if ($self->{config}->{$parameter} == "TABLEPREFIX") {
+			$result = $self->{config}->{$parameter} = "";
+		}
 	}
 	
 	return $result
diff --git a/AvailReportParser/Modules/Database/DBHandler.pm b/AvailReportParser/Modules/Database/DBHandler.pm
index 2d866d6..8b4f73a 100755
--- a/AvailReportParser/Modules/Database/DBHandler.pm
+++ b/AvailReportParser/Modules/Database/DBHandler.pm
@@ -6,10 +6,17 @@ use Switch;
 use Data::Dumper;
 
 # Define table names
-my $table_avail_breakdowns_percent = "raw_availability_breakdowns_percent";
-my $table_avail_breakdowns_time = "raw_availability_breakdowns_time";
-my $table_avail_logentries = "raw_availability_logentries";
-
+my $table_prefix = "icinga_";
+
+my $tableStates = "states";
+my $tableStatetypes = "statetypes";
+my $tableTimetypes = "timetypes";
+my $tableReasontypes = "reasontypes";
+my $tableObjecttypes = "objecttypes";
+my $tableAvailBreakdownsPercent = "raw_availability_breakdowns_percent";
+my $tableAvailBreakdownsTime = "raw_availability_breakdowns_time";
+my $tableAvailLogentries = "raw_availability_logentries";
+my $tableReports = "reports";
 
 =pod
 
@@ -38,7 +45,17 @@ sub new {
 	my $pkg = shift;
 	my $self = {};
 	
-	my ($dbHost, $dbName, $dbUser, $dbPass) = @_;
+	my ($dbHost, $dbName, $dbUser, $dbPass, $tablePrefix) = @_;
+	
+	$self->{table}->{states} = $tablePrefix.$tableStates;
+	$self->{table}->{statetypes} = $tablePrefix.$tableStatetypes;
+	$self->{table}->{timetypes} = $tablePrefix.$tableTimetypes;
+	$self->{table}->{reasontypes} = $tablePrefix.$tableReasontypes;
+	$self->{table}->{objecttypes} = $tablePrefix.$tableObjecttypes;
+	$self->{table}->{raw_availability_breakdowns_percent} = $tablePrefix.$tableAvailBreakdownsPercent;
+	$self->{table}->{raw_availability_breakdowns_time} = $tablePrefix.$tableAvailBreakdownsTime;
+	$self->{table}->{raw_availability_logentries} = $tablePrefix.$tableAvailLogentries;
+	$self->{table}->{reports} = $tablePrefix.$tableReports;
 	
 	$self->{dbh} = DBI->connect("DBI:mysql:database=$dbName;host=$dbHost","$dbUser","$dbPass");
 	
@@ -63,19 +80,19 @@ sub getAllDatasFromTable {
 	
 	switch ($table) {
 		case "states" {
-			$sth = $self->{dbh}->prepare("SELECT * FROM states");
+			$sth = $self->{dbh}->prepare("SELECT * FROM ".$self->{table}->{states});
 		}
 		case "statetypes" {
-			$sth = $self->{dbh}->prepare("SELECT * FROM statetypes");
+			$sth = $self->{dbh}->prepare("SELECT * FROM ".$self->{table}->{statetypes});
 		}
 		case "timetypes" {
-			$sth = $self->{dbh}->prepare("SELECT * FROM timetypes");
+			$sth = $self->{dbh}->prepare("SELECT * FROM ".$self->{table}->{timetypes});
 		}
 		case "reasontypes" {
-			$sth = $self->{dbh}->prepare("SELECT * FROM reasontypes");
+			$sth = $self->{dbh}->prepare("SELECT * FROM ".$self->{table}->{reasontypes});
 		}
 		case "objecttypes" {
-			$sth = $self->{dbh}->prepare("SELECT * FROM objecttypes");
+			$sth = $self->{dbh}->prepare("SELECT * FROM ".$self->{table}->{objecttypes});
 		}
 		else {
 			$self->{errMessage} = "Table ".$table." not defined!";
@@ -123,7 +140,7 @@ sub createNewReport {
 	my $sth;
 	my $insertID;
 		
-	$sth = $self->{dbh}->prepare("INSERT INTO reports (date, hostname, service_description, report_description, timeperiod, rpttimeperiod, config_filename) VALUES (now(), '".$hostname."', '".$service_description."', '".$report_description."', '".$timeperiod."', '".$rpttimeperiod."', '".$config_filename."')");
+	$sth = $self->{dbh}->prepare("INSERT INTO ".$self->{table}->{reports}." (date, hostname, service_description, report_description, timeperiod, rpttimeperiod, config_filename) VALUES (now(), '".$hostname."', '".$service_description."', '".$report_description."', '".$timeperiod."', '".$rpttimeperiod."', '".$config_filename."')");
 	$sth->execute();
 	$insertID = $self->{dbh}->{ q{mysql_insertid} };
 	$sth->finish;
@@ -161,7 +178,7 @@ sub finishReport {
 	
 	my $sth;
 	
-	$sth = $self->{dbh}->prepare("UPDATE reports SET execution_time = '".$executionTime."', exit_status = '".$status."', message = '".$message."' WHERE id = '".$reportID."'");
+	$sth = $self->{dbh}->prepare("UPDATE ".$self->{table}->{reports}." SET execution_time = '".$executionTime."', exit_status = '".$status."', message = '".$message."' WHERE id = '".$reportID."'");
 	$sth->execute();
 	$sth->finish();
 }
@@ -385,7 +402,7 @@ sub insertAvailBreakdown {
 		case "time" {
 			my ($days, $hours, $minutes, $seconds) = @_;
 
-			$sth = $self->{dbh}->prepare("INSERT INTO ".$table_avail_breakdowns_time." (report_id, state_id, reasontype_id, timetype_id, days, hours, minutes, seconds) VALUES (".$reportID.", ".$stateID.",".$reasonTimeID.", ".$timeTypeID.", ".$days.", ".$hours.", ".$minutes.", ".$seconds.")");
+			$sth = $self->{dbh}->prepare("INSERT INTO ".$self->{table}->{raw_availability_breakdowns_time}." (report_id, state_id, reasontype_id, timetype_id, days, hours, minutes, seconds) VALUES (".$reportID.", ".$stateID.",".$reasonTimeID.", ".$timeTypeID.", ".$days.", ".$hours.", ".$minutes.", ".$seconds.")");
 			$sth->execute();
 			$insertID = $self->{dbh}->{ q{mysql_insertid} };
 			$sth->finish;
@@ -394,7 +411,7 @@ sub insertAvailBreakdown {
 		case "knowntime" {
 			my ($percent) = @_;
 			
-			$sth = $self->{dbh}->prepare("INSERT INTO ".$table_avail_breakdowns_percent." (report_id, state_id, reasontype_id, timetype_id, percent) VALUES (".$reportID.", ".$stateID.",".$reasonTimeID.", ".$timeTypeID.", ".$percent.")");
+			$sth = $self->{dbh}->prepare("INSERT INTO ".$self->{table}->{raw_availability_breakdowns_percent}." (report_id, state_id, reasontype_id, timetype_id, percent) VALUES (".$reportID.", ".$stateID.",".$reasonTimeID.", ".$timeTypeID.", ".$percent.")");
 			$sth->execute();
 			$insertID = $self->{dbh}->{ q{mysql_insertid} };
 			$sth->finish;
@@ -403,7 +420,7 @@ sub insertAvailBreakdown {
 		case "totaltime" {
 			my ($percent) = @_;
 			
-			$sth = $self->{dbh}->prepare("INSERT INTO ".$table_avail_breakdowns_percent." (report_id, state_id, reasontype_id, timetype_id, percent) VALUES (".$reportID.", ".$stateID.",".$reasonTimeID.", ".$timeTypeID.", ".$percent.")");
+			$sth = $self->{dbh}->prepare("INSERT INTO ".$self->{table}->{raw_availability_breakdowns_percent}." (report_id, state_id, reasontype_id, timetype_id, percent) VALUES (".$reportID.", ".$stateID.",".$reasonTimeID.", ".$timeTypeID.", ".$percent.")");
 			$sth->execute();
 			$insertID = $self->{dbh}->{ q{mysql_insertid} };
 			$sth->finish;
@@ -481,7 +498,7 @@ sub insertAvailEventLogEntry {
 	my $sth;
 	my $insertID;
 
-	$sth = $self->{dbh}->prepare("INSERT INTO ".$table_avail_logentries." (report_id, start_time, stop_time, duration_days, duration_hours, duration_minutes, duration_seconds, event_text, objecttype_id, state_id, statetype_id, state_information) VALUES (".$reportID.", '".$startTime."', '".$stopTime."', ".$durationDays.", ".$durationHours.", ".$durationMinutes.", ".$durationSeconds.", '".$eventText."', ".$objecttypeID.", ".$stateID.", ".$stateTypeID.", '".$stateInformation."')");
+	$sth = $self->{dbh}->prepare("INSERT INTO ".$self->{table}->{raw_availability_logentries}." (report_id, start_time, stop_time, duration_days, duration_hours, duration_minutes, duration_seconds, event_text, objecttype_id, state_id, statetype_id, state_information) VALUES (".$reportID.", '".$startTime."', '".$stopTime."', ".$durationDays.", ".$durationHours.", ".$durationMinutes.", ".$durationSeconds.", '".$eventText."', ".$objecttypeID.", ".$stateID.", ".$stateTypeID.", '".$stateInformation."')");
 	$sth->execute();
 	$insertID = $self->{dbh}->{ q{mysql_insertid} };
 	$sth->finish;
diff --git a/AvailReportParser/etc/default.ini b/AvailReportParser/etc/default.ini
index 98b2c57..fab5b72 100755
--- a/AvailReportParser/etc/default.ini
+++ b/AvailReportParser/etc/default.ini
@@ -18,6 +18,8 @@ DBNAME = reporting
 DBUSER = icinga
 DBPASS = icinga
 
+TABLEPREFIX =
+
 # ===== Parsing Option =====
 # Use HTML or JSON for parsing
 PARSERTYPE = HTML
@@ -39,12 +41,12 @@ LOGENTRIES = 1
 
 # ===== 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 = 2
+# 1 = Normal (default)
+# 2 = More information about methods, variables etc.
+LOGLEVEL = 1
 
 # Path to debug file
 DEBUGFILE = /tmp/report_avail.log
diff --git a/AvailReportParser/importAvailReport.pl b/AvailReportParser/importAvailReport.pl
index cbf486d..a5cf0e2 100755
--- a/AvailReportParser/importAvailReport.pl
+++ b/AvailReportParser/importAvailReport.pl
@@ -236,7 +236,7 @@ if ($parseResult == -1) {
 	my $dbUser = $Config->getParameter('DBUSER');
 	my $dbPass = $Config->getParameter('DBPASS');
 	
-	my $DBHandler = new DBHandler($dbHost, $dbName, $dbUser, $dbPass);
+	my $DBHandler = new DBHandler($dbHost, $dbName, $dbUser, $dbPass, $Config->getParameter('TABLEPREFIX'));
 	$Debug->addMessage("LOGLEVEL 2: new DBHandler(".$dbHost.", ".$dbName.", ".$dbUser.", xxxxxxxx)", 2);
 	if ($DBHandler == -1) {
 		$Debug->addMessage("ERROR: Can't connect to database!", 1);





More information about the icinga-checkins mailing list