Annotation of loncom/cgi/userstatus.pl, revision 1.11

1.1       www         1: #!/usr/bin/perl
                      2: $|=1;
                      3: # User Status
1.11    ! albertel    4: # $Id: userstatus.pl,v 1.10 2004/01/14 01:42:09 albertel Exp $
1.7       albertel    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: 
1.1       www        29: 
1.5       albertel   30: use strict;
1.1       www        31: use lib '/home/httpd/lib/perl/';
                     32: use LONCAPA::Configuration;
                     33: 
                     34: use HTTP::Headers;
                     35: use IO::File;
                     36: 
1.5       albertel   37: 
                     38: my %usercount;
                     39: my @actl=('Active','Moderately Active','Inactive');
                     40: 
1.1       www        41:  
1.3       www        42: print "Content-type: text/html\n\n";
                     43:       
1.1       www        44: # -------------------- Read loncapa.conf (and by default, loncapa_apache.conf).
1.5       albertel   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"; }
1.8       albertel   53:     if ($since>1800) { $color="#666666"; $userclass=$actl[1]; }
1.5       albertel   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}={};
1.1       www        66:     }
1.5       albertel   67:     $usercount{$cat}{$scope}{$class}++;
1.1       www        68: }
1.5       albertel   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;
1.11    ! albertel   92: 	($userinfo{'user.name'},undef,$userinfo{'user.domain'})=
        !            93: 	    split('_',$filename);
1.5       albertel   94: 	my ($color,$userclass)=&analyze_time($since);
                     95: 	&add_count('Overall','all',$userclass);
                     96: 	&add_count('Domain',$userinfo{'user.domain'},$userclass);
                     97: 	
                     98: 	unless ($oneline) {
1.11    ! albertel   99: 	    my $fh=IO::File->new($$perlvar{'lonIDsDir'}.'/'.$filename);
        !           100: 	    while (my $line=<$fh>) {
        !           101: 		chomp($line);
        !           102: 		my ($name,$value)=split(/\=/,$line);
        !           103: 		$userinfo{$name}=$value;
        !           104: 	    }
        !           105: 	    $fh->close();
1.5       albertel  106: 	    if (!$justsummary) {
                    107: 		print '<font color="'.$color.'">';
                    108: 		print '<h3>'.$userinfo{'environment.lastname'}.', '.
                    109: 		    $userinfo{'environment.firstname'}.' '.
                    110: 		    $userinfo{'environment.middlename'}.' '.
                    111: 		    $userinfo{'environment.generation'}." (".
                    112: 		    $userinfo{'user.name'}."\@".$userinfo{'user.domain'}.
                    113: 		    ")</h3>\n<b>Login time:</b> ".
                    114: 		    localtime($userinfo{'user.login.time'}).
1.9       albertel  115: 		    ' <b>Browser</b>: '.$userinfo{'browser.type'}.
                    116: 		    " on ".$userinfo{'browser.os'}."<b>Client:</b> ".
1.5       albertel  117: 		    $userinfo{'request.host'}."<br />\n<b>Role: </b>".
                    118: 		    $userinfo{'request.role'}." ";
                    119: 	    }
                    120: 	    &add_count('Browser',$userinfo{'browser.type'},$userinfo{'browser.version'});
1.10      albertel  121: 	    &add_count('OS',$userinfo{'browser.os'},$userinfo{'browser.type'});
1.5       albertel  122: 	    if ($userinfo{'request.course.id'}) {
                    123: 		my $cid=$userinfo{'request.course.id'};
                    124: 		my $coursename= $userinfo{'course.'.$cid.'.description'}.
                    125: 		    ' ('.$cid.')';
                    126: 		if (!$justsummary) { print "<b>Course:</b> ".$coursename; }
                    127: 		&add_count('Course',$coursename,$userclass);
                    128: 	    } else {
                    129: 		if (!$justsummary) { print "Not in a course."; }
                    130: 		&add_count('Course','No Course',$userclass);
                    131: 	    }
                    132: 	    if (!$justsummary) {
                    133: 		print "<br /><b>Last Transaction:</b> ".localtime($mtime).
                    134: 		    " (".$since." secs ago) <br /><b>Last Access:</b> ".
                    135: 		    localtime($atime)." (".$sinceacc." secs ago)";
                    136: 		print ("</font>"); 
                    137: 	    }
                    138: 	}
                    139:     }
                    140:     closedir(DIR);
                    141:     open (LOADAVGH,"/proc/loadavg");
                    142:     my $loadavg=<LOADAVGH>;
                    143:     close(LOADAVGH);
                    144:     unless ($oneline) { 
                    145: 	print "<hr /><h2>User Counts</h2>";
                    146: #	print "<pre>\n";
                    147: 	&showact('Overall',%usercount);
                    148: 	&showact('Domain',%usercount);
                    149: 	&showact('Course',%usercount);
                    150: 	&show('Browser',%usercount);
1.9       albertel  151: 	&show('OS',%usercount);
1.5       albertel  152: 
                    153: #	print "\n</pre>";
                    154: 	print "<b>Load Average:<b> ".$loadavg;
                    155: 	print "</body></html>";
                    156:     } else {
1.6       albertel  157: 	foreach my $l1 (sort keys %usercount) {
                    158: 	    foreach my $l2 (sort keys %{$usercount{$l1}}) {
                    159: 		foreach my $l3 (sort keys %{$usercount{$l1}{$l2}}) {
                    160: 		    print $l1.'_'.$l2.'_'.$l3.'='.$usercount{$l1}{$l2}{$l3}.'&';
                    161: 		}
                    162: 	    }
                    163: 	}
                    164: 	#clusterstatus values
                    165: 	foreach my $act (@actl) {
                    166: 	    print "$act=".$usercount{'Overall'}{'all'}{$act}.'&';
1.5       albertel  167: 	}
                    168: 	print 'loadavg='.$loadavg;
                    169:     }
