]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/biostat
changed shebang
[biopieces.git] / bp_bin / biostat
1 #!/usr/bin/env perl
2
3 # Copyright (C) 2007-2009 Martin A. Hansen.
4
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19 # http://www.gnu.org/copyleft/gpl.html
20
21
22 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
23
24 # Monitor status running Biopieces.
25
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
27
28
29 use strict;
30 use Maasha::Biopieces;
31 use Maasha::Common;
32 use Maasha::Filesys;
33
34
35 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
36
37
38 my ( $options, $allowed, @files, $file, $user, $pid, $script, $info, $time0, $time1, $args, $du, @table );
39
40 $allowed = "USER,PID,%CPU,%MEM,TMP_DIR,START_TIME,RUN_TIME,COMMAND";
41
42 $options = Maasha::Biopieces::parse_options(
43     [
44         { long => 'time', short => 't', type => 'uint',   mandatory => 'no', default => 5,      allowed => undef,    disallowed => 0 },
45         { long => 'sort', short => 's', type => 'string', mandatory => 'no', default => '%CPU', allowed => $allowed, disallowed => undef },
46         { long => 'user', short => 'u', type => 'string', mandatory => 'no', default => undef,  allowed => undef,    disallowed => undef },
47     ]   
48 );
49
50 while ( 1 )
51 {
52     system( "clear" );
53
54     undef @table;
55
56     @files = Maasha::Filesys::ls_files( $ENV{ 'BP_TMP' } );
57     @files = grep /\.status$/, @files;
58
59     if ( $options->{ 'user' } ) {
60         @files = grep /$options->{ 'user' }/, @files;
61     }
62
63     foreach $file ( @files )
64     {
65         ( $user, $script, $pid ) = split /\./, $file;
66         
67         if ( $info = process_info_mac( $pid ) )
68         {
69             ( $time0, $args, $du ) = get_status_info( $file );
70
71             $info->{ 'START_TIME' } = $time0;
72             $info->{ 'RUN_TIME' }   = Maasha::Common::time_stamp_diff( $time0, Maasha::Common::time_stamp() );
73             $info->{ 'TMP_DIR' }    = $du || "N/A";
74             $info->{ 'COMMAND' }    = "$script $args";
75
76             push @table, $info;
77         }
78         else
79         {
80             print STDERR "Removing stale file: $file\n";
81             unlink $file;
82         }
83     }
84
85     if ( $options->{ 'sort' } =~ /^(USER|TMP_DIR|START_TIME|RUN_TIME|COMMAND)$/ ) {
86         @table = sort { $a->{ $options->{ 'sort' } } cmp $b->{ $options->{ 'sort' } } } @table;
87     } else {
88         @table = sort { $a->{ $options->{ 'sort' } } <=> $b->{ $options->{ 'sort' } } } @table;
89     }
90     
91     print join( "\t", 'USER', 'PID', '%CPU', '%MEM', 'TMP_DIR', 'START_TIME           ', 'RUN_TIME', 'COMMAND' ), "\n";
92     print join( "\t", $_->{ 'USER' }, $_->{ 'PID' }, $_->{ '%CPU' }, $_->{ '%MEM' }, $_->{ 'TMP_DIR' }, $_->{ 'START_TIME' }, $_->{ 'RUN_TIME' }, $_->{ 'COMMAND' } ), "\n" foreach @table;
93
94     sleep $options->{ 'time' };
95 }
96
97
98 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
99
100
101 sub process_info_mac
102 {
103     # Martin A. Hansen, June 2009.
104
105     # Get process info for a given process given a PID by
106     # calling ps aux on a Mac.
107
108     # ps aux:
109     # USER       PID %CPU %MEM      VSZ    RSS   TT  STAT STARTED      TIME COMMAND
110
111     my ( $pid,   # process id
112        ) = @_;
113
114     # Returns a hashref
115
116     my ( @processes, $process, @fields, %info_hash, $hour, $min, $ampm );
117
118     @processes = `ps aux`;
119
120     shift @processes;
121
122     foreach $process ( @processes )
123     {
124         @fields = split " ", $process;
125
126         if ( $fields[ 1 ] == $pid )
127         {
128             %info_hash = (
129                 "USER"    => $fields[ 0 ],
130                 "PID"     => $fields[ 1 ],
131                 "%CPU"    => $fields[ 2 ],
132                 "%MEM"    => $fields[ 3 ],
133                 "VSZ"     => $fields[ 4 ],
134                 "RSS"     => $fields[ 5 ],
135                 "TT"      => $fields[ 6 ],
136                 "STAT"    => $fields[ 7 ],
137                 "STARTED" => $fields[ 8 ],
138                 "TIME"    => $fields[ 9 ],
139                 "COMMAND" => $fields[ 10 ],
140             );
141
142             if ( $info_hash{ 'STARTED' } =~ /^(\d+):(\d+)(AM|PM)$/ )
143             {
144                 $hour = $1;
145                 $min  = $2;
146                 $ampm = $3;
147
148                 if ( $ampm eq "PM" ) {
149                     $hour += 12;
150                 }
151
152                 $info_hash{ 'STARTED' } = "$hour:$min";
153             }
154
155             return wantarray ? %info_hash : \%info_hash;
156         }
157     }
158
159     return;
160 }
161
162
163 sub get_status_info
164 {
165     my ( $file,   #
166        ) = @_;
167
168     my ( $fh, $line, $time, $args, $tmp_dir, $du );
169
170     $fh = Maasha::Filesys::file_read_open( $file );
171     $line = <$fh>;
172     close $fh;
173
174     chomp $line;
175
176     ( $time, $args, $tmp_dir ) = split ";", $line;
177
178     if ( $tmp_dir and -d $tmp_dir )
179     {
180         $du = `du -sh $tmp_dir`;
181
182         chomp $du;
183
184         $du =~ s/\s.*//;
185     }
186
187     return wantarray ? ( $time, $args, $du ) : [ $time, $args, $du ];
188 }
189
190
191 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
192
193
194 BEGIN
195 {
196     Maasha::Biopieces::status_set();
197 }
198
199
200 END
201 {
202     Maasha::Biopieces::status_log();
203 }
204
205
206 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
207
208
209 __END__