Skip to content

Commit fa570e4

Browse files
committed
json listing support
1 parent 4b9cb6e commit fa570e4

File tree

2 files changed

+78
-12
lines changed

2 files changed

+78
-12
lines changed

bin/v-list-web-domain-accesslog

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22
# info: list web domain access log
3-
# options: USER DOMAIN [LINES]
3+
# options: USER DOMAIN [LINES] [FORMAT]
44
#
55
# The function of obtaining raw access web domain logs.
66

@@ -12,33 +12,66 @@
1212
# Argument defenition
1313
user=$1
1414
domain=$2
15-
lines=${3-70}
15+
ttl=${3-70}
16+
format=${4-shell}
1617

1718
# Includes
1819
source $VESTA/func/main.sh
1920
source $VESTA/conf/vesta.conf
2021

2122

23+
# Json function
24+
json_list_log() {
25+
i=1
26+
echo '['
27+
for str in $lines; do
28+
str=$(echo "$str" |sed -e 's/"/\\"/g')
29+
if [ "$i" -lt "$counter" ]; then
30+
echo -e "\t\"$str\","
31+
else
32+
echo -e "\t\"$str\""
33+
fi
34+
(( ++i))
35+
done
36+
echo "]"
37+
}
38+
39+
# Shell function
40+
shell_list_log() {
41+
echo "$lines"
42+
}
43+
2244
#----------------------------------------------------------#
2345
# Verifications #
2446
#----------------------------------------------------------#
2547

26-
check_args '2' "$#" 'USER DOMAIN [FORMAT]'
48+
check_args '2' "$#" 'USER DOMAIN [LINES] [FORMAT]'
49+
validate_format 'user' 'domain' 'ttl'
2750
is_object_valid 'user' 'USER' "$user"
2851
is_object_valid 'web' 'DOMAIN' "$domain"
2952

53+
3054
#----------------------------------------------------------#
3155
# Action #
3256
#----------------------------------------------------------#
3357

3458
# Check number of output lines
35-
if [ "$lines" -gt '3000' ]; then
59+
if [ "$ttl" -gt '3000' ]; then
3660
read_cmd="cat"
3761
else
38-
read_cmd="tail -n $lines"
62+
read_cmd="tail -n $ttl"
3963
fi
4064

41-
$read_cmd /var/log/$WEB_SYSTEM/domains/$domain.log
65+
lines=$($read_cmd /var/log/$WEB_SYSTEM/domains/$domain.log)
66+
counter=$(echo "$lines" |wc -l)
67+
IFS=$'\n'
68+
69+
# Listing logs
70+
case $format in
71+
json) json_list_log ;;
72+
plain) shell_list_log ;;
73+
shell) shell_list_log ;;
74+
esac
4275

4376

4477
#----------------------------------------------------------#

bin/v-list-web-domain-errorlog

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22
# info: list web domain error log
3-
# options: USER DOMAIN [LINES]
3+
# options: USER DOMAIN [LINES] [FORMAT]
44
#
55
# The function of obtaining raw error web domain logs.
66

@@ -12,33 +12,66 @@
1212
# Argument defenition
1313
user=$1
1414
domain=$2
15-
lines=${3-70}
15+
ttl=${3-70}
16+
format=${4-shell}
1617

1718
# Includes
1819
source $VESTA/func/main.sh
1920
source $VESTA/conf/vesta.conf
2021

2122

23+
# Json function
24+
json_list_log() {
25+
i=1
26+
echo '['
27+
for str in $lines; do
28+
str=$(echo "$str" |sed -e 's/"/\\"/g')
29+
if [ "$i" -lt "$counter" ]; then
30+
echo -e "\t\"$str\","
31+
else
32+
echo -e "\t\"$str\""
33+
fi
34+
(( ++i))
35+
done
36+
echo "]"
37+
}
38+
39+
# Shell function
40+
shell_list_log() {
41+
echo "$lines"
42+
}
43+
2244
#----------------------------------------------------------#
2345
# Verifications #
2446
#----------------------------------------------------------#
2547

26-
check_args '2' "$#" 'USER DOMAIN [FORMAT]'
48+
check_args '2' "$#" 'USER DOMAIN [LINES] [FORMAT]'
49+
validate_format 'user' 'domain' 'ttl'
2750
is_object_valid 'user' 'USER' "$user"
2851
is_object_valid 'web' 'DOMAIN' "$domain"
2952

53+
3054
#----------------------------------------------------------#
3155
# Action #
3256
#----------------------------------------------------------#
3357

3458
# Check number of output lines
35-
if [ "$lines" -gt '3000' ]; then
59+
if [ "$ttl" -gt '3000' ]; then
3660
read_cmd="cat"
3761
else
38-
read_cmd="tail -n $lines"
62+
read_cmd="tail -n $ttl"
3963
fi
4064

41-
$read_cmd /var/log/$WEB_SYSTEM/domains/$domain.error.log
65+
lines=$($read_cmd /var/log/$WEB_SYSTEM/domains/$domain.error.log)
66+
counter=$(echo "$lines" |wc -l)
67+
IFS=$'\n'
68+
69+
# Listing logs
70+
case $format in
71+
json) json_list_log ;;
72+
plain) shell_list_log ;;
73+
shell) shell_list_log ;;
74+
esac
4275

4376

4477
#----------------------------------------------------------#

0 commit comments

Comments
 (0)