Monitor servers with Ubuntu and Zabbix

Zabbix is a nifty service monitoring application that also keeps a history of each machine, so it can be used for trend analysis as well as "normal" monitoring. It can be used to monitor just about anything, and uses a client agent for windows, linux, BSD, Solaris and others. I use it for an easy to use overview of how all the servers are keeping, and its useful to have a look at when users start moaning, because at a glance you can see what is happening. It can also send alerts based on a huge range of criteria, so you could know before the users even get a chance to moan. This installation is using an Ubuntu (6.0.6 Dapper) virtual machine to do the monitoring. Ive made a 4Gb HDD, and given it 64M of RAM, which should be enough.

Install the server

Install prerequisites (this was a "install LAMP server" install, you may need to install apache, php and gd as well):

sudo apt-get install libsnmp9-dev libc6-dev libmysqlclient12-dev build-essential

Add a zabbix user:

sudo useradd -m zabbix 

Downlad from http://www.zabbix.com/download.php - the source package can be used for both server and agents.

tar zxvf zabbix-?.?.?.tar.gz
cd zabbix-?.?.?

Prepare the database (if you have just installed, remember that mysql will have no root password at the moment, best set one! mysqladmin -uroot password 'new password')

mysql -uroot -p -e"create database zabbix;"
mysql -uroot -p -e"grant all privileges on zabbix.* to zabbix@localhost identified by 'enter-password-here';"
mysql -uzabbix -p zabbix < create/mysql/schema.sql #Enter the new zabbix password you made up
mysql -uzabbix -p zabbix < create/data/data.sql #The new zabbix password again

Now build it

(I also want to monitor the server, so I am including --enable-agent, you can leave this off if you want to)

./configure  --enable-server --enable-agent --with-mysql --with-net-snmp
sudo make install

Now edit /etc/zabbix/zabbix_server.conf and change the DBPassword to match the one you made earlier

Once thats done, you can su to the zabbix user and fire it up.

sudo su - zabbix
zabbix_server
#CTRL +D to get back to your normal user

Now it works, get it to start automatically, you can do this with an init.d script:

sudo vi /etc/init.d/zabbix-server
#!/bin/bash
# Slightly modified version o f zabbix-agent that comes in the zabbix tarball
PATH=/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin
DAEMON=/usr/local/bin/zabbix_server
NAME=zabbix_server
DESC="Zabbix server daemon"
USER=zabbix
#Check the daemon is executable
test -x $DAEMON || exit 0
set -e
case "$1" in
start)
echo "Starting $DESC: $NAME"
start-stop-daemon --oknodo --start --pidfile /var/tmp/$NAME.pid \
--user $USER --exec $DAEMON
;;
stop)
echo "Stopping $DESC: $NAME"
start-stop-daemon --oknodo --stop --pidfile /var/tmp/$NAME.pid \
--user $USER --exec $DAEMON
;;
restart|force-reload)
echo "Restarting $DESC: $NAME"
if [ -z "$(ps ax | egrep zabbix_server)" ]; then
echo " Zabbix isn't running, so not killed" ;
else
$0 stop
fi
sleep 1
$0 start
;;
*)
echo "Usage: $0 {start|stop|restart|force-reload}" >&2
exit 1
;;
esac
exit 0

Now use update-rc.d to make it run for default runlevels:

sudo chmod 755 /etc/init.d/zabbix-server
sudo update-rc.d zabbix-server defaults

Install the web frontend

Now we chuck the php files for the web frontend into zabix's public_html directory. In ubuntu, this is automatically configured, you may need to look at your apache config to make sure userdir is enabled.

sudo su - zabbix
cd ~
mkdir public_html
cp /path/to/untarred/zabbix/frontends/php/* /home/zabbix/public_html/ #change path for where you untarred zabbix to 
vi /home/zabbix/public_html/include/db.inc.php
#Change the following, and comment out what you dont need
$DB_TYPE =”MYSQL”; /* Or “POSTGRESQL” for PG */
$DB_SERVER =”localhost”; 
$DB_DATABASE =”zabbix”; 
$DB_USER =”zabbix”; 
$DB_PWD =”the password you made earlier”

