Net-Prometheus-ServerStatusLiteCollector

 view release on metacpan or  search on metacpan

lib/Net/Prometheus/ServerStatusLiteCollector.pm  view on Meta::CPAN

    @all_workers = ();
    my $psopt = $^O =~ m/bsd$/ ? '-ax' : '-e';
    my $ps = `LC_ALL=C command ps $psopt -o ppid,pid`;
    $ps =~ s/^\s+//mg;
    for my $line (split /\n/, $ps) {
        next if $line =~ m/^\D/;
        my ($ppid, $pid) = split /\s+/, $line, 2;
        push @all_workers, $pid if $ppid == $parent_pid;
    }

    # Count busy and idle workers
    my $idle = 0;
    my $busy = 0;
    my @process_status;
    for my $pid (@all_workers) {
        my $json = $stats->{$pid};
        $pstatus = eval {
            $JSON->decode($json || '{}');
        };
        $pstatus ||= {};
        if ($pstatus->{status} && $pstatus->{status} eq 'A') {
            $busy++;
        }
        else {
            $idle++;
        }

        if (defined $pstatus->{time}) {
            $pstatus->{ss} = time - $pstatus->{time};
        }
        $pstatus->{pid} ||= $pid;
        delete $pstatus->{time};
        delete $pstatus->{ppid};
        delete $pstatus->{uptime};
        push @process_status, $pstatus;
    }
    push @samples,
        MetricSamples('plack_busy_workers', gauge => 'Number of busy Plack workers',
            [Sample('plack_busy_workers', $self->labels, $busy)]);
    push @samples,
        MetricSamples('plack_idle_workers', gauge => 'Number of idle Plack workers',
            [Sample('plack_idle_workers', $self->labels, $idle)]);

    $stats = {};
    foreach my $pstatus (@process_status) {
        foreach my $stat (qw(method uri remote_addr protocol)) {
            $stats->{$stat}{$pstatus->{$stat}}++ if $pstatus->{$stat};
        }
    }



( run in 0.237 second using v1.01-cache-2.11-cpan-3cd7ad12f66 )