/**
- * This plug-in helps to monitor the GPS connected to a system.
- * It reads the data comming from GPSd.
- It look for the following parameters.
- */
+ * collectd - src/gps.c
+ * Copyright (C) 2015 Nicolas JOURDEN
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Authors:
+ * Nicolas JOURDEN <nicolas.jourden at laposte.net>
+ **/
#include "collectd.h"
#include "common.h"
#include "plugin.h"
+#include "utils_time.h"
+#include "configfile.h"
-#if HAVE_GPS_H
-#include <gps.h>
-#endif
+#define GPS_DEFAULT_HOST "localhost"
+#define GPS_DEFAULT_PORT "2947"
+#define GPS_DEFAULT_TIMEOUT TIME_T_TO_CDTIME_T (15)
+#define GPS_DEFAULT_PAUSE TIME_T_TO_CDTIME_T (1)
-#if HAVE_LIBPTHREAD
+#include <gps.h>
#include <pthread.h>
-#endif
-typedef struct
-{
+typedef struct {
char *host;
char *port;
- int timeout;
-} gps_definition_t;
-static gps_definition_t gps_data_config;
+ cdtime_t timeout;
+ cdtime_t pause;
+} cgps_config_t;
typedef struct {
- int satellites;
- double vdop;
- double hdop;
-} gpsDATA_t;
-static gpsDATA_t gps_data_read;
-
-
-static const char *config_keys[] =
-{
- "Host",
- "Port",
- "Timeout"
-};
-static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
-
+ gauge_t sats_used;
+ gauge_t sats_visible;
+ gauge_t hdop;
+ gauge_t vdop;
+} cgps_data_t;
// Thread items:
static pthread_t connector = (pthread_t) 0;
-static pthread_mutex_t data_mutex = PTHREAD_MUTEX_INITIALIZER;
+static cgps_config_t config;
+
+static cgps_data_t data = {NAN, NAN, NAN, NAN};
+static pthread_mutex_t data_lock = PTHREAD_MUTEX_INITIALIZER;
/**
- * Thread reading from GPSd.
+ * Thread reading from gpsd.
*/
static void * gps_collectd_thread (void * pData)
{
- struct gps_data_t gps_data;
+ struct gps_data_t conn;
while (1)
{
- if (gps_open(gps_data_config.host, gps_data_config.port, &gps_data) < 0)
+ int status = gps_open (config.host, config.port, &conn);
+ if (status < 0)
{
- printf ("cannot connect to: %s:%s", gps_data_config.host, gps_data_config.port);
- sleep(60);
+ WARNING ("gps plugin: Connecting to %s:%s failed: %s",
+ config.host, config.port, gps_errstr (status));
+ sleep (60);
continue;
}
- gps_stream(&gps_data, WATCH_ENABLE | WATCH_JSON, NULL);
+ gps_stream (&conn, WATCH_ENABLE | WATCH_JSON | WATCH_NEWSTYLE, NULL);
+ gps_send (&conn, "?WATCH={\"enable\":true,\"json\":true,\"nmea\":false}\r\n");
while (1)
{
- if (gps_waiting (&gps_data, gps_data_config.timeout))
+ long timeout_us = CDTIME_T_TO_US (config.timeout);
+ if (!gps_waiting (&conn, (int) timeout_us))
{
- if (gps_read (&gps_data) == -1)
- {
- WARNING ("incorrect data.\n");
- }
- else {
- pthread_mutex_lock (&data_mutex);
-
- // Dop data:
- if (isnan(gps_data.dop.vdop) == 0)
- {
- gps_data_read.vdop = gps_data.dop.vdop;
- }
- if (isnan(gps_data.dop.hdop) == 0)
- {
- gps_data_read.hdop = gps_data.dop.hdop;
- }
-
- // Sat in view:
- if ((gps_data.set & LATLON_SET))
- {
- gps_data_read.satellites = gps_data.satellites_used;
- }
-
- pthread_mutex_unlock (&data_mutex);
- }
+ struct timespec pause_ns;
+ CDTIME_T_TO_TIMESPEC (config.pause, &pause_ns);
+ nanosleep (&pause_ns, NULL);
+ continue;
}
+
+ if (gps_read (&conn) == -1)
+ {
+ WARNING ("gps plugin: incorrect data!");
+ continue;
+ }
+
+ pthread_mutex_lock (&data_lock);
+
+ // Number of sats in view:
+ data.sats_used = (gauge_t) conn.satellites_used;
+ data.sats_visible = (gauge_t) conn.satellites_visible;
+
+ // dilution of precision:
+ data.vdop = NAN; data.hdop = NAN;
+ if (data.sats_used > 0)
+ {
+ data.hdop = conn.dop.hdop;
+ data.vdop = conn.dop.vdop;
+ }
+
+
+ DEBUG ("gps plugin: %.0f sats used (of %.0f visible), hdop = %.3f, vdop = %.3f",
+ data.sats_used, data.sats_visible, data.hdop, data.vdop);
+
+ pthread_mutex_unlock (&data_lock);
}
}
- gps_stream(&gps_data, WATCH_DISABLE, NULL);
- gps_close(&gps_data);
+ gps_stream (&conn, WATCH_DISABLE, /* data = */ NULL);
+ gps_close (&conn);
- pthread_exit ((void *)0);
+ pthread_exit ((void *) 0);
}
-
/**
- * Submit the data.
+ * Submit a piece of the data.
*/
-static void gps_collectd_submit (const char *type, gauge_t value)
+static void cgps_submit (const char *type, gauge_t value, const char *type_instance)
{
value_t values[1];
value_list_t vl = VALUE_LIST_INIT;
sstrncpy (vl.host, hostname_g, sizeof (vl.host));
sstrncpy (vl.plugin, "gps", sizeof (vl.plugin));
sstrncpy (vl.type, type, sizeof (vl.type));
- sstrncpy (vl.type_instance, "gps", sizeof (vl.type_instance));
+ sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
plugin_dispatch_values (&vl);
}
-
/**
- * Read the data and submit.
+ * Read the data and submit by piece.
*/
-static int gps_collectd_read ()
+static int cgps_read ()
{
- pthread_mutex_lock (&data_mutex);
- gps_collectd_submit("gps_hdop", (gauge_t) gps_data_read.hdop);
- gps_collectd_submit("gps_vdop", (gauge_t) gps_data_read.vdop);
- gps_collectd_submit("gps_sat", (gauge_t) gps_data_read.satellites);
- printf ("gps: hdop=%1.3f, vdop=%1.3f, sat=%02d.\n",
- gps_data_read.hdop,
- gps_data_read.vdop,
- gps_data_read.satellites
- );
- pthread_mutex_unlock (&data_mutex);
+ cgps_data_t data_copy;
+
+ pthread_mutex_lock (&data_lock);
+ data_copy = data;
+ pthread_mutex_unlock (&data_lock);
+
+ cgps_submit ("dilution_of_precision", data_copy.hdop, "horizontal");
+ cgps_submit ("dilution_of_precision", data_copy.vdop, "vertical");
+ cgps_submit ("satellites", data_copy.sats_used, "used");
+ cgps_submit ("satellites", data_copy.sats_visible, "visible");
+
return (0);
}
-
/**
* Read configuration.
*/
-static int gps_collectd_config (const char *key, const char *value)
+static int cgps_config (oconfig_item_t *ci)
{
- if (strcasecmp (key, "Host") == 0) {
- if (gps_data_config.host != NULL) free (gps_data_config.host);
- gps_data_config.host = sstrdup (value);
- }
- if (strcasecmp (key, "Port") == 0) {
- if (gps_data_config.port != NULL) free (gps_data_config.port);
- gps_data_config.port = sstrdup (value);
- }
- if (strcasecmp (key, "Timeout") == 0) {
- gps_data_config.timeout = (int) strtol (value, NULL, 1000);
+ int i;
+
+ for (i = 0; i < ci->children_num; i++)
+ {
+ oconfig_item_t *child = ci->children + i;
+
+ if (strcasecmp ("Host", child->key) == 0)
+ cf_util_get_string (child, &config.host);
+ else if (strcasecmp ("Port", child->key) == 0)
+ cf_util_get_string (child, &config.port);
+ else if (strcasecmp ("Timeout", child->key) == 0)
+ cf_util_get_cdtime (child, &config.timeout);
+ else if (strcasecmp ("Pause", child->key) == 0)
+ cf_util_get_cdtime (child, &config.pause);
+ else
+ WARNING ("gps plugin: Ignoring unknown config option \"%s\".", child->key);
}
- return (0);
-}
+ return 0;
+}
/**
* Init.
*/
-static int gps_collectd_init (void)
+static int cgps_init (void)
{
- int err = 0;
-
- printf ("gps: will use %s:%s with timeout %d.\n", gps_data_config.host, gps_data_config.port, gps_data_config.timeout);
+ int status;
- err = plugin_thread_create (&connector, NULL, gps_collectd_thread, NULL);
+ DEBUG ("gps plugin: config{host: \"%s\", port: \"%s\", timeout: %.3f, pause: %.3f}",
+ config.host, config.port,
+ CDTIME_T_TO_DOUBLE (config.timeout), CDTIME_T_TO_DOUBLE (config.pause));
- if (err != 0) {
- WARNING ("pthread_create() failed.");
+ status = plugin_thread_create (&connector, NULL, gps_collectd_thread, NULL);
+ if (status != 0)
+ {
+ ERROR ("gps plugin: pthread_create() failed.");
return (-1);
}
return (0);
}
-
/**
* Shutdown.
*/
-static int gps_collectd_shutdown (void)
+static int cgps_shutdown (void)
{
- if (connector != ((pthread_t) 0)) {
+ if (connector != ((pthread_t) 0))
+ {
pthread_kill (connector, SIGTERM);
connector = (pthread_t) 0;
}
- sfree (gps_data_config.port);
- sfree (gps_data_config.host);
+ sfree (config.port);
+ sfree (config.host);
return (0);
}
/**
* Register the module.
*/
-void module_register (void)
+void module_register (void)
{
- gps_data_config.host = sstrdup ("localhost");
- gps_data_config.port = sstrdup ("2947");
- gps_data_read.hdop = 0;
- gps_data_read.vdop = 0;
- gps_data_read.satellites = 0;
-
- // Read the config params:
- plugin_register_config ("gps", gps_collectd_config, config_keys, config_keys_num);
- // Create the thread:
- plugin_register_init ("gps", gps_collectd_init);
- // Kill the thread and stop.
- plugin_register_shutdown ("gps", gps_collectd_shutdown);
- // Read plugin:
- plugin_register_read ("gps", gps_collectd_read);
+ config.host = sstrdup (GPS_DEFAULT_HOST);
+ config.port = sstrdup (GPS_DEFAULT_PORT);
+ config.timeout = GPS_DEFAULT_TIMEOUT;
+ config.pause = GPS_DEFAULT_PAUSE;
+
+ plugin_register_complex_config ("gps", cgps_config);
+ plugin_register_init ("gps", cgps_init);
+ plugin_register_read ("gps", cgps_read);
+ plugin_register_shutdown ("gps", cgps_shutdown);
}
-