-
Notifications
You must be signed in to change notification settings - Fork 1
/
qmlog
executable file
·371 lines (326 loc) · 8.61 KB
/
qmlog
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
#!/bin/bash
#
# Copyright (C) 2006-2008 Eric Shubert <[email protected]>
#
# Utility for listing/searching qmail log files
# Original script by Fabio Olaechea
#
# Future Enhancements
# .) find .sed file w/out hard coded path
#
#####################################################################
# Change Log
# 04/05/08 shubes - changed `` to $()
# 10/17/07 shubes - fixed -t option
# 12/17/06 shubes - added sed, grep, date/time parameters
# 11/24/06 shubes - restructured, added numerous capabilities
# 11/21/06 shubes - added -f option, thanks to [email protected]
#####################################################################
#####################################################################
## edit service to be sure it exists
#
a1_edit_parameters(){
if [ -z "$service" ] ; then
q1_usage
elif [ ! -d $logdir/$service ]; then
echo "Invalid service: $service"
q2_services
exit 1
fi
}
#####################################################################
## show log file names
#
a2_list_log_files(){
for file in $(find $logdir/$service -name "*.s"); do
filename=$(basename $file)
tempname=$(echo $filename | tai64nlocal | sed 's/ /_/')
datename=${tempname%\.[0-9]*\.s}
echo "$filename - $datename"
done
}
#####################################################################
## show logs content
#
a3_show_logs_content(){
if [ ! -z "$sedtrim" ]; then
b32_get_sed_script
fi
if [ ! -z "$greppattern" ]; then
grepcmd="| grep $greppattern"
fi
if [ ! -z "$namepattern" ] \
|| [ ! -z "$contpattern" ] \
|| [ ! -z "$dateparm" ]; then
b34_select_files
else
showfiles="$logdir/$service/current"
fi
if [ -z "$command" ]; then
command="cat"
else
command="tail $command"
fi
if [ ! -z "$lesscmd" ] && [ ! -z "$position_less" ]; then
lesscmd="$lesscmd $position_less"
fi
# show the log
#echo "$me: eval $command $showfiles $tai64n $sedtrim $grepcmd $lesscmd"
eval $command $showfiles $tai64n $sedtrim $grepcmd $lesscmd
}
#####################################################################
## get sed commands for trimming the output
#
b32_get_sed_script(){
#sedfile=${mydir%bin}etc/qmlog-trim.sed
sedfile=/opt/qmailtoaster-util/etc/qmlog-trim.sed
if [ -f $sedfile ]; then
while read sedline; do
sedtrim="$sedtrim $sedline;"
done < $sedfile
else
echo "$me $myver notice - sed file $sedfile not found"
fi
sedtrim="$sedtrim $sedparm'"
}
#####################################################################
## select files to show based on name, dates, and/or content pattern
#
b34_select_files(){
if [ -z "$namepattern" ]; then
filepattern="*.s"
else
filepattern="*$namepattern*.s"
fi
if [ ! -z "$dateparm" ]; then
c342_select_date_time_range
elif [ ! -z "$namepattern" ]; then
c344_select_filename_pattern
else
somefiles="$logdir/$service/$filepattern $logdir/$service/current"
fi
if [ ! -z "$contpattern" ]; then
c346_select_content
else
showfiles="$somefiles"
fi
}
#####################################################################
## select files by date, time range
#
c342_select_date_time_range(){
d3422_setup_date_time_fields
for logfile in $(ls $logdir/$service/$filepattern 2>/dev/null) \
$logdir/$service/current; do
d3425_select_each_file
done
if [ -z "$somefiles" ]; then
echo "$me - no $service log found with date/time matching $dateparm"
exit 1
fi
position_less="-p${from_mon}-${from_day}"
}
#####################################################################
## setup date, time fields for file selection
#
d3422_setup_date_time_fields(){
from_datetime=$(echo $dateparm | cut --delimiter=- --fields=1)
thru_datetime=$(echo $dateparm | cut -s --delimiter=- --fields=2)
from_mmdd=$(echo $from_datetime | cut --delimiter=: --fields=1)
from_hhmm=$(echo $from_datetime | cut -s --delimiter=: --fields=2)
thru_mmdd=$(echo $thru_datetime | cut --delimiter=: --fields=1)
thru_hhmm=$(echo $thru_datetime | cut -s --delimiter=: --fields=2)
: ${thru_mmdd:=$from_mmdd}
: ${from_hhmm:=0000}
: ${thru_hhmm:=2359}
from_mon=$(echo $from_mmdd | cut -b 1-2)
from_day=$(echo $from_mmdd | cut -b 3-4)
thru_mon=$(echo $thru_mmdd | cut -b 1-2)
thru_day=$(echo $thru_mmdd | cut -b 3-4)
curryear=$(date +%Y)
currmmdd=$(date +%m%d)
if [ "$from_mmdd" -gt "$currmmdd" ]; then
from_year=$(($curryear - 1))
else
from_year=$curryear
fi
if [ "$thru_mmdd" -gt "$currmmdd" ]; then
thru_year=$(($curryear - 1))
else
thru_year=$curryear
fi
from_date_time="$from_year$from_mon$from_day$from_hhmm"
thru_date_time="$thru_year$thru_mon$thru_day$thru_hhmm"
}
#####################################################################
## select each file by date/time
#
d3425_select_each_file(){
read logdate logtime restoflog <<!
$(head -n1 $logfile | tai64nlocal)
!
begdate=$(echo $logdate | tr -d -)
begtime=$(echo ${logtime%:[0-9][0-9]\.[0-9]*} | tr -d :)
beg_date_time=$begdate$begtime
read logdate logtime restoflog <<!
$(tail -n1 $logfile | tai64nlocal)
!
enddate=$(echo $logdate | tr -d -)
endtime=$(echo ${logtime%:[0-9][0-9]\.[0-9]*} | tr -d :)
end_date_time=$enddate$endtime
if [ "$end_date_time" -lt "$from_date_time" ] \
|| [ "$beg_date_time" -gt "$thru_date_time" ]; then
continue
else
somefiles="$somefiles $logfile"
fi
}
#####################################################################
## select files by filename pattern
#
c344_select_filename_pattern(){
somefiles=$(ls $logdir/$service/$filepattern 2>1)
if [ -z "$somefiles" ]; then
echo "$me - no $service log found with filename pattern $namepattern"
exit 1
fi
}
#####################################################################
## select files by content
#
c346_select_content(){
showfiles=$(grep -lce "$contpattern" $somefiles 2>/dev/null)
if [ -z "$showfiles" ]; then
if [ -z "$namepattern" ] && [ -z "$dateparm" ]; then
withmsg=""
elif [ -z "$namepattern" ]; then
withmsg=" with date/time matching $dateparm"
elif [ -z "$dateparm" ]; then
withmsg=" with filename pattern $namepattern"
else
withmsg=" with date/time matching $dateparm and filename pattern $namepattern"
fi
echo "$me - pattern $contpattern not found in any $service logs $withmsg"
exit 1
fi
if [ -z "$position_less" ]; then
position_less="-p$contpattern"
fi
}
#####################################################################
## show usage output
#
q1_usage(){
echo "$me $myver - show current log of service 'service'"
echo "usage: qmlog service [option] ..."
q2_services
echo "options:"
echo " -h[elp] this help"
echo " -l[ist] list saved logs of service"
echo " -f follow as it grows, using 'tail -f'"
echo " -t N show (tail) last N lines"
echo " -nl show without using less"
echo " -nt show with no trimming"
echo " -d mmdd[:hhmm][-mmdd[:hhmm]]"
echo " show logs that contain the date 'mmdd' [thru -'mmdd']"
echo " -lc regex show logs that contain a string that matches 'regex'"
echo " -ln pattern show logs with file name containing 'pattern'"
echo " -s command pipe output through sed 'command'"
echo " -g regex show only lines that match the string 'regex'"
exit 1
}
#####################################################################
## show services output
#
q2_services()
{
echo -n "services:"
for opt in `ls $logdir`; do
if [ -d "$logdir/$opt" ]; then
echo -n " $opt"
fi
done
echo
}
#####################################################################
## begin main processing here
#
me=$(basename $0)
myver=v0.3
mydir=$(dirname $0)
: ${logdir:=/var/log/qmail}
option=""
command=""
tai64n="| tai64nlocal"
lesscmd="| less"
grepcmd=""
sedtrim="| sed '"
sedparm=""
dateparm=""
namepattern=""
contpattern=""
service=""
showfiles=""
position_less=""
while (( "$#" )); do
case $1 in
"-h" | "-help" | "--help" )
q1_usage
;;
"-l" | "-list" | "--list" )
option="list"
shift
;;
"-f" )
command="$command $1"
lesscmd=""
shift
;;
"-t" )
command="$command -n $2"
shift 2
;;
"-nl" | "-noless" | "--noless" )
lesscmd=""
shift
;;
"-nt" | "-notrim" | "--notrim" )
sedtrim=""
shift
;;
"-d" | "-date" | "-dates" | "--date" | "--dates" )
dateparm=$2
shift 2
;;
"-lc" | "-logcont" | "--logcont" )
contpattern=$2
shift 2
;;
"-ln" | "-logname" | "--logname" )
namepattern=$2
shift 2
;;
"-s" | "-sed" | "--sed" )
sedparm="$sedparm $2;"
shift 2
;;
"-g" | "-grep" | "--grep" )
greppattern=$2
shift 2
;;
* )
service=$1
shift
;;
esac
done
a1_edit_parameters
case $option in
"list" )
a2_list_log_files
;;
* )
a3_show_logs_content
;;
esac
exit 0