File: sun-ps

package info (click to toggle)
libsnmp-session-perl 1.14~git20221124T101957-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,104 kB
  • sloc: perl: 11,920; ansic: 25; makefile: 15
file content (69 lines) | stat: -rwxr-xr-x 2,120 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/local/bin/perl -w
##
## Print the SNMP-accessibe portion of a Sun's process table
## in a manner similar to ps -ef.
##
## Uses the "sun-snmp" MIB according to /var/snmp/mibs/sun.mib in
## Solstice Enterprise Agents.
##
use strict;

use SNMP_Session;
use BER;

my $host = shift @ARGV || usage (1);
my $community = shift @ARGV || 'public';

my $psProcessID = [1,3,6,1,4,1,42,3,12,1,1,1];
my $psParentProcessID = [1,3,6,1,4,1,42,3,12,1,1,2];
my $psProcessSize = [1,3,6,1,4,1,42,3,12,1,1,3];
my $psProcessCpuTime = [1,3,6,1,4,1,42,3,12,1,1,4];
my $psProcessState = [1,3,6,1,4,1,42,3,12,1,1,5];
my $psProcessWaitChannel = [1,3,6,1,4,1,42,3,12,1,1,6];
my $psProcessTTY = [1,3,6,1,4,1,42,3,12,1,1,7];
my $psProcessUserName = [1,3,6,1,4,1,42,3,12,1,1,8];
my $psProcessUserID = [1,3,6,1,4,1,42,3,12,1,1,9];
my $psProcessProcessName = [1,3,6,1,4,1,42,3,12,1,1,10];
my $psProcessProcessStatus = [1,3,6,1,4,1,42,3,12,1,1,11];

my $session = SNMP_Session->open ($host, $community, 161);
$session->map_table ([$psProcessID, $psParentProcessID,
		      $psProcessSize, $psProcessCpuTime,
		      $psProcessState, $psProcessWaitChannel,
		      $psProcessTTY, $psProcessUserName,
		      $psProcessUserID, $psProcessProcessName,
		      $psProcessProcessStatus],
		     \&out_process)
    || warn "Problem walking process table";
$session->close ()
    || warn "Problem closing SNMP_Session";
1;

sub out_process ($$$$$$$$$$$$) {
    my ($index, $pid, $ppid, $size, $cpu_time,
	$status, $wchan, $tty, $user_name, $user_id,
	$name, $process_status);
    $index = shift @_;
    grep (defined $_ && ($_=pretty_print $_), @_);
    ($pid, $ppid, $size, $cpu_time,
     $status, $wchan, $tty, $user_name, $user_id,
     $name, $process_status) = @_;
    die "PID inconsistent"
	unless $index == $pid;
    printf STDOUT ("%8s %5d %5d %5s %-8s %-20.20s\n",
		   $user_name,
		   $pid, $ppid,
		   pretty_cpu_time ($cpu_time),
		   $tty,
		   $name);
}

sub pretty_cpu_time ($) {
    my ($time) = @_;
    sprintf ("%2d:%02d", $time / 60, $time % 60);
}

sub usage ($) {
    warn "usage: $0 host [community]\n";
    exit $_[0] if $_[0];
}