-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprocmon.pl
More file actions
executable file
·198 lines (159 loc) · 4.43 KB
/
procmon.pl
File metadata and controls
executable file
·198 lines (159 loc) · 4.43 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/perl
#
# Monitors processes in /proc, can:
# a) output process state in CSV
# b) insert into a sqlite database.
#
# All processes will be traced or you can choose to trig on a
# particular state.
use strict;
use POSIX qw(strftime);
use DBI;
use Getopt::Long;
use Time::HiRes qw(usleep);
my $TRIGGER_MODE = 0;
my $TRIGGER_STATE = 'D';
my $INTERVAL_TIME = 1;
# 0 -> CSV file to stdout
# 1 -> sqlite database
my $OUTPUT_MODE = 0;
# sqlite filename
my $SQLITE_FILENAME = "ps.db";
my $HELP;
GetOptions('trigger|t' => \$TRIGGER_MODE,
'trigger-state|s=s' => \$TRIGGER_STATE,
'interval|i=s' => \$INTERVAL_TIME,
'sqlmode|m' => \$OUTPUT_MODE,
'dbfile|d=s' => \$SQLITE_FILENAME,
'help|h' => \$HELP,
);
if ($HELP) {
print <<'USAGE';
usage: procmon.pl --trigger|t --trigger-state|s --interval|i --sqlmode|m --dbfile|d --help|h
--trigger, -t: Trigger mode, only output when process state is a certain value, default is OFF
--trigger-state, -s: Process state to trigger on, default is "D"
--interval, -i: how often to refresh, default is 1 (second)
--sqlmode, -m: Instead of CSV output to STDOUT, output to sqlite3 db
--dbfile, -d: sqlite3 filename
--help, -h: duh.
USAGE
exit;
}
my %WANT_FIELDS =
(Name => 1,
State => 1,
Pid => 1,
VmSize => 1,
VmPeak => 1,
VmRSS => 1,
VmData => 1,
VmStk => 1,
VmExe => 1,
VmLib => 1,
VmSwap => 1,
Threads => 1,
);
use constant COMMIT_LIMIT => 500;
sub get_handle {
my ($filename) = @_;
return DBI->connect("dbi:SQLite:$filename","","",
{ AutoCommit => 0 });
}
sub create_table {
my ($dbh) = @_;
$dbh->do(<<'CREATE_TABLE');
CREATE TABLE ps
(id integer primary key,
pid varchar(8),
start time,
name varchar(32),
state varchar(1),
vmsize integer,
vmpeak integer,
vmrss integer,
vmdata integer,
vmstk integer,
vmexe integer,
vmlib integer,
vmswap integer,
threads integer,
wchan varchar(64)
);
CREATE_TABLE
}
my $insert_handle;
sub prepare_insert {
my ($dbh) = @_;
$insert_handle = $dbh->prepare(<<'INSERT_QUERY');
INSERT INTO ps (pid, start, name, state, vmsize, vmpeak, vmrss, vmdata, vmstk, vmexe, vmlib, vmswap, threads, wchan)
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?);
INSERT_QUERY
return $insert_handle;
}
sub insert_row {
my ($sth, $info) = @_;
$sth->execute(@{$info}{qw(Pid Time Name State
VmSize VmPeak VmRSS VmData VmStk VmExe VmLib VmSwap
Threads Wchan)});
}
sub csv_output {
my ($info) = @_;
print join(",", @{ $info }{qw(Time Pid Name State VmSize VmPeak Threads Wchan)}), "\n";
}
sub main {
my ($output) = @_;
while (1) {
opendir(my $dh, "/proc") || die "Couldn't open /proc $!";
while (my $pid = readdir $dh) {
next unless $pid =~ /^\d+$/; # only pids please.
open(my $sfh, join("/", "/proc", $pid, "status")) || next;
my %info = (
Time => time,
);
while(<$sfh>) {
chomp;
/^(.+):\s+(.+)/;
my $key = $1;
my $value = $2;
$value =~ s/\t/ /g;
# Only save first value of State
if ($key eq "State") {
$value = substr($value, 0, 1);
}
$info{$key} = $value
if (exists $WANT_FIELDS{$key});
}
close($sfh);
open(my $wfh, join("/", "/proc", $pid, "wchan")) || next;
$info{Wchan} = <$wfh>;
close($wfh);
if (!$TRIGGER_MODE || ($TRIGGER_MODE && $TRIGGER_STATE eq $info{State})) {
&{ $output }(\%info);
}
}
close($dh);
usleep($INTERVAL_TIME * 1_000_000);
}
}
if ($OUTPUT_MODE) {
my $dbh = get_handle($SQLITE_FILENAME) || die "Could not open sqlite database.";
create_table($dbh);
my $prepare_handle = prepare_insert($dbh);
# commit signal handler
$SIG{'INT'} = sub {
$dbh->commit;
exit(1);
};
my $commit_count = 0;
main(sub {
my ($info) = @_;
insert_row($prepare_handle, $info);
if ($commit_count++ > COMMIT_LIMIT) {
$dbh->commit;
$commit_count = 0;
}
});
}
else {
main(\&csv_output);
}