From dbe88a211fb8cc0cbdb4663b686134cd7202bb37 Mon Sep 17 00:00:00 2001
From: slerena <slerena@gmail.com>
Date: Mon, 14 Sep 2009 01:10:20 +0000
Subject: [PATCH] 2009-09-14  Sancho Lerena <slerena@artica.es>

	* bin/pandora_server: Now uses $Config{"enterprise"} to know if
	enterprise is loaded, and don't try to call enterprise_hook if is not.
	I experience a high rate of inventory-enterprise hook errors in log.

	* SNMPServer.pm: Dont call snmp_get_trap2agent enterprise_hook if
	not enterprise loaded.

	* DataServer.pm: Dont call process_inventory_data if not
	enterprise loaded.

	* util/pandora_backup.sh: New command line backup and restore tool.



git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@1936 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
---
 pandora_server/ChangeLog                    |  15 +++
 pandora_server/bin/pandora_server           |  10 +-
 pandora_server/lib/PandoraFMS/DataServer.pm |   4 +-
 pandora_server/lib/PandoraFMS/SNMPServer.pm |  12 +-
 pandora_server/util/pandora_backup.sh       | 134 ++++++++++++++++++++
 5 files changed, 169 insertions(+), 6 deletions(-)
 create mode 100755 pandora_server/util/pandora_backup.sh

diff --git a/pandora_server/ChangeLog b/pandora_server/ChangeLog
index 17d7c4e163..5ca67a1860 100644
--- a/pandora_server/ChangeLog
+++ b/pandora_server/ChangeLog
@@ -1,3 +1,18 @@
+2009-09-14  Sancho Lerena <slerena@artica.es>
+
+	* bin/pandora_server: Now uses $Config{"enterprise"} to know if
+	enterprise is loaded, and don't try to call enterprise_hook if is not.
+	I experience a high rate of inventory-enterprise hook errors in log.
+
+	* SNMPServer.pm: Dont call snmp_get_trap2agent enterprise_hook if 
+	not enterprise loaded.
+
+	* DataServer.pm: Dont call process_inventory_data if not 
+	enterprise loaded.
+
+	* util/pandora_backup.sh: New command line backup and restore tool.
+
+
 2009-09-07  Sancho Lerena <slerena@artica.es>
 
 	* util/pandora_db.pl: Fixed bad calculation of utimestamp limit
diff --git a/pandora_server/bin/pandora_server b/pandora_server/bin/pandora_server
index 9ab941b781..bcc6f6425f 100755
--- a/pandora_server/bin/pandora_server
+++ b/pandora_server/bin/pandora_server
@@ -81,7 +81,10 @@ pandora_init(\%Config, 'Pandora FMS Server');
 pandora_load_config (\%Config);
 
 # Load enterprise module
