Category Archives: Scripts

icecast load test script

Icecast server Load tests are done with the following script.

#!/bin/sh
#
# run concurrent curls which download from URL to /dev/null.  output total
# and average counts to results directory.
#

# max concurrent curls to kick off
max=7000
# how long to stay connected (in seconds)
duration=99999999
# how long to sleep between each curl, can be decimal  0.5
delay=10
# url to request from
URL=http://dwrac1:8500/stream.ogg


#####
#mkdir -p results
echo > results
while /bin/true
do
count=1
while [ $count -le $max ]
do  
   curl -o /dev/null -m $duration -s -w "bytes %{size_download} avg %{speed_download} " "$URL" >> results &
   curl -o /dev/null -m $duration -s -w "bytes %{size_download} avg %{speed_download} " "$URL" >> results &
   curl -o /dev/null -m $duration -s -w "bytes %{size_download} avg %{speed_download} " "$URL" >> results &
   curl -o /dev/null -m $duration -s -w "bytes %{size_download} avg %{speed_download} " "$URL" >> results &
   curl -o /dev/null -m $duration -s -w "bytes %{size_download} avg %{speed_download} " "$URL" >> results &
   curl -o /dev/null -m $duration -s -w "bytes %{size_download} avg %{speed_download} " "$URL" >> results &
   curl -o /dev/null -m $duration -s -w "bytes %{size_download} avg %{speed_download} " "$URL" >> results &
   curl -o /dev/null -m $duration -s -w "bytes %{size_download} avg %{speed_download} " "$URL" >> results &
   curl -o /dev/null -m $duration -s -w "bytes %{size_download} avg %{speed_download} " "$URL" >> results &
   curl -o /dev/null -m $duration -s -w "bytes %{size_download} avg %{speed_download} " "$URL" >> results &
   curl -o /dev/null -m $duration -s -w "bytes %{size_download} avg %{speed_download} " "$URL" >> results &
   [ "$delay" != "" ] && sleep $delay
   let count=$count+10
done
wait
done
echo "done"

taken from  : http://icecast.org/loadtest/1/

Run scripts on Linux every few seconds, faster than Cron can do it.

With Cron you can run php scripts every minute but not in shorter period of time. So what can you do when you need to run your script every 15 seconds or even 1 second ?

Create shell sctipt like this one fasterthancron.sh

#!/bin/bash
#This script run every seconds
while (sleep 1 && php /path_to_script/faster_than_cron.php) &
do
  wait $!
done

Reference :
Ivan Đurđevac : Run scripts on Linux/Ubuntu every few seconds, faster than Cron can do it..

Automatically MountNFS Shares Without Autofs

Setup clients on a local network to automatically mount NFS shares whenever the fileserver is up, without using autofs. Instead, a simple bash script checks if the server is up, and if the shares need to be mounted or unmounted.
Can be used to include within backup scripts.
Found here : https://help.ubuntu.com/community/AutomaticallyMountNFSSharesWithoutAutofsHowto

#!/bin/bash

# AutoNFS v.1.1
#
# 2010-09-02    Jeroen Hoek <mail@jeroenhoek.nl>:
#   * Update script with helpful contributions from other users.
#   * Stop using logger, simply echo and let the system log it in /var/log/upstart.
# 2012-07-23    Martin Seener:
#   * Use rpcinfo instead of ping to check the status of the NFS server daemon, rather
#     than just the server being up.
#   * Add some useful mount options for a stable NFS mount.
# 2012-03-12    tobcro:
#   * Allow local and remote mountpoint to be different.
# 2010-01-24    Jeroen Hoek <mail@jeroenhoek.nl>:
#   * Initial script.


# Configuration parameters.

# The hostname or IP-address of the fileserver:
FILESERVER="obelix03.dmd2.ch"

# Mount Options (see mount man pages for info).
## wsize=32768,rsize=32768,soft,noatime,user,intr,suid,dev,exec,bg

MOUNTOPTS="-o rw,soft,bg,intr,tcp,actimeo=3"

# Check every X seconds (60 is a good default):
INTERVAL=60

# Delimeter used for separating fileserver/client shares below:
DELIMETER="|"

