-
Notifications
You must be signed in to change notification settings - Fork 0
/
sys_linux.c
446 lines (406 loc) · 11.5 KB
/
sys_linux.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*
* Copyright (c) 2012, Internet Initiative Japan, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/statvfs.h>
#include <netdb.h>
#include <unistd.h>
#include <syslog.h>
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "libarms.h"
#include "armsd.h"
struct cpustat {
time_t t;
uint64_t idle;
uint64_t interrupt;
uint64_t user;
uint64_t system;
uint64_t other;
uint64_t total;
};
struct trafficstat {
time_t t;
unsigned long long in_octet;
unsigned long long out_octet;
unsigned long long in_packet;
unsigned long long out_packet;
unsigned long long in_error;
unsigned long long out_error;
};
static struct cpustat cpu_prev = { 0 };
static struct trafficstat traffic_prev[3] = { {0}, {0}, {0} };
static time_t sys_heartbeat_gettime(void);
static void sys_heartbeat_cpu(arms_context_t *);
static void sys_heartbeat_disk(arms_context_t *);
static void sys_heartbeat_mem(arms_context_t *);
static void sys_heartbeat_traffic(arms_context_t *);
int
sys_ping(const arms_ping_arg_t *arg, struct arms_ping_report *rep)
{
FILE *fp;
struct addrinfo hints, *res, *res0;
int af, n, pkts, success, timeout;
const char *argv[4 + 3]; /* 3 for safety... */
char buf[300], *tmpfile;
memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_flags = AI_NUMERICHOST;
n = getaddrinfo(arg->dst, NULL, &hints, &res0);
if (n) {
logit(LOG_WARNING, "cannot resolve %s: %s",
arg->dst, gai_strerror(n));
return ARMS_ESYSTEM;
}
af = AF_UNSPEC;
for (res = res0; res; res = res->ai_next)
if (res->ai_family == AF_INET || res->ai_family == AF_INET6) {
af = res->ai_family;
break;
}
freeaddrinfo(res0);
if (af == AF_UNSPEC) {
logit(LOG_WARNING, "no IPv4/IPv6 address: %s", arg->dst);
return ARMS_ESYSTEM;
}
fclose(armsdir_create_tmpfile(&tmpfile));
snprintf(buf, sizeof(buf), "%s -nq -c %d -s %d %s > %s",
(af == AF_INET ? "ping -b" : "ping6"),
arg->count, arg->size, arg->dst, tmpfile);
logit(LOG_DEBUG, "ping command exec: %s", buf);
argv[0] = "/bin/sh";
argv[1] = "-c";
argv[2] = buf;
argv[3] = NULL;
timeout = dconf_get_int("timeout");
n = script_exec(argv, timeout);
if (n == -1) {
logit(LOG_WARNING, "ping command failed");
unlink(tmpfile);
return ARMS_ESYSTEM;
}
fp = fopen(tmpfile, "r");
if (fp == NULL) {
logit(LOG_ERR, "ping command succeeded but cannot open %s: %s",
tmpfile, strerror(errno));
unlink(tmpfile);
return ARMS_ESYSTEM;
}
n = 0;
while (fgets(buf, sizeof(buf), fp) != NULL) {
n = sscanf(buf, "%d packets transmitted, %d received",
&pkts, &success);
if (n == 2) {
rep->success = success;
rep->failure = pkts - success;
break;
}
}
fclose(fp);
unlink(tmpfile);
if (n == 2)
return 0;
else
return ARMS_ESYSTEM;
}
int
sys_traceroute(const arms_traceroute_arg_t *arg,
struct arms_traceroute_info *tr, int trcount)
{
FILE *fp;
struct addrinfo hints, *res, *res0;
int af, hop, n, timeout;
const char *argv[4 + 3]; /* 3 for safety... */
char buf[300], host[64], *tmpfile;
memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_flags = AI_NUMERICHOST;
n = getaddrinfo(arg->addr, NULL, &hints, &res0);
if (n) {
logit(LOG_WARNING, "cannot resolve %s: %s",
arg->addr, gai_strerror(n));
return ARMS_ESYSTEM;
}
af = AF_UNSPEC;
for (res = res0; res; res = res->ai_next)
if (res->ai_family == AF_INET || res->ai_family == AF_INET6) {
af = res->ai_family;
break;
}
freeaddrinfo(res0);
if (af == AF_UNSPEC) {
logit(LOG_WARNING, "no IPv4/IPv6 address: %s", arg->addr);
return ARMS_ESYSTEM;
}
fclose(armsdir_create_tmpfile(&tmpfile));
snprintf(buf, sizeof(buf), "traceroute %s -n -m %d -q %d %s > %s",
(af == AF_INET ? "-4" : "-6"),
arg->maxhop, arg->count, arg->addr, tmpfile);
logit(LOG_DEBUG, "traceroute command exec: %s", buf);
argv[0] = "/bin/sh";
argv[1] = "-c";
argv[2] = buf;
argv[3] = NULL;
timeout = dconf_get_int("timeout");
n = script_exec(argv, timeout);
if (n == -1) {
logit(LOG_WARNING, "traceroute command failed");
unlink(tmpfile);
return ARMS_ESYSTEM;
}
fp = fopen(tmpfile, "r");
if (fp == NULL) {
logit(LOG_ERR, "traceroute command succeeded but cannot open "
"%s: %s",
tmpfile, strerror(errno));
unlink(tmpfile);
return ARMS_ESYSTEM;
}
n = 0;
while (fgets(buf, sizeof(buf), fp) != NULL) {
n = sscanf(buf, " %d %63s", &hop, (char *)&host);
if (n == 2) {
if (trcount <= 0)
break;
tr->hop = hop;
snprintf(tr->addr, sizeof(tr->addr), "%s", host);
tr++;
trcount--;
}
}
fclose(fp);
unlink(tmpfile);
if (n == 2)
return 0;
else
return ARMS_ESYSTEM;
}
static time_t
sys_heartbeat_gettime(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec;
}
static void
sys_heartbeat_cpu(arms_context_t *ctx)
{
FILE *fp;
struct cpustat cur;
unsigned long long x[8], total, difftotal;
int i, n;
char cpu[5], linebuf[80];
uint8_t v_idle, v_interrupt, v_user, v_system, v_other;
fp = fopen("/proc/stat", "r");
if (fp == NULL) {
logit(LOG_WARNING, "cannot open /proc/stat: %s",
strerror(errno));
return;
}
for (i = 0; i < 8; i++)
x[i] = 0;
total = 0;
while (fgets(linebuf, sizeof(linebuf), fp) != NULL) {
n = sscanf(linebuf,
"%4s %llu %llu %llu %llu %llu %llu %llu %llu",
(char *)&cpu, &x[0], &x[1], &x[2], &x[3], &x[4],
&x[5], &x[6], &x[7]);
if (n >= 5 && strcmp(cpu, "cpu") == 0) {
for (i = 0; i < 8; i++)
total += x[i];
break;
}
}
fclose(fp);
if (total == 0)
return;
cur.t = sys_heartbeat_gettime();
cur.idle = x[3];
cur.interrupt = (x[5] + x[6]);
cur.user = (x[0] + x[1]);
cur.system = x[2];
cur.other = (x[4] + x[7]);
cur.total = total;
if (cpu_prev.t == 0) {
cpu_prev = cur;
return;
}
/* TODO: 4 sya 5 nyuu */
difftotal = cur.total - cpu_prev.total;
v_idle = (cur.idle - cpu_prev.idle) * 100 / difftotal;
v_interrupt = (cur.interrupt - cpu_prev.interrupt) * 100 / difftotal;
v_user = (cur.user - cpu_prev.user) * 100 / difftotal;
v_system = (cur.system - cpu_prev.system) * 100 / difftotal;
v_other = (cur.other - cpu_prev.other) * 100 / difftotal;
arms_hb_set_cpu_detail_usage(ctx, 0, v_idle, v_interrupt, v_user,
v_system, v_other);
if (opt_debug)
logit(LOG_INFO, "heartbeat cpu_detail_usage: idx=%u, idle=%u, "
"interrupt=%u, user=%u, system=%u, other=%u",
0, v_idle, v_interrupt, v_user, v_system, v_other);
arms_hb_set_cpu_usage(ctx, 0, 100 - v_idle);
cpu_prev = cur;
}
static void
sys_heartbeat_disk(arms_context_t *ctx)
{
struct statvfs fs;
uint64_t v_used, v_free;
if (statvfs(dconf_get_string("hb-disk-usage"), &fs) == -1) {
logit(LOG_WARNING, "statvfs failed: %s", strerror(errno));
return;
}
v_used = (uint64_t)fs.f_frsize * (fs.f_blocks - fs.f_bfree);
v_free = (uint64_t)fs.f_frsize * fs.f_bavail;
arms_hb_set_disk_usage(ctx, 0, v_used, v_free);
if (opt_debug)
logit(LOG_INFO, "heartbeat disk_usage: "
"idx=%u, used=%llu, free=%llu",
0,
(unsigned long long)v_used,
(unsigned long long)v_free);
}
static void
sys_heartbeat_mem(arms_context_t *ctx)
{
FILE *fp;
unsigned long long v_free, v_used;
unsigned long long b, c, f, t, v;
int n;
char name[17], linebuf[80];
fp = fopen("/proc/meminfo", "r");
if (fp == NULL) {
logit(LOG_WARNING, "cannot open /proc/meminfo: %s",
strerror(errno));
return;
}
f = t = b = c = 0;
while (fgets(linebuf, sizeof(linebuf), fp) != NULL) {
n = sscanf(linebuf, "%16s %llu kB", (char *)&name, &v);
if (n < 2)
continue;
if (strcmp(name, "MemTotal:") == 0)
t = v * 1024;
else if (strcmp(name, "MemFree:") == 0)
f = v * 1024;
else if (strcmp(name, "Buffers:") == 0)
b = v * 1024;
else if (strcmp(name, "Cached:") == 0)
c = v * 1024;
}
fclose(fp);
if (t < f + b + c)
return; /* should not happen */
v_free = f + b + c;
v_used = t - v_free;
arms_hb_set_mem_usage(ctx, 0, v_used, v_free);
if (opt_debug)
logit(LOG_INFO, "heartbeat mem_usage: "
"idx=%u, used=%llu, free=%llu",
0, v_used, v_free);
}
static void
sys_heartbeat_traffic(arms_context_t *ctx)
{
FILE *fp;
struct trafficstat cur, prev;
unsigned long long dur, v_in_octet, v_out_octet, v_in_packet,
v_out_packet, v_in_error, v_out_error;
int ifidx, n;
char ifname[8], linebuf[1024], *p;
fp = fopen("/proc/net/dev", "r");
if (fp == NULL) {
logit(LOG_WARNING, "cannot open /proc/net/dev: %s",
strerror(errno));
return;
}
while (fgets(linebuf, sizeof(linebuf), fp) != NULL) {
if ((p = strchr(linebuf, ':')) == NULL)
continue;
*p = ' ';
n = sscanf(linebuf,
"%7s "
"%llu %llu %llu %*u %*u %*u %*u %*u"
"%llu %llu %llu %*u %*u %*u %*u %*u",
ifname,
&cur.in_octet,
&cur.in_packet,
&cur.in_error,
&cur.out_octet,
&cur.out_packet,
&cur.out_error);
if (n < 7)
continue;
if (strcmp(ifname, "eth0") == 0)
ifidx = 0;
else if (strcmp(ifname, "eth1") == 0)
ifidx = 1;
else if (strcmp(ifname, "eth2") == 0)
ifidx = 2;
else
continue;
cur.t = sys_heartbeat_gettime();
if (traffic_prev[ifidx].t == 0) {
traffic_prev[ifidx] = cur;
continue;
}
prev = traffic_prev[ifidx];
if (cur.t <= prev.t)
continue; /* should not happen */
dur = cur.t - prev.t;
v_in_octet = (cur.in_octet - prev.in_octet) / dur;
v_out_octet = (cur.out_octet - prev.out_octet) / dur;
v_in_packet = (cur.in_packet - prev.in_packet) / dur;
v_out_packet = (cur.out_packet - prev.out_packet) / dur;
v_in_error = (cur.in_error - prev.in_error) / dur;
v_out_error = (cur.out_error - prev.out_error) / dur;
arms_hb_set_traffic_rate(ctx, ifidx, v_in_octet, v_out_octet,
v_in_packet, v_out_packet, v_in_error, v_out_error);
if (opt_debug)
logit(LOG_INFO, "heartbeat traffic_rate: ifidx=%u, "
"in_octet=%llu, out_octet=%llu, in_packet=%llu, "
"out_packet=%llu, in_error=%llu, out_error=%llu",
ifidx, v_in_octet, v_out_octet, v_in_packet,
v_out_packet, v_in_error, v_out_error);
traffic_prev[ifidx] = cur;
}
fclose(fp);
}
void
sys_heartbeat_prepare(arms_context_t *ctx)
{
sys_heartbeat_cpu(ctx);
sys_heartbeat_mem(ctx);
sys_heartbeat_disk(ctx);
sys_heartbeat_traffic(ctx);
}