#!/usr/ug/bin/perl5 # # sample use: # presence_cron # # by Wei-Hwa Huang (whuang@ugcs.caltech.edu) my $MYNAME = 'whuang@ugcs.caltech.edu'; my $TIME = `date +%s`; use strict; my $filename = "/home/whuang/prog/gale/data/presence.$MYNAME"; my @times = (); # add the old times if (-e $filename) { open(FHAND,"<$filename"); my @data = ; chomp(@data); my $tm; my $lastline = ""; foreach $tm (@data) { next if ($tm eq $lastline); if ($TIME - $tm <= (60 * 60 * 99)) { push(@times, $tm); } $lastline = $tm; } close(FHAND); } ############## now to analyze the stats sub numform { (my $boog) = @_; my $result = ''; $result .= '0' if (int($boog/3600) < 10); $result .= (int($boog/3600)); $result .= ':'; $result .= '0' if (int($boog/60%60) < 10); $result .= (int($boog/60%60)); $result .= ':'; $result .= '0' if (int($boog%60) < 10); $result .= (int($boog%60)); return $result; } sub since { my ($limit, @times) = @_; my $answer = 0; my $tm; foreach $tm (@times) { $answer++ if ($tm <= $limit); } return $answer; } sub metric { my (@times) = @_; my $answer = 0; my $tm; foreach $tm (@times) { $answer += 1/$tm; } return $answer; } my @difftimes; foreach (@times) { push(@difftimes,$TIME-$_); } my $presence; # if I haven't said anything in the last two hours, I'm # probably out if (&since(7200,@difftimes) == 0) { my $hrs = $difftimes[$#difftimes]/3600; $hrs = int($hrs*10)/10; $presence = "out/since/$hrs/hours/ago"; } else { $presence = "in/"; my $met = (int(&metric(@difftimes)*10000))/10000; if (&since(600,@difftimes) >= 7) { $presence .= "and/should/probably/shut/up"; } elsif (&since(30,@difftimes) >= 1) { $presence .= "currently/puffing"; } elsif (&metric(@difftimes) >= 0.03) { $presence .= "very/chatty"; } elsif (&metric(@difftimes) >= 0.01) { $presence .= "chatty"; } elsif (&metric(@difftimes) >= 0.003) { $presence .= "sporadic"; } elsif (&metric(@difftimes) >= 0.001) { $presence .= "quiet"; } elsif (&metric(@difftimes) >= 0.0003) { $presence .= "real/quiet"; } else { $presence .= "or/maybe/not"; } # $presence .= "/debug/$met"; } my @processes = `/usr/ug/bin/ps -ef | /bin/grep whuang | /bin/grep gsub`; my $proc; #open(FHAND,">/home/whuang/googoo"); #print FHAND "yayay", @processes, "yasuo"; foreach $proc (@processes) { if ($proc !~ /grep/) { my @arse = split(/ +/,$proc); #print FHAND ("kill $arse[1]"); system("kill $arse[1]"); } } #close FHAND; # system("/usr/contrib/bin/gsub -p $presence >/dev/null 2>\&1 || exit 0"); system("/usr/contrib/bin/gsub -p $presence");