-enterprise_load () && print " [*] Pandora FMS Enterprise module loaded.\n";
+$Config{"enterprise"} = enterprise_load ();
+if ($Config{"enterprise"} == 1){
+	print " [*] Pandora FMS Enterprise module loaded.\n";
+}
 
 # Daemonize and put in background
 if ($Config{'daemon'} == 1) {
@@ -107,7 +110,10 @@ push (@Servers, new PandoraFMS::SNMPServer (\%Config, $DBH));
 push (@Servers, new PandoraFMS::WMIServer (\%Config, $DBH));
 push (@Servers, new PandoraFMS::PluginServer (\%Config, $DBH));
 push (@Servers, new PandoraFMS::PredictionServer (\%Config, $DBH));
-enterprise_hook('load_enterprise_servers', [\@Servers, \%Config, $DBH]);
+
+if ($Config{"enterprise"} == 1){
+	enterprise_hook('load_enterprise_servers', [\@Servers, \%Config, $DBH]);
+}
 
 # Remove disabled servers
 @Servers = grep { defined ($_) } @Servers;
diff --git a/pandora_server/lib/PandoraFMS/DataServer.pm b/pandora_server/lib/PandoraFMS/DataServer.pm
index 721c8f236c..fd75fde433 100644
--- a/pandora_server/lib/PandoraFMS/DataServer.pm
+++ b/pandora_server/lib/PandoraFMS/DataServer.pm
@@ -234,8 +234,10 @@ sub process_xml_data ($$$$) {
 	}
 
 	# Process inventory modules
-	enterprise_hook('process_inventory_data', [$pa_config, $data, $server_id, $agent_name,
+	if ($pa_config->{enterprise} == 1){
+		enterprise_hook('process_inventory_data', [$pa_config, $data, $server_id, $agent_name,
 	                                           $interval, $timestamp, $dbh]);
+	}
 }
 
 ##########################################################################
diff --git a/pandora_server/lib/PandoraFMS/SNMPServer.pm b/pandora_server/lib/PandoraFMS/SNMPServer.pm
index 1b9f643804..24ef787d38 100644
--- a/pandora_server/lib/PandoraFMS/SNMPServer.pm
+++ b/pandora_server/lib/PandoraFMS/SNMPServer.pm
@@ -106,8 +106,13 @@ sub pandora_snmptrapd {
 
 	# Skip already processed lines
 	readline SNMPLOGFILE for (1..$last_line);
+
 	
-	my $trap2agent = enterprise_hook('snmp_get_trap2agent', [$dbh]);
+	my $trap2agent = 0;
+	
+	if ($pa_config->{enterprise} == 1){
+		$trap2agent = enterprise_hook('snmp_get_trap2agent', [$dbh]);
+	}
 
 	# Main loop
 	while (1) {
@@ -155,8 +160,9 @@ sub pandora_snmptrapd {
 				# Evaluate alerts for this trap
 				pandora_evaluate_snmp_alerts ($pa_config, $trap_id, $source, $oid, $oid, $custom_oid, $custom_value, $dbh);
 			}
-
-			enterprise_hook ('snmp_trap2agent', [$trap2agent, $pa_config, $source, $oid, $value, $custom_oid, $custom_value, $timestamp, $self->getServerID (), $dbh]);
+			if ($pa_config->{enterprise} == 1){
+				enterprise_hook ('snmp_trap2agent', [$trap2agent, $pa_config, $source, $oid, $value, $custom_oid, $custom_value, $timestamp, $self->getServerID (), $dbh]);
+			}
 		}
 		
 		sleep ($pa_config->{'server_threshold'});
diff --git a/pandora_server/util/pandora_backup.sh b/pandora_server/util/pandora_backup.sh
new file mode 100755
index 0000000000..ebde71fc38
--- /dev/null
+++ b/pandora_server/util/pandora_backup.sh
@@ -0,0 +1,134 @@
+#!/bin/bash
+
+# Pandora FMS Command line Backup Tool
+# (c) Sancho Lerena <slerena@gmail.com>, Artica Soluciones Tecnologicas 2009
+
+function help {
+	echo ""
+	echo -e "Pandora FMS Command line backup tool. http://www.pandorafms.org" 
+	echo -e "(c) 2009 Sancho Lerena <slerena@gmail.com>, Artica Soluciones Tecnologicas"
+	echo ""
+	echo -e "Syntax:" 
+	echo -e "\t\t-c Path to Pandora FMS console, p.e: /srv/www/htdocs/pandora_console"
+	echo -e "\t\t-d Destination path for backup file. p.e: /tmp"
+	echo -e "\t\t-s Source filename for backup restore. p.e: /tmp/pandorafms"
+	echo -e "\t\t-f Restore also files"
+	echo -e "\t\t-q Quiet. No output message (used for scripts/cron)"
+	echo -e "\n\nPlease BE SURE TO USE RESTORE (-s) option. This will OVERWRITE ALL your"
+	echo -e "PandoraFMS install, including files, configuration and data. Consided to"
+	echo -e "make a backup first."
+	echo ""
+	exit 1
+}
+
+if [ $# -eq 0 ]
+then
+	help
+fi
+
+SOURCEBACKUP="thisnotexist"
+QUIET=0
+RESTOREFILES=0
+TIMESTAMP=`date +"%Y-%m-%d-%H-%M-%S"`
+
+# Main parsing code
+
+while getopts "fhqc:d:s:" optname
+  do
+    case "$optname" in
+      "h")
+	        help
+	;;
+      "c")
+	        PANDORAPATH=$OPTARG
+        ;;
+      "f")
+	        RESTOREFILES=1
+        ;;
+      "d")
+		BACKUPDIR=$OPTARG
+        ;;
+      "s")
+		SOURCEBACKUP=$OPTARG
+        ;;
+      "q")
+		QUIET=1
+        ;;
+      ?)
+		help
+	;;
+      default) 
+		help
+	;;
+    esac
+done
+
+# Execution
+
+if [ ! -e "$PANDORAPATH/include/config.php" ]
+then
+	echo "Cannot read config file at $PANDORAPATH/include/config.php. Aborting"
+	exit 1
+fi
+
+
+DBUSER=`cat $PANDORAPATH/include/config.php | grep dbuser | grep -v "^\/" | grep -o "\=\"[a-zA-Z0-9]*\"" | grep -o "[A-Za-z0-9]*"`
+DBPASS=`cat $PANDORAPATH/include/config.php | grep dbpass | grep -v "^\/" | grep -o "\=\"[a-zA-Z0-9]*\"" | grep -o "[A-Za-z0-9]*"`
+DBHOST=`cat $PANDORAPATH/include/config.php | grep dbhost | grep -v "^\/" | grep -o "\=\"[a-zA-Z0-9]*\"" | grep -o "[A-Za-z0-9]*"`
+DBNAME=`cat $PANDORAPATH/include/config.php | grep dbname | grep -v "^\/" | grep -o "\=\"[a-zA-Z0-9]*\"" | grep -o "[A-Za-z0-9]*"`
+
+
+cd /tmp
+mkdir $TIMESTAMP
+cd $TIMESTAMP
+
+# Make the backup
+
+if [ ! -e "$SOURCEBACKUP" ]
+then
+
+	rm -Rf $BACKUPDIR/pandorafms_backup_$TIMESTAMP.tar.gz 2> /dev/null
+
+	mysqldump -u $DBUSER -p$DBPASS -h $DBHOST $DBNAME > pandorafms_backup_$TIMESTAMP.sql
+	tar cvzf pandorafms_backup_$TIMESTAMP.tar.gz pandorafms_backup_$TIMESTAMP.sql $PANDORAPATH /etc/pandora /var/spool/pandora/data_in --exclude .data 2> /dev/null > /dev/null
+
+	mv /tmp/$TIMESTAMP/pandorafms_backup_$TIMESTAMP.tar.gz $BACKUPDIR
+	cd /tmp
+	rm -Rf /tmp/$TIMESTAMP
+	if [ $QUIET == 0 ]
+	then
+		echo "Backup completed and placed in $BACKUPDIR/pandorafms_backup_$TIMESTAMP.tar.gz"
+	fi
+
+else 
+
+# Make the backup restore process
+
+	echo "Detected Pandora FMS backup at $SOURCEBACKUP, please wait..."
+	tar xvzf $SOURCEBACKUP > /dev/null 2> /dev/null
+
+	echo "Dropping current database"
+	echo "drop database $DBNAME;" | mysql -u $DBUSER -p$DBPASS -h $DBHOST
+
+	echo "Restoring backup database"
+	echo "create database $DBNAME;" | mysql -u $DBUSER -p$DBPASS -h $DBHOST
+	cat *.sql | mysql -u $DBUSER -p$DBPASS -h $DBHOST -D $DBNAME
+
+	if [ $RESTOREFILES == 1 ]
+	then
+		echo "Restoring files and configuration"
+#Need testing, not finished!
+		echo "mv var/spool/pandora/* /var/spool/pandora"
+		echo "mv etc/pandora/* /etc/pandora"
+		echo "BACKUPBASEPATH=`echo $PANDORAPATH | cut -c2-`"
+		echo "mv $BACKUPBASEPATH $PANDORAPATH"
+	fi
+	
+	cd /tmp
+	rm -Rf /tmp/$TIMESTAMP
+
+	echo "Done. Backup in $SOURCEBACKUP restored"
+fi
+
+
+exit 0