Showactive

From WeBWorK_wiki
Jump to navigation Jump to search
#!/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";
       }
    }
  }
}