Nagios on FreeBSD® 7.2

From BMO

Jump to: navigation, search

Nagios is a powerful monitoring system that enables organizations to identify and resolve IT infrastructure problems before they affect critical business processes. Nagios monitors your entire IT infrastructure to ensure systems, applications, services, and business processes are functioning properly. In the event of a failure, Nagios can alert technical staff of the problem, allowing them to begin toubleshooting before outages affect business processes, end-users, or customers.


This page contains information that has been adapted and referenced from the official Mysql website found here, the official Apache website found here, from the official Php website found here , from the Nagios website found here and also from the official Nconf website found here.


Contents

Requirements

Mysql

MySQL is an open source database system software.

Installation

Use the following commands to install mysql server.

cd /usr/ports/databases/mysql51-server/
make install clean 

Figure-1.1.jpg

fig.1


Figure-1.2a.jpg

fig.2


Figure-1.3.jpg

fig.3


Enable and launch the mysql service

To enable and launch the mysql service at system startup, add the line mysql_enable="YES" to the /etc/rc.conf file using the following command:

echo 'mysql_enable="YES"' >> /etc/rc.conf


Figure-1.3a.jpg
fig.4


Start mysql service

Start the mysql service using the following command:

/usr/local/etc/rc.d/mysql-server start

Figure-1.3b.jpg
fig.5


Set mysql admin password

Setting the mysql admin password Here we'll set an example of an admin password which is "adminpassword".

mysqladmin -u root password adminpassword 

Figure-1.3c.jpg
fig.6


Log in and out of mysql console

Log in to mysql console to check if your password was set correctly using the following command:

mysql -u root -p

Figure-1.3d.jpg
fig.7


Figure-1.3e.jpg
fig.8


Then you can log out of the mysql console screen by using the following command:

exit

Figure-1.3f.jpg
fig.9

Apache

The Apache HTTP Server is an open-source HTTP server for modern operating systems.

Installation

Use the following commands to install apache.

cd /usr/ports/www/apache22/ 
make install clean 

Figure-1.4.jpg

fig.10


Apache installation options

Make sure to enable mysql-support for apr-dbd on the screen which appears.

Figure-1.5.jpg

fig.11


Figure-1.6a.jpg

fig.12


Figure-1.7.jpg

fig.13

Enable and launch the apache service.

To enable and launch the apache service at system startup, add the line apache22_enable="YES" to the /etc/rc.conf file using the following command:

echo 'apache22_enable="YES"' >> /etc/rc.conf

Figure-1.7b.jpg
fig.14


Start the apache service.

Start the apache service using the following command:

/usr/local/etc/rc.d/apache22 start

Figure-1.7c.jpg
fig.15


Test Apache

To test Apache point your web browser which is in a separate computer to http://yourdomain.com/ (or http://yourdomain.com/index.html) or in this case http://192.168.6.37/ (or http://192.168.6.37/index.html) and if Apache works you will see the following page shown below.

Figure-1.7d.jpg
fig.16

php

PHP is a widely-used general-purpose scripting language suited for Web development and can be embedded into HTML.

Installation

Install php using the following commands:

cd /usr/ports/lang/php5/
make install clean

Figure-1.8.jpg

fig.17


php installation options

Make sure to enable apache module ie build apache module on the screen that appears.

Figure-1.9.jpg

fig.18


Figure-1.10a.jpg

fig.19


Figure-1.11.jpg

fig.20


Php Extensions

PHP extensions are sets of instructions (i.e. code) that are designed to add functionality to PHP. These can be installed using the following commands:

cd /usr/ports/lang/php5-extensions/
make install clean

Figure-1.11a1.jpg
fig.21


Make sure to select sessions and mysql support in the configurations options menu that appears.

Figure-1.11a3.jpg
fig.22


Select the default configurations options menu that appear later.

Figure-1.11a4.jpg
fig.23


Php Configuration

Settings for PHP are stored in the /usr/local/etc/php.ini file. You will need to create this file by copying it from the /usr/local/etc/php.ini-dist file using the following command:

cp /usr/local/etc/php.ini-dist /usr/local/etc/php.ini


