ZABBIX Forums  

Go Back   ZABBIX Forums > Zabbix Discussions and Feedback > Zabbix Cookbook

Reply
 
Thread Tools Display Modes
  #1  
Old 01-07-2008, 14:56
txalin txalin is offline
Member
 
Join Date: Jan 2008
Posts: 31
Default Standar init.d script working on debian and red hat distros

Hi all,

I have created an standar init.d script for zabbix agentd. It have been tested on debian and red hat based distros with success. i think will work ok in any UNIX based OS like AIX or HP/UX, but it haven't been tested yet, when i test it will update this thread.

Regarding the automatic startup on boot, if the distibution uses the etc/rcs for launching won't be any problem, for example the automatic startup will not work on AIX because it don't use the rcs.

License:

Feel free to use or modify it as you want, WTFPL license (http://en.wikipedia.org/wiki/WTFPL ), I assume no responsibility for any damages this script may cause - use at your own risk. Anyway, it's very very difficult to cause any problem, maybe stop the agent, but thats all

Requirement:

-User zabbix on the machine, he controls the zabbix applications such zabbix_agentd and so. If you have a different user just change the user on this line on start and stop functions:

"if [ $(whoami) != "zabbix" ];then"

Features:

- All the code commented for better understanding.
- No configuration file needed, so don't worry about zabbix_agentd.conf or directories and such things.
- Small help when you execute the script with any parameter different to start, stop, restart.

On start:

- Checks if the agent was running before starting it.
- Checks if the user is able to launch the agent, and if not, it launch it as zabbix
- Checks if the agent was correctly launched and tells you how many instances you have running. If the agent didn't launch properly show a message.

On stop:

- Checks if the user is able to launch the agent, and if not, it launch it as zabbix
- Checks if the agent is running, if it's not running shows a message
- Try to stop the agent for five times, if after that it can't be launched shows you a message, also, if it's correctly launched it shows you a message.

How to use it: Create a file on /etc/init.d/ called zabbix and copy the code below and change permissions to 755 (executable file).

Also here you have the symlinks to startup the agent on machine boot.

ln -s /etc/init.d/zabbix /etc/rc2.d/S84zabbix
ln -s /etc/init.d/zabbix /etc/rc3.d/S84zabbix
ln -s /etc/init.d/zabbix /etc/rc5.d/S84zabbix
ln -s /etc/init.d/zabbix /etc/rc4.d/S84zabbix

ln -s /etc/init.d/zabbix /etc/rc6.d/K15zabbix
ln -s /etc/init.d/zabbix /etc/rc0.d/K15zabbix
ln -s /etc/init.d/zabbix /etc/rc1.d/K15zabbix

Enjoy, feedback will be much appreciated.

Here you have the code:

#! /bin/sh
counter=0
zabbix_counter=$(ps -ef | grep zabbix_agentd | grep -v grep | wc -l)

start(){
echo "-------------------------------------------------------------------"
echo " LAUNCHING ZABBIX AGENT"

if [ $zabbix_counter -gt 0 ]; then
echo " * Zabbix agent was previously running"
echo " * Number of zabbix agentd instances= $zabbix_counter"
echo "-----------------------------------------------------------------"
fi

# Checking if the user is able to start the agent.... if the user is not able to, we make a su to
# the user zabbix and start the agent

if [ $(whoami) != "zabbix" ];then
sudo -u zabbix zabbix_agentd
else
# We are the zabbix user, so we can start the agent.
zabbix_agentd
fi
sleep 10

zabbix_counter=$(ps -ef | grep zabbix_agentd | grep -v grep | wc -l)
if [ $zabbix_counter -gt 0 ]; then
echo " * Zabbix agent succesfully launched"
echo " * Number of zabbix agentd instances= $zabbix_counter"
echo "-------------------------------------------------------------------"
else
echo " * Zabbix agent can't be launched, check zabbix logs"
echo "-------------------------------------------------------------------"
fi

}

stop(){
# Checking if the user is able to start the agent.... if the user is not able to, we make a su to
# the user zabbix and kill the agent. Also we try to kill it for 5 times using a counter, if at
# the fith try the agent is still there, we launch a message to the console.

echo "-------------------------------------------------------------------"
echo " STOPPING ZABBIX AGENT"

if [ $zabbix_counter -eq 0 ]; then
echo " * Zabbix agent was not running on this machine"
echo "-------------------------------------------------------------------"
fi

while [ $zabbix_counter -gt 0 ] && [ $counter -lt 5 ] ; do

let counter=counter+1

echo " * Number of Attempts (Max 5)=$counter"
echo " * Stopping zabbix.."
echo " * Number of zabbix agentd instances= $zabbix_counter"

if [ $(whoami) != "zabbix" ];then
sudo -u zabbix killall zabbix_agentd > /dev/null &
else
killall zabbix_agentd > /dev/null &
fi

sleep 10
# We make a 10 seconds delay to avoid the state defunct of the process, if we delete it,
# will enter again on the iteration and will show an error when he tries to kill the process.
# After 10 seconds we check again the number of zabbix_agentd processes running,
# if it's 0 will exit of the iteration and follow the code

zabbix_counter =$(ps -ef | grep zabbix_agentd | grep -v grep | wc -l)

done

if [ $zabbix_counter -gt 0 ]; then
echo "It was not possible to stop the agent, look at th zabbix logs"
echo "-------------------------------------------------------------------"
fi

if [ $zabbix_counter -eq 0 ]; then
echo " * Zabbix agent properly stopped"
echo "-------------------------------------------------------------------"
fi

}

restart(){
stop
# give stuff some time to stop before we restart
sleep 10
# Now we can start the agent again.
start
}



case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo "Usage: zabbix {start|stop|restart}"
exit 1
esac

exit 0

Last edited by txalin; 01-07-2008 at 15:09.
Reply With Quote
  #2  
Old 23-06-2012, 01:17
ITBlogger ITBlogger is offline
Junior Member
 
Join Date: Jun 2012
Posts: 2
Default

Thanks much for the script txalin. It solved a problem I was having with the default init.d scripts provided by Zabbix where the script wasn't properly shutting down the running processes.

Just one thing, on line 71, I ran into a problem with it as written because of an extra space between "zabbix_counter" and "=".

It worked just fine when the space was removed:

zabbix_counter=$(ps -ef | grep zabbix_agentd | grep -v grep | wc -l)
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 17:37.