line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# You may distribute under the terms of either the GNU General Public License |
2
|
|
|
|
|
|
|
# or the Artistic License (the same terms as Perl itself) |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# (C) Paul Evans, 2016 -- leonerd@leonerd.org.uk |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
package Net::Prometheus::ProcessCollector::linux; |
7
|
|
|
|
|
|
|
|
8
|
4
|
|
|
4
|
|
72857
|
use strict; |
|
4
|
|
|
|
|
18
|
|
|
4
|
|
|
|
|
116
|
|
9
|
4
|
|
|
4
|
|
20
|
use warnings; |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
113
|
|
10
|
4
|
|
|
4
|
|
22
|
use base qw( Net::Prometheus::ProcessCollector ); |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
860
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = '0.10'; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
use constant { |
15
|
4
|
|
|
|
|
2938
|
TICKS_PER_SEC => 100, |
16
|
|
|
|
|
|
|
BYTES_PER_PAGE => 4096, |
17
|
4
|
|
|
4
|
|
29
|
}; |
|
4
|
|
|
|
|
8
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=head1 NAME |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
C - Process Collector for F OS |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=head1 SYNOPSIS |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
use Net::Prometheus; |
26
|
|
|
|
|
|
|
use Net::Prometheus::ProcessCollector::linux; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
my $prometheus = Net::Prometheus->new; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
$prometheus->register( Net::Prometheus::ProcessCollector::linux->new ); |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
=head1 DESCRIPTION |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
This class provides a L collector instance to provide |
35
|
|
|
|
|
|
|
process-wide metrics for a process running on the F operating system. |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
At collection time, if the requested process does not exist, no metrics are |
38
|
|
|
|
|
|
|
returned. |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=head2 Other Process Collection |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
The C argument allows the collector to collect from processes other than |
43
|
|
|
|
|
|
|
the one actually running the code. |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
Note also that scraping processes owned by other users may not be possible for |
46
|
|
|
|
|
|
|
non-root users. In particular, most systems do not let non-root users see the |
47
|
|
|
|
|
|
|
L directory of processes they don't own. In this case, the |
48
|
|
|
|
|
|
|
C metric will not be returned. |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=cut |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=head1 CONSTRUCTOR |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=head2 new |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
$collector = Net::Prometheus::ProcessCollector::linux->new( %args ) |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
As well as the default arguments supported by |
59
|
|
|
|
|
|
|
L, the following extra named arguments are |
60
|
|
|
|
|
|
|
recognised: |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=over |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=item pid => STR |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
The numerical PID to collect information about; defaults to the string |
67
|
|
|
|
|
|
|
C<"self"> allowing the exporter to collect information about itself, even over |
68
|
|
|
|
|
|
|
fork calls. |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
If the collector is collecting from C<"self"> or from a numerical PID that |
71
|
|
|
|
|
|
|
matches its own PID, then it will subtract 1 from the count of open file |
72
|
|
|
|
|
|
|
handles, to account for the C handle being used to collect that |
73
|
|
|
|
|
|
|
count. If it is collecting a different process, it will not. |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=back |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=cut |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
my $BOOTTIME; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub new |
82
|
|
|
|
|
|
|
{ |
83
|
4
|
|
|
4
|
1
|
2180
|
my $class = shift; |
84
|
4
|
|
|
|
|
12
|
my %args = @_; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
# To report process_start_time_seconds correctly, we need the machine boot |
87
|
|
|
|
|
|
|
# time |
88
|
4
|
100
|
|
|
|
13
|
if( !defined $BOOTTIME ) { |
89
|
3
|
|
|
|
|
8
|
foreach my $line ( do { open my $fh, "<", "/proc/stat"; <$fh> } ) { |
|
3
|
|
|
|
|
171
|
|
|
3
|
|
|
|
|
354
|
|
90
|
60
|
100
|
|
|
|
137
|
next unless $line =~ m/^btime /; |
91
|
3
|
|
|
|
|
24
|
$BOOTTIME = +( split m/\s+/, $line )[1]; |
92
|
3
|
|
|
|
|
9
|
last; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
4
|
|
|
|
|
44
|
my $self = $class->__new( %args ); |
97
|
|
|
|
|
|
|
|
98
|
4
|
|
50
|
|
|
33
|
$self->{pid} = $args{pid} || "self"; |
99
|
|
|
|
|
|
|
|
100
|
4
|
|
|
|
|
21
|
return $self; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
sub _read_procfile |
104
|
|
|
|
|
|
|
{ |
105
|
8
|
|
|
8
|
|
14
|
my $self = shift; |
106
|
8
|
|
|
|
|
18
|
my ( $path ) = @_; |
107
|
|
|
|
|
|
|
|
108
|
8
|
50
|
|
|
|
343
|
open my $fh, "<", "/proc/$self->{pid}/$path" or return; |
109
|
8
|
|
|
|
|
413
|
return <$fh>; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub _open_fds |
113
|
|
|
|
|
|
|
{ |
114
|
4
|
|
|
4
|
|
8
|
my $self = shift; |
115
|
|
|
|
|
|
|
|
116
|
4
|
|
|
|
|
11
|
my $pid = $self->{pid}; |
117
|
|
|
|
|
|
|
|
118
|
4
|
50
|
|
|
|
191
|
opendir my $dirh, "/proc/$pid/fd" or return undef; |
119
|
4
|
|
|
|
|
130
|
my $count = ( () = readdir $dirh ); |
120
|
|
|
|
|
|
|
|
121
|
4
|
50
|
33
|
|
|
27
|
$count -= 1 if $pid eq "self" or $pid == $$; # subtract 1 for $dirh itself |
122
|
|
|
|
|
|
|
|
123
|
4
|
|
|
|
|
65
|
return $count; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
sub _limit_fds |
127
|
|
|
|
|
|
|
{ |
128
|
4
|
|
|
4
|
|
8
|
my $self = shift; |
129
|
4
|
|
|
|
|
12
|
my $line = ( grep m/^Max open files/, $self->_read_procfile( "limits" ) )[0]; |
130
|
4
|
50
|
|
|
|
24
|
defined $line or return undef; |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
# Max open files $SOFT $HARD |
133
|
4
|
|
|
|
|
33
|
return +( split m/\s+/, $line )[3]; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
sub collect |
137
|
|
|
|
|
|
|
{ |
138
|
4
|
|
|
4
|
0
|
541
|
my $self = shift; |
139
|
|
|
|
|
|
|
|
140
|
4
|
|
|
|
|
12
|
my $statline = $self->_read_procfile( "stat" ); |
141
|
4
|
50
|
|
|
|
24
|
defined $statline or return; # process missing |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
# /proc/PID/stat contains PID (COMM) more fields here |
144
|
4
|
|
|
|
|
114
|
my @statfields = split( m/\s+/, |
145
|
|
|
|
|
|
|
( $statline =~ m/\)\s+(.*)/ )[0] |
146
|
|
|
|
|
|
|
); |
147
|
|
|
|
|
|
|
|
148
|
4
|
|
|
|
|
20
|
my $utime = $statfields[11] / TICKS_PER_SEC; |
149
|
4
|
|
|
|
|
10
|
my $stime = $statfields[12] / TICKS_PER_SEC; |
150
|
4
|
|
|
|
|
16
|
my $starttime = $statfields[19] / TICKS_PER_SEC; |
151
|
4
|
|
|
|
|
9
|
my $vsize = $statfields[20]; |
152
|
4
|
|
|
|
|
11
|
my $rss = $statfields[21] * BYTES_PER_PAGE; |
153
|
|
|
|
|
|
|
|
154
|
4
|
|
|
|
|
13
|
my $open_fds = $self->_open_fds; |
155
|
|
|
|
|
|
|
|
156
|
4
|
|
|
|
|
18
|
my $limit_fds = $self->_limit_fds; |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
return |
159
|
4
|
50
|
|
|
|
36
|
$self->_make_metric( cpu_user_seconds_total => $utime, |
|
|
50
|
|
|
|
|
|
160
|
|
|
|
|
|
|
"counter", "Total user CPU time spent in seconds" ), |
161
|
|
|
|
|
|
|
$self->_make_metric( cpu_system_seconds_total => $stime, |
162
|
|
|
|
|
|
|
"counter", "Total system CPU time spent in seconds" ), |
163
|
|
|
|
|
|
|
$self->_make_metric( cpu_seconds_total => $utime + $stime, |
164
|
|
|
|
|
|
|
"counter", "Total user and system CPU time spent in seconds" ), |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
$self->_make_metric( virtual_memory_bytes => $vsize, |
167
|
|
|
|
|
|
|
"gauge", "Virtual memory size in bytes" ), |
168
|
|
|
|
|
|
|
$self->_make_metric( resident_memory_bytes => $rss, |
169
|
|
|
|
|
|
|
"gauge", "Resident memory size in bytes" ), |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
( defined $open_fds ? |
172
|
|
|
|
|
|
|
$self->_make_metric( open_fds => $open_fds, |
173
|
|
|
|
|
|
|
"gauge", "Number of open file handles" ) : |
174
|
|
|
|
|
|
|
() ), |
175
|
|
|
|
|
|
|
( defined $limit_fds ? |
176
|
|
|
|
|
|
|
$self->_make_metric( max_fds => $limit_fds, |
177
|
|
|
|
|
|
|
"gauge", "Maximum number of allowed file handles" ) : |
178
|
|
|
|
|
|
|
() ), |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
$self->_make_metric( start_time_seconds => $BOOTTIME + $starttime, |
181
|
|
|
|
|
|
|
"gauge", "Unix epoch time the process started at" ), |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
# TODO: consider some stats out of /proc/PID/io |
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=head1 AUTHOR |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
Paul Evans |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=cut |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
0x55AA; |