Figure-1.11b.jpg
fig.24


Apache also needs to be configured so that it can use php. Open the /usr/local/etc/apache22/httpd.conf file using the following command:

ee /usr/local/etc/apache22/httpd.conf


Figure-1.11b1.jpg
fig.25


Scroll down to the bottom of the /usr/local/etc/apache22/httpd.conf file and then add the following three lines to the /usr/local/etc/apache22/httpd.conf file.

DirectoryIndex index.php index.html index.htm 
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps

The first line tells Apache to open index.php first when the PHP 5 module is loaded.

The second and the third lines tell apache what it should parse the PHP files with.


Figure-1.11b2.jpg
fig.26


Save and close the /usr/local/etc/apache22/httpd.conf file.

Test Php

Restart apache by using the following command:

/usr/local/etc/rc.d/apache22 restart

Figure-1.11c.jpg
fig.27


Once Apache is restarted the changes take effect and you are ready to test php. To test php run the following command to create a php file in the /usr/local/www/data/ directory.

echo "<? phpinfo(); ?>" >> /usr/local/www/apache22/data/test.php

Figure-1.11d.jpg
fig.28


Then point your web browser which is in a separate computer to http://yourdomain.com/test.php or in this case http://192.168.6.37/test.php and if php works you will see several pages of information on your server's PHP settings as shown below. If it did not work you will see only the <? phpinfo(); ?> text you typed in.


Figure-1.11b4.jpg
fig.29


Nagios

Installation

Install nagios using the following commands:

cd /usr/ports/net-mgmt/nagios/
make install clean

Figure-1.12.jpg

fig.30


nagios installation options

Select the default for all from the installation options that appears on the screen ie select ok then enter.

Figure-1.13.jpg

fig.31


Figure-1.14a.jpg

fig.32


gd

Select the default for all from the installation options that appears on the screen ie select ok then enter.

Figure-1.15.jpg

fig.33


Figure-1.16.jpg

fig.34


nagios plugins

The installation process will get to a point where it will ask for installation options for the nagios plugins. Select check_fping and check_snmp.

Figure-1.17.jpg

fig.35


Figure-1.18.jpg

fig.36


net-snmp installation options

Select the default for all from the installation options that appears on the screen ie select ok then enter.


Figure-1.19.jpg

fig.37


Figure-1.20.jpg

fig.38


Create nagios group and user


Figure-1.21.jpg

fig.39


Figure-1.22.jpg

fig.40


Enable and launch the nagios service

To enable and launch the nagios service at system startup, add the line nagios_enable="YES" to the /etc/rc.conf file using the following command:

echo 'nagios_enable="YES"' >> /etc/rc.conf

Figure-1.24.jpg
fig.41

Nagios sample Config Files

Navigate to the /usr/local/etc/nagios/ directory and copy the .cfg-sample to their respective ./cfg as shown below

cd /usr/local/etc/nagios/
ls -l
cp cgi.cfg-sample cgi.cfg
cp nagios.cfg-sample nagios.cfg
cp resource.cfg-sample resource.cfg


Figure-1.24.1.jpg
fig.42

Then also navigate to the /usr/local/etc/nagios/objects/ directory and copy the .cfg-sample to their respective ./cfg as shown below

cd /usr/local/etc/nagios/objects/
ls -l
cp commands.cfg-sample commands.cfg
cp contacts.cfg-sample contacts.cfg
cp localhost.cfg-sample localhost.cfg
cp printer.cfg-sample printer.cfg
cp switch.cfg-sample switch.cfg
cp templates.cfg-sample templates.cfg
cp timeperiods.cfg-sample timeperiods.cfg

Figure-1.24.2.jpg
fig.43

Figure-1.24.3.jpg
fig.44

Starting the nagios service

Make the necessary configurations before starting the nagios service. Configurations can be made manually or by using a Nagios configurator such as Nconf explained below. The nagios service can be started using the following command:

/usr/local/etc/rc.d/nagios start 

Figure-1.25a.jpg
fig.45


You can create a password for the nagios web interface using the following command:

