Changeset 17519 for ext

Show
Ignore:
Timestamp:
08/30/07 17:49:33 (17 months ago)
Author:
bloonix
Message:

Added Dumper.pm and fixed a lot of code of Sys::Statistics::Linux*

Location:
ext
Files:
4 added
14 modified

Legend:

Unmodified
Added
Removed
  • ext/Sys-Statistics-Linux/Makefile.pl

    r17226 r17519  
    1 use strict; 
    21use lib "../../", "../../inc"; 
    32use inc::Module::Install prefix => '../../inc'; 
  • ext/Sys-Statistics-Linux/lib/Sys/Statistics/Linux.pm

    r17226 r17519  
    1212 
    1313   $lxs.set( 
    14       SysInfo   => 1, 
    15       CpuStats  => 1, 
    16       ProcStats => 1, 
    17       MemStats  => 1, 
    18       PgSwStats => 1, 
    19       NetStats  => 1, 
    20       SockStats => 1, 
    21       DiskStats => 1, 
    22       DiskUsage => 1, 
    23       LoadAVG   => 1, 
    24       FileStats => 1, 
    25       Processes => 1, 
     14      sysinfo   => 1, 
     15      cpustats  => 1, 
     16      procstats => 1, 
     17      memstats  => 1, 
     18      pgswstats => 1, 
     19      netstats  => 1, 
     20      sockstats => 1, 
     21      diskstats => 1, 
     22      diskusage => 1, 
     23      loadavg   => 1, 
     24      filestats => 1, 
     25      processes => 1, 
    2626   ); 
    2727 
     
    9595In addition it's possible to handoff a process list for option C<Processes>. 
    9696 
    97     my $lxs = Sys::Statistics::Linux->new( 
     97    my $lxs = Sys::Statistics::Linux.new( 
    9898       Processes => { 
    9999          init => 1, 
     
    104104To get more informations about the statistics refer the different modules of the distribution. 
    105105 
    106    SysInfo     -  Collect system informations             with Sys::Statistics::Linux::SysInfo. 
    107    CpuStats    -  Collect cpu statistics                  with Sys::Statistics::Linux::CpuStats. 
    108    ProcStats   -  Collect process statistics              with Sys::Statistics::Linux::ProcStats. 
    109    MemStats    -  Collect memory statistics               with Sys::Statistics::Linux::MemStats. 
    110    PgSwStats   -  Collect paging and swapping statistics  with Sys::Statistics::Linux::PgSwStats. 
    111    NetStats    -  Collect net statistics                  with Sys::Statistics::Linux::NetStats. 
    112    SockStats   -  Collect socket statistics               with Sys::Statistics::Linux::SockStats. 
    113    DiskStats   -  Collect disk statistics                 with Sys::Statistics::Linux::DiskStats. 
    114    DiskUsage   -  Collect the disk usage                  with Sys::Statistics::Linux::DiskUsage. 
    115    LoadAVG     -  Collect the load average                with Sys::Statistics::Linux::LoadAVG. 
    116    FileStats   -  Collect inode statistics                with Sys::Statistics::Linux::FileStats. 
    117    Processes   -  Collect process statistics              with Sys::Statistics::Linux::Processes. 
     106   sysinfo     -  Collect system informations             with Sys::Statistics::Linux::SysInfo. 
     107   cpustats    -  Collect cpu statistics                  with Sys::Statistics::Linux::CpuStats. 
     108   procstats   -  Collect process statistics              with Sys::Statistics::Linux::ProcStats. 
     109   memstats    -  Collect memory statistics               with Sys::Statistics::Linux::MemStats. 
     110   pgswstats   -  Collect paging and swapping statistics  with Sys::Statistics::Linux::PgSwStats. 
     111   netstats    -  Collect net statistics                  with Sys::Statistics::Linux::NetStats. 
     112   sockstats   -  Collect socket statistics               with Sys::Statistics::Linux::SockStats. 
     113   diskstats   -  Collect disk statistics                 with Sys::Statistics::Linux::DiskStats. 
     114   diskusage   -  Collect the disk usage                  with Sys::Statistics::Linux::DiskUsage. 
     115   loadavg     -  Collect the load average                with Sys::Statistics::Linux::LoadAVG. 
     116   filestats   -  Collect inode statistics                with Sys::Statistics::Linux::FileStats. 
     117   processes   -  Collect process statistics              with Sys::Statistics::Linux::Processes. 
    118118 
    119119=head1 METHODS 
     
    126126Without options 
    127127 
    128          my $lxs = Sys::Statistics::Linux->new(); 
     128         my $lxs = Sys::Statistics::Linux.new; 
    129129 
    130130Or with options 
    131131 
    132          my $lxs = Sys::Statistics::Linux->new(CpuStats => 1); 
     132         my $lxs = Sys::Statistics::Linux.new(CpuStats => 1); 
    133133 
    134134Would do nothing 
    135135 
    136          my $lxs = Sys::Statistics::Linux->new(CpuStats => 0); 
    137  
    138 It's possible to call C<new()> with a hash reference of options. 
    139  
    140          my %options = ( 
    141             CpuStats => 1, 
    142             MemStats => 1 
    143          ); 
    144  
    145          my $lxs = Sys::Statistics::Linux->new(\%options); 
     136         my $lxs = Sys::Statistics::Linux.new(CpuStats => 0); 
    146137 
    147138Take a look to C<set()> for more informations. 
     
    153144C<Sys::Statistics::Linux::SysInfo>: 
    154145 
    155          $lxs->set( 
     146         $lxs.set( 
    156147            Processes =>  0, # deactivate this statistic 
    157148            PgSwStats =>  1, # activate the statistic and calls new() and init() if necessary 
     
    159150         ); 
    160151 
    161 It's possible to call C<set()> with a hash reference of options. 
    162  
    163          my %options = ( 
    164             CpuStats => 2, 
    165             MemStats => 2 
    166          ); 
    167  
    168          $lxs->set(\%options); 
    169  
    170152=head2 get() 
    171153 
    172154Call C<get()> to get the collected statistics. C<get()> returns the statistics as a hash reference. 
    173155 
    174          my $stats = $lxs->get; 
     156         my $stats = $lxs.get; 
    175157 
    176158=head2 init() 
     
    179161higher than 0. 
    180162 
    181          $lxs->init; 
     163         $lxs.init; 
    182164 
    183165=head2 search(), psfind() 
    184166 
    185 Both methods provides a simple scan engine to find special statistics. Both methods except a filter 
    186 as a hash reference as the first argument. If your data comes from extern - maybe from a client that 
    187 send his statistics to the server - you can set the statistics as the second argument. The second 
    188 argument have to be a hash reference as well. 
    189  
    190 The method C<search()> scans for statistics and rebuilds the hash tree until that keys that matched 
    191 your filter and returns the hits as a hash reference. 
    192  
    193         my $hits = $lxs->search({ 
    194            Processes => { 
    195               cmd   => qr/\[su\]/, 
    196               owner => qr/root/ 
    197            }, 
    198            CpuStats => { 
    199               idle   => 'lt:10', 
    200               iowait => 'gt:10' 
    201            }, 
    202            DiskUsage => { 
    203               '/dev/sda1' => { 
    204                  usageper => 'gt:80' 
    205               } 
    206            } 
    207         }); 
    208  
    209 This would return the following matches: 
    210  
    211     * processes with the command "[su]" 
    212     * processes with the owner "root" 
    213     * all cpu where "idle" is less than 50 
    214     * all cpu where "iowait" is grather than 10 
    215     * only disk '/dev/sda1' if "usageper" is grather than 80 
    216  
    217 If the statistics are not gathered by the current process then you can handoff statistics as an 
    218 argument. 
    219  
    220         my %stats = ( 
    221            CpuStats => { 
    222               cpu => { 
    223                  system => '51.00', 
    224                  total  => '51.00', 
    225                  idle   => '49.00', 
    226                  nice   => '0.00', 
    227                  user   => '0.00', 
    228                  iowait => '0.00' 
    229               } 
    230            } 
    231         ); 
    232                 
    233         my %filter = ( 
    234            CpuStats => { 
    235               total => 'gt:50' 
    236            } 
    237         ); 
    238  
    239         my $hits = $lxs->search(\%filter, \%stats); 
    240  
    241 The method C<psfind()> scans for processes only and returns a array reference with all process 
    242 IDs that matched the filter. Example: 
    243  
    244         my $pids = $lxs->psfind({ cmd => qr/init/, owner => 'eq:apache' }); 
    245  
    246 You can handoff the statistics as second argument as well. 
    247  
    248         my $pids = $lxs->psfind(\%filter, \%stats); 
    249  
    250 This would return the following process ids: 
    251  
    252     * processes that matched the command "init" 
    253     * processes with the owner "apache" 
    254  
    255 There are different match operators available: 
    256  
    257     gt  -  grather than 
    258     lt  -  less than 
    259     eq  -  is equal 
    260     ne  -  is not equal 
    261  
    262 Notation examples: 
    263  
    264     gt:50 
    265     lt:50 
    266     eq:50 
    267     ne:50 
    268  
    269 Both argumnents have to be set as a hash reference. 
    270  
    271 Note: the operators < > = ! are not available any more. It's possible that in further releases 
    272 could be different changes for C<search()> and C<psfind()>. So please take a look to the  
    273 documentation if you use it. 
     167NOT YET IMPLEMENTED 
    274168 
    275169=head2 settime() 
    276170 
    277 Call C<settime()> to define a POSIX formatted time stamp, generated with localtime(). 
    278  
    279          $lxs->settime('%Y/%m/%d %H:%M:%S'); 
    280  
    281 To get more informations about the formats take a look at C<strftime()> of POSIX.pm 
    282 or the manpage C<strftime(3)>. 
     171NOT YET IMPLEMENTED 
    283172 
    284173=head2 gettime() 
    285174 
    286 C<gettime()> returns a POSIX formatted time stamp, @foo in list and $bar in scalar context. 
    287 If the time format isn't set then the default format "%Y-%m-%d %H:%M:%S" will be set 
    288 automatically. You can also set a time format with C<gettime()>. 
    289  
    290          my $date_time = $lxs->gettime; 
    291  
    292 Or 
    293  
    294          my ($date, $time) = $lxs->gettime; 
    295  
    296 Or 
    297  
    298          my ($date, $time) = $lxs->gettime('%Y/%m/%d %H:%M:%S'); 
     175NOT YET IMPLEMENTED 
    299176 
    300177=head1 EXAMPLES 
     
    302179A very simple perl script could looks like this: 
    303180 
    304          use warnings; 
    305          use strict; 
    306181         use Sys::Statistics::Linux; 
    307182 
    308          my $lxs = Sys::Statistics::Linux->new( CpuStats => 1 ); 
     183         my $lxs = Sys::Statistics::Linux.new( CpuStats => 1 ); 
    309184         sleep(1); 
    310          my $stats = $lxs->get; 
    311          my $cpu   = $stats->{CpuStats}->{cpu}; 
    312  
    313          print "Statistics for CpuStats (all)\n"; 
    314          print "  user      $cpu->{user}\n"; 
    315          print "  nice      $cpu->{nice}\n"; 
    316          print "  system    $cpu->{system}\n"; 
    317          print "  idle      $cpu->{idle}\n"; 
    318          print "  ioWait    $cpu->{iowait}\n"; 
    319          print "  total     $cpu->{total}\n"; 
    320  
    321 Set and get a time stamp: 
    322  
    323          use warnings; 
    324          use strict; 
    325          use Sys::Statistics::Linux; 
    326  
    327          my $lxs = Sys::Statistics::Linux->new(); 
    328          $lxs->settime('%Y/%m/%d %H:%M:%S'); 
    329          print "$lxs->gettime\n"; 
    330  
    331 If you're not sure you can use the the C<Data::Dumper> module to learn more about the hash structure: 
    332  
    333          use warnings; 
    334          use strict; 
    335          use Sys::Statistics::Linux; 
    336          use Data::Dumper; 
    337  
    338          my $lxs = Sys::Statistics::Linux->new( CpuStats => 1 ); 
    339          sleep(1); 
    340          my $stats = $lxs->get; 
    341  
    342          print Dumper($stats); 
    343  
    344 How to get processes with the highest cpu workload: 
    345  
    346          use warnings; 
    347          use strict; 
    348          use Sys::Statistics::Linux; 
    349  
    350          my $lxs = Sys::Statistics::Linux->new( Processes => 1 ); 
    351          sleep(1); 
    352          my $stats = $lxs->get; 
    353          my $procs = $stats->{Processes}; 
    354  
    355          my @top5 = ( 
    356             map  { $_->[0] } 
    357             reverse sort { $a->[1] <=> $b->[1] } 
    358             map  { [ $_, $procs->{$_}->{ttime} ] } keys %{$procs} 
    359          )[0..4]; 
     185         my %stats  = $lxs.get; 
     186         my %cpu   := %stats<CpuStats><cpu>; 
     187 
     188         say "Statistics for CpuStats (all)\n"; 
     189         say "  user      %cpu<user>"; 
     190         say "  nice      %cpu<nice>"; 
     191         say "  system    %cpu<system>"; 
     192         say "  idle      %cpu<idle>"; 
     193         say "  ioWait    %cpu<iowait>"; 
     194         say "  total     %cpu<total>"; 
    360195 
    361196=head1 DEPENDENCIED 
    362197 
    363     UNIVERSAL 
    364     UNIVERSAL::require 
    365     Test::More 
    366     Carp 
     198No at the moment. 
    367199 
    368200=head1 EXPORTS 
     
    388220This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. 
    389221 
    390 =end pod 
     222=cut 
    391223 
    392224#package Sys::Statistics::Linux; 
     
    432264#} 
    433265 
    434 has Hash $.inits    = { }; 
    435 has Hash $.stats    = { }; 
    436 has Hash $.objects  = { }; 
    437 has Hash $.regmaps  = { }; 
    438 has Hash $.options  = { }; 
     266has %.inits; 
     267has %.stats; 
     268has %.objects; 
     269has %.regmaps; 
     270has %.options; 
    439271 
    440272submethod BUILD (*%p) { 
     273    #               alias               module name               init() 
    441274    self.register('sysinfo',   'Sys::Statistics::Linux::SysInfo',   0); 
    442275    self.register('cpustats',  'Sys::Statistics::Linux::CpuStats',  1); 
     
    456289method register ($a, $m, $i) { 
    457290    self.regmaps{$a} = { class => $m, init => $i }; 
    458     self.options{$a}  = 0; 
     291    self.options{$a} = 0; 
    459292} 
    460293 
     
    524357            croak("invalid value for option '$k'"); 
    525358        } 
    526         %options{$k} = $v; 
    527359        if $v { 
    528360            if !%objects.exists($k) { 
     
    536368            self.stats{$k}.delete; 
    537369        } 
     370        %options{$k} = $v; 
    538371    } 
    539372} 
  • ext/Sys-Statistics-Linux/lib/Sys/Statistics/Linux/CpuStats.pm

    r17454 r17519  
    127127#} 
    128128 
    129 has Hash $.files = {}; 
    130 has Hash $.inits = {}; 
    131 has Hash $.stats = {}; 
     129has %.files; 
     130has %.inits; 
     131has %.stats; 
    132132 
    133133submethod BUILD () { 
     
    162162    self.stats = self.load; 
    163163    self.deltas(); 
    164     return self.stats; 
     164    # "return self.stats" will return a Hash::Const, for this reason 
     165    # I return %stats, then it returns a Hash 
     166    my %stats := self.stats; 
     167    return %stats; 
    165168} 
    166169 
  • ext/Sys-Statistics-Linux/lib/Sys/Statistics/Linux/DiskStats.pm

    r17454 r17519  
    135135#} 
    136136 
    137 has Hash $.files = {}; 
    138 has Hash $.inits = {}; 
    139 has Hash $.stats = {}; 
    140 has Int  $.uptime; 
    141 has Int  $.blocksize = 512; 
     137has %.files; 
     138has %.inits; 
     139has %.stats; 
     140has Int $.uptime; 
     141has Int $.blocksize = 512; 
    142142 
    143143submethod BUILD () { 
     
    176176    self.stats = self.load; 
    177177    self.deltas(); 
    178     return self.stats; 
     178    # "return self.stats" will return a Hash::Const, for this reason 
     179    # I return %stats, then it returns a Hash 
     180    my %stats := self.stats; 
     181    return %stats; 
    179182} 
    180183 
  • ext/Sys-Statistics-Linux/lib/Sys/Statistics/Linux/DiskUsage.pm

    r17454 r17519  
    115115#} 
    116116 
    117 has Hash $.cmd = {}; 
     117has %.cmd; 
    118118 
    119119submethod BUILD () { 
  • ext/Sys-Statistics-Linux/lib/Sys/Statistics/Linux/FileStats.pm

    r17454 r17519  
    120120#} 
    121121 
    122 has Hash $.files = {}; 
     122has %.files; 
    123123 
    124124submethod BUILD () { 
     
    157157 
    158158method get () { 
    159     my $file_nr  = self.files.<file_nr>; 
    160     my $inode_nr = self.files.<inode_nr>; 
    161     my $dentries = self.files.<dentries>; 
     159    my $file_nr  = self.files<file_nr>; 
     160    my $inode_nr = self.files<inode_nr>; 
     161    my $dentries = self.files<dentries>; 
    162162    my (%stats, $line); 
    163163 
  • ext/Sys-Statistics-Linux/lib/Sys/Statistics/Linux/LoadAVG.pm

    r17454 r17519  
    110110#} 
    111111 
    112 has Hash $.files = {}; 
     112has %.files; 
    113113 
    114114submethod BUILD () { 
  • ext/Sys-Statistics-Linux/lib/Sys/Statistics/Linux/MemStats.pm

    r17454 r17519  
    129129#} 
    130130 
    131 has Hash $.files = {}; 
     131has %.files; 
    132132 
    133133submethod BUILD () { 
  • ext/Sys-Statistics-Linux/lib/Sys/Statistics/Linux/NetStats.pm

    r17454 r17519  
    141141#} 
    142142 
    143 has Hash $.files = {}; 
    144 has Hash $.inits = {}; 
    145 has Hash $.stats = {}; 
    146 has Hash $.uptime; 
     143has %.files; 
     144has %.inits; 
     145has %.stats; 
     146has Int $.uptime; 
    147147 
    148148submethod BUILD () { 
     
    180180    self.stats = self.load; 
    181181    self.deltas(); 
    182     return self.stats; 
     182    # "return self.stats" will return a Hash::Const, for this reason 
     183    # I return %stats, then it returns a Hash 
     184    my %stats := self.stats; 
     185    return %stats; 
    183186} 
    184187 
     
    308311#} 
    309312 
     313my method uptime () { 
     314    my $file = self.files<uptime>; 
     315    my $upfh = open($file, :r) or croak("unable to open $file: $!"); 
     316    my ($up, $idle) = $upfh.readline.comb; 
     317    $upfh.close; 
     318    return $up; 
     319} 
     320 
    3103211; 
  • ext/Sys-Statistics-Linux/lib/Sys/Statistics/Linux/PgSwStats.pm

    r17454 r17519  
    127127#} 
    128128 
    129 has Hash $.files = {}; 
    130 has Hash $.inits = {}; 
    131 has Hash $.stats = {}; 
    132 has Int  $.uptime; 
     129has %.files; 
     130has %.inits; 
     131has %.stats; 
     132has Int $.uptime; 
    133133 
    134134submethod BUILD () { 
     
    167167    self.stats = self.load; 
    168168    self.deltas(); 
    169     return self.stats; 
     169    # "return self.stats" will return a Hash::Const, for this reason 
     170    # I return %stats, then it returns a Hash 
     171    my %stats := self.stats; 
     172    return %stats; 
    170173} 
    171174 
  • ext/Sys-Statistics-Linux/lib/Sys/Statistics/Linux/ProcStats.pm

    r17454 r17519  
    107107sub croak (*@m) { die @m } # waiting for Carp::croak 
    108108 
    109 has Hash $.files = {}; 
    110 has Hash $.inits = {}; 
    111 has Hash $.stats = {}; 
    112 has Int  $.uptime; 
     109has %.files; 
     110has %.inits; 
     111has %.stats; 
     112has Int $.uptime; 
    113113 
    114114#sub new { 
     
    159159    self.stats = self.load; 
    160160    self.deltas(); 
    161     return self.stats; 
     161    # "return self.stats" will return a Hash::Const, for this reason 
     162    # I return %stats, then it returns a Hash 
     163    my %stats := self.stats; 
     164    return %stats; 
    162165} 
    163166 
  • ext/Sys-Statistics-Linux/lib/Sys/Statistics/Linux/Processes.pm

    r17453 r17519  
    136136This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. 
    137137 
    138 =cut 
     138=end pod 
    139139 
    140140#package Sys::Statistics::Linux::Processes; 
     
    177177#} 
    178178 
    179 has Hash  $.files = {}; 
    180 has Hash  $.inits = {}; 
    181 has Hash  $.stats = {}; 
     179has %.files; 
     180has %.inits; 
     181has %.stats; 
     182 
     183#has @.pids is rw; # this doesnt run nice 
     184# this doesnt run nice and occurs the error 
     185#   "Can't modify constant item" 
     186# if I try 
     187#   "self.pids = @p" 
     188 
    182189has Array $.pids; 
    183 has Int   $.i_uptime; 
    184 has Int   $.s_uptime; 
    185  
    186 submethod BUILD (@pids) { 
     190has Int $.i_uptime; 
     191has Int $.s_uptime; 
     192 
     193multi submethod BUILD () { 
     194    self.files<basedir uptime p_stat p_statm p_status p_cmdline> = 
     195        ('/proc', '/proc/uptime', 'stat', 'statm', 'status', 'cmdline'); 
     196} 
     197 
     198multi submethod BUILD (@pids) { 
    187199    for @pids -> $pid { 
    188200        unless $pid ~~ /^\d+$/ { 
     
    190202        } 
    191203    } 
     204 
    192205    self.files<basedir uptime p_stat p_statm p_status p_cmdline> = 
    193206        ('/proc', '/proc/uptime', 'stat', 'statm', 'status', 'cmdline');