1 | #include <stdlib.h> |
---|
2 | #include <stdio.h> |
---|
3 | #include <string.h> |
---|
4 | #include <unistd.h> |
---|
5 | #include <fcntl.h> |
---|
6 | |
---|
7 | #ifdef LINUX |
---|
8 | #include <sys/vfs.h> |
---|
9 | #else |
---|
10 | #ifdef xBSD |
---|
11 | #include <sys/param.h> |
---|
12 | #include <sys/mount.h> |
---|
13 | #else |
---|
14 | #include <sys/statfs.h> |
---|
15 | #endif |
---|
16 | |
---|
17 | #endif |
---|
18 | |
---|
19 | |
---|
20 | #include "../../shared/sockets.h" |
---|
21 | |
---|
22 | #include "main.h" |
---|
23 | #include "mode.h" |
---|
24 | #include "disk.h" |
---|
25 | |
---|
26 | FILE *mtab_fd; |
---|
27 | |
---|
28 | |
---|
29 | typedef struct mounts |
---|
30 | { |
---|
31 | char dev[256], type[64], mpoint[256]; |
---|
32 | long bsize, blocks, bfree, files, ffree; |
---|
33 | } mounts; |
---|
34 | |
---|
35 | |
---|
36 | |
---|
37 | static int get_fs(mounts fs[]) |
---|
38 | { |
---|
39 | struct statfs fsinfo; |
---|
40 | char line[256]; |
---|
41 | int x = 0, y; |
---|
42 | |
---|
43 | mtab_fd = fopen("/etc/mtab", "r"); |
---|
44 | |
---|
45 | // Get rid of old, unmounted filesystems... |
---|
46 | memset(fs, 0, sizeof(mounts)*256); |
---|
47 | |
---|
48 | while (x < 256) |
---|
49 | { |
---|
50 | if(fgets(line, 256, mtab_fd) == NULL) |
---|
51 | { |
---|
52 | fclose(mtab_fd); |
---|
53 | return x; |
---|
54 | } |
---|
55 | |
---|
56 | sscanf(line, "%s %s %s", fs[x].dev, fs[x].mpoint, fs[x].type); |
---|
57 | |
---|
58 | if(strcmp(fs[x].type, "proc") |
---|
59 | #ifndef STAT_NFS |
---|
60 | && strcmp(fs[x].type, "nfs") |
---|
61 | #endif |
---|
62 | #ifndef STAT_SMBFS |
---|
63 | && strcmp(fs[x].type, "smbfs") |
---|
64 | #endif |
---|
65 | ) |
---|
66 | { |
---|
67 | #if LINUX || BSD |
---|
68 | y = statfs(fs[x].mpoint, &fsinfo); |
---|
69 | #else |
---|
70 | y = statfs(fs[x].mpoint, &fsinfo, sizeof(fsinfo), 0); |
---|
71 | #endif |
---|
72 | fs[x].blocks = fsinfo.f_blocks; |
---|
73 | if(fs[x].blocks > 0) |
---|
74 | { |
---|
75 | fs[x].bsize = fsinfo.f_bsize; |
---|
76 | fs[x].bfree = fsinfo.f_bfree; |
---|
77 | fs[x].files = fsinfo.f_files; |
---|
78 | fs[x].ffree = fsinfo.f_ffree; |
---|
79 | x++; |
---|
80 | } |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | fclose(mtab_fd); |
---|
85 | return x; |
---|
86 | } |
---|
87 | |
---|
88 | #if 0 |
---|
89 | static int get_fs(mounts fs[]) |
---|
90 | { |
---|
91 | struct statfs fsinfo; |
---|
92 | char line[256]; |
---|
93 | int x = 0, y; |
---|
94 | |
---|
95 | mtab_fd = fopen("/etc/mtab", "r"); |
---|
96 | |
---|
97 | // Get rid of old, unmounted filesystems... |
---|
98 | memset(fs, 0, sizeof(mounts)*256); |
---|
99 | |
---|
100 | while (x < 256) |
---|
101 | { |
---|
102 | if(fgets(line, 256, mtab_fd) == NULL) |
---|
103 | { |
---|
104 | fclose(mtab_fd); |
---|
105 | return x; |
---|
106 | } |
---|
107 | |
---|
108 | sscanf(line, "%s %s %s", fs[x].dev, fs[x].mpoint, fs[x].type); |
---|
109 | |
---|
110 | if( strcmp(fs[x].type, "proc") |
---|
111 | #ifndef STAT_NFS |
---|
112 | && strcmp(fs[x].type, "nfs") |
---|
113 | #endif |
---|
114 | #ifndef STAT_SMBFS |
---|
115 | && strcmp(fs[x].type, "smbfs") |
---|
116 | #endif |
---|
117 | ) |
---|
118 | { |
---|
119 | #ifdef LINUX |
---|
120 | y = statfs(fs[x].mpoint, &fsinfo); |
---|
121 | #else |
---|
122 | y = statfs(fs[x].mpoint, &fsinfo, sizeof(fsinfo), 0); |
---|
123 | #endif |
---|
124 | fs[x].bsize = fsinfo.f_bsize; |
---|
125 | fs[x].blocks = fsinfo.f_blocks; |
---|
126 | fs[x].bfree = fsinfo.f_bfree; |
---|
127 | fs[x].files = fsinfo.f_files; |
---|
128 | fs[x].ffree = fsinfo.f_ffree; |
---|
129 | x++; |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | fclose(mtab_fd); |
---|
134 | return x; |
---|
135 | } |
---|
136 | #endif |
---|
137 | |
---|
138 | |
---|
139 | int disk_init() |
---|
140 | { |
---|
141 | |
---|
142 | return 0; |
---|
143 | } |
---|
144 | |
---|
145 | int disk_close() |
---|
146 | { |
---|
147 | |
---|
148 | return 0; |
---|
149 | } |
---|
150 | |
---|
151 | |
---|
152 | /////////////////////////////////////////////////////////////////////////// |
---|
153 | // Gives disk stats. |
---|
154 | // |
---|
155 | // Stays onscreen until it is done. |
---|
156 | // |
---|
157 | |
---|
158 | // TODO: Disk screen! Requires virtual pages in the server, though... |
---|
159 | |
---|
160 | int disk_screen(int rep, int display) |
---|
161 | { |
---|
162 | static mounts mnt[256]; |
---|
163 | static int count=0; |
---|
164 | |
---|
165 | // Holds info to display (avoid recalculating it) |
---|
166 | struct disp |
---|
167 | { |
---|
168 | char dev[8]; |
---|
169 | char cap[8]; |
---|
170 | int full; |
---|
171 | } table[256]; |
---|
172 | int i; |
---|
173 | static int num_disks=0; |
---|
174 | static int first=1; // First line to display, sort of. |
---|
175 | |
---|
176 | #define huge long long int |
---|
177 | huge size; |
---|
178 | |
---|
179 | |
---|
180 | if(first) |
---|
181 | { |
---|
182 | first = 0; |
---|
183 | |
---|
184 | sock_send_string(sock, "screen_add D\n"); |
---|
185 | sprintf(buffer, "screen_set D name {Disk Use: %s}\n", host); |
---|
186 | sock_send_string(sock, buffer); |
---|
187 | sock_send_string(sock, "widget_add D title title\n"); |
---|
188 | sprintf(buffer, "widget_set D title {DISKS: %s}\n", host); |
---|
189 | sock_send_string(sock, buffer); |
---|
190 | sock_send_string(sock, "widget_add D f frame\n"); |
---|
191 | //sock_send_string(sock, "widget_set D f 1 2 20 4 20 3 v 8\n"); |
---|
192 | sprintf(buffer, "widget_set D f 1 2 %i %i %i %i v 12\n", |
---|
193 | lcd_wid, lcd_hgt, lcd_wid, lcd_hgt-1); |
---|
194 | sock_send_string(sock, buffer); |
---|
195 | sock_send_string(sock, "widget_add D err1 string\n"); |
---|
196 | sock_send_string(sock, "widget_add D err2 string\n"); |
---|
197 | sock_send_string(sock, "widget_set D err1 5 2 { Reading }\n"); |
---|
198 | sock_send_string(sock, "widget_set D err2 5 3 {Filesystems}\n"); |
---|
199 | } |
---|
200 | |
---|
201 | |
---|
202 | |
---|
203 | // Grab disk stats on first display, and fill "table". |
---|
204 | // Get rid of old, unmounted filesystems... |
---|
205 | memset(table, 0, sizeof(struct disp)*256); |
---|
206 | |
---|
207 | count = get_fs(mnt); |
---|
208 | first = 0; |
---|
209 | |
---|
210 | // Fill the display structure... |
---|
211 | if(count) |
---|
212 | { |
---|
213 | sock_send_string(sock, "widget_set D err1 30 5 .\n"); |
---|
214 | sock_send_string(sock, "widget_set D err2 30 5 .\n"); |
---|
215 | for(i=0; i<count; i++) |
---|
216 | { |
---|
217 | if(strlen(mnt[i].mpoint) > 6) |
---|
218 | { |
---|
219 | sprintf(table[i].dev, "%c%s", ELLIPSIS, |
---|
220 | (mnt[i].mpoint)+(strlen(mnt[i].mpoint)-5)); |
---|
221 | } |
---|
222 | else |
---|
223 | { |
---|
224 | sprintf(table[i].dev, "%s", mnt[i].mpoint); |
---|
225 | } |
---|
226 | |
---|
227 | //table[i].full = (lcd.cellwid * 4) |
---|
228 | table[i].full = (huge)(lcd_cellwid * 4) |
---|
229 | * (huge)(mnt[i].blocks - mnt[i].bfree) |
---|
230 | / (huge)mnt[i].blocks; |
---|
231 | |
---|
232 | size = (huge)mnt[i].bsize * (huge)mnt[i].blocks; |
---|
233 | memset(table[i].cap, 0, 8); |
---|
234 | |
---|
235 | // Kilobytes |
---|
236 | if(size > 0 && size < (huge)1000*(huge)1000) |
---|
237 | sprintf(table[i].cap, "%3.1fk", |
---|
238 | (double)(size)/1024.0); |
---|
239 | // Megabytes |
---|
240 | else if(size >= (huge)1000*(huge)1000 |
---|
241 | && size < (huge)1000*(huge)1000*(huge)1000) |
---|
242 | sprintf(table[i].cap, "%3.1fM", |
---|
243 | (float)(size/(huge)1024)/1024.0); |
---|
244 | // Gigabytes |
---|
245 | else if(size >= (huge)1000*(huge)1000*(huge)1000 |
---|
246 | && size < (huge)1000*(huge)1000*(huge)1000*(huge)1000) |
---|
247 | sprintf(table[i].cap, "%3.1fG", |
---|
248 | (float)(size/((huge)1024*(huge)1024))/1024.0); |
---|
249 | // Terabytes |
---|
250 | else if(size >= (huge)1000*(huge)1000*(huge)1000*(huge)1000 |
---|
251 | && size < (huge)1000*(huge)1000*(huge)1000*(huge)1000*(huge)1000) |
---|
252 | sprintf(table[i].cap, "%3.1fT", |
---|
253 | (float)(size/((huge)1024*(huge)1024*(huge)1024))/1024.0); |
---|
254 | |
---|
255 | // PectaBytes -- Yeah! I want some! |
---|
256 | else if(size >= (huge)1000*(huge)1000*(huge)1000*(huge)1000*(huge)1000 |
---|
257 | && size < (huge)1000*(huge)1000*(huge)1000*(huge)1000*(huge)1000*(huge)1000) |
---|
258 | sprintf(table[i].cap, "%3.1fP", |
---|
259 | (float)(size/((huge)1024*(huge)1024*(huge)1024*(huge)1024))/1024.0); |
---|
260 | |
---|
261 | } |
---|
262 | } |
---|
263 | |
---|
264 | |
---|
265 | |
---|
266 | if (!count) |
---|
267 | { |
---|
268 | sock_send_string(sock, "widget_set D err1 1 2 {Error Retrieving}\n"); |
---|
269 | sock_send_string(sock, "widget_set D err2 1 3 {Filesystem Stats}\n"); |
---|
270 | return 0; |
---|
271 | } |
---|
272 | |
---|
273 | |
---|
274 | // Display stuff... (show for two seconds, then scroll once per |
---|
275 | // second, then hold at the end for two seconds) |
---|
276 | sprintf(buffer, "widget_set D f 1 2 %i %i %i %i v 12\n", |
---|
277 | lcd_wid, lcd_hgt, lcd_wid, count); |
---|
278 | sock_send_string(sock, buffer); |
---|
279 | //sprintf(tmp, "widget_set D f 1 2 20 4 20 %i v 8\n", count); |
---|
280 | //sock_send_string(sock, tmp); |
---|
281 | for(i=0; i<count; i++) |
---|
282 | { |
---|
283 | if(table[i].dev[0] == 0) continue; |
---|
284 | if(i >= num_disks) // Make sure we have enough lines... |
---|
285 | { |
---|
286 | sprintf(tmp, "widget_add D s%i string -in f\n", i); |
---|
287 | sock_send_string(sock, tmp); |
---|
288 | sprintf(tmp, "widget_add D h%i hbar -in f\n", i); |
---|
289 | sock_send_string(sock, tmp); |
---|
290 | |
---|
291 | } |
---|
292 | sprintf(tmp, "widget_set D s%i 1 %i {%-6s %6s E F}\n", |
---|
293 | i, i+1, table[i].dev, table[i].cap); |
---|
294 | sock_send_string(sock, tmp); |
---|
295 | sprintf(tmp, "widget_set D h%i 16 %i %i\n", |
---|
296 | i, i+1, table[i].full); |
---|
297 | sock_send_string(sock, tmp); |
---|
298 | } |
---|
299 | |
---|
300 | // Now remove extra widgets... |
---|
301 | for(; i < num_disks; i++) |
---|
302 | { |
---|
303 | sprintf(tmp, "widget_del D s%i\n", i); |
---|
304 | sock_send_string(sock, tmp); |
---|
305 | sprintf(tmp, "widget_del D h%i\n", i); |
---|
306 | sock_send_string(sock, tmp); |
---|
307 | } |
---|
308 | |
---|
309 | |
---|
310 | num_disks = count; |
---|
311 | |
---|
312 | #undef huge |
---|
313 | |
---|
314 | /* |
---|
315 | // ** FILESYSTEMS ***** |
---|
316 | // / 543.2M E----F |
---|
317 | // /dos/c 2.1G E----F |
---|
318 | // /stuff 4.3G E----F |
---|
319 | */ |
---|
320 | return 0; |
---|
321 | } |
---|
322 | |
---|