LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices

Reply
 
Search this Thread
Old 07-12-2012, 11:03 AM   #1
Cr45h
LQ Newbie
 
Registered: Sep 2008
Location: UK
Distribution: Debian
Posts: 10

Rep: Reputation: 0
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.....)

Thanks in advance...
 
Old 07-12-2012, 12:48 PM   #2
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,950
Blog Entries: 5

Rep: Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755
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}
 
Old 07-13-2012, 05:35 AM   #3
Cr45h
LQ Newbie
 
Registered: Sep 2008
Location: UK
Distribution: Debian
Posts: 10

Original Poster
Rep: Reputation: 0
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.
 
Old 07-13-2012, 08:26 AM   #4
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,950
Blog Entries: 5

Rep: Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755
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.

Last edited by MensaWater; 07-13-2012 at 09:21 AM.
 
Old 07-19-2012, 03:43 AM   #5
Cr45h
LQ Newbie
 
Registered: Sep 2008
Location: UK
Distribution: Debian
Posts: 10

Original Poster
Rep: Reputation: 0
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
 
  


Reply

Tags
nagios


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ould not create external command file '/usr/local/nagios/var/rw/nagios.cmd gerard.zapata Linux - Newbie 2 09-14-2012 01:57 PM
How to change Threshold time in nagios services prak86 Linux - Newbie 5 05-03-2011 02:43 AM
nagios check resource djackbloodshed Ubuntu 2 03-30-2011 10:26 AM
how to check if the content of a file is "1"? fbs777 Programming 11 02-15-2010 07:26 AM
How to check initrd content elmu Linux - Newbie 1 11-08-2005 12:06 PM


All times are GMT -5. The time now is 12:34 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration