[LON-CAPA-cvs] cvs: modules /raeburn monitor.pl

raeburn raeburn@source.lon-capa.org
Thu, 05 May 2011 14:55:33 -0000


This is a MIME encoded message

--raeburn1304607333
Content-Type: text/plain

raeburn		Thu May  5 14:55:33 2011 EDT

  Modified files:              
    /modules/raeburn	monitor.pl 
  Log:
  Script for robotic monotoring of LON-CAPA login, role selection, contents page
    retrieval and logout. 
  - Protocol (http or https) can be defined.
    - Currently set via optional sith argument 1 if SSL, 0 otherwise.
    - Some additional logging in case of failures.    
  
  
--raeburn1304607333
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20110505145533.txt"

Index: modules/raeburn/monitor.pl
diff -u modules/raeburn/monitor.pl:1.9 modules/raeburn/monitor.pl:1.10
--- modules/raeburn/monitor.pl:1.9	Mon May 12 19:40:55 2008
+++ modules/raeburn/monitor.pl	Thu May  5 14:55:33 2011
@@ -1,6 +1,6 @@
 #! /usr/bin/perl
 #
-# $Id: monitor.pl,v 1.9 2008/05/12 19:40:55 raeburn Exp $
+# $Id: monitor.pl,v 1.10 2011/05/05 14:55:33 raeburn Exp $
 #
 use strict;
 use lib qw(/usr/lib/perl5/site_perl/);
@@ -155,12 +155,14 @@
 #     login=https://login.msu.edu/AppLogin.Asp
 #     redirect=adm/roles?Token=
 #
+# An optional sixth parameter is included if the target sever is using SSL (i.e., URLs begin https).
+# This parameter is set to 1 if SSL in use, and is 0 or blank if not.
+#
 ########################################################
 # Configuration
 #
 my $monitordir = '/home/msuremot/monitoring';
-#my $path_to_java = '/usr/java/j2sdk1.4.2_05/bin/java';
-my $path_to_java = '/usr/java/j2re1.4.2_07/bin/java';
+my $path_to_java = '/usr/java/jre1.6.0_17/bin/java';
 my $contact_email = 'helpdesk@loncapa.org';
 my $ssoconfig = $monitordir.'/ssoconfig.txt';
 my $ssourls = $monitordir.'/ssourls.txt';
@@ -173,7 +175,7 @@
 my ($uname,$udom,$role,$upass);
 
 if (@ARGV < 3) {
-    print "Usage: $0 - encryptionkey hostname servernickname [loadbal] [sso]\n";
+    print "Usage: $0 - encryptionkey hostname servernickname [loadbal] [sso] [is ssl]\n";
     exit(0);
 }
 
@@ -182,6 +184,7 @@
 my $serveralias = $ARGV[2];
 my $loadbalance = $ARGV[3];
 my $sso = $ARGV[4];
+my $is_ssl = $ARGV[5];
 my (%ssoparam,%ssourl,%recipients,@formitems);
 if ($sso) {
     if (open (my $fh, "<$ssoconfig") ) {
@@ -208,6 +211,7 @@
             my ($name,$value) = split(/=/,$line);
             $ssourl{$name} = $value;
         }
+        close($fh);
     }
 }
 my $authkeyfile = $monitordir.'/'.$serveralias.'/autouser.dat';
@@ -293,9 +297,14 @@
     mkdir("$monitordir/$serveralias/alert",0755);
 }
 
-my $logfile = $monitordir.'/'.$serveralias.'/log'; 
+my $logfile = $monitordir.'/'.$serveralias.'/log';
+
+my $protocol = 'http';
+if ($is_ssl) {
+    $protocol = 'https';
+}
 
