-
Notifications
You must be signed in to change notification settings - Fork 1
/
rrdupd.c
350 lines (318 loc) · 8.53 KB
/
rrdupd.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/* Copyright (c) 2014, 2016 Alexander Graf and André Koch-Kramer.
* All rights reserved. */
#include <assert.h>
#include <linux/major.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <rrd.h>
#define HEARTBEAT 120
/* uncomment this if you want to measure temperature. */
//#define HAVE_TEMP
#define TEMPSENSOR "/sys/devices/virtual/thermal/thermal_zone0/temp"
#define TEMPSAMPLES 8
#define prefmatch(str, prefix) !strncmp(str, prefix, strlen(prefix))
#ifdef HAVE_TEMP
static int temp_sum;
#endif
#if !defined(LVM_BLK_MAJOR)
#define LVM_BLK_MAJOR 58
#endif
#define DM_MAJOR_RAID 253
#define DM_MAJOR_CRYPT 254
typedef struct list {
char *devname;
char is_device;
struct list *next;
} *devicelist;
static devicelist devices = NULL;
static devicelist insert(devicelist list, char *device, char is_device) {
devicelist tmplist = (devicelist) malloc(sizeof(struct list));
if (list == NULL) {
tmplist->next = NULL;
} else {
tmplist->next = list;
}
int size = strlen(device) + 1;
tmplist->devname = (char*) malloc(size);
strncpy(tmplist->devname, device, size);
tmplist->is_device = is_device;
return tmplist;
}
static char is_disk(char *dev) {
char devpath[27] = "/sys/block/";
strncat(devpath, dev, 27);
devicelist tmplist = devices;
while (tmplist != NULL) {
if (!strcmp(tmplist->devname, dev))
break;
tmplist = tmplist->next;
}
if (tmplist != NULL)
return tmplist->is_device;
else {
char is_device = !access(devpath, F_OK);
devices = insert(devices, dev, is_device);
return is_device;
}
}
static void update()
{
FILE *proc;
unsigned long vm_MemTotal = 0, vm_MemFree = 0, vm_Buffers = 0,
vm_Cached = 0, vm_SwapCached = 0, vm_Shmem = 0,
vm_PageTables = 0, vm_KernelStack = 0, vm_Slab = 0,
vm_AnonPages = 0, vm_SwapTotal = 0, vm_SwapFree = 0;
int i, j;
char buf[256];
char *argv[4];
unsigned long long cp_times[7], cp_times_diff[7], cp_times_dsum;
static unsigned long long last_cp_times[7];
static int last_valid;
float cp_times_dn[7];
float loadavg5, loadavg15;
char devname[16];
unsigned long long ibytes, obytes;
unsigned long long ibytes_d, obytes_d;
static unsigned long long last_ibytes, last_obytes;
unsigned int major, minor;
unsigned long long reads, writes, reads_d, writes_d;
static unsigned long long last_reads, last_writes;
/* CPU usage */
proc = fopen("/proc/stat", "r");
i = fscanf(proc, "%*s %Lu %Lu %Lu %Lu %Lu %Lu %Lu",
&cp_times[0], &cp_times[1], &cp_times[2], &cp_times[3],
&cp_times[4], &cp_times[5], &cp_times[6]);
assert(i == 7);
fclose(proc);
if (last_valid) {
for (i = 0; i < 7; i++) {
if (last_cp_times[i] > cp_times[i])
goto cpuovfl;
cp_times_diff[i] = cp_times[i] - last_cp_times[i];
}
cp_times_dsum = 0;
for (i = 0; i < 7; i++) {
cp_times_dsum += cp_times_diff[i];
}
for (i = 0; i < 7; i++) {
cp_times_dn[i] = (float) cp_times_diff[i] / cp_times_dsum;
}
snprintf(buf, sizeof(buf), "N:%.6f:%.6f:%.6f:%.6f:%.6f:%.6f",
cp_times_dn[0], cp_times_dn[1], cp_times_dn[2],
cp_times_dn[4], cp_times_dn[5], cp_times_dn[6]);
argv[0] = "update";
argv[1] = "cpu.rrd";
argv[2] = buf;
argv[3] = 0;
rrd_update(3, argv);
}
cpuovfl:
memcpy(last_cp_times, cp_times, sizeof(cp_times));
/* VM stats */
proc = fopen("/proc/meminfo", "r");
i = 0;
while (fgets(buf, sizeof(buf), proc)) {
if (prefmatch(buf, "MemTotal:")) {
i++;
vm_MemTotal = atoi(buf + sizeof("MemTotal:"));
} else if (prefmatch(buf, "MemFree:")) {
i++;
vm_MemFree = atoi(buf + sizeof("MemFree:"));
} else if (prefmatch(buf, "Buffers:")) {
i++;
vm_Buffers = atoi(buf + sizeof("Buffers:"));
} else if (prefmatch(buf, "Cached:")) {
i++;
vm_Cached = atoi(buf + sizeof("Cached:"));
} else if (prefmatch(buf, "SwapCached:")) {
i++;
vm_SwapCached = atoi(buf + sizeof("SwapCached:"));
} else if (prefmatch(buf, "Shmem:")) {
i++;
vm_Shmem = atoi(buf + sizeof("Shmem:"));
} else if (prefmatch(buf, "PageTables:")) {
i++;
vm_PageTables = atoi(buf + sizeof("PageTables:"));
} else if (prefmatch(buf, "KernelStack:")) {
i++;
vm_KernelStack = atoi(buf + sizeof("KernelStack:"));
} else if (prefmatch(buf, "Slab:")) {
i++;
vm_Slab = atoi(buf + sizeof("Slab:"));
} else if (prefmatch(buf, "AnonPages:")) {
i++;
vm_AnonPages = atoi(buf + sizeof("AnonPages:"));
} else if (prefmatch(buf, "SwapTotal:")) {
i++;
vm_SwapTotal = atoi(buf + sizeof("SwapTotal:"));
} else if (prefmatch(buf, "SwapFree:")) {
i++;
vm_SwapFree = atoi(buf + sizeof("SwapFree:"));
}
if (i == 12)
break;
}
fclose(proc);
assert(i == 12);
// tautological assert:
/*assert((vm_MemTotal - (vm_MemFree + vm_Buffers + vm_Cached + vm_Shmem)) +
(vm_MemFree - vm_Shmem) + vm_Shmem + vm_Buffers +
(vm_Cached + vm_Shmem) == vm_MemTotal);
*/
snprintf(buf, sizeof(buf), "N:%lu:%lu:%lu:%lu:%lu:%lu:%lu",
1024 * vm_AnonPages,
1024 * (vm_PageTables + vm_KernelStack),
1024 * vm_Slab,
1024 * vm_Buffers,
1024 * (vm_Cached + vm_SwapCached - vm_Shmem),
1024 * vm_Shmem,
1024 * (vm_MemTotal - (vm_MemFree + vm_AnonPages +
vm_PageTables + vm_KernelStack + vm_Slab +
vm_Buffers + vm_Cached + vm_SwapCached)));
argv[0] = "update";
argv[1] = "mem.rrd";
argv[2] = buf;
argv[3] = 0;
rrd_update(3, argv);
snprintf(buf, sizeof(buf), "N:%li", vm_SwapTotal - vm_SwapFree);
argv[0] = "update";
argv[1] = "swap.rrd";
argv[2] = buf;
argv[3] = 0;
rrd_update(3, argv);
/* load average */
proc = fopen("/proc/loadavg", "r");
i = fscanf(proc, "%*f %f %f", &loadavg5, &loadavg15);
assert(i == 2);
fclose(proc);
snprintf(buf, sizeof(buf), "N:%.6f:%.6f", loadavg5, loadavg15);
argv[0] = "update";
argv[1] = "loadavg.rrd";
argv[2] = buf;
argv[3] = 0;
rrd_update(3, argv);
#ifdef HAVE_TEMP
/* cpu temperature */
snprintf(buf, sizeof(buf), "N:%.3f",
temp_sum / 1000.0 / (float) TEMPSAMPLES);
temp_sum = 0;
argv[0] = "update";
argv[1] = "temp.rrd";
argv[2] = buf;
argv[3] = 0;
rrd_update(3, argv);
#endif
/* network usage */
proc = fopen("/proc/net/dev", "r");
i = 0;
ibytes = obytes = 0;
while (fgets(buf, sizeof(buf), proc)) {
if (i++ < 2)
/* skip first two lines */
continue;
j = sscanf(buf, "%15s %Lu %*d %*d %*d %*d %*d %*d %*d %Lu",
devname, &ibytes_d, &obytes_d);
assert(j == 3);
if (!strcmp(devname, "lo:"))
/* skip loopback interface */
continue;
ibytes += ibytes_d;
obytes += obytes_d;
}
fclose(proc);
if (last_valid &&
ibytes >= last_ibytes &&
obytes >= last_obytes) {
snprintf(buf, sizeof(buf), "N:%Lu:%Lu",
(ibytes-last_ibytes)/HEARTBEAT,
(obytes-last_obytes)/HEARTBEAT);
argv[0] = "update";
argv[1] = "traffic.rrd";
argv[2] = buf;
argv[3] = 0;
rrd_update(3, argv);
}
last_ibytes = ibytes;
last_obytes = obytes;
/* disk I/O */
proc = fopen("/proc/diskstats", "r");
reads = writes = 0;
while (fgets(buf, sizeof(buf), proc)) {
j = sscanf(buf, "%u %u %s %*u %*u %Lu %*u %*u %*u %Lu", &major,
&minor, devname, &reads_d, &writes_d);
if (j == 5 && major != LVM_BLK_MAJOR && major != NBD_MAJOR
&& major != RAMDISK_MAJOR && major != LOOP_MAJOR
&& major != DM_MAJOR_CRYPT && major != DM_MAJOR_RAID) {
if (is_disk(devname)) {
reads += reads_d;
writes += writes_d;
}
}
}
fclose(proc);
if (last_valid &&
reads >= last_reads &&
writes >= last_writes) {
snprintf(buf, sizeof(buf), "N:%Lu:%Lu",
4096*(reads-last_reads)/HEARTBEAT,
4096*(writes-last_writes)/HEARTBEAT);
argv[0] = "update";
argv[1] = "disk.rrd";
argv[2] = buf;
argv[3] = 0;
rrd_update(3, argv);
}
last_reads = reads;
last_writes = writes;
last_valid = 1;
}
#ifdef HAVE_TEMP
static void measure_temp()
{
FILE *proc;
int i, temp;
proc = fopen(TEMPSENSOR, "r");
if (proc == NULL) {
fprintf(stderr, "Adjust TEMPSENSOR define in rrdupd.c!\n");
exit(1);
}
i = fscanf(proc, "%d", &temp);
assert(i == 1);
fclose(proc);
temp_sum += temp;
}
#endif
int main(int argc, char **argv)
{
struct timespec tp;
time_t nextrun;
#ifdef HAVE_TEMP
int cnt = 0;
#endif
/* go to directory where binary resides, as we expect rrd files there */
assert(argc == 1);
if (chdir(dirname(argv[0])) != 0)
perror("chdir");
clock_gettime(CLOCK_MONOTONIC, &tp);
nextrun = tp.tv_sec;
while (1) {
#ifdef HAVE_TEMP
nextrun += HEARTBEAT / TEMPSAMPLES;
measure_temp();
if (++cnt == TEMPSAMPLES) {
update();
cnt = 0;
}
#else
nextrun += HEARTBEAT;
update();
#endif
clock_gettime(CLOCK_MONOTONIC, &tp);
if (nextrun - tp.tv_sec > 0)
sleep(nextrun - tp.tv_sec);
}
}