#!/bin/bash # Status: Requires su password. Should rewrite to do sudo instead. # Also, other errors. Needs cleanup. # --Fred 11/26/2012 function usage() { bn=`basename $0` echo -e "usage: $bn -u -t -o -p .7" echo -e "Options: -u User, -t email to this address, -o suppress output, -p percentage of max files to allow before alerts go out" } to=root #Default to 70 percent thresholdPercent=.7 # Ensure we have some params, if not exit if [ ! $# -gt 0 ]; then usage exit 1 fi # -----------------------------------------------------------------------------------# # Set variables for commandline argument values # -----------------------------------------------------------------------------------# while getopts "u:t:p:o" opt; do case $opt in u) user=$OPTARG ;; o) output=1 ;; t) to=$OPTARG ;; p) thresholdPercent=$OPTARG ;; :) log "Option -$OPTARG requires an argument." >&2 exit 1 ;; esac done # get this users max files maxfiles=$(su - $user -c "ulimit -a | grep 'open files'" | awk -F ' *' '{print $4}') #ensure we were able to get something here if [ $? -ne 0 ]; then echo "Error occurred when SU - $user" exit 1 fi # get the number of current files open openfiles=$(/usr/sbin/lsof | grep "$user" | wc -l) threshold=$(echo "scale=10; $maxfiles*$thresholdPercent" | bc | awk '{printf "%.0f\n", $1}') # Echo some output if not supressing if [[ ! $output ]]; then echo -e "USER \t\t\t\tMaxFiles \t\t\tOpenFiles \t\t\tUserThreshold" echo -e "${user} \t\t\t${maxfiles} \t\t\t\t${openfiles} \t\t\t\t${threshold}" fi # Compare open files to threshold, if low send email if [ $openfiles -gt $threshold ]; then echo "File handles low..." echo -e "$HOSTNAME file handles low for $user \n\n openfiles: $openfiles, alert threshold: $threshold, maxfiles: $maxfiles" | mail -s "File handles low on $user@$HOSTNAME" -r "filelow@$HOSTNAME.visibiz.com" ${to} fi exit 0