Integrate Selenium Test Cases into Nagios

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:

  • Nagios (I assume you have this already running)
  • check_selenium
  • Selenium IDE
  • Firefox
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>
        <project.encoding>UTF-8</project.encoding>
        <jdk.version>1.7</jdk.version>
        <selenium.version>2.46.0</selenium.version>
    </properties>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-clean-plugin</artifactId>
                <version>2.6.1</version>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>${jdk.version}</source>
                    <target>${jdk.version}</target>
                    <encoding>${project.encoding}</encoding>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.7</version>
                <configuration>
                    <encoding>${project.encoding}</encoding>
                </configuration>
            </plugin>
            <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>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.5.5</version>
                <configuration>
                    <descriptors>
                        <descriptor>src/assembly/assembly.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </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>
assembly.xml
<assembly>
    <id>bin</id>
    <formats>
        <!--format>zip</format-->
        <format>dir</format>
    </formats>
    <baseDirectory>libexec</baseDirectory>
    <dependencySets>
        <dependencySet>
            <useProjectArtifact>true</useProjectArtifact>
            <outputDirectory>lib</outputDirectory>
        </dependencySet>
    </dependencySets>
    <fileSets>
        <fileSet>
            <directory>src/main/scripts</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>check_selenium.sh</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>

 CallSeleniumTest.java
package info.devopsabyss;

/*

 * This is a nagios plugin to integrate Selenium Test Cases into Nagios.
 * Copyright (C) 2010 Christian Zunker ([email protected])
 *
 * 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 static final int NAGIOS_OK = 0;
    private static final int NAGIOS_WARNING = 1;
    private static final int NAGIOS_CRITICAL = 2;
    private static final int NAGIOS_UNKNOWN = 3;

    private static final String NAGIOS_TEXT_OK = "OK";
    private static final String NAGIOS_TEXT_WARNING = "WARNING";
    private static final String NAGIOS_TEXT_CRITICAL = "CRITICAL";
    private static 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 (ClassNotFoundException ex) {
            output = seTest.NAGIOS_TEXT_UNKNOWN + " - Test case class: " + messageWithoutNewlines(ex) + " not found!";
            nagios = seTest.NAGIOS_UNKNOWN;
        } catch (Exception ex) {
            printStackTraceWhenVerbose(cmd, ex);
            output = seTest.NAGIOS_TEXT_WARNING + " - " + cmd.getOptionValue("c") + ": " + messageWithoutNewlines(ex);
            nagios = seTest.NAGIOS_WARNING;
        } 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("\r\n", " ").replaceAll("\n", " ");
    }

    private static void printStackTraceWhenVerbose(final CommandLine cmd, final Throwable ex) {
        if (cmd.hasOption("v")) {
            ex.printStackTrace();
        }
    }

    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 + // 当出现7054错误时,在图形界面运行此命令
JAVA_HOME=/opt/java/jdk1.7.0_75
DIR=$(/usr/bin/dirname ${0})

${JAVA_HOME}/bin/java -jar ${DIR}/lib/check-selenium-1.0.jar $@
 
Install Selenium IDE
Selenium IDE is a fully-featured Integrated Development Environment (IDE) that installs as a plugin in Mozilla Firefox and enables developers to test their web applications through Selenium. With the Selenium IDE, you can record user interactions with the web browser and play them back to test for errors.
Download latest released version from addons.mozilla.org or view the Release Notes and then install some plugins. Record Test Case Click the Selenium IDE button to open the Selenium IDE window.


 
  1. Create a new test case by going to File -> New Test Case.
  2. Hit the red record switch button to record our test.
  3. 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
}
  FAQ Error: Package: perl-Net-SNMP-6.0.1-7.el7.noarch (epel)
           Requires: perl(Crypt::DES)
在RHEL 7安装perl-Net-SNMP时,报这个错误,请先到 rpmfind下载安装perl-Crypt-DES-2.05-20.el7.x86_64.rpm   How to confirm the param "DISPLAY"?
Log into the graphical interface:
echo $DISPLAY

Unknown job type: 10 CHECK_NRPE: Received 0 bytes from daemon.  Check the remote server logs for error messages Install nrpe:
./configure --enable-command-args   NRPE installation error: configure: error: Cannot find ssl headers yum install openssl-devel (Service check timed out after 60.01 seconds) 在nagios.cfg配置中service_check_timeout=60,如果页面检查超时,nagios将停止检查,根据实际情况调整参数。 Reference Installing Nagios Core From Source Nagios Core Documentation NRPE Documentation Flexible Notifications for Nagios How To Set Up a Firewall Using FirewallD on CentOS 7 (systemctl start firewalld.service  firewall-cmd --state  firewall-cmd --zone=public --add-port=5000/tcp) Configure Linux to use NTLM authentication proxy (ISA Server) using CNTLM Enabling NTLM Authentication (Single Sign-On) in Firefox Zabbix - The Ultimate Enterprise-class Monitoring Platform

猜你喜欢

转载自billben.iteye.com/blog/2228160