summaryrefslogtreecommitdiff
path: root/src/pmdas/news/pmdanews.pl
blob: 9d5917890f12f6f8246ce6ce105f6bf36f99346b (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
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
193
194
195
196
#
# Copyright (c) 2012 Red Hat.
# Copyright (c) 2004 Silicon Graphics, Inc.  All Rights Reserved.
# 
# 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;

my @newsgroups = (
    1,  'comp.sys.sgi',
    2,  'comp.sys.sgi.graphics',
    3,  'comp.sys.sgi.hardware',
    4,  'sgi.bad-attitude',
    5,  'sgi.engr.all',
);

use vars qw( $total $news_regex %news_hash @news_count @news_last );
my ($nnrpd_count, $rn_count, $trn_count, $xrn_count, $vn_count) = (0,0,0,0,0);
my $news_file = pmda_config('PCP_PMDAS_DIR') . '/news/active';
my $news_indom = 0;
my $pmda;

sub news_fetch		# called once per ``fetch'' pdu, before callbacks
{
    my ( $group, $cmd );

    $total = 0;
    seek ACTIVE,0,0;
    while (<ACTIVE>) {
	next unless /$news_regex/;
	$group = $news_hash{$1} - 1;
	$news_last[$group] = $2;
	$total += $news_count[$group] = $2 - $3;
    }

    ($nnrpd_count, $rn_count, $trn_count, $xrn_count, $vn_count) = (0,0,0,0,0);
    if (open(READERS, 'ps -ef |')) {
	while (<READERS>) {
	    s/\b(:?\d\d:){2}\d\d\b/ Mmm DD /;	# replace times with dates
	    s/\s*?(\S+?\s+?){8}//;		# nuke the first eight fields
	    next unless /(\S+)\s*?/;		# prepare $cmd with command name
	    $cmd = $1;
	    ($cmd eq 'in.nnrpd' || $cmd =~ /\/in\.nnrpd$/) && $nnrpd_count++;
	    ($cmd eq 'rn'  || $cmd =~ /\/rn$/)	&& $rn_count++;
	    ($cmd eq 'trn' || $cmd =~ /\/trn$/)	&& $trn_count++;
	    ($cmd eq 'xrn' || $cmd =~ /\/xrn$/)	&& $xrn_count++;
	    ($cmd eq 'vn'  || $cmd =~ /\/vrn$/)	&& $vn_count++;
	}
	close READERS;
    }
    else {
	$pmda->log("Cannot execute 'ps' to count news reader processes");
    }
}

sub news_fetch_callback		# must return array of value,status
{
    my ($cluster, $item, $inst) = @_;

    return (PM_ERR_INST, 0) unless ( $inst == PM_IN_NULL ||
		( $cluster == 0 && ($item == 301 || $item == 302) &&
				$inst > 0 && $inst <= ($#newsgroups+1)/2 ) );
    if ($cluster == 0) {
	if ($item == 201)	{ ($total, 1); }		# articles.total
	elsif ($item == 301)	{ ($news_count[$inst-1], 1); }	# articles.count
	elsif ($item == 302)	{ ($news_last[$inst-1], 1); }	# articles.last
	elsif ($item == 101)	{ ($nnrpd_count, 1); }		# readers.nnrpd
	elsif ($item == 111)	{ ($rn_count, 1); }		# readers.rn
	elsif ($item == 112)	{ ($trn_count, 1); }		# readers.trn
	elsif ($item == 113)	{ ($xrn_count, 1); }		# readers.xrn
	elsif ($item == 114)	{ ($vn_count, 1); }		# readers.vn
    	else			{ (PM_ERR_PMID, 0); }
    }
    else { (PM_ERR_PMID, 0); }
}

sub news_init
{
    ($#newsgroups > 0 && $#newsgroups % 2 != 0)
		|| die "Invalid newsgroups array has been specified\n";
    open(ACTIVE, $news_file) || die "Can't open $news_file: $!\n";

    # build regex using the given newsgroup names
    $_ = "^($newsgroups[1]";
    $news_hash{$newsgroups[1]} = $newsgroups[0];
    for (my $i = 2; $i < $#newsgroups; $i += 2) {
	$_ .= "|$newsgroups[$i+1]";
	$news_hash{$newsgroups[$i+1]} = $newsgroups[$i];
    }
    $news_regex = $_ . ") (\\d+) (\\d+) y\$";
}

$pmda = PCP::PMDA->new('news', 28);

$pmda->add_metric(pmda_pmid(0,201), PM_TYPE_U32, PM_INDOM_NULL,
		  PM_SEM_INSTANT, pmda_units(0,0,1,0,0,PM_COUNT_ONE),
		  'news.articles.total',
		  'Total number of articles received for each newsgroup',
'Total number of articles received for each newsgroup.
Note this is the historical running total, see news.articles.count for
the current total of un-expired articles by newsgroup.');

$pmda->add_metric(pmda_pmid(0,301), PM_TYPE_U32, $news_indom,
		  PM_SEM_INSTANT, pmda_units(0,0,1,0,0,PM_COUNT_ONE),
		  'news.articles.count',
		  'Total number of un-expired articles in each newsgroup', '');

$pmda->add_metric(pmda_pmid(0,302), PM_TYPE_U32, $news_indom,
		  PM_SEM_INSTANT, pmda_units(0,0,1,0,0,PM_COUNT_ONE),
		  'news.articles.last', '', '');
$pmda->add_metric(pmda_pmid(0,101), PM_TYPE_U32, PM_INDOM_NULL,
		  PM_SEM_INSTANT, pmda_units(0,0,1,0,0,PM_COUNT_ONE),
		  'news.readers.nnrpd', '', '');
$pmda->add_metric(pmda_pmid(0,111), PM_TYPE_U32, PM_INDOM_NULL,
		  PM_SEM_INSTANT, pmda_units(0,0,1,0,0,PM_COUNT_ONE),
		  'news.readers.rn', '', '');
$pmda->add_metric(pmda_pmid(0,112), PM_TYPE_U32, PM_INDOM_NULL,
		  PM_SEM_INSTANT, pmda_units(0,0,1,0,0,PM_COUNT_ONE),
		  'news.readers.trn', '', '');
$pmda->add_metric(pmda_pmid(0,113), PM_TYPE_U32, PM_INDOM_NULL,
		  PM_SEM_INSTANT, pmda_units(0,0,1,0,0,PM_COUNT_ONE),
		  'news.readers.xrn', '', '');
$pmda->add_metric(pmda_pmid(0,114), PM_TYPE_U32, PM_INDOM_NULL,
		  PM_SEM_INSTANT, pmda_units(0,0,1,0,0,PM_COUNT_ONE),
		  'news.readers.vn', '', '');

$pmda->add_indom($news_indom, \@newsgroups, '', '');

$pmda->set_fetch(\&news_fetch);
$pmda->set_fetch_callback(\&news_fetch_callback);

&news_init;

$pmda->set_user('pcp');
$pmda->run;

=pod

=head1 NAME

pmdanews - sample Usenet news performance metrics domain agent (PMDA)

=head1 DESCRIPTION

B<pmdanews> is an example Performance Metrics Domain Agent (PMDA) which
exports metric values related to a set of newsgroups.

=head1 INSTALLATION

If you want access to the names and values for the news performance
metrics, do the following as root:

	# cd $PCP_PMDAS_DIR/news
	# ./Install

If you want to undo the installation, do the following as root:

	# cd $PCP_PMDAS_DIR/news
	# ./Remove

B<pmdanews> 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/news/Install

installation script for the B<pmdanews> agent

=item $PCP_PMDAS_DIR/news/Remove

undo installation script for the B<pmdanews> agent

=item $PCP_LOG_DIR/pmcd/news.log

default log file for error messages from B<pmdanews>

=back

=head1 SEE ALSO

pmcd(1).