3 * Copyright (C) 2015 Nicolas JOURDEN
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Nicolas JOURDEN <nicolas.jourden at laposte.net>
30 #include "utils_time.h"
31 #include "configfile.h"
33 #define GPS_DEFAULT_HOST "localhost"
34 #define GPS_DEFAULT_PORT "2947"
35 #define GPS_DEFAULT_TIMEOUT 15
36 #define GPS_DEFAULT_PAUSE 1
55 static const char *config_keys[] =
62 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
65 static pthread_t connector = (pthread_t) 0;
67 static cgps_config_t config;
69 static cgps_data_t data = {NAN, NAN, NAN, NAN};
70 static pthread_mutex_t data_lock = PTHREAD_MUTEX_INITIALIZER;
73 * Thread reading from gpsd.
75 static void * gps_collectd_thread (void * pData)
77 struct gps_data_t conn;
81 int status = gps_open (config.host, config.port, &conn);
84 WARNING ("gps plugin: Connecting to %s:%s failed: %s",
85 config.host, config.port, gps_errstr (status));
90 gps_stream (&conn, WATCH_ENABLE | WATCH_JSON | WATCH_NEWSTYLE, NULL);
91 gps_send (&conn, "?WATCH={\"enable\":true,\"json\":true,\"nmea\":false}\r\n");
95 if (!gps_waiting (&conn, config.timeout))
101 if (gps_read (&conn) == -1)
103 WARNING ("gps plugin: incorrect data!");
107 pthread_mutex_lock (&data_lock);
109 // Number of sats in view:
110 data.sats_used = (gauge_t) conn.satellites_used;
111 data.sats_visible = (gauge_t) conn.satellites_visible;
113 // dilution of precision:
114 data.vdop = NAN; data.hdop = NAN;
115 if (data.sats_used > 0)
117 data.hdop = conn.dop.hdop;
118 data.vdop = conn.dop.vdop;
122 DEBUG ("gps plugin: %.0f sats used (of %.0f visible), hdop = %.3f, vdop = %.3f",
123 data.sats_used, data.sats_visible, data.hdop, data.vdop);
125 pthread_mutex_unlock (&data_lock);
129 gps_stream (&conn, WATCH_DISABLE, /* data = */ NULL);
132 pthread_exit ((void *) 0);
136 * Submit a piece of the data.
138 static void cgps_submit (const char *type, gauge_t value, const char *type_instance)
141 value_list_t vl = VALUE_LIST_INIT;
143 values[0].gauge = value;
147 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
148 sstrncpy (vl.plugin, "gps", sizeof (vl.plugin));
149 sstrncpy (vl.type, type, sizeof (vl.type));
150 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
152 plugin_dispatch_values (&vl);
156 * Read the data and submit by piece.
158 static int cgps_read ()
160 cgps_data_t data_copy;
162 pthread_mutex_lock (&data_lock);
164 pthread_mutex_unlock (&data_lock);
166 cgps_submit ("dilution_of_precision", data_copy.hdop, "horizontal");
167 cgps_submit ("dilution_of_precision", data_copy.vdop, "vertical");
168 cgps_submit ("satellites", data_copy.sats_used, "used");
169 cgps_submit ("satellites", data_copy.sats_visible, "visible");
175 * Read configuration.
177 static int cgps_config (const char *key, const char *value)
181 if (strcasecmp (key, "Host") == 0)
184 config.host = sstrdup (value);
186 else if (strcasecmp (key, "Port") == 0)
189 config.port = sstrdup (value);
191 else if (strcasecmp (key, "Timeout") == 0)
193 config.timeout = (int) (strtod(value, &endptr) * 1000);
195 else if (strcasecmp (key, "Pause") == 0)
197 config.pause = (int) (strtod (value, &endptr));
206 static int cgps_init (void)
210 DEBUG ("gps plugin: config{host: \"%s\", port: \"%s\", timeout: %d, pause: %d}",
211 config.host, config.port, config.timeout, config.pause);
213 status = plugin_thread_create (&connector, NULL, gps_collectd_thread, NULL);
216 ERROR ("gps plugin: pthread_create() failed.");
226 static int cgps_shutdown (void)
228 if (connector != ((pthread_t) 0))
230 pthread_kill (connector, SIGTERM);
231 connector = (pthread_t) 0;
241 * Register the module.
243 void module_register (void)
245 config.host = sstrdup (GPS_DEFAULT_HOST);
246 config.port = sstrdup (GPS_DEFAULT_PORT);
247 config.timeout = GPS_DEFAULT_TIMEOUT;
248 config.pause = GPS_DEFAULT_PAUSE;
250 plugin_register_config ("gps", cgps_config, config_keys, config_keys_num);
251 plugin_register_init ("gps", cgps_init);
252 plugin_register_read ("gps", cgps_read);
253 plugin_register_shutdown ("gps", cgps_shutdown);