Nagios script that checks permissions for a file
A few days ago I run into a situation where the ownership of a file would change unexpectedly.
Fearing the worse, I looked around for a Nagios check to ensure that I get alerted if that were to happen but I couldn't find one so I wrote my own.
The code is here: https://gist.github.com/weirdbricks/b23d70c9a1e963ce09e2fb9b2f7dee68
It's usage is very simple:
1. Download the script, copy paste it or whatever works for you. On CentOS you most likely want this under /usr/lib64/nagios/plugins/ or /usr/local/bin/.
2. Make it executable:
chmod +x check_permissions.sh
3. Try it out:
./check_permissions.sh /etc/passwd root root 644
OK - all permissions match
4. Try it again, but this time tell it that the expected permissions are 777 ( hur hur hur):
./check_permissions.sh /etc/passwd root root 777
CRITICAL - the octal permissions do not match - expected: 777, found: 644
5. One more time - this time tell it that it belongs to the user goatlord:
./check_permissions.sh /etc/passwd goatlord root 644
CRITICAL - the users do not match - expected: goatlord, found: root
6. Let's check the groups too!
./check_permissions.sh /etc/passwd root goats 644
CRITICAL - the groups do not match - expected: goats, found: root