-($outcome,$loadbalserver) = &attempt_access($outcome,$server,$loginpage,$lonid,$role,\@formitems,\%formvalues,\%loadtimes,$monitordir,$path_to_java,$loncookie_file,$loadbalance,$sso,\%ssoparam,\%ssourl,$logfile);
+($outcome,$loadbalserver) = &attempt_access($outcome,$server,$loginpage,$lonid,$role,\@formitems,\%formvalues,\%loadtimes,$monitordir,$path_to_java,$loncookie_file,$loadbalance,$sso,\%ssoparam,\%ssourl,$logfile,$protocol);
 my %needmail = &alertstatus($outcome,$server,$serveralias,\@failures,$logfile,$monitordir,\%recipients,\%cleared);
 if (keys(%needmail) > 0) {
     my $mailresult = &mailalert($server,$outcome,$contact_email,\%needmail,'fail');
@@ -329,8 +338,8 @@
 sub attempt_access {
     my ($outcome,$server,$loginpage,$lonid,$role,$formitems,$formvalues,
         $loadtimes,$monitordir,$path_to_java,$loncookie_file,$loadbalance,
-        $sso,$ssoparamref,$ssourlref,$logfile) = @_;
-    ($outcome,$loginpage) = &get_loginpage($server,$ua,$loncookie_file,$sso,$logfile);
+        $sso,$ssoparamref,$ssourlref,$logfile,$protocol) = @_;
+    ($outcome,$loginpage) = &get_loginpage($server,$ua,$loncookie_file,$sso,$logfile,$protocol);
     my $loadbalserver;
     if ($outcome eq 'ok') {
         $ua->timeout(30);
@@ -338,25 +347,25 @@
             $outcome = &parse_loginpage($loginpage,$formitems,$formvalues,$sso);
             if ($outcome eq 'ok') {
                 if ($sso) {
-                    ($outcome,$lonid) = &ssologmein(\$server,$uname,$udom,$upass,$ua,$loncookie_file,$formvalues,$loadtimes,$monitordir,$loadbalance,$ssoparamref,$ssourlref);
+                    ($outcome,$lonid) = &ssologmein(\$server,$uname,$udom,$upass,$ua,$loncookie_file,$formvalues,$loadtimes,$monitordir,$loadbalance,$ssoparamref,$ssourlref,$logfile);
                 } else {
-                    ($outcome,$lonid) = &logmein(\$server,$uname,$udom,$upass,$ua,$loncookie_file,$formvalues,$loadtimes,$monitordir,$path_to_java,$loadbalance);
+                    ($outcome,$lonid) = &logmein(\$server,$uname,$udom,$upass,$ua,$loncookie_file,$formvalues,$loadtimes,$monitordir,$path_to_java,$loadbalance,$protocol);
                 }
                 if ($outcome eq 'ok')  {
                     if ($loadbalance) {
-                        ($loadbalserver) = ($server =~ /^(s\d+)\./);
+                        ($loadbalserver) = ($server =~ /^([^.]+)\./);
                     }
                     if ($lonid) {
                         &setcookie($loncookie_file,$lonid,$server);
-                        $outcome = &pickrole($server,$loncookie_file,$ua,$role,$loadtimes);
+                        $outcome = &pickrole($server,$loncookie_file,$ua,$role,$loadtimes,$protocol);
                         if ($outcome eq 'ok') {
                             $ua->timeout(120);
-                            my $outcome = &loadnavmap($server,$loncookie_file,$ua,$loadtimes);
+                            my $outcome = &loadnavmap($server,$loncookie_file,$ua,$loadtimes,$protocol);
                             $ua->timeout(30);
                             if ($outcome eq 'ok') { 
-                                $outcome = &logout($server,$loncookie_file,$ua);
+                                $outcome = &logout($server,$loncookie_file,$ua,$protocol);
                             } else {
-                                &logout($server,$loncookie_file,$ua);
+                                &logout($server,$loncookie_file,$ua,$protocol);
                             }
                             $loncookie_file->clear();
                         }
@@ -369,9 +378,9 @@
 }
 
 sub get_loginpage {
-    my ($server,$ua,$loncookie_file,$sso,$logfile) = @_;
+    my ($server,$ua,$loncookie_file,$sso,$logfile,$protocol) = @_;
     my ($outcome,$loginpage);
-    my $URL = 'http://'.$server.'/';
+    my $URL = $protocol.'://'.$server.'/';
     if ($sso) {
         $URL .= 'adm/roles';
     } else {
@@ -400,7 +409,7 @@
     my $outcome = 0;
     my $paramcount = 0;
     if ($sso) {
-        if ($loginpage =~ m/<input\sname="([^"]+)"\stype="password"\svalue=""\ssize="\d+"\smaxlength="\d+"\s\/>/) {
+        if ($loginpage =~ m/<input\sname="([^"]+)"\sid="pswd"\stype="password"\svalue=""\ssize="\d+"\smaxlength="\d+"\s\/>/) {
             $formvalues->{'password'} = $1;
             $paramcount ++;
         }
@@ -429,7 +438,7 @@
 }
 
 sub logmein {
-    my ($serverref,$uname,$udom,$upass,$ua,$loncookie_file,$formvalues,$loadtimes,$monitordir,$path_to_java,$loadbalance) = @_;
+    my ($serverref,$uname,$udom,$upass,$ua,$loncookie_file,$formvalues,$loadtimes,$monitordir,$path_to_java,$loadbalance,$protocol) = @_;
     my ($outcome,$lonid);
     my $upass0 = substr($upass,0,15);
     my $upass1 = substr($upass,15,15);
@@ -445,7 +454,7 @@
     open(PIPE,"-|") || exec "$path_to_java -classpath $classpath org.mozilla.javascript.tools.shell.Main $londesfile $$formvalues{uextkey} $$formvalues{lextkey} $upass2";
     my $cryppass2 = <PIPE>;
     close PIPE;
-    my $URL = 'http://'.$$serverref.'/adm/authenticate';
+    my $URL = $protocol.'://'.$$serverref.'/adm/authenticate';
     $ua->cookie_jar( $loncookie_file );
     my $req = POST $URL,
       Content_Type => 'application/x-www-form-urlencoded',
@@ -472,7 +481,7 @@
                                                             $uname,$udom);
             if ($switchurl) {
                 ($outcome,$lonid) =
-                    &switchserver($switchurl,$ua,$loadtimes,$loncookie_file);
+                    &switchserver($switchurl,$ua,$loadtimes,$loncookie_file,$start);
             } else {
                 $outcome = 'nologin';
             }
@@ -496,7 +505,7 @@
 }
 
 sub ssologmein {
-    my ($serverref,$uname,$udom,$upass,$ua,$loncookie_file,$formvalues,$loadtimes,$monitordir,$loadbalance,$ssoparamref,$ssourlref) = @_;
+    my ($serverref,$uname,$udom,$upass,$ua,$loncookie_file,$formvalues,$loadtimes,$monitordir,$loadbalance,$ssoparamref,$ssourlref,$logfile) = @_;
     my ($outcome,$lonid);
     my $URL = $ssourlref->{'login'};
     $ua->cookie_jar( $loncookie_file );
@@ -546,12 +555,12 @@
     if ($res->is_redirect()) {
         my $newurl = $res->header( "location" );
         my $redirpath = $ssourlref->{'redirect'};
-        if ($newurl =~ /^http:\/\/\Q$$serverref\E\/\Q$redirpath\E/) {
+        if ($newurl =~ /^https?\:\/\/\Q$$serverref\E\/\Q$redirpath\E/) {
             my $end = [gettimeofday];
             $loadtimes{sso} = tv_interval $start, $end;
             $loadtimes{sso} *= 1000;
             ($outcome,$lonid) = 
-                &welcomepage($ua,$newurl,$serverref,$loncookie_file,$uname,$udom,$loadtimes,$monitordir);
+                &welcomepage($ua,$newurl,$serverref,$loncookie_file,$uname,$udom,$loadtimes,$monitordir,$logfile);
         } else {
             $outcome = 'invalidcookie';
         }
@@ -562,7 +571,7 @@
 }
 
 sub welcomepage {
-    my ($ua,$newurl,$serverref,$loncookie_file,$uname,$udom,$loadtimes,$monitordir) = @_;
+    my ($ua,$newurl,$serverref,$loncookie_file,$uname,$udom,$loadtimes,$monitordir,$logfile) = @_;
     my ($outcome,$lonid);
     my $start = [gettimeofday];
     my $request = GET $newurl;
@@ -574,9 +583,12 @@
                                                             $uname,$udom);
             if ($switchurl) {
                 ($outcome,$lonid) = 
-                    &switchserver($switchurl,$ua,$loadtimes,$loncookie_file);
+                    &switchserver($switchurl,$ua,$loadtimes,$loncookie_file,$start);
             } else {
                 $outcome = 'nologin';
+                open (my $logfh,">>$logfile");
+                print $logfh localtime(time)." - nologin parse switchpage failed parsing: ".$response->content."\n";
+                close($logfh);
             }
         } else {
             $loncookie_file->extract_cookies($response);
@@ -584,38 +596,40 @@
             if ($cookie =~ m/lonID=(\w+);/) {
                 $lonid = $1;
                 $outcome = 'ok';
+                my $end = [gettimeofday];
+                $loadtimes{login} = tv_interval $start, $end;
+                $loadtimes{login} *= 1000;
             } else {
                 $outcome = 'invalidcookie';
             }
-        } 
+        }
     } else {
         $outcome = 'nologin';
+        open (my $logfh,">>$logfile");
+        print $logfh localtime(time)." - nologin to ".$newurl.", status_line: ".$response->status_line."\n";
+        close($logfh);
     }
-    my $end = [gettimeofday];
-    $loadtimes{login} = tv_interval $start, $end;
-    $loadtimes{login} *= 1000;
     return ($outcome,$lonid);
 }
 
 sub parse_switchpage {
     my ($content,$uname,$udom) = @_;
-    if ($content =~ m{<meta\shttp\-equiv="Refresh"\scontent="0\.5;\surl=(http://)([^/]+)(/adm/login\?domain=\Q$udom\E&amp;username=\Q$uname\E&amp;token=\w+)" />}s ) {
+    if ($content =~ m{<meta\shttp\-equiv="Refresh"\scontent="0\.5;\surl=(https?)\://([^/]+)(/adm/login\?domain=\Q$udom\E&amp;username=\Q$uname\E&amp;token=\w+)" />}s ) {
          my $protocol = $1;
          my $server= $2;
          my $path = $3;
          $path =~ s/\&amp;/\&/g;
-         return ($protocol.$server.$path, $server);
+         return ($protocol.'://'.$server.$path, $server);
     }
     return;
 }
 
 sub switchserver {
-    my ($url,$ua,$loadtimes,$loncookie_file) = @_;
+    my ($url,$ua,$loadtimes,$loncookie_file,$start) = @_;
     my ($outcome,$lonid);
     my $request = GET $url;
     $loncookie_file->add_cookie_header($request);
     my $res = $ua->request($request);
-    my $start = [gettimeofday];
     if ($res->is_success) {
         $loncookie_file->extract_cookies($res);
         my $cookie = $loncookie_file->as_string;
@@ -635,9 +649,9 @@
 }
 
 sub pickrole {
-    my ($server,$loncookie_file,$ua,$role,$loadtimes) = @_;
+    my ($server,$loncookie_file,$ua,$role,$loadtimes,$protocol) = @_;
     my ($start,$end,$outcome);
-    my $URL = 'http://'.$server.'/adm/roles';
+    my $URL = $protocol.'://'.$server.'/adm/roles';
     my $request = POST $URL,
       Content      =>
       [
@@ -659,7 +673,7 @@
         $loadtimes{initialize} *= 1000; 
     } else {
         open (my $tempfh,">>/tmp/rolesfailed.txt");
-        print $tempfh "$server ".localtime(time)." $response->status_line\n";
+        print $tempfh "$server ".localtime(time).' '.$response->status_line." for $URL\n";
         close($tempfh);
         $outcome = 'rolesfailed';
     }
@@ -667,9 +681,9 @@
 }
 
 sub loadnavmap {
-    my ($server,$loncookie_file,$ua,$loadtimes) = @_;
+    my ($server,$loncookie_file,$ua,$loadtimes,$protocol) = @_;
     my $outcome;
-    my $URL = 'http://'.$server.'/adm/navmaps';
+    my $URL = $protocol.'://'.$server.'/adm/navmaps';
     my $request = new HTTP::Request;
     $request =  GET $URL;
     $loncookie_file->add_cookie_header($request);
@@ -688,9 +702,9 @@
 }
 
 sub logout {
-    my ($server,$loncookie_file,$ua) = @_;
+    my ($server,$loncookie_file,$ua,$protocol) = @_;
     my $outcome;
-    my $URL = 'http://'.$server.'/adm/logout';
+    my $URL = $protocol.'://'.$server.'/adm/logout';
     my $request = new HTTP::Request;
     $request =  GET $URL;
     $loncookie_file->add_cookie_header($request);

--raeburn1304607333--