Nagios - Selenium: check for file content and compares with specified threshold
Linux - ServerThis forum is for the discussion of Linux Software used in a server related context.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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..
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.....)
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}
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.
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?
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:
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.
Last edited by MensaWater; 07-13-2012 at 09:21 AM.
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...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.