17 struct owparser_state {
18 double prev_delta[20];
22 static struct owparser_state *allocate_parser_state(const char **datastr)
26 /* Count how many sensor entries we need */
27 for (i = 0; datastr[i]; i++)
30 /* The first entry belongs to server address or mount point */
33 return calloc(sizeof(struct owparser_state), i);
36 static double max_glitch_delta(const struct owparser_state *s)
41 for (i = 0; i < ARRAY_SIZE(s->prev_delta); i++)
42 max_delta = max(s->prev_delta[i], max_delta);
47 static int might_be_glitch(double data, const struct owparser_state *s)
49 double max_delta, delta;
51 max_delta = max_glitch_delta(s);
53 /* Probably no enough data yet, so no glitch detection */
58 * Simple glitch detection. If delta to previous value is more
59 * than twice as larger as any of the older delta, we might
62 delta = fabs(data - s->prev_data);
64 return delta > max_delta * 2;
67 static void update_glitch_data(double data, struct owparser_state *s)
72 for (i = 1; i < ARRAY_SIZE(s->prev_delta); i++) {
73 s->prev_delta[i - 1] = s->prev_delta[i];
74 max_delta = max(s->prev_delta[i], max_delta);
77 /* Avoid storing the first incorrect delta value */
78 if (s->prev_data || max_delta)
79 s->prev_delta[--i] = fabs(data - s->prev_data);
84 static int parse_opts(const char *str, char *ow_path, size_t pathlen,
88 const char *start_str = str;
89 const char offset_str[] = "offset=";
95 * Skip the onewire path entry. Options begin after the first
102 /* Copy the onewire path without options */
103 strncpy(ow_path, start_str, pathlen);
104 ow_path[str - start_str] = '\0';
106 /* Get the next non-space, which is where the argument begins */
111 if (strncmp(str, offset_str, sizeof(offset_str) - 1))
113 str += sizeof(offset_str) - 1;
115 *offset = strtod(str, &endptr);
123 static int make_uncached(char *path, size_t len)
125 char p1[1024], p2[1024], *p = path;
127 if (strstr(path, "/uncached/"))
130 p1[sizeof(p1) - 1] = '\0';
131 p2[sizeof(p2) - 1] = '\0';
134 * Naively assume the "uncached" string can be put after the
137 while (*p && *p != '/')
146 strncpy(p1, path, sizeof(p1) - 1);
147 strncpy(p2, p, sizeof(p2) - 1);
148 snprintf(path, len, "%s/uncached/%s", p1, p2);
153 static int owfs_read(const char *mount_point, const char *path, char **res)
159 snprintf(file, sizeof(file), "%s/%s", mount_point, path);
161 fd = open(file, O_RDONLY | O_CLOEXEC);
163 pr_err("Failed to open file %s: %m\n", file);
167 ret = read(fd, result, sizeof(result));
169 pr_err("Failed to read from file %s: %m\n", file);
173 *res = strndup(result, sizeof(result));
181 static int is_mount_point(const char *str)
184 * Filesystem paths begin with a slash, everything else must
185 * be a network addresses
193 static int onewire_parser(char *rrd_data, const char **parser_data, void **s)
196 const char *server_addr, *mount_point;
197 struct owparser_state *state = *s;
200 int max_str = RRD_DATA_MAX_LEN;
201 int is_mountpoint = is_mount_point(parser_data[0]);
204 pr_err("No parser data available\n");
209 *s = state = allocate_parser_state(parser_data);
212 mount_point = parser_data[0];
215 pr_err("Server address not specified\n");
219 server_addr = parser_data[0];
222 pr_err("Server address not specified\n");
226 h = OWNET_init(server_addr);
228 pr_err("Failed to connect to server %s\n", server_addr);
233 while (parser_data[i]) {
234 double offset = 0, data, prev_data = 85;
240 if (!strcmp("U", parser_data[i])) {
242 ret = snprintf(rrd_data, max_str, "U");
248 parse_opts(parser_data[i], ow_path, sizeof(ow_path), &offset);
255 pr_info("Reading data for entry %s with offset of %.2f\n",
259 ret = owfs_read(mount_point, ow_path, &tmp);
261 ret = OWNET_read(h, ow_path, &tmp);
263 /* Skip leading white space */
265 for (j = 0; j < ret && *tmp2 == ' '; j++)
269 fail = !strncmp(tmp2, "85", 2);
273 if (ret <= 0 || fail)
278 * Older versions of OWNET_read did not NULL
281 memcpy(buf, tmp, min(ret, sizeof(buf) -1));
284 data = strtod(buf, &endptr);
290 * If we read the almost same value as
291 * previously, it's not a glitch
293 if (glitches && prev_data != 85) {
294 double d = max_glitch_delta(&state[i]);
296 if (fabs(data - prev_data) <= d * 2)
300 if (might_be_glitch(data, &state[i]) &&
301 glitches < 4 && retries < 7) {
304 pr_info("Retrying due to a glitch: %f\n", data);
311 * In case of failure, retry with uncached
312 * data. This is likely to help as it forces a
313 * retry even if the sensor is missing from
314 * the cache. We treat "85" also as a failure,
315 * as temp sensors some times report 85 under
318 ret = make_uncached(ow_path, sizeof(ow_path));
319 if (retries >= 10 || ret < 0) {
320 pr_err("Failed to read entry %s: %m\n",
329 update_glitch_data(data, &state[i]);
332 pr_err("Failed to parse data %s\n", buf);
338 ret = snprintf(rrd_data, max_str, "%f", data);
347 ret = snprintf(rrd_data, max_str, ":");
359 static struct parser_info onewire_parser_info = {
361 .parse = onewire_parser,
364 static int init_onewire_parser(void)
366 return register_parser(&onewire_parser_info);
369 struct plugin_info plugin_info = {
370 .name = "onewire_parser",
371 .init = init_onewire_parser,
372 .version = RRDD_VERSION,