simple shell script

Murray Taylor MTaylor at bytecraft.com.au
Thu Dec 8 15:42:55 PST 2005


Beech,

For what it is worth, and if you are interested in 
another method, here is a ruby script that I found 
somewhere and tweaked up a bit more.
(Probably not the best ruby, but it was my learning effort)

It is called by cron, and does a website 'connectivity'
test, ie it attempts to get the specified page. The script 
then generates a go/nogo email and exits.

This was built to monitor a web site at an ISP that was 
having serious brain damage with managing the DNS records
(they deleted the active list...;-( )
And it is still running as a health check to this day...

Run it _without_ the -v option and it will only email
when the remote site has a problem...



The ruby script
----------------------8<----------------------------
#!/usr/local/bin/ruby
require 'getoptlong'
require 'open-uri'
require 'net/smtp'

if ARGV.empty? then
  puts "usage: check_url.rb [-v] [-m emailaddr] <url>"
  exit 1
end

# specify options
opts = GetoptLong.new(
	[ "-v", GetoptLong::NO_ARGUMENT ],
	[ "-m", GetoptLong::REQUIRED_ARGUMENT ]
)

smtp_host = "smtp.example.com"
from = "root at example.com"
url = ""
message = ""
flag = 0
verbose = 0
# default this
to = "operator at example.com"

# process args
opts.each do |opt, arg|
	case opt
		when '-v'
			verbose = 1

		when '-m'
			to = arg
	end
end

# test again, did we clobber all the args ?

if ARGV.empty? then
  puts "usage: check_url.rb [-v] [-m emailaddr] <url>"
  exit 1
end

# get the url in question
url = ARGV[0]
subject="Web monitor alert re #{url}"

# go and test the site
begin
  page = open(url).read
  if page =~ /Error|Exception/
    message = "#{url} has returned either Error or Exception"
	flag = 1
  end
rescue
  message = "#{url} Unavailable\n - #{$!.message}"
  flag = 2
end

case flag
	when 0
		if verbose
			mail = <<MAIL
Subject: #{subject}

#{url} is feeling OK right now.

Diagnosis: Site up
MAIL

			Net::SMTP.start(smtp_host) do |smtp|
  				smtp.send_mail(mail, from, to)
			end
		end
	else
		mail = <<MAIL
Subject: #{subject}

Sadly, #{url} isn't feeling well right now.

Diagnosis: #{message}
Page     : #{page}
MAIL

		Net::SMTP.start(smtp_host) do |smtp|
  			smtp.send_mail(mail, from, to)
		end
end


----------------------8<----------------------------
end of ruby script

and the cron lines (modify to suit your timing)
This runs every 3 hours as is
----------------------8<----------------------------
# min hour date month day cmd
0 */3 * * * $HOME/bin/checkurl.rb -v -m user at example.com
http://site.example.com
----------------------8<----------------------------

cheers
mjt


> -----Original Message-----
> From: owner-freebsd-questions at freebsd.org 
> [mailto:owner-freebsd-questions at freebsd.org] On Behalf Of 
> Beecher Rintoul
> Sent: Thursday, December 08, 2005 4:17 PM
> To: Martin Cracauer
> Cc: Dan Nelson; freebsd-questions at freebsd.org
> Subject: Re: simple shell script
> 
> On Wednesday 07 December 2005 07:33 pm, Martin Cracauer wrote:
> > Dan Nelson wrote on Wed, Dec 07, 2005 at 09:52:55PM -0600:
> > > In the last episode (Dec 07), Beecher Rintoul said:
> > > > I'm trying to write a simple shell script that will 
> launch a program
> > > > (in this case lynx), wait 60 seconds and then kill it. 
> I can get it
> > > > to launch the program, but it seems to ignore anything 
> after that. I
> > > > am not a programmer so does anyone have a suggestion on 
> this script?
> > > > Any help would be appreciated.
> > >
> > > You need to background it so your script keeps running:
> > >
> > > #! /bin/sh
> > > # Launch program
> > > lynx &
> > > # Store its processid for later
> > > pid=$!
> > > # 60 seconds
> > > sleep 60
> > > # Kill backgrounded process
> > > kill -9 $pid
> >
> > This is probably not what Beecher wants, he/she probably needs the
> > lynx in the foreground.
> >
> > In a pure shell script that is difficult because you cannot get the
> > pid of a foreground process without reverting to `ps` which is not
> > sportish.
> >
> > It's better to use a dedicated timeoput mechanism, e.g.:
> > http://cracauer-forum.cons.org/forum/cratimeout.html
> >
> > Then you can do `cratimeout 60000 lynx`
> >
> > Martin
> 
> Thanks to you both, I missed the background option. Actually 
> it just needs to 
> run in the background to log some hits on a free webserver 
> that I use for 
> testing. I forget and they will cancel my account if there's 
> no traffic for a 
> month. The timeout also looks interesting and I'll look into it.
> 
> Beech
> 
> -- 
> 
> --------------------------------------------------------------
> -------------------------
> Beech Rintoul - System Administrator - akbeech at gmail.com
> /"\   ASCII Ribbon Campaign  | NorthWind Communications
> \ / - NO HTML/RTF in e-mail  | 201 East 9th Avenue Ste.310
>  X  - NO Word docs in e-mail | Anchorage, AK 99501
> / \  - Please visit Alaska Paradise - http://akparadise.byethost33.com
> --------------------------------------------------------------
> -------------------------
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> ***This Email has been scanned for Viruses by MailMarshal.***
> 
---------------------------------------------------------------
The information transmitted in this e-mail is for the exclusive
use of the intended addressee and may contain confidential
and/or privileged material. Any review, re-transmission,
dissemination or other use of it, or the taking of any action
in reliance upon this information by persons and/or entities
other than the intended recipient is prohibited. If you
received this in error, please inform the sender and/or
addressee immediately and delete the material. 

E-mails may not be secure, may contain computer viruses and
may be corrupted in transmission. Please carefully check this
e-mail (and any attachment) accordingly. No warranties are
given and no liability is accepted for any loss or damage
caused by such matters.
---------------------------------------------------------------

***This Email has been scanned for Viruses by MailMarshal.***


More information about the freebsd-questions mailing list