From 3df377e0d59d3e572c47bd8d0728f291ca5e5b49 Mon Sep 17 00:00:00 2001
From: slerena <slerena@gmail.com>
Date: Tue, 11 Aug 2009 16:58:01 +0000
Subject: [PATCH] 2009-08-11  Sancho Lerena <slerena@artica.es>

	* linux/pandora_agent_installer: Some fixes and support for Ubuntu/SUSE
	specific installer.

	* linux/plugins/grep_log: Support for more than module over the same file.

	* linux/pandora_agent_daemon: Fixed problems with PID control, rewrite
	function, now should works on any linux distro.

	* linux/pandora_agent.conf: Added new inventory plugin and minor changes in
	default modules.

	* linux/pandora_agent: Updated version and build



git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@1841 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
---
 pandora_agents/ChangeLog                     |  15 +++
 pandora_agents/linux/pandora_agent           |  11 +-
 pandora_agents/linux/pandora_agent.conf      |  37 ++-----
 pandora_agents/linux/pandora_agent_daemon    |  95 ++++++++++-------
 pandora_agents/linux/pandora_agent_installer | 105 ++++++++++++++-----
 pandora_agents/linux/plugins/grep_log        |   2 +-
 6 files changed, 172 insertions(+), 93 deletions(-)

diff --git a/pandora_agents/ChangeLog b/pandora_agents/ChangeLog
index 8c06ad494c..0e4d62f299 100644
--- a/pandora_agents/ChangeLog
+++ b/pandora_agents/ChangeLog
@@ -1,3 +1,18 @@
+2009-08-11  Sancho Lerena <slerena@artica.es>
+
+	* linux/pandora_agent_installer: Some fixes and support for Ubuntu/SUSE
+	specific installer.
+	
+	* linux/plugins/grep_log: Support for more than module over the same file.
+	
+	* linux/pandora_agent_daemon: Fixed problems with PID control, rewrite
+	function, now should works on any linux distro. 
+	
+	* linux/pandora_agent.conf: Added new inventory plugin and minor changes in
+	default modules.
+
+	* linux/pandora_agent: Updated version and build 
+
 2009-08-11  Miguel de Dios  <miguel.dedios@artica.es>
 
 	* ChangeLog: sorry, I had got a mistake with copy&paste.
diff --git a/pandora_agents/linux/pandora_agent b/pandora_agents/linux/pandora_agent
index 9c54f203dc..072d3db2bc 100755
--- a/pandora_agents/linux/pandora_agent
+++ b/pandora_agents/linux/pandora_agent
@@ -1,14 +1,13 @@
 #!/bin/bash
 # **********************************************************************
-# Pandora FMS Generic Host Agent
-# GNU/Linux version 2.0
-# (c) 2003-2008 Sancho Lerena <slerena@gmail.com>
-# with the help of many people. Please see http://pandora.sourceforge.net
+# Pandora FMS Generic Linux Agent
+# (c) 2009 Artica Soluciones Tecnológicas SL
+# with the help of many people. Please see http://pandorafms.org
 # This code is licensed under GPL 2.0 license.
 # **********************************************************************
 
-AGENT_VERSION=2.0
-AGENT_BUILD=081210
+AGENT_VERSION=3.0
+AGENT_BUILD=090608
 
 # **********************************************************************
 # function configure_agent()
diff --git a/pandora_agents/linux/pandora_agent.conf b/pandora_agents/linux/pandora_agent.conf
index 34b8c3764d..3254471fbc 100755
--- a/pandora_agents/linux/pandora_agent.conf
+++ b/pandora_agents/linux/pandora_agent.conf
@@ -1,8 +1,7 @@
 # Base config file for Pandora FMS agents
-# Version 2.0
+# Version 3.0
 # Licensed under GPL license v2,
-# (c) 2003-2008 Artica Soluciones Tecnologicas
-# (c) 2003-2008 Sancho Lerena <slerena@gmail.com>
+# (c) 2003-2009 Artica Soluciones Tecnologicas
 # please visit http://pandora.sourceforge.net
 
 # General Parameters
