Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 9 Dec 2005 10:40:12 +1100
From:      "Murray Taylor" <MTaylor@bytecraft.com.au>
To:        "Beecher Rintoul" <akbeech@gmail.com>, "Martin Cracauer" <cracauer@cons.org>
Cc:        Dan Nelson <dnelson@allantgroup.com>, freebsd-questions@freebsd.org
Subject:   RE: simple shell script
Message-ID:  <04E232FDCD9FBE43857F7066CAD3C0F105438E@svmailmel.bytecraft.internal>

next in thread | raw e-mail | index | archive | help
Beech,

For what it is worth, and if you are interested in=20
another method, here is a ruby script that I found=20
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=20
then generates a go/nogo email and exits.

This was built to monitor a web site at an ISP that was=20
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
=20 puts "usage: check_url.rb [-v] [-m emailaddr] <url>"
=20 exit 1
end

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

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

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

=09	when '-m'
=09		to =3D arg
=09end
end

# test again, did we clobber all the args ?

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

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

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

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

#{url} is feeling OK right now.

Diagnosis: Site up
MAIL

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

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

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

=09	Net::SMTP.start(smtp_host) do |smtp|
=20 			smtp.send_mail(mail, from, to)
=09	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@example.com
http://site.example.com
----------------------8<----------------------------

cheers
mjt


> -----Original Message-----
> From: owner-freebsd-questions@freebsd.org=20
> [mailto:owner-freebsd-questions@freebsd.org] On Behalf Of=20
> Beecher Rintoul
> Sent: Thursday, December 08, 2005 4:17 PM
> To: Martin Cracauer
> Cc: Dan Nelson; freebsd-questions@freebsd.org
> Subject: Re: simple shell script
>=20
> 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=20
> launch a program
> > > > (in this case lynx), wait 60 seconds and then kill it.=20
> I can get it
> > > > to launch the program, but it seems to ignore anything=20
> after that. I
> > > > am not a programmer so does anyone have a suggestion on=20
> 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=3D$!
> > > # 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
>=20
> Thanks to you both, I missed the background option. Actually=20
> it just needs to=20
> run in the background to log some hits on a free webserver=20
> that I use for=20
> testing. I forget and they will cancel my account if there's=20
> no traffic for a=20
> month. The timeout also looks interesting and I'll look into it.
>=20
> Beech
>=20
> --=20
>=20
> --------------------------------------------------------------
> -------------------------
> Beech Rintoul - System Administrator - akbeech@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
> --------------------------------------------------------------
> -------------------------
>=20
>=20
>=20
>=20
>=20
>=20
>=20
>=20
>=20
>=20
>=20
>=20
> ***This Email has been scanned for Viruses by MailMarshal.***
>=20
---------------------------------------------------------------
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.=20

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.***



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?04E232FDCD9FBE43857F7066CAD3C0F105438E>