Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

2015/08/25

Quickly check hundreds of Linux VM's for read-only filesystems

Sometimes a NAS cluster failover event for routine maintenance may take too long; or you bought the wrong storage solution; or you have a major network or SAN fault... or something else.

Any of these situations can cause your virtual machines to remount their filesystems "read-only".  The proper thing (apart from fixing your infrastructure) to make your VM's more tolerant is to configure SCSI timeouts within each VM, in the case of VMware, or in the case of KVM or Xen on NFS, to change the NFS mount options. (see http://discussions.citrix.com/topic/344713-linux-vm-storage-going-read-only-on-nfs-shares-and-a-proposed-fix-in-xenserver/ for the Citrix XenServer hack.)

To detect this "read-only filesystem" rapidly, I wrote a little script I call "ckrofs".  You use it as follows:

ckrofs ...
or
ckrofs `cat hostlist`



Here's the code:

#!/bin/bash

HostList=`echo ${@} | tr \\\n " "`

PARALLELISM=20

function remoteCheck()
{ 
   TargetHost=$1
   ThisHostOutput="### $TargetHost ###"
   ThisHostOutput+="$(ssh -q $TargetHost 'for targetfs in `mount | egrep "ext" | cut -d " " -f 3`; do touch $targetfs/foofile > /dev/null 2>&1 && rm -f $targetfs/foofile  || echo -n " $targetfs appears to be readonly #";  done ' 2>&1 || echo ' unable to log in to host' )"
   echo $ThisHostOutput
}

export -f remoteCheck


echo ${HostList} | xargs -P $PARALLELISM -d" " --replace="HOST" /bin/bash -c 'remoteCheck HOST'


2013/11/15

Automatically delete old NetApp snapshots left by backups

Backup software may sometimes leave behind a snapshot.  This can increase the space consumption a lot over time.

Here's a script that can be run against NetApp filers to clean up those "stale" backups.  Note that in my case, I configured CommVault to name the snapshots with the string "snapshot_for_backup", though the default is just "ndmp".  You may change that as needed.




#!/bin/bash

# This script is to look for snaps that are left over from backups, that # are no longer in use, and delete them.
# This script requires two parameters:
# snap_cleanup     (list or delete all stale snaps on the specified servers)

NasUser="root"
SnapshotString="snapshot_for_backup"
SshIdentityFile="/root/.ssh/id_rsa_auslxfs00_rsync"
SshBinary=/usr/bin/ssh


# This function receives a volume name as a parameter, and returns # the number of snaps that are elligible for deletion, defined by being:
#                             1. Having a certain string in the snapshot name;
#                             2. Not marked as "busy"
function CountStaleSnaps ()
{
                local VolToCheck=$1
                local StaleSnaps=`$SshCmd "snap list $VolToCheck" | grep $SnapshotString | grep -v "busy" | wc -l`
                if [ $? -ne 0 ]; then
                {
                                echo "getting count of elligible snapshots returned an error"
                                exit 1
                } else
                {
                                echo $StaleSnaps
                } fi
}

# Function GetStaleSnapNames
# This function creates, given a volume name, an array of snapshots that are # candidates for deletion.
# Parameters
#             1. volume to check
function GetStaleSnapNames ()
{
                local Volume=$1
                StaleSnapNames=( $($SshCmd "snap list $Volume" | grep $SnapshotString| grep -v busy | cut -c 39- | cut -f 1 -d " ") )
                if [ $? -ne 0 ]; then
                {
                                echo "getting names of elligible snapshots returned an error"
                                exit 1
                } else
                {
                                printf -- '%s\n' "${StaleSnapNames[@]}"
                } fi
}

# Function DelStaleSnaps
# This function deletes stale snaps for the Volume name passed to it.
# Parameters
#  1. volume
#  2. snapshot_name
function DelStaleSnaps ()
{
                local Volume=$1
                local SnapToDelete=$2
                $SshCmd "snap delete $Volume $SnapToDelete"
                if [ $? -ne 0 ]; then
                {
                                echo "error deleting snapshot $Volume:$SnapToDelete"
                                exit 1
                } else
                {
                                echo "successfully deleted snapshot $Volume:$SnapToDelete"
                } fi
}

