Skip to content

Instantly share code, notes, and snippets.

@weirdbricks
Created March 13, 2017 02:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weirdbricks/b23d70c9a1e963ce09e2fb9b2f7dee68 to your computer and use it in GitHub Desktop.
Save weirdbricks/b23d70c9a1e963ce09e2fb9b2f7dee68 to your computer and use it in GitHub Desktop.
Nagios script that checks permissions for a file
#!/bin/bash
################################
# Lampros Chaidas #
# 03/12/2017 #
################################
stat=/usr/bin/stat
help="Usage: $0 filename_to_check user group octal_permissions\nExample: $0 /usr/bin/stat root root 755"
# function to check if a file exists
check_if_file_exists () {
ls $1 &> /dev/null
if [ $? -ne 0 ];then
echo "The file $1 does not exist - exiting"
echo -e $help
exit 1
fi
}
check_if_file_exists $stat
# check if any arguments have been passed
if [ -z "$1" ]
then
echo "Please provide the filename you would like me to check"
echo -e $help
exit 1
fi
if [ -z "$2" ]
then
echo "Please provide the user that should own the file"
echo -e $help
exit 1
fi
if [ -z "$3" ]
then
echo "Please provide the group that should own the file"
echo -e $help
exit 1
fi
if [ -z "$4" ]
then
echo "Please provide the octal file permissions for the file"
echo -e $help
exit 1
fi
check_if_file_exists $1
filename=$1
octal_permissions=`$stat -c "%a" $filename`
user=`$stat -c "%U" $filename`
group=`$stat -c "%G" $filename`
if [ ! "$user" == "$2" ];then
echo "CRITICAL - the users do not match - expected: $2, found: $user"
exit 2
fi
if [ ! "$group" == "$3" ];then
echo "CRITICAL - the groups do not match - expected: $3, found: $group"
exit 2
fi
if [ ! "$octal_permissions" == "$4" ];then
echo "CRITICAL - the octal permissions do not match - expected: $4, found: $octal_permissions"
exit 2
fi
echo "OK - all permissions match"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment