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
|
# You may distribute under the terms of either the GNU General Public License
# or the Artistic License (the same terms as Perl itself)
#
# (C) Paul Evans, 2016-2024 -- leonerd@leonerd.org.uk
package Net::Prometheus::ProcessCollector::linux 0.14;
use v5.14;
use warnings;
use base qw( Net::Prometheus::ProcessCollector );
use constant {
TICKS_PER_SEC => 100,
BYTES_PER_PAGE => 4096,
};
=head1 NAME
C<Net::Prometheus::ProcessCollector::linux> - Process Collector for F<linux> OS
=head1 SYNOPSIS
=for highlighter language=perl
use Net::Prometheus;
use Net::Prometheus::ProcessCollector::linux;
my $prometheus = Net::Prometheus->new;
$prometheus->register( Net::Prometheus::ProcessCollector::linux->new );
=head1 DESCRIPTION
This class provides a L<Net::Prometheus> collector instance to provide
process-wide metrics for a process running on the F<linux> operating system.
At collection time, if the requested process does not exist, no metrics are
returned.
=head2 Other Process Collection
The C<pid> argument allows the collector to collect from processes other than
the one actually running the code.
Note also that scraping processes owned by other users may not be possible for
non-root users. In particular, most systems do not let non-root users see the
F</proc/self/fd> directory of processes they don't own. In this case, the
C<process_open_fds> metric will not be returned.
=cut
=head1 CONSTRUCTOR
=head2 new
$collector = Net::Prometheus::ProcessCollector::linux->new( %args )
As well as the default arguments supported by
L<Net::Prometheus::ProcessCollector>, the following extra named arguments are
recognised:
=over
=item pid => STR
The numerical PID to collect information about; defaults to the string
C<"self"> allowing the exporter to collect information about itself, even over
fork calls.
If the collector is collecting from C<"self"> or from a numerical PID that
matches its own PID, then it will subtract 1 from the count of open file
handles, to account for the C<readdir()> handle being used to collect that
count. If it is collecting a different process, it will not.
=back
=cut
my $BOOTTIME;
sub new
{
my $class = shift;
my %args = @_;
# To report process_start_time_seconds correctly, we need the machine boot
# time
if( !defined $BOOTTIME ) {
foreach my $line ( do { open my $fh, "<", "/proc/stat"; <$fh> } ) {
next unless $line =~ m/^btime /;
$BOOTTIME = +( split m/\s+/, $line )[1];
last;
}
}
my $self = $class->__new( %args );
$self->{pid} = $args{pid} || "self";
return $self;
}
sub _read_procfile
{
my $self = shift;
my ( $path ) = @_;
open my $fh, "<", "/proc/$self->{pid}/$path" or return;
return <$fh>;
}
sub _open_fds
{
my $self = shift;
my $pid = $self->{pid};
opendir my $dirh, "/proc/$pid/fd" or return undef;
my $count = ( () = readdir $dirh );
$count -= 1 if $pid eq "self" or $pid == $$; # subtract 1 for $dirh itself
return $count;
}
sub _limit_fds
{
my $self = shift;
my $line = ( grep m/^Max open files/, $self->_read_procfile( "limits" ) )[0];
defined $line or return undef;
# Max open files $SOFT $HARD
return +( split m/\s+/, $line )[3];
}
sub collect
{
my $self = shift;
my $statline = $self->_read_procfile( "stat" );
defined $statline or return; # process missing
# /proc/PID/stat contains PID (COMM) more fields here
my @statfields = split( m/\s+/,
( $statline =~ m/\)\s+(.*)/ )[0]
);
my $utime = $statfields[11] / TICKS_PER_SEC;
my $stime = $statfields[12] / TICKS_PER_SEC;
my $starttime = $statfields[19] / TICKS_PER_SEC;
my $vsize = $statfields[20];
my $rss = $statfields[21] * BYTES_PER_PAGE;
my $open_fds = $self->_open_fds;
my $limit_fds = $self->_limit_fds;
return
$self->_make_metric( cpu_user_seconds_total => $utime,
"counter", "Total user CPU time spent in seconds" ),
$self->_make_metric( cpu_system_seconds_total => $stime,
"counter", "Total system CPU time spent in seconds" ),
$self->_make_metric( cpu_seconds_total => $utime + $stime,
"counter", "Total user and system CPU time spent in seconds" ),
$self->_make_metric( virtual_memory_bytes => $vsize,
"gauge", "Virtual memory size in bytes" ),
$self->_make_metric( resident_memory_bytes => $rss,
"gauge", "Resident memory size in bytes" ),
( defined $open_fds ?
$self->_make_metric( open_fds => $open_fds,
"gauge", "Number of open file handles" ) :
() ),
( defined $limit_fds ?
$self->_make_metric( max_fds => $limit_fds,
"gauge", "Maximum number of allowed file handles" ) :
() ),
$self->_make_metric( start_time_seconds => $BOOTTIME + $starttime,
"gauge", "Unix epoch time the process started at" ),
# TODO: consider some stats out of /proc/PID/io
}
=head1 AUTHOR
Paul Evans <leonerd@leonerd.org.uk>
=cut
0x55AA;
|