forked from gauchocode/brolit-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaliases.sh
1386 lines (899 loc) · 33.1 KB
/
aliases.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
#!/usr/bin/env bash
#
# Author: BROOBE - A Software Development Agency - https://broobe.com
# Version: 3.1.7
################################################################################
# Server Name
VPSNAME="${HOSTNAME}"
SFOLDER="/root/brolit-shell"
# TODO: workaround
declare -g EXEC_TYPE="alias"
export EXEC_TYPE
BROLIT_CONFIG_PATH="/etc/brolit"
CLF_CONFIG_FILE=~/.cloudflare.conf
if [[ ${SUPPORT_CLOUDFLARE_STATUS} == "enabled" && -f ${CLF_CONFIG_FILE} ]]; then
# shellcheck source=${CLF_CONFIG_FILE}
source "${CLF_CONFIG_FILE}"
fi
DPU_CONFIG_FILE=~/.dropbox_uploader
if [[ ${BACKUP_DROPBOX_STATUS} == "enabled" && -f ${DPU_CONFIG_FILE} ]]; then
# shellcheck source=${DPU_CONFIG_FILE}
source "${DPU_CONFIG_FILE}"
# Dropbox-uploader directory
DPU_F="${SFOLDER}/tools/third-party/dropbox-uploader"
# Dropbox-uploader runner
DROPBOX_UPLOADER="${DPU_F}/dropbox_uploader.sh"
fi
# Version
SCRIPT_VERSION="3.1.7"
ALIASES_VERSION="3.1.7-088"
################################################################################
alias ..="cd .."
alias userlist="cut -d: -f1 /etc/passwd"
alias myip="curl http://ipecho.net/plain; echo"
alias myipv6="$(curl --silent 'https://api64.ipify.org')"
alias ports='netstat -tulanp'
alias path='echo -e ${PATH//:/\\n}'
alias now="echo It\'s now $(date +%T)"
## Colorize the grep command output for ease of use (good for log files)
alias grep='grep --color=auto'
alias lt='ls --human-readable --size -1 -S --classify'
alias lss='du -h --max-depth=1'
alias cpv='rsync -ah --info=progress2'
## Get top process eating memory
alias psmem='ps auxf | sort -nr -k 4'
alias psmem10='ps auxf | sort -nr -k 4 | head -10'
alias psmem20='ps auxf | sort -nr -k 4 | head -20'
## Get top process eating cpu
alias pscpu='ps auxf | sort -nr -k 3'
alias pscpu10='ps auxf | sort -nr -k 3 | head -10'
alias pscpu20='ps auxf | sort -nr -k 3 | head -20'
alias atop='atop -a 1'
## Get cpu info
alias cpuinfo='lscpu'
alias cpucores='grep -c "processor" /proc/cpuinfo'
alias ramamount='grep MemTotal /proc/meminfo | cut -d ":" -f 2'
alias get_script_version='echo $SCRIPT_VERSION'
alias get_aliases_version='echo $ALIASES_VERSION'
################################################################################
function _json_read_field() {
local json_file=$1
local json_field=$2
local json_field_value
json_field_value="$(cat "${json_file}" | jq -r ".${json_field}")"
# Return
echo "${json_field_value}"
}
function _json_write_field() {
local json_file=$1
local json_field=$2
local json_field_value=$3
json_field_value="$(jq ".${json_field} = \"${json_field_value}\"" "${json_file}")" && echo "${json_field_value}" >"${json_file}"
exitstatus=$?
if [[ "${exitstatus}" -eq 0 ]]; then
return 0
else
log_event "error" "Getting value from ${json_field}" "false"
return 1
fi
}
function _jsonify_output() {
local mode=$1
# Remove fir parameter
shift
# Mode "key-value" example:
# > echo "key1 value1 key2 value2"
# {'key1': value1, 'key2': value2}
if [[ ${mode} == "key-value" ]]; then
arr=()
while [ $# -ge 1 ]; do
arr=("${arr[@]}" "${1}")
shift
done
vars=(${arr[@]})
len=${#arr[@]}
printf "{"
for ((i = 0; i < len; i += 2)); do
printf "\"${vars[i]}\": \"${vars[i + 1]}\""
if [ $i -lt $((len - 2)) ]; then
printf ", "
fi
done
printf "}"
echo
else
# Mode "value-list" example:
# > echo "value1 value2 value3 value4"
# [ "value1" "value2" "value3" "value4" ]
arr=()
while [ $# -ge 1 ]; do
arr=("${arr[@]}" "${1}")
shift
done
vars=(${arr[@]})
len=${#arr[@]}
printf "["
for ((i = 0; i < len; i += 1)); do
printf "\"${vars[i]}\""
if [ $i -lt $((len - 1)) ]; then
printf ", "
fi
done
printf "]"
echo
fi
}
function _string_remove_spaces() {
# Parameters
# $1 = ${string}
local string=$1
# Return
echo "${string//[[:blank:]]/}"
}
function _cloudflare_get_zone_id() {
# $1 = ${zone_name}
local zone_name=$1
local zone_id
# Using globals: ${SUPPORT_CLOUDFLARE_EMAIL} and ${SUPPORT_CLOUDFLARE_API_KEY}
# Get Zone ID
zone_id="$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=${zone_name}" \
-H "X-Auth-Email: ${SUPPORT_CLOUDFLARE_EMAIL}" \
-H "X-Auth-Key: ${SUPPORT_CLOUDFLARE_API_KEY}" \
-H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]*' | head -1)"
exitstatus=$?
if [[ ${exitstatus} -eq 0 && ${zone_id} != "" ]]; then
# Return
echo "${zone_id}"
else
# Return
echo "Domain ${zone_name} not found"
return 1
fi
}
function _is_pkg_installed() {
# $1 = ${package}
local package=$1
if [ "$(dpkg-query -W -f='${Status}' "${package}" 2>/dev/null | grep -c "ok installed")" == "1" ]; then
# Return
echo "true"
else
# Return
echo "false"
fi
}
function _php_check_installed_version() {
local php_fpm_installed_pkg
local php_installed_versions
# Installed versions
php_fpm_installed_pkg="$(sudo dpkg --list | grep -oh 'php[0-9]\.[0-9]\-fpm')"
# Grep -oh parameters explanation:
#
# -h, --no-filename
# Suppress the prefixing of file names on output. This is the default
# when there is only one file (or only standard input) to search.
# -o, --only-matching
# Print only the matched (non-empty) parts of a matching line,
# with each such part on a separate output line.
#
# In this case, output example: php7.2-fpm php7.3-fpm php7.4-fpm
# Extract only version numbers
php_installed_versions="$(echo -n "${php_fpm_installed_pkg}" | grep -Eo '[+-]?[0-9]+([.][0-9]+)?')"
# The "tr '\n' ' '" part, will replace /n with space
# Return example: 7.4 7.2 7.0
# Check elements number on string
#count_elements="$(echo "${php_installed_versions}" | wc -w)"
#if [[ $count_elements != "0" ]]; then
#
# # Remove last space
# php_installed_versions="$(_string_remove_spaces "${php_installed_versions}")"
#
#fi
for php_v in ${php_installed_versions}; do
# Default versions
php_default_version="$(php -v | grep -Eo 'PHP [0-9.].[0-9.]' | cut -d " " -f 2)"
if [[ ${php_default_version} == "${php_v}" ]]; then
php_default=true
else
php_default=false
fi
phpv_data="{\"name\":\"php\",\"version\":\"${php_v}\",\"default\":\"${php_default}\"}"
all_php_data="${all_php_data} , ${phpv_data}"
done
# Remove 3 fist chars
all_php_data="${all_php_data:3}"
# Return
echo "${all_php_data}"
}
function _mysql_check_installed_version() {
local mysql_installed_pkg
local mysql_installed_version
# MySQL or MariaDB?
mysql_installed_pkg="$(sudo dpkg --list | grep -Eo 'mysql-server-[0-9]+([.][0-9]+)?')"
if [[ ${mysql_installed_pkg} != "" ]]; then
# Extract only version numbers
mysql_installed_version="$(mysql -V | awk -F' ' '{print $3}' | grep -o '[0-9.]*$' | tr '\n' ' ')"
else
mysql_installed_pkg="$(sudo dpkg --list | grep -Eo 'mariadb-server-[0-9]+([.][0-9]+)?')"
if [[ ${mysql_installed_pkg} != "" ]]; then
# Extract only version numbers
mysql_installed_version="$(mysql -V | grep -Eo '[+-]?[0-9]+([.][0-9]+)+([.][0-9]+)?-MariaDB' | cut -d "-" -f 1)"
fi
fi
# Return
echo "{\"name\":\"${mysql_installed_pkg}\",\"version\":\"${mysql_installed_version}\",\"default\":\"true\"}"
}
function _nginx_check_installed_version() {
local nginx_installed_version
# Check if package is installed
nginx_installed_pkg="$(sudo dpkg --list | grep -Eo 'nginx-core')"
if [[ ${nginx_installed_pkg} != "" ]]; then
# Installed versions
nginxv="$(nginx -v 2>&1)"
nginxv="$(_string_remove_spaces "${nginxv}")"
nginx_installed_version="$(echo "${nginxv}" | cut -d "(" -f 1 | grep -o '[0-9.]*$')"
if [[ ${nginx_installed_version} != "" ]]; then
# Return
echo "{\"name\":\"nginx\",\"version\":\"${nginx_installed_version}\",\"default\":\"true\"} , "
fi
fi
}
function _apache_check_installed_version() {
local apache_installed_version
# Check if package is installed
apache2_installed_pkg="$(sudo dpkg --list | grep -Eo 'apache2-bin')"
if [[ ${apache2_installed_pkg} != "" ]]; then
# Installed versions
apache_installed_version="$(apache2 -v | awk -F' ' '{print $3}' | grep -o '[0-9.]*$' | tr '\n' ' ' | cut -d " " -f 1)"
if [[ ${apache_installed_version} != "" ]]; then
# Return
echo "{\"name\":\"apache2\",\"version\":\"${apache_installed_version}\",\"default\":\"true\"} , "
fi
fi
}
function _get_backup_date() {
local backup_file=$1
local backup_date
backup_date="$(echo "${backup_file}" | grep -Eo '[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}')"
# Return
echo "${backup_date}"
}
function _certbot_certificate_get_valid_days() {
# $1 = domains (domain.com,www.domain.com)
local domain=$1
local cert_days
cert_days_output="$(certbot certificates --domain "${domain}" 2>&1)"
cert_days="$(echo "${cert_days_output}" | grep -Eo 'VALID: [0-9]+[0-9]' | cut -d ' ' -f 2)"
if [[ ${cert_days} == "" ]]; then
# Return
echo "no-cert"
else
# Return
echo "${cert_days}"
fi
}
function _cloudflare_domain_exists() {
# $1 = ${root_domain}
local root_domain=$1
local zone_name
local zone_id
zone_id="$(_cloudflare_get_zone_id "${root_domain}")"
exitstatus=$?
if [[ ${exitstatus} -eq 0 && ${zone_id} != "" ]]; then
# Return
echo "true"
else
# Return
echo "false"
fi
}
function _cloudflare_record_exists() {
# $1 = ${domain}
# $2 = ${zone_id}
local domain=$1
local zone_id=$2
# Only for better readibility
record_name="${domain}"
# Retrieve record_id
record_id="$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/${zone_id}/dns_records?name=${record_name}" -H "X-Auth-Email: ${SUPPORT_CLOUDFLARE_EMAIL}" -H "X-Auth-Key: ${SUPPORT_CLOUDFLARE_API_KEY}" -H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]*')"
exitstatus=$?
if [[ ${record_id} == "" ]]; then
return 1
else
# Clean output
record_id="$(echo "${record_id}" | tr -d '\n')"
# Return
echo "${record_id}"
fi
}
function _get_domain_extension() {
# Parameters
# $1 = ${domain}
local domain=$1
local first_lvl
local next_lvl
local domain_ext
# Get first_lvl domain name
first_lvl="$(cut -d'.' -f1 <<<"${domain}")"
# Extract first_lvl
domain_ext=${domain#"$first_lvl."}
next_lvl="${first_lvl}"
local -i count=0
while ! grep --word-regexp --quiet ".${domain_ext}" "${SFOLDER}/config/domain_extension-list" && [ ! "${domain_ext#"$next_lvl"}" = "" ]; do
# Remove next level domain-name
domain_ext=${domain_ext#"$next_lvl."}
next_lvl="$(cut -d'.' -f1 <<<"${domain_ext}")"
count=("$count"+1)
done
if grep --word-regexp --quiet ".${domain_ext}" "${SFOLDER}/config/domain_extension-list"; then
domain_ext=.${domain_ext}
# Return
echo "${domain_ext}"
else
return 1
fi
}
function _get_subdomain_part() {
# Parameters
# $1 = ${domain}
local domain=$1
local domain_extension
local domain_no_ext
local subdomain_part
# Get Domain Ext
domain_extension="$(_get_domain_extension "${domain}")"
# Check result
domain_extension_output=$?
if [[ ${domain_extension_output} -eq 0 ]]; then
# Remove domain extension
domain_no_ext=${domain%"$domain_extension"}
root_domain=${domain_no_ext##*.}${domain_extension}
if [[ ${root_domain} != "${domain}" ]]; then
subdomain_part=${domain//.$root_domain/}
# Return
echo "${subdomain_part}"
else
# Return
echo ""
fi
else
return 1
fi
}
function _get_root_domain() {
# Parameters
# $1 = ${domain}
local domain=$1
local domain_extension
local domain_no_ext
# Get Domain Ext
domain_extension="$(_get_domain_extension "${domain}")"
# Check result
domain_extension_output=$?
if [[ ${domain_extension_output} -eq 0 ]]; then
# Remove domain extension
domain_no_ext=${domain%"$domain_extension"}
root_domain=${domain_no_ext##*.}${domain_extension}
# Return
echo "${root_domain}"
else
return 1
fi
}
function _extract_domain_extension() {
# Parameters
# $1 = ${domain}
local domain=$1
local domain_extension
local domain_no_ext
domain_extension="$(_get_domain_extension "${domain}")"
domain_extension_output=$?
if [[ ${domain_extension_output} -eq 0 ]]; then
domain_no_ext=${domain%"$domain_extension"}
# Return
echo "${domain_no_ext}"
else
return 1
fi
}
function _project_get_name_from_domain() {
# Parameters
# $1 = ${project_domain}
local project_domain=$1
# Trying to extract project name from domain
root_domain="$(_get_root_domain "${project_domain}")"
possible_project_name="$(_extract_domain_extension "${root_domain}")"
# Replace '-' and '.' chars
possible_name="$(echo "${possible_project_name}" | sed -r 's/[.-]+/_/g')"
# Return
echo "${possible_name}"
}
function _project_get_stage_from_domain() {
local project_domain=$1
local project_stages
local possible_project_stage
project_stages="demo stage test beta dev"
# Trying to extract project state from domain
possible_project_stage="$(_get_subdomain_part "${project_domain}" | cut -d "." -f 1)"
if [[ ${project_stages} != *"${possible_project_stage}"* ]]; then
possible_project_stage="prod"
fi
# Return
echo "${possible_project_stage}"
}
function _project_get_config() {
# $1 = ${project_path}
# $2 = ${config_field}
local project_path=$1
local config_field=$2
local config_value
local project_name
local project_config_file
project_name="$(basename "${project_path}")"
project_config_file="${BROLIT_CONFIG_PATH}/${project_name}_conf.json"
if [[ -e ${project_config_file} ]]; then
config_value="$(cat ${project_config_file} | jq -r ".${config_field}")"
# Return
echo "${config_value}"
else
# Return
echo "false"
fi
}
################################################################################
# Creates an archive (*.tar.gz) from given directory
function maketar() { tar cvzf "${1%%/}.tar.gz" "${1%%/}/"; }
# Create a ZIP archive of a file or folder
function makezip() { zip -r "${1%%/}.zip" "$1"; }
function extract() {
local file_path=$1
local directory_to_extract=$2
local compress_type=$3
# Get filename and file extension
filename=$(basename -- "${file_path}")
#file_extension="${filename##*.}"
filename="${filename%.*}"
# Log
echo "Extracting compressed file: ${file_path}"
if [[ -f "${file_path}" ]]; then
case "${file_path}" in
*.tar.bz2)
if [[ -n "${compress_type}" ]]; then
#tar xp "${file_path}" -C "${directory_to_extract}" --use-compress-program="${compress_type}"
pv --width 70 "${file_path}" | tar xp -C "${directory_to_extract}" --use-compress-program="${compress_type}"
else
#tar xjf "${file_path}" -C "${directory_to_extract}"
pv --width 70 "${file_path}" | tar xp -C "${directory_to_extract}"
fi
;;
*.tar.gz)
#tar -xzvf "${file_path}" -C "${directory_to_extract}"
pv --width 70 "${file_path}" | tar xzvf -C "${directory_to_extract}"
;;
*.bz2)
#bunzip2 "${file_path}" "${directory_to_extract}"
pv --width 70 "${file_path}" | bunzip2 >"${directory_to_extract}/${filename}"
;;
*.rar)
#unrar x "${file_path}" "${directory_to_extract}"
unrar x "${file_path}" "${directory_to_extract}" | pv -l >/dev/null
;;
*.gz)
#gunzip "${file_path}" -C "${directory_to_extract}"
pv --width 70 "${file_path}" | gunzip -C "${directory_to_extract}"
;;
*.tar)
#tar xf "${file_path}"
pv --width 70 "${file_path}" | tar xf
;;
*.tbz2)
#tar xjf "${file_path}" -C "${directory_to_extract}"
pv --width 70 "${file_path}" | tar xjf -C "${directory_to_extract}"
;;
*.tgz)
#tar xzf "${file_path}" -C "${directory_to_extract}"
pv --width 70 "${file_path}" | tar xzf -C "${directory_to_extract}"
;;
*.zip)
#unzip "${file_path}" "${directory}"
unzip -o "${file_path}" -d "${directory_to_extract}" | pv -l >/dev/null
;;
*.Z)
#uncompress "${file_path}" "${directory}"
pv --width 70 "${file_path}" | uncompress "${directory_to_extract}"
;;
*.xz)
#tar xvf "${file_path}" -C "${directory}"
pv --width 70 "${file_path}" | tar xvf -C "${directory_to_extract}"
;;
*)
echo "${file_path} cannot be extracted via extract()"
return 1
;;
esac
else
echo "${file_path} is not a valid file"
return 1
fi
exitstatus=$?
if [[ ${exitstatus} -eq 0 ]]; then
echo "${file_path} extracted in ${directory_to_extract}"
else
echo "Error extracting ${file_path} in ${directory_to_extract}"
return 1
fi
}
# Make dir and cd
function mcd() {
local dir=$1
mkdir -p "${dir}"
cd "${dir}"
}
# Search with grep
function search() {
local path=$1
local string=$2
# grep parameters:
# -r or -R is recursive,
# -n is line number, and
# -w stands for match the whole word.
# -l (lower-case L) can be added to just give the file name of matching files.
grep -rnw "$path" -e "$string"
}
########################## UTILS FOR DEVOPS ###################################
function brolit_ssh_keygen() {
local keydir
keydir=/root/pem
if [[ -f "${keydir}/identity" ]]; then
echo "A sshkey already exists, showing the content:"
else
mkdir "${keydir}"
# Key generation
ssh-keygen -b 2048 -f identity -t rsa -f "${keydir}/identity"
# Copy credentials
cat ${keydir}/identity.pub >>~/.ssh/authorized_keys
fi
# Show identity content
cat ${keydir}/identity
}
function brolit_shell_config() {
# Return JSON part
echo "\"script_version\": \"${SCRIPT_VERSION}\" , \"netdata_url\": \"${NETDATA_SUBDOMAIN}\" , \"mail_notif\": \"${MAIL_NOTIF}\" , \"telegram_notif\": \"${NOTIFICATION_TELEGRAM_STATUS}\" , \"dropbox_enable\": \"${BACKUP_DROPBOX_STATUS}\" , \"cloudflare_enable\": \"${SUPPORT_CLOUDFLARE_STATUS}\" , \"smtp_server\": \"${NOTIFICATION_EMAIL_SMTP_SERVER}\""
}
function serverinfo() {
local distro
local cpu_cores
local ram_amount
local disk_volume
local disk_usage
local public_ip
local inet_ip # configured on network file
public_ip="$(curl --silent https://api.ipify.org)"
inet_ip="$(/sbin/ifconfig eth0 | grep -w "inet" | awk '{print $2}')"
distro="$(lsb_release -d | awk -F"\t" '{print $2}')"
cpu_cores="$(grep -c "processor" /proc/cpuinfo)"
ram_amount="$(grep MemTotal /proc/meminfo | cut -d ":" -f 2)"
ram_amount="$(_string_remove_spaces "${ram_amount}")"
disk_volume="$(df / | grep -Eo '/dev/[^ ]+')"
disk_size="$(df -h | grep -w "${disk_volume}" | awk '{print $2}')"
disk_usage="$(df -h | grep -w "${disk_volume}" | awk '{print $5}')"
if [[ ${public_ip} == "${inet_ip}" ]]; then
# Return JSON part
echo "\"server_name\": \"${VPSNAME}\" , \"distro\": \"${distro}\" , \"cpu_cores\": \"${cpu_cores}\" , \"ram_avail\": \"${ram_amount}\" , \"disk_size\": \"${disk_size}\" , \"disk_usage\": \"${disk_usage}\""
else
# Return JSON part
echo "\"server_name\": \"${VPSNAME}\" , \"floating_ip\": \"${inet_ip}\" , \"distro\": \"${distro}\" , \"cpu_cores\": \"${cpu_cores}\" , \"ram_avail\": \"${ram_amount}\" , \"disk_size\": \"${disk_size}\" , \"disk_usage\": \"${disk_usage}\""
fi
}
function mysql_databases() {
local database
local databases
local all_databases
# Database blacklist
local database_bl="information_schema,performance_schema,mysql,sys,phpmyadmin"
# Run command
all_databases="$(mysql -Bse 'show databases')"
# Check result
mysql_result=$?
if [[ ${mysql_result} -eq 0 && ${all_databases} != "error" ]]; then
for database in ${all_databases}; do
if [[ ${database_bl} != *"${database}"* ]]; then
databases="${databases} , \"${database}\""
fi
done
# Remove 3 fist chars
databases="${databases:3}"
# Return
echo "${databases}"
else
# Log
echo "Something went wrong listing MySQL databases!"
return 1
fi
}
# TODO: add read_site_config on json results
function sites_directories() {
local directories
local all_directories
local site_cert
local directory_bl
local site_size_du
local site_size
# Directory blacklist
local directory_bl="html,phpmyadmin,sql"
# Run command
all_directories="$(ls /var/www)"
for site in ${all_directories}; do
if [[ ${directory_bl} != *"${site}"* ]]; then
# Size
site_size_du="$(du --human-readable --max-depth=0 "/var/www/${site}")"
site_size="$(echo "${site_size_du}" | awk '{print $1;}')"
# Cert
site_cert="$(_certbot_certificate_get_valid_days "${site}")"
# Cloudflare
root_domain="$(_get_root_domain "${site}")"
site_cf="$(_cloudflare_domain_exists "${root_domain}")"
# Json
site_data="{\"name\":\"${site}\" , \"size\":\"${site_size}\" , \"certificate_days_to_expire\":\"${site_cert}\" , \"domain_on_cloudflare\":\"${site_cf}\"}"
directories="${directories} , ${site_data}"
fi
done
if [[ ${directories} != "" ]]; then
# Remove 3 first chars
directories="${directories:3}"
# Return
echo "${directories}"
else
# Return
echo "\"no-sites\""
fi
}
function dropbox_get_site_backups() {