10 #define STATFILE "/proc/stat"
12 int cpu_parser(char *data, const char **p)
16 FILE *file = fopen(STATFILE, "r");
17 long long user, nice, sys, idle, wait, irq, softirq;
20 pr_err("Failed to open file %s\n", STATFILE);
24 if (!fgets(buf, 1024, file)) {
25 pr_err("Failed to read file %s\n", STATFILE);
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);
38 snprintf(data, RRD_DATA_MAX_LEN, "%lld:%lld:%lld:%lld:%lld:%lld:%lld",
39 user, nice, sys, idle, wait, irq, softirq);
45 #define MEMFILE "/proc/meminfo"
47 int mem_parser(char *data, const char **p)
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");
56 pr_err("Failed to open file %s\n", MEMFILE);
60 while (fgets(buf, 1024, file)) {
61 get_word(buf, 0, word, 1024);
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);
89 other = total - free - buffered - cache - anon - slab - tables;
91 snprintf(data, RRD_DATA_MAX_LEN, "%f:%f:%f:%f:%f:%f:%f:%f:%f:%f:%f",
102 (swaptotal - swapfree) / 1024.0);
107 int cpu_mem_parser(char *data, const char **p)
109 char cpu[1024], mem[1024];
113 snprintf(data, RRD_DATA_MAX_LEN, "%s:%s", cpu, mem);
118 int digitemp_parser(char *data, const char **p)
120 const char digitemp_cmd[] = "/usr/bin/digitemp";
121 char *const digitemp_args[] = { "", "-o2", "-a", "-q", 0 };
124 float t1 = 0, t2 = 0, t3 = 0;
127 pid = run_piped_stream(digitemp_cmd, digitemp_args, 0, &readf, 0);
129 pr_err("Failed to parse digitemp\n");
130 sprintf(data, "U:U");
134 ret = fscanf(readf, "%f %f %f", &t1, &t2, &t3);
137 pr_err("Failed to parse digitemp output: %m\n");
138 sprintf(data, "U:U");
145 /* Read whatever the process might be still printing out */
146 while (fgets(buf, 1024, readf));
148 harvest_zombies(pid);
149 snprintf(data, RRD_DATA_MAX_LEN, "%.2f:%.2f", t3, t2);
153 int digitemp_parser_mod(char *data, const char **p)
158 ret = digitemp_parser(buf, p);
159 snprintf(data, RRD_DATA_MAX_LEN, "U:%s", buf);
164 /* Run a command and feed the output from stdout directly to rrdtool */
165 int script_parser(char *rrd_data, const char **parser_data)
169 void *tmp = parser_data;
172 pid = run_piped_stream(cmd[0], &cmd[1], NULL, &readf, NULL);
173 ret = fread(rrd_data, 1, RRD_DATA_MAX_LEN, readf);
175 pr_info("Read %d bytes :%s\n", ret, rrd_data);
178 harvest_zombies(pid);
185 long long rx_packets;
187 long long tx_packets;
190 #define PROC_NETDEV "/proc/net/dev"
192 static int get_iface_stats(const char *iface, struct iface_stats *stat)
200 netdev = fopen(PROC_NETDEV, "r");
202 if (netdev == NULL) {
204 pr_err("Failed to open file %s: %d (%m)\n",
209 while (fgets(buf, sizeof(buf), netdev)) {
210 get_word(buf, &str, if_name, sizeof(if_name));
212 /* Remove the ':' at the end of the if_name */
213 if_name[strlen(if_name) - 1] = 0;
214 if (strncmp(iface, if_name, sizeof(if_name)))
217 stat->rx_bytes = dec_to_longlong(str, &str);
218 stat->rx_packets = dec_to_longlong(str, &str);
219 /* errs */ dec_to_longlong(str, &str);
220 /* drop */ dec_to_longlong(str, &str);
221 /* fifo */ dec_to_longlong(str, &str);
222 /* frame */ dec_to_longlong(str, &str);
223 /* compressed */ dec_to_longlong(str, &str);
224 /* multicast */ dec_to_longlong(str, &str);
225 stat->tx_bytes = dec_to_longlong(str, &str);
226 stat->tx_packets = dec_to_longlong(str, &str);
228 pr_info("rx_b %lld rx_p %lld tx_b %lld tx_p %lld\n",
229 stat->rx_bytes, stat->rx_packets,
230 stat->tx_bytes, stat->tx_packets);
235 pr_err("Interface %s not found\n", iface);
239 int netstats_parser(char *rrd_data, const char **parser_data)
241 struct iface_stats stat;
242 const char **iface_name = parser_data;
243 int max_str = RRD_DATA_MAX_LEN;
247 pr_err("No device names specified\n");
252 pr_info("getting data for iface %s\n", *iface_name);
253 ret = get_iface_stats(*iface_name, &stat);
256 ret = snprintf(rrd_data, max_str, "%lld:%lld:%lld:%lld",
257 stat.rx_bytes, stat.rx_packets,
258 stat.tx_bytes, stat.tx_packets);
260 ret = snprintf(rrd_data, max_str, "U:U:U:U");
270 ret = snprintf(rrd_data, max_str, ":");