htpasswd -c /usr/local/etc/nagios/htpasswd.users nagiosadmin

The username nagiosadmin and the password is of your choice. Here we will use a password of nagiosweb.

Figure-1.25b.jpg
fig.46


Nagios Apache Configuration

Open the /usr/local/etc/apache22/httpd.conf file using the following command.

ee /usr/local/etc/apache22/httpd.conf

Figure-1.25.1.jpg
fig.47

Then add the following lines to the /usr/local/etc/apache22/httpd.conf file.

ScriptAlias /nagios/cgi-bin/ /usr/local/www/nagios/cgi-bin/
Alias /nagios /usr/local/www/nagios/

<Directory /usr/local/www/nagios>
        Options None
        AllowOverride None
        Order allow,deny
        Allow from all
        AuthName "Nagios Access"
        AuthType Basic
        AuthUSerFile /usr/local/etc/nagios/htpasswd.users
        Require valid-user
</Directory>

<Directory /usr/local/www/nagios/cgi-bin>
        Options ExecCGI
        AllowOverride None
        Order allow,deny
        Allow from all
        AuthName "Nagios Access"
        AuthType Basic
        AuthUSerFile /usr/local/etc/nagios/htpasswd.users
        Require valid-user
</Directory>


Figure-1.25.4.jpg
fig.48


Figure-1.25.5.jpg
fig.49

Then restart the Apache webserver using the following command:

/usr/local/etc/rc.d/apache22 restart

Figure-1.25.3.jpg
fig.50

Nagios Web Interface

For the Nagios web interface, point your web browser which is in a separate computer to http://yourdomain.com/nagios/ or in this case http://192.168.6.37/nagios/. Then enter the username nagiosadmin and the password nagiosweb as set earlier.

Figure-1.118.jpg
fig.51

Figure-1.119.jpg
fig.52

Figure-1.26c.jpg
fig.53

Congratulations you have completed the installation of Nagios together with the requirements.


Nagios Configuration Using Nconf

NConf is a PHP based web-tool for configuring the Nagios monitoring software. It differs from similar tools by offering enterprise-class features like templates, dependencies and the ability to configure a large-scale, distributed Nagios server topology. It is mainly targeted at sysadmins, who already know Nagios, but are looking for a more convenient way of managing their configuration files.


Requirements

Nconf requires the following packages.

  • Apache webserver
  • PHP 5 or higher, php-mysql, php-ldap (only if using LDAP auth)
  • MySQL 5.0.2 or higher (with InnoDB)
  • Perl 5.6 or higher, perl-DBI, perl-DBD-MySQL
  • Nagios 3.x


The above packages have all been installed as shown above apart from these two:

  • perl-DBI
  • perl-DBD-MySQL


Install perl-DBI using the following command:

cd /usr/ports/databases/p5-DBI/ && make install clean

Figure-1.27.jpg
fig.54


Figure-1.28.jpg
fig.55


Install perl-DBD-MySQL using the following command

cd /usr/ports/databases/p5-DBD-mysql51/ && make install clean

Figure-1.29.jpg
fig.56


Figure-1.30.jpg
fig.57


Also Nconf requires the /usr/local/etc/php.ini file be editted and have the following settings:

  • short_open_tag = On
  • register_globals = Off
  • magic_quotes_gpc = Off

Open the /usr/local/etc/php.ini using the following command:

ee /usr/local/etc/php.ini

Figure-1.40.jpg
fig.58


Figure-1.41.jpg
fig.59


Figure-1.42.jpg
fig.60


Figure-1.43.jpg
fig.61


Installation

Download and Extract

Download the Nconf archive using the fetch command:

fetch http://sourceforge.net/projects/nconf/files/nconf/1.2.6-0/nconf-1.2.6-0.tgz/download


Figure-1.44.jpg
fig.62


Navigate to the /usr/ports/distfiles directory and extract the download file. Then move the nconf directory to the webserver's document root directory (i.e the /usr/local/www/apache22/data/ directory). Navigate to the /usr/local/www/apache22/data directory and do an ls just to make sure the nconf directory was moved there.


