annotate vz_ps.pl @ 5:464dea07deb8 default tip

Add vzps replacements.
author Peter Gervai <grin@grin.hu>
date Wed, 07 May 2014 16:23:45 +0200
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5
464dea07deb8 Add vzps replacements.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
1 #!/usr/bin/perl
464dea07deb8 Add vzps replacements.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
2 # $Id: vz_ps.pl,v a19295214a68 2013/10/29 15:31:42 grin $
464dea07deb8 Add vzps replacements.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
3 # (c) Petre Gervai, 2011-2013
464dea07deb8 Add vzps replacements.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
4 # Released under CreativeCommons Attribution-ShareAlike v3.0 unported
464dea07deb8 Add vzps replacements.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
5 # CC-BY-SA-3.0-unp
464dea07deb8 Add vzps replacements.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
6 #
464dea07deb8 Add vzps replacements.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
7 # Show OpenVZ info using stock ps output instead of hacking it in
464dea07deb8 Add vzps replacements.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
8 #
464dea07deb8 Add vzps replacements.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
9
464dea07deb8 Add vzps replacements.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
10 use warnings;
464dea07deb8 Add vzps replacements.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
11 use strict;
464dea07deb8 Add vzps replacements.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
12 use IO::File;
464dea07deb8 Add vzps replacements.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
13
464dea07deb8 Add vzps replacements.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
14 my $ps = "/bin/ps";
464dea07deb8 Add vzps replacements.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
15
464dea07deb8 Add vzps replacements.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
16 my $f;
464dea07deb8 Add vzps replacements.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
17
464dea07deb8 Add vzps replacements.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
18 if( $#ARGV == 0 ) {
464dea07deb8 Add vzps replacements.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
19 # use 'spawn ps' mode
464dea07deb8 Add vzps replacements.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
20 $f = new IO::File 'ps ' . join(' ', @ARGV) . " |" or die $!;
464dea07deb8 Add vzps replacements.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
21
464dea07deb8 Add vzps replacements.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
22 } else {
464dea07deb8 Add vzps replacements.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
23 # get data on stdin
464dea07deb8 Add vzps replacements.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
24 $f = IO::Handle->new_from_fd( fileno(STDIN), 'r' );
464dea07deb8 Add vzps replacements.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
25 }
464dea07deb8 Add vzps replacements.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
26
464dea07deb8 Add vzps replacements.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
27 # get header
464dea07deb8 Add vzps replacements.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
28 my $hdr = <$f>;
464dea07deb8 Add vzps replacements.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
29 if( $hdr !~ /PID/ ) {
464dea07deb8 Add vzps replacements.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
30 print "Error: ps header doesn't contain PID, it's something I cannot accept. I'm going.\n";
464dea07deb8 Add vzps replacements.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
31 exit 1;
464dea07deb8 Add vzps replacements.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
32 }
464dea07deb8 Add vzps replacements.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
33
464dea07deb8 Add vzps replacements.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
34 my $pos;
464dea07deb8 Add vzps replacements.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
35 $hdr =~ s/^\s*(.+?)\s*$/$1/; # remove head/trail spc
464dea07deb8 Add vzps replacements.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
36 my @head = split /\s+/, $hdr;
464dea07deb8 Add vzps replacements.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
37 while( my ($i,$val) = each @head ) {
464dea07deb8 Add vzps replacements.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
38 if( $val=~ /^PID$/ ) {
464dea07deb8 Add vzps replacements.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
39 $pos = $i;
464dea07deb8 Add vzps replacements.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
40 last;
464dea07deb8 Add vzps replacements.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
41 }
464dea07deb8 Add vzps replacements.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
42 }
464dea07deb8 Add vzps replacements.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
43
464dea07deb8 Add vzps replacements.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
44 use Data::Dumper;
464dea07deb8 Add vzps replacements.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
45 print Dumper( \@head, $pos );
464dea07deb8 Add vzps replacements.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
46 die;
464dea07deb8 Add vzps replacements.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
47
464dea07deb8 Add vzps replacements.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
48 while( <$f> ) {
464dea07deb8 Add vzps replacements.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
49
464dea07deb8 Add vzps replacements.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
50 }