]> git.itanic.dy.fi Git - rrdd/blob - parser.c
Add primitive error detection to digitemp parser
[rrdd] / parser.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <error.h>
5
6 #include "parser.h"
7 #include "process.h"
8 #include "string.h"
9 #include "debug.h"
10
11 #define STATFILE "/proc/stat"
12
13 int cpu_parser(char *data)
14 {
15         char buf[1024];
16         char *str = buf;
17         FILE *file = fopen(STATFILE, "r");
18         int user, nice, sys, idle, wait, irq, softirq;
19
20         if (file == NULL) {
21                 pr_err("Failed to open file %s\n", STATFILE);
22                 return -1;
23         }
24
25         if (!fgets(buf, 1024, file)) {
26                 pr_err("Failed to read file %s\n", STATFILE);
27                 fclose(file);
28                 return -1;
29         }
30
31         user    = dec_to_int(str, &str);
32         nice    = dec_to_int(str, &str);
33         sys     = dec_to_int(str, &str);
34         idle    = dec_to_int(str, &str);
35         wait    = dec_to_int(str, &str);
36         irq     = dec_to_int(str, &str);
37         softirq = dec_to_int(str, &str);
38
39         sprintf(data, "%d:%d:%d:%d:%d:%d:%d",
40                 user, nice, sys, idle, wait, irq, softirq);
41
42         fclose(file);
43         return 0;
44 }
45
46 #define MEMFILE "/proc/meminfo"
47
48 int mem_parser(char *data)
49 {
50         char buf[1024], word[1024];
51         int free = 0, buffered = 0, cache = 0, active = 0, inactive = 0,
52                 swapfree = 0, anon = 0, slab = 0, tables = 0;
53         FILE *file = fopen(MEMFILE, "r");
54
55         if (file == NULL) {
56                 pr_err("Failed to open file %s\n", MEMFILE);
57                 return -1;
58         }
59
60         while (fgets(buf, 1024, file)) {
61                 get_word(buf, 0, word, 1024);
62                 
63                 if (!strcmp(word, "MemFree:")) {
64                         free = dec_to_int(buf, 0);
65                 } else if (!strcmp(word, "Buffers:")) {
66                         buffered = dec_to_int(buf, 0);
67                 } else if (!strcmp(word, "Cached:")) {
68                         cache = dec_to_int(buf, 0);
69                 } else if (!strcmp(word, "Active:")) {
70                         active = dec_to_int(buf, 0);
71                 } else if (!strcmp(word, "Inactive:")) {
72                         inactive = dec_to_int(buf, 0);
73                 } else if (!strcmp(word, "SwapFree:")) {
74                         swapfree = dec_to_int(buf, 0);
75                 } else if (!strcmp(word, "AnonPages:")) {
76                                 anon = dec_to_int(buf, 0);
77                 } else if (!strcmp(word, "Slab:")) {
78                         slab = dec_to_int(buf, 0);
79                 } else if (!strcmp(word, "PageTables:")) {
80                         tables = dec_to_int(buf, 0);
81                 }
82         }
83         fclose(file);
84
85         sprintf(data, "%f:%f:%f:%f:%f:%f:%f:%f:%f",
86                 free / 1024.0,
87                 buffered / 1024.0,
88                 cache / 1024.0,
89                 active / 1024.0,
90                 inactive / 1024.0,
91                 swapfree / 1024.0,
92                 anon / 1024.0,
93                 slab / 1024.0,
94                 tables / 1024.0);
95
96         return 0;
97 }
98
99 int cpu_mem_parser(char *data)
100 {
101         char cpu[1024], mem[1024];
102
103         cpu_parser(cpu);
104         mem_parser(mem);
105         sprintf(data, "%s:%s", cpu, mem);
106
107         return 0;
108 }
109
110 int digitemp_parser(char *data)
111 {
112         const char digitemp_cmd[] = "/usr/bin/digitemp";
113         char *const digitemp_args[] = { "", "-o2", "-a", "-q", 0 };
114         FILE *readf;
115         int pid, ret, error;
116         float t1 = 0, t2 = 0, t3 = 0;
117         char buf[1024];
118
119         pid = run_piped_stream(digitemp_cmd, digitemp_args, 0, &readf, 0);
120         if (pid < 0) {
121                 pr_err("Failed to parse digitemp\n");
122                 sprintf(data, "U:U");
123                 return -1;
124         }
125
126         ret = fscanf(readf, "%f %f %f", t1, t2, t3);
127
128         if (ret != 3) {
129                 error = errno;
130                 pr_err("Failed to parse digitemp output: %s\n",
131                        strerror(error));
132                 sprintf(data, "U:U");
133                 return 1;
134         }
135
136         t2 += 2.16;
137         t3 += -0.44;
138
139         /* Read whatever the process might be still printing out */
140         while (fgets(buf, 1024, readf));
141
142         harvest_zombies(pid);
143         sprintf(data, "%.2f:%.2f", t3, t2);
144         return 0;
145 }
146
147 int digitemp_parser_mod(char *data)
148 {
149         char buf[1024];
150         int ret;
151
152         ret = digitemp_parser(buf);
153         sprintf(data, "U:%s", buf);
154
155         return ret;
156 }