# Parse command line parameters
case $2 in
                "")
                                echo "ERROR: you must specify as a second parameter a host name on which you want to delete snapshots."
                                ;;
                *)
                                NasName=$2
                                ;;
esac
case $1 in
                list)
                                Operation=list
                                ;;
                delete)
                                Operation=delete
                                ;;
                *)
                                echo "ERROR: invalid operation specified on command line.  Please specify either 'list' or 'delete' followed by the servername on which you want to delete the snapshots."
                                exit 1
                                ;;
esac

SshCmd="/usr/bin/ssh -i $SshIdentityFile $NasUser@$NasName"

VolumesToCheck=( `$SshCmd "vol status -b" | cut -f 1 -d " " | egrep -v "Volume|-----"` )

for CurrentVol in ${VolumesToCheck[@]}; do {
                echo -n "checking $NasName:$CurrentVol... "
                StaleSnaps=`CountStaleSnaps $CurrentVol`
                echo $StaleSnaps            
                if [ $Operation = list ]; then
                                if [ $StaleSnaps -ne 0 ]; then
                                                GetStaleSnapNames $CurrentVol |  awk '{ print "    " $1 }'
                                fi
                elif [ $Operation = delete ]; then
                                GetStaleSnapNames $CurrentVol
                                ArrayOfSnaps=( $(GetStaleSnapNames $CurrentVol) )
                                for TargetSnap in `printf -- '%s\n' "${ArrayOfSnaps[@]}"`; do
                                                                DelStaleSnaps $CurrentVol $TargetSnap
                                done
                fi
                               
}; done


2013/07/10

Linux bash shell options parsing


Normally, command line parameters come in as separate, positional values, and may be referenced as:
  • $# - the number of command line arguments (positional parameters)
  • $* - all positional parameters expressed as a single string
  • $@ - all positional parameters, but with each as a quoted string (each positional parameter is intact and presented as a quoted string)
  • $0 - (the base name of the script itself)
  • $1 - The first positional parameter
  • $2 - The second positional parameter, and $3, $4, etc.  Starting with 10, they must be expressed as ${10}, ${11}, etc.
Sometimes a variable is passed from a wrapper script to a child script where the positional parameter is in fact several parameters that should be parsed separately.

Here, we test for that case, and if found, we peel off the first parameter within that group of strings, and assign the remaining parameters in the group of springs:

# from the front, and make the rest of the data be the options passed to rsync.
if [ $# -eq 1 ]; then
{
   Params=($1)
   PARAM_1=${Params[0]}
   unset Params[0]
   ChildCommandOpts=${Params[*]}
} fi


2008/03/24

Fix Absolute Symbolic Links from tar or rsync SNAFU

Tar will strip leading slashes from sym links (if you don't use -P), as will rsync in some situations (for example, with chroot=true). This can put you in a world of hurt. Here are some tips for how to fix it.
  • This one won't work for a couple of reasons; first, it can't distinguish between intended relative paths and accidental relative paths; also, cut won't work if any files that have a different character length in any fields of the ls output, such as ownership or date (there's a way to merge whitespace that escapes me, not fmt or pr, but something :
    • find . -type l -print0 | xargs -0 ls -laQ | grep ' -> \"relative/path/stuff'| cut --complement -d " " -f 1-8 | sed -e 's/ -> \"/\;\"\//g' | awk -F\; '{ print "ln -sf " $2 " " $1 }' > fix_symlinks
  • How about this:
    • find . -type l -printf 'ln -sf "/%l" "%p"\n' | grep "\"/accidental/relative/path" > fix_symlinks
    • check the fix_symlinks for things that you actually don't want to change.
    • . ./fix_symlinks
There has got to be a more elegant way to do this. *sigh* if my perl skills were half-decent, it'd probably be a breeze.

Also, note the "symlinks" command in the "symlinks" package.



2007/06/28

How to process command-line args in bash scripts

while [ $# -gt 0 ]; do
case $1 in
--somevar)
SOMEVAR=$2
SOMEVARSTR="--somevar $2"
shift; shift
;;
--othervar)
OTHERVAR=$2
shift; shift
;;
--version)
VERSION=$2
shift; shift
;;
--debug)
DEBUGSTR="--debug"
shift
;;
*)
if [ -n "$TARGET" -o ! -d $1 ]; then
usage
fi
TARGET=$1
shift
;;
esac
done