Difference between revisions of "Showactive"

From WeBWorK_wiki
Jump to navigation Jump to search
(Created page with "#!/usr/bin/perl # Showactive # Prints a list of active webwork users. # Author: Nandor Sieben @config= `cat $ENV{WEBWORK_ROOT}/conf/global.conf`; for $line (@config) { i...")
 
m (changed tag)
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
#!/usr/bin/perl
+
#!/usr/bin/perl
  +
  +
# Showactive
  +
# Prints a list of active webwork users.
  +
# Author: Nandor Sieben
  +
  +
@config= `cat $ENV{WEBWORK_ROOT}/conf/global.conf`;
  +
  +
for $line (@config) {
  +
if ($line =~ /\$database_username.*=.*"(.+)"/) {
  +
$database_username = $1;
  +
}
  +
if ($line =~ /\$database_password.*=.*"(.+)"/) {
  +
$database_password = $1;
  +
}
  +
}
  +
  +
sub mysql {
  +
my ($sql) = @_;
  +
qx[mysql -s -u "$database_username" -p"$database_password" webwork << EOF
  +
$sql
  +
quit
  +
EOF
  +
];
  +
}
  +
  +
@tables= mysql(q[show tables like '%key';]);
  +
  +
$ctime=time;
  +
  +
foreach $table (@tables) {
  +
chop $table;
  +
next if ($table =~ /-/);
  +
# print "|$table|\n";
  +
@rows=mysql(qq[select user_id, timestamp from $table]);
  +
foreach $row (@rows) {
  +
if ( $row =~ /(\S+)\s+(\S+)/) {
  +
$name=$1;
  +
$stamp=$2;
  +
$name =~ s/_.+//;
  +
$age =($ctime - $stamp) / 60;
  +
if ($age < 10) {
  +
$table =~ s/_key//;
  +
print "$table\t $name\t $age\n";
  +
}
  +
}
  +
}
  +
}
   
# Showactive
 
# Prints a list of active webwork users.
 
# Author: Nandor Sieben
 
   
@config= `cat $ENV{WEBWORK_ROOT}/conf/global.conf`;
 
  +
[[Category:Scripts]]
 
for $line (@config) {
 
if ($line =~ /\$database_username.*=.*"(.+)"/) {
 
$database_username = $1;
 
}
 
if ($line =~ /\$database_password.*=.*"(.+)"/) {
 
$database_password = $1;
 
}
 
}
 
 
sub mysql {
 
my ($sql) = @_;
 
qx[mysql -s -u $database_username -p$database_password webwork << EOF
 
$sql
 
quit
 
EOF
 
];
 
}
 
 
@tables= mysql(q[show tables like '%key';]);
 
 
$ctime=time;
 
 
foreach $table (@tables) {
 
chop $table;
 
next if ($table =~ /-/);
 
# print "|$table|\n";
 
@rows=mysql(qq[select user_id, timestamp from $table]);
 
foreach $row (@rows) {
 
if ( $row =~ /(\S+)\s+(\S+)/) {
 
$name=$1;
 
$stamp=$2;
 
$name =~ s/_.+//;
 
$age =($ctime - $stamp) / 60;
 
if ($age < 10) {
 
$table =~ s/_key//;
 
print "$table\t $name\t $age\n";
 
}
 
}
 
}
 
}
 

Latest revision as of 15:54, 21 June 2021

#!/usr/bin/perl

# Showactive
# Prints a list of active webwork users.
# Author: Nandor Sieben   

@config= `cat $ENV{WEBWORK_ROOT}/conf/global.conf`;

for $line (@config) {
  if ($line =~ /\$database_username.*=.*"(.+)"/) {
    $database_username = $1;
  }
  if ($line =~ /\$database_password.*=.*"(.+)"/) {
    $database_password = $1;
  }
}

sub mysql {
my ($sql) = @_;
qx[mysql -s -u "$database_username" -p"$database_password" webwork << EOF
$sql
quit
EOF
];
}

@tables= mysql(q[show tables like '%key';]);

$ctime=time;

foreach $table (@tables) {
  chop $table;
  next if ($table =~ /-/);
#  print "|$table|\n";
  @rows=mysql(qq[select user_id, timestamp from $table]);
  foreach $row (@rows) {
    if ( $row =~ /(\S+)\s+(\S+)/) {
       $name=$1;
       $stamp=$2;
       $name =~ s/_.+//;
       $age =($ctime - $stamp) / 60;
       if ($age < 10) {
          $table =~ s/_key//;
          print "$table\t $name\t $age\n";
       }
    }
  }
}