The check_selenium source code can be found on github:https://github.com/czunker/check_selenium
With this check it is possible to put your recorded test cases from your Selenium IDE into Nagios to use them for monitoring.
In my opinion this has the following advantages:
- You can transfer your test cases to monitoring without making any changes.
- You are more flexible, when monitoring more complex scenarios.
To get this working, you need the following components:
check_selenium Source Code:
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>info.devopsabyss</groupId>
<artifactId>check-selenium</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<properties>
<selenium.version>2.46.0</selenium.version>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifest>
<mainClass>info.devopsabyss.CallSeleniumTest</mainClass>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
<version>${selenium.version}</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-support</artifactId>
<version>${selenium.version}</version>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>
CallSeleniumTest.java
package info.devopsabyss;
/*
* This is a nagios plugin to integrate Selenium Test Cases into Nagios.
* Copyright (C) 2010 Christian Zunker (devops.abyss@googlemail.com)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
import org.apache.commons.cli.*;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
import java.util.List;
public class CallSeleniumTest {
private final int NAGIOS_OK = 0;
private final int NAGIOS_WARNING = 1;
private final int NAGIOS_CRITICAL = 2;
private final int NAGIOS_UNKNOWN = 3;
private final String NAGIOS_TEXT_OK = "OK";
private final String NAGIOS_TEXT_WARNING = "WARNING";
private final String NAGIOS_TEXT_CRITICAL = "CRITICAL";
private final String NAGIOS_TEXT_UNKNOWN = "UNKNOWN";
private Options options = null;
private Result runJUnitTest(String className) throws ClassNotFoundException {
Class<?> seleniumTestClass = Class.forName(className);
return new JUnitCore().run(seleniumTestClass);
}
public static void main(String[] args) throws Exception {
CallSeleniumTest seTest = new CallSeleniumTest();
Option optionClass = new Option("c", "class", true, "full classname of test case (required) e.g. \"com.example.tests.GoogleSeleniumWebdriverTestCase\"");
Option optionVerbose = new Option("v", "verbose", false, "show a lot of information (useful in case of problems)");
Option optionHelp = new Option("h", "help", false, "show this help screen");
seTest.options = new Options();
seTest.options.addOption(optionClass);
seTest.options.addOption(optionVerbose);
seTest.options.addOption(optionHelp);
CommandLineParser parser = new DefaultParser();
CommandLine cmd = null;
String output = seTest.NAGIOS_TEXT_UNKNOWN + " - Upps |";
int nagios = seTest.NAGIOS_UNKNOWN;
try {
cmd = parser.parse(seTest.options, args);
// has to be checked manually, otherwise you can't access the help message without specifying correct parameters
if (cmd.hasOption("h") || cmd.getOptionValue("c") == null) {
usage(seTest.options);
System.exit(nagios);
}
Result result = seTest.runJUnitTest(cmd.getOptionValue("c"));
if (result.wasSuccessful()) {
output = seTest.NAGIOS_TEXT_OK + " - " + cmd.getOptionValue("c") + " Tests passed | ExecTime=" + result.getRunTime() + "ms";
nagios = seTest.NAGIOS_OK;
} else {
printStackTraceWhenVerbose(cmd, result.getFailures());
String failureMessage = result.getFailures().toString();
output = seTest.NAGIOS_TEXT_CRITICAL + " - " + cmd.getOptionValue("c") + " Test Failures: " + withoutNewlines(failureMessage) + " | ExecTime=" + result.getRunTime() + "ms";
nagios = seTest.NAGIOS_CRITICAL;
}
} catch (ParseException ex) {
output = seTest.NAGIOS_TEXT_UNKNOWN + " - " + "Parameter problems: " + messageWithoutNewlines(ex);
nagios = seTest.NAGIOS_UNKNOWN;
usage(seTest.options);
} catch (NoClassDefFoundError ex) {
printStackTraceWhenVerbose(cmd, ex);
output = seTest.NAGIOS_TEXT_UNKNOWN + " - " + cmd.getOptionValue("c") + ": " + messageWithoutNewlines(ex);
nagios = seTest.NAGIOS_UNKNOWN;
} catch (ClassNotFoundException ex) {
output = seTest.NAGIOS_TEXT_UNKNOWN + " - " + cmd.getOptionValue("c") + ": Test case class " + messageWithoutNewlines(ex) + " not found!";
nagios = seTest.NAGIOS_UNKNOWN;
} catch (Exception ex) {
printStackTraceWhenVerbose(cmd, ex);
output = seTest.NAGIOS_TEXT_CRITICAL + " - " + cmd.getOptionValue("c") + ": " + messageWithoutNewlines(ex);
nagios = seTest.NAGIOS_CRITICAL;
} finally {
System.out.println(output);
System.exit(nagios);
}
}
private static String messageWithoutNewlines(final Throwable ex) {
return withoutNewlines(ex.getMessage());
}
private static String withoutNewlines(final String message) {
return message.replaceAll("\n", " ").replaceAll(" ", " ").replaceAll(" ", " ").replaceAll(" ", " ");
}
private static void printStackTraceWhenVerbose(final CommandLine cmd, final Throwable ex) {
if (cmd.hasOption("v")) {
System.out.println(ex.getMessage());
}
}
private static void printStackTraceWhenVerbose(final CommandLine cmd, final List<Failure> failures) {
for (Failure failure : failures) {
printStackTraceWhenVerbose(cmd, failure.getException());
}
}
private static void usage(Options options) {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("check_selenium", options);
System.out.println("This version of check_selenium was tested with:");
System.out.println(" - selenium 2.46.0");
System.out.println(" - selenium ide 2.5.0");
System.out.println(" - test case exported as Java / JUnit 4 / WebDriver");
System.out.println("Some example calls:");
System.out.println(" ./check_selenium.sh -c \"com.example.tests.GoogleSeleniumWebdriverTestCase\"");
System.out.println(" ./check_selenium.sh --class \"com.example.tests.GoogleSeleniumWebdriverTestCase\"");
}
}
check_selenium.sh
#!/bin/bash
export DISPLAY=:0
#xhost +
JAVA_HOME=/opt/java/jdk1.7.0_75
DIR=$(/usr/bin/dirname ${0})
${JAVA_HOME}/bin/java -jar ${DIR}/lib/check-selenium.jar $@
Install Selenium IDE
- Download Selenium IDE
- Firefox -> Add-ons -> Install Add-on From File... -> Restart Firefox
Record Test Case
After recording it, export it to a Java file (File -> Export Test Case As ... -> Java / JUnit4 / WebDriver).
Compile this Java file with javac or your favorite IDE.
Deploy to Nagios
On your Nagios host, put check_selenium.sh into your libexec path, add check-selenium.jar and other dependencies in the libexec/lib path.
As the Selenium IDE puts the Java classes automatically in the package com.example.tests,
create a directory com/example/tests on your filesystem and add it to the classpath. Put the compiled Java file into this directory.
Define Command and Service
Add the definition to your nagios configuration and test it.
define command {
command_name check_selenium
command_line $USER1$/check_selenium.sh -c $ARG1$
}
After that, you can define your service and asign it to a host:
define service {
service_description selenium_Google
use service-check-05min
host_name your.server.here
check_command check_selenium!com.example.tests.GoogleTestCase
}