First of all, what is Icinga?

They say that Icinga is an enterprise grade open source monitoring system which keeps watch over networks and any conceivable network resource, notifies the user of errors and recoveries and generates performance data for reporting. Scalable and extensible, Icinga can monitor complex, large environments across dispersed locations. Icinga is a fork of Nagios and is backward compatible. All configuration files that you used in Nagios, you can also use in Icinga.

They’re right. Icinga is all that. I’m not expert. In fact, I’m just a beginner in big world of system administration. About 2 or 3 months ago I installed and setup Icinga. At first, I didn’t even know wtf that is :). When it came to the configuration, of corse I needed some help as I never seen Nagios before in my life. But it’s really not that complicated.

To define new host monitoring first you must define configuration file which contains host definition and services definition. You can choose which services you want to use for your monitoring ( SMTP, DISK, LOAD …). You can add your own commands by modifying this script. When you’re checking services such as LOAD, DISK, SWAP … you must install NRPE plugin on destination machine. NRPE daemon will connect  Icinga monitoring system with machine you want to monitor. All this is explained in Icinga Documentation. When you have configuration file for your host, you must enter path to this file in icinga.cfg file which will tell Icinga where to look for configuration for specific host. This script will do all this automatically.

You can download this script here.

Here is example of host configuration file (test-test.cfg):

define host {
use generic-host
host_name test.test.com
address host_ip_address
contact_groups test_group
check_command check_icmp
}

define service {
use generic-service
host_name test.test.com
service_description SSH
contact_groups test_group
check_command   check_ssh!-p 22
}

define service{
use generic-service
host_name test.test.com
service_description LOAD
contact_groups test_group
check_command check_nrpe!check_load
}

### check_disk_usr is command defined on nrpe on monitored machine
define service{
use generic-service
host_name test.test.com
service_description DISK-/usr
contact_groups test_group
check_command check_nrpe!check_disk_usr
}

This line goes to icinga.cfg file:

### test-test  (test-test is example name)
cfg_file=/usr/local/icinga/etc/hosts/test-test.cfg

So as you can see, every time we want to make a new monitoring of a host, we must manualy create configuration file that define host, add path to configuration file to icinga.cfg and restart icinga. This can be very time consuming. This script allows you to add hosts to Icinga monitoring quickly and simple. Script will prompt user to enter data with dialog boxes. Wizard will guide you through the process. Creation of new host definitions will bi much simpler and quicker.

Script will create host cfg file which name you define. It will also add path to hosts cfg file in icinga.cfg. You can select between various of services which are defined in this script. Of course you can modify this script and add or remove services according to your needs.

When adding check_mysql command, don’t forget to create mysql user. Default name of mysql user in this script is nagios. Script will generate password for mysql user nagios. When finish, create nagios user with password provided by this script.

First download this script and put it in your /usr/local/sbin/ folder. Don’t forget to chmod 700 /path/to/script/addmonitor. Then open script with your favorite text editor and define some global variables. When you want to run the script, simple type and run addmonitor command.

# Global variables
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin
ICINGACFG=/path/to/icinga/icinga.cfg   #Icinga configuration file
CONFDIR=/path/to/icinga/conf/files/  #path where you store your hosts configuration files
TMPFILE=/tmp/dialog.tmp #file for dialogs
GROUP=”group” #define group

Attention: In this script services such as DISK, LOAD, SWAP are defined as nrpe services. So when configuration file will be created, this services will have check_nrpe prefix. You can simple modify this if you want. Script is designed for FreeBSD systems. Don’t forget to restart your Icinga service after new host has been generated.

Download addmonitor here

Script source code:

# Global variables
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin
ICINGACFG=/path/to/icinga.cfg
CONFDIR=/path/to/your/config/files/
TMPFILE=/tmp/dialog.tmp
GROUP=”group”

