summaryrefslogtreecommitdiff
path: root/src/pmdas/gpsd/pmdagpsd.pl
blob: c924d04bca5809c92ee921bb68746ed3d33a0cf4 (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#
# Copyright (c) 2010 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
#
# 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 JSON;
use Time::HiRes qw ( time );

use vars qw( $pmda );
use vars qw( %devdata %satdata );

my $gpspipe = "gpspipe -w";

my $json;

my $gpsd_release = "?";
my $gpsd_rev = "?";
my $gpsd_proto_maj = -1;
my $gpsd_proto_min = -1;

# FIXME: the following will need changing to support multiple devices
my $gpsd_device = undef;
my %devdata = (
    "time"           => -1,
    "ept"            => -1,
    "lat"            => -1,
    "lon"            => -1,
    "alt"            => -1,
    "track"          => -1,
    "speed"          => -1,
    "climb"          => -1,
    "mode"           => 0,  # 0 = no fix
    "xdop"           => -1,
    "ydop"           => -1,
    "vdop"           => -1,
    "tdop"           => -1,
    "hdop"           => -1,
    "gdop"           => -1,
    "pdop"           => -1,
    "num_satellites" => 0,
);
my $gpsd_sat_indom = 0;
my @gpsd_sat_dom = ();

sub gpsd_pipe_callback
{
    (undef, $_) = @_;

    # $pmda->log("pipe got: $_");

    my $jtxt = $json->decode($_);

    if ($jtxt->{"class"} eq "VERSION") {
        $gpsd_release   = $jtxt->{"release"};
        $gpsd_rev       = $jtxt->{"rev"};
        $gpsd_proto_maj = $jtxt->{"proto_major"};
        $gpsd_proto_min = $jtxt->{"proto_minor"};
    } elsif ($jtxt->{"class"} eq "DEVICES") {
        foreach my $dev (@{$jtxt->{"devices"}}) {
            if ($dev->{"class"} eq "DEVICE") {
                $pmda->log("gpsd_pipe_callback: oops!  multiple " .
                    "devices detected, only using the first " .
                    "($gpsd_device)") if (defined($gpsd_device) and
                                          $dev->{"path"} ne $gpsd_device);

                $gpsd_device = $dev->{"path"};
            } else {
                $pmda->log("gpsd_pipe_callback: unknown class '" .
                    $dev->{"class"} . "'");
            }
        }
    } elsif ($jtxt->{"class"} eq "DEVICE") {
        # nothing to do
    } elsif ($jtxt->{"class"} eq "WATCH") {
        # nothing to do
    } elsif ($jtxt->{"class"} eq "TPV") {
        return if ($jtxt->{"device"} ne $gpsd_device);

        $devdata{"time"}  = $jtxt->{"time"};
        $devdata{"ept"}   = $jtxt->{"ept"};
        $devdata{"lat"}   = $jtxt->{"lat"};
        $devdata{"lon"}   = $jtxt->{"lon"};
        $devdata{"alt"}   = $jtxt->{"alt"};
        $devdata{"track"} = $jtxt->{"track"};
        $devdata{"speed"} = $jtxt->{"speed"};
        $devdata{"climb"} = $jtxt->{"climb"};
        $devdata{"mode"}  = $jtxt->{"mode"};
    } elsif ($jtxt->{"class"} eq "SKY") {
        return if ($jtxt->{"device"} ne $gpsd_device);

        $devdata{"xdop"} = $jtxt->{"xdop"};
        $devdata{"ydop"} = $jtxt->{"ydop"};
        $devdata{"vdop"} = $jtxt->{"vdop"};
        $devdata{"tdop"} = $jtxt->{"tdop"};
        $devdata{"hdop"} = $jtxt->{"hdop"};
        $devdata{"gdop"} = $jtxt->{"gdop"};
        $devdata{"pdop"} = $jtxt->{"pdop"};

        my %sats = {};
        my @dom = ();
        foreach my $sat (@{$jtxt->{"satellites"}}) {
            push(@dom, $sat->{"PRN"} => "$sat->{'PRN'}");

            $sats{"el"}{$sat->{"PRN"}} = $sat->{"el"};
            $sats{"az"}{$sat->{"PRN"}} = $sat->{"az"};
            $sats{"ss"}{$sat->{"PRN"}} = $sat->{"ss"};
            $sats{"used"}{$sat->{"PRN"}} = $sat->{"used"};
        }
        %satdata = %sats;
        $pmda->replace_indom($gpsd_sat_indom, \@dom);

        $devdata{"num_satellites"} = scalar(@dom) / 2;
    } else {
        $pmda->log("gpsd_pipe_callback: unknown class '" . $jtxt->{"class"} . "'");
    }
}

sub gpsd_fetch_callback
{
    my ($cluster, $item, $inst) = @_;
    my $metric_name = pmda_pmid_name($cluster, $item);

    # $pmda->log("gpsd_fetch_callback $metric_name $cluster:$item ($inst)\n");
    
    return (PM_ERR_PMID, 0) if (!defined($metric_name));

    if ($cluster == 0) {
        return (PM_ERR_INST, 0) if ($inst != PM_IN_NULL);

        if ($metric_name eq "gpsd.release") {
            return ($gpsd_release, 1);
        } elsif ($metric_name eq "gpsd.rev") {
            return ($gpsd_rev, 1);
        } elsif ($metric_name eq "gpsd.proto.major") {
            return ($gpsd_proto_maj, 1);
        } elsif ($metric_name eq "gpsd.proto.minor") {
            return ($gpsd_proto_min, 1);
        }
    }

    $metric_name =~ s/^gpsd\.devices\.dev0\.//;

    if ($metric_name =~ m/^satellites\./) {
        # satellite info
        $metric_name =~ s/^satellites\.//;
    
        # $pmda->log("gpsd_fetch_callbac2 $metric_name $cluster:$item ($inst): ${satdata{$metric_name}}\n");
        # $pmda->log("gpsd_fetch_callbac2 $metric_name $cluster:$item ($inst): ${satdata{$metric_name}{$inst}}\n");
        return (PM_ERR_INST, 0) if ($inst == PM_IN_NULL);
        return (PM_ERR_PMID, 0) if (!defined($satdata{$metric_name}{$inst}));
        return ($satdata{$metric_name}{$inst}, 1);
    }
        
    return (PM_ERR_INST, 0) if ($inst != PM_IN_NULL);
    return (PM_ERR_PMID, 0) if (!defined($devdata{$metric_name}));
    return ($devdata{$metric_name}, 1);
}

$json = new JSON;

$pmda = PCP::PMDA->new('gpsd', 105);

$pmda->add_metric(pmda_pmid(0,0), PM_TYPE_STRING, PM_INDOM_NULL, PM_SEM_INSTANT,
                  pmda_units(0,0,0,0,0,0),
                  "gpsd.release", '', '');
$pmda->add_metric(pmda_pmid(0,1), PM_TYPE_STRING, PM_INDOM_NULL, PM_SEM_INSTANT,
                  pmda_units(0,0,0,0,0,0),
                  "gpsd.rev", '', '');
$pmda->add_metric(pmda_pmid(0,2), PM_TYPE_U32, PM_INDOM_NULL, PM_SEM_INSTANT,
                  pmda_units(0,0,0,0,0,0),
                  "gpsd.proto.major", '', '');
$pmda->add_metric(pmda_pmid(0,3), PM_TYPE_U32, PM_INDOM_NULL, PM_SEM_INSTANT,
                  pmda_units(0,0,0,0,0,0),
                  "gpsd.proto.minor", '', '');

$pmda->add_metric(pmda_pmid(1,0), PM_TYPE_FLOAT, PM_INDOM_NULL, PM_SEM_INSTANT,
                  pmda_units(0,0,0,0,0,0),
                  "gpsd.devices.dev0.time", '', '');
$pmda->add_metric(pmda_pmid(1,1), PM_TYPE_FLOAT, PM_INDOM_NULL, PM_SEM_INSTANT,
                  pmda_units(0,0,0,0,0,0),
                  "gpsd.devices.dev0.ept", '', '');
$pmda->add_metric(pmda_pmid(1,2), PM_TYPE_FLOAT, PM_INDOM_NULL, PM_SEM_INSTANT,
                  pmda_units(0,0,0,0,0,0),
                  "gpsd.devices.dev0.lat", '', '');
$pmda->add_metric(pmda_pmid(1,3), PM_TYPE_FLOAT, PM_INDOM_NULL, PM_SEM_INSTANT,
                  pmda_units(0,0,0,0,0,0),
                  "gpsd.devices.dev0.lon", '', '');
$pmda->add_metric(pmda_pmid(1,4), PM_TYPE_FLOAT, PM_INDOM_NULL, PM_SEM_INSTANT,
                  pmda_units(0,0,0,0,0,0),
                  "gpsd.devices.dev0.alt", '', '');
$pmda->add_metric(pmda_pmid(1,5), PM_TYPE_FLOAT, PM_INDOM_NULL, PM_SEM_INSTANT,
                  pmda_units(0,0,0,0,0,0),
                  "gpsd.devices.dev0.track", '', '');
$pmda->add_metric(pmda_pmid(1,6), PM_TYPE_FLOAT, PM_INDOM_NULL, PM_SEM_INSTANT,
                  pmda_units(0,0,0,0,0,0),
                  "gpsd.devices.dev0.speed", '', '');
$pmda->add_metric(pmda_pmid(1,7), PM_TYPE_FLOAT, PM_INDOM_NULL, PM_SEM_INSTANT,
                  pmda_units(0,0,0,0,0,0),
                  "gpsd.devices.dev0.climb", '', '');
$pmda->add_metric(pmda_pmid(1,8), PM_TYPE_32, PM_INDOM_NULL, PM_SEM_INSTANT,
                  pmda_units(0,0,0,0,0,0),
                  "gpsd.devices.dev0.mode", '', '');

$pmda->add_metric(pmda_pmid(1,9), PM_TYPE_FLOAT, PM_INDOM_NULL, PM_SEM_INSTANT,
                  pmda_units(0,0,0,0,0,0),
                  "gpsd.devices.dev0.xdop", '', '');
$pmda->add_metric(pmda_pmid(1,10), PM_TYPE_FLOAT, PM_INDOM_NULL, PM_SEM_INSTANT,
                  pmda_units(0,0,0,0,0,0),
                  "gpsd.devices.dev0.ydop", '', '');
$pmda->add_metric(pmda_pmid(1,11), PM_TYPE_FLOAT, PM_INDOM_NULL, PM_SEM_INSTANT,
                  pmda_units(0,0,0,0,0,0),
                  "gpsd.devices.dev0.vdop", '', '');
$pmda->add_metric(pmda_pmid(1,12), PM_TYPE_FLOAT, PM_INDOM_NULL, PM_SEM_INSTANT,
                  pmda_units(0,0,0,0,0,0),
                  "gpsd.devices.dev0.tdop", '', '');
$pmda->add_metric(pmda_pmid(1,13), PM_TYPE_FLOAT, PM_INDOM_NULL, PM_SEM_INSTANT,
                  pmda_units(0,0,0,0,0,0),
                  "gpsd.devices.dev0.hdop", '', '');
$pmda->add_metric(pmda_pmid(1,14), PM_TYPE_FLOAT, PM_INDOM_NULL, PM_SEM_INSTANT,
                  pmda_units(0,0,0,0,0,0),
                  "gpsd.devices.dev0.gdop", '', '');
$pmda->add_metric(pmda_pmid(1,15), PM_TYPE_FLOAT, PM_INDOM_NULL, PM_SEM_INSTANT,
                  pmda_units(0,0,0,0,0,0),
                  "gpsd.devices.dev0.pdop", '', '');

$pmda->add_metric(pmda_pmid(1,16), PM_TYPE_U32, PM_INDOM_NULL, PM_SEM_INSTANT,
                  pmda_units(0,0,0,0,0,0),
                  "gpsd.devices.dev0.num_satellites", '', '');

$pmda->add_metric(pmda_pmid(1,100), PM_TYPE_U32, $gpsd_sat_indom, PM_SEM_INSTANT,
                  pmda_units(0,0,0,0,0,0),
                  "gpsd.devices.dev0.satellites.el", '', '');
$pmda->add_metric(pmda_pmid(1,101), PM_TYPE_U32, $gpsd_sat_indom, PM_SEM_INSTANT,
                  pmda_units(0,0,0,0,0,0),
                  "gpsd.devices.dev0.satellites.az", '', '');
$pmda->add_metric(pmda_pmid(1,102), PM_TYPE_U32, $gpsd_sat_indom, PM_SEM_INSTANT,
                  pmda_units(0,0,0,0,0,0),
                  "gpsd.devices.dev0.satellites.ss", '', '');
$pmda->add_metric(pmda_pmid(1,103), PM_TYPE_U32, $gpsd_sat_indom, PM_SEM_INSTANT,
                  pmda_units(0,0,0,0,0,0),
                  "gpsd.devices.dev0.satellites.used", '', '');

$pmda->add_indom($gpsd_sat_indom, \@gpsd_sat_dom, '', '');
$pmda->add_pipe($gpspipe, \&gpsd_pipe_callback, 0);

$pmda->set_fetch_callback(\&gpsd_fetch_callback);
$pmda->set_user('pcp');
$pmda->run;

=pod

=head1 NAME

pmdagpsd - gpsd performance metrics domain agent (PMDA)

=head1 DESCRIPTION

B<pmdagpsd> is a Performance Metrics Domain Agent (PMDA) which exports
values from the gpsd daemon.

=head1 INSTALLATION

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

    # cd $PCP_PMDAS_DIR/gpsd
    # ./Install

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

    # cd $PCP_PMDAS_DIR/gpsd
    # ./Remove

B<pmdagpsd> 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/gpsd/Install

installation script for the B<pmdagpsd> agent

=item $PCP_PMDAS_DIR/gpsd/Remove

undo installation script for the B<pmdagpsd> agent

=item $PCP_LOG_DIR/pmcd/gpsd.log

default log file for error messages from B<pmdagpsd>

=back

=head1 SEE ALSO

pmcd(1).