Short background: I need to monitor the permissions on a unix file (a directory) with ZABBIX to see if/when they change. ZABBIX doesn't have any built in like vfs.file.mode[xxxx] for this, so I had to roll my own UserParameter, with a numeric type.
What I do so far, is use ls -l | cut -c 2-10
to get the rwxr-xr-x
part, and then use sed
to convert letters to their "weight", and awk
with substr
to sum it up, to get the numeric 755
or whatever value.
This is currently on Solaris, I don't have GNU coreutils stat
command, and I want it to be portable and efficient, and only using standard unix tools, that are always available. (IMHO, perl is not always available).
My first attempt (example for the root directory):
ls -ld / | \
cut -c 2-10 | \
sed -e 's%-%0%g' -e 's%r%4%g' -e 's%w%2%g' -e 's%x%1%g' | \
awk '{print (100 * ((substr($0,1,1)) + (substr($0,2,1)) + (substr($0,3,1))) + \
(10 * ((substr($0,4,1) + (substr($0,5,1)) + (substr($0,6,1)) ))) + \
( (substr($0,7,1)) + (substr($0,8,1)) + (substr($0,9,1)) ) );}'
As you can see, I don't care about setuid bits or anything other than files, but purist responses are always welcome!
Surely there must be a more elegant solution. Perhaps a standard unix tool that I didn't think of.
I found this place "accidentally" about a week ago, and I really really love it! Amazing to see that much knowledge, skills, and friendliness in one place! This is my first question, so I'm really excited to see if I get any response! :-) Thanks a lot!