@@ -56,8 +55,8 @@ transfer_mode tentacle
 # is much more safe.
 # cron_mode 
 
-# If set to 1 allows the agent to be configured via the web console.
-# remote_config 0
+# If set to 1 allows the agent to be configured via the web console (Only Enterprise version) 
+# remote_config 1
 
 # Secondary server configuration
 # ==============================
@@ -91,12 +90,10 @@ module_description User CPU Usage (%)
 module_end
 
 module_begin 
-module_name cpu_sys
+module_name Load Average
 module_type generic_data
-module_exec vmstat 1 2 | tail -1 | awk '{ print $14 }'
-module_max 100
-module_min 0
-module_description System CPU Usage (%)
+module_exec uptime | awk '{ print $10 }' | cut -f 1 -d ","
+module_description Average process in CPU (Last minute) 
 module_end
 
 module_begin 
@@ -109,15 +106,6 @@ module_description Free disk Percentage of root partition
 module_end
 
 
-module_begin
-module_name disk_var_free
-module_type generic_data
-module_exec df -kh /var | tail -1 | awk '{ print 100-$5 }'
-module_max 100
-module_min 0
-module_description Free disk Percentage of var partition
-module_end
-
 module_begin
 module_name memfree
 module_type generic_data
@@ -152,16 +140,13 @@ module_type async_string
 module_exec last | head -1
 module_end
 
-#SNMPget module example
-##module_begin
-##module_name Remote_Cisco_Port14_status
-##module_type generic_proc
-##module_exec snmpget -v 1 -c artica06 192.168.50.250 IF-MIB::ifOperStatus.13
-##module_end
-
 # Plugin example
 
 # This parses /var/log/syslog file, under the module name "syslog"
 # And search for "ssh" string into it, sending only that information.
 
 module_plugin grep_log /var/log/syslog Syslog ssh
+
+# Plugin for inventory on the agent (Only Enterprise)
+
+# module_plugin inventory 1 cpu ram video nic hd cdrom software
diff --git a/pandora_agents/linux/pandora_agent_daemon b/pandora_agents/linux/pandora_agent_daemon
index 8711e2ea4e..1963802f41 100755
--- a/pandora_agents/linux/pandora_agent_daemon
+++ b/pandora_agents/linux/pandora_agent_daemon
@@ -1,8 +1,11 @@
 #!/bin/bash
-# Init script for Pandora FMS agent
-# Generic GNU/Linux version
-# Sancho Lerena, <slerena@gmail.com>
-# v3.0
+
+# Pandora FMS Linux Agent, startup script
+# Copyright (c) 2006-2009 Artica ST, <info@artica.es>
+# Linux Version (generic), for SuSe and Debian/Ubuntu.
+# other Linux distros could not work properly without modifications
+# v3.0 Build 090810
+# http://www.pandorafms.com
 
 ### BEGIN INIT INFO
 # Provides: pandora_agent
@@ -16,9 +19,15 @@
 PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin
 PANDORA_PATH=/etc/pandora
 DAEMON=/usr/bin/pandora_agent
-PIDFILE=/var/run/pandora_agent.pid
 LOGFILE=/var/log/pandora_agent.log
 
+# This function replace pidof, not working in the same way in different linux distros
+
+function pidof_pandora () (
+	PANDORA_PID=`ps aux | grep $DAEMON | grep -v grep | head -1 | awk '{ print $2 }'`
+	echo $PANDORA_PID
+)
+
 if [ ! -f $DAEMON ]
 then
 	echo "Pandora FMS Agent not found at $DAEMON, please check setup"
@@ -26,37 +35,51 @@ then
 fi
 
 case "$1" in
