LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (http://www.linuxquestions.org/questions/linux-server-73/)
-   -   Nagios - Selenium: check for file content and compares with specified threshold (http://www.linuxquestions.org/questions/linux-server-73/nagios-selenium-check-for-file-content-and-compares-with-specified-threshold-4175416360/)

Cr45h 07-12-2012 11:03 AM

Nagios - Selenium: check for file content and compares with specified threshold
 
Hi all,
firstly I hope to write in the correct subforum.

BTW, I need to find some kind of nagios check that can read a value inside a .html file (particularly it's a result of selenium testcase) and compare it with some specified values that will act as a threshold for warning/critical status on nagios.


Out there (nagios-exchange) I found some "log_file" read check or something similar, but I don't think I can re-use that for my aim... Maybe with a bit of bash scripts or so on I can find a solution... but I'm very newbie in bash scripting.. :scratch:

Does anyone had a similar need before?


My goal is to have a selenium check for site performance and compare with a fixed threshold that send me a notification with nagios telling that a particular selenium testcase is slower than expected... (I know it's not so simple to understand and almost impossible but who knows.....)

Thanks in advance...

MensaWater 07-12-2012 12:48 PM

I don't use Selenium.

However, we have various web pages we've written customized scripts to do checks on. There are text based web browsers that you can use as the basis of such a script. They generally work well so long as you're not trying to access Javascript based functions.

I'd say the two I most commonly use are wget or curl. However others exists such as lynx, links, elinks etc...

curl and wget in their simplest form are simply followed by the URL you want to get information from. You can look at man pages for them to get more options (e.g. dump or source options are sometimes helpful when looking for information not gotten by basic web attachment).

Nagios allows you to develop scripts and use those for your checks. The key to such scripts is that they output only a single line of text (i.e. Nagios pukes on multiline output) for status and one of the return codes that Nagios understands which are:
UNKNOWN_STATE=3
CRITICAL_STATE=2
WARNING_STATE=1
OK_STATE=0
To issue such a return code you just put "exit <return code>" at the point you want to exit AFTER you've issued the output line.

An example for our check of MS Exchange Webmail site is shown below. Note that this one is also a good example of things you can test for when you can't exactly test the page directly.

Code:

#!/bin/ksh
# -----------------------------------------------------------------------------#
#
# 23-Nov-2011 <MensaWater> Initial write adapted from earlier check_rom_dash.sh
#
# This script checks to be sure wget of of lgntopl.gif (Login Top Left picture
# from https://webmail.example.com/owa/14.1.1355.2/themes/resources has specific
# cksum value.  The value being checked against was obtained by doing a
# wget https://webmail.example.com/owa/14.1.355.2/themes/resources/lgntopl.gif
# to save a copy of the gif in /root then doing a cksum of that.
# The actual command line below will not save a copy of the gif but rather
# send it to stdout which will be piped to cksum.
#
# Note default certificate file is not good per Curl web site so downloaded
# newere cacert.pem from them and put in /usr/local/nagios/libexec.  This
# script relies on that newere certificate file.
#

# SYNTAX: check_webmail.sh <URL>
# example:
#  check_webmail.sh https://webmail.example.com/owa/14.1.355.2/themes/resources/lgntopl.gif
#
# -----------------------------------------------------------------------------#

# Key command string:
# wget --ca-certificate /usr/local/nagios/libexec/cacert.pem https://webmail.example.com/owa/14.1.355.2/themes/resources/lgntopl.gif -q -O - | cksum
#

# Verify the correct number of arguments were given at invocation.
#
if [ $# -ne 1 ]
then URL=undefined
else URL=$1
fi

#echo URL is $URL

# Define standard Nagios return codes
#
UNKNOWN_STATE=3
CRITICAL_STATE=2
WARNING_STATE=1
OK_STATE=0

# Define full path to commands - Nagios can't execute them otherwise
#
WGET=/usr/bin/wget
CKSUM=/usr/bin/cksum

# Set path to SSL certficate file for wget to recognize cert from site:
#
SSL_CERT_FILE=/usr/local/nagios/libexec/cacert.pem

# Main routine.  If the cksum has expected value then it is OK.  If it isn't
# for any reason (wrong cksum, site down, gif file unavailable) it is CRITICAL.
#
if  [ ! $URL = "undefined" ]
then
    CKSUMOUT="$($WGET --ca-certificate $SSL_CERT_FILE $URL -q -O - | $CKSUM)"
#    echo CKSUMOUT is $CKSUMOUT
    if [ "$CKSUMOUT" = "3459451327 4722" ]
    then echo "$URL appears to be ok."
          exit ${OK_STATE}
    else echo "$URL did not return expected response so appears to be down."
          exit ${CRITICAL_STATE}
    fi
else echo "URL not properly specified - Check usage."
    exit ${UNKNOWN_STATE}
fi

# If for any reason above routine doesn't return a value then default will be
# "UNKNOWN"
#
echo "Unable to determine state of URL."
exit ${UNKNOWN_STATE}


Cr45h 07-13-2012 05:35 AM

Thank u MensaWater for the answer.

I think I've forget something on the explanation...
The .html file I was talking above (that is the selenium testcase result) is a "simple" 2 columns table basic html page, with a series of "text - values" in each row and some informations around.

Something like this:

Code:

<title>Test suite results</title></head>
<body>
<h1>Test suite results </h1>

<table>
<tr>
<td>result:</td>
<td>failed</td>
</tr>
<tr>
<td>totalTime:</td>
<td>50</td>
</tr>
<tr>
<td>numTestTotal:</td>
<td>1</td>
</tr>
<tr>
<td>numTestPasses:</td>
<td>0</td>
</tr>
<tr>
<td>numTestFailures:</td>
<td>1</td>
</tr>
<tr>
<td>numCommandPasses:</td>
<td>0</td>
</tr>
<tr>
<td>numCommandFailures:</td>
<td>1</td>
</tr>
<tr>
<td>numCommandErrors:</td>
<td>1</td>
</tr>
<tr>
<td>Selenium Version:</td>
<td>2.24</td>
</tr>
<tr>
<td>Selenium Revision:</td>
<td>.1</td>
</tr>


Starting from this, I've to extract ,for example, "50" from this row "Total Time: 50".

This number has to be interpreted by a nagios check that have to be as arguments some threshold like "if from 25 to 75, it's OK, if greater than 90 it's warning, if greater than 120 it's critical".

So, maybe, for the last part, I can use the exit code you show me above (so I'll put these threshold inside the script for comparison), but, how can I extract that "50" and put it F.E. in another text file and then let nagios read this?


Hope to have clarified a bit more than before... :)

Thanks again.

MensaWater 07-13-2012 08:26 AM

You say "the row" but I don't see a SINGLE row with "Total Time: 50". What I do see similar to that is TWO rows:
Code:

<td>totalTime:</td>
<td>50</td>

Using wget with '-O -" flag you can output what it gets from a web page to standard output and use grep and other text processing tools. So to get the above two lines you could do:
Code:

wget -O - <URL> |grep -A 1 totalTime
Where you substitute the URL of the web page for <URL>

If all you need is the second line that contains the value you could use tail:
Code:

wget -O - <URL> |grep -A 1 totalTime |tail -1
To extract just the value you could use awk:
Code:

wget -O - <URL> |grep -A 1 totalTime |tail -1 |awk -F\> '{print $2}' |awk -F\< '{print $1}'
And of course those are just quick and dirty examples. You could avoid the use of tail altogether with awk and prettier awk might eliminate the other pipes. I'll leave that optimization to you though.

Cr45h 07-19-2012 03:43 AM

Wonderful!!!

In my case I'll use "cat" instead of "wget" cause .html file is the output format of selenium result, so it's a "fake" webpage... :)

BTW, I'll study a bit more awk later, now my priority is to have it running.


So now, I have the number. I wrote a simple script to compare it with 3 threshold (nagios-style); my last doubt is:

The threshold now are fixed into the script, but AFAIK some other nagios check take advantage of $ARGS$ variables to set warning and critical level to custom values and it's more easy to modify (instead to edit the script I can simply edit these values, save the nagios check and then it grabs these values as the new threshold, leaving the script unmodified).

I'll check better on nagios-plugin documentation, but on a first quick view there was a lot of code to write...

the idea is something like:

"./check_selenium -w 70 -c 85 < file_with_number_found.txt"

Where, of course, -w is the warning threshold and -c is the critical one.

Suggestions?

Thanks a lot for your help MensaWater :)


All times are GMT -5. The time now is 10:28 PM.