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
|
/*
* Copyright (c) 2014 Red Hat.
* Copyright (c) 1999,2004 Silicon Graphics, Inc. All Rights Reserved.
* This code contributed by Michal Kara (lemming@arthur.plbohnice.cz)
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#include <ctype.h>
#include "pmapi.h"
#include "pmda.h"
#include "indom.h"
#include "proc_net_tcp.h"
#define MYBUFSZ (1<<14) /*16k*/
int
refresh_proc_net_tcp(proc_net_tcp_t *proc_net_tcp)
{
FILE *fp;
char buf[MYBUFSZ];
char *p = buf;
char *q;
unsigned int n;
ssize_t got = 0;
ptrdiff_t remnant = 0;
memset(proc_net_tcp, 0, sizeof(*proc_net_tcp));
if ((fp = linux_statsfile("/proc/net/tcp", buf, sizeof(buf))) == NULL)
return -oserror();
/* skip header */
if (fgets(buf, sizeof(buf), fp) == NULL) {
/* oops, no header! */
fclose(fp);
return -oserror();
}
for (buf[0]='\0';;) {
q = strchrnul(p, '\n');
if (*q == '\n') {
if (1 == sscanf(p, " %*s %*s %*s %x", &n)
&& n < _PM_TCP_LAST) {
proc_net_tcp->stat[n]++;
}
p = q + 1;
continue;
}
remnant = (q - p);
if (remnant > 0 && p != buf)
memmove(buf, p, remnant);
got = read(fileno(fp), buf + remnant, MYBUFSZ - remnant - 1);
if (got <= 0)
break;
buf[remnant + got] = '\0';
p = buf;
}
fclose(fp);
return 0;
}
|