BVB Source Codes

Gource Show gource-ps.pl Source code

Return Download Gource: download gource-ps.pl Source code - Download Gource Source code - Type:.pl
  1. #!/usr/bin/perl
  2. #poll ps and convert changes into Gource format.
  3.  
  4. #usage:
  5. #    gource-ps.pl [[USER@]SERVER] | gource --log-format custom -
  6. #
  7. # (requires Gource 0.27 or later as needs working STDIN support)
  8.  
  9. use strict;
  10. use warnings;
  11. use Getopt::Long qw(GetOptions);
  12.  
  13. #use Data::Dumper;
  14.  
  15. my %opt = ();
  16.  
  17. if(!GetOptions(\%opt, 'debug|d')) {
  18.     die("usage: gource-ps.pl [[USER\@]SERVER] | gource --log-format custom - ...\n");
  19. }
  20.  
  21. my $windows = $^O =~ /win32|msys/i;
  22.  
  23. if($windows) {
  24.     require Win32::Process::Info;
  25.     Win32::Process::Info->import();
  26. }
  27.  
  28. my($server) = @ARGV;
  29. my $user = $ENV{USER};
  30.  
  31. if($server && $server =~ /(.+)\@(.+)/) {
  32.     $user = $1;
  33.     $server = $2;
  34. }
  35.  
  36. die('remote ps via ssh unimplemented on Win32') if $windows && $server;
  37.  
  38. $|=1;
  39.  
  40. my %process;
  41.  
  42. sub _proc_list {
  43.  
  44.     my $ps_command =  'ps axo pid,ppid,user,time,comm';
  45.  
  46.     my @pslist = $server ? `ssh $user\@$server "$ps_command"` : `$ps_command`;
  47.  
  48.     if($?) {
  49.         die("ps command failed: $!\n");
  50.     }
  51.  
  52.     shift @pslist;
  53.  
  54.     chomp(@pslist);
  55.  
  56.     my @stack;
  57.  
  58.     #build process tree
  59.     foreach my $line (@pslist) {
  60.         $line =~ s/^\s+//;
  61.  
  62.         my ($pid, $ppid, $username, $time, @command) = split(/\s+/, $line);
  63.  
  64.         my $command = join(' ', @command) || '';
  65.         $command =~ s{^.+/}{}g;
  66.  
  67.         my $proc;
  68.  
  69.         if($proc = $process{$pid}) {
  70.             $proc->{status} = ($proc->{'time'} ne $time) ? 'M' : '';
  71.         } else {
  72.             $proc = {
  73.                 pid      => $pid,
  74.                 ppid     => $ppid,
  75.                 username => $username,
  76.                 command  => $command,
  77.                 'time'   => $time,
  78.                 status   => 'A',
  79.             };
  80.  
  81.             $process{$pid} = $proc;
  82.         }
  83.  
  84.         #warn Dumper($process{$pid});
  85.     }
  86. }
  87.  
  88. sub _win32_proc_list {
  89.     my $pi = Win32::Process::Info->new ();
  90.  
  91.     foreach my $winproc ($pi->GetProcInfo) {
  92.  
  93. #        use Data::Dumper;
  94. #        print Dumper($proc);
  95.  
  96.         my $pid      = $winproc->{ProcessId};
  97.         my $ppid     = $winproc->{ParentProcessId};
  98.         my $command  = $winproc->{Description};
  99.         my $username = $winproc->{Owner} || 'System';
  100.         my $time     = $winproc->{UserModeTime};
  101.  
  102.         $username =~ s/^.+\\//;              
  103.         $command  =~ s/\.exe$//i;
  104.  
  105.         next unless $command && $pid && $username;
  106.  
  107.         my $proc;
  108.  
  109.         if($proc = $process{$pid}) {
  110.             $proc->{status} = ($proc->{'time'} ne $time) ? 'M' : '';
  111.         } else {
  112.             $proc = {
  113.                 pid      => $pid,
  114.                 ppid     => $ppid,
  115.                 username => $username,
  116.                 command  => $command,
  117.                 'time'   => $time,
  118.                 status   => 'A',
  119.             };
  120.  
  121.             $process{$pid} = $proc;
  122.         }
  123.  
  124.  
  125.     }
  126. }
  127.  
  128. sub _make_command_path {
  129.     my $proc = shift;
  130.  
  131.     my @path;
  132.  
  133.     my $node = $proc;
  134.  
  135.     while($node) {
  136.         push @path, $node->{pid} . '.' . $node->{command};
  137.         $node = $node->{ppid} ? $process{$node->{ppid}} : 0;
  138.     }
  139.  
  140.     return join('/', reverse @path);
  141. }
  142.  
  143. while(1) {
  144.  
  145.     my @proclist = $windows ? _win32_proc_list : _proc_list;
  146.  
  147.     my @filter_pids;
  148.  
  149.     #filter ps / gource processes and parent processes owned by this user
  150.     foreach my $pid (sort {$a <=> $b} keys %process) {
  151.         my $proc = $process{$pid};
  152.  
  153.         next unless $proc;
  154.  
  155.         # delete ps process and parents of
  156.         if($proc->{command} eq 'ps' && $proc->{username} eq $user || $proc->{command} eq 'gource') {
  157.             while($proc) {
  158.                 push @filter_pids, $proc->{pid};
  159.                 $proc = $proc->{ppid} ? $process{$proc->{ppid}} : undef;
  160.             }
  161.         }
  162.     }
  163.  
  164.     delete $process{$_} for @filter_pids;
  165.  
  166.     my @expired_pids;
  167.  
  168.     my $current_time = time;
  169.  
  170.     foreach my $pid (sort {$a <=> $b} keys %process) {
  171.  
  172.         my $proc = $process{$pid};
  173.  
  174.         if($proc->{status}) {
  175.             my $output_line = join('|', $current_time, $proc->{username}, $proc->{status}, _make_command_path($proc)). "\n";
  176.             print $output_line;
  177.             print STDERR $output_line if $opt{debug};
  178.         }
  179.  
  180.         #delete if not seen next time
  181.         if($proc->{status} eq 'D') {
  182.             push @expired_pids,  $pid;
  183.         } else {
  184.             $proc->{status} = 'D';
  185.         }
  186.     }
  187.  
  188.     #remove expired pids
  189.     delete $process{$_} for @expired_pids;
  190.  
  191.     sleep(1);
  192. }
  193.  
  194.  
downloadgource-ps.pl Source code - Download Gource Source code
Related Source Codes/Software:
FDFullscreenPopGesture - A UINavigationController's category to enable full... 2017-01-10
node-style-guide - A guide for styling your node.js / JavaScript code... 2017-01-09
ImageOptim - GUI image optimizer for Mac htt... 2017-01-09
falcon - Falcon is a low-level, high-performance Python fra... 2017-01-09
prerender - Node server that uses phantomjs to render a javasc... 2017-01-09
che - Eclipse Che: Next-generation Eclipse IDE. Open sou... 2017-01-10
notepad-plus-plus - Notepad++ official repository h... 2017-01-10
waterline - An adapter-based ORM for Node.js with support for ... 2017-01-10
Workerman - An asynchronous event driven PHP framework for eas... 2017-01-10
structor - An advanced visual editor for React components ... 2017-01-10
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top