Welcome to Pandora FMS Community › Forums › Community support › The chkconfig-compatible start/stop scripts
-
The chkconfig-compatible start/stop scripts
Posted by daggett on December 18, 2006 at 17:03As promised, I modified the start/stop scripts of Pandora Agent and Pandora Server because they actually are not compatible with chkconfig, which is a quit useful and time-saving software.
[code:1]#!/bin/bash
# Init script for Pandora FMS agent
# Script de arranque del agente Pandora FMS
# Generic GNU/Linux version
# Version para GNU/Linux (generico)
# Sancho Lerena,
# v1.2PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin
PANDORA_PATH=/opt/pandora/pandora_agent/
DAEMON=pandora_agent.sh
PIDFILE=/var/run/pandora_agent.pid# chkconfig: 345 99 0
# description: Pandora_agent is a UNIX script that monitors the machines
# on which it runs. It collects informations about hardware and software
# then send them in a XML file via secured SSH connection to the server
# The Server parses, then stores , the XML data into a Mysql DB.
# processname: $DAEMON
# config: $PANDORA_PATH/pandora_agent.conf
# pidfile: $PIDFILE
#
### 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: The pandora_server
# Description: Pandora_agent is a UNIX script that monitors the machines
# on which it runs. It collects informations about hardware and software
# then send them in a XML file via secured SSH connection to the server
# The Server parses, then stores , the XML data into a Mysql DB.
### END INIT INFO# Source function library.
. /etc/init.d/functionsif [ ! -f $PANDORA_PATH/$DAEMON ]
then
echo “Pandora Agent not found at $PANDORA_PATH/$DAEMON, please check setup”
exit
ficase “$1” in
start)
if [ -f $PIDFILE ]
then
echo “Pandora Agent is currently running on this machine. Aborting now…”
exit
fi
$PANDORA_PATH/$DAEMON $PANDORA_PATH >> $PANDORA_PATH/pandora.log & MYPID=$!
echo $MYPID > $PIDFILE
echo “Pandora Agent is now running with PID $MYPID”
;;
stop)
if [ -f $PIDFILE ]
then
echo “Stopping Pandora Agent.”
PID_2=`cat $PIDFILE`
if [ ! -z “`ps -f -p $PID_2 | grep -v grep | grep ‘pandora_agent’`” ]
then
kill -9 $PID_2
fi
rm -f $PIDFILE
else
echo “Pandora Agent is not running, cannot stop it. Aborting now…”
fi
;;
status)
status pandora_agent.sh
;;
force-reload|restart)
$0 stop
$0 start
;;
*)
echo “Uso: /etc/init.d/pandora_agent {start|stop|restart|force-reload|status}”
exit 1
esacHere are the changes:
I added a few lines above BEGIN INIT INFO to make the script compatible with chkconfig, and added the file “/etc/init.d/functions to be able to add the “status” possibility.A line must then be added to my installation script to copy this modified script in /etc/init.d and execute the command: chkconfig –add pandora_agent_daemon to add all the links as defined in “# chkconfig: 345 99 0”.
I ‘ll post soon the script for the server.
bye for nowSancho replied 18 years, 1 month ago 3 Members · 7 Replies -
7 Replies
-
::
here it is: the server start/stop script chkconfig-compatible!
#!/bin/sh
# Pandora Data Server startup script
# Sancho Lerena,
# Linux Version (generic)
# v1.2 (Ene/2006)# Configurable path and filenames
PANDORA_HOME=”/opt/pandora/pandora_server”
PANDORA_SERVER_PID=”$PANDORA_HOME/var/pandora_server.pid”
DAEMON=pandora_server.pl# Pandora Start/Stop the pandora server daemon.
#
# chkconfig: 345 90 10
# description: Pandora_server is a UNIX script that manages the monitoring
# informations sent by the pandora_agents monitoring machines
# The Server parses, then stores , the XML data to a Mysql DB.
# processname: $DAEMON
# config: $PANDORA_HOME
# pidfile: $PANDORA_SERVER_PID
#
### BEGIN INIT INFO
# Provides: pandora_server
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 3 4 5
# Default-Stop: 0 1 2 6
# Short-Description: The pandora_server
# Description: Pandora_server is a UNIX script that manages the monitoring
# informations sent by the pandora_agents monitoring machines
# The Server parses, then stores , the XML data to a Mysql DB.
### END INIT INFO# Source function library.
. /etc/init.d/functions# Main script
if [ ! -f $PANDORA_HOME/bin/$DAEMON ]
then
echo “Pandora Data Server not found, please check setup and read manual”
exit
ficase “$1″ in
start)
OLD_PATH=”`pwd`”
if [ -f $PANDORA_SERVER_PID ]
then
echo “Pandora Data Server is currently running on this machine. Aborting now…”
exit
fi
# we check if we have instances of the server already running
NUMPID=`ps aux | grep ‘pandora_server.pl’ | grep -v grep | wc -l`
if [ $NUMPID -gt 0 ]
then
echo Warning! It seems that there are some instances of pandora server already running
echo Starting anyway… but you may have some problems.
fi
# we change to the pandora server bin directory
cd $PANDORA_HOME/bin
su -s /bin/bash – pandora -c ./$DAEMON $PANDORA_HOME -D
# the server may take some time to start, so we need to pause before collecting PIDs
sleep 2
# collecting first line (first PID with head -1) then the second (tail -1). It doesn’t matter if MYPID==MYPID2
MYPID=`ps aux | grep ‘pandora_server.pl’ | grep -v grep | tail -2 | head -1 | awk ‘{print $2}’`
MYPID2=`ps aux | grep ‘pandora_server.pl’ | grep -v grep | tail -1 | awk ‘{print $2}’`
if [ ! -z “$MYPID” ]
then
# there is at least one PID, so server is running, so put PID in the .pid file and create this file
echo $MYPID > $PANDORA_SERVER_PID
echo $MYPID2 >> $PANDORA_SERVER_PID
echo “Pandora Data Server is now running with PID $MYPID and $MYPID2”
else
# no PID collected, the server is not running
echo “Cannot start Pandora Data Server. Aborted”
fi
cd “$OLD_PATH”
;;
stop)
if [ -f $PANDORA_SERVER_PID ]
then
echo “Stopping Pandora Data Server”
# fetching PID from the .pid file it doesn’t matter if PID_1==PID_2
PID_1=`cat $PANDORA_SERVER_PID | head -1`
PID_2=`cat $PANDORA_SERVER_PID | tail -1`
# we check if PID_1 and PID_2 are still existing, if yes then we kill them
PIDS=`ps -F -p $PID_1 $PID_2 | grep -v grep | grep ‘pandora_server.pl’| awk ‘{print $2}’`
if [ ! -z “$PIDS” ]
then
kill $PIDS 2> /dev/null > /dev/null
fi
echo “Stopping PID: ” $PIDS
# we check if our PIDs were killed or not, we grep pandora_server. because of zombies loosing their “pl”
PIDS=`ps -F -p $PID_1 $PID_2 | grep -v grep | grep ‘pandora_server.’| awk ‘{print $2}’`
if [ ! -z “$PIDS”]
then
echo “Following PID didn’t stop, sending signal 9 to ” $PIDS
kill -9 $PIDS 2> /dev/null > /dev/null
fi
rm -f $PANDORA_SERVER_PID
echo Server Stopped
else
echo “Pandora Data Server is not running, cannot stop it.”
fi
;;
status)
status $DAEMON
;;
force-reload|restart)
$0 stop
$0 start
;;
*)
echo “Usage: pandora_network {start|stop|restart|status}”
exit 1
esac
As for the agent, I added the few commented lines at the top of the file next the var declarations to make the script compatible with chkconfig.
Then I added the /etc/init.d/functions to be able to add the status possibility.These are just little modifications, I now think that these scripts could be re-writed to use the predefined functions like the other more “conventionnal” scripts.
I’ll do that and post the result in this thread.
bye -
::
here is what I’ve done (edited 19/12/2006 9:30am, scripts perfectly working):
these are now divided in two scripts:
the first one is $PANDORA_AGENT_DIR/pandora_agent_daemon
[code:1]
#!/bin/bash
# Init script for Pandora FMS agent
# Script de arranque del agente Pandora FMS
# Generic GNU/Linux version
# Version para GNU/Linux (generico)
# Sancho Lerena,
# v1.2
# pandora agent daemon startup
PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin
PANDORA_PATH=/opt/pandora/pandora_agent
EXECUTABLE=pandora_agent.sh
PIDFILE=/var/run/pandora_agent.pidRETVAL=0
$PANDORA_PATH/$EXECUTABLE $PANDORA_PATH >> $PANDORA_PATH/pandora.log & MYPID=$!
RETVAL=$?
[ $RETVAL -eq 0 ] && echo $MYPID > $PIDFILE
exit $RETVALthe second one will be found in /etc/init.d/ as pandora_agent:
[code:1]#!/bin/bash
# Init.d script for Pandora FMS agent
# by Denis Chupau, Tranquil I.T. Systems
# this script is under GNU/GPL license.PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin
PANDORA_PATH=/opt/pandora/pandora_agent/
DAEMON=pandora_agent
PIDFILE=/var/run/pandora_agent.pid# chkconfig: 345 99 0
# description: Pandora_agent is a UNIX script that monitors the machines
# on which it runs. It collects informations about hardware and software
# then send them in a XML file via secured SSH connection to the server
# The Server parses, then stores , the XML data into a Mysql DB.
# processname: $DAEMON
# config: $PANDORA_PATH/pandora_agent.conf
# pidfile: $PIDFILE
#
### 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: The pandora_agent
# Description: Pandora_agent is a UNIX script that monitors the machines
# on which it runs. It collects informations about hardware and software
# then send them in a XML file via secured SSH connection to the server
# The Server parses, then stores , the XML data into a Mysql DB.
### END INIT INFO# Source function library.
. /etc/init.d/functionsRETVAL=0
if [ ! -f $PANDORA_PATH/$DAEMON”_daemon ]
then
echo “Pandora Agent not found at $PANDORA_PATH/$DAEMON”_daemon”, please check setup”
exit 1
fi
start() {
if [ ! -f $PIDFILE ]
then
gprintf “Starting %s: ” “$DAEMON”
daemon $PANDORA_PATH/$DAEMON”_daemon”
RETVAL=$?
echo
fi
return $RETVAL
}stop() {
gprintf “Stopping %s daemon: ” “$DAEMON”
killproc $PANDORA_HOME/$DAEMON
RETVAL=$?
[ $RETVAL -eq 0 ] && rm -f $PIDFILE
echo
return $RETVAL
}restart() {
$0 stop
$0 start
}case “$1” in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
force-reload)
restart
;;
status)
status $DAEMON
;;
*)
INITNAME=`basename $0`
gprintf “Usage: %s {start|stop|restart|force-reload|status}n” “$INITNAME”
exit 1
esacexit $RETVAL
Ok, this code takes advantage of the pre-defined functions in “/etc/inid.d/functions” file which enable to display the cool [ OK ] or [ failed ] when trying to start/stop a service, and on Mandriva 2007 Linux distribution all the displayed texts are translated in the default language, so you may see: “starting pandora_agent_daemon [ failed ]” or “Démarrage de pandora_agent_daemon [ echec ]” ( I choosed the “[ failed ]” example because “[ OK ]” is the same in every language! ).
so this script is working now, it’s split in two parts: one in /etc/init.d/ directory that will manage the start and stop of the pandora agent service, and the other in pandora_agent directory which will manage the command line to pass to pandora_agent when starting it.
I will come in a bit with the server scripts.
bye -
::
Cool!
Thanks dude!
When all your scripts, the ones you have in mind, were finished, could you package them into a tar.gz and send it to me? That will help me too test each script on our systems.
Yeah, I know I cop&paste them from the forum, but…:-)Well done, dagget, the contrib directory will be full of your work 🙂
Thanks, have a nice day -
::
Thanks to you, you’re welcome to comment my scripts, I will send a tgz for you to test when I’ll be finished with them.
here are the pandora_server sart/stop scripts:
The first one is the daemon manager, I called it pandora_server_daemon to have the same naming rules with pandora agent, it replaces the pandora_server script:
[code:1]
#!/bin/sh
# Pandora Data Server startup script
# Sancho Lerena,
# Linux Version (generic)
# v1.2 (Ene/2006)# Configurable path and filenames
PANDORA_PATH=”/opt/pandora/pandora_server”
PIDFILE=”/var/run/pandora_server.pid”
DAEMON=pandora_server.pl
LOGFILE=”$PANDORA_PATH/log/pandora_server.log”
PANDORA_USER=”pandora”RETVAL=0
if [ ! -f $PANDORA_PATH/bin/$DAEMON ]
then
echo “Pandora Data Server not found, please check setup and read manual”
exit 1
fi# we change to the pandora server bin directory and start the server
su -s /bin/sh – $PANDORA_USER -c “cd $PANDORA_PATH/bin/; perl $DAEMON $PANDORA_PATH -D >> $LOGFILE”
sleep 2
MYPID=$(ps aux | grep ‘pandora_server.pl’ | grep -v grep | tail -2 | head -1 | awk ‘{print $2}’)
MYPID2=$(ps aux | grep ‘pandora_server.pl’ | grep -v grep | tail -1 | awk ‘{print $2}’)
[ ! -z $MYPID ] && echo $MYPID > $PIDFILE && echo $MYPID2 >> $PIDFILE
if [ -z $MYPID ]
then
exit 1
else
exit 0
fithe second file is called pandora_server and is in /etc/init.d
[code:1]
#!/bin/bash
# Pandora server start/stop script compatible with chkconfig
# by Denis Chupau, Tranquil IT Systems
# published under GNU/GPL license# Configurable path and filenames
PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin
PANDORA_PATH=”/opt/pandora/pandora_server”
PIDFILE=”/var/run/pandora_server.pid”
DAEMON=pandora_server# Pandora Start/Stop the pandora server daemon.
#
# chkconfig: 345 90 10
# description: Pandora_server is a UNIX script that manages the monitoring
# informations sent by the pandora_agents monitoring machines
# The Server parses, then stores , the XML data to a Mysql DB.
# processname: $DAEMON
# config: $PANDORA_PATH
# pidfile: $PIDFILE
#
### BEGIN INIT INFO
# Provides: pandora_server
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 3 4 5
# Default-Stop: 0 1 2 6
# Short-Description: The pandora_server
# Description: Pandora_server is a UNIX script that manages the monitoring
# informations sent by the pandora_agents monitoring machines
# The Server parses, then stores , the XML data to a Mysql DB.
### END INIT INFO# Source function library.
. /etc/init.d/functionsRETVAL=0
if [ ! -f $PANDORA_PATH/$DAEMON”_daemon” ]
then
echo “Pandora Server not found at $PANDORA_PATH/$DAEMON”_daemon”, please check setup”
exit 1
fistart() {
if [ ! -f $PIDFILE ]
then
gprintf “Starting %s: ” “$DAEMON”
daemon $PANDORA_PATH/$DAEMON”_daemon”
RETVAL=$?
echo
fi
return $RETVAL
}stop() {
gprintf “Stopping %s daemon: ” “$DAEMON”
kill $(cat $PIDFILE | tail -1 ) # there are 2 processes running, so we kill the child
killproc $PANDORA_PATH/$DAEMON # then we kill the parent and issue a [OK] or [failed]
RETVAL=$?
[ $RETVAL -eq 0 ] && rm -f $PIDFILE # it’s usually for the lock file: for pid file killproc() does it for us
echo
return $RETVAL
}restart() {
$0 stop
$0 start
}case “$1” in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
force-reload)
restart
;;
status)
status $DAEMON
;;
*)
INITNAME=`basename $0`
gprintf “Usage: %s {start|stop|restart|force-reload|status}n” “$INITNAME”
exit 1
esacexit $RETVAL
NOTE: I changed the PID file, it’s not anymore in $PANDORA_SERVER/var/pandora_server.pid, I don’t know why it was in this directory though. Now the PID file is in /var/run/pandora_server.pid Just like pandora_agent.pid. This file can now be used and handled by standard system scripts.
Ok ,this one was a bit more difficult to do because on my system (don’t know if it’s normal) I have 2 processes running for the agent (both of them perl scripts), so I needed to first do a standard kill for the child process, then kill the parent.
Well it’s working now, and I just have to test them widely to be sure they’re working in any situation, if you want help… your welcome!
bye for now
-
::
yeah, send it to me, please:
[email protected]I´m finishing some other work I have, but next week I´ll be working full-time with those scripts and the pandora packing.
I will test its on RHEL too and Suse.That´s all folk!
Thanks -
-
::
Ok I will send them to you. they are tgz files containing the agent and server as I modified them.
The Start/Stop scripts are full functional, but the status command is working only on a mandriva 2007, I will need to add some code to make it more “universal”.
bye for now
Thank you very much for the scripts, will be a pleasure to put in the official code repository !!