summaryrefslogtreecommitdiff
path: root/common/filter
blob: bdd6427052da584284a56357bec8f5723532dede (plain)
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
##/bin/bash
#
# Copyright (c) 2000-2001 Silicon Graphics, Inc.  All Rights Reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it would be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write the Free Software Foundation,
# Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
#
# standard filters
#

# Checks that given_value is in range of correct_value +/- tolerance.
# Tolerance can be an absolute value or a percentage of the correct value
# (see examples with tolerances below).
# Outputs suitable message to stdout if it's not in range.
#
# A verbose option, -v, may be used as the LAST argument
# 
# e.g. 
# foo: 0.0298 = 0.03 +/- 5%
# _within_tolerance "foo" 0.0298 0.03 5%  
# 
# foo: 0.0298 = 0.03 +/- 0.01
# _within_tolerance "foo" 0.0298 0.03 0.01
#
# foo: 0.0298 = 0.03 -0.01 +0.002
# _within_tolerance "foo" 0.0298 0.03 0.01 0.002
#
# foo: verbose output of 0.0298 = 0.03 +/- 5% 
# _within_tolerance "foo" 0.0298 0.03 5% -v 
_within_tolerance()
{
  _name=$1
  _given_val=$2
  _correct_val=$3
  _mintol=$4
  _maxtol=$_mintol
  _verbose=0
  _debug=false

  # maxtol arg is optional
  # verbose arg is optional
  if [ $# -ge 5 ]
  then 
     if [ "$5" = "-v" ]
     then
	_verbose=1
     else
        _maxtol=$5
     fi
  fi
  if [ $# -ge 6 ]
  then
     [ "$6" = "-v" ] && _verbose=1
  fi

  # find min with or without %
  _mintolerance=`echo $_mintol | sed -e 's/%//'` 
  if [ $_mintol = $_mintolerance ]
  then 
      _min=`echo "scale=5; $_correct_val-$_mintolerance" | bc`
  else
      _min=`echo "scale=5; $_correct_val-$_mintolerance*0.01*$_correct_val" | bc`
  fi

  # find max with or without %
  _maxtolerance=`echo $_maxtol | sed -e 's/%//'` 
  if [ $_maxtol = $_maxtolerance ]
  then 
      _max=`echo "scale=5; $_correct_val+$_maxtolerance" | bc`
  else
      _max=`echo "scale=5; $_correct_val+$_maxtolerance*0.01*$_correct_val" | bc`
  fi

  $_debug && echo "min = $_min"
  $_debug && echo "max = $_max"

  cat <<EOF >$tmp.bc.1
scale=5;
if ($_min <= $_given_val) 1;
if ($_min > $_given_val) 0; 
EOF

  cat <<EOF >$tmp.bc.2
scale=5;
if ($_given_val <= $_max) 1;
if ($_given_val > $_max) 0;
EOF

  _above_min=`bc <$tmp.bc.1`
  _below_max=`bc <$tmp.bc.2`

  rm -f $tmp.bc.[12]

  _in_range=`expr $_above_min \& $_below_max` 

  # fix up min, max precision for output
  # can vary for 5.3, 6.2

  # remove any trailing zeroes from min, max if they have fractional parts
  _min=`echo $_min | sed -e '/\./s/0*$//' -e 's/\.$//'`
  _max=`echo $_max | sed -e '/\./s/0*$//' -e 's/\.$//'`

  if [ $_in_range -eq 1 ] 
  then
	[ $_verbose -eq 1 ] && echo $_name is in range
	return 0
  else
	[ $_verbose -eq 1 ] && echo $_name has value of $_given_val
	[ $_verbose -eq 1 ] && echo $_name is NOT in range $_min .. $_max	
	return 1
  fi
}

# ctime(3) dates
#
_filter_date()
{
    sed \
	-e 's/[A-Z][a-z][a-z] [A-z][a-z][a-z]  *[0-9][0-9]* [0-9][0-9]:[0-9][0-9]:[0-9][0-9] [0-9][0-9][0-9][0-9]$/DATE/'
}

# prints filtered output on stdout, values (use eval) on stderr
# 
_filter_mkfs()
{
    set -
    perl -ne '
    if (/^meta-data=([\w,|\/.-]+)\s+isize=(\d+)\s+agcount=(\d+), agsize=(\d+) blks/) {
	print STDERR "ddev=$1\nisize=$2\nagcount=$3\nagsize=$4\n";
	print STDOUT "meta-data=DDEV isize=XXX agcount=N, agsize=XXX blks\n";
    }
    if (/^\s+=\s+sectsz=(\d+)\s+attr=(\d+)/) {
        print STDERR "sectsz=$1\nattr=$2\n";
    }
    if (/^data\s+=\s+bsize=(\d+)\s+blocks=(\d+), imaxpct=(\d+)/) {
	print STDERR "dbsize=$1\ndblocks=$2\nimaxpct=$3\n";
	print STDOUT "data     = bsize=XXX blocks=XXX, imaxpct=PCT\n";
    }
    if (/^\s+=\s+sunit=(\d+)\s+swidth=(\d+) blks/) {
        print STDERR "sunit=$1\nswidth=$2\nunwritten=1\n";
	print STDOUT "         = sunit=XXX swidth=XXX, unwritten=X\n";
    }
    if (/^naming\s+=version\s+(\d+)\s+bsize=(\d+)/) {
	print STDERR "dirversion=$1\ndirbsize=$2\n";
	print STDOUT "naming   =VERN bsize=XXX\n";
    }
    if (/^log\s+=(internal log|[\w|\/.-]+)\s+bsize=(\d+)\s+blocks=(\d+),\s+version=(\d+)/ ||
	/^log\s+=(internal log|[\w|\/.-]+)\s+bsize=(\d+)\s+blocks=(\d+)/) {
	print STDERR "ldev=\"$1\"\nlbsize=$2\nlblocks=$3\nlversion=$4\n";
	print STDOUT "log      =LDEV bsize=XXX blocks=XXX\n";
    }
    if (/^\s+=\s+sectsz=(\d+)\s+sunit=(\d+) blks/) {
	print STDERR "logsectsz=$1\nlogsunit=$2\n\n";
    }
    if (/^realtime\s+=([\w|\/.-]+)\s+extsz=(\d+)\s+blocks=(\d+), rtextents=(\d+)/) {
	print STDERR "rtdev=$1\nrtextsz=$2\nrtblocks=$3\nrtextents=$4\n";
	print STDOUT "realtime =RDEV extsz=XXX blocks=XXX, rtextents=XXX\n";
    }'
}


# prints the bits we care about in growfs
# 
_filter_growfs()
{
    perl -ne '
    if (/^data\s+=\s+bsize=(\d+)\s+blocks=(\d+), imaxpct=(\d+)/) {
        print "xfs_growfs --BlockSize=$1 --Blocks=$2\n";
    }
    elsif (/^data/) {
        print;
    }'
}

_filter_dd()
{
    $AWK_PROG '
        /records in/                { next }
        /records out/               { next }
        /No space left on device/   { print "   !!! disk full (expected)" 
                                      next }
                                    { print "   *** " $0 }
    '
}

common_line_filter()
{
    perl -ne 'if (/.*:(.*)/) {
        if ( "$last_line" ne "$1" ) { print "$_"; $first_match=1; }
        elsif ( $first_match==1 ) { print "*\n"; $first_match=0; }
        $last_line="$1";
    }
    else {
        print $_; $last_line=$_;
    }'
}

_filter_xfs_io()
{
    sed -e "s/[0-9/.]* [GMKiBbytes]*, [0-9]* ops\; [0-9/:. sec]* ([inf0-9/.]* [EPGMKiBbytes]*\/sec and [inf0-9/.]* ops\/sec)/XXX Bytes, X ops\; XX:XX:XX.X (XXX YYY\/sec and XXX ops\/sec)/"
}

_filter_xfs_io_unique()
{
    common_line_filter | _filter_xfs_io
}

_filter_test_dir()
{
	sed -e "s,$TEST_DEV,TEST_DEV,g" -e "s,$TEST_DIR,TEST_DIR,g"
}

_filter_scratch()
{
	sed -e "s,$SCRATCH_DEV,SCRATCH_DEV,g" \
	    -e "s,$SCRATCH_MNT,SCRATCH_MNT,g" \
	    -e "/.use_space/d"
}

# Turn any device in the scratch pool into SCRATCH_DEV
_filter_scratch_pool()
{
	FILTER_STRINGS=`echo $SCRATCH_DEV_POOL | sed -e 's/\s\+/\\\|/g'`
	sed -e "s,$FILTER_STRINGS,SCRATCH_DEV,g"
}

_filter_spaces()
{
       sed -e 's/ [ ]*/ /g'
}

# Account for different "ln" failure messages
_filter_ln()
{
	sed -e "s,\(creating symbolic link .*\) to .*: ,\1," \
	    -e "s,failed to create,creating,"
}

# If given an arg, filter *that* UUID string
# Otherwise look for something that looks like a generic UUID
_filter_uuid()
{
	if [ ! -z $1 ]; then
		UUID=$1
		sed -e "s/\(uuid:\) $UUID/\1 <EXACTUUID>/i"
	else
		sed -e "s/\(uuid:\) *[0-9a-f-][0-9a-f-]*/\1 <UUID>/i"
	fi
}

# Filter out sizes like 6.14MB etc
_filter_size()
{
	sed -e "s/[0-9\.]\+\s\?[b|k|m|g|t][b]\?/<SIZE>/ig"
}

# Convert string read from stdin like 128K to bytes and print it to stdout
_filter_size_to_bytes()
{
	read size
	suffix=${size:${#size}-1}
	mul=1
	case $suffix in
		k|K) mul=1024 ;;
		m|M) mul=$((1024*1024)) ;;
		g|G) mul=$((1024*1024*1024)) ;;
		t|T) mul=$((1024*1024*1024*1024)) ;;
	esac
	echo $((${size:0:${#size}-1}*$mul))
}

# make sure this script returns success
/bin/true