-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput.c
266 lines (231 loc) · 6.43 KB
/
output.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
/*
* Copyright (c) 2008-2021 Andreas Lundin <lunde@dreamhosted.se>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/time.h>
#include <errno.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <pcap.h>
#include "ifpstat.h"
extern pcap_t *pcap_p;
extern int FLG_BWDIVIDER, FLG_CNT, FLG_CSV, FLG_DROPS, FLG_PPSDIVIDER;
extern int FLG_TIME, FLG_TIMESTAMP;
extern int64_t FLG_BWMULTIPLIER;
extern int64_t cntBytes; /* counter for matched bytes */
extern int64_t cntPackets; /* counter for matched packets */
extern time_t periodStart; /* timestamp for start of measurement period */
static size_t str_rfc3339(char *, const size_t, const time_t *);
void
print_drops(stats_drops_t *drops)
{
char *fmt = "%10.0f";
char sep = '\t';
if (FLG_CSV) {
fmt = "%.0f";
sep = ',';
}
fprintf(stdout, fmt, drops->kernDropRate);
#ifndef __OpenBSD__
fputc(sep, stdout);
fprintf(stdout, fmt, drops->ifDropRate);
#endif
return;
}
void
print_header(const char *bwPrefix, const char bwUnit, const char *packetPrefix)
{
char bwHeader[8];
char ppsHeader[8];
char *ts;
ts = "";
if (FLG_TIMESTAMP && FLG_CSV)
ts = "timestamp,";
else if (FLG_TIMESTAMP)
ts = "timestamp\t\t";
if (FLG_CSV) {
fprintf(stdout, "%s%s%cps,%spps", ts,
bwPrefix, bwUnit, packetPrefix);
if (FLG_DROPS) {
fprintf(stdout, ",drops");
#ifndef __OpenBSD__
fprintf(stdout, ",ifdrops");
#endif
}
fprintf(stdout, "\n");
return;
}
snprintf(bwHeader, sizeof(bwHeader), "%s%cps", bwPrefix,
bwUnit);
snprintf(ppsHeader, sizeof(ppsHeader), "%spps", packetPrefix);
fprintf(stdout, "%s%10s\t%10s", ts, bwHeader, ppsHeader);
if (FLG_DROPS) {
fprintf(stdout, "\t%10s", "drops");
#ifndef __OpenBSD__
fprintf(stdout, "\t%10s", "ifdrops");
#endif
}
fprintf(stdout, "\n");
return;
}
/* print_stats prints bandwidth and packet rate to stdout */
void
print_stats(int unused)
{
static stats_drops_t drops;
char tsbuf[32]; /* timestamp buffer */
static int64_t announceCount; /* counter for the announcements */
static int64_t oldBytes; /* bytes from last announcement */
static int64_t oldPackets; /* packets from last announcement */
int64_t deltaBytes;
int64_t deltaPackets;
int64_t deltaTime;
double byteRate, packetRate;
deltaBytes = cntBytes - oldBytes;
deltaPackets = cntPackets - oldPackets;
oldBytes = cntBytes;
oldPackets = cntPackets;
announceCount++;
deltaTime = FLG_TIME;
/* FLG_TIME is 0 when announced is triggered on a signal */
if (FLG_TIME == 0) {
time_t now;
now = time(NULL);
deltaTime = now - periodStart;
/* if announce is called more than once a second via signal
* this ensures that we don't divide by zero */
if (deltaTime <= 0)
deltaTime = 1;
periodStart = now;
}
if (FLG_TIMESTAMP) {
time_t now;
struct tm *tp;
now = time(NULL);
tp = localtime(&now);
if (tp == NULL) {
fprintf(stderr, "Error: Failed to get local time");
exit(EXIT_FAILURE);
}
if (FLG_CSV) {
/* use rfc3339 timestamp */
if (str_rfc3339(tsbuf, sizeof(tsbuf), &now) == 0) {
fprintf(stderr, "Error: Failed to create time stamp");
exit(EXIT_FAILURE);
}
} else {
/* yyyy-mm-dd hh:mm:ss */
if (strftime(tsbuf, sizeof(tsbuf), "%Y-%m-%d %T", tp) == 0) {
fprintf(stderr, "Error: Failed to create time stamp");
exit(EXIT_FAILURE);
}
}
}
byteRate = (deltaBytes / (double)FLG_BWDIVIDER) / deltaTime;
packetRate = (deltaPackets / (double)FLG_PPSDIVIDER) / deltaTime;
if (FLG_DROPS) {
net_update_drops(pcap_p, &drops, deltaTime);
}
if (FLG_CSV) {
if (FLG_TIMESTAMP) {
fprintf(stdout, "%s", tsbuf);
fputc(',', stdout);
}
if (FLG_BWDIVIDER > 1)
fprintf(stdout, "%.2f", byteRate * FLG_BWMULTIPLIER);
else
fprintf(stdout, "%.0f", byteRate * FLG_BWMULTIPLIER);
fputc(',', stdout);
if (FLG_PPSDIVIDER > 1)
fprintf(stdout, "%.2f", packetRate);
else
fprintf(stdout, "%.0f", packetRate);
if (FLG_DROPS) {
fputc(',', stdout);
print_drops(&drops);
}
} else {
if (FLG_TIMESTAMP) {
fprintf(stdout, "%s", tsbuf);
fputc('\t', stdout);
}
if (FLG_BWDIVIDER > 1)
fprintf(stdout, "%10.2f", byteRate * FLG_BWMULTIPLIER);
else
fprintf(stdout, "%10.0f", byteRate * FLG_BWMULTIPLIER);
fputc('\t', stdout);
if (FLG_PPSDIVIDER > 1)
fprintf(stdout, "%10.2f", packetRate);
else
fprintf(stdout, "%10.0f", packetRate);
if (FLG_DROPS) {
fputc('\t', stdout);
print_drops(&drops);
}
}
fprintf(stdout, "\n");
if (fflush(stdout) == EOF) {
fprintf(stderr, "Error: Couldn't write to stdout: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
if (announceCount == FLG_CNT)
exit(EXIT_SUCCESS);
}
/*
* str_rfc3339 writes an rfc3339 formatted timestamp from tp into buf.
* Returns 0 on error and bytes written into buffer on success.
*/
static size_t
str_rfc3339(char *buf, const size_t bufSize, const time_t *ts)
{
struct tm *tp;
char tsbuf[32];
char utcOffsetBuf[8];
long offset;
int hours, minutes;
size_t ret;
char c;
tp = localtime(ts);
if (tp == NULL) {
fprintf(stderr, "Error: Failed to get local time");
exit(EXIT_FAILURE);
}
offset = tp->tm_gmtoff;
if (offset == 0) {
/* rfc3339 - 2006-01-02T15:04:05Z */
return strftime(buf, bufSize, "%Y-%m-%dT%TZ", localtime(ts));
} else if (offset > 0) {
c = '+';
} else {
c = '-';
offset = offset * -1;
}
/* include timezone offset - rfc3339 - 2006-01-02T15:04:05+06:00 */
hours = offset / 3600;
minutes = (offset / 60) % 60;
ret = snprintf(utcOffsetBuf, sizeof(utcOffsetBuf),
"%c%.2d:%.2d", c, hours, minutes);
if (ret >= sizeof(utcOffsetBuf))
return 0;
ret = strftime(tsbuf, sizeof(tsbuf), "%Y-%m-%dT%T", tp);
if (ret == 0)
return ret;
ret = snprintf(buf, bufSize, "%s%s", tsbuf, utcOffsetBuf);
if (ret >= bufSize)
return 0;
return ret;
}