Integrating Selenium with Nagios XI

Nagios XI - Integrating Selenium with Nagios XI
The Industry Standard in IT Infrastructure Monitoring
Purpose
The purpose of this document is to show how integrating Selenium into Nagios XI can be done. Selenium is a powerful tool for web
browser based testing and automation. Integrating Selenium with Nagios XI allows users to create service checks which automatically
test your website and trigger status changes within Nagios XI if issues occur. In this document we will be testing the navigation bar on
http://www.nagios.com for changes from the expected results which would indicate that an issue has occurred with the website.
Target Audience
This document is designed for Nagios administrators interested in integrating Selenium web testing into Nagios XI to automate testing
of their website and allow more complexity within their service checks.
Prerequisites
The following are basic prerequisites required for using Selenium. They will be gone over in their respective sections with brief guides
on installation.
•
A Nagios XI server with NRPE installed and functioning (Not covered in this document)
◦ Perl
◦ CPAN
◦ Perl Modules: TimePerf2
If you do not have NRPE installed and working on your Nagios XI server, you can follow the instruction in the document below:
http://assets.nagios.com/downloads/nagiosxi/docs/Monitoring_Hosts_Using_NRPE.pdf
•
A separate system with a GUI installed, in this example we will be using Fedora 17 (Not covered in this document)
◦ Firefox installed with the Selenium IDE addon
◦ CPAN
◦ Java
◦ Perl Modules: Time HiRes
◦ Selenium RC Server
◦ Selenium IDE: Perl Formatter
Installation Of Firefox Prerequisites
We will start with the Fedora 17 system and login as the root user. Open Firefox and install the Selenium IDE which is located here:
http://release.seleniumhq.org/selenium-ide/2.4.0/selenium-ide-2.4.0.xpi
Once Firefox has installed it will need to restart. Next install the Firefox addon Selenium IDE: Perl Formatter located here:
https://addons.mozilla.org/en-US/firefox/addon/selenium-ide-perl-formatter/?src=search
Once again Firefox will install the addon and need to restart. Once this is finished click on Tools in the Firefox navigation bar, and
select Selenium IDE.
We will be greeted by the addon's pop out window, and in order to make use of the Perl Formatter we just installed, we must enable a
setting located in Options. To do so select the Options from the menu bar and then select Options from the drop down. Near the
bottom of the pane there will be a selection of check boxes. Check the Enable experimental features check box, and then click Ok at
the bottom of the pane.
Nagios Enterprises, LLC US: 1-888-NAGIOS-1
P.O. Box 8154
Int'l: +1 651-204-9102
Saint Paul, MN 55108
Fax: +1 651-204-9103
USA
Web: www.nagios.com
Email:[email protected]
Page 1
Copyright © 2010 - 2014 Nagios Enterprises, LLC
Revision 1.0 – July, 2014
Nagios XI - Integrating Selenium with Nagios XI
If you wish to familiarize yourself with the Selenium IDE, a documentation index from
Selenium may be found here:
http://seleniumhq.org/projects/ide/plugins.html
Recording A Test Case
For the purpose of this documentation we will be creating a short Test Case based
around the Nagios Enterprises website.
Begin by navigating to the http://www.google.com/ search homepage. Open the
Selenium IDE recorder by selecting it from the Tool options. This starts to record our
actions. Select the Google search pane by clicking in the search box, type “Nagios” and
hit the Enter key. Once the search page has been brought up and the results are listed,
select the first result:
Nagios – The Industry Standard in IT Infrastructure Monitoring
When the page loads click the News tab on the Nagios.com navigation panel. Once
that has loaded do the same for Products, and then Documentation tabs. Once the
Documentation page has loaded exit Firefox (Not Selenium IDE), and click the red
record button on the Selenium IDE to stop recording. Now from the IDE's action bar click File, Export Test Case As, Perl. Save the
example test to a file
sampletest.
Setting Up The Selenium Server
From the Fedora 17 system, or your choice distribution. Open the terminal and run the following as the root user:
yum install cpan
cpan
For this example, we selected to configure as much of CPAN automatically and let it select our mirror sites. Once CPAN loads type the
following:
install Time::HiRes
install Test::WWW::Selenium
Nagios Enterprises, LLC US: 1-888-NAGIOS-1
P.O. Box 8154
Int'l: +1 651-204-9102
Saint Paul, MN 55108
Fax: +1 651-204-9103
USA
Web: www.nagios.com
Email:[email protected]
Page 2
Copyright © 2010 - 2014 Nagios Enterprises, LLC
Revision 1.0 – July, 2014
Nagios XI - Integrating Selenium with Nagios XI
Once these have been installed exit CPAN by typing “q” and hitting enter then install Java by running:
yum install java
Create a new directory for the Selenium server and Navigate
to the new directory (in this example usr/local/selenium) and download the stand alone Selenium server:
mkdir /usr/local/selenium
cd /usr/local/selenium
wget http://selenium.googlecode.com/files/selenium-server-standalone-2.35.0.jar
Since the server is stand alone all that needs to be done to initiate it is:
java -jar selenium-server-standalone2.35.0.jar
And exit by using Ctrl + C
Setting Up The Nagios Server
Moving to the Nagios Server's terminal, we will need to be logged into the machine as root to install Perl:
yum install perl
And also install CPAN:
yum install cpan
Start running cpan:
cpan
(Configure as much as possible automatically)
Once CPAN is installed enter the following:
install WWW::Selenium
Once this has been installed exit CPAN by typing “q”. A few modules do not correctly install via CPAN so we must install them by hand:
cd /tmp
wget http://assets.nagios.com/downloads/nagiosxi/packages/Test-WWW-Selenium-1.36.tar.gz
tar -zxvf Test-WWW-Selenium-1.36.tar.gz
cd Test-WWW-Selenium-1.36
perl Makefile.PL
make
make test
make install
cd ../
And then the second file:
wget http://assets.nagios.com/downloads/nagiosxi/packages/Time.tar
tar -xvf Time.tar
Nagios Enterprises, LLC US: 1-888-NAGIOS-1
P.O. Box 8154
Int'l: +1 651-204-9102
Saint Paul, MN 55108
Fax: +1 651-204-9103
USA
Web: www.nagios.com
Email:[email protected]
Page 3
Copyright © 2010 - 2014 Nagios Enterprises, LLC
Revision 1.0 – July, 2014
Nagios XI - Integrating Selenium with Nagios XI
cd TimePerf2
perl Makefile.PL
make
make test
make install
cd ../
And the third:
wget http://assets.nagios.com/downloads/nagiosxi/packages/Test-Mock-LWP-0.06.tar.gz
tar -zxvf Test-Mock-LWP-0.06.tar.gz
cd Test-Mock-LWP-0.06
perl Makefile.PL
make
make test
make install
cd ../
Customize The Simpletest Script
The final step that must be completed before testing our Perl script is to install a script cleaner. This script adds the TimePerf2 usability
to our scripts and cleans up the code a bit. The script may be found here:
wget http://assets.nagios.com/downloads/nagiosxi/packages/clean_selenium_script
Once downloaded to the /tmp directory be sure to move the Selenium test that we recorded at the beginning of this document, to the
same directory you placed the clean_selenium_script in, then run the following:
perl clean_selenium_script --filename=simpletest –o=simpletest
If you placed your simpletest script in another directory simply change the command like so:
perl clean_selenium_script –filename=/path/to/simpletest –o=simpletest
This will edit and format the file named simpletest and rename the output as simpletest once again.
Now, we must set the values inside the Perl script to those of
our Selenium server. Use the text editor of your choice and
edit the simpletest file. Edit following lines:
new(host => “<selenium.server.ip.here>”,
port => <port here>
browser => “*firefox”,
These values will undoubtedly be different for your situation based on which ports you use for traffic, the default port for instance is
4444. The default browser is chrome, which is not Google Chrome but a special version of Firefox, but we will be using Firefox so that
must be changed accordingly.
Save your changes to the simpletest file.
Testing The Script From The Nagios Server
Before you can test your simpletest script from the Nagios XI server you will need to start the selenium server on the remote machine
again. This is done by running the following command on your remote machine:
java -jar /usr/local/selenium/selenium-server-standalone-2.35.0.jar
Nagios Enterprises, LLC US: 1-888-NAGIOS-1
P.O. Box 8154
Int'l: +1 651-204-9102
Saint Paul, MN 55108
Fax: +1 651-204-9103
USA
Web: www.nagios.com
Email:[email protected]
Page 4
Copyright © 2010 - 2014 Nagios Enterprises, LLC
Revision 1.0 – July, 2014
Nagios XI - Integrating Selenium with Nagios XI
Now we may test our script from the Nagios XI server command line:
perl simpletest
And shortly after you run that command you will see output from the test coming through to the Nagios Server, you can also see the
browser open and the script run on the Fedora 17 system.
Finally, we must add the Selenium check to our plugin directory on our Nagios server, usually /usr/local/nagios/libexec.
The check can be found here:
wget http://assets.nagios.com/downloads/nagiosxi/scripts/check_selenium
Once the plugin has been added, make sure you are in the libexec directory and run the following:
chmod 755 check_selenium
./check_selenium -–script=/usr/local/nagios/libexec/simpletest
What the check_selenium script is doing is running the simpletest script and returning the data all at once at the end in a friendly way
readable by Nagios XI.
Adding The Selenium Command To Nagios XI
From the Nagios XI Navigation bar select Configure → Core
Config Manager → Commands → Add New.
Here fill in the Command Management boxes with the following:
Command: Selenium Check
Command line: $USER1$/check_selenium -–script=$USER1$/$ARG1$
Command Type: check command
Note: You can chose a command name to match your requirements
Then click Save.
Adding A Selenium Service To Nagios XI
Now while still in the Core Config Manager select Services from the menu on the left side and click Add New.
Nagios Enterprises, LLC US: 1-888-NAGIOS-1
P.O. Box 8154
Int'l: +1 651-204-9102
Saint Paul, MN 55108
Fax: +1 651-204-9103
USA
Web: www.nagios.com
Email:[email protected]
Page 5
Copyright © 2010 - 2014 Nagios Enterprises, LLC
Revision 1.0 – July, 2014
Nagios XI - Integrating Selenium with Nagios XI
From the Common Settings tab we will fill in the boxes with the following:
Config Name: Selenium Nagios
Service description: Web Interface Navigation
Check
Check Command: Selenium Check
$ARG1$: simpletest
Note: You can chose a service name/description to match
your requirements.
Click the Manage Host button to select the Host on which
the Selenium Server is running. Click Add Selected and
Close.
Finally add a Template via the Manage Template button (or
fill in the required Check Settings and Alert Setting) and
click Save.
You will be brought back to the Services page and from here
click Apply Configuration.
The Selenium server should now be monitoring this Nagios XI check and reporting back as a service which is viewable from
Home → Service Detail:
Wrapping Up
For any support related questions please visit the Nagios Support Forums at:
http://support.nagios.com/forum/
Nagios Enterprises, LLC US: 1-888-NAGIOS-1
P.O. Box 8154
Int'l: +1 651-204-9102
Saint Paul, MN 55108
Fax: +1 651-204-9103
USA
Web: www.nagios.com
Email:[email protected]
Page 6
Copyright © 2010 - 2014 Nagios Enterprises, LLC
Revision 1.0 – July, 2014