-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.sh
executable file
·152 lines (132 loc) · 5.53 KB
/
parser.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
#!/bin/bash
log_folder_path="./logs"
function parse_logs_to_json() {
# Generate output file name
output_file="$log_folder_path/error-log.json"
# Check if output file exists and remove its content if it does
if [ -f "$output_file" ]; then
echo -n > "$output_file"
fi
# Process files and create a temporary file
temp_file=$(mktemp)
echo "[" > "$temp_file"
# Use find to get all non-json files and process them with jq
find "$log_folder_path" -type f ! -name "*.json" | while read -r file; do
# Check if file is empty or contains only whitespace
if [ ! -s "$file" ] || ! grep -q '[^[:space:]]' "$file"; then
continue # Skip empty files or files with only whitespace
fi
# Don't add a comma if the last file didn't have any errors to avoid invalid JSON.
last_char=$(tail -c 2 "$temp_file" | cut -c 1)
if [ "$last_char" != "," ] && [ "$last_char" != "[" ]; then
echo "," >> "$temp_file"
fi
first_entry=true
jq -Rs --arg file "$file" '
{
filename: $file,
plugin: ($file | split("/")[-1]),
content: (
split("\n") |
reduce .[] as $line (
[];
if ($line | test("^\\[[0-9]{2}-[A-Za-z]{3}-[0-9]{4} [0-9]{2}:[0-9]{2}:[0-9]{2} UTC\\]")) then
. + [{
message: "",
level: (
if ($line | test("^\\[[^\\]]+\\]\\s*PHP Fatal error")) then
"FATAL"
elif ($line | test("^\\[[^\\]]+\\]\\s*PHP Warning")) then
"WARNING"
elif ($line | test("^\\[[^\\]]+\\]\\s*PHP Parse error")) then
"PARSE"
elif ($line | test("^\\[[^\\]]+\\]\\s*PHP Notice")) then
"NOTICE"
elif ($line | test("^\\[[^\\]]+\\]\\s*PHP Deprecated")) then
"DEPRECATED"
elif ($line | test("^\\[[^\\]]+\\]\\s*PHP Strict Standards")) then
"STRICT"
elif ($line | test("^\\[[^\\]]+\\]\\s*PHP Recoverable fatal error")) then
"RECOVERABLE_FATAL"
elif ($line | test("^\\[[^\\]]+\\]\\s*WordPress database")) then
"FATAL"
else
"INFO"
end
),
type: (
if ($line | test("^\\[[^\\]]+\\]\\s*WordPress database")) then
"SQL"
elif ($line | test("^\\[[^\\]]+\\]\\s*PHP")) then
"PHP"
else
"OTHER"
end
),
plugin: ($file | split("/")[-1]),
details: $line
}]
elif length > 0 then
.[:-1] + [(last | .details += (if .details == "" then "" else "\n" end) + $line)]
else
.
end
)
)
} | .content | map(
if .details != "" then
.message = (
if (.type == "SQL") then
(.details | gsub("<[^>]*>"; "") | split("Error message was: ")[1] // empty | split("\n\nBacktrace:")[0])
elif (.type == "PHP") then
(.details | sub("^\\[[^\\]]+\\]\\s*PHP [^:]+:\\s*"; "") | split("\n")[0])
else
.details
end
)
else
.
end
)
' "$file" | jq -c '.[]' | while read -r object; do
if [ "$first_entry" = false ]; then
echo "," >> "$temp_file"
elif [ "$first_entry" = true ]; then
first_entry=false
fi
echo "$object" >> "$temp_file"
done
done
last_char=$(tail -c 2 "$temp_file" | cut -c 1)
if [ "$last_char" = "," ]; then
# Remove the last two characters (comma and newline) and add the closing bracket
truncate -s -2 "$temp_file" >> "$temp_file"
fi
echo "]" >> "$temp_file"
# Move the temporary file to the final output file
mv "$temp_file" "$output_file"
echo "JSON output has been saved to $output_file"
}
function parse_json_to_csv() {
# Generate input and output file names
input_file="$log_folder_path/error-log.json"
output_file="$log_folder_path/error-log.csv"
# Check if input file exists
if [ ! -f "$input_file" ]; then
echo "Error: JSON input file '$input_file' not found"
return 1
fi
# Check if output file exists and remove its content if it does
if [ -f "$output_file" ]; then
echo -n > "$output_file"
fi
echo "plugin,message,level,type,details" > "$output_file"
jq -r '
.[] |
[.plugin, .message, .level, .type, .details] |
@csv
' "$input_file" >> "$output_file"
echo "CSV output has been saved to $output_file"
}
parse_logs_to_json
parse_json_to_csv