-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdisk.c
174 lines (155 loc) · 4.21 KB
/
disk.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "mrbig.h"
/* disk
green Tue Jul 06 02:43:47 VS 2004 [ntserver]
Filesystem 1K-blocks Used Avail Capacity Mounted
C 1249696 813056 436640 65% /FIXED/C ()
E 1249888 4458 1245430 0% /FIXED/E ()
*/
struct cfg {
char *name;
double yellow, red;
struct cfg *next;
} *pcfg;
static void read_diskcfg(/*char *p*/)
{
struct cfg *pc;
char b[100], name[100];
int n, i;
double y, r;
pcfg = NULL;
for (i = 0; get_cfg("disk", b, sizeof b, i); i++) {
if (b[0] == '#') continue;
n = sscanf(b, "%s %lf %lf", name, &y, &r);
if (n != 3) continue;
pc = big_malloc("read_diskcfg (node)", sizeof *pc);
pc->name = big_strdup("read_diskcfg (name)", name);
pc->yellow = y;
pc->red = r;
pc->next = pcfg;
pcfg = pc;
}
}
static void free_cfg(void)
{
struct cfg *dl;
while (pcfg) {
dl = pcfg;
pcfg = dl->next;
big_free("free_cfg (name)", dl->name);
big_free("free_cfg (node)", dl);
}
}
static void lookup_limits(char *drive, double *yellow, double *red)
{
struct cfg *pc;
for (pc = pcfg; pc; pc = pc->next) {
if (!strcasecmp(pc->name, drive)) {
*yellow = pc->yellow;
*red = pc->red;
return;
}
}
*yellow = dfyellow;
*red = dfred;
}
static void append_limits(char *a, size_t n)
{
struct cfg *pc;
#if 0 /* this upsets Hobbit's rrd handler module do_disk.c */
snprcat(a, n, "\n<b>Limits:</b>\n<table>\n");
snprcat(a, n, "<tr><th>Drive</th><th>Yellow</th><th>Red</th></tr>\n");
for (pc = pcfg; pc; pc = pc->next) {
snprcat(a, n, "<tr><td>%s</td><td>%.1f%%</td><td>%.1f%%</td></tr>\n",
pc->name, pc->yellow, pc->red);
}
snprcat(a, n, "<tr><td>Default</td><td>%.1f%%</td><td>%.1f%%</td></tr>\n",
dfyellow, dfred);
snprcat(a, n, "</table>\n");
#else /* fixed width plain text should be safe */
snprcat(a, n, "\nLimits:\n");
snprcat(a, n, "%-10s %-10s %-10s\n", "Drive", "Yellow", "Red");
for (pc = pcfg; pc; pc = pc->next) {
snprcat(a, n, "%-10s %-10.1f %-10.1f\n", pc->name, pc->yellow, pc->red);
}
snprcat(a, n, "%-10s %-10.1f %-10.1f\n", "Default", dfyellow, dfred);
#endif
}
void disk(void)
{
char b[5000];
int n = sizeof b;
int i, j, res;
double yellow, red;
char d[10], q[5000], r[5000], *color = "green";
char cfgfile[1024], drive[10];
DWORD drives;
unsigned int dtype;
double pct;
uint64_t total_bytes, free_bytes;
ULARGE_INTEGER fa, tb, fb;
if (debug > 1) mrlog("disk(%p, %d)", b, n);
if (get_option("no_disk", 0)) {
mrsend(mrmachine, "disk", "clear", "option no_disk\n");
return;
}
drives = GetLogicalDrives();
cfgfile[0] = '\0';
snprcat(cfgfile, sizeof cfgfile, "%s%c%s", cfgdir, dirsep, "disk.cfg");
read_cfg("disk", cfgfile);
read_diskcfg(/*cfgfile*/);
r[0] = '\0';
q[0] = '\0';
snprcat(q, sizeof q, "%-11s %11s %11s %11s %11s %s\n",
"Filesystem", "1k-blocks", "Used", "Avail", "Capacity",
"Mounted");
for (i = 'A'; i <= 'Z'; i++) {
if (drives & 1) {
drive[0] = d[0] = '\0';
snprcat(drive, sizeof drive, "%c", i);
snprcat(d, sizeof d, "%c:\\", i);
dtype = GetDriveType(d);
if (debug) mrlog("%s is type %d", d, dtype);
if (dtype == DRIVE_FIXED) {
res = GetDiskFreeSpaceEx(d, &fa, &tb, &fb);
if (debug) mrlog("GetDiskFreeSpaceEx returns %d", res);
if (res == 0) {
mrlog("No size info (%d)", GetLastError());
total_bytes = free_bytes = 0;
pct = 0;
} else {
total_bytes = tb.QuadPart;
free_bytes = fb.QuadPart;
pct = 100.0-100.0*free_bytes/total_bytes;
}
lookup_limits(drive, &yellow, &red);
if (pct >= red) {
snprcat(r, sizeof r,
"&red %s (%.1f%%) has reached the PANIC level (%.1f%%)\n",
d, pct, red);
color = "red";
} else if (!strcmp(color, "green")
&& pct >= yellow) {
snprcat(r, sizeof r,
"&yellow %s (%.1f%%) has reached the WARNING level (%.1f%%)\n",
d, pct, yellow);
color = "yellow";
}
j = 0;
/* Filesystem */
snprcat(q, sizeof q,
"%-11c % 11" PRIu64 " % 11" PRIu64 " % 11" PRIu64 " % 10.1f%% /FIXED/%c\n",
i,
(total_bytes / 1024),
((total_bytes - free_bytes) / 1024),
(free_bytes / 1024),
pct, i);
}
}
drives >>= 1;
}
b[0] = '\0';
snprcat(b, n, "%s\n\n%s\n%s\n", now, r, q);
append_limits(b, n);
free_cfg();
mrsend(mrmachine, "disk", color, b);
}