# The shares that need to be mounted. If the local and remote mount point
# differ, write something like "/media/remoteshare|/media/localshare", where "|" is
# the delimeter configured above. If the mount points are the same, you can also use 
# the short-hand "/media/share".
# MOUNTS=( "/media/exampleRemote1|/media/exampleLocal1" "/media/exampleMount2" )
MOUNTS=( "/home|/mnt/obelix03" )

# Logging. Set to true for debugging and testing; false when everything works. Logs 
# are written to /var/log/upstart/autonfs.log.
LOG=true

# End of configuration


function log {
    if $LOG; then
        echo $1
    fi
}


log "Automatic NFS mount script started."

declare -a MOUNTP
while true; do
    # Is the NFS daemon responding?
    rpcinfo -t "$FILESERVER" nfs &>/dev/null
    if [ $? -eq 0 ]; then
        # Fileserver is up.
        log "Fileserver is up."
        for MOUNT in ${MOUNTS[@]}; do
            # Split up the share into the remote and local mount point.
            MOUNTP=(`echo ${MOUNT//$DELIMETER/ }`)
            # The second part of the mount string is the local mount point.
            # If there is no second part, local and remote are mounted on
            # the same location.
            HERE=${MOUNTP[${#MOUNTP[@]}-1]}
            THERE=${MOUNTP[0]}
            if grep -qsE "^([^ ])+ ${HERE}" /proc/mounts; then
                log "$HERE is already mounted."
            else
                # NFS mount not mounted, attempt mount
                log "NFS share not mounted; attempting to mount ${HERE}:"
                mount -t nfs ${MOUNTOPTS} ${FILESERVER}:${THERE} ${HERE}
            fi
        done
    else
        # Fileserver is down.
        log "Fileserver is down."
        for MOUNT in ${MOUNTS[@]}; do
            # Split up the share into the remote and local mount point.
            MOUNTP=(`echo ${MOUNT//$DELIMETER/ }`)
            # The second part of the mount string is the local mount point.
            # If there is no second part, local and remote are mounted on
            # the same location.
            HERE=${MOUNTP[${#MOUNTP[@]}-1]}
            THERE=${MOUNTP[0]}
            if grep -qsE "^([^ ])+ ${HERE}" /proc/mounts; then
                # NFS mount is still mounted; attempt umount
                log "Cannot reach ${FILESERVER}, unmounting NFS share ${HERE}."
                umount -f ${HERE}
            fi
        done
    fi
    sleep $INTERVAL
done

HP Smart Array Controller – Monitor the status of the Raid Controller

The script hp_smartarray_monitor.sh checks the status of your HP Smart Array Controller and send an Mail in case something is broken.

#!/bin/bash

###
# FILE: hpacucli-check.sh (2010-11-05)
# LICENCIA: GNU/GPL v3.0
# USAGE: Check the status of the logical drives on a HP Server 
#        with hpacu (HP Array Configuration Utility Client)
#        installed, syslog and send an email with errors.
# AUTHOR: Olaf Reitmaier Veracierta <olafrv@gmail.com> / www.olafrv.com
##

MAIL=thomas.zumbrunnen@dmdtwo.com
HPACUCLI=`which hpacucli`
HPACUCLI_TMP=/tmp/hpacucli.log

hpacucli ctrl all show | grep "Slot " | while read line1
do
   slot=`expr match "$line1" '.*Slot \([0-9]\).*'`
   echo "Searching controller in slot #$slot..."
   hpacucli ctrl slot=$slot array all show | grep array | while read line2
   do
      array=`expr match "$line2" '.*array \([a-Z]\).*'`
      echo "Searching array $array..."
      hpacucli ctrl slot=$slot array $array logicaldrive all show | grep logicaldrive | while read line3
      do
         logicaldrive=`expr match "$line3" '.*logicaldrive \([0-9]\).*'`
         echo "Searching logical drive #$logicaldrive..."
         if [ `hpacucli ctrl slot=$slot array $array logicaldrive $logicaldrive show | grep "Status: OK$" | wc -l` -lt 1 ]
         then
            msg="RAID Controller Error Slot #$slot Array $array Logical Drive #$logicaldrive"
            echo $msg
            logger -p syslog.error -t RAID "$msg"
            $HPACUCLI ctrl slot=$slot show config detail > $HPACUCLI_TMP
            mail -s "$HOSTNAME [ERROR] - $msg" "$MAIL" < $HPACUCLI_TMP
            rm -f $HPACUCLI_TMP
         fi
      done
   done
done