]> git.itanic.dy.fi Git - rrdd/blob - parser.c
Fix 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
9 static int dec_to_int(char *src, char **dst)
10 {
11         int ret;
12
13         while(((*src < '0') || (*src > '9')) && *src)
14                 src++;
15
16         ret = atoi(src);
17
18         if (dst) {
19                 while(((*src >= '0') && (*src <= '9')) && *src)
20                         src++;
21                 *dst = src;
22         }
23
24         return ret;
25 }
26
27 static float dec_to_float(char *src, char **dst)
28 {
29         float ret;
30
31         while(((*src < '0') || (*src > '9')) && *src)
32                 src++;
33
34         ret = atof(src);
35
36         if (dst) {
37                 while((((*src >= '0') && (*src <= '9')) || (*src =='.')) && *src)
38                         src++;
39                 *dst = src;
40         }
41
42         return ret;
43 }
44
45 /*
46  * Copy white space separated stirng from *src to *word
47  *
48  * src:  source
49  * dst:  place where store the end of word pointer from source string
50  * word: destination where to copy the string
51  * size: maximum size of bytes to copy to the output buffer
52  *
53  * return the number of bytes copied
54  */
55
56 static int get_word(char *src, char **dst, char *word, int size)
57 {
58         int ret = 0;
59         while(((*src == ' ') || (*src == '\t') || (*src == '\n')) && *src)
60                 src++;
61
62         while(((*src != ' ') && (*src != '\t') && (*src != '\n')) && *src && (ret < (size - 1))) {
63                 *word = *src;
64                 word++;
65                 src++;
66                 ret++;
67         }
68         *word = 0;
69
70         if (dst)
71                 *dst = src;
72
73         return ret;
74 }
75
76 #define STATFILE "/proc/stat"
77
78 int cpu_parser(char *data)
79 {
80         char buf[1024];
81         char *str = buf;
82         FILE *file = fopen(STATFILE, "r");
83         int user, nice, sys, idle, wait, irq, softirq;
84
85         if (file == NULL) {
86                 fprintf(stderr, "Failed to open file %s\n", STATFILE);
87                 return -1;
88         }
89
90         if (!fgets(buf, 1024, file)) {
91                 fprintf(stderr, "Failed to read file %s\n", STATFILE);
92                 fclose(file);
93                 return -1;
94         }
95
96         user    = dec_to_int(str, &str);
97         nice    = dec_to_int(str, &str);
98         sys     = dec_to_int(str, &str);
99         idle    = dec_to_int(str, &str);
100         wait    = dec_to_int(str, &str);
101         irq     = dec_to_int(str, &str);
102         softirq = dec_to_int(str, &str);
103
104         sprintf(data, "%d:%d:%d:%d:%d:%d:%d",
105                 user, nice, sys, idle, wait, irq, softirq);
106
107         fclose(file);
108         return 0;
109 }
110
111 #define MEMFILE "/proc/meminfo"
112
113 int mem_parser(char *data)
114 {
115         char buf[1024], word[1024];
116         int free = 0, buffered = 0, cache = 0, active = 0, inactive = 0,
117                 swapfree = 0, anon = 0, slab = 0, tables = 0;
118         FILE *file = fopen(MEMFILE, "r");
119
120         if (file == NULL) {
121                 fprintf(stderr, "Failed to open file %s\n", MEMFILE);
122                 return -1;
123         }
124
125         while (fgets(buf, 1024, file)) {
126                 get_word(buf, 0, word, 1024);
127                 
128                 if (!strcmp(word, "MemFree:")) {
129                         free = dec_to_int(buf, 0);
130                 } else if (!strcmp(word, "Buffers:")) {
131                         buffered = dec_to_int(buf, 0);
132                 } else if (!strcmp(word, "Cached:")) {
133                         cache = dec_to_int(buf, 0);
134                 } else if (!strcmp(word, "Active:")) {
135                         active = dec_to_int(buf, 0);
136                 } else if (!strcmp(word, "Inactive:")) {
137                         inactive = dec_to_int(buf, 0);
138                 } else if (!strcmp(word, "SwapFree:")) {
139                         swapfree = dec_to_int(buf, 0);
140                 } else if (!strcmp(word, "AnonPages:")) {
141                                 anon = dec_to_int(buf, 0);
142                 } else if (!strcmp(word, "Slab:")) {
143                         slab = dec_to_int(buf, 0);
144                 } else if (!strcmp(word, "PageTables:")) {
145                         tables = dec_to_int(buf, 0);
146                 }
147         }
148         fclose(file);
149
150         sprintf(data, "%f:%f:%f:%f:%f:%f:%f:%f:%f",
151                 free / 1024.0,
152                 buffered / 1024.0,
153                 cache / 1024.0,
154                 active / 1024.0,
155                 inactive / 1024.0,
156                 swapfree / 1024.0,
157                 anon / 1024.0,
158                 slab / 1024.0,
159                 tables / 1024.0);
160
161         return 0;
162 }
163
164 int cpu_mem_parser(char *data)
165 {
166         char cpu[1024], mem[1024];
167
168         cpu_parser(cpu);
169         mem_parser(mem);
170         sprintf(data, "%s:%s", cpu, mem);
171
172         return 0;
173 }
174
175 int digitemp_parser(char *data)
176 {
177         const char digitemp_cmd[] = "/usr/bin/digitemp";
178         char *const digitemp_args[] = { "", "-o2", "-a", "-q", 0 };
179         FILE *readf, *writef;
180         int pid;
181         char buf[1024], *bptr = buf;
182         float t1, t2, t3;
183
184         pid = run_piped_stream(digitemp_cmd, digitemp_args, &readf, &writef);
185         if (pid < 0) {
186                 fprintf(stderr, "Failed to parse digitemp");
187                 sprintf(data, "U:U");
188                 return -1;
189         }
190
191         fgets(buf, 1024, readf);
192
193 /*
194         get_word(bptr, &bptr, t1, 16);
195         *bptr++ = 0;
196         get_word(bptr, &bptr, t2, 16);
197         *bptr++ = 0;
198         get_word(bptr, &bptr, t3, 16);
199         *bptr++ = 0;
200 */
201         t1 = dec_to_float(bptr, &bptr);
202         t2 = dec_to_float(bptr, &bptr);
203         t3 = dec_to_float(bptr, &bptr);
204
205         t2 += 2.16;
206         t3 += -0.44;
207
208         while (fgets(buf, 1024, readf));
209         harvest_zombies(pid);
210         sprintf(data, "%.2f:%.2f", t3, t2);
211         return 0;
212 }
213
214 int digitemp_parser_mod(char *data)
215 {
216         char buf[1024];
217         int ret;
218
219         ret = digitemp_parser(buf);
220         sprintf(data, "U:%s", buf);
221
222         return ret;
223 }