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 int might_be_glitch(double data, const struct owparser_state *s)
38 double max_delta = 0, delta;
41 for (i = 0; i < ARRAY_SIZE(s->prev_delta); i++)
42 max_delta = max(s->prev_delta[i], max_delta);
44 /* Probably no enough data yet, so no glitch detection */
49 * Simple glitch detection. If delta to previous value is more
50 * than twice as larger as some older delta, we might have a
53 delta = fabs(data - s->prev_data);
54 return delta > max_delta * 2;
57 static void update_glitch_data(double data, struct owparser_state *s)
62 for (i = 1; i < ARRAY_SIZE(s->prev_delta); i++) {
63 s->prev_delta[i - 1] = s->prev_delta[i];
64 max_delta = max(s->prev_delta[i], max_delta);
67 /* Avoid storing the first incorrect delta value */
68 if (s->prev_data || max_delta)
69 s->prev_delta[--i] = fabs(data - s->prev_data);
74 static int parse_opts(const char *str, char *ow_path, size_t pathlen,
78 const char *start_str = str;
79 const char offset_str[] = "offset=";
85 * Skip the onewire path entry. Options begin after the first
92 /* Copy the onewire path without options */
93 strncpy(ow_path, start_str, pathlen);
94 ow_path[str - start_str] = '\0';
96 /* Get the next non-space, which is where the argument begins */
101 if (strncmp(str, offset_str, sizeof(offset_str) - 1))
103 str += sizeof(offset_str) - 1;
105 *offset = strtod(str, &endptr);
113 static int make_uncached(char *path, size_t len)
115 char p1[1024], p2[1024], *p = path;
117 if (strstr(path, "/uncached/"))
121 * Naively assume the "uncached" string can be put after the
124 while (*p && *p != '/')
133 strncpy(p1, path, sizeof(p1));
134 strncpy(p2, p, sizeof(p2));
135 snprintf(path, len, "%s/uncached/%s", p1, p2);
140 static int owfs_read(const char *mount_point, const char *path, char **res)
146 snprintf(file, sizeof(file), "%s/%s", mount_point, path);
148 fd = open(file, O_RDONLY | O_CLOEXEC);
150 pr_err("Failed to open file %s: %m\n", file);
154 ret = read(fd, result, sizeof(result));
156 pr_err("Failed to read from file %s: %m\n", file);
160 *res = strndup(result, sizeof(result));
168 static int is_mount_point(const char *str)
171 * Filesystem paths begin with a slash, everything else must
172 * be a network addresses
180 static int onewire_parser(char *rrd_data, const char **parser_data, void **s)
183 const char *server_addr, *mount_point;
184 struct owparser_state *state = *s;
187 int max_str = RRD_DATA_MAX_LEN;
188 int is_mountpoint = is_mount_point(parser_data[0]);
191 pr_err("No parser data available\n");
196 *s = state = allocate_parser_state(parser_data);
199 mount_point = parser_data[0];
202 pr_err("Server address not specified\n");
206 server_addr = parser_data[0];
209 pr_err("Server address not specified\n");
213 h = OWNET_init(server_addr);
215 pr_err("Failed to connect to server %s\n", server_addr);
220 while (parser_data[i]) {
221 double offset = 0, data, prev_data = 85;
227 if (!strcmp("U", parser_data[i])) {
229 ret = snprintf(rrd_data, max_str, "U");
235 parse_opts(parser_data[i], ow_path, sizeof(ow_path), &offset);
242 pr_info("Reading data for entry %s with offset of %.2f\n",
246 ret = owfs_read(mount_point, ow_path, &tmp);
248 ret = OWNET_read(h, ow_path, &tmp);
250 /* Skip leading white space */
252 for (j = 0; j < ret && *tmp2 == ' '; j++)
256 fail = !strncmp(tmp2, "85", 2);
260 if (ret <= 0 || fail)
265 * Older versions of OWNET_read did not NULL
268 memcpy(buf, tmp, min(ret, sizeof(buf) -1));
271 data = strtod(buf, &endptr);
277 * If we read the same value as previously,
280 if (glitches && prev_data == data)
283 if (might_be_glitch(data, &state[i]) &&
284 glitches < 2 && retries < 7) {
287 pr_info("Retrying due to a glitch: %f\n", data);
294 * In case of failure, retry with uncached
295 * data. This is likely to help as it forces a
296 * retry even if the sensor is missing from
297 * the cache. We treat "85" also as a failure,
298 * as temp sensors some times report 85 under
301 ret = make_uncached(ow_path, sizeof(ow_path));
302 if (retries >= 10 || ret < 0) {
303 pr_err("Failed to read entry %s: %m\n",
312 update_glitch_data(data, &state[i]);
315 pr_err("Failed to parse data %s\n", buf);
321 ret = snprintf(rrd_data, max_str, "%f", data);
330 ret = snprintf(rrd_data, max_str, ":");
342 static struct parser_info onewire_parser_info = {
344 .parse = onewire_parser,
347 static int init_onewire_parser(void)
349 return register_parser(&onewire_parser_info);
352 struct plugin_info plugin_info = {
353 .name = "onewire_parser",
354 .init = init_onewire_parser,
355 .version = RRDD_VERSION,