summaryrefslogtreecommitdiff
path: root/src/pmdas/nginx/pmdanginx.pl
blob: 3ec8b65b5026c370f468b8e6d50a10b63f04d662 (plain)
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
#
# Copyright (c) 2013 Ryan Doyle.
# 
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
# 
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
# 

use strict;
use warnings;
use PCP::PMDA;
use LWP::UserAgent;

my @nginx_status = ();
my $nginx_status_url = "http://localhost/nginx_status";
my $nginx_fetch_timeout = 1;
my $http_client = LWP::UserAgent->new;

# Configuration files for overriding the above settings
for my $file (pmda_config('PCP_PMDAS_DIR') . '/nginx/nginx.conf', 'nginx.conf') {
	eval `cat $file` unless ! -f $file;
}

$http_client->agent('pmdanginx');
$http_client->timeout($nginx_fetch_timeout);

sub update_nginx_status 
{
	my $response = $http_client->get($nginx_status_url);
	if ($response->is_success) {
	    # All the content on the status page are digits. Map the array
	    # index to the metric item ID.
	    @nginx_status = ($response->decoded_content =~ m/(\d+)/gm);
	} else {
	    @nginx_status = undef;
	}
}

sub nginx_fetch_callback
{
	my ($cluster, $item, $inst) = @_;
	if ($cluster == 0 && defined($nginx_status[$item])){
	    return ($nginx_status[$item], 1);
	}
	return (PM_ERR_PMID, 0);
}

my $pmda = PCP::PMDA->new('nginx', 117);

$pmda->add_metric(pmda_pmid(0,0), PM_TYPE_U32, PM_INDOM_NULL,
	PM_SEM_INSTANT, pmda_units(0,0,0,0,0,0),
	'nginx.active',
	'Number of active connections', '');
$pmda->add_metric(pmda_pmid(0,1), PM_TYPE_U64, PM_INDOM_NULL,
	PM_SEM_COUNTER, pmda_units(0,0,1,0,0,PM_COUNT_ONE),
	'nginx.accepts_count',
	'Total number of accepted connections', '');
$pmda->add_metric(pmda_pmid(0,2), PM_TYPE_U64, PM_INDOM_NULL,
	PM_SEM_COUNTER, pmda_units(0,0,1,0,0,PM_COUNT_ONE),
	'nginx.handled_count',
	'Total number of handled connections', '');
$pmda->add_metric(pmda_pmid(0,3), PM_TYPE_U64, PM_INDOM_NULL,
	PM_SEM_COUNTER, pmda_units(0,0,1,0,0,PM_COUNT_ONE),
	'nginx.requests_count',
	'Total number of requests', '');
$pmda->add_metric(pmda_pmid(0,4), PM_TYPE_U32, PM_INDOM_NULL,
	PM_SEM_INSTANT, pmda_units(0,0,0,0,0,0),
	'nginx.reading',
	'Reading the request header', '');
$pmda->add_metric(pmda_pmid(0,5), PM_TYPE_U32, PM_INDOM_NULL,
	PM_SEM_INSTANT, pmda_units(0,0,0,0,0,0),
	'nginx.writing',
	'Reading the request body, processing the request or writing response', '');
$pmda->add_metric(pmda_pmid(0,6), PM_TYPE_U32, PM_INDOM_NULL,
	PM_SEM_INSTANT, pmda_units(0,0,0,0,0,0),
	'nginx.waiting',
	'Keepalive connections', '');

$pmda->set_fetch_callback(\&nginx_fetch_callback);
$pmda->set_refresh(\&update_nginx_status);
$pmda->set_user('pcp');
$pmda->run;

=pod

=head1 NAME

pmdanginx - nginx performance metrics domain agent (PMDA)

=head1 DESCRIPTION

B<pmdanginx> is a Performance Metrics Domain Agent (PMDA) which exports
metric values from nginx.


=head1 INSTALLATION

This PMDA requires that the nginx stub_status module is active and 
available at http://localhost/nginx_status


Install the nginx PMDA by using the B<Install> script as root:

	# cd $PCP_PMDAS_DIR/nginx
	# ./Install

To uninstall, do the following as root:

	# cd $PCP_PMDAS_DIR/nginx
	# ./Remove

B<pmdanginx> is launched by pmcd(1) and should never be executed
directly.  The Install and Remove scripts notify pmcd(1) when
the agent is installed or removed.

=head1 FILES

=over

=item $PCP_PMDAS_DIR/nginx/nginx.conf

optional configuration file for B<pmdanginx>

=item $PCP_PMDAS_DIR/nginx/Install

installation script for the B<pmdanginx> agent

=item $PCP_PMDAS_DIR/nginx/Remove

undo installation script for the B<pmdanginx> agent

=item $PCP_LOG_DIR/pmcd/nginx.log

default log file for error messages from B<pmdanginx>

=back

=head1 SEE ALSO

pmcd(1).