Annotation of loncom/cgi/userstatus.pl, revision 1.18
1.1 www 1: #!/usr/bin/perl
2: $|=1;
3: # User Status
1.18 ! raeburn 4: # $Id: userstatus.pl,v 1.17 2008/11/28 20:45:21 raeburn 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.17 raeburn 29: use strict;
1.1 www 30:
31: use lib '/home/httpd/lib/perl/';
1.17 raeburn 32: use Apache::lonlocal;
1.1 www 33: use LONCAPA::Configuration;
1.17 raeburn 34: use LONCAPA::loncgi;
1.18 ! raeburn 35: use LONCAPA::lonauthcgi;
1.1 www 36: use HTTP::Headers;
1.15 albertel 37: use GDBM_File;
1.1 www 38:
1.17 raeburn 39: # -------------------- Read loncapa.conf (and by default, loncapa_apache.conf).
40: my $perlvar=&LONCAPA::Configuration::read_conf('loncapa.conf');
1.5 albertel 41:
1.17 raeburn 42: print "Content-type: text/html\n\n";
1.5 albertel 43: my %usercount;
44: my @actl=('Active','Moderately Active','Inactive');
45:
1.17 raeburn 46: &main($perlvar);
1.5 albertel 47:
48: sub analyze_time {
49: my ($since)=@_;
50: my $color="#000000";
51: my $userclass=$actl[0];
1.14 albertel 52: if ($since>300) { $color="#222222"; $userclass=$actl[1]; }
1.5 albertel 53: if ($since>600) { $color="#444444"; }
1.14 albertel 54: if ($since>1800) { $color="#666666"; }
1.5 albertel 55: if ($since>7200) { $color="#888888"; }
56: if ($since>21600) { $color="#AAAAAA"; $userclass=$actl[2]; }
57: return ($color,$userclass);
58: }
59:
60: sub add_count {
61: my ($cat,$scope,$class)=@_;
62: if (!defined($usercount{$cat})) {
63: $usercount{$cat}={};
64: }
65: if (!defined($usercount{$cat}{$scope})) {
66: $usercount{$cat}{$scope}={};
1.1 www 67: }
1.5 albertel 68: $usercount{$cat}{$scope}{$class}++;
1.1 www 69: }
1.5 albertel 70:
71: sub main {
1.17 raeburn 72: my ($perlvar) = @_;
1.5 albertel 73: delete $$perlvar{'lonReceipt'}; # remove since sensitive and not needed
74: delete $$perlvar{'lonSqlAccess'}; # remove since sensitive and not needed
75:
1.18 ! raeburn 76: if (!&LONCAPA::lonauthcgi::check_ipbased_access()) {
1.17 raeburn 77: if (!&LONCAPA::loncgi::check_cookie_and_load_env()) {
78: &Apache::lonlocal::get_language_handle();
79: print &LONCAPA::loncgi::missing_cookie_msg();
80: return;
81: }
82:
1.18 ! raeburn 83: if (!&LONCAPA::lonauthcgi::can_view('userstatus')) {
1.17 raeburn 84: &Apache::lonlocal::get_language_handle();
1.18 ! raeburn 85: print &LONCAPA::lonauthcgi::unauthorized_msg('userstatus');
1.17 raeburn 86: return;
87: }
88: }
89:
90: &Apache::lonlocal::get_language_handle();
91: my (%gets,$dom,$oneline,$justsummary);
92: &LONCAPA::loncgi::cgi_getitems($ENV{'QUERY_STRING'},\%gets);
93: if (defined($gets{'simple'})) {
94: $oneline = 'simple';
95: }
96: if (defined($gets{'summary'})) {
97: $justsummary = 'summary';
98: }
99:
100: my %lt = &Apache::lonlocal::texthash(
101: usrs => 'User Status',
102: login => 'Login time',
103: on => 'on',
104: Client => 'Client',
105: role => 'Role',
106: notc => 'Not in a course',
107: ltra => 'Last Transaction',
108: lacc => 'Last Access',
109: secs => 'secs ago',
110: usrc => 'User Counts',
111: load => 'Load Average',
112: Overall => 'Overall',
113: Domain => 'Domain',
114: Course => 'Course',
115: Browser => 'Browser',
116: OS => 'OS',
117: Active => 'Active',
118: 'Moderately Active' => 'Moderately Active',
119: Inactive => 'Inactive',
120: );
121:
122: unless ($oneline) {
123: my $now = time();
124: print '<html><body bgcolor="#FFFFFF">'."\n".
125: "<h1>$lt{'usrs'} ".&Apache::lonlocal::locallocaltime($now).'</h1>';
126: }
1.5 albertel 127:
128: opendir(DIR,$$perlvar{'lonIDsDir'});
129: my @allfiles=(sort(readdir(DIR)));
1.14 albertel 130: my %users;
1.5 albertel 131: foreach my $filename (@allfiles) {
132: if ($filename=~/^\./) { next; }
1.12 albertel 133: if ($filename=~/^publicuser_/) { next; }
1.5 albertel 134: my ($dev,$ino,$mode,$nlink,
135: $uid,$gid,$rdev,$size,
136: $atime,$mtime,$ctime,
137: $blksize,$blocks)=stat($$perlvar{'lonIDsDir'}.'/'.$filename);
138: my $now=time;
139: my $since=$now-$mtime;
140: my $sinceacc=$now-$atime;
1.14 albertel 141: #unless ($oneline || $justsummary) { print ("\n\n<hr />"); }
1.5 albertel 142: my %userinfo;
1.11 albertel 143: ($userinfo{'user.name'},undef,$userinfo{'user.domain'})=
144: split('_',$filename);
1.5 albertel 145: my ($color,$userclass)=&analyze_time($since);
146: &add_count('Overall','all',$userclass);
147: &add_count('Domain',$userinfo{'user.domain'},$userclass);
148:
149: unless ($oneline) {
1.15 albertel 150: if (!tie(%userinfo,'GDBM_File',
151: $$perlvar{'lonIDsDir'}.'/'.$filename,
152: &GDBM_READER(),0640)) {
153: next;
1.11 albertel 154: }
1.5 albertel 155: if (!$justsummary) {
1.14 albertel 156: $users{$userclass}{$filename} .=
157: '<font color="'.$color.'">'.
158: '<h3>'.$userinfo{'environment.lastname'}.', '.
1.5 albertel 159: $userinfo{'environment.firstname'}.' '.
160: $userinfo{'environment.middlename'}.' '.
161: $userinfo{'environment.generation'}." (".
162: $userinfo{'user.name'}."\@".$userinfo{'user.domain'}.
1.16 albertel 163: ")</h3>\n".
164: "<p><tt>$filename</tt></p>".
1.17 raeburn 165: "<b>$lt{'login'}:</b> ".
166: &Apache::lonlocal::locallocaltime($userinfo{'user.login.time'}).
167: " <b>$lt{'Browser'}</b>: ".$userinfo{'browser.type'}.
168: " $lt{'on'} ".$userinfo{'browser.os'}."<b>$lt{'Client'}:</b>".
169: $userinfo{'request.host'}."<br />\n<b>$lt{'role'}: </b>".
1.5 albertel 170: $userinfo{'request.role'}." ";
171: }
172: &add_count('Browser',$userinfo{'browser.type'},$userinfo{'browser.version'});
1.10 albertel 173: &add_count('OS',$userinfo{'browser.os'},$userinfo{'browser.type'});
1.5 albertel 174: if ($userinfo{'request.course.id'}) {
175: my $cid=$userinfo{'request.course.id'};
176: my $coursename= $userinfo{'course.'.$cid.'.description'}.
177: ' ('.$cid.')';
1.14 albertel 178: if (!$justsummary) {
179: $users{$userclass}{$filename} .=
1.17 raeburn 180: "<b>$lt{'Course'}:</b> ".$coursename;
1.14 albertel 181: }
1.5 albertel 182: &add_count('Course',$coursename,$userclass);
183: } else {
1.14 albertel 184: if (!$justsummary) {
1.17 raeburn 185: $users{$userclass}{$filename} .= $lt{'notc'};
1.14 albertel 186: }
1.5 albertel 187: &add_count('Course','No Course',$userclass);
188: }
189: if (!$justsummary) {
1.14 albertel 190: $users{$userclass}{$filename} .=
1.17 raeburn 191: "<br /><b>$lt{'ltra'}:</b> ".&Apache::lonlocal::locallocaltime($mtime).
192: " (".$since." $lt{'secs'}) <br /><b>$lt{'lacc'}:</b> ".
193: &Apache::lonlocal::locallocaltime($atime)." (".$sinceacc." $lt{'secs'})".
1.14 albertel 194: "</font>";
195: }
196: }
1.15 albertel 197: untie(%userinfo);
1.14 albertel 198: }
199: if (!$oneline && !$justsummary) {
200: foreach my $class (@actl) {
1.17 raeburn 201: print("\n\n<hr /><h1>$lt{$class}</h1>");
1.14 albertel 202: foreach my $filename (sort(keys(%{$users{$class}}))) {
203: print("\n\n".$users{$class}{$filename}."\n\n<hr />");
1.5 albertel 204: }
205: }
206: }
1.14 albertel 207:
1.5 albertel 208: closedir(DIR);
209: open (LOADAVGH,"/proc/loadavg");
210: my $loadavg=<LOADAVGH>;
211: close(LOADAVGH);
212: unless ($oneline) {
1.17 raeburn 213: print "<hr /><h2>$lt{'usrc'}</h2>";
1.5 albertel 214: # print "<pre>\n";
1.17 raeburn 215: &showact('Overall',\%lt,%usercount);
216: &showact('Domain',\%lt,%usercount);
217: &showact('Course',\%lt,%usercount);
218: &show('Browser',\%lt,%usercount);
219: &show('OS',\%lt,%usercount);
1.5 albertel 220:
221: # print "\n</pre>";
1.17 raeburn 222: print "<b>$lt{'load'}:<b> ".$loadavg;
1.5 albertel 223: print "</body></html>";
224: } else {
1.6 albertel 225: foreach my $l1 (sort keys %usercount) {
226: foreach my $l2 (sort keys %{$usercount{$l1}}) {
227: foreach my $l3 (sort keys %{$usercount{$l1}{$l2}}) {
228: print $l1.'_'.$l2.'_'.$l3.'='.$usercount{$l1}{$l2}{$l3}.'&';
229: }
230: }
231: }
232: #clusterstatus values
233: foreach my $act (@actl) {
234: print "$act=".$usercount{'Overall'}{'all'}{$act}.'&';
1.5 albertel 235: }
236: print 'loadavg='.$loadavg;
237: }
1.1 www 238: }
1.5 albertel 239:
240: sub show {
1.17 raeburn 241: my ($cat,$ltref,%usercount)=@_;
242: print("<h3>$ltref->{$cat}</h3>\n");
1.5 albertel 243: foreach my $type (sort(keys(%{$usercount{$cat}}))) {
244: print("<table border='1'><tr><th>$type</th><th>");
245: print(join("</th><th>",sort(keys(%{$usercount{$cat}{$type}}))));
246: my $temp;
247: my $count=0;
248: foreach my $version (sort(keys(%{$usercount{$cat}{$type}}))) {
249: $temp.="<td>".$usercount{$cat}{$type}{$version}.
250: "</td>";
251: $count+=$usercount{$cat}{$type}{$version};
252: }
253: print("</th></tr><tr><td>$count</td>");
254: print($temp."</tr></table>\n");
255: }
1.3 www 256: }
1.5 albertel 257:
258: sub showact {
1.17 raeburn 259: my ($cat,$ltref,%usercount)=@_;
260: print("<h3>$ltref->{$cat}</h3>\n");
1.5 albertel 261:
262: print("<table border='1'><tr><th></th><th>");
263: print(join("</th><th>",('Any',@actl)));
264: print("</th></tr>");
265: foreach my $type (sort(keys(%{$usercount{$cat}}))) {
266: print("<tr><td>$type</td>");
267: my $temp;
268: my $count=0;
269: foreach my $activity (@actl) {
270: $temp.="<td> ".$usercount{$cat}{$type}{$activity}."</td>";
271: $count+=$usercount{$cat}{$type}{$activity};
272: }
273: print("<td>$count</td>");
274: print($temp);
275: }
276: print("</tr></table>\n");
1.3 www 277: }
1.5 albertel 278:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>