1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4 #include "dat.h"
5 
6 void xapm(int);
7 void xloadavg(int);
8 void xmeminfo(int);
9 void xnet(int);
10 void xstat(int);
11 void xvmstat(int);
12 void xwireless(int);
13 
14 void (*statfn[])(int) =
15 {
16 	xapm,
17 	xloadavg,
18 	xmeminfo,
19 	xnet,
20 	xstat,
21 	xvmstat,
22 	xwireless,
23 	0
24 };
25 
26 void
xapm(int first)27 xapm(int first)
28 {
29 	static int fd = -1;
30 	int curr = -1;
31 
32 	if(first){
33 		fd = open("/sys/class/power_supply/BAT0/capacity", OREAD);
34 		return;
35 	}
36 	if(fd == -1)
37 		return;
38 
39 	readfile(fd);
40 	tokens(0);
41 	curr = atoi(tok[0]);
42 
43 	if(curr != -1)
44 		Bprint(&bout, "battery =%d 100\n", curr);
45 
46 }
47 
48 void
xloadavg(int first)49 xloadavg(int first)
50 {
51 	static int fd = -1;
52 
53 	if(first){
54 		fd = open("/proc/loadavg", OREAD);
55 		return;
56 	}
57 
58 	readfile(fd);
59 	tokens(0);
60 	if(ntok >= 1)
61 		Bprint(&bout, "load =%d 1000\n", (int)(atof(tok[0])*1000));
62 }
63 
64 void
xmeminfo(int first)65 xmeminfo(int first)
66 {
67 	int i;
68 	vlong tot, used;
69 	vlong mtot, mfree;
70 	vlong stot, sfree;
71 	static int fd = -1;
72 
73 	if(first){
74 		fd = open("/proc/meminfo", OREAD);
75 		return;
76 	}
77 
78 	readfile(fd);
79 	mtot = 0;
80 	stot = 0;
81 	mfree = 0;
82 	sfree = 0;
83 	for(i=0; i<nline; i++){
84 		tokens(i);
85 		if(ntok < 3)
86 			continue;
87 		tot = atoll(tok[1]);
88 		used = atoll(tok[2]);
89 		if(strcmp(tok[0], "Mem:") == 0)
90 			Bprint(&bout, "mem =%lld %lld\n", used/1024, tot/1024);
91 		else if(strcmp(tok[0], "Swap:") == 0)
92 			Bprint(&bout, "swap =%lld %lld\n", used/1024, tot/1024);
93 		else if(strcmp(tok[0], "MemTotal:") == 0)
94 			mtot = atoll(tok[1]);	/* kb */
95 		else if(strcmp(tok[0], "MemFree:") == 0)
96 			mfree += atoll(tok[1]);
97 		else if(strcmp(tok[0], "Buffers:") == 0)
98 			mfree += atoll(tok[1]);
99 		else if(strcmp(tok[0], "Cached:") == 0){
100 			mfree += atoll(tok[1]);
101 			if(mtot < mfree)
102 				continue;
103 			Bprint(&bout, "mem =%lld %lld\n", mtot-mfree, mtot);
104 		}else if(strcmp(tok[0], "SwapTotal:") == 0)
105 			stot = atoll(tok[1]);	/* kb */
106 		else if(strcmp(tok[0], "SwapFree:") == 0){
107 			sfree = atoll(tok[1]);
108 			if(stot < sfree)
109 				continue;
110 			Bprint(&bout, "swap =%lld %lld\n", stot-sfree, stot);
111 		}
112 	}
113 }
114 
115 void
xnet(int first)116 xnet(int first)
117 {
118 	int i, n;
119 	vlong totb, totp, tote, totin, totou, totinb, totoub, b, p, e, in, ou, inb, oub;
120 	char *q;
121 	static int fd = -1;
122 
123 	if(first){
124 		fd = open("/proc/net/dev", OREAD);
125 		return;
126 	}
127 
128 	readfile(fd);
129 	n = 0;
130 	totb = 0;
131 	tote = 0;
132 	totp = 0;
133 	totin = 0;
134 	totou = 0;
135 	totinb = 0;
136 	totoub = 0;
137 	for(i=0; i<nline; i++){
138 		if((q = strchr(line[i], ':')) != nil)
139 			*q = ' ';
140 		tokens(i);
141 		if(ntok < 8+8)
142 			continue;
143 		if(strncmp(tok[0], "eth", 3) != 0 && strncmp(tok[0], "wlan", 4) != 0)
144 			continue;
145 		inb = atoll(tok[1]);
146 		oub = atoll(tok[9]);
147 		in = atoll(tok[2]);
148 		ou = atoll(tok[10]);
149 		b = inb+oub;
150 		p = in+ou;
151 		e = atoll(tok[3])+atoll(tok[11]);
152 		totb += b;
153 		totp += p;
154 		tote += e;
155 		totin += in;
156 		totou += ou;
157 		totinb += inb;
158 		totoub += oub;
159 		n++;
160 	}
161 	Bprint(&bout, "etherb %lld %d\n", totb, n*1000000);
162 	Bprint(&bout, "ether %lld %d\n", totp, n*1000);
163 	Bprint(&bout, "ethererr %lld %d\n", tote, n*1000);
164 	Bprint(&bout, "etherin %lld %d\n", totin, n*1000);
165 	Bprint(&bout, "etherout %lld %d\n", totou, n*1000);
166 	Bprint(&bout, "etherinb %lld %d\n", totinb, n*1000);
167 	Bprint(&bout, "etheroutb %lld %d\n", totoub, n*1000);
168 }
169 
170 void
xstat(int first)171 xstat(int first)
172 {
173 	static int fd = -1;
174 	int i;
175 
176 	if(first){
177 		fd = open("/proc/stat", OREAD);
178 		return;
179 	}
180 
181 	readfile(fd);
182 	for(i=0; i<nline; i++){
183 		tokens(i);
184 		if(ntok < 2)
185 			continue;
186 		if(strcmp(tok[0], "cpu") == 0 && ntok >= 5){
187 			Bprint(&bout, "user %lld 100\n", atoll(tok[1]));
188 			Bprint(&bout, "sys %lld 100\n", atoll(tok[3]));
189 			Bprint(&bout, "cpu %lld 100\n", atoll(tok[1])+atoll(tok[3]));
190 			Bprint(&bout, "idle %lld 100\n", atoll(tok[4]));
191 		}
192 	/*
193 		if(strcmp(tok[0], "page") == 0 && ntok >= 3){
194 			Bprint(&bout, "pagein %lld 500\n", atoll(tok[1]));
195 			Bprint(&bout, "pageout %lld 500\n", atoll(tok[2]));
196 			Bprint(&bout, "page %lld 1000\n", atoll(tok[1])+atoll(tok[2]));
197 		}
198 		if(strcmp(tok[0], "swap") == 0 && ntok >= 3){
199 			Bprint(&bout, "swapin %lld 500\n", atoll(tok[1]));
200 			Bprint(&bout, "swapout %lld 500\n", atoll(tok[2]));
201 			Bprint(&bout, "swap %lld 1000\n", atoll(tok[1])+atoll(tok[2]));
202 		}
203 	*/
204 		if(strcmp(tok[0], "intr") == 0)
205 			Bprint(&bout, "intr %lld 1000\n", atoll(tok[1]));
206 		if(strcmp(tok[0], "ctxt") == 0)
207 			Bprint(&bout, "context %lld 10000\n", atoll(tok[1]));
208 		if(strcmp(tok[0], "processes") == 0)
209 			Bprint(&bout, "fork %lld 1000\n", atoll(tok[1]));
210 	}
211 }
212 
213 void
xvmstat(int first)214 xvmstat(int first)
215 {
216 	static int fd = -1;
217 	int i;
218 
219 	if(first){
220 		fd = open("/proc/vmstat", OREAD);
221 		return;
222 	}
223 
224 	readfile(fd);
225 	for(i=0; i<nline; i++){
226 		tokens(i);
227 		if(ntok < 2)
228 			continue;
229 		if(strcmp(tok[0], "pgfault") == 0)
230 			Bprint(&bout, "fault %lld 100000\n", atoll(tok[1]));
231 	}
232 }
233 
234 void
xwireless(int first)235 xwireless(int first)
236 {
237 	static int fd = -1;
238 	int i;
239 
240 	if(first){
241 		fd = open("/proc/net/wireless", OREAD);
242 		return;
243 	}
244 
245 	readfile(fd);
246 	for(i=0; i<nline; i++){
247 		tokens(i);
248 		if(ntok < 3)
249 			continue;
250 		if(strcmp(tok[0], "wlan0:") == 0)
251 			Bprint(&bout, "802.11 =%lld 100\n", atoll(tok[2]));
252 	}
253 }
254