File:  [LON-CAPA] / loncom / cgi / userstatus.pl
Revision 1.10: download - view: text, annotated - select for diffs
Wed Jan 14 01:42:09 2004 UTC (20 years, 3 months ago) by albertel
Branches: MAIN
CVS tags: version_1_2_0, version_1_1_99_5, version_1_1_99_4, version_1_1_99_3, version_1_1_99_2, version_1_1_99_1, version_1_1_99_0, HEAD
- subjectively better

    1: #!/usr/bin/perl
    2: $|=1;
    3: # User Status
    4: # $Id: userstatus.pl,v 1.10 2004/01/14 01:42:09 albertel Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: 
   29: 
   30: use strict;
   31: use lib '/home/httpd/lib/perl/';
   32: use LONCAPA::Configuration;
   33: 
   34: use HTTP::Headers;
   35: use IO::File;
   36: 
   37: 
   38: my %usercount;
   39: my @actl=('Active','Moderately Active','Inactive');
   40: 
   41:  
   42: print "Content-type: text/html\n\n";
   43:       
   44: # -------------------- Read loncapa.conf (and by default, loncapa_apache.conf).
   45: &main();
   46: 
   47: sub analyze_time {
   48:     my ($since)=@_;
   49:     my $color="#000000";
   50:     my $userclass=$actl[0];
   51:     if ($since>300) { $color="#222222"; }
   52:     if ($since>600) { $color="#444444"; }
   53:     if ($since>1800) { $color="#666666"; $userclass=$actl[1]; }
   54:     if ($since>7200) { $color="#888888"; }
   55:     if ($since>21600) { $color="#AAAAAA"; $userclass=$actl[2]; }
   56:     return ($color,$userclass);
   57: }
   58: 
   59: sub add_count {
   60:     my ($cat,$scope,$class)=@_;
   61:     if (!defined($usercount{$cat})) {
   62: 	$usercount{$cat}={};
   63:     }
   64:     if (!defined($usercount{$cat}{$scope})) {
   65: 	$usercount{$cat}{$scope}={};
   66:     }
   67:     $usercount{$cat}{$scope}{$class}++;
   68: }
   69: 
   70: sub main {
   71:     my $perlvar=LONCAPA::Configuration::read_conf('loncapa.conf');
   72:     delete $$perlvar{'lonReceipt'}; # remove since sensitive and not needed
   73:     delete $$perlvar{'lonSqlAccess'}; # remove since sensitive and not needed
   74: 
   75:     my $oneline=($ENV{'QUERY_STRING'} eq 'simple');
   76:     my $justsummary=($ENV{'QUERY_STRING'} eq 'summary');
   77:     unless ($oneline) { print "<html><body bgcolor=#FFFFFF>\n<h1>User Status ".localtime()."</h1>"; }
   78: 
   79:     opendir(DIR,$$perlvar{'lonIDsDir'});
   80:     my @allfiles=(sort(readdir(DIR)));
   81:     foreach my $filename (@allfiles) {
   82: 	if ($filename=~/^\./) { next; }
   83: 	my ($dev,$ino,$mode,$nlink,
   84: 	    $uid,$gid,$rdev,$size,
   85: 	    $atime,$mtime,$ctime,
   86: 	    $blksize,$blocks)=stat($$perlvar{'lonIDsDir'}.'/'.$filename);
   87: 	my $now=time;
   88: 	my $since=$now-$mtime;
   89: 	my $sinceacc=$now-$atime;
   90: 	unless ($oneline || $justsummary) { print ("\n\n<hr />"); }
   91: 	my %userinfo;
   92: 	my $fh=IO::File->new($$perlvar{'lonIDsDir'}.'/'.$filename);
   93: 	while (my $line=<$fh>) {
   94: 	    chomp($line);
   95: 	    my ($name,$value)=split(/\=/,$line);
   96: 	    $userinfo{$name}=$value;
   97: 	}
   98: 	$fh->close();
   99: 	my ($color,$userclass)=&analyze_time($since);
  100: 	&add_count('Overall','all',$userclass);
  101: 	&add_count('Domain',$userinfo{'user.domain'},$userclass);
  102: 	
  103: 	unless ($oneline) {
  104: 	    if (!$justsummary) {
  105: 		print '<font color="'.$color.'">';
  106: 		print '<h3>'.$userinfo{'environment.lastname'}.', '.
  107: 		    $userinfo{'environment.firstname'}.' '.
  108: 		    $userinfo{'environment.middlename'}.' '.
  109: 		    $userinfo{'environment.generation'}." (".
  110: 		    $userinfo{'user.name'}."\@".$userinfo{'user.domain'}.
  111: 		    ")</h3>\n<b>Login time:</b> ".
  112: 		    localtime($userinfo{'user.login.time'}).
  113: 		    ' <b>Browser</b>: '.$userinfo{'browser.type'}.
  114: 		    " on ".$userinfo{'browser.os'}."<b>Client:</b> ".
  115: 		    $userinfo{'request.host'}."<br />\n<b>Role: </b>".
  116: 		    $userinfo{'request.role'}." ";
  117: 	    }
  118: 	    &add_count('Browser',$userinfo{'browser.type'},$userinfo{'browser.version'});
  119: 	    &add_count('OS',$userinfo{'browser.os'},$userinfo{'browser.type'});
  120: 	    if ($userinfo{'request.course.id'}) {
  121: 		my $cid=$userinfo{'request.course.id'};
  122: 		my $coursename= $userinfo{'course.'.$cid.'.description'}.
  123: 		    ' ('.$cid.')';
  124: 		if (!$justsummary) { print "<b>Course:</b> ".$coursename; }
  125: 		&add_count('Course',$coursename,$userclass);
  126: 	    } else {
  127: 		if (!$justsummary) { print "Not in a course."; }
  128: 		&add_count('Course','No Course',$userclass);
  129: 	    }
  130: 	    if (!$justsummary) {
  131: 		print "<br /><b>Last Transaction:</b> ".localtime($mtime).
  132: 		    " (".$since." secs ago) <br /><b>Last Access:</b> ".
  133: 		    localtime($atime)." (".$sinceacc." secs ago)";
  134: 		print ("</font>"); 
  135: 	    }
  136: 	}
  137:     }
  138:     closedir(DIR);
  139:     open (LOADAVGH,"/proc/loadavg");
  140:     my $loadavg=<LOADAVGH>;
  141:     close(LOADAVGH);
  142:     unless ($oneline) { 
  143: 	print "<hr /><h2>User Counts</h2>";
  144: #	print "<pre>\n";
  145: 	&showact('Overall',%usercount);
  146: 	&showact('Domain',%usercount);
  147: 	&showact('Course',%usercount);
  148: 	&show('Browser',%usercount);
  149: 	&show('OS',%usercount);
  150: 
  151: #	print "\n</pre>";
  152: 	print "<b>Load Average:<b> ".$loadavg;
  153: 	print "</body></html>";
  154:     } else {
  155: 	foreach my $l1 (sort keys %usercount) {
  156: 	    foreach my $l2 (sort keys %{$usercount{$l1}}) {
  157: 		foreach my $l3 (sort keys %{$usercount{$l1}{$l2}}) {
  158: 		    print $l1.'_'.$l2.'_'.$l3.'='.$usercount{$l1}{$l2}{$l3}.'&';
  159: 		}
  160: 	    }
  161: 	}
  162: 	#clusterstatus values
  163: 	foreach my $act (@actl) {
  164: 	    print "$act=".$usercount{'Overall'}{'all'}{$act}.'&';
  165: 	}
  166: 	print 'loadavg='.$loadavg;
  167:     }
  168: }
  169: 
  170: sub show {
  171:     my ($cat,%usercount)=@_;
  172:     print("<h3>$cat</h3>\n");
  173:     foreach my $type (sort(keys(%{$usercount{$cat}}))) {
  174: 	print("<table border='1'><tr><th>$type</th><th>");
  175: 	print(join("</th><th>",sort(keys(%{$usercount{$cat}{$type}}))));
  176: 	my $temp;
  177: 	my $count=0;
  178: 	foreach my $version (sort(keys(%{$usercount{$cat}{$type}}))) {
  179: 	    $temp.="<td>".$usercount{$cat}{$type}{$version}.
  180: 		"</td>";
  181: 	    $count+=$usercount{$cat}{$type}{$version};
  182: 	}
  183: 	print("</th></tr><tr><td>$count</td>");
  184: 	print($temp."</tr></table>\n");
  185:     }    
  186: }
  187: 
  188: sub showact {
  189:     my ($cat,%usercount)=@_;
  190:     print("<h3>$cat</h3>\n");
  191:     
  192:     print("<table border='1'><tr><th></th><th>");
  193:     print(join("</th><th>",('Any',@actl)));
  194:     print("</th></tr>");
  195:     foreach my $type (sort(keys(%{$usercount{$cat}}))) {
  196: 	print("<tr><td>$type</td>");
  197: 	my $temp;
  198: 	my $count=0;
  199: 	foreach my $activity (@actl) {
  200: 	    $temp.="<td>&nbsp;".$usercount{$cat}{$type}{$activity}."</td>";
  201: 	    $count+=$usercount{$cat}{$type}{$activity};
  202: 	}
  203: 	print("<td>$count</td>");
  204: 	print($temp);
  205:     }    
  206:     print("</tr></table>\n");
  207: }
  208: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>