-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathlog-trimmer.ksh
More file actions
185 lines (155 loc) · 5.11 KB
/
log-trimmer.ksh
File metadata and controls
185 lines (155 loc) · 5.11 KB
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
#!/bin/ksh
########################################################
#
# Shell Script: log-trimmer
#
# 1999, Michael Doran, doran@uta.edu
# University of Texas at Arlington Libraries
#
# Trims log files that tend to outgrow their
# respective file systems.
#
########################################################
# Standard Variables
script_name=$0
sess_file=/tmp/log-trimmer.$$
# Function to add comments to session file
add ()
{
/usr/bin/echo "$1" >> ${sess_file}
}
# Function to mail session file to operator
swoosh ()
{
/usr/bin/mailx -s "$1" operator < ${sess_file}
}
# Give session file an identifying header
add "${script_name}\n `date`\n"
# Show current disk space
add "Starting disk space:\n`df -k`\n"
######################################################
# TrimLog: Function to trim flat-file logs
# (i.e. one line = one entry)
# Requires two arguments:
# $1 - full path name of log file
# $2 - threshold size (in number of lines)
# If the log file exceeds the threshold size, then it
# is trimmed to one-half of the threshold.
TrimLog ()
{
# Check for presence of log file and threshold value
if [ ! -w $1 -o -z $2 ]
then
# append note to session file
add "\n\n *** Missing $1 and/or threshold value ***\n\n"
# mail session file with error
swoosh "ERROR: ${script_name}"
# and exit script
exit 1
fi
# Determine the size of the log file in lines
lines=`wc -l < $1` # Number of lines in log file
keep=`expr $2 / 2` # One half of threshold size
tmp_f=/tmp/TrimLog.tmp
# Include info in session file
add "Threshold size for $1 is $2 lines."
add " Since it was at ${lines} lines,"
if [ ${lines} -gt $2 ] # If filesize is over threshold
then
# skim off most recent records to temp file
/usr/bin/tail -${keep} $1 > ${tmp_f}
# copy them over the original file
/usr/bin/cat ${tmp_f} > $1
# remove the big temp file
/usr/bin/rm ${tmp_f}
# report what was done
add " the log was trimmed to ${keep} lines.\n"
else
# otherwise report no action
add " the log was not trimmed.\n"
fi
return 0
}
# Directory for Apache log files
apache=/opt/apache*/logs
# Show size of apache log files
add "Apache log files:\n`/usr/bin/ls -l ${apache} | grep log`\n"
# Call TrimLog and give filename and threshold size
# Both arguments are REQUIRED
TrimLog ${apache}/access_log 1000000 >> ${sess_file} 2>&1
TrimLog ${apache}/error_log 40000 >> ${sess_file} 2>&1
# Trim the AnswerBook access log
TrimLog /var/log/ab2/logs/access-8888.log 10000 >> ${sess_file} 2>&1
######################################################
# TrimWTMP: function to trim wtmp and wtmpx files
#
# These are data files, not flat files and have to be
# truncated according to their record size.
# see: SunSolve FAQ article 0921
# see: SunSolve Symptoms and Resolutions article 10516
TrimWTMP ()
{
# wtmp files
wtmp=/var/adm/wtmp
tmp_wtmp=/tmp/wtmp
# wtmpx files
wtmpx=/var/adm/wtmpx
tmp_wtmpx=/tmp/wtmpx
# Check for presence of log files
# If they do not exist or are not writable
if [ ! -w ${wtmp} -o ! -w ${wtmpx} ]
then
# append note to session file
add "\n\n *** Missing ${wtmp} and/or ${wtmpx} ***\n\n"
# mail session file with error
swoosh "ERROR: ${script_name}"
# and exit script
exit 1
fi
# Display log sizes
add "Wtmp log files:\n`/usr/bin/ls -l /var/adm | grep wtmp`\n"
# Determine file size (in bytes) by awk'ing from long listing
filesize=`ls -l ${wtmpx} | awk '{print $5}'`
# Divide filesize by bytes-per-record to get number of records
# (wtmp and wtmpx should contain the same number of records
# so this is only done for one of the logs)
# wtmp records are 36 bytes and wtmpx records are 372 bytes
num_recs=`expr ${filesize} / 372`
# Threshold limit in number of records
threshold=20000
# Retain 1/2 of the threshold size
retain=`expr ${threshold} / 2`
# Include info in session file
add "Threshold size for ${wtmp} and ${wtmpx} is ${threshold} records."
add " Since they were at ${num_recs} records,"
# If there are more than the threshold number of records
if [ ${num_recs} -gt ${threshold} ]
then
# determine amount of records to skip
skip=`expr ${num_recs} - ${retain}`
# Make copy of log files to be used as input files for dd
/usr/bin/cp ${wtmp} ${tmp_wtmp}
/usr/bin/cp ${wtmpx} ${tmp_wtmpx}
# Do the truncation
/usr/bin/dd if=${tmp_wtmp} \
of=${wtmp} ibs=36 obs=36 skip=${skip}
/usr/bin/dd if=${tmp_wtmpx} \
of=${wtmpx} ibs=372 obs=372 skip=${skip}
# update session file
add " the logs were trimmed to retain ${retain} records.\n"
add "Ending file size:\n`/usr/bin/ls -l /var/adm | grep wtmp`\n"
# Remove the temporary input files
/usr/bin/rm ${tmp_wtmp}
/usr/bin/rm ${tmp_wtmpx}
else
add " the logs were not trimmed.\n"
fi
return 0
}
TrimWTMP >> ${sess_file} 2>&1
# Show ending disk space
add "Ending disk space:\n`df -k`\n"
add "${script_name} complete.\n`date`"
# Send session file to operator
swoosh "`uname -n` logs trimmed"
exit 0