-
Notifications
You must be signed in to change notification settings - Fork 7
/
sipnagios.c
2414 lines (1960 loc) · 61.4 KB
/
sipnagios.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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $Id$ */
/*
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
* Copyright (C) 2003-2008 Benny Prijono <[email protected]>
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* sipnagios.c (this file) is brought to you by
* Giovanni Maruzzelli
* gmaruzz at OpenTelecom.IT
*
* sipnagios.c is a modification of the original
* sample siprtp.c in pjproject distribution
*
* enjoy!
*/
/******************************************
HOW TO COMPILE SIPNAGIOS:
apt install python-dev gcc make gcc binutils build-essential git wget unzip uuid-dev
cd /usr/src
git clone https://github.com/gmaruzz/sipnagios.git
wget https://github.com/pjsip/pjproject/archive/refs/tags/2.11.zip
unzip 2.11.zip
cd pjproject-2.11/
./configure
CFLAGS="$CFLAGS -fPIC -DPJ_LOG_MAX_LEVEL=3" make dep
cp ../sipnagios/sipnagios.c pjsip-apps/src/samples/
cp ../sipnagios/Samples.mak pjsip-apps/build/
CFLAGS="$CFLAGS -fPIC -DPJ_LOG_MAX_LEVEL=3" make
./pjsip-apps/bin/samples/x86_64-unknown-linux-gnu/sipnagios --help
******************************************/
/* Usage */
static const char *USAGE =
" PURPOSE: \n"
" sipnagios is a modification of the original siprtp sample in pjproject \n"
" \n"
" siprtp original blurb: \n"
" This program establishes SIP INVITE session and media, and calculate \n"
" the media quality (packet lost, jitter, rtt, etc.). Unlike normal \n"
" pjmedia applications, this program bypasses all pjmedia stream \n"
" framework and transmit encoded RTP packets manually using own thread. \n"
" \n"
" sipnagios blurb: \n"
" sipnagios implements the Nagios plugin API for monitoring and \n"
" performance data. It makes a call, checks all the various resulting \n"
" values (mos, rtt, pdd, tta, jitter, packet loss, bytes and packets \n"
" transferred, and so on). It verifies these values are included into \n"
" acceptable, warning, or critical ranges. If the call has gone well,\n"
" sipnagios print performance data for Nagios graphs, and returns 0.\n"
" If the call fails, or if its measured values are not inside acceptable \n"
" ranges, it exits with Nagios conventional WARNING or CRITICAL values.\n"
" \n"
"\n"
" USAGE:\n"
" sipnagios [options] SIPURL\n"
"\n"
" Call options:\n"
" --duration=SEC, -d Set maximum call duration in seconds (default:40) \n"
"\n"
" Address and ports options:\n"
" --local-port=PORT,-p Set local SIP port (default: 5060)\n"
" --rtp-port=PORT, -r Set start of RTP port (default: 4000)\n"
" --ip-addr=IP, -i Set local IP address to use (otherwise it will\n"
" try to determine local IP address from hostname)\n"
"\n"
" Local user options:\n"
" --local-user=USER, -u Set local SIP user (default: alice)\n"
" --local-password=PASSWD, -w Set local SIP user password (default: 1234)\n"
" --local-siprealm=REALM, -s Set local SIP user SIP realm (default: atlanta.example.com)\n"
"\n"
" Logging Options:\n"
" --log-level=N, -l Set log verbosity level (default=1)\n"
" --app-log-level=N Set app log verbosity (default=1)\n"
" --log-file=FILE Write log to file FILE\n"
"\n"
"NAGIOS RANGES OPTIONS DEFAULTS:\n"
"\n"
"-c --rxmosWARN = 4.0\n"
"-g --txmosWARN = 4.0\n"
"-t --rxmosCRIT = 3.6\n"
"-m --txmosCRIT = 3.6\n"
"\n"
"-A --durationmsWARN = 40000\n"
"-B --pddmsWARN = 8000\n"
"-C --ttamsWARN = 11000\n"
"-D --rxpktsWARN = 1000\n"
"-E --rxbytesWARN = 150000\n"
"-F --rxlosspctWARN = 1.5\n"
"-G --rxduppctWARN = 1.5\n"
"-H --rxreorderpctWARN =1.5\n"
"-I --rxjitavgWARN = 15.1\n"
"-L --txpktsWARN = 1000\n"
"-M --txbytesWARN = 150000\n"
"-N --txlosspctWARN = 1.5\n"
"-O --txduppctWARN = 1.5\n"
"-P --txreorderpctWARN =1.5\n"
"-Q --txjitavgWARN = 15.1\n"
"-R --rttmeanWARN = 150.1\n"
"\n"
"-S --durationmsCRIT = 20000\n"
"-T --pddmsCRIT = 16000\n"
"-U --ttamsCRIT = 20000\n"
"-V --rxpktsCRIT = 500\n"
"-Z --rxbytesCRIT = 75000\n"
"-K --rxlosspctCRIT = 3.0\n"
"-J --rxduppctCRIT = 3.0\n"
"-X --rxreorderpctCRIT =3.0\n"
"-Y --rxjitavgCRIT = 30.1\n"
"-v --txpktsCRIT = 500\n"
"-z --txbytesCRIT = 75000\n"
"-k --txlosspctCRIT = 3.0\n"
"-j --txduppctCRIT = 3.0\n"
"-x --txreorderpctCRIT =3.0\n"
"-y --txjitavgCRIT = 30.1\n"
"-b --rttmeanCRIT = 300.1\n"
"\n"
"==> rx and tx mos, durationms, rx and tx pkts and bytes becomes WARNING and CRITICAL if they're measured as LOWER than threshold\n"
"==> all other values becomes not OK if they are measured as HIGHER than threshold\n"
"\n"
"Eg:\n"
"sipnagios --rtp-port=4000 --local-port=8090 --ip-addr=188.166.74.47 --local-user=9599 --local-siprealm=acme.cloudpbx.opentelecom.it --local-password=cAcyAgaC46AKuRk sip:[email protected]:5030\n"
"(sipnagios is brought to you by Giovanni Maruzzelli. Enjoy!)\n"
;
/* Include all headers. */
#include <pjsip.h>
#include <pjmedia.h>
#include <pjmedia-codec.h>
#include <pjsip_ua.h>
#include <pjsip_simple.h>
#include <pjlib-util.h>
#include <pjlib.h>
#include <stdlib.h>
#include <uuid/uuid.h>
/* Uncomment these to disable threads.
* NOTE:
* when threading is disabled, sipnagios won't transmit any
* RTP packets.
*/
/*
#undef PJ_HAS_THREADS
#define PJ_HAS_THREADS 0
*/
#if PJ_HAS_HIGH_RES_TIMER==0
# error "High resolution timer is needed for this sample"
#endif
#define THIS_FILE "sipnagios.c"
#define MAX_CALLS 1024
#define RTP_START_PORT 4000
/* Codec descriptor: */
struct codec
{
unsigned pt;
char* name;
unsigned clock_rate;
unsigned bit_rate;
unsigned ptime;
char* description;
};
/* A bidirectional media stream created when the call is active. */
struct media_stream
{
/* Static: */
unsigned call_index; /* Call owner. */
unsigned media_index; /* Media index in call. */
pjmedia_transport *transport; /* To send/recv RTP/RTCP */
/* Active? */
pj_bool_t active; /* Non-zero if is in call. */
/* Current stream info: */
pjmedia_stream_info si; /* Current stream info. */
/* More info: */
unsigned clock_rate; /* clock rate */
unsigned samples_per_frame; /* samples per frame */
unsigned bytes_per_frame; /* frame size. */
/* RTP session: */
pjmedia_rtp_session out_sess; /* outgoing RTP session */
pjmedia_rtp_session in_sess; /* incoming RTP session */
/* RTCP stats: */
pjmedia_rtcp_session rtcp; /* incoming RTCP session. */
/* Thread: */
pj_bool_t thread_quit_flag; /* Stop media thread. */
pj_thread_t *thread; /* Media thread. */
};
/* This is a call structure that is created when the application starts
* and only destroyed when the application quits.
*/
struct call
{
unsigned index;
pjsip_inv_session *inv;
unsigned media_count;
struct media_stream media[1];
pj_time_val start_time;
pj_time_val response_time;
pj_time_val connect_time;
pj_timer_entry d_timer; /**< Disconnect timer. */
};
/* Application's global variables */
static struct app
{
unsigned max_calls;
unsigned call_gap;
pj_bool_t call_report;
pj_bool_t call_monitoring;
unsigned uac_calls;
unsigned duration;
pj_bool_t auto_quit;
unsigned thread_count;
int sip_port;
int rtp_start_port;
pj_str_t local_addr;
pj_str_t local_uri;
pj_str_t local_contact;
pj_str_t local_user;
pj_str_t local_password;
pj_str_t local_siprealm;
int app_log_level;
int log_level;
char *log_filename;
char *report_filename;
struct codec audio_codec;
pj_str_t uri_to_call;
pj_caching_pool cp;
pj_pool_t *pool;
pjsip_endpoint *sip_endpt;
pj_bool_t thread_quit;
pj_thread_t *sip_thread[1];
pjmedia_endpt *med_endpt;
struct call call[MAX_CALLS];
long durationmsWARN ; // A
long pddmsWARN ; //B
long ttamsWARN ; //C
long rxpktsWARN ; //D
long rxbytesWARN ; //E
float rxlosspctWARN ; //F
float rxduppctWARN ; //G
float rxreorderpctWARN ; //H
float rxjitavgWARN ; //I
long txpktsWARN ; //L
long txbytesWARN ; //M
float txlosspctWARN ; //N
float txduppctWARN ; //O
float txreorderpctWARN ; //P
float txjitavgWARN ; //Q
float rttmeanWARN ; //R
long durationmsCRIT ; //S
long pddmsCRIT ; //T
long ttamsCRIT ; //U
long rxpktsCRIT ; //V
long rxbytesCRIT ; //Z
float rxlosspctCRIT ; //K
float rxduppctCRIT ; //J
float rxreorderpctCRIT ; //X
float rxjitavgCRIT ; //Y
long txpktsCRIT ; //v
long txbytesCRIT ; //z
float txlosspctCRIT ; //k
float txduppctCRIT ; //j
float txreorderpctCRIT ; //x
float txjitavgCRIT ; //y
float rttmeanCRIT ; //b
float rxmosWARN ; //c
float txmosWARN ; //g
float rxmosCRIT ; //t
float txmosCRIT ; //m
} app;
/*
* Prototypes:
*/
/* Callback to be called when SDP negotiation is done in the call: */
static void call_on_media_update( pjsip_inv_session *inv,
pj_status_t status);
/* Callback to be called when invite session's state has changed: */
static void call_on_state_changed( pjsip_inv_session *inv,
pjsip_event *e);
/* Callback to be called when dialog has forked: */
static void call_on_forked(pjsip_inv_session *inv, pjsip_event *e);
/* Callback to be called to handle incoming requests outside dialogs: */
static pj_bool_t on_rx_request( pjsip_rx_data *rdata );
/* Worker thread prototype */
static int sip_worker_thread(void *arg);
/* Create SDP for call */
static pj_status_t create_sdp( pj_pool_t *pool,
struct call *call,
pjmedia_sdp_session **p_sdp);
/* Hangup call */
static void hangup_call(unsigned index);
/* Destroy the call's media */
static void destroy_call_media(unsigned call_index);
/* Destroy media. */
static void destroy_media();
/* This callback is called by media transport on receipt of RTP packet. */
static void on_rx_rtp(void *user_data, void *pkt, pj_ssize_t size);
/* This callback is called by media transport on receipt of RTCP packet. */
static void on_rx_rtcp(void *user_data, void *pkt, pj_ssize_t size);
/* Display error */
static void app_perror(const char *sender, const char *title,
pj_status_t status);
/* Convenient function to convert transmission factor to MOS */
static float rfactor_to_mos(float rfactor) {
float mos;
if (rfactor <= 0) {
mos = 0.0;
} else if (rfactor > 100) {
mos = 4.5;
} else {
mos = rfactor*4.5/100;
}
return mos;
}
static void print_call2(int call_index, struct app app)
{
struct call *call = &app.call[call_index];
int len;
pjsip_inv_session *inv = call->inv;
pjsip_dialog *dlg = inv->dlg;
struct media_stream *audio = &call->media[0];
char userinfo[PJSIP_MAX_URL_SIZE];
char duration[80], last_update[80];
char bps[16], ipbps[16], packets[16], bytes[16], ipbytes[16];
unsigned decor;
pj_time_val now;
long durationms;
decor = pj_log_get_decor();
pj_log_set_decor(0);
pj_gettimeofday(&now);
if (app.report_filename)
puts(app.report_filename);
/* Print duration */
if (inv->state >= PJSIP_INV_STATE_CONFIRMED && call->connect_time.sec) {
PJ_TIME_VAL_SUB(now, call->connect_time);
durationms = PJ_TIME_VAL_MSEC(now);
} else {
durationms = 0;
}
/* Call identification */
len = pjsip_hdr_print_on(dlg->remote.info, userinfo, sizeof(userinfo));
if (len < 0)
pj_ansi_strcpy(userinfo, "<--uri too long-->");
else
userinfo[len] = '\0';
if (call->inv == NULL || call->inv->state < PJSIP_INV_STATE_CONFIRMED ||
call->connect_time.sec == 0)
{
PJ_LOG(1, (THIS_FILE, "%s MOS[rx=%1.2f tx=%1.2f] Call %s TIME[pddms=%ldms ttams=%ldms durationms=%ldms] RX[rxmos=%1.2f rxpkts=%ld rxbytes=%ldB rxloss=%d rxlosspct=%3.1f%% rxdup=%d rxduppct=%3.1f%% rxreorder=%d rxreorderpct=%3.1f%% rxjitmin=%3.1fms rxjitavg=%3.1fms rxjitmax=%3.1fms] TX[txmos=%1.2f txpkts=%ld txbytes=%ldB txloss=%d txlosspct=%3.1f%% txdup=%d txduppct=%3.1f%% txreorder=%d txreorderpct=%3.1f%% txjitmin=%3.1fms txjitavg=%3.1fms txjitmax=%3.1fms] RTT[rttmin=%3.1fms rttmean=%3.1fms rttmax=%3.1fms] |durationms=%ldms pddms=%ldms ttams=%ldms rxmos=%1.2f rxpkts=%ld rxbytes=%ldB rxloss=%d rxlosspct=%3.1f%% rxdup=%d rxduppct=%3.1f%% rxreorder=%d rxreorderpct=%3.1f%% rxjitmin=%3.1fms rxjitavg=%3.1fms rxjitmax=%3.1fms txmos=%1.2f txpkts=%ld txbytes=%ldB txloss=%d txlosspct=%3.1f%% txdup=%d txduppct=%3.1f%% txreorder=%d txreorderpct=%3.1f%% txjitmin=%3.1fms txjitavg=%3.1fms txjitmax=%3.1fms rttmin=%3.1fms rttmean=%3.1fms rttmax=%3.1fms ",
"KO",
0.0,
0.0,
userinfo,
0,
0,
0,
0.0,
0 ,
0 ,
0 ,
0.0 ,
0 ,
0.0 ,
0 ,
0.0,
0.0 ,
0.0 ,
0.0 ,
0.0,
0 ,
0 ,
0 ,
0.0 ,
0 ,
0.0 ,
0 ,
0.0,
0.0 ,
0.0 ,
0.0 ,
0.0 ,
0.0 ,
0.0 ,
0,
0,
0,
0.0,
0 ,
0 ,
0 ,
0.0 ,
0 ,
0.0 ,
0 ,
0.0,
0.0 ,
0.0 ,
0.0 ,
0.0,
0 ,
0 ,
0 ,
0.0 ,
0 ,
0.0 ,
0 ,
0.0,
0.0 ,
0.0 ,
0.0 ,
0.0 ,
0.0 ,
0.0
));
pj_log_set_decor(decor);
exit(2); //NAGIOS CRITICAL
}
pj_time_val t;
long pddms;
long ttams;
long rxpkts = audio->rtcp.stat.rx.pkt;
long rxbytes = audio->rtcp.stat.rx.bytes;
int rxloss = audio->rtcp.stat.rx.loss;
float rxlosspct = audio->rtcp.stat.rx.loss * 100.0 / (audio->rtcp.stat.rx.pkt + audio->rtcp.stat.rx.loss);
int rxdup = audio->rtcp.stat.rx.dup;
float rxduppct = audio->rtcp.stat.rx.dup * 100.0 / (audio->rtcp.stat.rx.pkt + audio->rtcp.stat.rx.loss);
int rxreorder = audio->rtcp.stat.rx.reorder;
float rxreorderpct =audio->rtcp.stat.rx.reorder * 100.0 / (audio->rtcp.stat.rx.pkt + audio->rtcp.stat.rx.loss);
float rxjitmin = audio->rtcp.stat.rx.jitter.min / 1000.0;
float rxjitavg = audio->rtcp.stat.rx.jitter.mean / 1000.0;
float rxjitmax = audio->rtcp.stat.rx.jitter.max / 1000.0;
long txpkts = audio->rtcp.stat.tx.pkt;
long txbytes = audio->rtcp.stat.tx.bytes;
int txloss = audio->rtcp.stat.tx.loss;
float txlosspct = audio->rtcp.stat.tx.loss * 100.0 / (audio->rtcp.stat.tx.pkt + audio->rtcp.stat.tx.loss);
int txdup = audio->rtcp.stat.tx.dup;
float txduppct = audio->rtcp.stat.tx.dup * 100.0 / (audio->rtcp.stat.tx.pkt + audio->rtcp.stat.tx.loss);
int txreorder = audio->rtcp.stat.tx.reorder;
float txreorderpct =audio->rtcp.stat.tx.reorder * 100.0 / (audio->rtcp.stat.tx.pkt + audio->rtcp.stat.tx.loss);
float txjitmin = audio->rtcp.stat.tx.jitter.min / 1000.0;
float txjitavg = audio->rtcp.stat.tx.jitter.mean / 1000.0;
float txjitmax = audio->rtcp.stat.tx.jitter.max / 1000.0;
float rttmin = audio->rtcp.stat.rtt.min / 1000.0;
float rttmean = audio->rtcp.stat.rtt.mean / 1000.0;
float rttmax = audio->rtcp.stat.rtt.max / 1000.0;
char alarm[512];
alarm[0] = '\0'; // empty string
if (call->response_time.sec) {
t = call->response_time;
PJ_TIME_VAL_SUB(t, call->start_time);
pddms = PJ_TIME_VAL_MSEC(t);
} else {
pddms = 0;
}
if (call->connect_time.sec) {
t = call->connect_time;
PJ_TIME_VAL_SUB(t, call->start_time);
ttams = PJ_TIME_VAL_MSEC(t);
} else {
ttams = 0;
}
/*
https://medium.com/obkio/measuring-voip-quality-with-mos-score-mean-opinion-score-4de9c7541304
# VoIP Codec Processing Delay in milliseconds (ms)
codec_delay = 10.0
# Effective Latency is the Average Latency plus the doubled
# Average Jitter plus the VoIP Codec Delays
effective_latency = average_latency + 2* average_jitter + codec_delay
# Calculate the R-Value (Transmission Rating Factor R) based on
# Effective Latency. The voice quality drops more significantly
# over 160ms so the R-Value is penalized more.
if effective_latency < 160:
r = 93.2 - (effective_latency / 40)
else:
r = 93.2 - ((effective_latency - 120)/ 10)
# Reduce R-Value 2.5 times the Packet Loss percentage
r = r - 2.5 * packet_loss
# Based on ITU-T G.107
if r < 0:
mos = 1.0
else:
mos = 1 + 0.035*r + 0.000007*r*(r-60)*(100-r)
*/
/*****************************/
// merci Julien Chavanton, VoIP Patrol, https://github.com/jchavanton/voip_patrol/blob/master/src/voip_patrol/voip_patrol.cc
// mos calculation here is adapted from your work I can't even understand :)
/* represent loss dependent effective equipment impairment factor and percentage loss probability */
int Bpl = 25; /* packet-loss robustness factor Bpl is defined as a codec-specific value. */
float Ie_eff_rx, Ppl_rx, Ppl_cut_rx, Ie_eff_tx, Ppl_tx, Ppl_cut_tx;
int Ie = 0; /* Not used : Refer to Appendix I of [ITU-T G.113] for the currently recommended values of Ie.*/
float Ta = 0.0; /* Absolute Delay */
Ppl_rx = (rxloss+rxdup) * 100.0 / (rxpkts + rxloss);
Ppl_tx = (txloss+txdup) * 100.0 / (txpkts + txloss);
float BurstR_rx = 1.0;
Ie_eff_rx = (Ie + (95 - Ie) * Ppl_rx / (Ppl_rx/BurstR_rx + Bpl));
int rfactor_rx = 100 - Ie_eff_rx;
float rxmos = rfactor_to_mos(rfactor_rx);
float BurstR_tx = 1.0;
Ie_eff_tx = (Ie + (95 - Ie) * Ppl_tx / (Ppl_rx/BurstR_tx + Bpl));
int rfactor_tx = 100 - Ie_eff_tx;
float txmos = rfactor_to_mos(rfactor_tx);
/*****************************/
int retval = 0;
if( (100000 - durationms) > app.durationmsWARN ) {sprintf(alarm, "WARN durationms=%ld", durationms); retval = 1;}
if(pddms > app.pddmsWARN) {sprintf(alarm, "WARN pddms=%ld", pddms); retval = 1;}
if(ttams > app.ttamsWARN) {sprintf(alarm, "WARN ttams=%ld", ttams); retval = 1;}
if( (10000 - rxpkts) > app.rxpktsWARN ) {sprintf(alarm, "WARN rxpkts=%ld", rxpkts); retval = 1;}
if( (400000 - rxbytes) > app.rxbytesWARN ) {sprintf(alarm, "WARN rxbytes=%ld", rxbytes); retval = 1;}
if(rxlosspct > app.rxlosspctWARN) {sprintf(alarm, "WARN rxlosspct=%3.1f%%", rxlosspct); retval = 1;}
if(rxduppct > app.rxduppctWARN) {sprintf(alarm, "WARN rxduppct=%3.1f%%", rxduppct); retval = 1;}
if(rxreorderpct > app.rxreorderpctWARN) {sprintf(alarm, "WARN rxreorderpct=%3.1f%%", rxreorderpct); retval = 1;}
if(rxjitavg > app.rxjitavgWARN) {sprintf(alarm, "WARN rxjitavg=%3.1f", rxjitavg); retval = 1;}
if( (5.0 - rxmos) > app.rxmosWARN ) {sprintf(alarm, "WARN rxmos=%1.2f", rxmos); retval = 1;}
if( (10000 - txpkts) > app.txpktsWARN ) {sprintf(alarm, "WARN txpkts=%ld", txpkts); retval = 1;}
if( (400000 - txbytes) > app.txbytesWARN ) {sprintf(alarm, "WARN txbytes=%ld", txbytes); retval = 1;}
if(txlosspct > app.txlosspctWARN) {sprintf(alarm, "WARN txlosspct=%3.1f%%", txlosspct); retval = 1;}
if(txduppct > app.txduppctWARN) {sprintf(alarm, "WARN txduppct=%3.1f%%", txduppct); retval = 1;}
if(txreorderpct > app.txreorderpctWARN) {sprintf(alarm, "WARN txreorderpct=%3.1f%%", txreorderpct); retval = 1;}
if(txjitavg > app.txjitavgWARN) {sprintf(alarm, "WARN txjitavg=%3.1f", txjitavg); retval = 1;}
if( (5.0 - txmos) > app.txmosWARN ) {sprintf(alarm, "WARN txmos=%1.2f", txmos); retval = 1;}
if(rttmean > app.rttmeanWARN) {sprintf(alarm, "WARN rttmean=%3.1f", rttmean); retval = 1;}
if( (100000 - durationms) > app.durationmsCRIT ) {sprintf(alarm, "CRIT durationms=%ld", durationms); retval = 2;}
if(pddms > app.pddmsCRIT) {sprintf(alarm, "CRIT pddms=%ld", pddms); retval = 2;}
if(ttams > app.ttamsCRIT) {sprintf(alarm, "CRIT ttams=%ld", ttams); retval = 2;}
if( (10000 - rxpkts) > app.rxpktsCRIT ) {sprintf(alarm, "CRIT rxpkts=%ld", rxpkts); retval = 2;}
if( (400000 - rxbytes) > app.rxbytesCRIT ) {sprintf(alarm, "CRIT rxbytes=%ld", rxbytes); retval = 2;}
if(rxlosspct > app.rxlosspctCRIT) {sprintf(alarm, "CRIT rxlosspct=%3.1f%%", rxlosspct); retval = 2;}
if(rxduppct > app.rxduppctCRIT) {sprintf(alarm, "CRIT rxduppct=%3.1f%%", rxduppct); retval = 2;}
if(rxreorderpct > app.rxreorderpctCRIT) {sprintf(alarm, "CRIT rxreorderpct=%3.1f%%", rxreorderpct); retval = 2;}
if(rxjitavg > app.rxjitavgCRIT) {sprintf(alarm, "CRIT rxjitavg=%3.1f", rxjitavg); retval = 2;}
if( (5.0 - rxmos) > app.rxmosCRIT ) {sprintf(alarm, "CRIT rxmos=%1.2f", rxmos); retval = 2;}
if( (10000 - txpkts) > app.txpktsCRIT ) {sprintf(alarm, "CRIT txpkts=%ld", txpkts); retval = 2;}
if( (400000 - txbytes) > app.txbytesCRIT ) {sprintf(alarm, "CRIT txbytes=%ld", txbytes); retval = 2;}
if(txlosspct > app.txlosspctCRIT) {sprintf(alarm, "CRIT txlosspct=%3.1f%%", txlosspct); retval = 2;}
if(txduppct > app.txduppctCRIT) {sprintf(alarm, "CRIT txduppct=%3.1f%%", txduppct); retval = 2;}
if(txreorderpct > app.txreorderpctCRIT) {sprintf(alarm, "CRIT txreorderpct=%3.1f%%", txreorderpct); retval = 2;}
if(txjitavg > app.txjitavgCRIT) {sprintf(alarm, "CRIT txjitavg=%3.1f", txjitavg); retval = 2;}
if( (5.0 - txmos) > app.txmosCRIT ) {sprintf(alarm, "CRIT txmos=%1.2f", txmos); retval = 2;}
if(rttmean > app.rttmeanCRIT) {sprintf(alarm, "CRIT rttmean=%3.1f", rttmean); retval = 2;}
if(retval == 0) {sprintf(alarm, "OK");}
PJ_LOG(1, (THIS_FILE, "%s MOS[rx=%1.2f tx=%1.2f] Call %s TIME[pddms=%ldms ttams=%ldms durationms=%ldms] RX[rxmos=%1.2f rxpkts=%ld rxbytes=%ldB rxloss=%d rxlosspct=%3.1f%% rxdup=%d rxduppct=%3.1f%% rxreorder=%d rxreorderpct=%3.1f%% rxjitmin=%3.1fms rxjitavg=%3.1fms rxjitmax=%3.1fms] TX[txmos=%1.2f txpkts=%ld txbytes=%ldB txloss=%d txlosspct=%3.1f%% txdup=%d txduppct=%3.1f%% txreorder=%d txreorderpct=%3.1f%% txjitmin=%3.1fms txjitavg=%3.1fms txjitmax=%3.1fms] RTT[rttmin=%3.1fms rttmean=%3.1fms rttmax=%3.1fms] |durationms=%ldms pddms=%ldms ttams=%ldms rxmos=%1.2f rxpkts=%ld rxbytes=%ldB rxloss=%d rxlosspct=%3.1f%% rxdup=%d rxduppct=%3.1f%% rxreorder=%d rxreorderpct=%3.1f%% rxjitmin=%3.1fms rxjitavg=%3.1fms rxjitmax=%3.1fms txmos=%1.2f txpkts=%ld txbytes=%ldB txloss=%d txlosspct=%3.1f%% txdup=%d txduppct=%3.1f%% txreorder=%d txreorderpct=%3.1f%% txjitmin=%3.1fms txjitavg=%3.1fms txjitmax=%3.1fms rttmin=%3.1fms rttmean=%3.1fms rttmax=%3.1fms ",
alarm,
rxmos ,
txmos ,
userinfo,
pddms,
ttams,
durationms,
rxmos ,
rxpkts ,
rxbytes ,
rxloss ,
rxlosspct ,
rxdup ,
rxduppct ,
rxreorder ,
rxreorderpct,
rxjitmin ,
rxjitavg ,
rxjitmax ,
txmos ,
txpkts ,
txbytes ,
txloss ,
txlosspct ,
txdup ,
txduppct ,
txreorder ,
txreorderpct,
txjitmin ,
txjitavg ,
txjitmax ,
rttmin ,
rttmean ,
rttmax ,
durationms,
pddms,
ttams,
rxmos ,
rxpkts ,
rxbytes ,
rxloss ,
rxlosspct ,
rxdup ,
rxduppct ,
rxreorder ,
rxreorderpct,
rxjitmin ,
rxjitavg ,
rxjitmax ,
txmos ,
txpkts ,
txbytes ,
txloss ,
txlosspct ,
txdup ,
txduppct ,
txreorder ,
txreorderpct,
txjitmin ,
txjitavg ,
txjitmax ,
rttmin ,
rttmean ,
rttmax
));
PJ_LOG(1, (THIS_FILE, "\n"));
pj_log_set_decor(decor);
if(retval != 0) {
exit(retval);
}
}
/* This is a PJSIP module to be registered by application to handle
* incoming requests outside any dialogs/transactions. The main purpose
* here is to handle incoming INVITE request message, where we will
* create a dialog and INVITE session for it.
*/
static pjsip_module mod_sipnagios =
{
NULL, NULL, /* prev, next. */
{ "mod-sipnagiosapp", 13 }, /* Name. */
-1, /* Id */
PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
NULL, /* load() */
NULL, /* start() */
NULL, /* stop() */
NULL, /* unload() */
&on_rx_request, /* on_rx_request() */
NULL, /* on_rx_response() */
NULL, /* on_tx_request. */
NULL, /* on_tx_response() */
NULL, /* on_tsx_state() */
};
/* Codec constants */
struct codec audio_codecs[] =
{
{ 0, "PCMU", 8000, 64000, 20, "G.711 ULaw" },
{ 3, "GSM", 8000, 13200, 20, "GSM" },
{ 4, "G723", 8000, 6400, 30, "G.723.1" },
{ 8, "PCMA", 8000, 64000, 20, "G.711 ALaw" },
{ 18, "G729", 8000, 8000, 20, "G.729" },
};
/*
* Init SIP stack
*/
static pj_status_t init_sip()
{
unsigned i;
pj_status_t status;
/* init PJLIB-UTIL: */
status = pjlib_util_init();
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
/* Must create a pool factory before we can allocate any memory. */
pj_caching_pool_init(&app.cp, &pj_pool_factory_default_policy, 0);
/* Create application pool for misc. */
app.pool = pj_pool_create(&app.cp.factory, "app", 1000, 1000, NULL);
/* Create the endpoint: */
status = pjsip_endpt_create(&app.cp.factory, pj_gethostname()->ptr,
&app.sip_endpt);
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
/* Add UDP transport. */
{
pj_sockaddr_in addr;
pjsip_host_port addrname;
pjsip_transport *tp;
pj_bzero(&addr, sizeof(addr));
addr.sin_family = pj_AF_INET();
addr.sin_addr.s_addr = 0;
addr.sin_port = pj_htons((pj_uint16_t)app.sip_port);
if (app.local_addr.slen) {
addrname.host = app.local_addr;
addrname.port = app.sip_port;
status = pj_sockaddr_in_init(&addr, &app.local_addr,
(pj_uint16_t)app.sip_port);
if (status != PJ_SUCCESS) {
app_perror(THIS_FILE, "Unable to resolve IP interface", status);
return status;
}
}
status = pjsip_udp_transport_start( app.sip_endpt, &addr,
(app.local_addr.slen ? &addrname:NULL),
1, &tp);
if (status != PJ_SUCCESS) {
app_perror(THIS_FILE, "Unable to start UDP transport", status);
return status;
}
PJ_LOG(3,(THIS_FILE, "SIP UDP listening on %.*s:%d",
(int)tp->local_name.host.slen, tp->local_name.host.ptr,
tp->local_name.port));
}
/*
* Init transaction layer.
* This will create/initialize transaction hash tables etc.
*/
status = pjsip_tsx_layer_init_module(app.sip_endpt);
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
/* Initialize UA layer. */
status = pjsip_ua_init_module( app.sip_endpt, NULL );
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
/* Initialize 100rel support */
status = pjsip_100rel_init_module(app.sip_endpt);
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
/* Init invite session module. */
{
pjsip_inv_callback inv_cb;
/* Init the callback for INVITE session: */
pj_bzero(&inv_cb, sizeof(inv_cb));
inv_cb.on_state_changed = &call_on_state_changed;
inv_cb.on_new_session = &call_on_forked;
inv_cb.on_media_update = &call_on_media_update;
/* Initialize invite session module: */
status = pjsip_inv_usage_init(app.sip_endpt, &inv_cb);
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
}
/* Register our module to receive incoming requests. */
status = pjsip_endpt_register_module( app.sip_endpt, &mod_sipnagios);
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
/* Init calls */
for (i=0; i<app.max_calls; ++i)
app.call[i].index = i;
/* Done */
return PJ_SUCCESS;
}
/*
* Destroy SIP
*/
static void destroy_sip()
{
unsigned i;
app.thread_quit = 1;
for (i=0; i<app.thread_count; ++i) {
if (app.sip_thread[i]) {
pj_thread_join(app.sip_thread[i]);
pj_thread_destroy(app.sip_thread[i]);
app.sip_thread[i] = NULL;
}
}
if (app.sip_endpt) {
pjsip_endpt_destroy(app.sip_endpt);
app.sip_endpt = NULL;
}
}
/*
* Init media stack.
*/
static pj_status_t init_media()
{
unsigned i, count;
pj_uint16_t rtp_port;
pj_status_t status;
/* Initialize media endpoint so that at least error subsystem is properly
* initialized.
*/
#if PJ_HAS_THREADS
status = pjmedia_endpt_create(&app.cp.factory, NULL, 1, &app.med_endpt);
#else
status = pjmedia_endpt_create(&app.cp.factory,
pjsip_endpt_get_ioqueue(app.sip_endpt),
0, &app.med_endpt);
#endif
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
/* Must register codecs to be supported */
#if defined(PJMEDIA_HAS_G711_CODEC) && PJMEDIA_HAS_G711_CODEC!=0
pjmedia_codec_g711_init(app.med_endpt);
#endif
/* RTP port counter */
rtp_port = (pj_uint16_t)(app.rtp_start_port & 0xFFFE);
/* Init media transport for all calls. */
for (i=0, count=0; i<app.max_calls; ++i, ++count) {
unsigned j;
/* Create transport for each media in the call */
for (j=0; j<PJ_ARRAY_SIZE(app.call[0].media); ++j) {
/* Repeat binding media socket to next port when fails to bind
* to current port number.
*/
int retry;
app.call[i].media[j].call_index = i;
app.call[i].media[j].media_index = j;
status = -1;
for (retry=0; retry<100; ++retry,rtp_port+=2) {
struct media_stream *m = &app.call[i].media[j];
status = pjmedia_transport_udp_create2(app.med_endpt,
"sipnagios",
&app.local_addr,
rtp_port, 0,
&m->transport);
if (status == PJ_SUCCESS) {
rtp_port += 2;
break;
}
}
}
if (status != PJ_SUCCESS)
goto on_error;
}
/* Done */
return PJ_SUCCESS;
on_error:
destroy_media();
return status;
}
/*
* Destroy media.
*/
static void destroy_media()
{
unsigned i;
for (i=0; i<app.max_calls; ++i) {
unsigned j;
for (j=0; j<PJ_ARRAY_SIZE(app.call[0].media); ++j) {
struct media_stream *m = &app.call[i].media[j];
if (m->transport) {
pjmedia_transport_close(m->transport);
m->transport = NULL;
}
}
}
if (app.med_endpt) {