-  start)
-        PANDORA_PID=$(pidof -x $DAEMON)
-	if [ ! -z "$PANDORA_PID" ]
-	then
-            echo "Pandora FMS Agent is currently running on this machine with PID $PANDORA_PID"
-            echo "Cannot launch again. Aborting."
-            exit 1
-        fi
-        nohup $DAEMON $PANDORA_PATH 2> $LOGFILE & PANDORA_PID=$!
-        echo $PANDORA_PID > $PIDFILE
-	echo "Pandora FMS Agent is now running with PID $PANDORA_PID"
-        ;;
-  stop)
-	PANDORA_PID=$(pidof -x $DAEMON)
-        if [ -z "$PANDORA_PID" ]
-        then
-		echo "Pandora FMS Agent is not running, cannot stop it. Aborting now..."
-		exit 1
-        else
-		echo "Stopping Pandora Agent."
-		kill $PANDORA_PID > /dev/null 2>&1
-	        rm -f $PANDORA_PID        
-	fi
-        ;;
-  force-reload|restart)
-        $0 stop
-		sleep 3
-        $0 start
-        ;;
-  *)
-        echo "Uso: /etc/init.d/pandora_agent_daemon {start|stop|restart|force-reload}"
+	start)
+		PANDORA_PID=`pidof_pandora`
+		if [ ! -z "$PANDORA_PID" ]
+		then
+			echo "Pandora FMS Agent is currently running on this machine with PID $PANDORA_PID"
+			echo "Cannot launch again. Aborting."
+			exit 1
+		fi
+		nohup $DAEMON $PANDORA_PATH 2> $LOGFILE & 
+		sleep 1
+		PANDORA_PID=`pidof_pandora`
+		echo "Pandora FMS Agent is now running with PID $PANDORA_PID"
+	;;
+	
+	stop)
+		PANDORA_PID=`pidof_pandora`
+		if [ -z "$PANDORA_PID" ]
+		then
+			echo "Pandora FMS Agent is not running, cannot stop it. Aborting now..."
+			exit 1
+		else
+			echo "Stopping Pandora Agent."
+			kill $PANDORA_PID > /dev/null 2>&1        
+		fi
+	;;
+
+	status)
+		PANDORA_PID=`pidof_pandora`	
+		if [ -z "$PANDORA_PID" ]
+		then
+			echo "Pandora FMS Agent is not running."
+		else
+			echo "Pandora FMS Agent is running with PID $PANDORA_PID."
+		fi
+		exit 0
+	;;
+
+	force-reload|restart)
+		$0 stop
+		sleep 2
+		$0 start
+	;;
+	
+	*)
+        echo "Uso: /etc/init.d/pandora_agent_daemon {start|stop|restart|status|force-reload}"
         exit 1
 esac
 
diff --git a/pandora_agents/linux/pandora_agent_installer b/pandora_agents/linux/pandora_agent_installer
index ea0145fa52..c40f07bd7c 100755
--- a/pandora_agents/linux/pandora_agent_installer
+++ b/pandora_agents/linux/pandora_agent_installer
@@ -1,13 +1,14 @@
 #!/bin/bash
-# **********************************************************************
-# Pandora FMS Agent - Generic Host Agent Installer
-# GNU/Linux version
-# (c) 2007 Sancho Lerena <slerena@gmail.com>
-# Please see http://pandora.sourceforge.net
+
+# Pandora FMS Agent Installer (c) 2008-2009 Artica ST
+# Linux Version (generic), for SuSe and Debian/Ubuntu only
+# other Linux distros could not work properly without modifications
+# Please see http://www.pandorafms.org
+# v3.0 Build 090810
 # This code is licensed under GPL 2.0 license.
 # **********************************************************************
 
-PI_VERSION=2.1
+PI_VERSION=3.0
 PANDORA_BIN=/usr/bin/pandora_agent
 PANDORA_HOME=/usr/share/pandora_agent
 PANDORA_TEMP=/var/spool/pandora
@@ -30,6 +31,41 @@ then
 	exit 1
 fi
 
