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