From: Florian Forster Date: Fri, 20 Nov 2015 13:57:07 +0000 (+0100) Subject: gps plugin: Store timeout and pause as cdtime_t. X-Git-Tag: collectd-5.6.0~73^2~7^2~1 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=f3b18c23c1ec9b844a84894ef6b3de804e4a840a;p=collectd.git gps plugin: Store timeout and pause as cdtime_t. This allows to specify sub-second precision in the usual manner in the config file. --- diff --git a/src/gps.c b/src/gps.c index b4a273eb..cdbad791 100644 --- a/src/gps.c +++ b/src/gps.c @@ -32,8 +32,8 @@ #define GPS_DEFAULT_HOST "localhost" #define GPS_DEFAULT_PORT "2947" -#define GPS_DEFAULT_TIMEOUT 15 -#define GPS_DEFAULT_PAUSE 1 +#define GPS_DEFAULT_TIMEOUT TIME_T_TO_CDTIME_T (15) +#define GPS_DEFAULT_PAUSE TIME_T_TO_CDTIME_T (1) #include #include @@ -41,8 +41,8 @@ typedef struct { char *host; char *port; - int timeout; - int pause; + cdtime_t timeout; + cdtime_t pause; } cgps_config_t; typedef struct { @@ -52,15 +52,6 @@ typedef struct { gauge_t vdop; } cgps_data_t; -static const char *config_keys[] = -{ - "Host", - "Port", - "Timeout", - "Pause" -}; -static int config_keys_num = STATIC_ARRAY_SIZE (config_keys); - // Thread items: static pthread_t connector = (pthread_t) 0; @@ -92,9 +83,12 @@ static void * gps_collectd_thread (void * pData) while (1) { - if (!gps_waiting (&conn, config.timeout)) + long timeout_us = CDTIME_T_TO_US (config.timeout); + if (!gps_waiting (&conn, (int) timeout_us)) { - sleep (config.pause); + struct timespec pause_ns; + CDTIME_T_TO_TIMESPEC (config.pause, &pause_ns); + nanosleep (&pause_ns, NULL); continue; } @@ -174,30 +168,27 @@ static int cgps_read () /** * Read configuration. */ -static int cgps_config (const char *key, const char *value) +static int cgps_config (oconfig_item_t *ci) { - char *endptr = NULL; + int i; - if (strcasecmp (key, "Host") == 0) + for (i = 0; i < ci->children_num; i++) { - free (config.host); - config.host = sstrdup (value); - } - else if (strcasecmp (key, "Port") == 0) - { - free (config.port); - config.port = sstrdup (value); - } - else if (strcasecmp (key, "Timeout") == 0) - { - config.timeout = (int) (strtod(value, &endptr) * 1000); - } - else if (strcasecmp (key, "Pause") == 0) - { - config.pause = (int) (strtod (value, &endptr)); + 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; } /** @@ -207,8 +198,9 @@ static int cgps_init (void) { int status; - DEBUG ("gps plugin: config{host: \"%s\", port: \"%s\", timeout: %d, pause: %d}", - config.host, config.port, config.timeout, config.pause); + 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)); status = plugin_thread_create (&connector, NULL, gps_collectd_thread, NULL); if (status != 0) @@ -247,7 +239,7 @@ void module_register (void) config.timeout = GPS_DEFAULT_TIMEOUT; config.pause = GPS_DEFAULT_PAUSE; - plugin_register_config ("gps", cgps_config, config_keys, config_keys_num); + 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); diff --git a/src/gps.pod b/src/gps.pod index 540a1627..3148299b 100644 --- a/src/gps.pod +++ b/src/gps.pod @@ -23,28 +23,28 @@ LoadPlugin gps The C connects to gpsd on the host machine. The host, port, timeout and pause are configurable. -This is useful if you run an NTP server using a GPS for source and you want to monitor it. - -Mind your GPS must send $--GSA for having the data reported ! +This is useful if you run an NTP server using a GPS for source and you want to +monitor it. +Mind your GPS must send $--GSA for having the data reported! =head1 OPTIONS =over 4 -=item B +=item B I -The host on which gpsd runs (default localhost). +The host on which gpsd runs. Defaults to B. -=item B +=item B I -Port to connect to gpsd on the host machine (with quotes), (default 2947). +Port to connect to gpsd on the host machine. Defaults to B<2947>. -=item B +=item B I Timeout in seconds (default 15 sec). -=item B +=item B I Pause to apply between readings in seconds (default 1 sec). @@ -56,16 +56,15 @@ Pause to apply between readings in seconds (default 1 sec). =item B -Number of satellites in view. -0 means no GPS are visible. +Number of satellites used for fix (type instance "used") and in view (type +instance "visible"). 0 means no GPS satellites are visible. =item B -Vertical or horizontal dilution. +Vertical and horizontal dilution. It should be between 0 and 3. Look at the documentaiton of your GPS to know more. - =head1 SEE ALSO L,