1.1       www       170: }
1.5       albertel  171: 
                    172: sub show {
                    173:     my ($cat,%usercount)=@_;
                    174:     print("<h3>$cat</h3>\n");
                    175:     foreach my $type (sort(keys(%{$usercount{$cat}}))) {
                    176: 	print("<table border='1'><tr><th>$type</th><th>");
                    177: 	print(join("</th><th>",sort(keys(%{$usercount{$cat}{$type}}))));
                    178: 	my $temp;
                    179: 	my $count=0;
                    180: 	foreach my $version (sort(keys(%{$usercount{$cat}{$type}}))) {
                    181: 	    $temp.="<td>".$usercount{$cat}{$type}{$version}.
                    182: 		"</td>";
                    183: 	    $count+=$usercount{$cat}{$type}{$version};
                    184: 	}
                    185: 	print("</th></tr><tr><td>$count</td>");
                    186: 	print($temp."</tr></table>\n");
                    187:     }    
1.3       www       188: }
1.5       albertel  189: 
                    190: sub showact {
                    191:     my ($cat,%usercount)=@_;
                    192:     print("<h3>$cat</h3>\n");
                    193:     
                    194:     print("<table border='1'><tr><th></th><th>");
                    195:     print(join("</th><th>",('Any',@actl)));
                    196:     print("</th></tr>");
                    197:     foreach my $type (sort(keys(%{$usercount{$cat}}))) {
                    198: 	print("<tr><td>$type</td>");
                    199: 	my $temp;
                    200: 	my $count=0;
                    201: 	foreach my $activity (@actl) {
                    202: 	    $temp.="<td>&nbsp;".$usercount{$cat}{$type}{$activity}."</td>";
                    203: 	    $count+=$usercount{$cat}{$type}{$activity};
                    204: 	}
                    205: 	print("<td>$count</td>");
                    206: 	print($temp);
                    207:     }    
                    208:     print("</tr></table>\n");
1.3       www       209: }
1.5       albertel  210: 

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