From c091bff9591a3ee56e058bd7fb46eb9abadcb4d5 Mon Sep 17 00:00:00 2001 From: Walter Doekes Date: Mon, 8 Apr 2019 18:15:53 +0200 Subject: [PATCH] Alter output to json --- out_rtpsniff.c | 68 ++++++++++++++++++++++++++++++++++++-------------- sniff_rtp.c | 35 ++++++++++++++------------ 2 files changed, 68 insertions(+), 35 deletions(-) diff --git a/out_rtpsniff.c b/out_rtpsniff.c index 8bab745..2ba45d6 100644 --- a/out_rtpsniff.c +++ b/out_rtpsniff.c @@ -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; @@ -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) @@ -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); } diff --git a/sniff_rtp.c b/sniff_rtp.c index 4dcc51e..e60f577 100644 --- a/sniff_rtp.c +++ b/sniff_rtp.c @@ -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); @@ -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 */ @@ -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 */ @@ -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; } }