]> git.itanic.dy.fi Git - rrdd/blob - parser.c
Initial comit
[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
8 static int str_to_dec(char *src, char **dst)
9 {
10         int ret;
11
12         while(((*src < '0') || (*src > '9')) && *src)
13                 src++;
14
15         ret = atoi(src);
16
17         if (dst) {
18                 while(((*src >= '0') && (*src <= '9')) && *src)
19                         src++;
20                 *dst = src;
21         }
22
23         return ret;
24 }
25
26 static int get_word(char *src, char **dst, char *word)
27 {
28         int ret = 0;
29         while(((*src == ' ') || (*src == '\t')) && *src)
30                 src++;
31
32         while(((*src != ' ') && (*src != '\t')) && *src) {
33                 *word = *src;
34                 word++;
35                 src++;
36                 ret++;
37         }
38         *word = 0;
39
40         if (dst)
41                 *dst = src;
42
43         return ret;
44 }
45
46 #define STATFILE "/proc/stat"
47
48 int cpu_parser(char *data)
49 {
50         char buf[1024];
51         char *str = buf;
52         FILE *file = fopen(STATFILE, "r");
53         int user, nice, sys, idle, wait, irq, softirq;
54
55         if (file == NULL) {
56                 fprintf(stderr, "Failed to open file %s\n", STATFILE);
57                 return -1;
58         }
59
60         if (!fgets(buf, 1024, file)) {
61                 fprintf(stderr, "Failed to read file %s\n", STATFILE);
62                 fclose(file);
63                 return -1;
64         }
65
66         user    = str_to_dec(str, &str);
67         nice    = str_to_dec(str, &str);
68         sys     = str_to_dec(str, &str);
69         idle    = str_to_dec(str, &str);
70         wait    = str_to_dec(str, &str);
71         irq     = str_to_dec(str, &str);
72         softirq = str_to_dec(str, &str);
73
74         sprintf(data, "%d:%d:%d:%d:%d:%d:%d",
75                 user, nice, sys, idle, wait, irq, softirq);
76
77         fclose(file);
78         return 0;
79 }
80
81 #define MEMFILE "/proc/meminfo"
82
83 int mem_parser(char *data)
84 {
85         char buf[1024], word[1024];
86         int free = 0, buffered = 0, cache = 0, active = 0, inactive = 0,
87                 swapfree = 0, anon = 0, slab = 0, tables = 0;
88         FILE *file = fopen(MEMFILE, "r");
89
90         if (file == NULL) {
91                 fprintf(stderr, "Failed to open file %s\n", MEMFILE);
92                 return -1;
93         }
94
95         while (fgets(buf, 1024, file)) {
96                 get_word(buf, 0, word);
97                 
98                 if (!strcmp(word, "MemFree:")) {
99                         free = str_to_dec(buf, 0);
100                 } else if (!strcmp(word, "Buffers:")) {
101                         buffered = str_to_dec(buf, 0);
102                 } else if (!strcmp(word, "Cached:")) {
103                         cache = str_to_dec(buf, 0);
104                 } else if (!strcmp(word, "Active:")) {
105                         active = str_to_dec(buf, 0);
106                 } else if (!strcmp(word, "Inactive:")) {
107                         inactive = str_to_dec(buf, 0);
108                 } else if (!strcmp(word, "SwapFree:")) {
109                         swapfree = str_to_dec(buf, 0);
110                 } else if (!strcmp(word, "AnonPages:")) {
111                                 anon = str_to_dec(buf, 0);
112                 } else if (!strcmp(word, "Slab:")) {
113                         slab = str_to_dec(buf, 0);
114                 } else if (!strcmp(word, "PageTables:")) {
115                         tables = str_to_dec(buf, 0);
116                 }
117         }
118         fclose(file);
119
120         sprintf(data, "%f:%f:%f:%f:%f:%f:%f:%f:%f",
121                 free / 1024.0,
122                 buffered / 1024.0,
123                 cache / 1024.0,
124                 active / 1024.0,
125                 inactive / 1024.0,
126                 swapfree / 1024.0,
127                 anon / 1024.0,
128                 slab / 1024.0,
129                 tables / 1024.0);
130
131         return 0;
132 }
133
134 int cpu_mem_parser(char *data)
135 {
136         char cpu[1024], mem[1024];
137
138         cpu_parser(cpu);
139         mem_parser(mem);
140         sprintf(data, "%s:%s", cpu, mem);
141
142         return 0;
143 }