# Colors
green=’\e[0;32m’
red=’\e[0;31m’
blue=’\e[1;34m’
endColor=’\e[0m’

#Generate password
PASSWORD=$(/usr/bin/perl -le’print map+(A..Z,a..z,0..9)[rand 62],1..’8);

# Get monitor name
echo -n “Monitoring title: ”
read -e NAME

# Define config file name
FILE=$NAME”.cfg”

# Check if file with this name exists
if [ -f “$CONFDIR$FILE” ]; then
while [ -f “$CONFDIR$FILE” ]
do
echo -n “File with this name exists. Enter new name: ”
read -e NAME
FILE=$NAME”.cfg”
done
fi

# Get hostname
echo -n “Enter hostname: ”
read -e HOSTNAME

# Check if hostname exists.
if [ “$(ls -A $CONFDIR)” ]; then
while grep “host_name” $CONFDIR/* | grep -q $HOSTNAME
do
echo -n “Entered hostname exists. Enter new hostname: ”
read -e HOSTNAME
done
fi

# Get IP address of server
echo -n “Enter IP address: ”
read -e IP

# Show dialog box with list of services
dialog \
–title “SELECT SERVICES” \
–checklist “Chose services” 20 80 15 \
check_ssh “Check SSH” on \
check_http “Check HTTP” off \
check_mysql “Check MYSQL” off \
check_ftp “Check FTP” off \
check_smtp “Check SMTP” off \
check_imap “Check IMAP” off \
check_pop “Check POP” off \
check_load “Check LOAD” on \
check_swap “Check SWAP” on \
check_disk_usr “Check disk /usr” off \
check_disk_var “Check disk /var” off \
check_disk_root “Check disk /root” off \
check_disk_tmp “Check disk /tmp” off 2> $TMPFILE

# Define name of configuration files
HOSTSCFG=$NAME”.cfg”

# Define host
echo “define host {” >> $CONFDIR$HOSTSCFG
echo -e “\tuse\t\t\t\t\t generic-host” >> $CONFDIR$HOSTSCFG
echo -e “\thost_name\t\t\t\t $HOSTNAME” >> $CONFDIR$HOSTSCFG
echo -e “\taddress\t\t\t\t\t $IP” >> $CONFDIR$HOSTSCFG
echo -e “\tcontact_groups\t\t\t\t $GROUP” >> $CONFDIR$HOSTSCFG
echo -e “\tcheck_command\t\t\t\t check_icmp” >> $CONFDIR$HOSTSCFG
echo “}” >> $CONFDIR$HOSTSCFG

# Generate configuration file
for WORD in `cat $TMPFILE`
do
WORD=${WORD//\”/}
case “$WORD” in
‘check_ssh’)
COMMAND=”SSH”
OPERATION=”$WORD! -p 65022″;;
‘check_http’)
COMMAND=”HTTP”
OPERATION=”$WORD! -H $HOSTNAME”;;
‘check_mysql’)
COMMAND=”MYSQL”
OPERATION=”$WORD! -u nagios -p $PASSWORD”;;
‘check_ftp’)
COMMAND=”FTP”
OPERATION=”$WORD! -H $HOSTNAME”;;
‘check_smtp’)
COMMAND=”SMTP”
OPERATION=”$WORD! -H $HOSTNAME”;;
‘check_imap’)
COMMAND=”IMAP”
OPERATION=”$WORD! -H $HOSTNAME”;;
‘check_pop’)
COMMAND=”POP”
OPERATION=”$WORD! -H $HOSTNAME”;;
‘check_load’)
COMMAND=”LOAD”
OPERATION=”check_nrpe!$WORD”;;
‘check_swap’)
COMMAND=”SWAP”
OPERATION=”check_nrpe!$WORD”;;
‘check_disk_root’)
COMMAND=”DISK-/”
OPERATION=”check_nrpe!$WORD”;;
‘check_disk_var’)
COMMAND=”DISK-/var”
OPERATION=”check_nrpe!$WORD”;;
‘check_disk_usr’)
COMMAND=”DISK-/usr”
OPERATION=”check_nrpe!$WORD”;;
‘check_disk_tmp’)
COMMAND=”DISK-/tmp”
OPERATION=”check_nrpe!$WORD”;;
esac

# Write data
echo -e “\n” >> $CONFDIR$HOSTSCFG
echo -e “define service {” >> $CONFDIR$HOSTSCFG
echo -e “\tuse\t\t\t\t\t generic-service” >> $CONFDIR$HOSTSCFG
echo -e “\thost_name\t\t\t\t $HOSTNAME” >> $CONFDIR$HOSTSCFG
echo -e “\tservice_description\t\t\t $COMMAND” >> $CONFDIR$HOSTSCFG
echo -e “\tcontact_groups\t\t\t\t $GROUP” >> $CONFDIR$HOSTSCFG
echo -e “\tcheck_command\t\t\t\t $OPERATION” >> $CONFDIR$HOSTSCFG
echo -e “}” >> $CONFDIR$HOSTSCFG
done

# Add new created host to icinga.cfg
echo -e “\n” >> $ICINGACFG
echo “### $NAME” >> $ICINGACFG
echo “cfg_file=”$CONFDIR$HOSTSCFG >> $ICINGACFG

if [ $(ls -A $CONFDIR$HOSTSCFG) ]; then
STATUS=”${green}[OK]${endColor}”
else
STATUS=”${red}[FAIL]${endColor}”
fi

if grep -H -r -w “$HOSTSCFG” $ICINGACFG; then
STATUS2=”${green}[OK]${endColor}”
else
STATUS2=”${red}[FAIL]${endColor}”
fi

echo -e “\n”
echo -e ” File $HOSTSCFG created\t\t\t $STATUS”
echo -e ” Data added to icinga.cfg\t\t\t\t $STATUS2″
if grep -q ‘check_mysql’ $TMPFILE; then
echo -e ” ${green}Password for mysql user nagios\t\t\t\t${endColor} ${blue}$PASSWORD${endColor}”
fi
echo -e “\n”

rm -rf $TMPFILE

Dodaj odgovor

Vaš e-naslov ne bo objavljen. * označuje zahtevana polja