]> git.ipfire.org Git - telemetry.git/commitdiff
proc: Use the new parser to parse PSI information
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 8 Oct 2025 16:07:13 +0000 (16:07 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 8 Oct 2025 16:07:13 +0000 (16:07 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/daemon/proc.c

index e8a6bd9b49458cda11d24ac1ae519eba95673309..c8579eac2a9acd023c82a5d45978435ae58e2665 100644 (file)
@@ -170,31 +170,9 @@ ERROR:
        return r;
 }
 
-static int collecty_proc_read_pressure_line(collecty_ctx* ctx,
-               const char* line, collecty_pressure_stats* stats) {
-       int r;
-
-       // Try parsing the some line
-       r = sscanf(line, "some avg10=%lf avg60=%lf avg300=%lf total=%lu\n",
-                       &stats->some.avg10, &stats->some.avg60, &stats->some.avg300, &stats->some.total);
-       if (r)
-               return r;
-
-       // Try parsing the full line
-       r = sscanf(line, "full avg10=%lf avg60=%lf avg300=%lf total=%lu\n",
-                       &stats->full.avg10, &stats->full.avg60, &stats->full.avg300, &stats->full.total);
-       if (r)
-               return r;
-
-       return 0;
-}
-
 int collecty_proc_read_pressure(collecty_ctx* ctx,
                const char* what, collecty_pressure_stats* stats) {
        char path[PATH_MAX];
-       char* line = NULL;
-       size_t length = 0;
-       FILE* f = NULL;
        int r;
 
        // Make the path
@@ -202,36 +180,16 @@ int collecty_proc_read_pressure(collecty_ctx* ctx,
        if (r < 0)
                return -errno;
 
-       // Open the file
-       f = fopen(path, "r");
-       if (!f) {
-               ERROR(ctx, "Failed to open %s: %m\n", path);
-               r = -errno;
-               goto ERROR;
-       }
-
-       // Walk through all lines
-       for (;;) {
-               r = getline(&line, &length, f);
-               if (r < 0)
-                       break;
-
-               // Process the line
-               r = collecty_proc_read_pressure_line(ctx, line, stats);
-               if (r < 0)
-                       goto ERROR;
-       }
-
-       // Reset r
-       r = 0;
-
-ERROR:
-       if (line)
-               free(line);
-       if (f)
-               fclose(f);
+       collecty_file_parser parser[] = {
+               PARSE4("some avg10=%lf avg60=%lf avg300=%lf total=%lu\n",
+                       &stats->some.avg10, &stats->some.avg60, &stats->some.avg300, &stats->some.total),
+               PARSE4("full avg10=%lf avg60=%lf avg300=%lf total=%lu\n",
+                       &stats->full.avg10, &stats->full.avg60, &stats->full.avg300, &stats->full.total),
+               { NULL },
+       };
 
-       return r;
+       // Run the parser
+       return collecty_parse(ctx, path, parser);
 }
 
 typedef struct collecty_proc_softirq_state {