Skip to content

Commit

Permalink
Alter output to json
Browse files Browse the repository at this point in the history
  • Loading branch information
wdoekes committed Apr 8, 2019
1 parent c761689 commit c091bff
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 35 deletions.
68 changes: 49 additions & 19 deletions out_rtpsniff.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ void out_write(uint32_t unixtime_begin, uint32_t interval, struct rtpstat_t *mem
unsigned packets = 0;
unsigned lost = 0;
unsigned late = 0;
unsigned gaps = 0;
unsigned jumps = 0;
unsigned printed = 0;

struct rtpstat_t *rtpstat, *tmp;

Expand All @@ -86,18 +89,28 @@ void out_write(uint32_t unixtime_begin, uint32_t interval, struct rtpstat_t *mem
}
}

/* {"info": ... */
for (fp = fps; *fp; ++fp) {
fprintf(*fp, "Storage output: unixtime_begin=%" SCNu32 ", "
"interval=%" SCNu32 ", memory=%p\n",
fprintf(
*fp,
"{\"info\": {\"mod\": \"out_rtpsniff\", "
"\"begin_time\": %" SCNu32 ", "
"\"interval\": %" SCNu32 ", \"memory\": \"%p\"}\n",
unixtime_begin, interval, memory);
}

/* Show per-stream summary */
/* "streams": ... */
for (fp = fps; *fp; ++fp)
fprintf(*fp, ",\"streams\": [");
HASH_ITER(hh, memory, rtpstat, tmp) {
float miss_percent;
streams += 1;
packets += rtpstat->packets;
lost += rtpstat->missed;
late += rtpstat->late;
gaps += rtpstat->gaps;
jumps += rtpstat->jumps;

/* Streams with significant amounts of packets */
if ((rtpstat->packets + rtpstat->missed) < 20)
Expand All @@ -121,37 +134,54 @@ void out_write(uint32_t unixtime_begin, uint32_t interval, struct rtpstat_t *mem
for (fp = fps; *fp; ++fp) {
fprintf(
*fp,
"RTP: %s:%hu > %s:%hu"
", ssrc: %" PRIu32
", packets: %" PRIu32
", seq: %" PRIu16
", lost: %" PRIu16
", lostpct: %.1f%%"
", gaps: %" PRIu16
", late-or-dupe: %" PRIu16
", jump: %" PRIu16
"\n",
"\n%c{\"from\": \"%s:%hu\", \"to\": \"%s:%hu\""
", \"ssrc\": %" PRIu32
", \"seq\": %" PRIu16
", \"not-lost\": %" PRIu32
", \"lost\": %" PRIu16
", \"lost-percent\": %.1f"
", \"late-or-dupe\": %" PRIu16
", \"gaps\": %" PRIu16
", \"jumps\": %" PRIu16
"}",
(printed == 0 ? ' ' : ','),
src_ip, rtpstat->src_port,
dst_ip, rtpstat->dst_port,
rtpstat->ssrc,
(rtpstat->packets + rtpstat->missed),
rtpstat->seq,
rtpstat->packets,
rtpstat->missed,
miss_percent,
rtpstat->gaps,
rtpstat->late,
rtpstat->gaps,
rtpstat->jumps);
}
++printed;
}
for (fp = fps; *fp; ++fp)
fprintf(*fp, "]\n");

/* Class C summary */
/* "class_c": ... */
/* FIXME */

/* End output */
for (fp = fps; *fp; ++fp) {
if (!packets) {
fprintf(*fp, "RTP-SUM: nothing\n");
fprintf(*fp, ",\"summary\": null\n}\n");
} else {
fprintf(*fp, "RTP-SUM: streams %u, not-lost %u, lost %u (%.2f%%), "
"late-or-dupe %u (%.2f%%)\n",
streams, packets, lost, 100.0 * lost / (lost + packets),
late, 100.0 * late / (lost + packets));
fprintf(
*fp,
",\"summary\": {"
"\"streams\": %u, "
"\"not-lost\": %u, "
"\"lost\": %u, "
"\"lost-percent\": %.2f, "
"\"late-or-dupe\": %u, "
"\"gaps\": %u, "
"\"jumps\": %u}\n}\n",
streams, packets, lost, 100.0 * lost / (lost + packets),
late, gaps, jumps);
}
fflush(*fp);
}
Expand Down
35 changes: 19 additions & 16 deletions sniff_rtp.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ struct sniff_rtp {

static struct memory_t *sniff__memory;
static pcap_t *sniff__handle;

static unsigned sniff__adjust_slowpoll_dropped;

static void sniff__adjust_slowpoll_wait();
static void sniff__switch_memory(int signum);
Expand Down Expand Up @@ -244,16 +244,14 @@ void sniff_loop(pcap_t *handle, struct memory_t *memory) {
fprintf(stderr, "sniff_loop: Ended loop at user/system request.\n");
#endif

/* Fetch grand total stats */
if (pcap_stats(handle, &stat) < 0) {
fprintf(stderr, "pcap_stats: %s\n", pcap_geterr(handle));
return;
}

// FIXME: move this to out_*
//fprintf(stderr, "%u packets captured\n", packets_captured);
// and how many minutes? produce a grand total?
fprintf(stderr, "%u packets received by filter\n", stat.ps_recv);
fprintf(stderr, "%u packets dropped by kernel\n", stat.ps_drop);
fprintf(stderr, "%u packets dropped by kernel\n",
stat.ps_drop - sniff__adjust_slowpoll_dropped);
fprintf(stderr, "%u packets dropped by interface\n", stat.ps_ifdrop);

/* Remove signal handlers */
Expand All @@ -268,9 +266,11 @@ static void sniff__switch_memory(int signum) {
int recently_active = sniff__memory->active;
sniff__memory->active = !recently_active;
#ifndef NDEBUG
fprintf(stderr, "sniff__switch_memory: Switched from memory %d (%p) to %d (%p).\n",
recently_active, sniff__memory->rtphash[recently_active],
!recently_active, sniff__memory->rtphash[!recently_active]);
fprintf(
stderr,
"sniff__switch_memory: Switched from memory %d (%p) to %d (%p).\n",
recently_active, sniff__memory->rtphash[recently_active],
!recently_active, sniff__memory->rtphash[!recently_active]);
#endif

/* Ajust wait time if needed */
Expand All @@ -291,18 +291,21 @@ void sniff_release(struct rtpstat_t **memory) {

void sniff__adjust_slowpoll_wait() {
struct pcap_stat stat;
static unsigned prev_drop;

if (pcap_stats(sniff__handle, &stat) >= 0) {
if (stat.ps_drop > prev_drop) {
unsigned dropped = stat.ps_drop - prev_drop;
if (stat.ps_drop > sniff__adjust_slowpoll_dropped) {
unsigned dropped = stat.ps_drop - sniff__adjust_slowpoll_dropped;
if (libslowpoll_wait > 500) {
/* Decrease slowpoll wait time */
libslowpoll_wait /= 2;
fprintf(stderr, "(dropped %u; reducing slowpoll_wait to %d)\n",
dropped, libslowpoll_wait);
fprintf(stderr, "%u packets dropped by kernel: "
"reducing slowpoll_wait to %d\n", dropped,
libslowpoll_wait);
} else {
fprintf(stderr, "(dropped %u; your system is too slow)\n", dropped);
fprintf(stderr, "%u packets dropped by kernel: "
"your system is too slow\n", dropped);
}
}
prev_drop = stat.ps_drop;
sniff__adjust_slowpoll_dropped = stat.ps_drop;
}
}

0 comments on commit c091bff

Please sign in to comment.