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)
128 char p1[1024], p2[1024], *p = path;
130 if (strstr(path, "/uncached/"))
133 p1[sizeof(p1) - 1] = '\0';
134 p2[sizeof(p2) - 1] = '\0';
137 * Naively assume the "uncached" string can be put after the
140 while (*p && *p != '/')
149 strncpy(p1, path, sizeof(p1) - 1);
150 strncpy(p2, p, sizeof(p2) - 1);
151 ret = snprintf(path, len, "%s/uncached/%s", p1, p2);
153 /* No actual data overflow, snprintf just couldn't fit all data in the buffer */
154 if (ret >= RRD_DATA_MAX_LEN)
155 pr_err("Buffer overlfow\n");
160 static int owfs_read(const char *mount_point, const char *path, char **res)
166 snprintf(file, sizeof(file), "%s/%s", mount_point, path);
168 fd = open(file, O_RDONLY | O_CLOEXEC);
170 pr_err("Failed to open file %s: %m\n", file);
174 ret = read(fd, result, sizeof(result));
176 pr_err("Failed to read from file %s: %m\n", file);
180 *res = strndup(result, sizeof(result));
188 static int is_mount_point(const char *str)
191 * Filesystem paths begin with a slash, everything else must
192 * be a network addresses
200 static int onewire_parser(char *rrd_data, const char **parser_data, void **s)
203 const char *server_addr, *mount_point;
204 struct owparser_state *state = *s;
207 int max_str = RRD_DATA_MAX_LEN;
208 int is_mountpoint = is_mount_point(parser_data[0]);
211 pr_err("No parser data available\n");
216 *s = state = allocate_parser_state(parser_data);
219 mount_point = parser_data[0];
222 pr_err("Server address not specified\n");
226 server_addr = parser_data[0];
229 pr_err("Server address not specified\n");
233 h = OWNET_init(server_addr);
235 pr_err("Failed to connect to server %s\n", server_addr);
240 while (parser_data[i]) {
241 double offset = 0, data, prev_data = 85;
247 if (!strcmp("U", parser_data[i])) {
249 ret = snprintf(rrd_data, max_str, "U");
255 parse_opts(parser_data[i], ow_path, sizeof(ow_path), &offset);
262 pr_info("Reading data for entry %s with offset of %.2f\n",
266 ret = owfs_read(mount_point, ow_path, &tmp);
268 ret = OWNET_read(h, ow_path, &tmp);
270 /* Skip leading white space */
272 for (j = 0; j < ret && *tmp2 == ' '; j++)
276 fail = !strncmp(tmp2, "85", 2);
280 if (ret <= 0 || fail)
285 * Older versions of OWNET_read did not NULL
288 memcpy(buf, tmp, min(ret, sizeof(buf) -1));
291 data = strtod(buf, &endptr);
297 * If we read the almost same value as
298 * previously, it's not a glitch
300 if (glitches && prev_data != 85) {
301 double d = max_glitch_delta(&state[i]);
303 if (fabs(data - prev_data) <= d * 2)
307 if (might_be_glitch(data, &state[i]) &&
308 glitches < 4 && retries < 7) {
311 pr_info("Retrying due to a glitch: %f\n", data);
318 * In case of failure, retry with uncached
319 * data. This is likely to help as it forces a
320 * retry even if the sensor is missing from
321 * the cache. We treat "85" also as a failure,
322 * as temp sensors some times report 85 under
325 ret = make_uncached(ow_path, sizeof(ow_path));
326 if (retries >= 10 || ret < 0) {
327 pr_err("Failed to read entry %s: %m\n",
336 update_glitch_data(data, &state[i]);
339 pr_err("Failed to parse data %s\n", buf);
345 ret = snprintf(rrd_data, max_str, "%f", data);
354 ret = snprintf(rrd_data, max_str, ":");
366 static struct parser_info onewire_parser_info = {
368 .parse = onewire_parser,
371 static int init_onewire_parser(void)
373 return register_parser(&onewire_parser_info);
376 struct plugin_info plugin_info = {
377 .name = "onewire_parser",
378 .init = init_onewire_parser,
379 .version = RRDD_VERSION,