Nagios on FreeBSD® 7.2
From BMO
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
fig.1
fig.2
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
Start mysql service
Start the mysql service using the following command:
/usr/local/etc/rc.d/mysql-server start
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
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
Then you can log out of the mysql console screen by using the following command:
exit
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
fig.10
Apache installation options
Make sure to enable mysql-support for apr-dbd on the screen which appears.
fig.11
fig.12
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
Start the apache service.
Start the apache service using the following command:
/usr/local/etc/rc.d/apache22 start
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.
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
fig.17
php installation options
Make sure to enable apache module ie build apache module on the screen that appears.
fig.18
fig.19
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
Make sure to select sessions and mysql support in the configurations options menu that appears.
Select the default configurations options menu that appear later.
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
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
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.
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
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
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.
Nagios
Installation
Install nagios using the following commands:
cd /usr/ports/net-mgmt/nagios/ make install clean
fig.30
nagios installation options
Select the default for all from the installation options that appears on the screen ie select ok then enter.
fig.31
fig.32
gd
Select the default for all from the installation options that appears on the screen ie select ok then enter.
fig.33
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.
fig.35
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.
fig.37
fig.38
Create nagios group and user
fig.39
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
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
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
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
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.
Nagios Apache Configuration
Open the /usr/local/etc/apache22/httpd.conf file using the following command.
ee /usr/local/etc/apache22/httpd.conf
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>

fig.49
Then restart the Apache webserver using the following command:
/usr/local/etc/rc.d/apache22 restart
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.
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
Install perl-DBD-MySQL using the following command
cd /usr/ports/databases/p5-DBD-mysql51/ && make install clean
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
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
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
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/
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;
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
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.
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.
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.
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.
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.
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.
Login
In this section NConf web interface provides the login screen to allow the administrator to login and start generating nagios configuration files.
The username is admin and the password is admin_password as set in step 3 fig.64 above.
NConf Web Interface
Finally this is the NConf web interface after login.
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:
This will take you to the following page.
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.
This will take you to the following page.
Select the services you want checked for the host that you have just added. Here we will select check_ping then click on Submit.
This will take you to the following page.
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.
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.
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.
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.
When you are done click on the Submit button.
Now lets add the second host by clicking on the Add link of the Hosts category.
Fill in the required fields as shown below and then click on Submit.
This will take you to the following page where we willselect check_ping again and click on Submit.
This will take you to the following page where we will click on finish.
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.
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.
This will take you to the following page.
Click on the Add link of the Hostgroups category to add the second Hostgroup.
Our second Hostgroup will be the one for routers. Fill in the required fields and click on Submit.
Now we have created two Hostgroups.
When you are done click on the Generate Nagios config link as shown below to generate the nagios configuration files.
If all went well you will have a confirmation telling you that the changes were updated successfully.
You can click on the + sign on the Default_collector to have a more detailed view of the config files generated.
This will look like this.
Now you are through with NConf you can logout by clicking on the Logout link as shown.
This then takes you back to the Login screen.
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/
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
Comment all the lines beginning with the the words cfg_file as shown below.
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
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
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.
Click on the Hosts link which is one the links on the left hand side bar to see the hosts you have added.
There you have them.
Congratulations! you can now install Nagios together with its requirements and then add hosts to your Nagios installation using NConf.























































































































