Welcome to Pandora FMS Community!

Find answers, ask questions, and connect with our community around the world.

Welcome to Pandora FMS Community Forums Community support Pandora FMS Agent startup script mod

  • Pandora FMS Agent startup script mod

    Posted by ivanhoe on November 25, 2009 at 14:23

    I’ve noticed that you remove the PIDFILE in the new versions of the Pandora FMS Agent script.

    Unfortunately, I have a watchdog program that needs the PIDFILE to monitor the status of the Agent. So I had to keep that feature by myself in the script file. Maybe someone else is in a similar situation.

    I’ve taken the opportunity to introduce some improvements to the startup script. I think that now it’s more complete and consistent. For example, now it doesn’t write those nasty nohup.out files at /etc/init.d or /etc/pandora. And it doesn’t sleep 1 second during start or stop if it isn’t necessary. It also checks if more than one Agent is running, etc. Personally, I feel more comfortable with these enhancements.

    I’ve tested it with Debian/Ubuntu and Fedora. I’m not sure how it will behave in other distros or BSD, sorry.

    Here is the code (attached is a copy). Use it under your responsibility.

    Please, feel free to use it to improve the current version or as a source for new ideas.

    pandora_agent_daemon

    #!/bin/sh
    
    # Pandora FMS Linux Agent, startup script
    # Copyright (c) 2006-2009 Artica ST, 
    # Linux Version (generic), for SuSe and Debian/Ubuntu.
    # other Linux distros could not work properly without modifications
    # v3.0.1 Build 091104
    # http://www.pandorafms.com
    
    ### BEGIN INIT INFO
    # Provides: pandora_agent
    # Required-Start: $network
    # Required-Stop: $network
    # Default-Start:  S 2 3 4 5
    # Default-Stop: 0 1 6
    # Short-Description: Startup script daemon for Pandora FMS agent
    ### END INIT INFO
    
    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
    
    pidof_pandora() {
    	COLUMNS=250
    	PANDORA_PID=`ps ax | grep $DAEMON | grep -v grep | head -1 | awk '{ print $1 }'`
    	echo $PANDORA_PID
    }
    
    if [ ! -x "$DAEMON" ]
    then
    	echo "Cannot locate an executable Pandora FMS Agent at $DAEMON, please check setup"
    	exit 1
    fi
    
    case "$1" in
        start)
    	PANDORA_PID=`pidof_pandora`
    	if [ -n "$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 > /dev/null 2> $LOGFILE &
    	if [ -z "$!" ]
    		then
    		sleep 1
    		PANDORA_PID=`pidof_pandora`
    	else
    	PANDORA_PID=$!
    	fi
    	if [ -z "$PANDORA_PID" ]
    	then
    		echo "Cannot initialize Pandora FMS Agent"
    		exit 1
    	fi
    	echo $PANDORA_PID > $PIDFILE
    	echo "Pandora FMS Agent is now running with PID $PANDORA_PID"
    	;;
    	
        stop)
    	# check if any agent is running (this isn't really necessary)
    	PANDORA_PID=`pidof_pandora`
    	if [ -z "$PANDORA_PID" ]
    	then
    		echo "Pandora FMS Agent is not running, cannot stop it. Aborting now..."
    		exit 1
    	fi
    	echo "Stopping Pandora Agent"
    	# first check pidfile
    	if [ -f "$PIDFILE" ]
    	then
    		kill -15 `cat $PIDFILE` > /dev/null 2>&1	
    		[ -f "$PIDFILE" ] && rm -f $PIDFILE
    	fi
    	# check if any agent is running
    	COUNTER=10
    	PANDORA_PID=`pidof_pandora`
    	while [ "$COUNTER" -gt 0 ] && [ -n "$PANDORA_PID" ]
    	do
    		kill -15 $PANDORA_PID > /dev/null 2>&1
    		sleep 1
    		COUNTER=$(( $COUNTER - 1 ))
    		PANDORA_PID=`pidof_pandora`
    	done
    	# last resort if any agent is still alive
    	[ -n "$PANDORA_PID" ] && kill -9 $PANDORA_PID > /dev/null 2>&1
    	;;
    
        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 "Usage: /etc/init.d/pandora_agent_daemon {start|stop|restart|status|force-reload}"
    	exit 1
    esac
    
    ivanhoe replied 15 years, 2 months ago 2 Members · 2 Replies
  • 2 Replies
  • Sancho

    Administrator
    December 2, 2009 at 02:32
    2321 Karma points
    Community awards: bulb Bright ideas
    Community rank: tentacle_master_icon Tentacle Master
    Like it
    Up
    0
    Down
    Drop it
    ::

    Problems with PID, IMHO is not very confiable, for example if your systems stops (power failure) you can have a PID but not a process :(, the same happen if your not stopping well the agent (kill -9), setup is not correct (don’t have a script to stop it with Kxxpandora_agent_daemon) or something like that, you add more complexity to the daemon management 🙁

    Whatever, we have also a nice watchdog who could be used for server or agents, search in “extras” directory in the SVN.

  • ivanhoe

    Member
    December 3, 2009 at 21:31
    0 Karma points
    Community rank: tentacle-noob-1 Tentacle noob
    Like it
    Up
    0
    Down
    Drop it
    ::

    I see. Anyway, as you can see in my script I don’t check or trust any pidfile when I need to start the agent. I call pidof_pandora instead. 🙂

    I know it doesn’t make sense, but the point is that my monitor didn’t run without a pidfile. Now it works like a charm.

    Anyway, I will look for that watchdog in “extras” directory. Sounds interesting.

    Thanks!