403Webshell
Server IP : 172.67.216.182  /  Your IP : 172.70.92.246
Web Server : Apache
System : Linux krdc-ubuntu-s-2vcpu-4gb-amd-blr1-01.localdomain 5.15.0-142-generic #152-Ubuntu SMP Mon May 19 10:54:31 UTC 2025 x86_64
User : www ( 1000)
PHP Version : 7.4.33
Disable Function : passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : OFF  |  Sudo : ON  |  Pkexec : ON
Directory :  /usr/share/doc/libproc-processtable-perl/examples/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /usr/share/doc/libproc-processtable-perl/examples/pctcpu_example.pl
#!/usr/bin/perl
# created on 2018-02-12

use warnings;
use strict;
use v5.11;
use Proc::ProcessTable;
use Time::HiRes;

my @time_spans;
my %cpu_times;

my $ppt        = Proc::ProcessTable->new;
my $start_time = [ Time::HiRes::gettimeofday() ];

my $poll_intervall = int( 2 * 1000 * 1000 );    # 2 sec
my $num_steps      = 2;

local $| = 1;

my $cur_step = 0;
while (1) {


  # current and old index of array.
  # because of modulo, the idcs are rotated.
  # the old index is always the oldest recorded entry
  # this means the entry *after* the current index
  #[ c o . . . ]
  #[ . c o . . ]
  #[ . . c o . ]
  #[ . . . c o ]
  #[ o . . . c ]
  my $cur_idx = $cur_step % $num_steps;
  my $old_idx = ( $cur_step + 1 ) % $num_steps;

  # calc pct cpu per interval:
  # we need seconds since
  # https://stackoverflow.com/questions/16726779/how-do-i-get-the-total-cpu-usage-of-an-application-from-proc-pid-stat

  my $t = Time::HiRes::tv_interval($start_time);
  my $pt           = $ppt->table;

  my %active_procs = ();
  for my $p (@$pt) {
    # init process info if non-existent
    my $pid = $p->pid;
    $active_procs{$pid} = 1;
    $cpu_times{$pid} //= [];

    $cpu_times{$pid}[$cur_idx] = $p->time;
  }
  $time_spans[$cur_idx] = $t;

  # clean the cputimes hash
  my @killed_procs = grep { !$active_procs{$_} } keys %cpu_times ;
  delete @cpu_times{@killed_procs};

  my $ratio = 0;
  # make sure the array is filled with cpu times
  if ( $cur_step >= $num_steps ) {

    # move cursor to top left of screen
    print "\033[2J";

    printf( "%-5s %s\n", "cpu", "pid");
    # show cpu usage per process
    for my $pid (keys %cpu_times) {
      my $cpu_time   = $cpu_times{$pid};
      my $diff_cpu   = ( $cpu_time->[$cur_idx] - ( $cpu_time->[$old_idx] // 0 ) ) / 1e6;
      my $diff_start = ( $time_spans[$cur_idx] - $time_spans[$old_idx] );
      $ratio = $diff_cpu / $diff_start if ( $diff_start > 0 );
      # show only processes that have a usage > 1%
      printf( "%5.1f %s\n", $ratio * 100, $pid ) if ( $ratio > 0.01 );
    }
  }

  $cur_step++;
  Time::HiRes::usleep($poll_intervall);
}

Youez - 2016 - github.com/yon3zu
LinuXploit