-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathhetrixtools_agent.sh
More file actions
1678 lines (1535 loc) · 55.2 KB
/
hetrixtools_agent.sh
File metadata and controls
1678 lines (1535 loc) · 55.2 KB
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/bash
#
#
# HetrixTools Server Monitoring Agent
# Copyright 2015 - 2026 @ HetrixTools
# For support, please open a ticket on our website https://hetrixtools.com
#
#
# DISCLAIMER OF WARRANTY
#
# The Software is provided "AS IS" and "WITH ALL FAULTS," without warranty of any kind,
# including without limitation the warranties of merchantability, fitness for a particular purpose and non-infringement.
# HetrixTools makes no warranty that the Software is free of defects or is suitable for any particular purpose.
# In no event shall HetrixTools be responsible for loss or damages arising from the installation or use of the Software,
# including but not limited to any indirect, punitive, special, incidental or consequential damages of any character including,
# without limitation, damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses.
# The entire risk as to the quality and performance of the Software is borne by you, the user.
#
# END OF DISCLAIMER OF WARRANTY
# Set PATH/Locale
export LC_NUMERIC="C"
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
ScriptPath=$(dirname "${BASH_SOURCE[0]}")
# Agent Version (do not change)
Version="2.4.0"
# Load configuration file
if [ -f "$ScriptPath"/hetrixtools.cfg ]
then
. "$ScriptPath"/hetrixtools.cfg
else
exit 1
fi
DisksIgnoreFilter="$IgnoredDisks"
function filterignoreddisks() {
if [ -n "$DisksIgnoreFilter" ]
then
grep -v -E -- "$DisksIgnoreFilter"
else
cat
fi
}
# Script start time
ScriptStartTime=$(date +[%Y-%m-%d\ %T)
function validextendedregex() {
printf '' | grep -E -- "$1" > /dev/null 2>&1
local GrepExitCode=$?
[ "$GrepExitCode" -ne 2 ]
}
if [ -n "$DisksIgnoreFilter" ] && ! validextendedregex "$DisksIgnoreFilter"
then
if [ "$DEBUG" -eq 1 ]
then
echo -e "$ScriptStartTime-$(date +%T]) WARNING: IgnoredDisks contains an invalid regex ($DisksIgnoreFilter), disk filtering disabled" >> "$ScriptPath"/debug.log
fi
DisksIgnoreFilter=""
fi
function regexescape() {
printf '%s' "$1" | sed -e 's/[][(){}.^$*+?|\\]/\\&/g'
}
function serviceprocessrunning() {
local service_regex
service_regex=$(regexescape "$1")
if command -v "pgrep" > /dev/null 2>&1
then
pgrep -x "$1" > /dev/null 2>&1 || pgrep -f "(^|\/)${service_regex}([[:space:]]|$)" > /dev/null 2>&1
else
(( $(ps -eo args= | grep -E "(^|/)${service_regex}([[:space:]]|$)" | grep -v "grep" | wc -l) > 0 ))
fi
}
# Service status function
function servicestatus() {
if command -v "systemctl" > /dev/null 2>&1
then
if systemctl is-active --quiet "$1"
then
echo "1"
return 0
fi
fi
if command -v "service" > /dev/null 2>&1
then
if service "$1" status > /dev/null 2>&1
then
echo "1"
return 0
fi
fi
if serviceprocessrunning "$1"
then
echo "1"
else
echo "0"
fi
}
# Function used to prepare base64 str for url encoding
function base64prep() {
str=$1
str="${str//+/%2B}"
str="${str//\//%2F}"
echo "$str"
}
function timems() {
local TimeNow
TimeNow=$(date +%s%3N 2>/dev/null)
if [[ "$TimeNow" =~ ^[0-9]+$ ]]
then
echo "$TimeNow"
else
echo "$(( $(date +%s) * 1000 ))"
fi
}
function runwithtimeout() {
local TimeoutSeconds=$1
local CaptureOutput=0
local OutputFile=""
local ExitCode=0
shift
RUNWITHTIMEOUT_LAST_OUTPUT=""
if [ "$DEBUG" -eq 1 ]
then
CaptureOutput=1
OutputFile=$(mktemp "${TMPDIR:-/tmp}/hetrixtools_tcp_probe.XXXXXX" 2>/dev/null)
if [ -z "$OutputFile" ]
then
OutputFile="/tmp/hetrixtools_tcp_probe.$$.$RANDOM"
: > "$OutputFile"
fi
fi
if command -v "timeout" > /dev/null 2>&1
then
if [ "$CaptureOutput" -eq 1 ]
then
timeout -s 9 "$TimeoutSeconds" "$@" > "$OutputFile" 2>&1
else
timeout -s 9 "$TimeoutSeconds" "$@" > /dev/null 2>&1
fi
ExitCode=$?
else
if [ "$CaptureOutput" -eq 1 ]
then
"$@" > "$OutputFile" 2>&1 &
else
"$@" > /dev/null 2>&1 &
fi
local CmdPID=$!
local WaitTicks=0
local MaxTicks=$(( TimeoutSeconds * 10 ))
while kill -0 "$CmdPID" > /dev/null 2>&1
do
if [ "$WaitTicks" -ge "$MaxTicks" ]
then
kill -9 "$CmdPID" > /dev/null 2>&1
wait "$CmdPID" > /dev/null 2>&1
ExitCode=124
break
fi
sleep 0.1
WaitTicks=$(( WaitTicks + 1 ))
done
if [ "$ExitCode" -ne 124 ]
then
wait "$CmdPID" > /dev/null 2>&1
ExitCode=$?
fi
fi
if [ "$CaptureOutput" -eq 1 ] && [ -f "$OutputFile" ]
then
RUNWITHTIMEOUT_LAST_OUTPUT=$(cat "$OutputFile")
rm -f "$OutputFile"
fi
return $ExitCode
}
function ncprobeavailable() {
if ! command -v "nc" > /dev/null 2>&1
then
return 1
fi
local NCHelp
NCHelp=$(nc -h 2>&1)
if [ -z "$NCHelp" ]
then
NCHelp=$(nc --help 2>&1)
fi
if echo "$NCHelp" | grep -q -- '-z' && echo "$NCHelp" | grep -q -- '-w'
then
return 0
fi
return 1
}
function devtcpprobeavailable() {
local DevTCPTest
DevTCPTest=$(bash -c 'exec 3<>/dev/tcp/127.0.0.1/1' 2>&1 || true)
if [[ "$DevTCPTest" == *"No such file or directory"* ]]
then
return 1
fi
return 0
}
function gettcpprobemethod() {
if [ -n "$TCPProbeMethod" ]
then
echo "$TCPProbeMethod"
return 0
fi
if ncprobeavailable
then
TCPProbeMethod="nc"
elif devtcpprobeavailable
then
TCPProbeMethod="devtcp"
elif command -v "telnet" > /dev/null 2>&1
then
TCPProbeMethod="telnet"
else
TCPProbeMethod="none"
fi
echo "$TCPProbeMethod"
}
function tcpsamplecount() {
local SampleCount=$(( (OutgoingPingsCount + 4) / 5 ))
if [ "$SampleCount" -lt 2 ]
then
SampleCount=2
fi
if [ "$SampleCount" -gt 8 ]
then
SampleCount=8
fi
echo "$SampleCount"
}
function tcpportprobe() {
local PingTarget=$1
local PingPort=$2
local ProbeTimeout=3
local ProbeMethod
ProbeMethod=$(gettcpprobemethod)
if [ "$ProbeMethod" == "nc" ]
then
TCPProbeLastCommand="nc -z -w $ProbeTimeout $PingTarget $PingPort"
runwithtimeout "$ProbeTimeout" nc -z -w "$ProbeTimeout" "$PingTarget" "$PingPort"
return $?
fi
if [ "$ProbeMethod" == "devtcp" ]
then
TCPProbeLastCommand="bash -c exec 3<>/dev/tcp/$PingTarget/$PingPort"
runwithtimeout "$ProbeTimeout" bash -c "exec 3<>/dev/tcp/$PingTarget/$PingPort"
return $?
fi
if [ "$ProbeMethod" == "telnet" ]
then
TCPProbeLastCommand="bash -c printf 'quit\\n' | telnet $PingTarget $PingPort"
runwithtimeout "$ProbeTimeout" bash -c "printf 'quit\n' | telnet $PingTarget $PingPort"
return $?
fi
TCPProbeLastCommand=""
return 1
}
function icmppingstatus() {
local TargetName=$1
local PingTarget=$2
local PING_OUTPUT
local PACKET_LOSS
local RTT_LINE
local AVG_RTT
PING_OUTPUT=$(ping "$PingTarget" -c "$OutgoingPingsCount" 2>/dev/null)
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T])PING_OUTPUT:\n$PING_OUTPUT" >> "$ScriptPath"/debug.log; fi
PACKET_LOSS=$(echo "$PING_OUTPUT" | grep -o '[0-9]\+% packet loss' | cut -d'%' -f1)
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T])PACKET_LOSS: $PACKET_LOSS" >> "$ScriptPath"/debug.log; fi
if [ -z "$PACKET_LOSS" ]
then
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) Unable to extract packet loss" >> "$ScriptPath"/debug.log; fi
exit 1
fi
RTT_LINE=$(echo "$PING_OUTPUT" | grep 'rtt min/avg/max/mdev')
if [ -n "$RTT_LINE" ]
then
AVG_RTT=$(echo "$RTT_LINE" | awk -F'/' '{print $5}')
AVG_RTT=$(echo | awk "{print $AVG_RTT * 1000}" | awk '{printf "%18.0f",$1}' | xargs)
else
AVG_RTT="0"
fi
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T])AVG_RTT: $AVG_RTT" >> "$ScriptPath"/debug.log; fi
echo "$TargetName,$PingTarget,$PACKET_LOSS,$AVG_RTT;" >> "$ScriptPath"/ping.txt
}
function tcppingstatus() {
local TargetName=$1
local PingTarget=$2
local PingPort=$3
local OutputTarget="${PingTarget}_${PingPort}"
local ProbeMethod
local SampleCount
local SampleIndex=1
local SuccessCount=0
local FailCount=0
local RTTSum=0
local ProbeStart
local ProbeEnd
local ProbeRTT
local ProbeExitCode
local ProbeOutput
local PACKET_LOSS
local AVG_RTT
ProbeMethod=$(gettcpprobemethod)
if [ "$ProbeMethod" == "none" ]
then
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) No TCP probing method available for $PingTarget:$PingPort" >> "$ScriptPath"/debug.log; fi
exit 1
fi
SampleCount=$(tcpsamplecount)
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) TCP probing $PingTarget:$PingPort via $ProbeMethod using $SampleCount samples 5 seconds apart" >> "$ScriptPath"/debug.log; fi
while [ "$SampleIndex" -le "$SampleCount" ]
do
ProbeStart=$(timems)
tcpportprobe "$PingTarget" "$PingPort"
ProbeExitCode=$?
ProbeOutput=$RUNWITHTIMEOUT_LAST_OUTPUT
if [ "$DEBUG" -eq 1 ]
then
if [ -n "$ProbeOutput" ]
then
echo -e "$ScriptStartTime-$(date +%T]) TCP sample $SampleIndex/$SampleCount raw output via $ProbeMethod ($TCPProbeLastCommand):\n$ProbeOutput" >> "$ScriptPath"/debug.log
else
echo -e "$ScriptStartTime-$(date +%T]) TCP sample $SampleIndex/$SampleCount raw output via $ProbeMethod ($TCPProbeLastCommand): <empty>" >> "$ScriptPath"/debug.log
fi
fi
if [ "$ProbeExitCode" -eq 0 ]
then
ProbeEnd=$(timems)
ProbeRTT=$(( ProbeEnd - ProbeStart ))
if [ "$ProbeRTT" -lt 0 ]
then
ProbeRTT=0
fi
RTTSum=$(( RTTSum + ProbeRTT ))
SuccessCount=$(( SuccessCount + 1 ))
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) TCP sample $SampleIndex/$SampleCount success for $PingTarget:$PingPort RTT ${ProbeRTT}ms exit code $ProbeExitCode" >> "$ScriptPath"/debug.log; fi
else
FailCount=$(( FailCount + 1 ))
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) TCP sample $SampleIndex/$SampleCount failed for $PingTarget:$PingPort exit code $ProbeExitCode" >> "$ScriptPath"/debug.log; fi
fi
if [ "$SampleIndex" -lt "$SampleCount" ]
then
sleep 5
fi
SampleIndex=$(( SampleIndex + 1 ))
done
PACKET_LOSS=$(awk -v fail="$FailCount" -v total="$SampleCount" 'BEGIN { printf "%18.0f", (fail * 100) / total }' | xargs)
if [ "$SuccessCount" -gt 0 ]
then
AVG_RTT=$(awk -v sum="$RTTSum" -v success="$SuccessCount" 'BEGIN { printf "%18.0f", sum / success }' | xargs)
else
AVG_RTT="0"
fi
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) TCP packet loss: $PACKET_LOSS AVG_RTT: $AVG_RTT" >> "$ScriptPath"/debug.log; fi
echo "$TargetName,$OutputTarget,$PACKET_LOSS,$AVG_RTT;" >> "$ScriptPath"/ping.txt
}
# Function used to perform outgoing PING tests
function pingstatus() {
local TargetName=$1
local PingTarget=$2
local PingPort=$3
if ! [[ "$TargetName" =~ ^[A-Za-z0-9._-]+$ ]]
then
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) Invalid PING target name value" >> "$ScriptPath"/debug.log; fi
exit 1
fi
if ! [[ "$PingTarget" =~ ^[A-Za-z0-9.:_-]+$ ]]
then
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) Invalid PING target value" >> "$ScriptPath"/debug.log; fi
exit 1
fi
if [ -n "$PingPort" ] && ( ! [[ "$PingPort" =~ ^[0-9]+$ ]] || (( PingPort < 1 || PingPort > 65535 )) )
then
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) Invalid PING port value" >> "$ScriptPath"/debug.log; fi
exit 1
fi
if ! [[ "$OutgoingPingsCount" =~ ^[0-9]+$ ]] || (( OutgoingPingsCount < 10 || OutgoingPingsCount > 40 ))
then
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) Invalid PING count value" >> "$ScriptPath"/debug.log; fi
exit 1
fi
if [ -n "$PingPort" ]
then
tcppingstatus "$TargetName" "$PingTarget" "$PingPort"
else
icmppingstatus "$TargetName" "$PingTarget"
fi
}
# Check if the agent needs to run Outgoing PING tests
if [ "$1" == "ping" ]
then
if [ -n "$4" ]
then
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) Starting TCP PING: $2 ($3:$4) sampled from OutgoingPingsCount=$OutgoingPingsCount" >> "$ScriptPath"/debug.log; fi
else
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) Starting PING: $2 ($3) $OutgoingPingsCount times" >> "$ScriptPath"/debug.log; fi
fi
pingstatus "$2" "$3" "$4"
exit 1
fi
# Clear debug.log every day at midnight
if [ -z "$(date +%H | sed 's/^0*//')" ] && [ -z "$(date +%M | sed 's/^0*//')" ] && [ -f "$ScriptPath"/debug.log ]
then
rm -f "$ScriptPath"/debug.log
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) Cleared debug.log" >> "$ScriptPath"/debug.log; fi
fi
# Start timers
START=$(date +%s)
tTIMEDIFF=0
# Get current minute
M=$(date +%M | sed 's/^0*//')
if [ -z "$M" ]
then
# If minute is empty, set it to 0
M=0
# Clear hetrixtools_cron.log every hour
if [ -f "$ScriptPath"/hetrixtools_cron.log ]
then
rm -f "$ScriptPath"/hetrixtools_cron.log
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) Cleared hetrixtools_cron.log" >> "$ScriptPath"/debug.log; fi
fi
fi
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) Starting HetrixTools Agent v$Version" >> "$ScriptPath"/debug.log; fi
# Kill any lingering agent processes
HTProcesses=$(pgrep -f hetrixtools_agent.sh | wc -l)
if [ -z "$HTProcesses" ]
then
HTProcesses=0
fi
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) Found $HTProcesses agent processes\n$(ps aux | grep 'hetrixtools_agent.sh')" >> "$ScriptPath"/debug.log; fi
if [ "$HTProcesses" -ge 50 ]
then
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) Killing $HTProcesses lingering agent processes" >> "$ScriptPath"/debug.log; fi
pgrep -f hetrixtools_agent.sh | xargs -r kill -9
fi
if [ "$HTProcesses" -ge 10 ]
then
for PID in $(pgrep -f hetrixtools_agent.sh)
do
PID_TIME=$(ps -p "$PID" -oetime= | tr '-' ':' | awk -F: '{total=0; m=1;} {for (i=0; i < NF; i++) {total += $(NF-i)*m; m *= i >= 2 ? 24 : 60 }} {print total}')
if [ -n "$PID_TIME" ] && [ "$PID_TIME" -ge 90 ]
then
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) Killing PID $PID, running for $PID_TIME seconds" >> "$ScriptPath"/debug.log; fi
kill -9 "$PID"
fi
done
fi
# Outgoing PING
if [ -n "$OutgoingPings" ]
then
IFS='|' read -r -a OutgoingPingsArray <<< "$OutgoingPings"
for i in "${OutgoingPingsArray[@]}"
do
IFS=',' read -r -a OutgoingPing <<< "$i"
if [ "${#OutgoingPing[@]}" -eq 2 ] || [ "${#OutgoingPing[@]}" -eq 3 ]
then
bash "$ScriptPath"/hetrixtools_agent.sh ping "${OutgoingPing[0]}" "${OutgoingPing[1]}" "${OutgoingPing[2]}" &
else
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) Invalid OutgoingPings entry: $i" >> "$ScriptPath"/debug.log; fi
fi
done
fi
# Network interfaces
if [ -n "$NetworkInterfaces" ]
then
# Use the network interfaces specified in Settings
IFS=',' read -r -a NetworkInterfacesArray <<< "$NetworkInterfaces"
else
# Automatically detect the network interfaces
NetworkInterfacesArray=()
while IFS='' read -r line; do NetworkInterfacesArray+=("$line"); done < <(ip a | grep BROADCAST | grep 'state UP' | grep -v 'SLAVE' | awk '{print $2}' | awk -F ":" '{print $1}' | awk -F "@" '{print $1}')
fi
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) Network Interfaces: ${NetworkInterfacesArray[*]}" >> "$ScriptPath"/debug.log; fi
# Initial network usage
T=$(cat /proc/net/dev)
declare -A aRX
declare -A aTX
declare -A tRX
declare -A tTX
# Loop through network interfaces
for NIC in "${NetworkInterfacesArray[@]}"
do
aRX[$NIC]=$(echo "$T" | grep -w "$NIC:" | awk '{print $2}')
aTX[$NIC]=$(echo "$T" | grep -w "$NIC:" | awk '{print $10}')
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) Network Interface $NIC RX: ${aRX[$NIC]} TX: ${aTX[$NIC]}" >> "$ScriptPath"/debug.log; fi
done
AutoDetectedPorts=()
if [ -z "${ConnectionPorts// }" ]
then
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) Detecting external connection ports" >> "$ScriptPath"/debug.log; fi
if command -v "ss" > /dev/null 2>&1
then
mapfile -t AutoDetectedPorts < <(ss -Htnl 2>/dev/null | awk '{print $4}' | grep -E ':[0-9]+$' | grep -Ev '^(127\.|::1|\[::1\])' | grep -Ev '\[?fe80:' | sed -E 's/.*:([0-9]+)$/\1/' | grep -E '^[0-9]+$' | sort -n | uniq)
elif command -v "netstat" > /dev/null 2>&1
then
mapfile -t AutoDetectedPorts < <(netstat -tnl 2>/dev/null | awk 'NR>2 {print $4}' | grep -E ':[0-9]+$' | grep -Ev '^(127\.|::1|\[::1\])' | grep -Ev '\[?fe80:' | sed -E 's/.*:([0-9]+)$/\1/' | grep -E '^[0-9]+$' | sort -n | uniq)
fi
if [ ${#AutoDetectedPorts[@]} -gt 30 ]
then
AutoDetectedPorts=("${AutoDetectedPorts[@]:0:30}")
fi
if [ ${#AutoDetectedPorts[@]} -gt 0 ]
then
ConnectionPorts=$(IFS=','; printf '%s' "${AutoDetectedPorts[*]}")
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) Auto detected connection ports: ${AutoDetectedPorts[*]}" >> "$ScriptPath"/debug.log; fi
else
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) No external connection ports detected" >> "$ScriptPath"/debug.log; fi
fi
fi
# Port connections
if [ -n "$ConnectionPorts" ]
then
IFS=',' read -r -a ConnectionPortsArray <<< "$ConnectionPorts"
declare -A Connections
if command -v "ss" > /dev/null 2>&1
then
ConnectionPeers=$(ss -ntu | awk '{print $5}')
elif command -v "netstat" > /dev/null 2>&1
then
ConnectionPeers=$(netstat -ntu | grep "ESTABLISHED" 2>/dev/null | awk 'NR>2 {print $4}')
else
ConnectionPeers=""
fi
for cPort in "${ConnectionPortsArray[@]}"
do
Connections[$cPort]=$(echo "$ConnectionPeers" | grep -c ":$cPort$")
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) Port $cPort Connections: ${Connections[$cPort]}" >> "$ScriptPath"/debug.log; fi
done
fi
# Temperature
declare -A TempArray
declare -A TempArrayCnt
SensorsCmdDisable=0
# Check Services
if [ -n "$CheckServices" ]
then
declare -A SRVCSR
IFS=',' read -r -a CheckServicesArray <<< "$CheckServices"
for i in "${CheckServicesArray[@]}"
do
SRVCSR[$i]=$(( ${SRVCSR[$i]} + $(servicestatus "$i") ))
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) Service $i status: ${SRVCSR[$i]}" >> "$ScriptPath"/debug.log; fi
done
fi
# Disks IOPS
declare -A vDISKs
df_mount_output=$(timeout 3 df 2>/dev/null)
if [ $? -ne 0 ] || [ -z "$df_mount_output" ]
then
df_mount_output=$(timeout 3 df -l 2>/dev/null)
fi
for i in $(echo "$df_mount_output" | filterignoreddisks | awk '$1 ~ /\// {print $(NF)}')
do
vDISKs[$i]=$(lsblk -l | grep -w "$i" | awk '{print $1}')
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) Disk $i: ${vDISKs[$i]}" >> "$ScriptPath"/debug.log; fi
done
declare -A IOPSRead
declare -A IOPSWrite
diskstats=$(cat /proc/diskstats)
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) /proc/diskstats:\n$diskstats" >> "$ScriptPath"/debug.log; fi
lsblk_mnt_option="MOUNTPOINTS"
if ! lsblk -l -o NAME,MOUNTPOINTS >/dev/null 2>&1; then
lsblk_mnt_option="MOUNTPOINT"
fi
DiskstatsSectorSize=512
for i in "${!vDISKs[@]}"
do
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) IOPS Disk $i: ${vDISKs[$i]}" >> "$ScriptPath"/debug.log; fi
IOPSRead[$i]=0
IOPSWrite[$i]=0
if [ ! -z "${vDISKs[$i]}" ]
then
IOPSRead[$i]=$(echo "$diskstats" | grep -w "${vDISKs[$i]}" | awk '{print $6}')
IOPSWrite[$i]=$(echo "$diskstats" | grep -w "${vDISKs[$i]}" | awk '{print $10}')
fi
if [ -z "${IOPSRead[$i]}" ]
then
IOPSRead[$i]=0
fi
if [ -z "${IOPSWrite[$i]}" ]
then
IOPSWrite[$i]=0
fi
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) Disk $i Sector Bytes: $DiskstatsSectorSize Read Sectors: ${IOPSRead[$i]} Write Sectors: ${IOPSWrite[$i]}" >> "$ScriptPath"/debug.log; fi
done
# Zpool IOPS
if [ -x "$(command -v zpool)" ]
then
readarray -t zpoolsray < <(zpool list -H -o name)
if [ ${#zpoolsray[@]} -gt 0 ]
then
current_second=$(date +%S | sed 's/^0*//')
remaining_seconds=$((58 - current_second))
declare -A pipes
declare -A pids
declare -A zpools_mountpoints
for pool in "${zpoolsray[@]}"
do
zpools_mountpoints[$pool]=$(zfs get -H -o value mountpoint "$pool")
pipe=$(mktemp -u)
mkfifo "$pipe"
pipes[$pool]="$pipe"
timeout 60 zpool iostat -v -p "$pool" "$remaining_seconds" 2 | awk 'BEGIN{found=0} /capacity/ {found++} found==2' | grep "$pool" > "$pipe" &
pids[$pool]=$!
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) zpool $pool mounted at ${zpools_mountpoints[$pool]} starting iostat pid ${pids[$pool]} pipe ${pipes[$pool]} for $remaining_seconds seconds" >> "$ScriptPath"/debug.log; fi
done
fi
fi
# Calculate how many how many data sample loops
RunTimes=$(echo | awk "{print 60 / $CollectEveryXSeconds}")
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) Collecting data for $RunTimes loops" >> "$ScriptPath"/debug.log; fi
# Collect data loop
for X in $(seq "$RunTimes")
do
# Get vmstat
VMSTAT=$(vmstat "$CollectEveryXSeconds" 2 | tail -1)
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) $VMSTAT" >> "$ScriptPath"/debug.log; fi
# Read /proc/meminfo
read -r aRAMTotal aRAMFree aRAMAvailable aRAMHasAvailable aRAMBuffRaw aRAMCacheRaw aRAMShmem aRAMSReclaimable aRAMSwapTotal aRAMSwapFree < <(awk '
BEGIN {
memtotal=0; memfree=0; memavailable=0; buffers=0; cached=0;
memavailable_found=0; shmem=0; sreclaimable=0; swaptotal=0; swapfree=0;
}
/^MemTotal:/ { memtotal=$2 }
/^MemFree:/ { memfree=$2 }
/^MemAvailable:/ { memavailable=$2; memavailable_found=1 }
/^Buffers:/ { buffers=$2 }
/^Cached:/ { cached=$2 }
/^Shmem:/ { shmem=$2 }
/^SReclaimable:/ { sreclaimable=$2 }
/^SwapTotal:/ { swaptotal=$2 }
/^SwapFree:/ { swapfree=$2 }
END {
print memtotal, memfree, memavailable, memavailable_found, buffers, cached, shmem, sreclaimable, swaptotal, swapfree
}
' /proc/meminfo)
# CPU usage
CPU=$(echo "$VMSTAT" | awk '{print 100 - $15}')
tCPU=$(echo | awk "{print $tCPU + $CPU}")
# CPU IO wait
CPUwa=$(echo "$VMSTAT" | awk '{print $16}')
tCPUwa=$(echo | awk "{print $tCPUwa + $CPUwa}")
# CPU steal time
CPUst=$(echo "$VMSTAT" | awk '{print $17}')
tCPUst=$(echo | awk "{print $tCPUst + $CPUst}")
# CPU user time
CPUus=$(echo "$VMSTAT" | awk '{print $13}')
tCPUus=$(echo | awk "{print $tCPUus + $CPUus}")
# CPU system time
CPUsy=$(echo "$VMSTAT" | awk '{print $14}')
tCPUsy=$(echo | awk "{print $tCPUsy + $CPUsy}")
# CPU clock
CPUSpeed=$(grep 'cpu MHz' /proc/cpuinfo | awk -F": " '{print $2}' | awk '{printf "%18.0f",$1}' | xargs | sed -e 's/ /+/g')
if [ -z "$CPUSpeed" ]
then
CPUSpeed=0
fi
tCPUSpeed=$(echo | awk "{print $tCPUSpeed + $CPUSpeed}")
# CPU Load
loadavg=$(cat /proc/loadavg)
loadavg1=$(echo "$loadavg" | awk '{print $1}')
tloadavg1=$(echo | awk "{print $tloadavg1 + $loadavg1}")
loadavg5=$(echo "$loadavg" | awk '{print $2}')
tloadavg5=$(echo | awk "{print $tloadavg5 + $loadavg5}")
loadavg15=$(echo "$loadavg" | awk '{print $3}')
tloadavg15=$(echo | awk "{print $tloadavg15 + $loadavg15}")
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) CPU: $CPU IO wait: $CPUwa Steal time: $CPUst User time: $CPUus System time: $CPUsy Load: $loadavg1 $loadavg5 $loadavg15" >> "$ScriptPath"/debug.log; fi
if [ "$aRAMHasAvailable" -eq 1 ]
then
aRAMUsed=$(( aRAMTotal - aRAMAvailable ))
if [ "$aRAMUsed" -lt 0 ]
then
aRAMUsed=0
fi
aRAMCacheReclaimable=$(( aRAMCacheRaw + aRAMSReclaimable - aRAMShmem ))
if [ "$aRAMCacheReclaimable" -lt 0 ]
then
aRAMCacheReclaimable=0
fi
aRAMReclaimableShown=$(( aRAMAvailable - aRAMFree ))
if [ "$aRAMReclaimableShown" -lt 0 ]
then
aRAMReclaimableShown=0
fi
aRAMReclaimableTotal=$(( aRAMBuffRaw + aRAMCacheReclaimable ))
if [ "$aRAMReclaimableShown" -gt "$aRAMReclaimableTotal" ]
then
aRAMReclaimableShown=$aRAMReclaimableTotal
fi
if [ "$aRAMReclaimableTotal" -gt 0 ]
then
aRAMBuffShown=$(( aRAMReclaimableShown * aRAMBuffRaw / aRAMReclaimableTotal ))
aRAMCacheShown=$(( aRAMReclaimableShown - aRAMBuffShown ))
else
aRAMBuffShown=0
aRAMCacheShown=0
fi
else
aRAMUsed=$(( aRAMTotal - aRAMFree - aRAMBuffRaw - aRAMCacheRaw ))
if [ "$aRAMUsed" -lt 0 ]
then
aRAMUsed=0
fi
aRAMBuffShown=$aRAMBuffRaw
aRAMCacheShown=$aRAMCacheRaw
fi
RAM=$(echo | awk "{print $aRAMUsed * 100 / $aRAMTotal}")
tRAM=$(echo | awk "{print $tRAM + $RAM}")
# RAM swap usage
aRAMSwapUsed=$(( aRAMSwapTotal - aRAMSwapFree ))
if [ "$aRAMSwapUsed" -lt 0 ]
then
aRAMSwapUsed=0
fi
if [ "$aRAMSwapTotal" -gt 0 ]
then
RAMSwap=$(echo | awk "{print $aRAMSwapUsed * 100 / $aRAMSwapTotal}")
else
RAMSwap=0
fi
tRAMSwap=$(echo | awk "{print $tRAMSwap + $RAMSwap}")
RAMBuff=$(echo | awk "{print $aRAMBuffShown * 100 / $aRAMTotal}")
tRAMBuff=$(echo | awk "{print $tRAMBuff + $RAMBuff}")
# RAM cache usage
RAMCache=$(echo | awk "{print $aRAMCacheShown * 100 / $aRAMTotal}")
tRAMCache=$(echo | awk "{print $tRAMCache + $RAMCache}")
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) RAM: $RAM Swap: $RAMSwap Buffers: $RAMBuff Cache: $RAMCache" >> "$ScriptPath"/debug.log; fi
# Network usage
T=$(cat /proc/net/dev)
END=$(date +%s)
TIMEDIFF=$(echo | awk "{print $END - $START}")
tTIMEDIFF=$(echo | awk "{print $tTIMEDIFF + $TIMEDIFF}")
START=$(date +%s)
# Loop through network interfaces
for NIC in "${NetworkInterfacesArray[@]}"
do
# Received Traffic
RX=$(echo | awk "{print $(echo "$T" | grep -w "$NIC:" | awk '{print $2}') - ${aRX[$NIC]}}")
RX=$(echo | awk "{print $RX / $TIMEDIFF}")
RX=$(echo "$RX" | awk '{printf "%18.0f",$1}' | xargs)
aRX[$NIC]=$(echo "$T" | grep -w "$NIC:" | awk '{print $2}')
tRX[$NIC]=$(echo | awk "{print ${tRX[$NIC]} + $RX}")
tRX[$NIC]=$(echo "${tRX[$NIC]}" | awk '{printf "%18.0f",$1}' | xargs)
# Transferred Traffic
TX=$(echo | awk "{print $(echo "$T" | grep -w "$NIC:" | awk '{print $10}') - ${aTX[$NIC]}}")
TX=$(echo | awk "{print $TX / $TIMEDIFF}")
TX=$(echo "$TX" | awk '{printf "%18.0f",$1}' | xargs)
aTX[$NIC]=$(echo "$T" | grep -w "$NIC:" | awk '{print $10}')
tTX[$NIC]=$(echo | awk "{print ${tTX[$NIC]} + $TX}")
tTX[$NIC]=$(echo "${tTX[$NIC]}" | awk '{printf "%18.0f",$1}' | xargs)
done
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) Network Traffic: ${tRX[*]} ${tTX[*]}" >> "$ScriptPath"/debug.log; fi
# Port connections
if [ -n "$ConnectionPorts" ]
then
netstat=$(ss -ntu | awk '{print $5}')
for cPort in "${ConnectionPortsArray[@]}"
do
Connections[$cPort]=$(echo | awk "{print ${Connections[$cPort]} + $(echo "$netstat" | grep -c ":$cPort$")}")
done
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) Port Connections: ${Connections[*]}" >> "$ScriptPath"/debug.log; fi
fi
# Temperature (thermal_zone)
if [ "$(find /sys/class/thermal/thermal_zone*/type 2> /dev/null | wc -l)" -gt 0 ]
then
TempArrayIndex=()
TempArrayVal=()
for zone in /sys/class/thermal/thermal_zone*/
do
if [[ -f "${zone}/type" ]] && [[ -f "${zone}/temp" ]]
then
type_value=$(<"${zone}/type")
temp_value=$(<"${zone}/temp")
if [[ -n $type_value ]]
then
TempArrayIndex+=("$type_value")
fi
if [[ $temp_value =~ ^[0-9]+$ ]]
then
TempArrayVal+=("$temp_value")
else
TempArrayVal+=("0")
fi
fi
done
TempNameCnt=0
for TempName in "${TempArrayIndex[@]}"
do
TempArray[$TempName]=${TempArray[$TempName]:-0}
TempArrayCnt[$TempName]=${TempArrayCnt[$TempName]:-0}
if [[ ${TempArrayVal[$TempNameCnt]} =~ ^[0-9]+$ ]]
then
TempArray[$TempName]=$((${TempArray[$TempName]} + ${TempArrayVal[$TempNameCnt]}))
TempArrayCnt[$TempName]=$((TempArrayCnt[$TempName] + 1))
fi
TempNameCnt=$((TempNameCnt + 1))
done
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) Temperature thermal_zone: ${TempArray[*]}" >> "$ScriptPath"/debug.log; fi
fi
# Temperature (sensors)
if command -v "sensors" > /dev/null 2>&1 && [ "$SensorsCmdDisable" -eq 0 ]
then
SensorsCmd=$(LANG=en_US.UTF-8 sensors -A 2>/dev/null)
if [ $? -eq 0 ]
then
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) Sensors command output:\n$SensorsCmd" >> "$ScriptPath"/debug.log; fi
SensorsArray=()
SensorsCoreSum=0
SensorsCoreCnt=0
while IFS='' read -r line; do SensorsArray+=("$line"); done <<< "$SensorsCmd"
for i in "${SensorsArray[@]}"
do
if [ -n "$i" ]
then
if [[ "$i" != *":"* ]] && [[ "$i" != *"="* ]]
then
SensorsCat=$(echo "$i" | xargs)
else
if [[ "$i" == *":"* ]]
then
TempLabel=$(echo "$i" | awk -F":" '{print $1}' | xargs | sed 's/ /_/g')
TempRaw=$(echo "$i" | awk -F":" '{print $2}' | grep -oE '[-+]?[0-9]+(\.[0-9]+)?' | head -n 1)
if [ -n "$TempRaw" ]
then
TempName="$SensorsCat|$TempLabel"
TempVal=$(awk -v val="$TempRaw" 'BEGIN { printf "%18.3f", val }' | sed 's/\.//g' | xargs)
TempArray[$TempName]=${TempArray[$TempName]:-0}
TempArray[$TempName]=$((TempArray[$TempName] + TempVal))
TempArrayCnt[$TempName]=${TempArrayCnt[$TempName]:-0}
TempArrayCnt[$TempName]=$((TempArrayCnt[$TempName] + 1))
if [[ "$TempName" == *"|Core_"* ]]
then
SensorsCoreSum=$((SensorsCoreSum + TempVal))
SensorsCoreCnt=$((SensorsCoreCnt + 1))
fi
fi
fi
fi
fi
done
if [ "$SensorsCoreCnt" -gt 0 ]
then
SensorAvgName="AllCoreAvg"
TempArray[$SensorAvgName]=${TempArray[$SensorAvgName]:-0}
TempArrayCnt[$SensorAvgName]=${TempArrayCnt[$SensorAvgName]:-0}
TempArray[$SensorAvgName]=$((TempArray[$SensorAvgName] + (SensorsCoreSum / SensorsCoreCnt)))
TempArrayCnt[$SensorAvgName]=$((TempArrayCnt[$SensorAvgName] + 1))
fi
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) Temperature sensors: ${TempArray[*]}" >> "$ScriptPath"/debug.log; fi
else
SensorsCmdDisable=1
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) Unable to get temperature via sensors" >> "$ScriptPath"/debug.log; fi
fi
fi
# Check if minute changed, so we can end the loop
MM=$(date +%M | sed 's/^0*//')
if [ -z "$MM" ]
then
MM=0
fi
if [ "$MM" -ne "$M" ]
then
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) Minute changed, ending loop" >> "$ScriptPath"/debug.log; fi
break
fi
done
# Temperature (ipmitool) - just once due to slowness
if command -v "ipmitool" > /dev/null 2>&1
then
IPMIArray=()
while IFS='' read -r line; do IPMIArray+=("$line"); done < <(timeout -s 9 5 ipmitool sdr type Temperature)
for i in "${IPMIArray[@]}"
do
if [ -n "$i" ]
then
if [[ "$i" == *"degrees"* ]]
then
TempName=$(echo "$i" | awk -F"|" '{print $1}' | xargs | sed 's/ /_/g')
TempVal=$(echo "$i" | awk -F"|" '{print $NF}' | awk -F"degrees" '{print $1}' | sed 's/ //g' | awk '{printf "%18.3f",$1}' | sed -e 's/\.//g' | xargs)
TempArray[$TempName]=${TempArray[$TempName]:-0}
TempArray[$TempName]=$((TempArray[$TempName] + TempVal))
TempArrayCnt[$TempName]=${TempArrayCnt[$TempName]:-0}
TempArrayCnt[$TempName]=$((TempArrayCnt[$TempName] + 1))
fi
fi
done
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) Temperature ipmitool: ${TempArray[*]}" >> "$ScriptPath"/debug.log; fi
fi
# Get user running the agent
User=$(whoami)
# Check if system requires reboot
RequiresReboot=0
if [ -f /var/run/reboot-required ]
then
RequiresReboot=1
fi
# Operating System
# Check via lsb_release if possible
if command -v "lsb_release" > /dev/null 2>&1
then
OS=$(lsb_release -s -d)
# Check if it's Debian
elif [ -f /etc/debian_version ]
then
OS="Debian $(cat /etc/debian_version)"
# Check if it's CentOS/Fedora
elif [ -f /etc/redhat-release ]
then
OS=$(cat /etc/redhat-release)
# Check if system is CloudLinux release 8 (CL8 will only output "This system is receiving updates from CloudLinux Network server.")
if [[ "$OS" != "CloudLinux release 8."* ]]
then
# Check if system requires reboot (Only supported in CentOS/RHEL 7 and later, with yum-utils installed)
if timeout -s 9 5 needs-restarting -r | grep -q 'Reboot is required'
then
RequiresReboot=1
fi
fi
# If all else fails
else
OS="$(grep '^PRETTY_NAME=' /etc/os-release | awk -F'"' '{print $2}')" || OS="$(uname -s)" || OS="Linux"
fi
OS=$(echo -ne "$OS" | base64 | tr -d '\n\r\t ')
# Kernel
Kernel=$(uname -r | base64 | tr -d '\n\r\t ')
# Hostname
Hostname=$(uname -n | base64 | tr -d '\n\r\t ')