# Copyright (c) 2001-2009 bivio Software, Inc. All rights reserved. # # Visit http://www.bivio.biz for more info. # # This library is free software; you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as # published by the Free Software Foundation; either version 2.1 of the # License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; If not, you may get a copy from: # http://www.opensource.org/licenses/lgpl-license.html # # $Id: HTTPPing.pm,v 2.5 2009/01/28 20:59:09 nagler Exp $ package Bivio::Util::HTTPPing; use strict; use Bivio::Base 'Bivio.ShellUtil'; use Bivio::Ext::LWPUserAgent; use Bivio::IO::Config; use Bivio::IO::File; use Bivio::IO::Trace; use HTTP::Headers (); use HTTP::Request (); our($VERSION) = sprintf('%d.%02d', q$Revision: 2.5 $ =~ /\d+/g); our($_TRACE); Bivio::IO::Config->register(my $_CFG = { host_map => {}, status_file => '/var/tmp/httpd.status', loadavg_file => '/proc/loadavg', }); sub USAGE { # : string # Returns usage. return <<'EOF'; usage: b-http-ping [options] command [args...] commands: page url ... -- request url(s) process_status -- check load avg db_status -- check for too many INSERT waiting EOF } sub db_status { # (self) : undef # Checks for too many INSERT waiting. my($self) = @_; my $count = grep(/INSERT waiting/, `ps ax`); return $count ? "$count process(es) waiting on db insert\n" : (); } sub handle_config { # (proto, hash) : undef # host_map : hash_ref # # Name mapping for paged hosts. # # status_file : string [/var/tmp/httpd.status] # # Location of process_status cache. my(undef, $cfg) = @_; $_CFG = $cfg; return; } sub page { # (self, array) : string_ref # Request I and report any problems. # Truncate data returned from the server at 512 bytes. my($self, @pages) = @_; $self->initialize_ui; my($user_agent) = b_use('Ext.LWPUserAgent')->new(1), my($status) = ''; foreach my $page (@pages) { my($host) = $page =~ m!^\w+://([^:/]+)!; $host = $_CFG->{host_map}->{$host} if exists($_CFG->{host_map}->{$host}); _trace('paging ', $host) if $_TRACE; my($reply) = $user_agent->request(HTTP::Request->new('GET', $page, HTTP::Headers->new(Host => $host))); next if $reply->is_success; $status .= $host . ': ' . $reply->status_line . "\n"; } return $status; } sub process_status { # (self) : string # Returns significant load average changes. my($new) = int( (split(' ', ${Bivio::IO::File->read($_CFG->{loadavg_file})}))[2]); my($old) = Bivio::Die->eval(sub { ${Bivio::IO::File->read($_CFG->{status_file}, $new)} }) || 0; my($res) = $new != $old && ($new > 3 || $old > 3) ? "Load average $new\n" : ''; Bivio::IO::File->write($_CFG->{status_file}, $new); return $res; } 1;