]> git.itanic.dy.fi Git - rrdd/blob - parser.c
netstats_parser: Fix crash when network device names are undefined
[rrdd] / parser.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4
5 #include "parser.h"
6 #include "process.h"
7 #include "string.h"
8 #include "debug.h"
9
10 #define STATFILE "/proc/stat"
11
12 int cpu_parser(char *data, void *p)
13 {
14         char buf[1024];
15         char *str = buf;
16         FILE *file = fopen(STATFILE, "r");
17         long long user, nice, sys, idle, wait, irq, softirq;
18
19         if (file == NULL) {
20                 pr_err("Failed to open file %s\n", STATFILE);
21                 return -1;
22         }
23
24         if (!fgets(buf, 1024, file)) {
25                 pr_err("Failed to read file %s\n", STATFILE);
26                 fclose(file);
27                 return -1;
28         }
29
30         user    = dec_to_longlong(str, &str);
31         nice    = dec_to_longlong(str, &str);
32         sys     = dec_to_longlong(str, &str);
33         idle    = dec_to_longlong(str, &str);
34         wait    = dec_to_longlong(str, &str);
35         irq     = dec_to_longlong(str, &str);
36         softirq = dec_to_longlong(str, &str);
37
38         snprintf(data, RRD_DATA_MAX_LEN, "%lld:%lld:%lld:%lld:%lld:%lld:%lld",
39                 user, nice, sys, idle, wait, irq, softirq);
40
41         fclose(file);
42         return 0;
43 }
44
45 #define MEMFILE "/proc/meminfo"
46
47 int mem_parser(char *data, void *p)
48 {
49         char buf[1024], word[1024];
50         int free = 0, buffered = 0, cache = 0, active = 0, inactive = 0,
51                 swapfree = 0, anon = 0, slab = 0, tables = 0, other = 0,
52                 swaptotal = 0, total = 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, NULL);
65                 } else if (!strcmp(word, "MemTotal:")) {
66                         total = dec_to_int(buf, NULL);
67                 } else if (!strcmp(word, "Buffers:")) {
68                         buffered = dec_to_int(buf, NULL);
69                 } else if (!strcmp(word, "Cached:")) {
70                         cache = dec_to_int(buf, NULL);
71                 } else if (!strcmp(word, "Active:")) {
72                         active = dec_to_int(buf, NULL);
73                 } else if (!strcmp(word, "Inactive:")) {
74                         inactive = dec_to_int(buf, NULL);
75                 } else if (!strcmp(word, "SwapFree:")) {
76                         swapfree = dec_to_int(buf, NULL);
77                 } else if (!strcmp(word, "AnonPages:")) {
78                         anon = dec_to_int(buf, NULL);
79                 } else if (!strcmp(word, "Slab:")) {
80                         slab = dec_to_int(buf, NULL);
81                 } else if (!strcmp(word, "PageTables:")) {
82                         tables = dec_to_int(buf, NULL);
83                 } else if (!strcmp(word, "SwapTotal:")) {
84                         swaptotal = dec_to_int(buf, NULL);
85                 }
86         }
87         fclose(file);
88
89         other = total - free - buffered - cache - anon - slab - tables;
90
91         snprintf(data, RRD_DATA_MAX_LEN, "%f:%f:%f:%f:%f:%f:%f:%f:%f:%f:%f",
92                 free / 1024.0,
93                 buffered / 1024.0,
94                 cache / 1024.0,
95                 active / 1024.0,
96                 inactive / 1024.0,
97                 swapfree / 1024.0,
98                 anon / 1024.0,
99                 slab / 1024.0,
100                 tables / 1024.0,
101                 other / 1024.0,
102                 (swaptotal - swapfree) / 1024.0);
103
104         return 0;
105 }
106
107 int cpu_mem_parser(char *data, void *p)
108 {
109         char cpu[1024], mem[1024];
110
111         cpu_parser(cpu, p);
112         mem_parser(mem, p);
113         snprintf(data, RRD_DATA_MAX_LEN, "%s:%s", cpu, mem);
114
115         return 0;
116 }
117
118 int digitemp_parser(char *data, void *p)
119 {
120         const char digitemp_cmd[] = "/usr/bin/digitemp";
121         char *const digitemp_args[] = { "", "-o2", "-a", "-q", 0 };
122         FILE *readf;
123         int pid, ret;
124         float t1 = 0, t2 = 0, t3 = 0;
125         char buf[1024];
126
127         pid = run_piped_stream(digitemp_cmd, digitemp_args, 0, &readf, 0);
128         if (pid < 0) {
129                 pr_err("Failed to parse digitemp\n");
130                 sprintf(data, "U:U");
131                 return -1;
132         }
133
134         ret = fscanf(readf, "%f %f %f", &t1, &t2, &t3);
135
136         if (ret != 3) {
137                 pr_err("Failed to parse digitemp output: %m\n");
138                 sprintf(data, "U:U");
139                 return 1;
140         }
141
142         t2 += 2.16;
143         t3 += -0.44;
144
145         /* Read whatever the process might be still printing out */
146         while (fgets(buf, 1024, readf));
147
148         harvest_zombies(pid);
149         snprintf(data, RRD_DATA_MAX_LEN, "%.2f:%.2f", t3, t2);
150         return 0;
151 }
152
153 int digitemp_parser_mod(char *data, void *p)
154 {
155         char buf[1024];
156         int ret;
157
158         ret = digitemp_parser(buf, p);
159         snprintf(data, RRD_DATA_MAX_LEN, "U:%s", buf);
160
161         return ret;
162 }
163
164 /* Run a command and feed the output from stdout directly to rrdtool */
165 int script_parser(char *rrd_data, void *parser_data)
166 {
167         FILE *readf;
168         char **cmd = parser_data;
169         int pid, ret;
170
171         pid = run_piped_stream(cmd[0], &cmd[1], NULL, &readf, NULL);
172         ret = fread(rrd_data, RRD_DATA_MAX_LEN, 1, readf);
173
174         pr_info("Read %d bytes :%s\n", ret, rrd_data);
175         fclose(readf);
176
177         harvest_zombies(pid);
178
179         return 0;
180 }
181
182 struct iface_stats {
183         long long rx_bytes;
184         long long rx_packets;
185         long long tx_bytes;
186         long long tx_packets;
187 };
188
189 #define PROC_NETDEV     "/proc/net/dev"
190
191 static int get_iface_stats(const char *iface, struct iface_stats *stat)
192 {
193         FILE *netdev;
194         char buf[1024];
195         char if_name[16];
196         char *str;
197         int error;
198
199         netdev = fopen(PROC_NETDEV, "r");
200
201         if (netdev == NULL) {
202                 error = errno;
203                 pr_err("Failed to open file %s: %d (%m)\n",
204                         PROC_NETDEV, error);
205                 return error;
206         }
207
208         while (fgets(buf, sizeof(buf), netdev)) {
209                 get_word(buf, &str, if_name, sizeof(if_name));
210
211                 /* Remove the ':' at the end of the if_name */
212                 if_name[strlen(if_name) - 1] = 0;
213                 if (strncmp(iface, if_name, sizeof(if_name)))
214                         continue;
215
216                 stat->rx_bytes          = dec_to_longlong(str, &str);
217                 stat->rx_packets        = dec_to_longlong(str, &str);
218                 /* errs */                dec_to_longlong(str, &str);
219                 /* drop */                dec_to_longlong(str, &str);
220                 /* fifo */                dec_to_longlong(str, &str);
221                 /* frame */               dec_to_longlong(str, &str);
222                 /* compressed */          dec_to_longlong(str, &str);
223                 /* multicast */           dec_to_longlong(str, &str);
224                 stat->tx_bytes          = dec_to_longlong(str, &str);
225                 stat->tx_packets        = dec_to_longlong(str, &str);
226
227                 pr_info("rx_b %lld rx_p %lld tx_b %lld tx_p %lld\n",
228                         stat->rx_bytes, stat->rx_packets,
229                         stat->tx_bytes, stat->tx_packets);
230
231                 return 0;
232         }
233
234         pr_err("Interface %s not found\n", iface);
235         return -ENODEV;
236 }
237
238 int netstats_parser(char *rrd_data, void *parser_data)
239 {
240         struct iface_stats stat;
241         char **iface_name = parser_data, *original = rrd_data;
242         int max_str = RRD_DATA_MAX_LEN;
243         int ret;
244
245         if (!parser_data) {
246                 pr_err("No device names specified\n");
247                 return -1;
248         }
249
250         while(*iface_name) {
251                 pr_info("getting data for iface %s\n", *iface_name);
252                 ret = get_iface_stats(*iface_name, &stat);
253
254                 if (!ret) {
255                         ret = snprintf(rrd_data, max_str, "%lld:%lld:%lld:%lld",
256                                 stat.rx_bytes, stat.rx_packets,
257                                 stat.tx_bytes, stat.tx_packets);
258                 } else {
259                         ret = snprintf(rrd_data, max_str, "U:U:U:U");
260                 }
261
262                 max_str -= ret;
263                 rrd_data += ret;
264
265                 iface_name++;
266                 if (!*iface_name)
267                         break;
268
269                 ret = snprintf(rrd_data, max_str, ":");
270                 max_str -= ret;
271                 rrd_data += ret;
272         }
273
274         printf("result: %s\n", original);
275         return 0;
276 }