2 * This plug-in helps to monitor the GPS connected to a system.
3 * It reads the data comming from GPSd.
4 It look for the following parameters.
25 static gps_definition_t gps_data_config;
32 static gpsDATA_t gps_data_read;
35 static const char *config_keys[] =
41 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
45 static pthread_t connector = (pthread_t) 0;
46 static pthread_mutex_t data_mutex = PTHREAD_MUTEX_INITIALIZER;
50 * Thread reading from GPSd.
52 static void * gps_collectd_thread (void * pData)
54 struct gps_data_t gps_data;
58 if (gps_open(gps_data_config.host, gps_data_config.port, &gps_data) < 0)
60 printf ("cannot connect to: %s:%s", gps_data_config.host, gps_data_config.port);
65 gps_stream(&gps_data, WATCH_ENABLE | WATCH_JSON, NULL);
69 if (gps_waiting (&gps_data, gps_data_config.timeout))
71 if (gps_read (&gps_data) == -1)
73 WARNING ("incorrect data.\n");
76 pthread_mutex_lock (&data_mutex);
79 if (isnan(gps_data.dop.vdop) == 0)
81 gps_data_read.vdop = gps_data.dop.vdop;
83 if (isnan(gps_data.dop.hdop) == 0)
85 gps_data_read.hdop = gps_data.dop.hdop;
89 if ((gps_data.set & LATLON_SET))
91 gps_data_read.satellites = gps_data.satellites_used;
94 pthread_mutex_unlock (&data_mutex);
100 gps_stream(&gps_data, WATCH_DISABLE, NULL);
101 gps_close(&gps_data);
103 pthread_exit ((void *)0);
110 static void gps_collectd_submit (const char *type, gauge_t value)
113 value_list_t vl = VALUE_LIST_INIT;
115 values[0].gauge = value;
119 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
120 sstrncpy (vl.plugin, "gps", sizeof (vl.plugin));
121 sstrncpy (vl.type, type, sizeof (vl.type));
122 sstrncpy (vl.type_instance, "gps", sizeof (vl.type_instance));
124 plugin_dispatch_values (&vl);
129 * Read the data and submit.
131 static int gps_collectd_read ()
133 pthread_mutex_lock (&data_mutex);
134 gps_collectd_submit("gps_hdop", (gauge_t) gps_data_read.hdop);
135 gps_collectd_submit("gps_vdop", (gauge_t) gps_data_read.vdop);
136 gps_collectd_submit("gps_sat", (gauge_t) gps_data_read.satellites);
137 printf ("gps: hdop=%1.3f, vdop=%1.3f, sat=%02d.\n",
140 gps_data_read.satellites
142 pthread_mutex_unlock (&data_mutex);
148 * Read configuration.
150 static int gps_collectd_config (const char *key, const char *value)
152 if (strcasecmp (key, "Host") == 0) {
153 if (gps_data_config.host != NULL) free (gps_data_config.host);
154 gps_data_config.host = sstrdup (value);
156 if (strcasecmp (key, "Port") == 0) {
157 if (gps_data_config.port != NULL) free (gps_data_config.port);
158 gps_data_config.port = sstrdup (value);
160 if (strcasecmp (key, "Timeout") == 0) {
161 gps_data_config.timeout = (int) strtol (value, NULL, 1000);
170 static int gps_collectd_init (void)
174 printf ("gps: will use %s:%s with timeout %d.\n", gps_data_config.host, gps_data_config.port, gps_data_config.timeout);
176 err = plugin_thread_create (&connector, NULL, gps_collectd_thread, NULL);
179 WARNING ("pthread_create() failed.");
190 static int gps_collectd_shutdown (void)
192 if (connector != ((pthread_t) 0)) {
193 pthread_kill (connector, SIGTERM);
194 connector = (pthread_t) 0;
197 sfree (gps_data_config.port);
198 sfree (gps_data_config.host);
204 * Register the module.
206 void module_register (void)
208 gps_data_config.host = sstrdup ("localhost");
209 gps_data_config.port = sstrdup ("2947");
210 gps_data_read.hdop = 0;
211 gps_data_read.vdop = 0;
212 gps_data_read.satellites = 0;
214 // Read the config params:
215 plugin_register_config ("gps", gps_collectd_config, config_keys, config_keys_num);
216 // Create the thread:
217 plugin_register_init ("gps", gps_collectd_init);
218 // Kill the thread and stop.
219 plugin_register_shutdown ("gps", gps_collectd_shutdown);
221 plugin_register_read ("gps", gps_collectd_read);