The purpose of the script is to login to multiple servers and execute the df -k command and to send out email containing the list similar like below : Thu Nov 3 12:59:49 EDT 2016
Running out of space "/opt (80%)" on (a******001s02)
Running out of space "/var (83%)" on (a*******01s01)
Running out of space "/opt/IBM/ITM (98%)" on (a*******001s01)
Running out of space "/apps (80%)" on (a*********01s01)
Running out of space "/opt/wily (80%)" on (a********01s01)
My challenge is : This script has to run as a scheduled job like autosys job . And the challenge is that, what i believe(not 100 percent sure) , the username ( ssh $username@$host) should be a sudo user for it to run as an autosys job. But if i hardcode username as a sudo user , the ssh command wont work . As in , we cannot login to multiple servers with username as sudo. Here I have used 'logname' , so logname will take the name of the user executing the command .But , logname doesnt seem to work if i have to make it as autosys job .
Basically what I want is to make this script run as an autosys job. Currently its working perfectly fine If I can manually run the script .I appreciate any help. Thanks in advance :)
Entire script:
#Shell script to watch disk space
#!/bin/sh
MAILTO="xxxxxxx@xxxxx.com"
# Set alert level 80% is default
ALERT=70
path=/tmp/Joshma
report=$path/report.txt
date > $report
echo >>$report
servers=$path/serversfile.txt
username=`logname`
for host in `cat $servers`
do
echo "$host"
ssh $username@$host df -k | grep -vE '^Filesystem|tmpfs|cdrom|proc' | awk '{ print $4 " " $7 }' | while read output;
do
echo " $output"
usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1 )
partition=$(echo $output | awk '{ print $2 }' )
if [ $usep -ge $ALERT ]
then
echo "Running out of space \"$partition ($usep%)\" on ($host)\n" >>$report
fi
done
done
cat $report | mail -s "File System Space Check" $MAILTO