Once this is done, you should be able to go to http://yourserver/~zabbix and see a login screen, leave that for now, its not much use without some data, which we get from the clients.

 

Install clients

This is easy, you can build the agents, or just download premade binaries. I have built them on linux servers, and downloaded the binaries for windows (there are windows binaries in the bin directory of the untarred server download). To make the linux client, you can use the same tarball as the server, just the configure arguments change:

configure --enable-agent
sudo make install

Copy the default file to /etc/zabbix and modify to suit your environment

sudo cp misc/conf/zabbix_agentd.conf /etc/zabbix/
sudo vi /etc/zabbix/zabbix_agentd.conf
#Make sure you change Server= to the ip or your zabbix server!

Now get it to start up each time the box boots:

sudo vi /etc/init.d/zabbix-agent

I have made a slight modification to the file to make it run as the zabbix user, otherwise it is the same as the one in the src tarball.

#! /bin/sh
#
# Zabbix agent start/stop script.
#
# Written by Alexei Vladishev <alexei.vladishev@zabbix.com>
PATH=/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin:/home/zabbix/bin
DAEMON=/usr/local/bin/zabbix_agentd
NAME=zabbix_agentd
DESC="Zabbix agent"
PID=/tmp/$NAME.pid
USER=zabbix
test -f $DAEMON || exit 0
set -e
case "$1" in
start)
echo "Starting $DESC: $NAME"
su -c "start-stop-daemon --oknodo --start --pidfile $PID \
--exec $DAEMON" $USER
;;
stop)
echo "Stopping $DESC: $NAME"
start-stop-daemon --oknodo --stop  --pidfile $PID \
--exec $DAEMON
;;
restart|force-reload)
#
#       If the "reload" option is implemented, move the "force-reload"
#       option to the "reload" entry above. If not, "force-reload" is
#       just the same as "restart".
#
#       echo -n "Restarting $DESC: zabbix_suckerd"
$0 stop
$0 start
#       start-stop-daemon --stop --quiet --pidfile \
#               /tmp/$NAME.pid --user zabbix --exec $DAEMON
#       sleep 1
#       start-stop-daemon --start --quiet --pidfile \
#               /tmp/$NAME.pid --user zabbix --exec $DAEMON
#       echo "$NAME."
;;
*)
N=/etc/init.d/$NAME
# echo "Usage: $N {start|stop|restart|force-reload}" >&2
echo "Usage: $N {start|stop|restart|force-reload}" >&2
exit 1
;;
esac
exit 0

Save that, and now run update-rc.d

sudo chmod 755 /etc/init.d/zabbix-server
sudo update-rc.d zabbix-agent defaults

The agent should now start with the computer.

For windows agents, look in the bin directory of the extracted tarball, or download from the zabbix download page. The windows agent also needs a config file, which can be pretty much the same as the linux one. Make sure you have the correct server ip in the config file (zabbix_agentd.conf) and put both the config file and the exe in C:\ then open a command prompt and run "ZabbixW32.exe install", this will install a windows service and start it.

Configure hosts in the web gui

First add a new user and change admin password: Click on "profile" in the top right of the screen, you can enter a new password there. I also prefer to completely disable guest access, you can do this from the users screen under configuration. More details on users is here: http://www.zabbix.com/manual/v1.1/qs.user.php

Even with the agents blasting info to the server, you wont see anything until you configure them in the web gui. Log in as admin (no password) and then go to "Configuration" > "Hosts" and add a host, choose link to template for nice quick provisioning (windows_t for windows, unix_t for unix etc) There is good documentation on the site, so have a look there for all the things you can do: http://www.zabbix.com/manual/v1.1/qs.host.agent.php Have a look through the manual for all the other things you can do, its a very flexible system!

 

Links

Heres one for doing it in debian (seems someone has made some debs, wonder if they would work on ubuntu?) http://www.debianhelp.co.uk/zabbix.htm