Reputation: 273
This is a snippet from the middle of a bash script we use for monitoring the state of mounts on a server:
OIFS=$IFS
IFS=$'\n'
for mount in $mounts; do
mountcount=$(($mountcount+1))
dev=`echo $mount | awk {'print $1'};`
dir=`echo $mount | awk {'print $2'};`
opts=`echo $mount | awk {'print $4'};`
state=`echo $opts | cut -d ',' -f 1`
if [ "$state" = "ro" ]; then
crit="true"
break
fi
done
IFS=$IFS
$mounts will have content similar to:
rootfs / rootfs rw 0 0
none /sys sysfs rw,nosuid,nodev,noexec,relatime 0 0
none /proc proc rw,nosuid,nodev,noexec,relatime 0 0
none /dev devtmpfs rw,relatime,size=1028136k,nr_inodes=218146,mode=755 0 0
none /dev/pts devpts rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000 0 0
fusectl /sys/fs/fuse/connections fusectl rw,relatime 0 0
/dev/disk/by-uuid/f2337686-ec8d-429a-9002-592c564ddbf3 / ext3 rw,relatime,errors=remount-ro,barrier=0,data=ordered 0 0
none /sys/kernel/debug debugfs rw,relatime 0 0
none /sys/kernel/security securityfs rw,relatime 0 0
none /dev/shm tmpfs rw,nosuid,nodev,relatime 0 0
none /var/run tmpfs rw,nosuid,relatime,mode=755 0 0
none /var/lock tmpfs rw,nosuid,nodev,noexec,relatime 0 0
As you should be able to see, I am parsing to split each line out in to it's components looking for mounts that are mounted read only. Functionally this works absolutely fine, however the problem is we are running this on 100s of servers, and currently it takes over a second (sometimes) to run through the above loop with the above data.
I believe this problem is caused by wait when executing awk
and cut
, as they are external programs, so I was wondering if there's a more efficient way the same function could be achieved. I'm not quite proficient enough in bash to know the internal functions that might be able to help with this, or proficient enough in awk
to do this all as one line.
My feeling is the 3 calls of awk
and 1 call of cut
could all be achieved in 1 line of awk
. Any help greatly appreciated!
EDIT
The variables dev, dir and mountcount are used later in the script for building output.
EDIT
I've altered the script to the following: (All the echo's are in there as a test)
mountcount=0
OIFS=$IFS
IFS=$'\n'
for mount in $mounts; do
mountcount=$(($mountcount+1))
echo $mount
echo $mount | read dev dir fs opts
echo $dev
echo $dir
echo $fs
echo $opts
state=`echo $opts | cut -d ',' -f 1`
if [ "$state" = "ro" ]; then
crit="true"
break
fi
done
IFS=$OIFS
And that gives me the following:
rootfs / rootfs rw 0 0
fusectl /sys/fs/fuse/connections fusectl rw,relatime 0 0
/dev/disk/by-uuid/1be5b3ae-8239-4177-9af6-22ad0afa662a / ext3 rw,relatime,errors=remount-ro,data=ordered 0 0
/dev/disk/by-uuid/1be5b3ae-8239-4177-9af6-22ad0afa662a /dev/.static/dev ext3 rw,relatime,errors=remount-ro,data=ordered 0 0
devpts /dev/pts devpts rw,relatime 0 0
securityfs /sys/kernel/security securityfs rw,relatime 0 0
So read
isn't work quite as expected.
Upvotes: 0
Views: 982
Reputation: 9819
Maybe you abbreviated your script here, and there's more in it than i can see. But why don't you just do
echo "$mounts" | grep -w 'ro'
to get a list of all read-only mounts, or, if you want just the first one,
echo "$mounts" | grep -w 'ro' | head -1
?
You can still process the output of this with awk, but awk will have much less to do so it should run significantly faster.
'ro' together with -w should be unique enough for the task, but you could use egrep with a more elaborate pattern if you get false positives.
Upvotes: 0
Reputation: 143229
maybe something like
read dev dir fs opts <<<"$mount"
for starters?
Or the whole thing looks very much like
read dev dir fs opts <<<"$(grep ' ro,' <<<"$mounts"|head -n 1)"
It will get you the critical line if there is one (maybe more elaborate expression for grep would be nice). No counting, tho in this instance.
P.S. By the last line IFS=$IFS
I believe you meant IFS=$OIFS
.
Upvotes: 0
Reputation: 241968
What about inverting the logic? Run awk
3 times at the beginning and cut
once, store the results to arrays $devs, $dirs, $optses, $states
. Then, in a for ((i=0; i<max; i++))
loop, get ${devs[i]}
and so on and do the job on them.
Upvotes: 0
Reputation: 58473
This might work for you:
OIFS=$IFS; IFS=$'\n'; ma=($mounts); IFS=$OIFS
mountcount=0
for mount in "${ma[@]}"; do
((mountcount++))
fa=($mount)
dev=${fa[0]}
dir=${fa[1]}
opts=${fa[3]}
state=${fa[3]/,*}
if [ "$state" = "ro" ]; then
crit="true"
break
fi
done
Upvotes: 6