本文共 23027 字,大约阅读时间需要 76 分钟。
Nagios与Selenium结合可以执行更复杂的页面检查,检测网站运行状态、任务运行情况等。
Nagios集成Selenium步骤:
说明:Java Main函数使用了 的CallSeleniumTest类,略作修改。
完整源码可从下载。4.0.0 org.itrunner nagios-selenium 1.0 jar UTF-8 1.8 3.141.5 maven-clean-plugin 3.1.0 maven-compiler-plugin 3.8.0 maven-resources-plugin 3.1.0 ${project.encoding} maven-jar-plugin 3.1.0 org.itrunner.tests.CallSeleniumTest true maven-assembly-plugin 3.1.0 src/assembly/assembly.xml make-assembly package single org.sonarsource.scanner.maven sonar-maven-plugin 3.5.0.1254 org.seleniumhq.selenium selenium-firefox-driver ${selenium.version} org.seleniumhq.selenium selenium-chrome-driver ${selenium.version} org.seleniumhq.selenium selenium-support ${selenium.version} com.codeborne phantomjsdriver 1.4.4 junit junit 4.12 commons-cli commons-cli 1.4 commons-io commons-io 2.6
调用测试的入口类。
CallSeleniumTestpackage org.itrunner.tests;import org.apache.commons.cli.*;import org.openqa.selenium.TimeoutException;import org.openqa.selenium.firefox.NotConnectedException;public class CallSeleniumTest { private static int timeout = 30; 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 TestResult runTest(String className) throws ClassNotFoundException, IllegalAccessException, InstantiationException { ClassseleniumTestClass = (Class ) Class.forName(className); return seleniumTestClass.newInstance().run(); } public static void main(String[] args) { CallSeleniumTest seTest = new CallSeleniumTest(); Option optionClass = new Option("c", "class", true, "full classname of test case (required) e.g. \"org.itrunner.tests.hosts.Baidu\""); Option optionTimeout = new Option("t", "timeout", true, "timeout, default is 30"); 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(optionTimeout); seTest.options.addOption(optionVerbose); seTest.options.addOption(optionHelp); CommandLineParser parser = new DefaultParser(); CommandLine cmd = null; String output = NAGIOS_TEXT_UNKNOWN + " - Upps"; int nagios = 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.hasOption("c")) { usage(seTest.options); System.exit(nagios); //NOSONAR } if (cmd.hasOption("t")) { timeout = Integer.parseInt(cmd.getOptionValue("t")); } TestResult result = seTest.runTest(cmd.getOptionValue("c")); output = NAGIOS_TEXT_OK + " - " + cmd.getOptionValue("c") + " Tests passed - " + result.toString(); nagios = NAGIOS_OK; } catch (ParseException e) { output = NAGIOS_TEXT_UNKNOWN + " - Parameter problems: " + e.getMessage(); nagios = NAGIOS_UNKNOWN; usage(seTest.options); } catch (ClassNotFoundException e) { output = NAGIOS_TEXT_UNKNOWN + " - Test case class: " + e.getMessage() + " not found!"; nagios = NAGIOS_UNKNOWN; } catch (TimeoutException | CriticalException e) { output = NAGIOS_TEXT_CRITICAL + " - Test Failures: " + e.getMessage(); nagios = NAGIOS_CRITICAL; } catch (Exception e) { output = NAGIOS_TEXT_WARNING + " - Test Failures: " + processException(cmd, e); nagios = NAGIOS_WARNING; } finally { println(output); System.exit(nagios); //NOSONAR } } private static String processException(CommandLine cmd, Exception e) { if (cmd != null && cmd.hasOption("v")) { e.printStackTrace(); //NOSONAR } if (isCausedBy(e, NotConnectedException.class)) { return "Failed to connect to binary FirefoxBinary"; } return e.getMessage(); } private static void usage(Options options) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("check_selenium", options); println("This version of check_selenium was tested with:"); println(" - selenium 3.14.0"); println(" - selenium ide 3.3.1"); println(" - test case exported as Java / JUnit 4 / WebDriver"); println("Some example calls:"); println(" ./check_selenium.sh -c \"org.itrunner.tests.hosts.Baidu\""); println(" ./check_selenium.sh --class \"org.itrunner.tests.hosts.Baidu\""); } public static int getTimeout() { return timeout; } private static void println(String x) { System.out.println(x); //NOSONAR } private static boolean isCausedBy(final Throwable exception, Class clazz) { Throwable cause = exception; while (cause != null) { if (clazz.isInstance(cause)) { return true; } cause = cause.getCause(); } return false; }}
check_selenium.sh
#!/bin/bashJAVA_HOME=/opt/java/jdk1.8.0_191$JAVA_HOME/bin/java -Djava.util.logging.config.file=logging.properties -jar $(dirname $0)/lib/nagios-selenium-1.0.jar $@
config.ini
# drive type - chrome, firefox or phantomjsdriver.type=chromelog.file=chrome.loglog.level=FATALchrome.driver=/home/nagios/chrome/chromedrivergecko.driver=/home/nagios/gecko/geckodriverphantomjs.binary.path=/home/nagios/phantomjs/bin/phantomjsphantomjs.ghostdriver.path=/home/nagios/ghostdriver-master/src/main.jsproxy.host=baidu.url=https://www.baidu.combaidu.username=xxxxxx@163.combaidu.password=xxxxxx
Config
import java.io.FileReader;import java.io.IOException;import java.util.Properties;public enum Config { CONFIG; private static final String FILE_NAME = "config.ini"; private Properties properties; Config() { properties = new Properties(); try { properties.load(new FileReader(CommonUtil.getJarPath() + FILE_NAME)); } catch (IOException e) { //NOSONAR e.printStackTrace(); //NOSONAR } } public String getDriverType() { return getProperty("driver.type"); } public String getLogFile() { return getProperty("log.file"); } public String getLogLevel() { return getProperty("log.level"); } public String getChromeDriver() { return getProperty("chrome.driver"); } public String getGeckoDriver() { return getProperty("gecko.driver"); } public String getPhantomJsBinaryPath() { return getProperty("phantomjs.binary.path"); } public String getPhantomJsGhostDriverPath() { return getProperty("phantomjs.ghostdriver.path"); } public String getProxyHost() { return getProperty("proxy.host"); } public String getBaiduUrl() { return getProperty("baidu.url"); } public String getBaiduUsername() { return getProperty("baidu.username"); } public String getBaiduPassword() { return getProperty("baidu.password"); } public String getProperty(String key) { return properties.getProperty(key); }}
在nagios中运行Selenium测试时,为避免浪费资源,提高性能,一般不使用图形界面。
Selenium支持的Headless Web Driver:轻量级的Web Driver实现,基于HtmlUnit,纯Java、支持JavScript。HtmlUnit可以模拟Chrome、Firefox或IE浏览器,默认为Chrome。HtmlUnit使用了JavaScript引擎Rhino,主流浏览器没有采用Rhino的,测试时可能会有JavaScript兼容性问题。
org.seleniumhq.selenium htmlunit-driver 2.33.1
public static WebDriver createHtmlUnitDriver() { HtmlUnitDriver htmlUnitDriver = new HtmlUnitDriver(true); if (hasProxy()) { htmlUnitDriver.setProxySettings(getProxy()); } return htmlUnitDriver;}
基于Headless Browser PhantomJS,对JavaScript的支持好,常用于网络监控,但PhantomJS项目现已暂停,selenium 3.8.1以后不再支持。
使用PhantomJSDriver时,需下载和,配置“phantomjs.binary.path”和“phantomjs.ghostdriver.path”。启用Headless模式:
FirefoxOptions options = new FirefoxOptions();options.setHeadless(true);
Selenium 3 FirefoxDriver提供两种DriverService:GeckoDriverService和XpiDriverService,默认使用GeckoDriverService,需安装firefox,下载,配置GeckoDriver路径。
FirefoxDriver与Nagios集成,若Driver没有正常执行quit(),会造成大量进程不能终止,临时目录堆积大量profile文件。启用Headless模式:
ChromeOptions options = new ChromeOptions();options.setHeadless(true);
使用ChromeDriver,需安装Chrome,下载,配置ChromeDriver路径。
DriverFactory创建ChromeDriver、FirefoxDriver或PhantomJSDriver。package org.itrunner.tests;import org.openqa.selenium.Proxy;import org.openqa.selenium.chrome.ChromeDriverService;import org.openqa.selenium.chrome.ChromeOptions;import org.openqa.selenium.firefox.FirefoxDriverLogLevel;import org.openqa.selenium.firefox.FirefoxOptions;import org.openqa.selenium.firefox.GeckoDriverService;import org.openqa.selenium.phantomjs.PhantomJSDriver;import org.openqa.selenium.remote.DesiredCapabilities;import org.openqa.selenium.remote.RemoteWebDriver;import org.openqa.selenium.remote.service.DriverService;import java.io.File;import java.util.ArrayList;import java.util.List;import static org.itrunner.tests.utils.Config.CONFIG;import static org.openqa.selenium.phantomjs.PhantomJSDriverService.*;public class DriverFactory { private static DriverService service; private static RemoteWebDriver driver; private DriverFactory() { } public static RemoteWebDriver createDriver() { String driverType = CONFIG.getDriverType(); if (driverType.equals("phantomjs")) { return createPhantomJSDriver(); } if (driverType.equals("firefox")) { return createFirefoxDriver(); } return createChromeDriver(); } public static RemoteWebDriver createChromeDriver() { try { service = new ChromeDriverService.Builder() .usingDriverExecutable(new File(CONFIG.getChromeDriver())) .usingAnyFreePort() .withSilent(true) .withVerbose(false) .withLogFile(new File(CONFIG.getLogFile())) .build(); service.start(); ChromeOptions options = new ChromeOptions(); options.setHeadless(true); if (hasProxy()) { options.setProxy(getProxy()); } driver = new RemoteWebDriver(service.getUrl(), options); } catch (Exception e) { // do nothing } return driver; } public static RemoteWebDriver createFirefoxDriver() { try { service = new GeckoDriverService.Builder() .usingDriverExecutable(new File(CONFIG.getGeckoDriver())) .usingAnyFreePort() .withLogFile(new File(CONFIG.getLogFile())) .build(); service.start(); FirefoxOptions options = new FirefoxOptions(); options.setHeadless(true); options.setLogLevel(FirefoxDriverLogLevel.fromString(CONFIG.getLogLevel())); if (hasProxy()) { options.setProxy(getProxy()); } driver = new RemoteWebDriver(service.getUrl(), options); } catch (Exception e) { // do nothing } return driver; } public static RemoteWebDriver createPhantomJSDriver() { DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setJavascriptEnabled(true); capabilities.setCapability(PHANTOMJS_EXECUTABLE_PATH_PROPERTY, CONFIG.getPhantomJsBinaryPath()); capabilities.setCapability(PHANTOMJS_GHOSTDRIVER_PATH_PROPERTY, CONFIG.getPhantomJsGhostDriverPath()); ListcliArgs = new ArrayList<>(); cliArgs.add("--web-security=false"); cliArgs.add("--ssl-protocol=any"); cliArgs.add("--ignore-ssl-errors=true"); capabilities.setCapability(PHANTOMJS_CLI_ARGS, cliArgs); // Control LogLevel for GhostDriver, via CLI arguments String[] ghostDriverCliArgs = {"--logFile=" + CONFIG.getLogFile(), "--logLevel=" + CONFIG.getLogLevel()}; capabilities.setCapability(PHANTOMJS_GHOSTDRIVER_CLI_ARGS, ghostDriverCliArgs); if (hasProxy()) { capabilities.setCapability("proxy", getProxy()); } driver = new PhantomJSDriver(capabilities); return driver; } public static void quit() { if (driver != null) { driver.quit(); } if (service != null) { service.stop(); } driver = null; service = null; } private static Proxy getProxy() { Proxy proxy = new Proxy(); proxy.setHttpProxy(CONFIG.getProxyHost()); return proxy; } private static boolean hasProxy() { return CONFIG.getProxyHost() != null && !CONFIG.getProxyHost().equals(""); }}
说明:
TestBase
测试基类package org.itrunner.tests;import org.apache.commons.io.FileUtils;import org.junit.After;import org.junit.Before;import org.openqa.selenium.By;import org.openqa.selenium.NoSuchElementException;import org.openqa.selenium.TimeoutException;import org.openqa.selenium.remote.RemoteWebDriver;import org.openqa.selenium.support.ui.ExpectedCondition;import org.openqa.selenium.support.ui.WebDriverWait;import java.io.File;import java.io.IOException;import java.nio.charset.Charset;import java.util.concurrent.TimeUnit;import static java.lang.System.currentTimeMillis;import static org.openqa.selenium.OutputType.BYTES;import static org.openqa.selenium.support.ui.ExpectedConditions.titleIs;import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOfElementLocated;public abstract class TestBase { protected RemoteWebDriver driver; private long startTime; private TestResult result = new TestResult(); @Before public void setup() { startTime = currentTimeMillis(); nextStep(this::createDriver, "init"); setTimeout(); addShutdownHook(); } TestResult run() { setup(); test(); tearDown(); setTotalTime(); return result; } @After public void tearDown() { nextStep(() -> DriverFactory.quit(), "destroy"); } private void createDriver() { driver = DriverFactory.createDriver(); } public abstract void test(); protected void savePageSource(String host) { try { File sourceFile = new File("reports/" + host + ".html"); FileUtils.writeStringToFile(sourceFile, driver.getPageSource(), Charset.defaultCharset()); } catch (IOException e) { //NOSONAR // do nothing } } protected void takeScreenshot(String host) { File imageFile = new File("reports/" + host + ".png"); try { FileUtils.writeByteArrayToFile(imageFile, driver.getScreenshotAs(BYTES)); } catch (IOException e) { //NOSONAR // do nothing } } protected void nextStep(TestStep step, String stepName) { long beginTime = currentTimeMillis(); try { step.run(); } catch (TimeoutException | NoSuchElementException e) { throw new CriticalException(stepName + " failed: " + e.getMessage(), e); } result.putStepTime(stepName, currentTimeMillis() - beginTime); } public boolean isElementPresent(By by) { try { return waitForElementPresent(by); } catch (TimeoutException e) { //NOSONAR return false; } } protected void open(final String url) { nextStep(() -> driver.get(url), "loading page"); } public void click(final By by, String stepName) { nextStep(() -> driver.findElement(by).click(), stepName); } protected void waitForTitlePresent(final String title) { nextStep(() -> waitForCondition(titleIs(title)), "finding title"); } private boolean waitForElementPresent(By by) { return waitForCondition(visibilityOfElementLocated(by)).isDisplayed(); } privateT waitForCondition(ExpectedCondition condition) { return (new WebDriverWait(driver, CallSeleniumTest.getTimeout())).until(condition); } private void setTimeout() { driver.manage().timeouts().implicitlyWait(CallSeleniumTest.getTimeout(), TimeUnit.SECONDS); } private void setTotalTime() { result.setTotalTime(currentTimeMillis() - startTime); } private void addShutdownHook() { Runtime.getRuntime().addShutdownHook(new Thread("Selenium Quit Hook") { @Override public void run() { DriverFactory.quit(); } }); }}
示例代码记录了每步执行的时间。
网站测试package org.itrunner.tests.hosts;import org.itrunner.tests.TestBase;import org.junit.Test;import org.openqa.selenium.By;import static org.itrunner.tests.utils.Config.CONFIG;public class Baidu extends TestBase { @Override @Test public void test() { open(CONFIG.getBaiduUrl()); waitForTitlePresent("百度一下,你就知道"); nextStep(() -> { driver.findElement(By.linkText("登录")).click(); driver.findElement(By.id("TANGRAM__PSP_10__footerULoginBtn")).click(); driver.findElement(By.id("TANGRAM__PSP_10__userName")).sendKeys(CONFIG.getBaiduUsername()); driver.findElement(By.id("TANGRAM__PSP_10__password")).sendKeys(CONFIG.getBaiduPassword()); driver.findElement(By.id("TANGRAM__PSP_10__submit")).click(); }, "login"); }}
在nagios中运行是不需要selenium日志的,将其关闭。
logging.propertiesorg.openqa.selenium.level=OFF
使用maven assembly打包,为便于随时调整配置,将配置文件放在Jar外。
bin dir libexec true lib src/main/resources . check_selenium.sh target/classes lib config.ini
打包后的结构如下:
部署时将libexec目录下的内容拷贝到nagios/libexec目录即可。define command { command_name check_selenium command_line $USER1$/check_selenium.sh -c $ARG1$ }
define service { service_description selenium_baidu use service-check-05min host_name www.baidu.com check_command check_selenium!org.itrunner.tests.hosts.Baidu}
Error: Package: perl-Net-SNMP-6.0.1-7.el7.noarch (epel) Requires: perl(Crypt::DES)
在RHEL 7安装perl-Net-SNMP时,报这个错误,请先到下载安装perl-Crypt-DES-2.05-20.el7.x86_64.rpmCHECK_NRPE: Received 0 bytes from daemon. Check the remote server logs for error messages
Install nrpe:./configure --enable-command-argsNRPE installation error: configure: error: Cannot find ssl headers
yum install openssl-devel创建文件/etc/yum.repos.d/google-chrome.repo,内容如下:
[google-chrome]name=google-chromebaseurl=http://dl.google.com/linux/chrome/rpm/stable/$basearchenabled=1gpgcheck=1gpgkey=https://dl-ssl.google.com/linux/linux_signing_key.pub
$ wget https://dl.google.com/linux/linux_signing_key.pub$ sudo rpm --import linux_signing_key.pub
$ sudo yum install google-chrome-stable
转载于:https://blog.51cto.com/7308310/2289762