cd /usr/ports/distfiles/
tar -xf download
mv nconf/ /usr/local/www/apache22/data/
cd /usr/local/www/apache22/data/
ls


Figure-1.45.jpg
fig.63

Figure-1.46.jpg
fig.64

Figure-1.47.jpg
fig.65

Figure-1.48.jpg
fig.66


Directory Permissions

Navigate into the /usr/local/www/apache22/data/nconf/ directory and set the directory permissions of the following directories to make them writeable by the webserver user.

  • ./config
  • ./output
  • ./static_cfg
  • ./temp
cd /usr/local/www/apache22/data/nconf/
chmod 777 config/ output/ static_cfg/ temp/


Figure-1.49.jpg
fig.67

MySQL database for NConf

Creating the MySQL Database

Create a new MySQL database for NConf. Log into mysql and then create the database which we will call nconf. Create a user whom we will call nconf_admin with a password of nconf_password although we can still use the mysql root user in which case creating a new user wont be necessary. Grant the appropriate privileges to the user who will administer the database.

mysql -u root -p
CREATE DATABASE nconf;
CREATE USER 'nconf_admin'@'localhost' IDENTIFIED BY 'nconf_password';
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER ON nconf.* TO 'nconf_admin'@'localhost' IDENTIFIED BY 'nconf_password';
flush privileges;



Figure-1.50.jpg
fig.68


Figure-1.51.jpg
fig.69


Figure-1.52.jpg
fig.70


Figure-1.53.jpg
fig.71


Figure-1.54.jpg
fig.72

Import the database structure

Import the database structure from /usr/local/www/apache22/data/nconf/INSTALL/create_database.sql file to the nconf mysql database just created using the following command:

mysql -u nconf_admin -p nconf < /usr/local/www/apache22/data/nconf/INSTALL/create_database.sql


Figure-1.55.jpg
fig.73

Nconf Web Based Installation

For the web interface nconf installation point your web browser which is in a separate computer to http://yourdomain.com/nconf/INSTALL.php or in this case http://192.168.6.37/nconf/INSTALL.php to proceed with the installation.

Pre-install check

The pre-install check will try to detect some requirements. If all checks are OK, you may safely proceed with the installation. If one or more checks fail, you may still proceed to install NConf, but it might not work properly. In our case here our MySQL database system is being detected but nconf cannot get the version number. Since we know that we installed version 5.1.44 and the minimum version required is 5.0.2 then its safe for us to proceed.

Figure-1.59e.jpg
fig.74


Step 1

Step 1 of the installation asks you for the MySQL information. Enter the required information like hostname, database name, username and password. Make sure you have created a new database and have given sufficent privileges to the user you're connecting with. Here our hostname is localhost, the database name is nconf, the username is nconf_admin and the password id nconf_password as created above.

Figure-1.60e.jpg
fig.75


When the next button is clicked, nconf will verify that information and return values which confirm that the database is available and can be accessed. Nconf also checks if the database supports InnoDB and also if the tables were already created. If all these checks are OK, it is safe to proceed.

Figure-1.61e.jpg
fig.76


Step 2

In Step 2 the following information is required:

  • NCONFDIR - This is the directory where NConf is located in the host machine. It is auto detected by NConf.
  • NAGIOS_BIN - This is the path to the Nagios binary. The binary is needed in order to do syntax checking of the generated config. NConf will not work properly if these checks cannot be executed. This path should either point to the original binary (if Nagios is installed on the same host), to a symbolic link or to a copy of the binary (copy it to the nconf/bin/ directory). Make sure the binary is executable to the webserver user. Here we will copy the nagios binary on the server side from /usr/local/bin/ directory to the /usr/local/www/apache22/data/nconf/bin/ directory as shown below then proceed with the web interface installation.
  • TEMPLATE_DIR - This defines which design-template (i.e. skin) to be used by NConf.

Figure-1.62e.jpg
fig.77


Figure-1.63.jpg
fig.78


Figure-1.63b.jpg
fig.79


Step 3

