Igor Gariev (gariev) wrote in changelog,
Igor Gariev
gariev
changelog

[livejournal] r19976: LJSUP-4848: add tool to get web node upt...

Committer: gariev
LJSUP-4848: add tool to get web node uptime
U   trunk/bin/web_servers_uptime.pl
Modified: trunk/bin/web_servers_uptime.pl
===================================================================
--- trunk/bin/web_servers_uptime.pl	2011-09-07 03:13:26 UTC (rev 19975)
+++ trunk/bin/web_servers_uptime.pl	2011-09-07 03:35:15 UTC (rev 19976)
@@ -1,29 +1,42 @@
 #!/usr/bin/perl
 use strict;
-use LWP::Simple qw//;
+use LWP::UserAgent;
 use HTTP::Request;
-use LWP::UserAgent;
-    
-    my $pool_file = $ARGV[0] || 'etc/pool_lj_web_servers.txt';
-    print "Get uptime of servers from pool $pool_file\n";
+use Getopt::Long;
+use lib "$ENV{LJHOME}/cgi-bin";
+use LJ::AdminTools qw(get_hosts_from_varnish_file);
 
-    #
-    local *FILE;
-    open FILE, "<", $pool_file
-        or die "Can't open file $pool_file: $!";
-    my @servers = 
-        grep { not /^\s*#/ and not /^\s*$/ and length }
-        <FILE>;
-    close FILE;
-    
-    #
-    print "Server -> Uptime\n";
-    foreach my $server (@servers){
-        my $uptime = get_uptime($server);
+my $usage = <<"USAGE";
+$0 - script to check uptime of servers
+Usage:
+    $0 [options] [list of servers]
+Options:
+    --vcl=file  Load list of servers from given varnish config file
+                By default, "etc/production.vcl" is used
+    --help      Show this help and exit
+USAGE
+
+my ($need_help, $vcl_file);
+GetOptions(
+    "vcl=s" =>  \$vcl_file,
+    "help"  =>  \$need_help,
+) or die $usage;
+die $usage if $need_help;
+
+my @servers = (@ARGV) 
+    ? @ARGV 
+    : get_hosts_from_varnish_file( $vcl_file || "$ENV{LJHOME}/etc/production.vcl");
+
+print "Server -> Uptime\n";
+foreach my $server (@servers){
+    chomp $server;
+    print "$server -> ";
+    if (my $uptime = get_uptime($server)) {
         my $str = localtime($uptime);
-        chomp $server;
-        print "$server -> $uptime ($str)\n";
+        print "$uptime ($str)";
     }
+    print "\n";
+}
 
 sub get_uptime {
     my $server = shift;
@@ -32,7 +45,12 @@
     $request->header(Host => 'www.livejournal.com');
     my $ua = LWP::UserAgent->new;
     my $response = $ua->request($request);
-    my $content = $response->content;
-    return int $content;
+    if ($response->is_success) {
+        return int $response->content;
+    } else {
+        warn "$server: ", $response->status_line;
+        return;
+    }
+}
 
-}
+

Tags: gariev, livejournal, pl
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments