| 1 | #!/usr/bin/env perl | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | use Time::HiRes; | ||
| 6 | use Proc::ProcessTable; | ||
| 7 | use Scalar::Util::Numeric qw/isnan isinf/; | ||
| 8 | use List::Util qw/sum/; | ||
| 9 | use Mail::Sendmail; | ||
| 10 | use Data::Dumper; | ||
| 11 | unshift @{ $Mail::Sendmail::mailcfg{'smtp'} }, 'net.wur.nl'; | ||
| 12 | |||
| 13 | use Pod::Usage; | ||
| 14 | use Getopt::Long qw(:config auto_help); | ||
| 15 | |||
| 16 | my %opt = ( | ||
| 17 | interval => 60, | ||
| 18 | mail => ['joachim.bargsten@wur.nl'], | ||
| 19 | 'threshold' => 0.005, | ||
| 20 | 'num_steps' => 3, | ||
| 21 | waiting_time => 30, | ||
| 22 | subject => 'Proc::ProcessTable process tracking' | ||
| 23 | ); | ||
| 24 | |||
| 25 | GetOptions( | ||
| 26 | \%opt, 'help|?', 'waiting_time=i', 'subject=s', 'interval=i', 'mail=s@', | ||
| 27 | 'threshold=s', 'num_steps=i', "verbose" | ||
| 28 | ) or pod2usage(2); | ||
| 29 | |||
| 30 | pod2usage( -exitval => 0, -verbose => 2 ) if ( $opt{help} ); | ||
| 31 | |||
| 32 | pod2usage(2) | ||
| 33 | unless ( @ARGV && @ARGV > 0 ); | ||
| 34 | |||
| 35 | my @pids = @ARGV; | ||
| 36 | my $poll_intervall = $opt{interval} * 1000 * 1000; | ||
| 37 | |||
| 38 | my @start_time; | ||
| 39 | |||
| 40 | $SIG{CHLD} = 'IGNORE'; | ||
| 41 | |||
| 42 | my @cpu_time; | ||
| 43 | |||
| 44 | my $ppt = Proc::ProcessTable->new; | ||
| 45 | |||
| 46 | my $pid_running = 1; | ||
| 47 | |||
| 48 | BIG: | ||
| 49 | while ($pid_running) { | ||
| 50 | my $pt = parse_ppt( $ppt->table ); | ||
| 51 | my %childs; | ||
| 52 | for my $pid (@pids) { | ||
| 53 | map { $childs{$_} => 1 } subproc_ids( $pid, $pt ); | ||
| 54 | $childs{$pid} = 1; | ||
| 55 | } | ||
| 56 | |||
| 57 | my $sum_cpu = 0; | ||
| 58 | my $sum_start = 0; | ||
| 59 | |||
| 60 | $pid_running = 0; | ||
| 61 | for my $p (@$pt) { | ||
| 62 | if ( $childs{ $p->[0] } ) { | ||
| 63 | $pid_running = 1; | ||
| 64 | #say STDERR "timepoint" unless(defined($time_point)); | ||
| 65 | #say STDERR "time" unless(defined($t)); | ||
| 66 | #say STDERR "process" unless((grep { defined $_ } @$p ) != @$p); | ||
| 67 | $sum_cpu += $p->[4]; | ||
| 68 | $sum_start += time - $p->[5]; | ||
| 69 | } | ||
| 70 | } | ||
| 71 | shift @cpu_time if ( @cpu_time > $opt{num_steps} ); | ||
| 72 | push @cpu_time, $sum_cpu; | ||
| 73 | |||
| 74 | shift @start_time if ( @start_time > $opt{num_steps} ); | ||
| 75 | push @start_time, $sum_start; | ||
| 76 | |||
| 77 | if ( @start_time >= $opt{num_steps} ) { | ||
| 78 | my $diff_cpu = ( $cpu_time[-1] - $cpu_time[0] ) / ( 1000 * 1000 ); | ||
| 79 | my $diff_start = ( $start_time[-1] - $start_time[0] ); | ||
| 80 | my $ratio = $diff_cpu / $diff_start; | ||
| 81 | printf "%3.2f ", $ratio if ( $opt{verbose} ); | ||
| 82 | if ( $ratio < $opt{threshold} ) { | ||
| 83 | print "| " if ( $opt{verbose} ); | ||
| 84 | for my $mail ( @{ $opt{mail} } ) { | ||
| 85 | send_mail( | ||
| 86 | { | ||
| 87 | To => $mail, | ||
| 88 | Subject => $opt{subject}, | ||
| 89 | Message => "blast2go has low cpu usage, probably it is finished and you can do the next step" | ||
| 90 | } | ||
| 91 | ); | ||
| 92 | |||
| 93 | } | ||
| 94 | sleep $opt{waiting_time} * 60; | ||
| 95 | |||
| 96 | } | ||
| 97 | } else { | ||
| 98 | print ". " if ( $opt{verbose} ); | ||
| 99 | } | ||
| 100 | Time::HiRes::usleep($poll_intervall); | ||
| 101 | } | ||
| 102 | print "\n" if ( $opt{verbose} ); | ||
| 103 | |||
| 104 | for my $mail ( @{ $opt{mail} } ) { | ||
| 105 | send_mail( | ||
| 106 | { To => $mail, Subject => $opt{subject}, Message => "blast2go is not running anymore and/or finished" } ); | ||
| 107 | } | ||
| 108 | |||
| 109 | sub send_mail { | ||
| 110 | my $c = shift; | ||
| 111 | my %mail = ( | ||
| 112 | From => 'joachim.bargsten@wur.nl', | ||
| 113 | %$c | ||
| 114 | ); | ||
| 115 | |||
| 116 | sendmail(%mail) or die $Mail::Sendmail::error; | ||
| 117 | } | ||
| 118 | |||
| 119 | sub parse_ppt { | ||
| 120 | my $ppt_table = shift; | ||
| 121 | my @table = map { [ $_->pid, $_->ppid, $_->rss, $_->size, $_->time, $_->start, $_->exec ] } @$ppt_table; | ||
| 122 | return \@table; | ||
| 123 | } | ||
| 124 | |||
| 125 | sub subproc_ids { | ||
| 126 | my ( $pid, $procs ) = @_; | ||
| 127 | #[ pid, parentid ] | ||
| 128 | my @childs; | ||
| 129 | for my $c ( grep { $_->[1] == $pid } @$procs ) { | ||
| 130 | push @childs, $c->[0]; | ||
| 131 | push @childs, subproc_ids( $c->[0], $procs ); | ||
| 132 | } | ||
| 133 | return @childs; | ||
| 134 | } | ||
| 135 | |||
| 136 | __END__ | ||
| 137 | |||
| 138 | =head1 NAME | ||
| 139 | |||
| 140 | |||
| 141 | |||
| 142 | =head1 SYNOPSIS | ||
| 143 | |||
| 144 | track_b2g.pl [OPTIONS] <pid1> [<pid2 ... <pidn>] | ||
| 145 | |||
| 146 | =head1 DESCRIPTION | ||
| 147 | |||
| 148 | Track the CPU usage of processes and send a mail if the usage falls below a certain threshold. | ||
| 149 | |||
| 150 | =head1 OPTIONS | ||
| 151 | |||
| 152 | =over 4 | ||
| 153 | |||
| 154 | =item B<< --waiting_time <minutes> >> | ||
| 155 | |||
| 156 | If the mail got send, wait C<< <minutes> >> minutes before resume tracking. This | ||
| 157 | prevents sending you hundreds of mails if the processes are below the | ||
| 158 | threshold. | ||
| 159 | |||
| 160 | =item B<< --subject <subject> >> | ||
| 161 | |||
| 162 | Set the subject of the mail. | ||
| 163 | |||
| 164 | =item B<< --interval <seconds> >> | ||
| 165 | |||
| 166 | Record every C<< <seconds> >> seconds the CPU usage. | ||
| 167 | |||
| 168 | =item B<< --mail <address> >> | ||
| 169 | |||
| 170 | Send notification mails to C<< <address >>. | ||
| 171 | |||
| 172 | =item B<< --threshold <fraction> >> | ||
| 173 | |||
| 174 | Set he notification threshold to C<< <fraction> >>. 100% CPU usage corresponds to a fraction of 1. | ||
| 175 | |||
| 176 | =item B<< --num_steps <steps> >> | ||
| 177 | |||
| 178 | The CPU usage is averaged over a time span of C<< <steps> * <interval time> >>. | ||
| 179 | |||
| 180 | =item B<< --verbose >> | ||
| 181 | |||
| 182 | Enable reporting of CPU usage fraction to STDOUT. If the number of steps is | ||
| 183 | smaller than the averaging window, "." is printed. If a mail is sent, "|" is | ||
| 184 | printed. | ||
| 185 | |||
| 186 | =back | ||
| 187 | |||
| 188 | =head1 SEE ALSO | ||
| 189 | |||
| 190 | =head1 AUTHOR | ||
| 191 | |||
| 192 | jw bargsten, C<< <jwb at cpan dot org> >> | ||
| 193 | |||
| 194 | =cut |