Step 3 allows you to activate basic authentication for NConf. More sophisticated authentication methods are available, but these must be configured manually after completing the installation. Select True for AUTH_ENABLED, file for AUTH_TYPE snd then set an administrator password for the NConf web interface administration tasks.  Here we will set an administrator password of admin_password.

Figure-1.64e.jpg
fig.80


Step 4

Step 4 creates basic settings, saves the installation settings and confirm's that the installation is complete. It also prompts you to delete the following files and directories since the installation is complete:

  • INSTALL
  • INSTALL.php
  • UPDATE
  • UPDATE.php

Delete them as shown in fig.66 below on the server side them proceed to click on finish on the NConf web interface.

Figure-1.65e.jpg
fig.81


Figure-1.66.jpg
fig.82


Figure-1.67e.jpg
fig.83


Login

In this section NConf web interface provides the login screen to allow the administrator to login and start generating nagios configuration files.

Figure-1.68e.jpg
fig.84


The username is admin and the password is admin_password as set in step 3 fig.64 above.

Figure-1.69e.jpg
fig.85


NConf Web Interface

Finally this is the NConf web interface after login.

Figure-1.70e.jpg
fig.86


Congratulations! you can start generating nagios configuration files using NConf.

Create configurations by adding or editting in the different sections of the configurations mainly:

  • Contacts
  • Contactgroups
  • Hosts
  • Hostgroups
  • Services

Then when you are done just click on the "Generate Nagios config" link and NConf will ganerate the configurations for you. The only other thing you are required to do in order for the new configurations to take effect is to navigate to the /usr/local/www/apache22/data/nconf/output/ directory and copy the compressed configurations and uncompress them to the /usr/local/etc/nagios/ directory. Then restart Nagios and and the new configurations will take effect.

Adding Hosts

Adding hosts in NConf is done by clicking on the Add link of the Hosts category as shown below:

Figure-1.85a.jpg
fig.87

This will take you to the following page.

Figure-1.86.jpg
fig.88

Input the fields as shown below. The fields marked with a red star must be filled. After filling the fields, click on Submit at the bottom of the page.

Figure-1.87.jpg
fig.89

This will take you to the following page.

Figure-1.88.jpg
fig.90

Select the services you want checked for the host that you have just added. Here we will select check_ping then click on Submit.

Figure-1.89.jpg
fig.91

This will take you to the following page.

Figure-1.90.jpg
fig.92

On the services page choose other services you want checked on the host you have just added. Here we will still choose check_ping and click on Finish.

Figure-1.91.jpg
fig.93

Now we have added one host which is a computer running on FreeBSD. We now want to add a router, but first we need t add router host preset. We will do this by first clicking on the Show link of the Host presets category.

Figure-1.92.jpg
fig.94

As you can see there is no Host preset for a router. Therefore we will create one by clicking on the Add link of the Host presets category as shown below.

Figure-1.93.jpg
fig.95

This will take you to the page shown below where you will enter the host preset name, select which host-alive check to use and finally select the host preset command you want to use as shown below.

Figure-1.94.jpg
fig.96

When you are done click on the Submit button.

Figure-1.95.jpg
fig.97

Now lets add the second host by clicking on the Add link of the Hosts category.

Figure-1.96.jpg
fig.98

Fill in the required fields as shown below and then click on Submit.

Figure-1.97.jpg
fig.99

This will take you to the following page where we willselect check_ping again and click on Submit.

Figure-1.98.jpg
fig.100

This will take you to the following page where we will click on finish.

Figure-1.99.jpg
fig.101

Now we have two hosts added one which is a computer or server and the other is a router. We need to add Hostgroups and this is done by clicking on the Add link of the Hostgroups category as shown.

Figure-1.100.jpg
fig.102

This will take you to the following page where you will fill in the hostgroup name, alias, select the group members and click on Submit. We will create a first Hostgroup of freebsd-servers.

Figure-1.101.jpg
fig.103

This will take you to the following page.

Figure-1.102.jpg
fig.104

Click on the Add link of the Hostgroups category to add the second Hostgroup.

Figure-1.103.jpg
fig.105

