Posts Tagged ‘nagios’
Installing Mail Server on Nagios
August 19th, 2013
Postfix Server Installation
First you need to install postfix;
1 |
apt-get install postfix |
When prompted, choose internet site, then enter the @ part where you wish the mail to be sent from, e.g nagios.notify. This will mean that emails received from nagios will be @nagios.notify.
Mailx Server Installation
Now install Nagios’s part of the mail server;
1 |
apt-get install heirloom-mailx |
Configuring Nagios Mail Command
You need to edit the commands.cfg folder to take into account the new paths;
1 |
vi /usr/local/nagios/etc/objects/commands.cfg |
Replace;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# 'notify-host-by-email' command definition define command{ command_name notify-host-by-email command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$ } # 'notify-service-by-email' command definition define command{ command_name notify-service-by-email command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$ } |
With;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# 'notify-host-by-email' command definition define command{ command_name notify-host-by-email command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /usr/bin/mailx -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$ } # 'notify-service-by-email' command definition define command{ command_name notify-service-by-email command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n" | /usr/bin/mailx -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$ } |
Note that the change is ‘/bin/mail’ to ‘/usr/bin/mailx’.
Now restart Nagios;
1 |
service nagios restart |
You should now receive email notifications of services/host states.
Enabling and Disabling Notifications for a Service
To disable notifications follows the steps below.
- Access the web interface;
1 |
http://<nagiosip>/nagios |
- Choose Hosts or Services from the side menu (depending on which you wish to change).
- Select a service or host you wish to disable notifications for (note that HTTP and SSH notifications are disabled by default on localhost).
- On the page which opens, from the right hand section select the option ‘Disable Notifications’.
- This should open a page asking to confirm the host or host and service you wish to disable notifications for. Select ‘Commit’ to continue;
- From this page it will now show a page verifying that it has been complete. Make sure to select the option ‘Done';
This will return you to the homepage. Navigate back to the host/service (from 4.) and you will see at the bottom it shows Notifications in red and as ‘DISABLED’. Also the right hand menu has changed to give the option to ‘Enable’ notifications as they are currently disabled.
Configure Nagios
March 7th, 2013
This will guide you through a basic setup of a host and some services for Nagios.
First thing to note is that Nagios Core has no web based UI for configuring hosts, services or commands.
Adding a Host
To start the configuration, add a host. You can choose the default location to create a hosts file;
1 |
/usr/local/nagios/etc/objects/hosts.cfg |
Any new files/directories created must be added to nagios.cfg;
1 |
vi /usr/local/nagios/etc/nagios.cfg |
New files are added as follows;
1 |
cfg_file=/usr/local/nagios/etc/objects/hosts.cfg |
Once you have created the new file, open it with your text editor;
1 |
vi /usr/local/nagios/etc/objects/linux/hosts.cfg |
A simple linux host is defined as follows;
1 |
define host{ |
1 |
use linux-server |
1 |
host_name <hostname> |
1 |
alias <hostalias> |
1 |
address <hostip> |
1 |
hostgroups <hostgroup/s to be part of> |
1 |
} |
The “use” part of this host definition is taken from;
1 |
/usr/local/nagios/etc/objects/templates.cfg |
Here you can choose the default settings for a host, e.g. contact groups, notifications enabled, retry attempts.
A full list of possible host definitions can be found here;
http://nagios.sourceforge.net/docs/3_0/objectdefinitions.html#host
If you wish to set a specific setting for just a few hosts, adding it to the hosts.cfg folder overrides the template.
Plugin Usage and Adding Commands
Commands are added through the commands.cfg file located here;
1 |
/usr/local/nagios/etc/objects/commands.cfg |
Adding a command requires a plugin for the command you wish to add, as well as knowledge on how to use the plugin.
The default locations for plugins you wish to add can be found here;
1 |
/usr/local/nagios/libexec |
To know what command is required for a plugin to work, you can request help on the plugins usage;
1 |
cd /usr/local/nagios/libexec/ |
1 |
./check_<plugin> -h |
Note that some plugins may require pre-requisites, which will be prompted when you attempt to use the plugin in the command line.
An example would be the plugin check_ping which has the following output when you use the “-h” argument;
1 |
Usage: |
1 |
check_ping -H <host_address> -w <wrta>,<wpl>% -c <crta>,<cpl>% |
1 |
[-p packets] [-t timeout] [-4|-6] |
This helps you understand how to define a command.
Now edit the commands.cfg file and look for the check_ping command definition;
1 |
vi /usr/local/nagios/etc/objects/commands.cfg |
1 |
define command{ |
1 |
command_name check_ping |
1 |
command_line $USER1$/check_ping -H $HOSTADDRESS$ -w $ARG1$ -c $ARG2$ -p 5 |
1 |
} |
1 |
command_name |
– This is the name you use to define this command. This is used in the service definition to reference this command.
1 |
$USER1$ |
– This is defined in /usr/local/nagios/etc/objects/resources.cfg. In this case it defines the plugin directory being used (/usr/local/nagios/libexec).
“check_ping” requires the arguments -H, -w and -c.
1 |
-H $HOSTADDRESS$ |
– This defines the host which needs to be checked, and is common in nearly all plugins. The $HOSTADDRESS$ part is an inbuilt Nagios macro which uses the hostname defined in the service command. This won’t need to be defined in each service which uses this commands, as you will see below.
1 |
-w $ARG1$ |
– This sets the warning value which is required for the plugin to work. The $ARG1$ is defined in the service command.
1 |
-c $ARG2$ |
– This sets the critical value which is required for the plugin to work. The $ARG2$ is defined in the service command.
1 |
-p 5 |
– This argument is an optional addition to the command definition. By not using $ARG3$ we are saying that every time this command is used, set “-p” to “5”.
Adding Services
Now you have a host and a command, you need a service!
This services config file can be created/found in the same place as the host file;
1 |
/usr/local/nagios/etc/objects/services.cfg |
Remember to add this location to nagios.cfg if it isn’t already there.
Now you need to add some services to the config file for your host.
1 |
vi /usr/local/nagios/etc/objects/services.cfg |
The service for ping is defined as follows;
1 |
define service{ |
1 |
use local-service |
1 |
host_name <hostname> |
1 |
hostgroup_name <hostgroup name> |
1 |
service_description PING |
1 |
check_command check_ping!100.0,20%!500.0,60% |
1 |
} |
With regards to the “check_command”, this is where you define the command to be used for this service by entering the “command_name” defined in commands.cfg.
1 |
check_ping |
– This is the “command_name”. This is always entered first.
After the command_name, you must enter the details for $ARG1$, $ARG2$, etc. These are separated by an exclamation mark, “!”.
1 |
!100.0,20% |
– This the response time of the ping request. If it’s sufficient for this argument, you will receive a WARNING status.
1 |
!500.0,60% |
– This the response time of the ping request. If it’s sufficient for this argument, you will receive a CRITICAL status.
As before, the “use” part is defined in templates.cfg.
A full list of service definitions can be found here;
http://nagios.sourceforge.net/docs/3_0/objectdefinitions.html#service
Adding a definition to services.cfg overrides the template.
You now have a new host configured with the ping service.
Hostgroups
If you will have, say, 10 linux servers, all requiring the “ping” command, it would be best to define a hostgroup in the host and service definitions instead of the host name.
A hostgroup is created as follows;
1 |
vi /usr/local/nagios/etc/objects/hostgroups.cfg |
1 |
define hostgroup{ |
1 |
hostgroup_name <hostgroup name> |
1 |
alias <hostgroup alias> |
1 |
} |
Note that when adding hostgroups/hostgroup_name to the host/service definitions, respectively, they are seperated by commas.
e.g.
1 |
hostgroups hostgroup1, hostgroup2, ... |
These basic steps can be repeated for most plugins – the only differences come when using NRPE, NSCA, or a custom plugin, but even then the basics are essential, or when you are missing pre-requisites for the plugin to work.
When you are finished with your customization, run the command below to confirm that there are no issues with the configuration files;
1 |
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg |
Any problems are made apparent to you after running this command, telling you both the file and line where the error occurs.
This makes debugging errors very easy.
If there are no problems, you can restart nagios and head to the web page;
1 |
service nagios restart |
On a web browser;
http://<nagios server ip>/nagios
The following will be how the ping service is displayed;
Install Nagios on Ubuntu
March 7th, 2013
The following is a guide to install Nagios Core on an Ubuntu machine.
Preparing for the Installation
First you will need to be root;
1 |
sudo -s |
For convenience it is best to install ssh and vim;
1 |
apt-get install ssh vim |
Now you need to install some pre-requisites required for Nagios to correctly install/run;
1 |
apt-get install apache2 |
1 |
apt-get install libapache2-mod-php5 |
1 |
apt-get install build-essential |
1 |
apt-get install libgd2-xpm-dev |
1 |
1 |
apt-get install libssl-dev |
Now add the user ‘nagios’ to the system;
1 |
/usr/sbin/useradd -m -s /bin/bash nagios |
1 |
passwd nagios |
Create nagcmd group to allow commands through the web;
1 |
/usr/sbin/groupadd nagcmd |
1 |
/usr/sbin/usermod -a -G nagcmd nagios |
1 |
/usr/sbin/usermod -a -G nagcmd www-data |
Installing Nagios and the Plugins
First, make a directory to download the plugins to
1 |
mkdir ~/downloads |
1 |
1 |
cd ~/downloads |
Now you need to download Nagios and Nagios plugins;
1 |
wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.4.1.tar.gz |
1 |
1 |
wget http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagiosplugins-1.4.16.tar.gz |
Extracting Nagios so it can be compiled/installed;
1 |
cd ~/downloads |
1 |
tar -zxvf nagios-3.4.1.tar.gz |
1 |
cd nagios |
Now you must run the install script and compile the source code;
1 |
./configure --with-command-group=nagcmd |
1 |
1 |
make |
1 |
1 |
make all |
1 |
1 |
make install |
1 |
1 |
make install-init |
1 |
1 |
make install-config |
1 |
1 |
make install-commandmode |
Run the following command to install the web server
1 |
make install-webconf |
The next step is to create a password for the nagios web server login account, ‘nagiosadmin’
1 |
htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin |
Remember this password as it is required to log on to the web interface.
Restart apache to apply the changes
1 |
/etc/init.d/apache2 reload |
Now you must extract the plugins;
1 |
cd ~/downloads |
1 |
tar -zxvf nagios-plugins-1.4.16.tar.gz |
1 |
1 |
cd nagios-plugins-1.4.16 |
Use the commands below to compile and install the plugins
1 |
./configure --with-nagios-user=nagios --with-nagios-group=nagios |
1 |
make |
1 |
make install |
Now configure Nagios to start on system start
1 |
ln -s /etc/init.d/nagios /etc/rcS.d/S99nagios |
Check the nagios configuration with the following command
1 |
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg |
Make note of this command, it is very important later on for debugging issues with config files.
If there are no errors, you are ready to start Nagios;
1 |
/etc/init.d/nagios start |
Now access the web interface from a web browser, navigating to
http://localhost/nagios/
or from within the network;
http://<hostname/ip>/nagios/
Congratulations, you are now done with the installation!
Please refer to our blog for a post on configuring your first host.