-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yamon3.4.8.sh
1984 lines (1809 loc) · 64 KB
/
yamon3.4.8.sh
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
#!/bin/sh
##########################################################################
#
# Yet Another Monitor (YAMon)
# Copyright (c) 2013-present Al Caughey
# All rights reserved.
#
##########################################################################
#HISTORY
# 3.4.0 (2018-02-04): first release of 3.4... added better support for bridged devices, fixed multiple ips per mac; numerous other changes & fixes
# 3.4.1 (2018-02-10): fixed local ips; improved alert.js; reports on monthly usage vs cap
# 3.4.3 (2018-03-10): numerous fixes and firmware compatibility issues
# 3.4.4 (2018-03-12): re-added _path2ip; fixed null issues in clearDupIPs & checkIPs; recreate router.js if null; fixed totMem
# 3.4.4b (2018-03-28): replaced sed -E with sed -r
# 3.4.4c (2018-04-02): updated check for old duplicate IPs marked with x/y
# 3.4.5 (2018-04-02): added _local_ip6, cleaned up lots of log2file entries; added bytes & unknown to conntrack data; cleanup up iptables -L to -nL, removed unused getForwardData
# 3.4.6 (2018-08-11): added option to exclude failed/incomplete entries; check static leases on launch; detach from terminal
# 3.4.6a (2019-01-25): added check for firmware in updateStaticLeases()
# 3.4.6b (2019-01-26): suppressed the error output from crippled ip function; fixed owner replacement in updateStaticLeases()
# 3.4.7 (2019-01-29): fixed lingering _updated_ in users.js; better handling of crippled ip function for IPv6
# 3.4.8 (2019-04-19): minor updates, added _guest_iface
# ==========================================================
# Functions
# ==========================================================
setupIPChains(){
$send2log "setupIPChains ($cmd/$rule/$_includeIPv6)" 0
ip6chains_0()
{ #_includeIPv6=0
$send2log "ip6chains_0" 0
return
}
ip6chains_1()
{ #_includeIPv6=1
$send2log "ip6chains_1" 0
ipchains=$(eval ip6tables $_tMangleOption -nL -vx | grep Chain)
checkChain 'ip6tables' "$YAMON_IP6"
checkChain 'ip6tables' "${YAMON_IP6}Entry"
checkChain 'ip6tables' "${YAMON_IP6}Local"
addLocalBlocks 'ip6tables' "$YAMON_IP6" "$_PRIVATE_IP6_BLOCKS" "$_generic_ipv6"
checkIPChain 'ip6tables' 'FORWARD' "$YAMON_IP6"
checkIPChain 'ip6tables' 'INPUT' "$YAMON_IP6"
checkIPChain 'ip6tables' 'OUTPUT' "$YAMON_IP6"
}
checkChain(){
$send2log "checkChain: $1 $2" 0
local cmd="$1"
local chain="$2"
local ce=$(echo "$ipchains" | grep "$chain\b")
if [ -z "$ce" ] ; then
$send2log "Adding $chain in $cmd ($_tMangleOption)" 2
eval $cmd $_tMangleOption -N $chain
else
$send2log "$chain exists in $cmd ($_tMangleOption)" 0
fi
}
addLocalBlocks(){
$send2log "addLocalBlocks: $1 $2 $3 $4" 0
local cmd="$1"
local chain="$2"
local ip_blocks="$3"
local generic="$4"
eval $cmd $_tMangleOption -F "$chain"
eval $cmd $_tMangleOption -F "${chain}Entry"
eval $cmd $_tMangleOption -F "${chain}Local"
IFS=$','
for iprs in $ip_blocks
do
for iprd in $ip_blocks
do
if [ "$cmd" == 'iptables' ] ; then
eval $cmd $_tMangleOption -I "${chain}Entry" -g "${chain}Local" -s $iprs -d $iprd
else
eval $cmd $_tMangleOption -I "${chain}Entry" -j "RETURN" -s $iprs -d $iprd
eval $cmd $_tMangleOption -I "${chain}Entry" -j "${chain}Local" -s $iprs -d $iprd
fi
done
done
eval $cmd $_tMangleOption -A "${chain}Entry" -j "${chain}"
eval $cmd $_tMangleOption -I "${chain}Local" -j "RETURN" -s $generic -d $generic
#[ "$cmd" == 'iptables' ] && eval $cmd $_tMangleOption -A "$chain" -g "${chain}_gp_Unknown" -s $generic -d $generic
unset IFS
$send2log "chains --> $cmd / $chain
$(eval $cmd $_tMangleOption -nL -vx | grep $chain | grep Chain)" 0
}
addLocalIPs(){
$send2log "addLocalIPs: $1 $2 $3 $4" 0
local cmd="$1"
local chain="$2"
local ip_addresses="$3"
local generic="$4"
IFS=$','
for ip in $ip_addresses
do
eval $cmd $_tMangleOption -I "${chain}Entry" -g "${chain}Local" -s $ip -d $generic
eval $cmd $_tMangleOption -I "${chain}Entry" -g "${chain}Local" -s $generic -d $ip
done
unset IFS
}
ipchains=$(eval iptables $_tMangleOption -nL -vx | grep Chain)
checkChain 'iptables' "$YAMON_IP4"
checkChain 'iptables' "${YAMON_IP4}Entry"
checkChain 'iptables' "${YAMON_IP4}Local"
addLocalBlocks 'iptables' "$YAMON_IP4" "$_PRIVATE_IP4_BLOCKS" "$_generic_ipv4"
addLocalIPs 'iptables' "$YAMON_IP4" "$_LOCAL_IP4" "$_generic_ipv4"
checkIPChain "iptables" "FORWARD" "$YAMON_IP4"
checkIPChain "iptables" "INPUT" "$YAMON_IP4"
checkIPChain "iptables" "OUTPUT" "$YAMON_IP4"
eval "ip6chains_"$_includeIPv6
}
setInitValues(){
_configFile="$d_baseDir/config.file"
source "$_configFile"
loadconfig
d_max_digits=$(check4Overflow)
source "$d_baseDir/strings/$_lang/strings.sh"
setLogFile
setFirmware
setupIPChains
local sortStr=''
if [ -z "$(which sort)" ] || [ -z "$(which uniq)" ] ; then
hasUniq=0
tallyHourlyData="tallyHourlyData_0"
else
sortStr=" | sort -k2"
hasUniq=1
tallyHourlyData="tallyHourlyData_1"
fi
updateServerStats
setDataDirectories
if [ ! "$_guest_iface" == '' ] ; then
local spdn=$(echo "$_pndData" | grep "\"hour\":\"start\"")
if [ -z "$(echo $spdn | grep 'guest-')" ] ; then
gpdn=$(getPND 'start')
local g_do=$(getCV "$gpdn" 'guest-down')
local g_up=$(getCV "$gpdn" 'guest-up')
local nl=$(echo "$spdn" | sed -e "s~,\"lost\"~,\"guest-down\":$g_do,\"guest-up\":$g_up,\"lost\"~Ig")
$send2log "Update PDN entries to include guest- params: $nl" 1
_pndData="$nl
$(echo "$_pndData" | grep -v "\"hour\":\"start\"")"
echo "$_pndData" >&2
fi
fi
setWebDirectories
setUsers
setConfigJS
[ ! -d "$_lockDir" ] && mkdir "$_lockDir"
local ts=$(date +"%H:%M:%S")
if [ "$started" -eq "0" ] ; then
echo -e "$_s_started"
if [ "$_doLocalFiles" -gt "0" ] ; then
source "$d_baseDir/includes/getLocalCopies.sh"
getLocalCopies
fi
$send2log "YAMon was started" 99
fi
meminfo=$(cat /proc/meminfo | tr -s ' ')
_totMem=$(getMI "MemTotal")
$send2log "_totMem: $_totMem" 0
local iginc=''
[ "$_includeIncomplete" -eq "0" ] && iginc=" grep -v '00:00:00:00:00:00' | " #completely disregard incomplete macs
[ -z "$_path2ip" ] && _path2ip=$(which ip)
if [ "$_firmware" -eq "4" ] ; then
_getIP4List="cat /proc/net/arp | grep '^[0-9]' | $iginc tr -s ' ' | cut -d' ' -f1,4 | tr 'A-Z' 'a-z' $sortStr"
else
$($_path2ip -4 neigh show >> /tmp/ipv4.text 2>&1)
if [ $? -eq 0 ] ; then
$send2log "Using ip to detect active IP/MAC combinations" 1
_getIP4List="$_path2ip -4 neigh | cut -d' ' -f1,5 | tr 'A-Z' 'a-z' $sortStr"
else
_getIP4List="cat /proc/net/arp | grep '^[0-9]' | $iginc tr -s ' ' | cut -d' ' -f1,4 | tr 'A-Z' 'a-z' $sortStr"
$send2log "Using arp to detect active IP/MAC combinations" 1
fi
rm '/tmp/ipv4.text'
fi
$send2log "_getIP4List-->$_getIP4List" 0
if [ "$_includeIPv6" -eq "1" ] ; then
_local_ip6=${_local_ip6//,/|}
_getIP6List="$_path2ip -6 neigh | grep -Evi \"$_local_ip6\" | cut -d' ' -f1,5 | tr 'A-Z' 'a-z' $sortStr"
$send2log "_getIP6List-->$_getIP6List" 0
$send2log "_local_ip6-->$_local_ip6" 0
fi
_usersLastMod=$(date -r "$_usersFile" "+%Y-%d-%m %T")
started=1
}
setLogFile()
{
if [ "${_logDir:0:1}" == "/" ] ; then
local lfpath=$_logDir
else
local lfpath="${d_baseDir}/$_logDir"
fi
lfpath=${lfpath//\/\//\/}
[ ! -d "$lfpath" ] && mkdir -p "$lfpath"
_logfilename="${lfpath}monitor$_version-$_cYear-$_cMonth-$_cDay.log"
[ ! -d "$$_wwwPath${_wwwJS}" ] && mkdir -p "$_wwwPath${_wwwJS}"
_alertfilename="$_wwwPath${_wwwJS}alerts.js"
[ ! -f "$_logfilename" ] && touch "$_logfilename"
if [ ! -f "$_alertfilename" ] ; then
touch $_alertfilename
$send2log "Created $_alertfilename" 0
fi
$send2log "YAMon:: version $_version _loglevel: $_loglevel" 1
$send2log "setLogFile" 0
$send2log "Installed firmware: $installedfirmware $installedversion $installedtype" 1
$send2log "_logfilename-->$_logfilename" 1
}
setDataDirectories()
{
$send2log "setDataDirectories" 0
local rMonth=${_cMonth#0}
local rYear="$_cYear"
local rday=$(printf %02d $_ispBillingDay)
if [ "$_cDay" -lt "$_ispBillingDay" ] ; then
rMonth=$(($rMonth-1))
if [ "$rMonth" == "0" ] ; then
rMonth=12
rYear=$(($rYear-1))
fi
fi
rMonth=$(printf %02d $rMonth)
if [ "${_dataDir:0:1}" == "/" ] ; then
_dataPath=$_dataDir
else
_dataPath="${d_baseDir}/$_dataDir"
fi
$send2log ">>> _dataPath --> $_dataPath" 0
if [ ! -d "$_dataPath" ] ; then
$send2log ">>> Creating data directory" 0
mkdir -p "$_dataPath"
chmod -R 666 "$_dataPath"
fi
case $_organizeData in
(*"0"*)
local savePath="$_dataPath"
local wwwsavePath="$_wwwPath$_wwwData"
;;
(*"1"*)
local savePath="$_dataPath$rYear/"
local wwwsavePath="$_wwwPath$_wwwData$rYear/"
;;
(*"2"*)
local savePath="$_dataPath$rYear/$rMonth/"
local wwwsavePath="$_wwwPath$_wwwData$rYear/$rMonth/"
;;
esac
savePath=${savePath//\/\//\/}
wwwsavePath=${wwwsavePath//\/\//\/}
if [ ! -d "$savePath" ] ; then
$send2log ">>> Adding data directory - $savePath " 0
mkdir -p "$savePath"
chmod -R 666 "$savePath"
else
$send2log ">>> data directory exists - $savePath " -1
fi
if [ "$_symlink2data" -eq "0" ] && [ ! -d "$wwwsavePath" ] ; then
$send2log ">>> Adding web directory - $wwwsavePath " 0
mkdir -p "$wwwsavePath"
chmod -R 666 "$wwwsavePath"
else
$send2log ">>> web directory exists - $wwwsavePath " -1
fi
[ "$(ls -A $_dataPath)" ] && $copyfiles "$_dataPath*" "$_wwwPath$_wwwData"
_macUsageDB="$savePath$rYear-$rMonth-$_usageFileName"
_macUsageWWW="$wwwsavePath$rYear-$rMonth-$rday-$_usageFileName"
local old_macUsageDB="$savePath$rYear-$rMonth-$rday-$_usageFileName"
if [ -f "$_macUsageDB" ] ; then
$send2log "_macUsageDB exists--> $_macUsageDB" 1
elif [ -f "$old_macUsageDB" ] ; then
$send2log "copying $old_macUsageDB --> $_macUsageDB" 1
$(cp -a $old_macUsageDB $_macUsageDB)
else
createMonthlyFile
fi
[ ! -d "$_wwwPath$_wwwJS" ] && mkdir -p "$_wwwPath$_wwwJS"
[ "$_doLiveUpdates" -eq "1" ] && _liveFilePath="$_wwwPath$_wwwJS$_liveFileName"
[ "$_doArchiveLiveUpdates" -eq "1" ] && _liveArchiveFilePath="$wwwsavePath$_cYear-$_cMonth-$_cDay-$_liveFileName"
_hourlyUsageDB="$savePath$_cYear-$_cMonth-$_cDay-$_hourlyFileName"
_hourlyUsageWWW="$wwwsavePath$_cYear-$_cMonth-$_cDay-$_hourlyFileName"
[ ! -f "$_hourlyUsageDB" ] && createHourlyFile
local hd=$(cat "$_hourlyUsageDB")
local hdhu=$(echo "$hd" | grep '^hu' )
local hdpd=$(echo "$hd" | grep '^pnd' )
_hourlyCreated=$(echo "$hd" | grep '^var hourly_created')
local hr=$(date +"%H")
_hourlyData=$(echo "$hdhu" | grep -v "\"hour\":\"$hr\"")
_thisHrdata=$(echo "$hdhu" | grep "\"hour\":\"$hr\"")
_pndData=$(echo "$hdpd" | grep -v "\"hour\":\"$hr\"")
_thisHrpnd=$(echo "$hdpd" | grep "\"hour\":\"$hr\"")
$send2log "_hourlyData--> $_hourlyData" -1
$send2log "_thisHrdata ($hr)--> $_thisHrdata" 0
$send2log "_pndData--> $_pndData" -1
$send2log "_thisHrpnd ($hr)--> $_thisHrpnd" 0
routerfile="${d_baseDir}/${_webDir}/${_wwwJS}/router.js"
routerfile=${routerfile//\/\//\/}
[ -f "$routerfile" ] && return
$send2log "recreated $routerfile" 2
local installed=$(date +"%Y-%m-%d %H:%M:%S")
local updated=''
local routermodel='Unknown'
local installedfirmware=''
local installedversion=''
local installedtype=''
if [ -f "/etc/openwrt_release" ] ; then
installedfirmware=$(cat /etc/openwrt_release | grep -i 'DISTRIB_DESCRIPTION' | cut -d"'" -f2)
elif [ "$_firmware" -eq "2" ] ; then
routermodel=$(nvram get model)
installedversion=$(nvram get buildno)_$(nvram get extendno)
installedtype='merlin'
elif [ "$_has_nvram" -eq "1" ] ; then
installedfirmware=$(uname -o)
routermodel=$(nvram get DD_BOARD)
installedversion=$(nvram get os_version)
installedtype=$(nvram get dist_type)
fi
if [ -d /tmp/sysinfo/ ] ; then
local model=$(cat /tmp/sysinfo/model)
local board=$(cat /tmp/sysinfo/board_name)
routermodel="$model $board"
fi
echo "var installed='$installed'
var updated='$updated'
var router='$routermodel'
var firmware='$installedfirmware $installedversion $installedtype'
var version='$_version'" > $routerfile
}
createMonthlyFile()
{
$send2log "createMonthlyFile" 0
$send2log ">>> Monthly usage file not found... creating new file: $_macUsageDB" 2
local ds=$(date +"%Y-%m-%d %H:%M:%S")
touch $_macUsageDB
chmod 666 $_macUsageDB
local nmf="var monthly_created=\"$ds\"
var monthly_updated=\"$ds\"
var monthlyDataCap=\"$_monthlyDataCap\""
$save2File "$nmf" "$_macUsageDB"
$copyfiles "$_macUsageDB" "$_macUsageWWW"
}
createHourlyFile()
{
$send2log "createHourlyFile" 0
touch $_hourlyUsageDB
chmod 666 $_hourlyUsageDB
$send2log ">>> Hourly usage file not found... creating new file: $_hourlyUsageDB" 2
$doliveUpdates
local upsec=$(cat /proc/uptime | cut -d' ' -f1)
local ds=$(date +"%Y-%m-%d %H:%M:%S")
local hc="var hourly_created=\"$ds\""
_pndData=$(getPND 'start' "$upsec")
local hourlyHeader=$(getHourlyHeader "$upsec" "$ds")
_hourlyData=''
local nht="$_hourlyCreated
$hourlyHeader
$_pndData"
$save2File "$nht" "$_hourlyUsageDB"
$copyfiles "$_hourlyUsageDB" "$_hourlyUsageWWW"
}
getMI()
{
local result=$(echo "$meminfo" | grep -i "^$1:" | cut -d' ' -f2)
[ -z "$result" ] && result=0
echo "$result"
$send2log "getMI: $1=$result" 0
}
getHourlyHeader(){
$send2log "getHourlyHeader: $1 $2" 0
meminfo=$(cat /proc/meminfo | tr -s ' ')
local freeMem=$(getMI "MemFree")
local bufferMem=$(getMI "Buffers")
local cacheMem=$(getMI "Cached")
local availMem=$(($freeMem+$bufferMem+$cacheMem))
local disk_utilization=$(df "${d_baseDir}" | tail -n 1 | tr -s ' ' | cut -d' ' -f5)
echo "
var hourly_updated=\"$2\"
var users_updated=\"$_usersLastMod\"
var disk_utilization=\"$disk_utilization\"
var serverUptime=\"$1\"
var freeMem=\"$freeMem\",availMem=\"$availMem\",totMem=\"$_totMem\"
serverloads(\"$sl_min\",\"$sl_min_ts\",\"$sl_max\",\"$sl_max_ts\")
"
}
getPND(){
guestPND_0(){
#_guest_iface
$send2log "guestPND_0: _guest_iface: null" 0
echo ''
}
guestPND_1(){
#_guest_iface contains a single entry or CSV list
local brg=$(echo "$pnd" | grep -i "$_guest_iface")
$send2log "guestPND_1 --> _guest_iface: $_guest_iface" 0
$send2log "*** guestPND_1: brg: $brg" -1
local g_d=0
local g_u=0
IFS=$'\n'
for line in $brg
do
l_d=$(echo $line | cut -d' ' -f11) || $(echo $line | cut -d' ' -f12) || 0
l_u=$(echo $line | cut -d' ' -f3) || $(echo $line | cut -d' ' -f4) || 0
g_d=$(($g_d + $l_d))
g_u=$(($g_u + $l_u))
done
echo "\"guest-down\":$g_d,\"guest-up\":$g_u,"
}
$send2log "getPND: $1 $2" 0
local thr="$1"
local pnd=$(cat "/proc/net/dev" | tr -s ': ' ' ')
local br0=$(echo "$pnd" | grep -i "$_lan_iface")
$send2log "*** PND: br0: [$br0]" -1
local br_d=$(echo $br0 | cut -d' ' -f10) || $(echo $br0 | cut -d' ' -f11) || 0
local br_u=$(echo $br0 | cut -d' ' -f2) || br_u=$(echo $br0 | cut -d' ' -f3) || 0
local guest_traffic=$(eval $guestPND)
$send2log "*** PND: br_d: $br_d br_u: $br_u" -1
$send2log "*** guest_traffic: $guest_traffic" -1
local result="pnd({\"hour\":\"$thr\",\"uptime\":$2,\"down\":$br_d,\"up\":$br_u,$guest_traffic\"lost\":$_totalLostBytes,\"hr-loads\":\"$hr_min1,$hr_min5,$hr_max5,$hr_max1\"})"
echo "$result"
$send2log "getPND: result-->$result" 0
}
setUsers(){
fixIncompletes()
{
local gplist="$(cat $_usersFile | grep -i "in:co:mp:le:te-\d\b")"
IFS=$'\n'
for lline in $gplist
do
local mac=$(getField "$lline" 'mac')
local m=$(echo "$mac" | cut -d- -f1)
local n=$(printf %02d $(echo "$mac" | cut -d- -f2) )
_currentUsers=$(echo "$_currentUsers" | sed -e "s~$mac\b~$m-$n~Ig")
_changesInUsersJS=$(($_changesInUsersJS + 1))
done
}
getGroups_0()
{ #for firmware without sort/uniq
local gplist="$(cat $_usersFile | grep -o \"owner\":\"[^\"]*\" | cut -d: -f2 | sed "s~[^a-z0-9]~~ig")"
local retlist=''
IFS=$'\n'
for lline in $gplist
do
local gp=${lline//\"/}
if [ -z "$(echo $retlist | grep $gp)" ] ; then
retlist="$gp
$retlist"
fi
done
echo "$retlist"
}
getGroups_1()
{ #for firmware with sort/uniq
echo "$(cat $_usersFile | grep -o \"owner\":\"[^\"]*\" | cut -d: -f2 | sort | uniq | sed "s~[^a-z0-9]~~ig")"
}
$send2log "setUsers" 0
_usersFile="$_dataPath$_usersFileName"
[ "$_symlink2data" -eq "0" ] && _usersFileWWW="$_wwwPath$_wwwData$_usersFileName"
[ ! -f "$_usersFile" ] && createUsersFile
_currentUsers=$(cat "$_usersFile" | sed -e "s~(dup) (dup)~(dup)~Ig" | sed -e "s~0.0.0.0_0~0.0.0.0\/0~Ig")
fixIncompletes
local groups=$(eval "getGroups_$hasUniq")
local gpchains=$(eval iptables $_tMangleOption -nL -vx | grep "${YAMON_IP4}_gp_")
IFS=$'\n'
for group in $groups
do
[ -z "$group" ] && continue;
local gc="${YAMON_IP4}_gp_$group"
local ce=$(echo "$gpchains" | grep "$group\b")
if [ -z "$ce" ] ; then
$send2log "Adding group chain to iptables: $gc " 2
eval iptables $_tMangleOption -N "$gc"
eval iptables $_tMangleOption -A "$gc" -j "RETURN" -s $_generic_ipv4 -d $_generic_ipv4
else
$send2log "Group chain $gc exists in iptables" 0
fi
done
unset IFS
ifcl=$(ifconfig | grep HWaddr | tr -s ' ' | cut -d ' ' -f1,5)
$send2log "ifcl: $ifcl" -1
IFS=$'\n'
for line in $ifcl
do
[ -z "$line" ] && continue
f1=$(echo "$line" | cut -d' ' -f1)
mac=$(echo "$line" | cut -d' ' -f2 | tr 'A-Z' 'a-z')
ip4=$(ifconfig $f1 | grep 'inet addr' | tr -s ' ' | cut -d' ' -f3 | sed "s~addr:~~ig")
$send2log "line: $line" -1
if [ -n "$ip4" ] ; then
$send2log "$ip4 / $mac" -1
checkIPTableEntries "iptables" "$YAMON_IP4" "$ip4" "$mac"
CheckUsersJS $mac $ip4 0 "Hardware" "$f1" 1
fi
if [ "$_includeIPv6" -eq "1" ] ; then
ip6=$(ifconfig $f1 | grep 'inet6' | grep -Ev "$_local_ip6" | tr -s ' ' | cut -d' ' -f4 | cut -d/ -f1)
ipl=''
comma=''
$send2log "ip6: $ip6" -1
for ips in $ip6
do
$send2log "$ips / $mac" 0
[ -z "$(echo "$ips" | grep -Ei "$_local_ip6")" ] && checkIPTableEntries "ip6tables" "$YAMON_IP6" "$ips" "$mac"
ipl="$ipl$comma$ips"
comma=','
done
[ -n "$ipl" ] && CheckUsersJS $mac $ipl 1 "Hardware" "$f1" 1
fi
done
if [ -n "$_lan_ipaddr" ] ; then
local lipe=$(echo "$_currentUsers" | grep -i "\b$_lan_hwaddr\b")
[ -z "$lipe" ] && CheckUsersJS $_lan_hwaddr $_lan_ipaddr 0 "Hardware" "LAN MAC"
fi
if [ -n "$_wan_ipaddr" ] ; then
lipe=$(echo "$_currentUsers" | grep -i "\b$_wan_hwaddr\b")
[ -z "$lipe" ] && [ "$_wan_hwaddr" != "$_lan_hwaddr" ] && CheckUsersJS $_wan_hwaddr $_wan_ipaddr 0 "Hardware" "WAN MAC"
fi
lipe=$(echo "$_currentUsers" | grep "\b$_generic_ipv4\b")
[ -z "$lipe" ] && CheckUsersJS $_generic_mac $_generic_ipv4 0 "Unknown" "No Matching MAC"
$send2log "$_generic_ipv4 / $_generic_mac" 0
checkIPTableEntries "iptables" "$YAMON_IP4" "$_generic_ipv4" "$_generic_mac"
if [ -n "$_lan_ipaddr" ] ; then
$send2log "$_lan_ipaddr / $_lan_hwaddr" 0
checkIPTableEntries "iptables" "$YAMON_IP4" "$_lan_ipaddr" "$_lan_hwaddr"
fi
if [ -n "$_wan_ipaddr" ] ; then
$send2log "$_wan_ipaddr / $_wan_hwaddr" 0
checkIPTableEntries "iptables" "$YAMON_IP4" "$_wan_ipaddr" "$_wan_hwaddr"
fi
if [ "$_includeIPv6" -eq "1" ] ; then
mline=$(echo "$_currentUsers" | grep "[\b\"]$_generic_ipv6\b")
[ -z "$mline" ] && CheckUsersJS $_generic_mac $_generic_ipv6 1
local gp6chains=$(eval ip6tables $_tMangleOption -nL -vx | grep "${YAMON_IP6}_gp_")
IFS=$'\n'
for group in $groups
do
[ -z "$group" ] && continue;
local gc="${YAMON_IP6}_gp_$group"
local ce=$(echo "$gp6chains" | grep "$group\b")
if [ -z "$ce" ] ; then
$send2log "Adding group chain to ip6tables: $gc " 2
eval ip6tables $_tMangleOption -N "$gc"
eval ip6tables $_tMangleOption -A "$gc" -j "RETURN" -s $_generic_ipv6 -d $_generic_ipv6
else
$send2log "Group chain $gc exists in ip6tables" 0
fi
done
unset IFS
$send2log "$_generic_ipv6 / $_generic_mac" 0
checkIPTableEntries "ip6tables" "$YAMON_IP6" "$_generic_ipv6" "$_generic_mac"
[ -n "$_lan_ip6addr" ] && [ -z "$(echo "$_lan_ip6addr" | grep -Ei "$_local_ip6")" ] && checkIPTableEntries "ip6tables" "$YAMON_IP6" "$_lan_ip6addr" "$_lan_hwaddr"
fi
$send2log "started-->$started _includeIPv6-->$_includeIPv6 " -1
[ "$started" -eq "0" ] && checkUsers4IP
$send2log "_currentUsers -->
$_currentUsers" -1
IFS=$'\n'
local device_list=$(echo "$_currentUsers" | grep -e "^ud_a")
for device in $device_list
do
$send2log "device: $device" 0
ip4=$(getField "$device" 'ip')
mac=$(getField "$device" 'mac')
owner=$(getField "$device" 'owner')
name=$(getField "$device" 'name')
local lastSeen=$(getField "$device" 'last-seen')
IFS=$','
for ip in $ip4
do
[ -z "$(echo "$ip" | grep '(dup)')" ] || continue
wip='ip'
if [ -n "$(echo "${ip:0:1}" | grep -i "[xy]")" ] ; then
o_ip="$ip"
ip="${ip:1} (dup)"
tip=${ip//\./\\.}
mline="$device"
updateinUsersJS
IFS=$','
fi
[ ! -z "$lastSeen" ] && checkIPTableEntries "iptables" "$YAMON_IP4" "$ip" "$mac"
#CheckUsersJS $mac "$ip" 0 "$owner" "$name"
done
if [ "$_includeIPv6" -eq "1" ] ; then
ip6=$(getField "$device" 'ip6')
$send2log "ip6: $ip6" -1
for ip in $ip6
do
[ -z "$(echo "$ip" | grep '(dup)')" ] || continue
[ ! -z "$lastSeen" ] && [ -z "$(echo "$ip" | grep -Ei "$_local_ip6")" ] && checkIPTableEntries "ip6tables" "$YAMON_IP6" "$ip" "$mac"
done
fi
IFS=$'\n'
done
unset IFS
if [ "$_changesInUsersJS" -gt "0" ] ; then
$send2log ">>> $_changesInUsersJS changes in users.js" 1
$save2File "$_currentUsers" "$_usersFile"
fi
}
checkUsers4IP()
{
$send2log "checkUsers4IP" 0
local ccd=$(echo "$_currentUsers" | grep 'users_created' | cut -d= -f2)
ccd=${ccd//\"/}
[ -z "$ccd" ] && ccd=$(date +"%Y-%m-%d %H:%M:%S")
IFS=$'\n'
local ncu="var users_created=\"$ccd\"
"
local nline=''
local cdl=$(echo "$_currentUsers" | grep 'ud_a')
local dups=''
for device in $cdl
do
local hasIP=$(echo $device | grep '\"ip\"')
local hasIP6=$(echo $device | grep '\"ip6\"')
if [ -z "$hasIP" ] && [ -z "$hasIP6" ] ; then
nline=$(echo $device | sed -e "s~\"owner\"~\"ip\":\"\",\"ip6\":\"\",\"owner\"~Ig" )
elif [ -z "$hasIP6" ] ; then
nline=$(echo $device | sed -e "s~\"owner\"~\"ip6\":\"\",\"owner\"~Ig" )
elif [ -z "$hasIP" ] ; then
nline=$(echo $device | sed -e "s~\"ip6\"~\"ip\":\"\",\"ip6\"~Ig" )
else
nline="$device"
fi
local hasLS=$(echo $device | grep '\"last-seen\"')
if [ -z "$hasLS" ] ; then
nline=$(echo $device | sed -e "s~})~,\"last-seen\":\"\"})~Ig" )
fi
ncu="$ncu
$nline"
local mac=$(getField "$device" 'mac')
local nm=$(echo "$cdl" | grep -ic "\b$mac\b")
[ $nm -eq 1 ] && continue
local de=$(echo "$dups" | grep -i "$mac")
if [ -z "$de" ] && [ -z "$dups" ] ; then
dups=" $mac"
elif [ -z "$de" ] ; then
dups="$dups
$mac"
fi
done
[ -n "$dups" ] && [ "$_allowMultipleIPsperMAC" -eq "0" ] && $send2log "There are duplicated mac addresses in $_usersFile:
$dups" 99
_currentUsers="$ncu"
$save2File "$_currentUsers" "$_usersFile"
}
createUsersFile()
{
$send2log "createUsersFile" 0
local ds=$(date +"%Y-%m-%d %H:%M:%S")
local users=''
$send2log ">>> Creating empty users file: $_usersFile" -1
touch $_usersFile
chmod 666 $_usersFile
users="var users_created=\"$ds\""
}
setFirmware()
{
$send2log "setFirmware ($firmware)" 0
if [ -f "/proc/net/ip_conntrack" ] && [ "$_use_nf_conntrack" -ne "1" ] ; then
_conntrack="/proc/net/ip_conntrack"
_conntrack_awk='BEGIN { printf "var curr_connections=[ "} { gsub(/(src|dst|sport|dport|bytes)=/, ""); if($1 == "tcp"){ printf "[ '\''%s'\'','\''%s'\'','\''%s'\'','\''%s'\'','\''%s'\'','\''%s'\'' ],",$1,$5,$7,$6,$8,$10;} else if($1 == "udp"){ printf "[ '\''%s'\'','\''%s'\'','\''%s'\'','\''%s'\'','\''%s'\'','\''%s'\'' ],",$1,$4,$6,$5,$7,$9;} else { printf "[ '\''%s'\'','\''%s'\'','\'\'','\''%s'\'','\'\'','\''%s'\'' ],",$1,$4,$5,$9;} } END { print "[ null ] ]"}'
else
_conntrack="/proc/net/nf_conntrack"
_conntrack_awk='BEGIN { printf "var curr_connections=[ "} { gsub(/(src|dst|sport|dport|bytes)=/, ""); if($3 == "tcp"){ printf "[ '\''%s'\'','\''%s'\'','\''%s'\'','\''%s'\'','\''%s'\'','\''%s'\'' ],",$3,$7,$9,$8,$10,$12;} else if($3 == "udp"){ printf "[ '\''%s'\'','\''%s'\'','\''%s'\'','\''%s'\'','\''%s'\'','\''%s'\'' ],",$3,$6,$8,$7,$9,$11;} else { printf "[ '\''%s'\'','\''%s'\'','\'\'','\''%s'\'','\'\'','\''%s'\'' ],",$3,$6,$7,$9;} } END { print "[ null ] ]"}'
fi
_lan_iface='br0'
if [ "$_firmware" -eq "1" ] || [ "$_firmware" -eq "4" ] || [ "$_firmware" -eq "6" ] || [ "$_firmware" -eq "7" ] ; then #OpenWRT & variants
_lan_iface="br-lan"
fi
$send2log "_lan_iface: $_lan_iface" 0
if [ "$_guest_iface" == '' ] ; then
guestPND="guestPND_0"
else
guestPND="guestPND_1"
fi
_guest_iface=${_guest_iface//,/\\|}
_lan_ipaddr=$(ifconfig $_lan_iface | grep 'inet addr:' | tr -s ' ' | cut -d' ' -f3 | cut -d: -f2)
_lan_hwaddr=$(ifconfig $_lan_iface | grep 'HWaddr' | tr -s ' ' | cut -d' ' -f5 | tr 'A-Z' 'a-z')
_wan_ipaddr=$(ifconfig 'eth0' | grep 'inet addr:' | tr -s ' ' | cut -d' ' -f3 | cut -d: -f2)
_wan_hwaddr=$(ifconfig 'eth0' | grep 'HWaddr' | tr -s ' ' | cut -d' ' -f5 | tr 'A-Z' 'a-z')
if [ "$_has_nvram" -eq "1" ] ; then
[ -z "$_wan_ipaddr" ] && _wan_ipaddr=$(nvram get wan_ipaddr)
[ -z "$wan_hwaddr" ] && _wan_hwaddr=$(nvram get wan_hwaddr)
[ "$(nvram get lan_proto)" == "dhcp" ] || $send2log "Router is not the DHCP Server for your network... You must enable this feature if you want to use YAMon!" 2
[ "$_firmware" -eq "0" ] && [ "$(nvram get sfe)" == "1" ] && $send2log "The \`Shortcut Forwarding Engine\` is enabled in your DD-WRT config ($_firmware)... You must disable this feature if you want to use YAMon!" 2
[ "$(nvram get upnp_enable)" == "1" ] && $send2log "\`UPnP\` is enabled in your DD-WRT config... It is recommended that you disable this feature if you want to use YAMon!" 2
[ "$(nvram get privoxy_enable)" == "1" ] && $send2log "\`Privoxy\` is enabled in your DD-WRT config... You must disable this feature if you want to use YAMon!" 2
[ "$(nvram get ntp_enable)" == "1" ] || $send2log "\`NTP Client\` is not enabled in your DD-WRT config... You must enable this feature if you want to use YAMon!" 2
fi
_local_ip6=${_local_ip6//,/|}
[ "$_includeIPv6" -eq "1" ] && _lan_ip6addr=$(ifconfig $_lan_iface | grep 'inet6 addr:' | grep -Ev "$_local_ip6" | tr -s ' ' | cut -d' ' -f4 | cut -d/ -f1)
}
setConfigJS()
{
$send2log "setConfigJS" 0
if [ "$_symlink2data" -eq "0" ] ; then
local configjs="$_wwwPath/$_wwwJS/$_configWWW"
else
local configjs="${d_baseDir}/$_webDir/$_wwwJS/$_configWWW"
fi
configjs=${configjs//\/\//\/}
local processors=$(cat /proc/cpuinfo | grep -iE "^processor\s:\s[0-9]{1,}$" -c)
#Check for directories
if [ ! -f "$configjs" ] ; then
$send2log ">>> $_configWWW not found... creating new file: $configjs" 2
touch $configjs
chmod 666 $configjs
fi
local configtxt="var _ispBillingDay=$_ispBillingDay
var _wwwData='$_wwwData'
var _scriptVersion='$_version'
var _file_version='$_file_version'
var _usersFileName='$_usersFileName'
var _usageFileName='$_usageFileName'
var _hourlyFileName='$_hourlyFileName'
var _alertfilename='$_alertfilename'
var _processors='$processors'
var _doLiveUpdates='$_doLiveUpdates'
var _updatefreq='$_updatefreq'"
[ "$_includeIPv6" -eq "1" ] && configtxt="$configtxt
var _includeIPv6='1'"
[ "$_doLiveUpdates" -eq "1" ] && configtxt="$configtxt
var _liveFileName='./$_wwwJS$_liveFileName'
var _doCurrConnections='$_doCurrConnections'"
configtxt="$configtxt
var _monthlyDataCap='$_monthlyDataCap'
var _unlimited_usage='$_unlimited_usage'
var _doLocalFiles='$_doLocalFiles'
var _organizeData='$_organizeData'"
[ "$_unlimited_usage" -eq "1" ] && configtxt="$configtxt
var _unlimited_start='$_unlimited_start'
var _unlimited_end='$_unlimited_end'"
if [ ! "$_settings_pswd" == "" ] ; then
local _md5_pswd=$(echo -n "$_settings_pswd" | md5sum | awk '{print $1}')
configtxt="$configtxt
var _settings_pswd='$_md5_pswd'"
fi
[ ! "$_dbkey" == "" ] && configtxt="$configtxt
var _dbkey='$_dbkey'"
$save2File "$configtxt" "$configjs"
$send2log ">>> configjs --> $configjs" -1
$send2log ">>> configtxt --> $configtxt" -1
}
shutDown(){
#one last backup before shutting down
$send2log "shutDown" 1
updateHourly
updateLastSeen
$send2log "\`yamon.sh\` has been stopped" 2
exit 0
}
changeDates()
{
$send2log ">>> date change: $_pDay --> $_cDay " 1
source "$d_baseDir/includes/hourly2monthly.sh"
updateHourly $_p_hr
updateHourly2Monthly $_cYear $_cMonth $_pDay &
local avrt='n/a'
[ "$_dailyiterations" -gt "0" ] && avrt=$(echo "$_totalDailyRunTime $_dailyiterations" | awk '{printf "%.3f \n", $1/$2}')
$send2log ">>> Daily stats: day-> $_pDay #iterations--> $_dailyiterations total runtime--> $_totalDailyRunTime Ave--> $avrt min-> $_daily_rt_min max--> $_daily_rt_max" 1
_hriterations=0
_dailyiterations=0
_totalhrRunTime=0
_totalDailyRunTime=0
_hr_rt_max=''
_hr_rt_min=''
_daily_rt_max=''
_daily_rt_min=''
[ -n "$(which sort)" ] && sortStr=" | sort -k3"
local yEntry=$(eval "iptables -nL ${YAMON_IP4}Entry -vxZ | tr -s '-' ' ' | cut -d' ' -f2,3,8,9,10 $sortStr")
local yLocal=$(eval "iptables -nL ${YAMON_IP4}Local -vxZ | tr -s '-' ' ' | cut -d' ' -f2,3,8,9,10 $sortStr")
local yall=$(eval "iptables -nL ${YAMON_IP4} -vx | tr -s '-' ' ' | cut -d' ' -f2,3,8,9,10 $sortStr")
$send2log ">>> ${YAMON_IP4}Entry:
$yEntry" 0
$send2log ">>> ${YAMON_IP4}Local:
$yLocal" 0
$send2log ">>> ${YAMON_IP4}:
$yall" 0
[ "$_doDailyBU" -eq "1" ] && dailyBU "$_cYear-$_cMonth-$_pDay" &
sl_max=''
sl_min=''
hr_max5=''
hr_min5=''
hr_max1=''
hr_min1=''
sl_max_ts=''
sl_min_ts=''
ndAMS=0
_totalLostBytes=0
_pndData=""
_cMonth=$(date +%m)
_cYear=$(date +%Y)
_ds="$_cYear-$_cMonth-$_cDay"
if [ "$_unlimited_usage" -eq "1" ] ; then
_ul_start=$(date -d "$_unlimited_start" +%s);
_ul_end=$(date -d "$_unlimited_end" +%s);
[ "$_ul_end" -lt "$_ul_start" ] && _ul_start=$((_ul_start - 86400))
$send2log "_unlimited_usage-->$_unlimited_usage ($_unlimited_start->$_unlimited_end / $_ul_start->$_ul_end)" 0
fi
setLogFile
updateServerStats
setDataDirectories
_pDay="$_cDay"
}
add2UsersJS()
{
$send2log "add2UsersJS: $1 $2 $3 $4 $5" 0
local mac=$1
local ip=$2
local is_ipv6=$3
local oname=''
local dname=''
[ -n "$4" ] && oname="$4"
[ -n "$5" ] && dname="$5"
$send2log "add2UsersJS: $mac $ip $is_ipv6 / $oname / $dname" 1
[ -z "$ip" ] || clearDupIPs
local ds=$(date +"%Y-%m-%d %H:%M:%S")
if [ -z "$dname" ] ; then
local deviceName=$(getDeviceName $mac)
$send2log "deviceName-->$deviceName" -1
if [ -z "$_do_separator" ] ; then
dname="$deviceName"
else
[ -z "$oname" ] && oname=${deviceName%%"$_do_separator"*}
dname=${deviceName#*"$_do_separator"}
fi
fi
#add count of new devices
local inc_count=$(echo "$_currentUsers" | grep -c "$_defaultDeviceName")
inc_count=$(printf %02d $(($inc_count+1)) )
[ -z "$dname" ] || [ "$dname" == '*' ] && dname="$_defaultDeviceName-$inc_count"
[ -z "$oname" ] || [ "$oname" == '*' ] && oname="$_defaultOwner"
if [ "$is_ipv6" -eq '0' ] ; then
local ip_str="\"ip\":\"$ip\","
local ip6_str=""
[ "$_includeIPv6" -eq "1" ] && ip6_str="\"ip6\":\"\","
else
local ip_str="\"ip\":\"\","
local ip6_str="\"ip6\":\"$ip\","
fi
unset IFS
local newuser="ud_a({\"mac\":\"$mac\",$ip_str$ip6_str\"owner\":\"$oname\",\"name\":\"$dname\",\"colour\":\"\",\"added\":\"$ds\",\"updated\":\"$ds\",\"last-seen\":\"$ds\"})"
$send2log "New device $dname (group $oname) was added to the network: $mac & $ip ($is_ipv6)" 99
$send2log "newuser-->$newuser" -1
_changesInUsersJS=$(($_changesInUsersJS + 1))
_currentUsers="$_currentUsers
$newuser"
$send2log "_currentUsers-->$_currentUsers" -1
}
updateinUsersJS()
{
$send2log "updateinUsersJS - $mac / $ip" 0
[ -z "$tip" ] && tip=${ip//\./\\.}
[ -z "$ip" ] || clearDupIPs
[ -z "$ip" ] || mline=$(echo "$mline" | sed -re "s~([\b,\"])$tip([,\b\"])~\1$ip (dup)\2~Ig")
unset IFS
$send2log "mline: $mline
mac: $mac
ip: $ip
tip: $tip
wip: $wip
o_ip: $o_ip" -1
newline=$(replace "$mline" "$wip" "$ip")
newline=$(replace "$newline" "updated" "_updated_")
$send2log "newline: $newline" -1
_currentUsers=$(echo "$_currentUsers" | sed -e "s~$mline~$newline~Ig")
_changesInUsersJS=$(($_changesInUsersJS + 1))
$send2log ">>> Device $mac & $o_ip was updated to $mac & $ip" 1
}
clearDupIPs()
{
$send2log "clearDupIPs: ip: $ip" 1
local tip=${ip//\./\\.}
[ -z "$ip" ] && return
_currentUsers=$(echo "$_currentUsers" | sed -re "s~([\b,\"])$tip([,\b\"])~\1$ip (dup)\2~Ig" | sed -e "s~$tip (dup) (dup)~$ip (dup)~Ig")
}
checkIPs()
{
checkIncompleteMac()
{
local incomplete=$(echo "$iplist" | grep -Ei "00:00:00:00:00:00|failed|incomplete")
[ -z "$incomplete" ] && return
$send2log "checkIncompleteMac: $iplist / $incomplete" 0
local cu_inc=$(echo "$_currentUsers" | grep -i "in:co:mp:le:te" | sort -r)
local cu_not_inc=$(echo "$_currentUsers" | grep -iv "in:co:mp:le:te")
IFS=$'\n'
for line in $incomplete
do
[ -z "$line" ] && continue
local ip=$(echo "$line" | cut -d' ' -f1)
local tip=${ip//\./\\.}
local mac=$(echo "$line" | cut -d' ' -f2)
local o_mac=$mac
$send2log "Incomplete MAC ($mac) for IP: $ip" 0
local uline=$(echo "$cu_not_inc" | grep -i "[\b\",]$tip[,\"]")
#uline gets just matching `active` IPs... not (dup) IPs
if [ -z "$uline" ] ; then #no match
local mline=$(echo "$cu_inc" | grep -Ei "\b$tip\b")
#mline gets just in:co:mp:le:te entries with matching IPs
$send2log "mline: $mline" 0
if [ -z "$mline" ] ; then #no matching inactive IPs... add a new entry
local num_inc=$(echo "$(getField "$cu_inc" "mac")" | cut -d'-' -f2)
[ -z "$num_inc" ] && num_inc=0
num_inc=$(printf %02d $((${num_inc#0}+1)))
mac="in:co:mp:le:te-$num_inc"
$send2log "Changed incomplete MAC for IP: $ip from $o_mac to $mac" 0
add2UsersJS "$mac" "$ip" "$is_ipv6" 'Unknown' "Incomplete-$num_inc"
else
local nm=$(echo "$mline" | wc -l)
if [ "$nm" -eq '1' ] ; then