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.
This might work for you: