Commit ec8934d6 authored by thomas.forbriger's avatar thomas.forbriger
Browse files

examples [FEATURE]: provide some example scripts

parent e8280ae9
this is <README>
============================================================================
files in this directory
-----------------------
This directory contains some examples selected from my every-day use. The
scripts are designed to be run by observer and produce the expected output.
They are placed in one of the subdirectories of $HOME/observer/scripts/
csbackbromo
Checks the status of csback on host bromo and produces a report.
diskspace
Checks local disk space and produces a warning if thresholds are exceeded.
localsikloft
Copies local directories (defined in a list) to a loft space in order to
maintain a simple backup copy. The loft location is date stamped. The amount
of copies is checked and a warning is produced, if the defined number is
exceeded.
sikloft
Similar to localsikolft but sends to a remote host.
ziplog
Compresses observer log files in the users observer directory. Sends a
notice if total log size exceeds a defined thershold.
----- END OF README -----
#!/bin/sh
# this is <csbackobserver>
# ----------------------------------------------------------------------------
# $Id$
#
# Copyright (c) 2012 by Thomas Forbriger (BFO Schiltach)
#
# check csback messages on pinatubo
#
# REVISIONS and CHANGES
# 18/05/2012 V1.0 Thomas Forbriger
# 19/11/2013 V1.1 tail syntax has changed
#
# ============================================================================
#
# issue an alert; if this is the only response receibed by observer, it
# indicates a fatal failure of the script
echo $OBS_STATUS_ALERT
echo $OBS_KEY_MESSAGE terminated unintendedly
# get into contact with ssh agent in order to use ssh keys
. $HOME/.ssh/agent-info
set -o pipefail
# ============================================================================
# set parameters
# repository name
CSBACKHOST=bromo
CSBACKNTFY=/home/thof/git/csback/csbackntfy.py
CSBACKOPTS="-D 4"
# ============================================================================
# commands and standard parameters
AWK=/usr/bin/awk
CAT=/bin/cat
CP=/bin/cp
CUT=/usr/bin/cut
CVS=/usr/bin/cvs
CVS_RSH=ssh
DATECMD=/bin/date
EGREP=/usr/bin/egrep
EXPR=/usr/bin/expr
FIND=/usr/bin/find
GPIFIND=/usr/bin/find
GREP=/usr/bin/grep
HEAD=/usr/bin/head
LS=/bin/ls
MKDIR=/bin/mkdir
MV=/bin/mv
PING=/bin/ping
PWD=/bin/pwd
RM=/bin/rm
SSH="/usr/bin/ssh -oBatchMode=yes"
SVN=/usr/bin/svn
SVNSYNC=/usr/bin/svnsync
TAIL=/usr/bin/tail
TAR=/bin/tar
TAREXT=.tar
TEE=/usr/bin/tee
TR=/usr/bin/tr
WC=/usr/bin/wc
XARGS=/usr/bin/xargs
ZIP=/usr/bin/bzip2
ZIPEXT=.bz2
export CVS_RSH
# ============================================================================
DATE=$($DATECMD +%Y%m%d)
# ============================================================================
# send a greeting message
echo "This is $0 at $($DATECMD)" | $TEE -a $OBS_LOG
echo "This is observer script $OBS_SCRIPT" | $TEE -a $OBS_LOG
(
echo '$Id$'
echo Action:
echo Check csback log entries on $CSBACKHOST
) | $TEE -a $OBS_LOG
# ============================================================================
# begin operation
# ---------------
# report time
echo >> $OBS_LOG
echo $($DATECMD) | $TEE -a $OBS_LOG
# initialize status variables
STATUS=0
UNAVAIL=0
HOSTAVAIL=0$($PING $CSBACKHOST -w 60 -c 1 -q -v | $TAIL -n -2 | $HEAD -1 | \
$CUT -d " " -f 4)
if test $HOSTAVAIL -eq 1
then
$SSH $CSBACKHOST "$CSBACKNTFY -o $CSBACKOPTS" | $TEE -a $OBS_LOG
STATUS=$($SSH $CSBACKHOST "$CSBACKNTFY -n $CSBACKOPTS" | tr -s ' ' | cut -f 2 -d ' ')
if test "$STATUS" == 'OK'
then
echo $OBS_STATUS_OK
elif test "$STATUS" == 'WARNING'
then
echo $OBS_STATUS_NOTICE
else
echo $OBS_STATUS_ALERT
fi
echo $OBS_KEY_MESSAGE "$CSBACKHOST csback messages level: $STATUS"
else
echo $OBS_STATUS_NOTICE
echo $OBS_KEY_MESSAGE "$CSBACKHOST is not available"
fi
# report time
echo >> $OBS_LOG
echo $($DATECMD) | $TEE -a $OBS_LOG
# ----- END OF csbackobserver -----
#!/bin/sh
# this is <diskspace>
# ----------------------------------------------------------------------------
# $Id$
#
# Copyright (c) 2012 by Thomas Forbriger (BFO Schiltach)
#
# check disk space
#
# REVISIONS and CHANGES
# 14/01/2012 V1.0 Thomas Forbriger
# 23/04/2012 V1.1 handle two-line NFS mounts
#
# ============================================================================
#
# issue an alert; if this is the only response receibed by observer, it
# indicates a fatal failure of the script
echo $OBS_STATUS_ALERT
echo $OBS_KEY_MESSAGE terminated unintendedly
# ============================================================================
# set parameters
# alarm is issued if used disk space exceeds $ATH %
ATH=97
# notice is issued if used disk space exceeds $ATH %
NTH=94
# indicate failure if any command in a pipe fails
set -o pipefail
# ============================================================================
# commands and standard parameters
AWK=/usr/bin/awk
CAT=/bin/cat
CP=/bin/cp
CUT=/usr/bin/cut
CVS=/usr/bin/cvs
CVS_RSH=ssh
DATECMD=/bin/date
DF=/bin/df
EGREP=/usr/bin/egrep
EXPR=/usr/bin/expr
FIND=/usr/bin/find
GPIFIND=/usr/bin/find
GREP=/usr/bin/grep
HEAD=/usr/bin/head
LS=/bin/ls
MKDIR=/bin/mkdir
MV=/bin/mv
PING=/bin/ping
PWD=/bin/pwd
RM=/bin/rm
SSH="/usr/bin/ssh -oBatchMode=yes"
SVN=/usr/bin/svn
SVNSYNC=/usr/bin/svnsync
TAIL=/usr/bin/tail
TAR=/bin/tar
TAREXT=.tar
TEE=/usr/bin/tee
TR=/usr/bin/tr
WC=/usr/bin/wc
XARGS=/usr/bin/xargs
ZIP=/usr/bin/bzip2
ZIPEXT=.bz2
export CVS_RSH
# ============================================================================
# send a greeting message
echo "This is $0 at $($DATECMD)" | $TEE -a $OBS_LOG
echo "This is observer script $OBS_SCRIPT" | $TEE -a $OBS_LOG
(
echo '$Id$'
echo Action:
echo This script checks the available disk space
echo notice threshold: $NTH'%' disk space used
echo alarm threshold: $ATH'%' disk space used
) | $TEE -a $OBS_LOG
# ============================================================================
# begin operation
# ---------------
ERROR=0
NN=0
DN=
NA=0
DA=
NT=0
PARTITIONS=$($DF 2>/dev/null | $TR -s ' ' | $CUT -f 6 -d ' ' \
| $EGREP '(^/)')
$DF -h 2>/dev/null | $TEE -a $OBS_LOG
for PART in $PARTITIONS
do
LINE=$($DF -h $PART | $TAIL -n +2 | $TR -d '\n' | $TR -s ' ')
STATUS=$?
if test $STATUS -gt 0
then
ERROR=1
fi
DEVICE=$(echo $LINE | $CUT -f 1 -d ' ')
USAGE=$(echo $LINE | $CUT -f 5 -d ' ' | $TR -d '%')
if test $USAGE -gt $ATH
then
NA=$($EXPR $NA + 1)
DA="$DA $DEVICE"
elif test $USAGE -gt $NTH
then
NN=$($EXPR $NN + 1)
DN="$DN $DEVICE"
fi
NT=$($EXPR $NT + 1)
done
# ----------------------------------------------------------------------------
# report final status
if test $ERROR -gt 0
then
echo $OBS_STATUS_ALERT
echo $OBS_KEY_MESSAGE "unexpected error!"
elif test $NA -gt 0
then
echo $OBS_STATUS_ALERT
echo $OBS_KEY_MESSAGE "$NA is/are critical: $DA"
elif test $NN -gt 0
then
echo $OBS_STATUS_NOTICE
echo $OBS_KEY_MESSAGE "$NN exceed(s) thershold: $DN"
else
echo $OBS_STATUS_OK
echo $OBS_KEY_MESSAGE "checked $NT file systems"
fi
# ----- END OF diskspace -----
#!/bin/sh
# this is <localsikloft>
# ----------------------------------------------------------------------------
# $Id$
#
# Copyright (c) 2008 by Thomas Forbriger (BFO Schiltach)
#
# maintain a local loft dir for sik files
#
# this script is designed to run in an observer environment
#
# Action:
# - copy all directories that are specified in $LOFTSRC
# - create loft in a subdirectory of $LOFTDIR with a date specific name
# - report how many more directories are kept in the loft than are specified
# in $LOFTDIRSTOKEEP
#
# REVISIONS and CHANGES
# 23/11/2008 V1.0 Thomas Forbriger
# 19/11/2013 V1.1 tail syntax has changed
#
# ============================================================================
#
echo $OBS_STATUS_ALERT
echo $OBS_KEY_MESSAGE terminated unintendedly
#
LOFTSRC="$HOME/sik"
LOFTDIR=/var/sik/$USER/loft
LOFTDIRSTOKEEP=14
LOFTLOG=$OBS_LOG
LOFTNAME=LOFT$(date +'%Y%m%d')
CPOPTS=-vprd
FIND=/usr/bin/find
XARGS=/usr/bin/xargs
TAIL=/usr/bin/tail
RM=/bin/rm
CP=/bin/cp
LS=/bin/ls
MKDIR=/bin/mkdir
BASENAME=/usr/bin/basename
MAIL=/usr/bin/mail
DATE=/bin/date
CAT=/bin/cat
DU=/usr/bin/du
WC=/usr/bin/wc
TEE=/usr/bin/tee
# send a greeting message
echo "This is $0 at $($DATE)" | $TEE -a $OBS_LOG
echo "This is observer script $OBS_SCRIPT" | $TEE -a $OBS_LOG
(
echo '$Id$'
echo Action:
echo copy directories: $LOFTSRC
echo create loft in a subdirectory of $LOFTDIR with a date specific name
echo report how many more directories are kept in the loft than $LOFTDIRSTOKEEP
) | $TEE -a $OBS_LOG
# tell loft directory
(
echo '$Id$'
$DATE
echo
echo creating next loft in
echo " $LOFTDIR/$LOFTNAME"
echo the following directories will be copied:
echo $LOFTSRC | tr ' ' '\n' | sed 's/^/ /'
echo
) | $TEE -a $OBS_LOG
# create loft directory
echo $MKDIR -p $LOFTDIR/$LOFTNAME | $TEE -a $OBS_LOG
$MKDIR -p $LOFTDIR/$LOFTNAME || \
( echo $OBS_STATUS_ALERT; \
echo $OBS_KEY_MESSAGES failed; exit 2 )
# cycle through all directories specified as LOFTSRC
for d in $LOFTSRC
do
echo | $TEE -a $OBS_LOG
echo $CP $CPOPTS $d $LOFTDIR/$LOFTNAME | $TEE -a $OBS_LOG
$CP $CPOPTS $d $LOFTDIR/$LOFTNAME >> $OBS_LOG 2>&1 || \
( echo $OBS_STATUS_ALERT; \
echo $OBS_KEY_MESSAGES failed; exit 2 )
done
echo | $TEE -a $OBS_LOG
# check number of directories kept in the loft more than specified in
# $LOFTDIRSTOKEEP
numloft=$($LS -l --sort=time $LOFTDIR | $TAIL -n +$LOFTDIRSTOKEEP | $WC -l)
echo $OBS_STATUS_OK
echo $OBS_KEY_MESSAGE keeping $numloft more archives than specified
# ----- END OF localsikloft -----
#!/bin/sh
# this is <sikloft>
# ----------------------------------------------------------------------------
#
# 07/11/2001 by Thomas Forbriger (IMGF Frankfurt)
#
# Erzeuge Loft-Kopien
#
# Designed to communicate with observer
#
# Action:
# All directories specified in $LOFTSRC will by copied to $LOFTHOST into
# directory $LOFTDIR there by executing $SCP $SCPOPTS
#
# REVISIONS and CHANGES
# 07/11/2001 V1.0 Thomas Forbriger
# 09/05/2003 V1.1 update for BFO use
# 22/06/2003 V1.2 include CVSsik
# 29/04/2004 V1.3 changed lofthost to anton (was rotondo)
# 20/11/2007 V1.4 explains what it does
# 11/08/2010 V1.5 make pinghost test robust against failing name
# resolution
# path to tr and cut changed
# added missing -d and make loftsize check failsafe
# 13/08/2010 V1.6 add SVNsik
# 19/11/2013 V1.7 tail syntax has changed
#
# ============================================================================
echo $OBS_STATUS_ALERT
echo $OBS_KEY_MESSAGE terminated unintendedly
# get into contact with ssh agent to use key to $LOFTHOST
. $HOME/.ssh/agent-info
#
LOFTSRC="$HOME/CVSsik/tars $HOME/sik $HOME/SVNsik/tars"
#LOFTSRC="$HOME/sik"
LOFTDIR=/home/thof/loft
#LOFTHOST=rotondo
LOFTHOST=anton
LOFTDIRSTOKEEP=14
LOFTLOG=$OBS_LOG
LOFTNAME=LOFT$(date +'%Y%m%d')
SSH="/usr/bin/ssh -oBatchMode=yes"
SCP=/usr/bin/scp
SCPOPTS=-vqpr
FIND=/usr/bin/find
XARGS=/usr/bin/xargs
TAIL=/usr/bin/tail
RM=/bin/rm
LS=/bin/ls
MKDIR=/bin/mkdir
BASENAME=/usr/bin/basename
MAIL=/usr/bin/mail
DATE=/bin/date
CAT=/bin/cat
TR=/usr/bin/tr
CUT=/usr/bin/cut
DU=/usr/bin/du
TEE=/usr/bin/tee
PINGHOST=$HOME/bin/scripts/pinghost
# send a greeting message
echo "This is $0 at $($DATE)" | $TEE -a $OBS_LOG
echo "This is observer script $OBS_SCRIPT" | $TEE -a $OBS_LOG
(
echo '$Id$'
echo Action:
echo Copy all directories specified in LOFTSRC:
echo $LOFTSRC
echo and copy them to LOFTDIR=$LOFTDIR on LOFTHOST=$LOFTHOST
echo by executing $SCP $SCPOPTS
) | $TEE -a $OBS_LOG
# check for lofthost
avail=0$($PINGHOST $LOFTHOST)
if test $avail -ne 1
then
echo $OBS_STATUS_NOTICE
echo $OBS_KEY_MESSAGE $LOFTHOST not available
exit 2
fi
# tell loft directory
(
echo '$Id$'
date
echo
echo creating next loft at
echo " $LOFTHOST:$LOFTDIR/$LOFTNAME"
echo the following directories will be copied:
echo $LOFTSRC | tr ' ' '\n' | sed 's/^/ /'
echo
) | $TEE -a $OBS_LOG
# create loft directory
echo $SSH $LOFTHOST $MKDIR -p $LOFTDIR/$LOFTNAME | $TEE -a $OBS_LOG
$SSH $LOFTHOST $MKDIR -p $LOFTDIR/$LOFTNAME || \
( echo $OBS_STATUS_ALERT; \
echo $OBS_KEY_MESSAGES failed; exit 2 )
# cycle through all directories specified as LOFTSRC
for d in $LOFTSRC
do
echo | $TEE -a $OBS_LOG
echo $SCP -vqpr $d $LOFTHOST:$LOFTDIR/$LOFTNAME | $TEE -a $OBS_LOG
$SCP $SCPOPTS $d $LOFTHOST:$LOFTDIR/$LOFTNAME >> $OBS_LOG 2>&1 || \
( echo $OBS_STATUS_ALERT; \
echo $OBS_KEY_MESSAGES failed; exit 2 )
done
echo | $TEE -a $OBS_LOG
#echo | $TEE -a $OBS_LOG
#echo $SSH $LOFTHOST "$LS -l --sort=time $LOFTDIR | tail +$LOFTDIRSTOKEEP | \
# $XARGS --no-run-if-empty $RM -vrf" | $TEE -a $OBS_LOG
#$SSH $LOFTHOST "$LS -l --sort=time $LOFTDIR | tail +$LOFTDIRSTOKEEP | \
# $XARGS --no-run-if-empty $RM -vrf" || \
# ( echo $OBS_STATUS_ALERT; \
# echo $OBS_KEY_MESSAGES failed; exit 2 )
# check number of files kept in the loft
numloft=$($SSH $LOFTHOST "$LS -l --sort=time $LOFTDIR" | \
$TAIL -n +$LOFTDIRSTOKEEP | wc -l)
echo $OBS_STATUS_OK
echo $OBS_KEY_MESSAGE keeping $numloft more archives than specified
# check size of loft
loftsize=0$($SSH $LOFTHOST "$DU -sm $LOFTDIR" | \
$TR '\t' ' ' | $CUT -f 1 -d ' ')
if [ $loftsize -gt 10000 ]
then
echo $OBS_STATUS_NOTICE
echo $OBS_KEY_MESSAGE loft size exceeds 10 GByte with $loftsize MByte
fi
if [ $loftsize -gt 20000 ]
then
echo $OBS_STATUS_ALARM
echo $OBS_KEY_MESSAGE loft size exceeds 20 GByte with $loftsize MByte
fi
#
# ----- END OF sikloft -----
#!/bin/sh
# this is <ziplog>
# ----------------------------------------------------------------------------
# $Id$
#
# Copyright (c) 2003 by Thomas Forbriger (BFO Schiltach)
#
# zip all logfiles
#
# action:
# compress all log files in the personal observer log directories
#
# REVISIONS and CHANGES
# 22/06/2003 V1.0 Thomas Forbriger
# 20/11/2007 V1.1 explains what it does
#
# ============================================================================
#
echo $OBS_STATUS_ALERT
echo $OBS_KEY_MESSAGE terminated unintendedly
TEE=/usr/bin/tee
MKDIR=/bin/mkdir
LS=/bin/ls
DATE=/bin/date
FIND=/usr/bin/find
WC=/usr/bin/wc
XARGS=/usr/bin/xargs
DU=/usr/bin/du
CUT=/usr/bin/cut
CAT=/bin/cat
GZIP=/usr/bin/gzip
GZIPOPTS=-fv9
# send a greeting message
echo "This is $0 at $($DATE)" | $TEE -a $OBS_LOG
echo "This is observer script $OBS_SCRIPT" | $TEE -a $OBS_LOG
(
echo '$Id$'
echo Action:
echo compress all files in the personal observer log directory
echo files are compressed by $GZIP $GZIPOPTS
) | $TEE -a $OBS_LOG
# set ALERT status in case of accident
echo $OBS_STATUS_ALERT
echo "$OBS_KEY_MESSAGE unintended termination"
# find name of base directory for all log files
LOGbasedir=$(dirname $OBS_LOG_DIR)
echo log base directory: | $TEE -a $OBS_LOG
echo $LOGbasedir | $TEE -a $OBS_LOG
# find not-compressed log files and apply compression
$FIND $LOGbasedir -name \*.log | \
$XARGS --no-run-if-empty $GZIP $GZIPOPTS 2>&1 | \
$TEE -a $OBS_LOG
# check total size of log files
sizeoflogs=$($DU -sm $LOGbasedir | $CUT -f 1)
echo total size of logs: $sizeoflogs MByte | $TEE -a $OBS_LOG
echo $OBS_KEY_MESSAGE total size of logs: $sizeoflogs MByte
echo $OBS_STATUS_OK
if test $sizeoflogs -gt 100
then
echo $OBS_STATUS_NOTICE
fi
# ----- END OF ziplog -----
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment