-
Notifications
You must be signed in to change notification settings - Fork 10
/
diagSinusbot.sh
1736 lines (1514 loc) · 56.4 KB
/
diagSinusbot.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/bash
#
### AUTHOR INFO
# (C) Patrik Kernstock
# Website: pkern.at
#
### SCRIPT INFO
# Version: see in changelog
# Licence: GNU GPL v2
# Description:
# Collects some important diagnostic data about
# the operating system and the bot installation.
# When finished it returns informative information,
# ready to copy and paste it in the right section
# in the sinusbot forum to your forum post.
#
# Important links:
# Development of this script: https://github.com/patschi/sinusbot-tools
# TeamSpeak: https://www.teamspeak.com
# SinusBot forum: https://forum.sinusbot.com
# SinusBot forum thread [english]: https://forum.sinusbot.com/threads/diagsinusbot-sh-sinusbot-diagnostic-script.831/#post-4418
# SinusBot forum thread [german]: https://forum.sinusbot.com/threads/diagsinusbot-sh-sinusbot-diagnostik-script.832/#post-4419
#
### Known issues:
# Mostly this issues are non-critical and just kind of hard to fix or workaround.
# If you have any ideas, feel free to tell me them.
#
# - Getting DISK data on OpenVZ machines and non-english systems may still not work.
#
### THANKS TO...
# all people, who helped developing and testing
# this script in any way. For more information
# see with parameter: '-c' or '--credits'.
#
### USAGE
# To download and execute this script you can use:
# $ cd /path/to/sinusbot/ # usually /opt/ts3bot/
# $ curl -O https://raw.githubusercontent.com/patschi/sinusbot-tools/master/tools/diagSinusbot.sh
# $ bash diagSinusbot.sh
# $ rm diagSinusbot.sh # optionally to cleanup
#
# Simple One-liner:
# $ curl https://raw.githubusercontent.com/patschi/sinusbot-tools/master/tools/diagSinusbot.sh | bash
#
### DISCLAIMER
# No warranty, execute on your own risk.
# No cats were harmed during development.
# May contain traces of eastereggs.
#
##################################################
#### MAGIC HAPPENS BELOW.
#### DO NOT TOUCH ANYTHING BELOW, IF YOU
#### DO NOT KNOW WHAT YOU ARE DOING!
##################################################
### SCRIPT CONFIGURATION VARIABLES
# setting important variables
# NOTICE: Unfortunately this does not when the script file is being piped to bash.
#SCRIPT_FILE_DETAILS="$(awk 'match($0, /v([0-9.]*)\:((\s)*)\[(.*)\]/) { print substr($0, RSTART, RLENGTH) };' "$(basename "$0")" | awk 'END{ print }')"
#SCRIPT_VERSION_NUMBER="$(echo $SCRIPT_FILE_DETAILS | awk -F":" '{ print $1 }')"
#SCRIPT_VERSION_DATE="$(echo $SCRIPT_FILE_DETAILS | awk -F"[" '{ print substr($2, 1, length($2)-1) }')"
# general settings
# SCRIPT
SCRIPT_AUTHOR_NAME="Patrik Kernstock aka. Patschi"
SCRIPT_AUTHOR_WEBSITE="patrik.kernstock.net"
SCRIPT_YEAR="2015-2019"
SCRIPT_NAME="diagSinusbot"
# get version number and date automatically from changelog
SCRIPT_VERSION_NUMBER="0.8.0"
SCRIPT_VERSION_DATE="2019-06-16 00:01 UTC"
VERSION_CHANNEL="master"
SCRIPT_PROJECT_SITE="https://github.com/patschi/sinusbot-tools/tree/$VERSION_CHANNEL"
SCRIPT_PROJECT_DLURL="https://raw.githubusercontent.com/patschi/sinusbot-tools/$VERSION_CHANNEL/tools/diagSinusbot.sh"
SCRIPT_VERSION_FILE="https://raw.githubusercontent.com/patschi/sinusbot-tools/$VERSION_CHANNEL/tools/updates/diagSinusbot/version.txt"
SCRIPT_CHANGELOG_LIST="https://github.com/patschi/sinusbot-tools/tree/master/tools/updates/diagSinusbot"
SCRIPT_CHANGELOG_FILE="https://raw.githubusercontent.com/patschi/sinusbot-tools/$VERSION_CHANNEL/tools/updates/diagSinusbot/changelog-{VER}.txt"
# script COMMANDS dependencies
SCRIPT_REQ_CMDS="apt-get pwd awk wc free grep echo cat date df stat getconf netstat sort head curl date ldd"
# script PACKAGES dependencies
SCRIPT_REQ_PKGS="bc binutils coreutils lsb-release util-linux net-tools curl"
# which domain to check for accessibility
CHECK_WEB_URL="https://www.sinusbot.com/diag"
CHECK_DOMAIN_ACCESS="auto"
# check reachability for update servers
CHECK_UPDATE_SERVERS="https://update01.sinusbot.com/diag https://update02.sinusbot.com/diag https://update03.sinusbot.com/diag"
# BOT
# bot PACKAGES dependencies
BOT_REQ_PACKAGES="x11vnc xvfb libxcursor1 ca-certificates bzip2 libnss3 libegl1-mesa x11-xkb-utils libasound2"
## SCRIPT SETTINGS
# IPv6 HTTPS check, 0 = off, 1 = on
CHECK_CURL_IPV6=0
# NTP server address
NTP_SERVER_ADDR="time.google.com"
# Do not force anything. Let it unchanged! (Trust me I'm an engineer.)
MODE_FORCE=0
## EXECUTION VARIABLES
EXEC_CURL="curl -q --user-agent "$SCRIPT_NAME/v$SCRIPT_VERSION_NUMBER""
### FUNCTIONS
## Function for text output
say()
{
if [ -z "$1" ] && [ -z "$2" ]; then
echo
return
fi
# criteria
local CRIT=$(echo "$1" | tr '[:lower:]' '[:upper:]')
# message
local MSG="$2"
local MSG="$(string_replace "$MSG" "\[b\]" "\x1b[1m")"
local MSG="$(string_replace "$MSG" "\[\/b\]" "\x1b[0m")"
# default prefix
local PREFIX=""
# modes for echo command
local MODES="-e"
if [ "$CRIT" == "WAIT" ] || [ "$CRIT" == "QUESTION" ]; then
local MODES="$MODES -n"
fi
# color for criterias
if [ ! -z "$CRIT" ]; then
# prefix
local PREFIX="[$(date +"%Y-%m-%d %H:%M:%S")] "
case "$CRIT" in
ERROR)
# RED
CRIT="\e[0;31m$CRIT\e[0;37m"
;;
WARNING)
# YELLOW
CRIT="\e[0;33m$CRIT\e[0;37m"
;;
INFO)
# CYAN
CRIT="\e[0;36m$CRIT\e[0;37m"
;;
OKAY|QUESTION|WAIT|WELCOME)
# GREEN
CRIT="\e[0;32m$CRIT\e[0;37m"
;;
DEBUG)
# PURPLE
CRIT="\e[0;35m$CRIT\e[0;37m"
;;
*)
# WHITE
CRIT="\e[0;37m$CRIT"
;;
esac
fi
echo -ne "\e[40m"
# echo message
if [ ! -z "$CRIT" ]; then
# if $CRIT is set...
echo $MODES "$PREFIX[$CRIT] $MSG"
else
# if $CRIT is NOT set...
echo $MODES "$PREFIX$MSG"
fi
echo -ne "\e[0m"
}
## Function for string replacing (Usage: string_replace "string" "pattern" "replacement")
string_replace()
{
# (the missing $ of "$1" is correct!)
echo "${1/$2/$3}"
}
## Function to parse comments from the script file
parse_version_comment_line()
{
echo "$1" | awk 'match($0, /# Version: (.*)/) { print $3 };'
}
## Function to pause till input
pause()
{
say "wait" "Press [ENTER] to continue..."
await_answer
}
## Function for welcome header
show_welcome()
{
# do not forget to manually replace the domain name
# below here with the domain behind $CHECK_DOMAIN_ACCESS!
# AND keep the format that style! (justify the text)
say
say "welcome" "================================================"
say "welcome" "= [b]HELLO![/b] Having an issue? I'm here to assist! ="
say "welcome" "= ="
say "welcome" "= Thanks for using this diagnostic script! ="
say "welcome" "= The more information you provide, the ="
say "welcome" "= better we can help to solve your problem. ="
say "welcome" "= ="
say "welcome" "= The execution may take some moments to ="
say "welcome" "= collection the most important information ="
say "welcome" "= of your system and your bot installation. ="
say "welcome" "= ="
say "welcome" "= After everything is done, you will get a ="
say "welcome" "= summarized output ready to copy & paste it ="
say "welcome" "= within a CODE-tag in the SinusBot forum. ="
say "welcome" "= [Link: https://forum.sinusbot.com] ="
say "welcome" "= ="
say "welcome" "= This script uses DNS resolutions and HTTPS ="
say "welcome" "= requests to Sinusbot-related domains to ="
say "welcome" "= check if your network settings are valid. ="
say "welcome" "================================================"
say "welcome" "= Please also report any feedback or issues ="
say "welcome" "= related to the script in the official forum. ="
say "welcome" "= Thank you and happy debugging! ="
say "welcome" "= -- $SCRIPT_AUTHOR_NAME. ="
say "welcome" "================================================"
say
pause
}
## Function to show help text
show_help()
{
say "info" "Available parameters:"
say "info" " -h|--help This help."
say "info" " -w|--skip-welcome-text Skips the welcome screen."
say "info" " -a|--skip-os-update-check Skips the APT OS updates check."
say "info" " -o|--skip-update-check Skips the script update check."
say "info" " -u|--only-update-check Only check for script update. (overrides update skip)"
say "info" " -m|--skip-wait-messages Skip waiting time after important messages"
say "info" " -c|--credits Show credits."
say "info" " -v|--version Show version."
say "info" " moo The cow says."
say "info" "This tool has Super Cow Powers."
}
## Function to show current version
show_version()
{
say "info" "(C) $SCRIPT_YEAR, $SCRIPT_AUTHOR_NAME ($SCRIPT_AUTHOR_WEBSITE)"
say "info" "$SCRIPT_NAME v$SCRIPT_VERSION_NUMBER [$SCRIPT_VERSION_DATE]"
say "info" "Project site...: $SCRIPT_PROJECT_SITE"
say "info" "Script download: $SCRIPT_PROJECT_DLURL"
}
## Function to show credits (whooaaa!)
show_credits()
{
say "info" ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
say "info" " THANKS TO EVERYONE WHO HAVE HELPED IN ANY WAY!"
say "info" " Special thanks goes to..."
say "info" ""
say "info" " [b]flyth[/b] Michael F. for developing SinusBot, testing this script and ideas"
say "info" " [b]Xuxe[/b] Julian H. for testing, ideas and contributing code"
say "info" " [b]GetMeOutOfHere[/b] - for testing and ideas"
say "info" " [b]JANNIX[/b] Jan H. for testing"
say "info" " [b]maxibanki[/b] Max S. for testing, finding bugs and contributing code"
say "info" " [b]irgendwer[/b] Jonas for testing and ideas"
say "info" " [b]Multivitamin[/b] David for testing and ideas"
say "info" ""
say "info" " ...if u see 'em somewhere, give 'em some chocolate cookieees!"
say "info" "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
}
## Function t000 m0o0o0o0o0o0oo t000d4y
show_moo()
{
cat <<EOF
(__)
(oo)
/------\/
/ | ||
* /\---/\\
~~ ~~
..."Have you mooed today?"...
EOF
}
## Function when something fails
failed()
{
say "error" "Something went wrong!"
say "wait" "Press [ENTER] to exit."
read -p ""
if [ ! -z "$1" ]; then
say "debug" "exit reason code: $1"
fi
exit 1
}
## Function for human output
bytes_format()
{
# This separates the number from the text
local SPACE=" "
# Convert input parameter (number of bytes)
# to Human Readable form
local SLIST="B,KB,MB,GB,TB,PB,EB,ZB,YB"
local POWER=1
local VAL=$(echo "scale=2; $1 * 1024" | bc)
local VINT=$(echo $VAL / 1024 | bc )
while [ $VINT -gt 0 ]; do
let POWER=POWER+1
local VAL=$(echo "scale=2; $VAL / 1024" | bc)
local VINT=$(echo $VAL / 1024 | bc )
done
echo "$VAL$SPACE$(echo $SLIST | cut -f$POWER -d',')"
}
## Function to confirm a command
confirm_package_install()
{
say "question" "Should I install '$1' for you? [y/N] "
if [[ $(await_answer) =~ [yY](es)* ]]; then
INSTALL_CMD="apt-get install -y $1"
say "debug" "Installing package '$1' using '$INSTALL_CMD'..."
sleep 1
eval "$INSTALL_CMD"
if [ $? -ne 0 ]; then
say "error" "Installing package '$1' went wrong! Check and retry."
failed "failed package installation"
else
return 0
fi
else
return 1
fi
}
## Function for checking commands
check_command()
{
if ! which "$1" >/dev/null; then
if [ -z "$2" ]; then
say "error" "Missing command '$1'."
return 1
else
say "error" "Missing command '$1'. Please install package '$2': apt-get install $2"
confirm_package_install "$2"
if [ $? -ne 0 ]; then
return 1
else
return 0
fi
fi
return 1
else
return 0
fi
}
## Function for checking if command exists
is_command_available()
{
if which "$1" >/dev/null; then
return 0
else
return 1
fi
}
## Function to check root privileges
is_user_root()
{
if [ "$(id -u)" -ne 0 ]; then
say "error" "This diagnostic script must be run with root privileges!"
say "info" "[b]Reason[/b]: This script does perform many different checks and some of them"
say "info" "do require root privileges to do so - example: apt-get calls, port checks,"
say "info" "or to even be able to operate, if any permissions are set wrong."
failed "no root privileges"
fi
}
## Function to check if it is a debian-based operating system
is_supported_os()
{
SYS_OS_LSBRELEASE_ID=$(lsb_release --id --short | tr '[:upper:]' '[:lower:]')
SYS_OS_LSBRELEASE_RELEASE=$(lsb_release --release --short | tr '[:upper:]' '[:lower:]')
SYS_OS_LSBRELEASE_RELEASE_MAJOR=$(echo "$SYS_OS_LSBRELEASE_RELEASE" | awk -F'.' '{ print $1 }')
SYS_OS_LSBRELEASE_DESCRIPTION=$(lsb_release --description --short)
# check if operating system supported
if [ "$SYS_OS_LSBRELEASE_ID" != "debian" ] && [ "$SYS_OS_LSBRELEASE_ID" != "ubuntu" ]; then
say "error" "This script is only designed to run on following operating systems: Debian, Ubuntu."
say "error" "IMPORTANT: Running the script with parameter '--force-run|-f' will prevent aborting here. However it is /!\ STRONGLY NOT RECOMMENDED /!\ as it may result in various errors, incorrect output or unexpected errors!"
if [ "$MODE_FORCE" -eq 0 ]; then
failed "unsupported operating system"
fi
fi
say "info" "Detected operating system: $SYS_OS_LSBRELEASE_DESCRIPTION"
# check version of operating system: debian
if [ "$SYS_OS_LSBRELEASE_ID" == "debian" ] && (( $(echo "$SYS_OS_LSBRELEASE_RELEASE_MAJOR <= 6" | bc -l) )); then
# is less or equal 6 = too old.
say "warning" "You are using a too old operating system! Debian Squeeze and before are not officially supported for SinusBot. Please upgrade to a more recent system."
sleep 1
fi
# check version of operating system: ubuntu
if [ "$SYS_OS_LSBRELEASE_ID" == "ubuntu" ] && (( $(echo "$SYS_OS_LSBRELEASE_RELEASE <= 12.04" | bc -l) )); then
# is less or equal 12.04 = too old.
say "warning" "You are using a too old operating system! Ubuntu 12.04 and before are not officially supported for SinusBot. Please upgrade to a more recent system."
sleep 1
fi
}
## Function to crawl given URL
load_webfile()
{
# timeout is 10 seconds, because maybe slower internet connections or slow DNS resolutions.
$EXEC_CURL --fail --insecure --connect-timeout 10 --silent "$1"
}
## Function to check outgoing connections
check_web()
{
local URL=$1
# timeout is 10 seconds, because maybe slower internet connections or slow DNS resolutions.
$EXEC_CURL --head --write-out "%{http_code}" --fail --insecure --silent --connect-timeout 10 -o /dev/null "$URL"
}
## Function to check outgoing IPv4 connections
check_web_ipv4()
{
local URL=$1
# timeout is 10 seconds, because maybe slower internet connections or slow DNS resolutions.
$EXEC_CURL --head --write-out "%{http_code}" --fail --insecure --ipv4 --silent --connect-timeout 10 -o /dev/null "$URL"
}
## Function to check outgoing IPv6 connections
check_web_ipv6()
{
local URL=$1
# timeout is 10 seconds, because maybe slower internet connections or slow DNS resolutions.
$EXEC_CURL --head --write-out "%{http_code}" --fail --insecure --ipv6 --silent --connect-timeout 10 -o /dev/null "$URL"
}
## Function to parse host from a given URL
parse_host_of_url()
{
echo "$1" | awk -F/ '{ print $3 }'
}
## Function to check if a new update is available
script_check_for_update()
{
# Return codes:
# 0 = no update
# 1 = failed retrieving info
# 2 = update available
local UPD_CHECK=$(load_webfile "$SCRIPT_VERSION_FILE")
if [ $? -ne 0 ]; then
return 1
else
local UPD_CHECK_STATUS="$(echo "$UPD_CHECK" | grep -Po '(?<="status": ")[^"]*')"
if [ "$UPD_CHECK_STATUS" != "true" ]; then
return 1
else
UPD_CHECK_VER="$(echo "$UPD_CHECK" | grep -Po '(?<="version": ")[^"]*')"
if compare_version $SCRIPT_VERSION_NUMBER $UPD_CHECK_VER; then
return 2
else
return 0
fi
fi
fi
}
## Function to check if there is a changelog for the given version number
script_check_for_changelog()
{
# Return codes:
# 1 = failed retrieving changelog
# ...else changelog may be returned.
local UPD_CHANGELOG=$(load_webfile "$(script_get_changelog_url "$1")")
if [ $? -ne 0 ]; then
return 1
else
echo "$UPD_CHANGELOG"
fi
}
## Function to get actual changelog url file of the given version number
script_get_changelog_url()
{
string_replace "$SCRIPT_CHANGELOG_FILE" "\{VER\}" "$1"
}
## Function to compare version numbers
compare_version()
{
# Return codes:
# 0 = does not match (means: there is a newer version available)
# 1 = does match (means: no other version available = no update)
if [ "$1" == "$2" ]; then
return 1
else
if [ "$1" == "$(echo -e "$1\n$2" | sort --version-sort --reverse | head -n1)" ]; then
return 1
else
return 0
fi
fi
}
## Function to search bot binary
check_bot_binary()
{
BOT_BINARY=""
local BINARIES="ts3bot sinusbot"
for BINARY in $BINARIES; do
if [ -f "$BOT_PATH/$BINARY" ]; then
BOT_BINARY="$BINARY"
say "debug" "Binary '$BOT_BINARY' found."
fi
done
if [ -z "$BOT_BINARY" ]; then
# empty. not found.
return 1
else
return 0
fi
}
## Function to check if bot config exists
check_bot_config()
{
if [ ! -f "$BOT_PATH/config.ini" ]; then
say "error" "Bot configuration not found!"
failed "bot config not found"
fi
}
## Function to check available updates via apt-get
check_available_updates()
{
apt-get -s dist-upgrade | awk '/^Inst/ { print $2 }' | wc -l
}
## Function to parse bot configuration file
parse_bot_config()
{
echo "$BOT_CONFIG" | grep "$1" | cut -d '=' -f2 | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' | sed -e 's/^[\"]*//' -e 's/[\"]*$//'
}
## Function to get version of SinusBot
get_bot_version()
{
say "debug" "Trying to get SinusBot version using version parameter..." > /proc/${PPID}/fd/0
local BOT_VERSION_CMD=$("$BOT_PATH/$BOT_BINARY" --version 2>/dev/null)
echo "$BOT_VERSION_CMD" | grep -q -P '^flag provided but not defined' >/dev/null
if [ $? -eq 0 ]; then
say "debug" "Error getting SinusBot version. Falling back to other method." > /proc/${PPID}/fd/0
local BOT_VERSION_STRING=$(strings "$BOT_PATH/$BOT_BINARY" | grep "Version:" | cut -d ' ' -f2)
if [ "$BOT_VERSION_STRING" != "" ]; then
echo "$BOT_VERSION_STRING"
else
echo "unknown"
fi
else
local BOT_VERSION_CMD=$(echo -e "$BOT_VERSION_CMD" | egrep "^SinusBot" | awk '{ print $2 }')
echo "$BOT_VERSION_CMD"
fi
}
## Function to get version of the ts3 client
get_ts3_client_version()
{
local TS3_VER=$(awk 'match($0, /Client Release (.*)/) { print $4 };' "$1" | awk 'NR==1')
if [ $? -ne 0 ]; then
TS3_VER="unknown"
fi
echo "$TS3_VER"
}
## Function to check if package is installed
is_os_package_installed()
{
dpkg-query -W -f='${Status}' "$1" 2>&1 | grep -q -P 'install ok installed' 2>&1
if [ $? -eq 0 ]; then
return 0
else
return 1
fi
}
## Function to get the current installed version for a given package
get_installed_version_package()
{
local PKG_VERSION="$(dpkg-query -W -f='${Version}' "$1" 2>&1)"
if [ $? -eq 0 ]; then
echo "$PKG_VERSION"
else
echo "unknown"
fi
}
## Function to check if package is installed
is_os_package_installed_check()
{
if ! is_os_package_installed "$1"; then
say "error" "Missing package '$1'. Please install package '$2': apt-get install $2"
confirm_package_install "$2"
if [ $? -eq 0 ]; then
return 0
else
return 1
fi
fi
}
## Function to get missing packages from a list
get_missing_os_packages()
{
local OS_PACKAGES_MISSING=""
for PACKAGE in $1; do
is_os_package_installed "$PACKAGE"
if [ $? -ne 0 ]; then
local OS_PACKAGES_MISSING="$OS_PACKAGES_MISSING $PACKAGE"
fi
done
trim_spaces "$OS_PACKAGES_MISSING"
}
## Function to get installed scripts
get_installed_bot_scripts()
{
local INSTALLED_SCRIPTS=""
if [ -d "$BOT_PATH/scripts/" ]; then
for SCRIPT_FILE in $BOT_PATH/scripts/*.js; do
if [ "$INSTALLED_SCRIPTS" == "" ]; then
local INSTALLED_SCRIPTS="$(basename "$SCRIPT_FILE")"
else
local INSTALLED_SCRIPTS="$INSTALLED_SCRIPTS; $(basename "$SCRIPT_FILE")"
fi
done
trim_spaces "$INSTALLED_SCRIPTS"
else
say "warning" "Scripts folder not found! (are you using v0.9.9 or prior?)"
fi
}
## Function to trim whitespaces before and after a string
trim_spaces()
{
echo -e "$(echo -e "$@" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
}
## Function to get a md5 hash of a file
get_file_hash()
{
if [ -f "$1" ]; then
md5sum "$1" | awk '{print $1}'
else
echo "unknown"
fi
}
## Function to check if port is in use
port_in_use()
{
PORT=$1
# check if port $1 is in use.
netstat -lnt | awk -v port="$PORT" '$4 ~ "."port' | grep -i 'LISTEN' &>/dev/null
return $?
}
## Function to get user id from a running process id
get_userid_from_pid()
{
grep -r '^Uid:' /proc/$1/status | cut -f2
}
## Function to get username by id (linux os)
get_user_name_by_uid()
{
awk -F: "/:$1:/{print \$1}" /etc/passwd
}
## Function which exites the script with a successful exit code
script_done()
{
exit 0
}
## Function to resolve hostname to IP
resolve_hostname()
{
getent hosts "$1" | head -n 1 | cut -d ' ' -f 1
}
## Function to check DNS resolution
check_dns_resolution()
{
if [ "$(resolve_hostname "$1")" != "" ]; then
return 0
else
return 1
fi
}
## Get time by NTP
get_time_by_ntp()
{
# get current external date by NTP (Credits: http://seriot.ch/ntp.php)
local NTP_TIME="$(echo $((0x`printf c%47s|nc -uw1 $NTP_SERVER_ADDR 123|xxd -s40 -l4 -p`-64#23GDW0)) 2>&1)"
if ! [[ $NTP_TIME =~ ^-?[0-9]+([.][0-9]+)?$ ]] || [ "$NTP_TIME" == "-2208988800" ]; then
NTP_TIME=0
fi
echo $NTP_TIME
}
## Function await answer
await_answer()
{
# workaround with /proc/[...] required only when read command
# is in a function. When not given, the script may not wait
# for an entered answer.
read -p "" prompt < /proc/${PPID}/fd/0
echo "$prompt"
}
## Function to get current locale
get_locale_current()
{
echo $LANG
}
################
## MAIN CODE! ##
################
SCRIPT_PATH=$(pwd)
# PARAMETERS
while [ $# -gt 0 ]; do
case "$1" in
-w|--skip-welcome-text )
NO_WELCOME="yes"
;;
-a|--skip-os-update-check )
NO_OS_UPD_CHECK="yes"
;;
-o|--skip-update-check )
NO_UPD_CHECK="yes"
;;
-u|--only-update-check|--update )
ONLY_SCRIPT_UPDATE_CHECK="yes"
;;
-m|--skip-wait-messages )
SKIP_WAIT_MESSAGES="yes"
;;
-f|--force-run )
MODE_FORCE=1
;;
-h|--help )
show_help
script_done
;;
-c|--credits )
show_credits
script_done
;;
-v|--version )
show_version
script_done
;;
moo )
show_moo
script_done
;;
# unknown parameters
-*|--* )
say "warning" "Unknown parameter: '$1'."
;;
esac
shift
done
# check values.
if [ "$SCRIPT_VERSION_NUMBER" == "" ]; then
say "error" "Important variable empty: SCRIPT_VERSION_NUMBER. Aborting."
failed "Version number incorrectly set."
fi
if [ "$SCRIPT_VERSION_DATE" == "" ]; then
say "error" "Important variable empty: SCRIPT_VERSION_DATE. Aborting."
failed "Version date incorrectly set."
fi
# further checks.
is_user_root
# do not show welcome screen, when user dont want to
if [ "$NO_WELCOME" != "yes" ]; then
show_welcome
fi
# running what...?
say "info" "Starting $SCRIPT_NAME v$SCRIPT_VERSION_NUMBER [$SCRIPT_VERSION_DATE]..."
# Check if we want to automatically set the domain access
if [ "$CHECK_DOMAIN_ACCESS" = "auto" ]; then
CHECK_DOMAIN_ACCESS="$(parse_host_of_url "$CHECK_WEB_URL")"
fi
# checking script dependencies
PACKAGES_MISSING=$(get_missing_os_packages "$SCRIPT_REQ_PKGS")
if [ "$PACKAGES_MISSING" != "" ]; then
say "warning" "Required packages for the script are not installed on this system."
say "info" "Following packages are missing: $PACKAGES_MISSING"
say "question" "Should I install them for you now? [y/N] "
if [[ $(await_answer) =~ [yY](es)* ]]; then
INSTALL_CMD="apt-get install -y $PACKAGES_MISSING"
say "debug" "Installing packages using '$INSTALL_CMD'..."
sleep 1
# initiating installation
eval "$INSTALL_CMD"
# check if everything worked
if [ $? -ne 0 ]; then
say "error" "Installation went wrong! Please install required packages manually!"
failed "failed package installation for script"
else
say "info" "Installation seems to be finished. Please re-run this script now!"
script_done
fi
else
say "warning" "Installation aborted. Please install the packages yourself before re-starting this script."
failed "automated script installation aborted"
fi
fi
# check if commands are available for the script
REQ_CMDS=0
for SMCMD in $SCRIPT_REQ_CMDS; do
check_command "$SMCMD"
if [ $? -ne 0 ]; then
REQ_CMDS=1
fi
done
# check if any commands are missing
if [ $REQ_CMDS -ne 0 ]; then
say "error" "Missing commands... Install and try again please."
failed "missing commands"
fi
# check for script update... maybe there are important changes, or something like that.
# of course skip check, if the user don't want to stay up 2 date.
if [ "$NO_UPD_CHECK" == "yes" ] && [ "$ONLY_SCRIPT_UPDATE_CHECK" != "yes" ]; then
say "warning" "Script update check skipped by user. This is NOT recommended!"
say "warning" "Please at least check manually if there is any new update available."
say "warning" "You are currently using script version: $SCRIPT_VERSION_NUMBER from $SCRIPT_VERSION_DATE."
say "warning" "The latest script can be found at: $SCRIPT_PROJECT_DLURL"
else
DISPLAY_CHANGELOG="yes"
say "info" "Checking for new diagnostic script version..."
script_check_for_update
UPD_CHECK_RETURN=$?
if [ $UPD_CHECK_RETURN -eq 0 ]; then
say "okay" "You are using the latest version."
else
if [ $UPD_CHECK_RETURN -eq 2 ]; then
say "info" "There is a new version available for download! You are using v$SCRIPT_VERSION_NUMBER, but v$UPD_CHECK_VER is already available."
say "info" "It is recommended to update the script to the latest version before continuing executing this diagnostic script."
# does the user want to see the changelog?
say "question" "Do you want to see the changelog of the recent update? [Y/n] "
if [[ $(await_answer) =~ [nN](o)* ]]; then
DISPLAY_CHANGELOG="no"
fi
# load and show it!
if [ "$DISPLAY_CHANGELOG" == "yes" ]; then
# hint for complete changelog.
say "info" "######################################################################################"
say "info" " The complete detailed history of every release you may find under the following URL:"
say "info" " > $SCRIPT_CHANGELOG_LIST"
say "info" "######################################################################################"
# trying to get changelog
say "debug" "Trying to get script update changelog..."
CHANGELOG="$(script_check_for_changelog "$UPD_CHECK_VER")"
if [ "$CHANGELOG" != "" ]; then
say "info" "Displaying CHANGELOG for diagSinusBot v$UPD_CHECK_VER:"
say "info" "######################################################################################"
while IFS= read -r line; do
say "info" " $line"
done <<< "$CHANGELOG"
say "info" "######################################################################################"
else
say "warning" "Failed getting update changelog."
say "debug" "Tried getting changelog from '$(script_get_changelog_url "$UPD_CHECK_VER")'..."
fi
fi
# do u wanna update?
say "question" "Should I automatically update the diagnostic script for you? [Y/n] "
# default is here yes. So if the user says explicity no, we do nothing than throwing out some text.
if [[ $(await_answer) =~ [nN](o)* ]]; then
say "warning" "Automated update skipped by user."
sleep 1
say "warning" "Please at least update the diagnostic script manually before continuing using this script."
say "warning" "You are currently using version: $SCRIPT_VERSION_NUMBER from $SCRIPT_VERSION_DATE, but v$UPD_CHECK_VER is already available."
say "warning" "The latest script can be found at: $SCRIPT_PROJECT_DLURL"
sleep 1
pause
else
say "info" "Downloading new script version..."
say "debug" "Download script URL: '$SCRIPT_PROJECT_DLURL'."
CUR_SCRIPT_PATH="$SCRIPT_PATH/$(basename "$0")"
# check if tmp file of this script does already exist. if so, we delete it.
if [ -f "$CUR_SCRIPT_PATH.tmp" ]; then
rm "$CUR_SCRIPT_PATH.tmp"
fi
# downloading new script...
curl -o "$CUR_SCRIPT_PATH.tmp" "$SCRIPT_PROJECT_DLURL"
if [ $? -ne 0 ]; then
say "error" "Error when downloading the new script! Please investigate issues and try again. Skipping update for now."
say "warning" "Please at least update the diagnostic script manually before continuing using this script."
say "warning" "The latest script can be found at: $SCRIPT_PROJECT_DLURL"
pause
else
if [ ! -f "$CUR_SCRIPT_PATH.tmp" ]; then
say "error" "Strange issue here: Even after successful download according to the exit status, the new script file is missing. Please investigate."
say "warning" "Please at least update the diagnostic script manually before continuing using this script."
say "warning" "The latest script can be found at: $SCRIPT_PROJECT_DLURL"
pause
else
# check syntax of new script (should be enough to detect non-bash script files, when something got wrong when downloading)
bash -n "$CUR_SCRIPT_PATH.tmp" &>/dev/null
if [ $? -ne 0 ]; then
say "error" "Something went wrong while downloading the script. Either the download failed or the syntax of the script is faulty."
say "warning" "Skipping automated update..."
sleep 1
say "warning" "Please at least update the diagnostic script manually before continuing using this script."
say "warning" "You are currently using version: $SCRIPT_VERSION_NUMBER from $SCRIPT_VERSION_DATE, but v$UPD_CHECK_VER is already available."
say "warning" "The latest script can be found at: $SCRIPT_PROJECT_DLURL"
pause
else
# make backup of old script
mv "$CUR_SCRIPT_PATH" "$CUR_SCRIPT_PATH.bak"
if [ $? -ne 0 ] || [ ! -f "$CUR_SCRIPT_PATH.bak" ]; then
say "error" "Strange issue here: Renaming script file to backup file did failed. Skipping update. Please investigate."
say "warning" "Please at least update the diagnostic script manually before continuing using this script."
say "warning" "The latest script can be found at: $SCRIPT_PROJECT_DLURL"
pause
else
# rename temp update script to the filename of before
mv "$CUR_SCRIPT_PATH.tmp" "$CUR_SCRIPT_PATH"
if [ $? -ne 0 ] || [ ! -f "$CUR_SCRIPT_PATH.bak" ]; then
say "error" "Strange issue here: Renaming the new script file to the original file name failed. Skipping update. Please investigate."
say "warning" "Please at least update the diagnostic script manually before continuing using this script."
say "warning" "The latest script can be found at: $SCRIPT_PROJECT_DLURL"