From: Tim Pope Date: Tue, 16 Aug 2005 07:30:13 +0000 (+0000) Subject: Lots of optimization X-Git-Url: http://git.tpope.net/?p=tpope-extra.git;a=commitdiff_plain;h=62d1a430c533c078983e3e94889f5bd353d8b2ea Lots of optimization --- diff --git a/perl/away-tpope b/perl/away-tpope index dd26efb..38ce70d 100755 --- a/perl/away-tpope +++ b/perl/away-tpope @@ -3,7 +3,7 @@ # -*- perl -*- vim:set ft=perl sw=4 sts=4: use strict; -use vars qw(%state @ssh $last $pipe $arg); +use vars qw(%state @ssh $last $pipe $arg $slow); @ssh=("ssh","-a","-x","-oBatchmode=yes","-oSetupTimeOut=20"); $pipe = "/tmp/.away-tpope"; @@ -12,7 +12,7 @@ daemonize() if ($arg eq "-d"); daemon() if ($arg eq "-D"); -if($arg eq "activity" || $arg eq "away") { +if($arg eq "activity" || $arg eq "away" || $arg eq "out" || $arg eq "check") { if(exists($ARGV[0])) { die "Daemon not running\n" unless (-p $pipe); my $a = join(" ",@ARGV); @@ -20,7 +20,6 @@ if($arg eq "activity" || $arg eq "away") { open FIFO, ">$pipe"; print FIFO "$arg $a"; } else { - $arg =~ s/^away$/customaway/; load(); print $state{$arg}."\n" if($state{$arg}); } @@ -29,6 +28,7 @@ if($arg eq "activity" || $arg eq "away") { } sub daemon { + die "Daemon already running?\n" if (-e "/tmp/.away-tpope.pid"); open TMP, ">/tmp/.away-tpope.pid"; print TMP "$$\n"; close TMP; @@ -36,15 +36,17 @@ sub daemon { $SIG{'INT'} = \&quit_handler; $SIG{'TERM'} = \&quit_handler; $SIG{'QUIT'} = \&quit_handler; - $SIG{'HUP'} = \&cycle; + $SIG{'HUP'} = \&hup_handler; $SIG{'USR2'} = \&restart_handler; die "Daemon already running\n" if (-r "/tmp/.tpope-away.pid"); unless (-p $pipe) { unlink $pipe; system("mkfifo", $pipe); + chmod 0600, $pipe; } + $last=0; + $slow=1; cycle(); - $last=time; while(1) { main_loop(); } @@ -83,24 +85,37 @@ sub main_loop { custom_activity($read); } elsif($c eq "away") { custom_away($read); + } elsif($c eq "out") { + custom_out($read); + } elsif($c eq "check") { + host_check($read); } else { print "Unknown command: $c\n"; } } - if(time-$last >=300) { - local $SIG{'HUP'} = "IGNORE"; + if(time-$last >= 240) { cycle(); - $last=time; } } sub cycle { - do_phone(); - do_hosts(); + local $SIG{'HUP'} = sub {$last = 1}; + do_chat(); + if((!$last) || $slow++ ) { + do_hosts(); + do_power(); + do_phone(); + $slow=0; + } do_schedule(); - process_away(); + do_decision(); save(); do_custom(); + if($last==1) { + $last=0; + } else { + $last=int(time/60)*60; + } } sub ping { @@ -122,9 +137,10 @@ sub do_schedule { my ($begin, $end, $maxend); next unless /(\d\d):(\d\d)-(\d\d):(\d\d) (.*)/; my ($hs,$ms,$he,$me,$ev) = ($1, $2, $3, $4, $5); + $ev =~ s/ *\[.*$//; $begin = $hs*60+$ms-25; - $maxend = $he*60+$me+5; - $end = ($begin+25)*3/4+($maxend-5)/4; + $maxend = $he*60+$me+10; + $end = ($begin+25)*3/4+($maxend-10)/4; if($begin <= $now && $now < $end) { $state{'class'} = $ev; $familiar = 1; @@ -145,6 +161,7 @@ sub do_schedule { my ($begin, $end, $maxend); next unless /(\d\d):(\d\d)-(\d\d):(\d\d) (.*)/; my ($hs,$ms,$he,$me,$ev) = ($1, $2, $3, $4, $5); + $ev =~ s/ *\[.*$//; $begin = $hs*60+$ms; $maxend = $he*60+$me; $end = ($begin)*3/4+($maxend)/4; @@ -152,7 +169,7 @@ sub do_schedule { $state{'schedule'} = $ev; $familiar = 1; last; - } elsif ($state{'schedule'} eq $ev) { + } elsif (($state{'schedule'}||'') eq $ev) { $familiar = 1; undef $state{'schedule'} if ($now > $maxend || $state{'phone'} eq "present"); @@ -162,16 +179,67 @@ sub do_schedule { undef $state{'schedule'} unless ($familiar); } +sub do_chat { + if(-r ($ENV{'HOME'} . "/.chat")) { + open TMP, $ENV{'HOME'} . "/.chat"; + my $chat = join ("", ); + close TMP; + chomp $chat; + $state{'chat'} = $chat; + } else { + undef $state{'chat'}; + } +} + sub do_hosts { - my @livehosts; - foreach my $host ("sarah", "homer", "lisa", "mona") { - push @livehosts, $host if(ping($host)); + my (@check) = ("mona", "lisa", "homer", "sarah"); + my (@uphosts, @livehosts, $host, $hostlist); + if(($_[0] || 0) == 1) { + $hostlist=$state{'hosts'}; + $hostlist=~s/0\S* ?//g; + @livehosts = split / /, $hostlist; + } else { + $hostlist=""; + foreach $host (@check) { + push @uphosts, $host if(ping($host)); + } + foreach $host (@uphosts) { + if(is_alive($host)) { + push @livehosts,$host; + $hostlist="$host $hostlist"; + } else { + $hostlist="0$host $hostlist"; + } + } + $hostlist=~s/ $//; + $state{'hosts'}=$hostlist; + } + if(scalar @livehosts == 0) { + $state{'alive'} = ''; + } elsif(scalar @livehosts > 1 && $state{'chat'}) { + foreach $host (@livehosts) { + if ($host eq $state{'chat'}) { + $state{'alive'} = $host; + return; + } + } } - my $alive = ""; - foreach my $host (@livehosts) { - $alive=$host unless(system(@ssh,$host, 'if pidof xscreensaver >/dev/null && DISPLAY=:0.0 xscreensaver-command -version >/dev/null 2>&1; then if DISPLAY=:0.0 xscreensaver-command -time 2>&1 |egrep "non-blanked|no saver status" >/dev/null; then true; else pid=`ps ax|egrep "[0-9]:[0-9][0-9] ssh marge .*(screen.*RR irc|Chat)"|sed -e "s/^ *//"|cut -d" " -f 1`; [ -f "$HOME/.irc.lock" -o -z "$pid" ] || kill $pid; false; fi; else false; fi') >> 8); + $state{'alive'}=$livehosts[0]; +} + +sub is_alive { + my $ret; + eval { + local $SIG{ALRM} = sub { die "alarm\n" }; + alarm(30); + $ret=!(system(@ssh,shift, 'if pidof xscreensaver >/dev/null && DISPLAY=:0.0 xscreensaver-command -version >/dev/null 2>&1; then if DISPLAY=:0.0 xscreensaver-command -time 2>&1 |egrep "non-blanked|no saver status" >/dev/null; then true; else pid=`ps ax|egrep "[0-9]:[0-9][0-9] ssh marge .*(screen.*RR irc|Chat)"|sed -e "s/^ *//"|cut -d" " -f 1`; [ -f "$HOME/.irc.lock" -o -z "$pid" ] || kill $pid; false; fi; else false; fi') >> 8); + alarm(0); + }; + if($@) { + undef $ret; + die unless $@ eq "alarm\n"; } - $state{'alive'} = $alive; + return $ret; } sub do_phone { @@ -182,19 +250,48 @@ sub do_phone { my $last_slh=`@ssh mona cat .blue/last_slh 2>/dev/null`; if(!$last_slh) { $phone="unknown"; - } elsif (time-$last_slh < 600) { + } elsif (time-$last_slh < 540) { $phone="present"; } else { $phone="absent"; } } - if($state{'phone'} eq 'absent' && $phone eq 'present') { - custom_away(""); + if($state{'phone'} ne 'present' && $phone eq 'present') { + custom_out(""); } + $state{'phone'} = $phone; } -sub process_away { - if ($state{'customaway'}) { +sub do_power { + open TMP, "upsc milhouse\@localhost|"; + my $ups=''; + while() { + chomp; + if(/status: (.*)/i) { + $ups=$1; + } + } + close TMP; + $state{'ups'}=$ups; +} + +sub do_decision { + custom_away("") + if ($state{'customawayexp'} && $state{'customawayexp'}) { $line =~ s/\\n/\n/g; - $line =~ s/^([^=]*)=//; + $line =~ s/\\"/"/g; + $line =~ s/"$//; + $line =~ s/^([^=]*)="?//; chomp $line; $state{$1}=$line; } close CONF; } +sub hup_handler { + $last=1; +} + sub restart_handler { alarm 0; unlink $pipe;