Friday, November 12, 2010

Shell Script To Check Disk Usage

Shell Script To Check Disk Usage ... 


#!/bin/sh
# Get ip address of this server 
IP=$(ifconfig eth0 | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}')

# tmp keep all the info in a file.
fname="~/diskusage.txt"

# put IP address at the top of the file
echo $IP > $fname
# new line ...
echo "" >> $fname
echo "" >> $fname

# put the disk info to file
df -l -H >> $fname
echo "" >> $fname

# keep diskinfo in a variable 
DF=$(cat $fname)

# get the server uptime
UPTIME=`uptime`

# SET ALRT DISK LIMIT 90% ALERT TRIGGER ...
ALERT_LIMIT=90

# Ignore Filesystem|tmpfs|cdrom|boot partition ... get the data disk only ...
df -l -H | grep -vE '^Filesystem|tmpfs|cdrom|boot' | awk '{ print $5 " " $1 }' | while read output;

do
  # get the % usage of the disk 
  usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1  )
  
  # Which partition id this ...
  partition=$(echo $output | awk '{ print $2 }' )
  if [ $usep -ge $ALERT_LIMIT ]; then
    echo "Server: $IP Running out of space" >> $fname
    echo "" >> $fname
    echo "\"$partition ($usep%)\" on $(hostname) as on $(date) .." >> $fname
    echo "" >> $fname
    echo "$output" >> $fname
    echo "" >> $fname
    echo "" >> $fname
    cat $fname | mail -s "Alert: $aa Almost out of disk space $usep%" razimi@funnel.my
  else
echo "" >> $fname
echo $UPTIME >> $fname
cat $fname | mail -s "Server $IP Disk Usage report $(date)" razimi@funnel.my
  fi
done

Centos 7 reset root/ any user lost password / lockout due to cant remember password

1. Need to be in front of the terminal. (Physically if not vm). 2. Reboot the server 3. Press 'e' in the GRUB2 boot screen. 3. bunch...