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