Our second Hostgroup will be the one for routers. Fill in the required fields and click on Submit.

Figure-1.104.jpg
fig.106

Now we have created two Hostgroups.

Figure-1.105.jpg
fig.107

When you are done click on the Generate Nagios config link as shown below to generate the nagios configuration files.

Figure-1.106.jpg
fig.108

If all went well you will have a confirmation telling you that the changes were updated successfully.

Figure-1.107.jpg
fig.109

You can click on the + sign on the Default_collector to have a more detailed view of the config files generated.

Figure-1.108.jpg
fig.110

This will look like this.

Figure-1.109.jpg
fig.111

Now you are through with NConf you can logout by clicking on the Logout link as shown.

Figure-1.110.jpg
fig.112

This then takes you back to the Login screen.

Figure-1.111.jpg
fig.113

Now the generated Nagios config files are located in the /usr/local/www/apache22/data/nconf/output/ directory and the are in a compressed file named NagiosConfig.tgz. Therefore navigate to this directory, then uncompress the archive, create two directories in the /usr/local/etc/nagios/ directory and then copy the contents of the two unconmpressed directories into the newly created directories using the following commands:

cd /usr/local/www/apache22/data/nconf/output/
ls -l
tar -xf NagiosConfig.tgz
mkdir /usr/local/etc/nagios/Default_collector/
mkdir /usr/local/etc/nagios/global/
cp -iprv /usr/local/www/apache22/data/nconf/output/Default_collector/ /usr/local/etc/nagios/Default_collector/
cp -iprv /usr/local/www/apache22/data/nconf/output/global/ /usr/local/etc/nagios/global/

Figure-1.112.jpg
fig.114

Figure-1.113.jpg
fig.115

Navigate to the /usr/local/etc/nagios/Default_collector/ directory and do an ls just to make sure that the files have been copied there. Also do the same for the /usr/local/etc/nagios/global/ directory. Then open the /usr/local/etc/nagios/nagios.cfg file and edit it as follows:

cd /usr/local/etc/nagios/Default_collector/
ls -l
cd /usr/local/etc/nagios/global/
ls -l
ee /usr/local/etc/nagios/nagios.cfg


Figure-1.114.jpg
fig.116

Comment all the lines beginning with the the words cfg_file as shown below.

Figure-1.115.jpg
fig.117

Then add the following lines in the section which has line beginning with cfg_dir

cfg_dir=/usr/local/etc/nagios/Default_collector
cfg_dir=/usr/local/etc/nagios/global

Figure-1.116.jpg
fig.118

Save and close /usr/local/etc/nagios/nagios.cfg file and restart nagios and if all went well then you will have no errors.

/usr/local/etc/rc.d/nagios restart


Figure-1.117.jpg
fig.119

This process can be easily done by using a shell script which can be found in the /usr/local/www/apache22/data/nconf/ADD-ONS/deploy_local.sh. You will only be reqiured to edit it and configure it for your own setup:

Most important:

  • OUTPUT_DIR: set the path to 'output/NagiosConfig.tgz'
  • NAGIOS_DIR: set the path to your Nagios config directory

Additionally, you can add the script to /etc/crontab to make the deployment more automatic.

For example in the /etc/crontab file add the following line:

* * * * * root /usr/local/www/apache22/data/nconf/ADD-ONS/deploy_local.sh

OR

*/5 * * * * /bin/bash /usr/local/www/apache22/data/nconf/ADD-ONS/deploy_local.sh 2>&1 > /dev/null


Now lets go to the Nagios web interface, point your web browser which is in a separate computer to http://yourdomain.com/nagios/ or in this case http://192.168.6.37/nagios/. Then enter the username nagiosadmin and the password nagiosweb as set earlier.

Figure-1.118.jpg
fig.120

Figure-1.119.jpg
fig.121

Click on the Hosts link which is one the links on the left hand side bar to see the hosts you have added.

Figure-1.120.jpg
fig.122

There you have them.

Figure-1.121.jpg
fig.123

Congratulations! you can now install Nagios together with its requirements and then add hosts to your Nagios installation using NConf.

Personal tools