17 struct owparser_state {
18 double prev_delta[20];
22 static struct owparser_state *allocate_parser_state(const char **datastr)
27 * Count how many sensor entries we need. First entry belongs
28 * to server address or mount point and last one is NULL. So
29 * the index final is the count of actual valid sensor
32 for (i = 0; datastr[i]; i++)
35 return calloc(sizeof(struct owparser_state), i);
38 static double max_glitch_delta(const struct owparser_state *s)
43 for (i = 0; i < ARRAY_SIZE(s->prev_delta); i++)
44 max_delta = max(s->prev_delta[i], max_delta);
49 static int might_be_glitch(double data, const struct owparser_state *s)
51 double max_delta, delta;
53 max_delta = max_glitch_delta(s);
55 /* Probably no enough data yet, so no glitch detection */
60 * Simple glitch detection. If delta to previous value is more
61 * than twice as larger as any of the older delta, we might
64 delta = fabs(data - s->prev_data);
66 return delta > max_delta * 2;
69 static void update_glitch_data(double data, struct owparser_state *s)
74 for (i = 1; i < ARRAY_SIZE(s->prev_delta); i++) {
75 s->prev_delta[i - 1] = s->prev_delta[i];
76 max_delta = max(s->prev_delta[i], max_delta);
79 /* Avoid storing the first incorrect delta value */
80 if (s->prev_data || max_delta)
81 s->prev_delta[--i] = fabs(data - s->prev_data);
86 static int parse_opts(const char *str, char *ow_path, size_t pathlen,
90 const char *start_str = str;
91 const char offset_str[] = "offset=";
97 * Skip the onewire path entry. Options begin after the first
104 /* Copy the onewire path without options */
105 strncpy(ow_path, start_str, pathlen);
106 ow_path[str - start_str] = '\0';
108 /* Get the next non-space, which is where the argument begins */
113 if (strncmp(str, offset_str, sizeof(offset_str) - 1))
115 str += sizeof(offset_str) - 1;
117 *offset = strtod(str, &endptr);
125 static int make_uncached(char *path, size_t len)
127 char p1[1024], p2[1024], *p = path;
129 if (strstr(path, "/uncached/"))
132 p1[sizeof(p1) - 1] = '\0';
133 p2[sizeof(p2) - 1] = '\0';
136 * Naively assume the "uncached" string can be put after the
139 while (*p && *p != '/')
148 strncpy(p1, path, sizeof(p1) - 1);
149 strncpy(p2, p, sizeof(p2) - 1);
150 snprintf(path, len, "%s/uncached/%s", p1, p2);
155 static int owfs_read(const char *mount_point, const char *path, char **res)
161 snprintf(file, sizeof(file), "%s/%s", mount_point, path);
163 fd = open(file, O_RDONLY | O_CLOEXEC);
165 pr_err("Failed to open file %s: %m\n", file);
169 ret = read(fd, result, sizeof(result));
171 pr_err("Failed to read from file %s: %m\n", file);
175 *res = strndup(result, sizeof(result));
183 static int is_mount_point(const char *str)
186 * Filesystem paths begin with a slash, everything else must
187 * be a network addresses
195 static int onewire_parser(char *rrd_data, const char **parser_data, void **s)
198 const char *server_addr, *mount_point;
199 struct owparser_state *state = *s;
202 int max_str = RRD_DATA_MAX_LEN;
203 int is_mountpoint = is_mount_point(parser_data[0]);
206 pr_err("No parser data available\n");
211 *s = state = allocate_parser_state(parser_data);
214 mount_point = parser_data[0];
217 pr_err("Server address not specified\n");
221 server_addr = parser_data[0];
224 pr_err("Server address not specified\n");
228 h = OWNET_init(server_addr);
230 pr_err("Failed to connect to server %s\n", server_addr);
235 while (parser_data[i]) {
236 double offset = 0, data, prev_data = 85;
242 if (!strcmp("U", parser_data[i])) {
244 ret = snprintf(rrd_data, max_str, "U");
250 parse_opts(parser_data[i], ow_path, sizeof(ow_path), &offset);
257 pr_info("Reading data for entry %s with offset of %.2f\n",
261 ret = owfs_read(mount_point, ow_path, &tmp);
263 ret = OWNET_read(h, ow_path, &tmp);
265 /* Skip leading white space */
267 for (j = 0; j < ret && *tmp2 == ' '; j++)
271 fail = !strncmp(tmp2, "85", 2);
275 if (ret <= 0 || fail)
280 * Older versions of OWNET_read did not NULL
283 memcpy(buf, tmp, min(ret, sizeof(buf) -1));
286 data = strtod(buf, &endptr);
292 * If we read the almost same value as
293 * previously, it's not a glitch
295 if (glitches && prev_data != 85) {
296 double d = max_glitch_delta(&state[i]);
298 if (fabs(data - prev_data) <= d * 2)
302 if (might_be_glitch(data, &state[i]) &&
303 glitches < 4 && retries < 7) {
306 pr_info("Retrying due to a glitch: %f\n", data);
313 * In case of failure, retry with uncached
314 * data. This is likely to help as it forces a
315 * retry even if the sensor is missing from
316 * the cache. We treat "85" also as a failure,
317 * as temp sensors some times report 85 under
320 ret = make_uncached(ow_path, sizeof(ow_path));
321 if (retries >= 10 || ret < 0) {
322 pr_err("Failed to read entry %s: %m\n",
331 update_glitch_data(data, &state[i]);
334 pr_err("Failed to parse data %s\n", buf);
340 ret = snprintf(rrd_data, max_str, "%f", data);
349 ret = snprintf(rrd_data, max_str, ":");
361 static struct parser_info onewire_parser_info = {
363 .parse = onewire_parser,
366 static int init_onewire_parser(void)
368 return register_parser(&onewire_parser_info);
371 struct plugin_info plugin_info = {
372 .name = "onewire_parser",
373 .init = init_onewire_parser,
374 .version = RRDD_VERSION,