+get_distro () {
+
+	# Get Linux Distro type and version
+	if [ -f "/etc/SuSE-release" ]
+	then
+		OS_VERSION=`cat /etc/SuSE-release | grep VERSION | cut -f 3 -d " "`
+		LINUX_DISTRO=SUSE
+	else
+		if [ -f "/etc/lsb-release" ]
+		then
+			OS_VERSION=`cat /etc/lsb-release | grep DISTRIB_RELEASE | cut -f 2 -d "="`
+			LINUX_DISTRO=UBUNTU
+			OS_VERSION="UBUNTU $OS_VERSION"
+		else
+			if [ -f "/etc/debian_version" ]
+			then
+				OS_VERSION=`cat /etc/debian_version`
+				OS_VERSION="DEBIAN $OS_VERSION"
+				LINUX_DISTRO=DEBIAN
+			else
+				if [ -f "/etc/fedora-release" ]
+				then
+					OS_VERSION=`cat /etc/fedora-release | cut -f 4 -d " "`
+					OS_VERSION="FEDORA $OS_VERSION"
+					LINUX_DISTRO=FEDORA
+				else
+					LINUX_DISTRO=GENERIC
+					OS_VERSION=`uname -r`
+				fi
+			fi
+		fi
+	fi
+	echo $LINUX_DISTRO
+}
+
 uninstall () {
         echo "Removing Pandora FMS Agent..."
         rm -Rf $PANDORA_BIN
@@ -43,6 +79,12 @@ uninstall () {
 }
 
 install () {
+
+		DISTRO=`get_distro`
+		OLDFILENAMETMP=`date +"%Y-%m-%d"`
+		
+		echo "Detecting Linux distribution: $DISTRO"
+		
         if [ -f $PANDORA_HOME ] && [ "$FORCE" = "0" ]
         then
                 echo "Seems that default dir already exists. Please use --force to"
@@ -56,6 +98,7 @@ install () {
         then
                 echo "Seems that $PANDORA_BIN already exists. Please use --force to"
                 echo "force installer to reinstall overwriting it"
+                echo " "
                 exit
         else
                 echo "Checking Pandora FMS Agent on $PANDORA_BIN...."
@@ -63,11 +106,11 @@ install () {
 
         # Create directories
         echo "Creating Pandora FMS Agent home directory at $PANDORA_HOME ..."
-        mkdir $PANDORA_HOME
-        mkdir $PANDORA_TEMP
-        mkdir $PANDORA_TEMP/data_out
-        mkdir $PANDORA_CFG
-	mkdir /var/log/pandora
+        mkdir $PANDORA_HOME 2> /dev/null
+        mkdir $PANDORA_TEMP 2> /dev/null
+        mkdir $PANDORA_TEMP/data_out 2> /dev/null
+        mkdir $PANDORA_CFG 2> /dev/null
+		mkdir /var/log/pandora 2> /dev/null
 
         # Create logfile
         if [ ! -z "`touch $PANDORA_LOG`" ]
@@ -88,7 +131,14 @@ install () {
         cp pandora_agent_daemon $PANDORA_HOME
 
         echo "Copying Pandora FMS Agent configuration file to $PANDORA_HOME/pandora_agent.conf..."
-        cp pandora_agent.conf $PANDORA_HOME
+        if [ -e $PANDORA_HOME/pandora_agent.conf ]
+        then
+        	cp $PANDORA_HOME/pandora_agent.conf $PANDORA_HOME/pandora_agent.conf.$OLDFILENAMETMP
+        	echo "Backing up old configuration file to $PANDORA_HOME/pandora_agent.conf.$OLDFILENAMETMP"
+        fi
+        
+        echo "Copying default agent configuration to $PANDORA_HOME/pandora_agent.conf"
+        cp pandora_agent.conf $PANDORA_HOME/pandora_agent.conf
         chmod 600 $PANDORA_HOME/pandora_agent.conf
 
         echo "Copying Pandora FMS Agent plugins to $PANDORA_HOME/plugins..."
@@ -99,32 +149,39 @@ install () {
 		cp tentacle_client $TENTACLE
 
         echo "Linking Pandora FMS Agent plugins directory to $PANDORA_CFG/plugins..."
-        ln -s $PANDORA_HOME/plugins $PANDORA_CFG
+        rm $PANDORA_CFG/plugins 2> /dev/null
+        ln -s $PANDORA_HOME/plugins $PANDORA_CFG 2> /dev/null
 
         echo "Linking Pandora FMS Agent configuration to $PANDORA_CFG/pandora_agent.conf..."
-        ln -s $PANDORA_HOME/pandora_agent.conf $PANDORA_CFG
-
-        echo "Linking Pandora FMS Agent user configuration to $PANDORA_CFG/pandora_user.conf..."
-        ln -s $PANDORA_HOME/pandora_user.conf $PANDORA_CFG
+        rm $PANDORA_CFG/pandora_agent.conf 2> /dev/null
+        ln -s $PANDORA_HOME/pandora_agent.conf $PANDORA_CFG/pandora_agent.conf 
         
         echo "Setting secure permissions and ownership for all Pandora FMS Agent files..."
         chown -R root $PANDORA_HOME
         chmod -R 600 $PANDORA_TEMP/data_out
         chmod 640 $PANDORA_LOG
-        chgrp 3 $PANDORA_LOG
+        chgrp root $PANDORA_LOG
 
         echo "Linking start-up daemon script at $PANDORA_STARTUP";
         cp pandora_agent_daemon $PANDORA_STARTUP
 
-        echo "Linking start-up daemon script to /etc/rc2.d";
-        ln -s /etc/init.d/pandora_agent_daemon /etc/rc2.d/S90pandora_agent
-
-        chown -R root $PANDORA_BIN
+		if [ "$DISTRO" == "UBUNTU" ]
+		then
+	        echo "Linking start-up daemon script to /etc/rc2.d";
+	        ln -s /etc/init.d/pandora_agent_daemon /etc/rc2.d/S90pandora_agent
+		else
+			INITLV=`cat /etc/inittab | grep "[0-9]\:initdefault" | cut -f 2 -d ":"`
+			echo "Linking start-up daemon script to /etc/rc.d/rc$INITLV.d";
+	        ln -s /etc/init.d/pandora_agent_daemon /etc/rc.d/rc$INITLV.d/S90pandora_agent
+		fi
+		
+        chown -R root:root $PANDORA_BIN
+        
         echo "Done."
         echo " "
         echo "You have your startup script ready at $PANDORA_STARTUP"
 		echo " "
-		echo "Tentacle is the default transfer mode"
+		echo "Tentacle is the default transfer mode since 2.0 version."
         echo "If you want to use SSH, firstly you need to copy your public SSH keys ($HOME/.ssh/id_dsa)"
         echo "under /home/pandora/.ssh/authorized_keys on your Pandora FMS Server host"
         echo "You also need to setup your $PANDORA_CFG/pandora_agent.conf config file"
@@ -141,7 +198,7 @@ help () {
 
 # Script banner at start
 echo " "
-echo "Pandora FMS Agent Installer $PI_VERSION (c) 2009 ArticaST"
+echo "Pandora FMS Agent Installer $PI_VERSION (c) 2008-2009 ArticaST"
 echo "This program is licensed under GPL2 Terms. http://pandorafms.com"
 echo " "
 
diff --git a/pandora_agents/linux/plugins/grep_log b/pandora_agents/linux/plugins/grep_log
index 67fb215a20..2e46fbbdac 100755
--- a/pandora_agents/linux/plugins/grep_log
+++ b/pandora_agents/linux/plugins/grep_log
@@ -226,7 +226,7 @@ if (! -e $Log_file) {
 }
 
 # Create index file if it does not exist
-$Idx_file=$Idx_dir . basename($Log_file) . ".idx";
+$Idx_file=$Idx_dir . $Module_name . "_" . basename($Log_file) . ".idx";
 if (! -e $Idx_file) {
 	create_idx();
 	exit 0;