Skip to content

Instantly share code, notes, and snippets.

@ckxng
Created March 5, 2022 23:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ckxng/482d612b03a117920d7ec66eb9719382 to your computer and use it in GitHub Desktop.
Save ckxng/482d612b03a117920d7ec66eb9719382 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
#$Id$
use strict;
use warnings;
my $VERSION=1.000;
=head1 NAME
check_port80.pl
=head1 DESCRIPTION
Check HTTP on some sites and send an email alert if there is an error.
Runs forever until killed with 1 second spacing between each check.
=head1 VERSION
1.000
=head1 REQUIRES
=over 4
=item Sys::Hostname
=item bin C<syslog> in B<$ENV{PATH}>
=back
=head1 SITES
The following sites are checked:
=over 4
=item api.example.com
=item www.example.com
=item www.unt.edu
=item www.microsoft.com
=item www.google.com
=back
=cut
my @sites = qw(
api.example.com
www.example.com
www.unt.edu
www.microsoft.com
www.google.com
);
=head1 EMAILS
The following addresses are emailed on error:
=over 4
=item nfl-host@zerolag.com
=back
=cut
my @emails = qw'
cameron@example.com
james@example.com
';
use Sys::Hostname;
my $hostname = hostname;
my @commands = (
'nc -zvv -w 5 %s 80 2>&1',
"curl -vo /dev/null -m 5 -A 'check_port80.pl ($hostname)' http://%s",
);
while(1) { #### begin main loop
my %results = ();
my $errors = 0;
for my $s(@sites) {
for my $c(@commands) {
my $cmd = sprintf($c, $s);
$results{$cmd} = `$cmd 2>&1` . "[Return code ${^CHILD_ERROR_NATIVE}]\n";
$errors += ${^CHILD_ERROR_NATIVE};
}
}
if($errors) {
for my $to(@emails) {
open SENDMAIL, "|sendmail $to" or die "errors and cannot open sendmail\n";
print SENDMAIL "From: noc\@zerolag.com\n";
print SENDMAIL "Subject: Port 80 Errors on $hostname\n";
print SENDMAIL "To: $to\n";
print SENDMAIL "Content-type: text/plain\n\n";
print SENDMAIL "\n\n Limited distribution. Script is in test mode.\n\n";
print SENDMAIL "This script checks for errors on port 80 to multiple sites.\n";
print SENDMAIL "All available information is included below.\n\n";
print SENDMAIL "Command: $_\nOutput:\n$results{$_}\n\n" for keys %results;
close SENDMAIL;
}
}
sleep 1;
} #### end main loop
=head1 AUTHOR
Cameron King <http://cameronking.me>
=head1 COPYRIGHT
Copyright 2012 Cameron C. King. All rights reserved.
=head1 LICENSE
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY CAMERON C. KING ''AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL CAMERON C. KING OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
=cut
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment