| 1 | package MYDan::Util::Check::Proc; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | $|++; | ||
| 6 | |||
| 7 | sub new | ||
| 8 | { | ||
| 9 | my ( $class, $col, $type, $name, @condition ) = @_; | ||
| 10 | |||
| 11 | die "not supported.\n" unless $col && ( $col eq 'num' || $col eq 'time' ); | ||
| 12 | die "not supported.\n" unless $type && ( $type eq 'name' || $type eq 'cmdline' || $type eq 'exe' ); | ||
| 13 | die "not supported.\n" unless $name && $name =~ /^[=~].+/; | ||
| 14 | |||
| 15 | $name =~ s/^([=~])//; | ||
| 16 | my $grep = $1 eq '~' ? 1 : 0; | ||
| 17 | |||
| 18 | die "format error.\n" unless @condition; | ||
| 19 | map{ die "format error.\n" unless $_ =~ /^[><=]\d+$/ }@condition; | ||
| 20 | bless +{ grep => $grep, col => $col, type => $type, name => $name, condition => \@condition }, ref $class || $class; | ||
| 21 | } | ||
| 22 | |||
| 23 | sub check | ||
| 24 | { | ||
| 25 | my ( $this, %run, $error ) = @_; | ||
| 26 | my ( $col, $type, $name, $condition ) = @$this{qw( col type name condition )}; | ||
| 27 | |||
| 28 | my %proc = $type eq 'name' ? $this->procByName() : $type eq 'cmdline' ? $this->procByCmdline() : $this->procByExe(); | ||
| 29 | %proc = map{ $_ => +{ %{$proc{$_}}, time => time - $proc{$_} } }grep{ defined $proc{$_}{time} }keys %proc if $col eq 'time'; | ||
| 30 | |||
| 31 | map{ print "pid:$_ name:$proc{$_}{name} time:$proc{$_}{time}\n"; }keys %proc if $run{debug}; | ||
| 32 | |||
| 33 | $error = $col eq 'num' ? $this->checkNum( %proc ) : $this->checkTime( %proc ); | ||
| 34 | } | ||
| 35 | |||
| 36 | sub checkNum | ||
| 37 | { | ||
| 38 | my ( $this, %proc, $error ) = @_; | ||
| 39 | my ( $condition ) = @$this{qw( condition )}; | ||
| 40 | my $len = scalar keys %proc; | ||
| 41 | |||
| 42 | for my $cond ( @$condition ) | ||
| 43 | { | ||
| 44 | next unless $cond =~ /^([><=])\d+$/; | ||
| 45 | my $x = $1 eq '=' ? '=' : ''; | ||
| 46 | |||
| 47 | if( eval "$len$x$cond" ) | ||
| 48 | { | ||
| 49 | print "$len$cond ok\n"; | ||
| 50 | } | ||
| 51 | else | ||
| 52 | { | ||
| 53 | print "$len$cond err\n"; | ||
| 54 | $error ++; | ||
| 55 | } | ||
| 56 | } | ||
| 57 | return $error; | ||
| 58 | } | ||
| 59 | |||
| 60 | sub checkTime | ||
| 61 | { | ||
| 62 | my ( $this, %proc, $error ) = @_; | ||
| 63 | my ( $condition ) = @$this{qw( condition )}; | ||
| 64 | my $len = scalar keys %proc; | ||
| 65 | |||
| 66 | for my $cond ( @$condition ) | ||
| 67 | { | ||
| 68 | next unless $cond =~ /^([><=])\d+$/; | ||
| 69 | my $x = $1 eq '=' ? '=' : ''; | ||
| 70 | |||
| 71 | if( grep{ ! eval "$_->{time}$x$cond" } values %proc ) | ||
| 72 | { | ||
| 73 | print "$cond err\n"; | ||
| 74 | $error ++; | ||
| 75 | } | ||
| 76 | else | ||
| 77 | { | ||
| 78 | print "$cond ok\n"; | ||
| 79 | } | ||
| 80 | } | ||
| 81 | return $error; | ||
| 82 | } | ||
| 83 | |||
| 84 | sub procByCmdline | ||
| 85 | { | ||
| 86 | my $this = shift; | ||
| 87 | my ( $col, $grep, $name, %proc, @proc ) = @$this{qw( col grep name )}; | ||
| 88 | |||
| 89 | map{ push( @proc, $1 ) if m#^/proc/(\d+)$# }glob "/proc/*"; | ||
| 90 | |||
| 91 | for my $proc ( @proc ) | ||
| 92 | { | ||
| 93 | my $x = `cat '/proc/$proc/cmdline' 2>/dev/null`; | ||
| 94 | chomp $x; | ||
| 95 | |||
| 96 | $x =~ s/\0//g; | ||
| 97 | next unless ( ! $grep && $x eq $name ) || ( $grep && $x =~ /$name/ ); | ||
| 98 | |||
| 99 | $proc{$proc}{name} = $x; | ||
| 100 | $proc{$proc}{time} = $col eq 'num' ? 1 : ( stat "/proc/$proc" )[10]; | ||
| 101 | } | ||
| 102 | |||
| 103 | return %proc; | ||
| 104 | } | ||
| 105 | |||
| 106 | sub procByName | ||
| 107 | { | ||
| 108 | my $this = shift; | ||
| 109 | my ( $col, $grep, $name, %proc, @proc ) = @$this{qw( col grep name )}; | ||
| 110 | |||
| 111 | map{ push( @proc, $1 ) if m#^/proc/(\d+)$# }glob "/proc/*"; | ||
| 112 | |||
| 113 | for my $proc ( @proc ) | ||
| 114 | { | ||
| 115 | my @x = `cat '/proc/$proc/status' 2>/dev/null`; | ||
| 116 | chomp @x; | ||
| 117 | |||
| 118 | for( @x ) | ||
| 119 | { | ||
| 120 | my @xx = split /:/, $_, 2; | ||
| 121 | next unless $xx[0] eq 'Name'; | ||
| 122 | |||
| 123 | $xx[1] =~ s/^\s*//; | ||
| 124 | next unless ( ! $grep && $xx[1] eq $name ) || ( $grep && $xx[1] =~ /$name/ ); | ||
| 125 | |||
| 126 | $proc{$proc}{name} = $xx[1]; | ||
| 127 | $proc{$proc}{time} = $col eq 'num' ? 1 : ( stat "/proc/$proc" )[10]; | ||
| 128 | } | ||
| 129 | } | ||
| 130 | |||
| 131 | return %proc; | ||
| 132 | } | ||
| 133 | |||
| 134 | sub procByExe | ||
| 135 | { | ||
| 136 | my $this = shift; | ||
| 137 | my ( $col, $grep, $name, %proc, @proc ) = @$this{qw( col grep name )}; | ||
| 138 | |||
| 139 | map{ push( @proc, $1 ) if m#^/proc/(\d+)$# }glob "/proc/*"; | ||
| 140 | |||
| 141 | for my $proc ( @proc ) | ||
| 142 | { | ||
| 143 | next unless my $x = readlink "/proc/$proc/exe"; | ||
| 144 | |||
| 145 | next unless ( ! $grep && $x eq $name ) || ( $grep && $x =~ /$name/ ); | ||
| 146 | |||
| 147 | $proc{$proc}{name} = $x; | ||
| 148 | $proc{$proc}{time} = $col eq 'num' ? 1 : ( stat "/proc/$proc" )[10]; | ||
| 149 | } | ||
| 150 | |||
| 151 | return %proc; | ||
| 152 | } | ||
| 153 | |||
| 154 | 1; | ||
| 155 | __END__ |