Page 1 of 1

Search for multiple text strings on web page

Posted: Thu Oct 23, 2014 9:53 am
by rickwilson7425
Hi,

I am trying to figure out how to check a web page for the existence of multiple text string on one page. In particular I need to see if 3 different strings exist on a page and if any of them are missing to throw an alert.

I have tried check_http with the -s option listing all 3 of the strings as separate -s entries but it doesn't appear that this works to check for all 3 combined.

Here is what I have:

check_http -H my.website.com.com -p 13001 -u /MonitorStartupServlet/MonitorStartup -s EmailMonitor -s WswMonitor -s Notifier --timeout=30

It seems to check and if only one of the strings is there then it returns an OK.

Re: Search for multiple text strings on web page

Posted: Thu Oct 23, 2014 12:41 pm
by abrist
The optarg functions in the plugins project will only process the last arg value for any redundant args. So you cannot use multiple switches (like -r or -R). Additionally, I don't think you can really achieve a full "AND" statement as check_http does not have look ahead. You can though, very easily use the multiline switch (-l) and wildcards (.*) to achieve something close.

For example, nagios-plugins.org has a bunch of text. We want to test for the existence of the following 3 strings: enhancements, check_ntp, and reverts. These strings were chosen as they exist on separate lines. As long as you know the order of these words appearing, you can test as follows:

Code: Select all

./check_http -H nagios-plugins.org -u /nagios-plugins-2-0-3-released/  -l -R "enhancements.*check_ntp.*reverts"
It returns:

Code: Select all

HTTP OK: HTTP/1.1 200 OK - 56865 bytes in 1.517 second response time |time=1.516833s;;;0.000000 size=56865B;;;0
Now lets change the order of the strings:

Code: Select all

./check_http -H nagios-plugins.org -u /nagios-plugins-2-0-3-released/ -l -R "enhancements.*reverts.*check_ntp"
Returns:

Code: Select all

HTTP CRITICAL: HTTP/1.1 200 OK - pattern not found - 22889 bytes in 1.397 second response time |time=1.396615s;;;0.000000 size=22889B;;;0
So if you know the order of appearance, you could use a regex wildcard string match. If you are unsure of the order, you could also use the logical "OR" ( | ) operator:

Code: Select all

./check_http -H nagios-plugins.org -u /nagios-plugins-2-0-3-released/ -l -R "enhancements.*reverts.*check_ntp|enhancements.*check_ntp.*reverts"
Returns:

Code: Select all

HTTP OK: HTTP/1.1 200 OK - 22889 bytes in 1.571 second response time |time=1.570581s;;;0.000000 size=22889B;;;0
But now we are just getting silly. Enjoy the rabbit hole.

EDIT: I probably should have used your example. So with the above syntax, your check would look like (assuming the order is correct):

Code: Select all

check_http -H my.website.com.com -p 13001 -u /MonitorStartupServlet/MonitorStartup -r "EmailMonitor.*WswMonitor.*Notifier" --timeout=30

Re: Search for multiple text strings on web page

Posted: Thu Oct 23, 2014 1:18 pm
by rickwilson7425
That seems to work.

Pardon my ignorance but why the " * " in front of the second and third strings?

Re: Search for multiple text strings on web page

Posted: Thu Oct 23, 2014 1:32 pm
by abrist
rickwilson7425 wrote: Pardon my ignorance but why the " * " in front of the second and third strings?
No problem. I think wiki explains it well:
Regular expressions

In regular expressions, the period (".", also called "dot") is the wildcard character for a single character. Combined with the asterisk operator (.*) it will match any number of characters.
From: http://en.wikipedia.org/wiki/Wildcard_c ... xpressions
Combined with the period, it basically allows us to use the wildcard for *any* number of characters.
Regex is pretty neat and powerful. Definitely worth learning if you are in a technical field - especially those that require shell parsing or programming in general.
Hope this helps, cheers!

Re: Search for multiple text strings on web page

Posted: Thu Oct 23, 2014 1:42 pm
by rickwilson7425
Thanks a lot Andy.

Please feel free to close this thread.

Re: Search for multiple text strings on web page

Posted: Thu Oct 23, 2014 2:19 pm
by abrist
Will do! Have a good weekend!