14
The Industry Standard in IT Infrastructure Monitoring Purpose The purpose of this document is to show how you can integrate Selenium into Nagios XI. 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. This document provides an example of testing the navigation bar on http://www.nagios.com for changes from the expected results, this 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 explained in their respective sections with brief guides on installation. A Nagios XI server Perl CPAN Perl Modules: TimePerf2 All of these modules will be installed following instructions in this guide A separate system with a GUI installed, in this example we use a CentOS 7 DVD installation (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 The purpose of the separate system with a GUI installed is so that the Selenium application can open the Firefox web browser and actually test the website is working, just like a normal user would. Having a GUI running on your Nagios XI server is not supported and it's better to separate this functionality out to a separate server. Page 1 Copyright © 2010 - 2017 Nagios Enterprises, LLC Updated – February, 2017 Nagios XI - Integrating Selenium with Nagios XI Nagios Enterprises, LLC US: 1-888-NAGIOS-1 Web: www.nagios.com P.O. Box 8154 Saint Paul, MN 55108 USA Int'l: +1 651-204-9102 Email:[email protected] Fax: +1 651-204-9103

Integrating Selenium with Nagios XI · The Industry Standard in IT Infrastructure Monitoring Purpose The purpose of this document is to show how you can integrate Selenium into Nagios

Embed Size (px)

Citation preview

Page 1: Integrating Selenium with Nagios XI · The Industry Standard in IT Infrastructure Monitoring Purpose The purpose of this document is to show how you can integrate Selenium into Nagios

The Industry Standard in IT Infrastructure Monitoring

Purpose

The purpose of this document is to show how you can integrate Selenium into Nagios XI. 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. This document provides an example of testing the navigation bar

on http://www.nagios.com for changes from the expected results, this 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 explained in their respective sections with brief guides

on installation.

● A Nagios XI server

● Perl

● CPAN

● Perl Modules: TimePerf2

● All of these modules will be installed following instructions in this guide

● A separate system with a GUI installed, in this example we use a CentOS 7 DVD installation (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

The purpose of the separate system with a GUI installed is so that the Selenium application can open the Firefox web browser and

actually test the website is working, just like a normal user would. Having a GUI running on your Nagios XI server is not supported and

it's better to separate this functionality out to a separate server.

Page 1

Copyright © 2010 - 2017 Nagios Enterprises, LLCUpdated – February, 2017

Nagios XI - Integrating Selenium with Nagios XI

Nagios Enterprises, LLC US: 1-888-NAGIOS-1 Web: www.nagios.comP.O. Box 8154Saint Paul, MN 55108USA

Int'l: +1 651-204-9102 Email:[email protected]: +1 651-204-9103

Page 2: Integrating Selenium with Nagios XI · The Industry Standard in IT Infrastructure Monitoring Purpose The purpose of this document is to show how you can integrate Selenium into Nagios

Installation Of Firefox Prerequisites

On the CentOS 7 system login as the root user. Open Firefox and install the Selenium IDE which is located here:

https://addons.mozilla.org/en-us/firefox/addon/selenium-ide/

Once Firefox has installed it will need to restart.

Next install the Firefox add-on Selenium IDE: Perl Formatter located here:

https://addons.mozilla.org/en-us/firefox/addon/selenium-ide-perl-formatter/

Once again Firefox will install the add-on and will need to restart.

Once this is finished you will need to change the preferences of the

Selenium IDE add-on. This can be done by typing about:addons in the

address bar of Firefox. In this list of add-ons, click the Preferences button

for the Selenium IDE add-on.

The Preferences window will appear. Near the bottom of the window there

will be a selection of check boxes. Check the Enable experimental

features check box and then click OK at the bottom of the window.

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

In addition to this change, you need to enable the Firefox Menu Bar, as it is hidden by default. With Firefox open, press Alt on your

keyboard and you'll see the menu bar appear. In the View menu select Toolbars > Menu Bar. You'll see that the Menu Bar is

permanently visible now.

Page 2

Copyright © 2010 - 2017 Nagios Enterprises, LLCUpdated – February, 2017

Nagios XI - Integrating Selenium with Nagios XI

Nagios Enterprises, LLC US: 1-888-NAGIOS-1 Web: www.nagios.comP.O. Box 8154Saint Paul, MN 55108USA

Int'l: +1 651-204-9102 Email:[email protected]: +1 651-204-9103

Page 3: Integrating Selenium with Nagios XI · The Industry Standard in IT Infrastructure Monitoring Purpose The purpose of this document is to show how you can integrate Selenium into Nagios

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 https://www.nagios.com homepage. Open the Selenium IDE recorder by selecting it from the menu bar

under Tools. If this is the first time the Selenium IDE recorder has been opened it will open an additional tab in Firefox, the Selenium

page on GitHub. You can close this tab as it's not required.

When the Selenium IDE recorder is opened it starts to record our actions on the Nagios Web page. Perform the following steps on the

Nagios web page to generate data for Selenium:

● From the Company section of the top menu, click the About item

● Once the About Our Company page has finished loading, click the blue Learn More button near the top of the page

● Once the IT Monitoring Software. Nagios Products. Nagios.com page has loaded, click the Solutions section of the top

menu

● Once the Nagios Solutions page has finished loading, close the Firefox window and this will return you to the Selenium IDE

recorder

● Click the red record button on the Selenium IDE to stop recording (right side of the window)

● From the Selenium IDE menu bar click File > Export Test Case As > Perl

● Save the test case to a file called simpletest, save it to the Desktop as we'll copy it to the correct location later

● If you do not have the option for Perl, check to make sure the Selenium IDE: Perl Formatter add-on has been

installed.

This completes the step of recording a test case, you can close the Selenium IDE recorder as it is no longer required.

Page 3

Copyright © 2010 - 2017 Nagios Enterprises, LLCUpdated – February, 2017

Nagios XI - Integrating Selenium with Nagios XI

Nagios Enterprises, LLC US: 1-888-NAGIOS-1 Web: www.nagios.comP.O. Box 8154Saint Paul, MN 55108USA

Int'l: +1 651-204-9102 Email:[email protected]: +1 651-204-9103

Page 4: Integrating Selenium with Nagios XI · The Industry Standard in IT Infrastructure Monitoring Purpose The purpose of this document is to show how you can integrate Selenium into Nagios

Copy Test Case To Nagios XI Server

The simpletest file you saved to your desktop needs to be copied to your Nagios XI server. In a terminal session on your CentOS

system execute the following command where x.x.x.x is the IP Address of your Nagios XI system:

cat ~/Desktop/simpltest | ssh [email protected] "cat > /tmp/simpletest"

You will be prompted to add the ssh key of the Nagios XI server to your list of known hosts, type yes to accept this and then you will

need to provide the password of the root account on the Nagios XI server. Here is an example of the output you can expect:

Setting Up The Selenium Server

The CentOS 7 system will be used as the Selenium server that Nagios XI will contact to request Selenium test cases to be executed.

On the CentOS 7 system open a terminal and run the following as the root user to install these pre-requisites:

yum install -y cpan gcc

Once CPAN has been installed, execute the following command to open CPAN:

cpan

When you first run CPAN, you will be prompted for several configurations. You can accept the default responses for all of these

configuration prompts (just press Enter). Once you've completed those steps you'll be at the cpan[1]> prompt. Execute the following

commands:

install Time::HiRes

install Test::WWW::Selenium

Installing these modules will present more configuration prompts to which you can accept the default responses. Once these have been

installed exit CPAN by typing q and press Enter.

Next Java needs to be installed, execute the following commands in your terminal session:

yum install -y java

Page 4

Copyright © 2010 - 2017 Nagios Enterprises, LLCUpdated – February, 2017

Nagios XI - Integrating Selenium with Nagios XI

Nagios Enterprises, LLC US: 1-888-NAGIOS-1 Web: www.nagios.comP.O. Box 8154Saint Paul, MN 55108USA

Int'l: +1 651-204-9102 Email:[email protected]: +1 651-204-9103

Page 5: Integrating Selenium with Nagios XI · The Industry Standard in IT Infrastructure Monitoring Purpose The purpose of this document is to show how you can integrate Selenium into Nagios

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-release.storage.googleapis.com/2.53/selenium-server-standalone-2.53.0.jar

This is a standalone Selenium server. In order for Nagios to make use of it, you must allow connections from our Nagios server to the

Selenium server on port 4444 where x.x.x.x is the IP Address of your Nagios XI system (the first two lines are one long command):

firewall-cmd --permanent --zone=public --add-rich-rule='rule family="ipv4" source

address="x.x.x.x/32" port protocol="tcp" port="4444" accept'

firewall-cmd --reload

Now start the Selenium server with the -host parameter where y.y.y.y is the IP Address of your CentOS 7 system (NOT your Nagios

XI server). Without specifying this the Selenium server will only accept connections from localhost:

java -jar selenium-server-standalone-2.53.0.jar -host y.y.y.y

Your Selenium server is now running. We will leave the server running for now, but you can stop the server from the terminal window

with Ctrl + C.

Page 5

Copyright © 2010 - 2017 Nagios Enterprises, LLCUpdated – February, 2017

Nagios XI - Integrating Selenium with Nagios XI

Nagios Enterprises, LLC US: 1-888-NAGIOS-1 Web: www.nagios.comP.O. Box 8154Saint Paul, MN 55108USA

Int'l: +1 651-204-9102 Email:[email protected]: +1 651-204-9103

Page 6: Integrating Selenium with Nagios XI · The Industry Standard in IT Infrastructure Monitoring Purpose The purpose of this document is to show how you can integrate Selenium into Nagios

Configure The Nagios XI Server

There are components that need to be installed on your Nagios XI server to be able to communicate with your Selenium server.

In the following commands:

● The make test command will display failures for many of these modules, you can disregard the failures (they are expected)

● The perl Makefile.PL command will use CPAN. If this is the first time CPAN is run you will be prompted for several

configurations. You can accept the default responses for all of these configuration prompts (just press Enter)

Establish a terminal session to your Nagios XI server as root and execute the following commands:

cd /tmp

wget https://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 ../

wget https://assets.nagios.com/downloads/nagiosxi/packages/Time.tar

tar -xvf Time.tar

cd TimePerf2

perl Makefile.PL

make

make test

make install

cd ../

wget https://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

Page 6

Copyright © 2010 - 2017 Nagios Enterprises, LLCUpdated – February, 2017

Nagios XI - Integrating Selenium with Nagios XI

Nagios Enterprises, LLC US: 1-888-NAGIOS-1 Web: www.nagios.comP.O. Box 8154Saint Paul, MN 55108USA

Int'l: +1 651-204-9102 Email:[email protected]: +1 651-204-9103

Page 7: Integrating Selenium with Nagios XI · The Industry Standard in IT Infrastructure Monitoring Purpose The purpose of this document is to show how you can integrate Selenium into Nagios

Configure The simpletest Script for Nagios XI

The next 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. On your Nagios XI server, execute the following commands:

cd /tmp

wget https://assets.nagios.com/downloads/nagiosxi/packages/clean_selenium_script

Once downloaded to the /tmp directory, you can run the clean_selenium_script on the simpletest file you copied to the /tmp

directory earlier. The following command will place the "cleaned" simpletest file into /usr/local/nagios/libexec/ ready for a

future step:

perl clean_selenium_script –f /tmp/simpletest –o /usr/local/nagios/libexec/simpletest

It will output a simple message of Tidy'ing up the code...Done.

The cleaned simpletest file requires some values to be changed to those of our Selenium server. The following command will open

the file in the vi text editor:

vi /usr/local/nagios/libexec/simpletest

When using the vi editor, to make changes press i on the keyboard first to enter insert mode. Press Esc to exit insert mode.

Find the following lines:

my $sel = Test::WWW::Selenium->new( host => "localhost",

port => 4444,

browser => "*chrome",

These values will undoubtedly be different for your situation based on which ports you use for traffic. The default port is 4444 and the

Selenium server used for this document is running on 10.25.13.35. The default browser is chrome, which is not Google Chrome but a

special version of Firefox. We will be using Firefox so that must be changed accordingly. Here are the adjusted values:

my $sel = Test::WWW::Selenium->new( host => "10.25.13.35",

port => 4444,

browser => "*firefox",

When you have finished, save

the changes in vi by typing:

:wq

and press Enter.

Page 7

Copyright © 2010 - 2017 Nagios Enterprises, LLCUpdated – February, 2017

Nagios XI - Integrating Selenium with Nagios XI

Nagios Enterprises, LLC US: 1-888-NAGIOS-1 Web: www.nagios.comP.O. Box 8154Saint Paul, MN 55108USA

Int'l: +1 651-204-9102 Email:[email protected]: +1 651-204-9103

Page 8: Integrating Selenium with Nagios XI · The Industry Standard in IT Infrastructure Monitoring Purpose The purpose of this document is to show how you can integrate Selenium into Nagios

Testing The Script From The Nagios XI Server

Before you can test your simpletest script from the Nagios XI server you will need to make sure the Selenium server on the CentOS

7 machine is running. If you have followed this guide so far, your Selenium server should be running.

Now you can test your script from the Nagios XI server command line by executing the following command:

perl /usr/local/nagios/libexec/simpletest

When you run that command you will see output in the Selenium server terminal window, you will also see the browser open and the

script run.

The Nagios XI server will show the result as it executes:

Page 8

Copyright © 2010 - 2017 Nagios Enterprises, LLCUpdated – February, 2017

Nagios XI - Integrating Selenium with Nagios XI

Nagios Enterprises, LLC US: 1-888-NAGIOS-1 Web: www.nagios.comP.O. Box 8154Saint Paul, MN 55108USA

Int'l: +1 651-204-9102 Email:[email protected]: +1 651-204-9103

Page 9: Integrating Selenium with Nagios XI · The Industry Standard in IT Infrastructure Monitoring Purpose The purpose of this document is to show how you can integrate Selenium into Nagios

Testing Plugin From The Nagios XI Server

Now that your simpletest script is correctly executing, the next step is to add the check_selenium plugin to your Nagios XI

server. Execute the following commands to download the plugin:

cd /usr/local/nagios/libexec/

wget https://assets.nagios.com/downloads/nagiosxi/scripts/check_selenium

chmod 755 check_selenium

Now execute the following command to test that the plugin works with your simpletest script:

./check_selenium –-script=/usr/local/nagios/libexec/simpletest

The check_selenium plugin is executing the simpletest test case, receiving the results from your Selenium server and returning

the data in a way that is readable by Nagios XI.

If you've made it this far, then the hard work is over. All that remains is to create the Nagios XI definitions for a command, host and

service.

Adding The Selenium Command To Nagios XI

Now that you have tested the check_selenium plugin and it is running successfully from the command line and returning valid

information, you can move on to integrating this check with Nagios XI. This is done by:

● Creating a command definition for the check_selenium plugin

● Creating a Nagios XI host object for your Selenium server

● Create a service that uses the newly created command

To keep it simple, the Generic Network Device Wizard will be used to create the host object (using a wizard makes it easy).

Page 9

Copyright © 2010 - 2017 Nagios Enterprises, LLCUpdated – February, 2017

Nagios XI - Integrating Selenium with Nagios XI

Nagios Enterprises, LLC US: 1-888-NAGIOS-1 Web: www.nagios.comP.O. Box 8154Saint Paul, MN 55108USA

Int'l: +1 651-204-9102 Email:[email protected]: +1 651-204-9103

Page 10: Integrating Selenium with Nagios XI · The Industry Standard in IT Infrastructure Monitoring Purpose The purpose of this document is to show how you can integrate Selenium into Nagios

Create Command Definition

To create a command within Nagios XI, navigate to Configure > Core Config Manager > Commands > >_Commands.

Click the Add New button.

Create the command using the information as follows:

Command Name:

check_selenium

Command Line:

$USER1$/check_selenium --script=$USER1$/$ARG1$

Command Type:

check command

Click the Save button and then Apply Configuration.

Create Selenium Server Host Object:

As mentioned earlier, using the Generic Network Device Wizard will make this step easy.

Navigate via the top menu bar to Configure > Run a configuring wizard, and select the Generic Network Device wizard from the list.

In the screenshot on the following page you can see how the search field allows you to quickly find a wizard.

Page 10

Copyright © 2010 - 2017 Nagios Enterprises, LLCUpdated – February, 2017

Nagios XI - Integrating Selenium with Nagios XI

Nagios Enterprises, LLC US: 1-888-NAGIOS-1 Web: www.nagios.comP.O. Box 8154Saint Paul, MN 55108USA

Int'l: +1 651-204-9102 Email:[email protected]: +1 651-204-9103

Page 11: Integrating Selenium with Nagios XI · The Industry Standard in IT Infrastructure Monitoring Purpose The purpose of this document is to show how you can integrate Selenium into Nagios

On Step 1 enter the address to your Selenium

server, in this example it is 10.25.13.35

Click Next to continue to Step 2.

Step 2 is where you define the monitoring options.

Host Name

This is the name the host will be given in

Nagios XI.

Under Device Services the Ping service has

been selected. After the wizard has created

this service you'll go into Core Configuration

Manager (CCM) and change this service so it

performs the Selenium check.

Click Next and then complete the wizard by choosing the required options in Step 3 – Step 5.

To finish up, click on Finish in the final step of the wizard.

Page 11

Copyright © 2010 - 2017 Nagios Enterprises, LLCUpdated – February, 2017

Nagios XI - Integrating Selenium with Nagios XI

Nagios Enterprises, LLC US: 1-888-NAGIOS-1 Web: www.nagios.comP.O. Box 8154Saint Paul, MN 55108USA

Int'l: +1 651-204-9102 Email:[email protected]: +1 651-204-9103

Page 12: Integrating Selenium with Nagios XI · The Industry Standard in IT Infrastructure Monitoring Purpose The purpose of this document is to show how you can integrate Selenium into Nagios

Define Selenium Service:

Now you need to correctly define the Selenium service.

Navigate to Configure > Core Config Manager > Monitoring > Services. Use the Config Name drop down list to select the new

Selenium host, this will display the new Ping service. Click the Edit icon in the actions column to begin editing the service.

Change the Description to Web Interface

Navigation Check.

In the Check command drop down list

select check_selenium.

Enter the following into the $ARG1$ field:

simpletest

You can continue to configure the service

object as you normally would using

templates, check ,alert and misc. settings.

Using the Configuration Wizard to create

this service means that there isn't anything

else you need to do.

Click the Save button.

Apply Configuration to start monitoring the new service.

Page 12

Copyright © 2010 - 2017 Nagios Enterprises, LLCUpdated – February, 2017

Nagios XI - Integrating Selenium with Nagios XI

Nagios Enterprises, LLC US: 1-888-NAGIOS-1 Web: www.nagios.comP.O. Box 8154Saint Paul, MN 55108USA

Int'l: +1 651-204-9102 Email:[email protected]: +1 651-204-9103

Page 13: Integrating Selenium with Nagios XI · The Industry Standard in IT Infrastructure Monitoring Purpose The purpose of this document is to show how you can integrate Selenium into Nagios

The check should now be active and working. From the navigation menu search for Selenium and your new host and service will

appear with it's current state.

That’s it, Nagios XI is now monitoring the https://wwwnagios.com website and we will be altered if something breaks.

An Example Problem

For the purposes of providing the full picture, here's what the plugin would output if the https://www.nagios.com website was

compromised and the content was changed.

Way back at the beginning of this document you created a test case that clicked the About icon on the web page. In the simpletest

file being used by the check_selenium plugin, here is the reference to that:

elsif ( ! $sel->click_ok("xpath=(//a[contains(text(),'About')])[2]") ) {print "Location: " . $sel-

>get_location() . "\n";}

If I change the About word in that line to NOTAbout then the plugin will output the following:

So you can see that if the website was compromised, the check_selenium plugin will alert you to it.

Page 13

Copyright © 2010 - 2017 Nagios Enterprises, LLCUpdated – February, 2017

Nagios XI - Integrating Selenium with Nagios XI

Nagios Enterprises, LLC US: 1-888-NAGIOS-1 Web: www.nagios.comP.O. Box 8154Saint Paul, MN 55108USA

Int'l: +1 651-204-9102 Email:[email protected]: +1 651-204-9103

Page 14: Integrating Selenium with Nagios XI · The Industry Standard in IT Infrastructure Monitoring Purpose The purpose of this document is to show how you can integrate Selenium into Nagios

Selenium Server Notes

Earlier in the document you will have observed the following two behaviours:

● The Selenium Java program needed to be manually started with the following command:

● java -jar selenium-server-standalone-2.53.0.jar -host y.y.y.y

● When the Selenium server executes a test case, it opens the Firefox web browser and performs all the actions.

These two behaviours mean that the Selenium server needs to be logged on as a user AND the Selenium Java program needs to be

started when the user logs on.

Configure CentOS 7 To Automatically Login

Edit the file /etc/gdm/custom.conf and define the following values:

[daemon]

AutomaticLogin=username

AutomaticLoginEnable=True

Replace username with the name of the user that you require to be automatically logged on.

Configure CentOS 7 To Automatically Start Selenium

● Navigate to Applications > System Tools > Startup Applications

● Click the Add button

● Name: Selenium

● Command: /usr/bin/gnome-terminal -e "java -jar /usr/local/selenium/selenium-server-

standalone-2.53.0.jar -host y.y.y.y"

● Where y.y.y.y is the IP Address of your CentOS 7 system

● Click the Add button to create the entry

● Click Close

After implementing those two changes reboot your CentOS system and it will automatically log in and start the Selenium Java

application in a terminal window.

Final Thoughts

Now that you have successfully created a Selenium test case and are monitoring it in Nagios XI, you can proceed to to implement

additional test cases that fit your needs. For any support related questions please visit the Nagios Support Forums at:

https://support.nagios.com/forum/

Page 14

Copyright © 2010 - 2017 Nagios Enterprises, LLCUpdated – February, 2017

Nagios XI - Integrating Selenium with Nagios XI

Nagios Enterprises, LLC US: 1-888-NAGIOS-1 Web: www.nagios.comP.O. Box 8154Saint Paul, MN 55108USA

Int'l: +1 651-204-9102 Email:[email protected]: +1 651-204-9103