Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Apress committed Oct 14, 2016
0 parents commit a5f5ce7
Show file tree
Hide file tree
Showing 60 changed files with 3,525 additions and 0 deletions.
Binary file added 4271.pdf
Binary file not shown.
Binary file added 4283.pdf
Binary file not shown.
Binary file added 9781430218418.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions Chapter_03/epoch_days
@@ -0,0 +1,27 @@
#!/bin/bash

#
# This script outputs the number of days since 1/1/1970
#
seconds_gnu_epoch=`date +'%s'`

# This is the equation that works for the Gregorian calendar for years from
# 1582 through 9999. If you need to calculate something else, you'll need
# a new equation.
epoch_days=$(((1969*365)+(1969/4)-(1969/100)+(1969/400)+(14*306001/10000)+1))

day=`date +'%d'`
month=`date +'%m' | sed 's/0*//'`
year=`date +'%Y'`

if [ $month -gt 2 ]
then
month=$((month+1))
else
month=$((month+13))
year=$((year-1))
fi

today_days=$(((year*365)+(year/4)-(year/100)+(year/400)+(month*306001/10000)+day))
days_since_epoch=$((today_days-epoch_days))
echo $days_since_epoch
40 changes: 40 additions & 0 deletions Chapter_03/epoch_seconds
@@ -0,0 +1,40 @@
#!/bin/sh

#
# This script outputs the number of seconds since 1/1/1970
#
seconds_gnu_epoch=`date +'%s'`

# This is the equation that works for the Gregorian calendar for years from
# 1582 through 9999. If you need to calculate something else, you'll need
# a new equation.
#Date_days=(Year*365)+(Year/4)-(Year/100)+(Year/400)+(Month*306001/10000)+(Day)
# If the month is greater than 2, add 1 to the month. Otherwise, for Jan/Feb,
# add 13 to the month and subtract 1 from the year. The following is the
# equation for Jan 1, 1970.
epoch_days=$(((1969*365)+(1969/4)-(1969/100)+(1969/400)+(14*306001/10000)+1))

hour=`date +'%k'`
minute=`date +'%M'`
second=`date +'%S'`

day=`date +'%d'`
month=`date +'%m'`
year=`date +'%Y'`

if [ $month -gt 2 ]
then
month=$(($month+1))
else
month=$(($month+13))
year=$(($year-1))
fi

today_days=$((($year*365)+($year/4)-($year/100)+($year/400)+($month*306001/10000)+$day))
days_since_epoch=$(($today_days-$epoch_days))
seconds_since_epoch=`echo "($days_since_epoch*86400)+($hour*3600)+($minute*60)+$second" | bc`
# There will likely be a difference in the following two values. The GNU
# time in seconds is calculated based on GMT instead of the timezone you
# are currently in. Mine, being pacific time is currently calculating with
# a difference of exactly 8 hr.
echo $seconds_since_epoch
40 changes: 40 additions & 0 deletions Chapter_03/time2notify
@@ -0,0 +1,40 @@
#!/bin/sh

#
# This script is the basic structure for performing or not performing a
# task based on the hour of the day or the day of the week.
# The endpoints in the range are inclusive. 0-6 and 0-23 are will notify
# all the time.
#
if [ $# -ne 4 ]
then
echo Usage: $0 {day begin} {day end} {hour begin} {hour end}
echo " Days are 0-6 where 0 is Sunday."
echo " Hours are 0-23."
exit
fi

DAY_BEGIN=$1
DAY_END=$2
HOUR_BEGIN=$3
HOUR_END=$4

#
# Setup variables for current day and hour for paging times
#

# day of week 0-6 where 0=Sunday
DAY=`date +%w`
# hour of day in the form of 0-23
HOUR=`date +%H`

if [ $DAY -ge $DAY_BEGIN -a $DAY -le $DAY_END -a $HOUR -ge $HOUR_BEGIN -a $HOUR -le $HOUR_END ]
then
# traditionally a good return or in this case, a yes.
echo "It is time to notify"
echo 0
else
# traditionally a bad return or in this case, a no.
echo "It is not time to notify"
echo 1
fi
51 changes: 51 additions & 0 deletions Chapter_05/adm_opts
@@ -0,0 +1,51 @@
#
# a function that is sourced into your environment that will
# perform certain tasks based on the switches it is passed.
#

jkl () {

Usage="Usage: \n \
\tjkl [-hlkntdx] \n \
\t\t[-h] \tThis usage text.\n \
\t\t[-l] \tGo to system log directory with ls. \n \
\t\t[-k] \tDisplay disk utilization \n \
\t\t[-n] \tDisplay network connectivity via less\n \
\t\t[-t] \tDisplay top cpu consuming processes \n \
\t\t[-d] \tTurn on debug (set -x) information.\n \
\t\t[-x] \tTurn off debug (set +x) information.\n"
UNAME=`uname -n`
DATE=`date '+%y%m'`

if [ "$#" -lt 1 ]
then
echo -e $Usage
fi

OPTIND=1
while getopts hlkntdx ARGS
do
case $ARGS in
l) if [ -d /var/log ] ; then
cd /var/log
/bin/ls
fi
;;
k) df -k
;;
n) netstat -a | less
;;
t) ps -eo user,pid,ppid,pcpu,cmd | sort -rn +3 | head -n 5
;;
d) set -x
;;
x) set +x
;;
h) echo -e $Usage
;;
*) echo -e $Usage
#return
;;
esac
done
}
3 changes: 3 additions & 0 deletions Chapter_05/myapp.sh
@@ -0,0 +1,3 @@
MAJOR_VER=4
MINOR_VER=3
DOT_VER=15
91 changes: 91 additions & 0 deletions Chapter_05/use_opts
@@ -0,0 +1,91 @@

#
# a function to be sourced into your shell environment that performs
# various tasks based on the switches it is passed.
#

APPHOME=/home/rbpeters/scripts/apphome

if [ ! -f $APPHOME/myapp.sh ]
then
echo "Myapp is not installed on this system so jkl is not functional"
return 1
fi

jkl () {

Usage="Usage: \n \
\t$0 [-lf:bmcdxh] \n \
\t\t[-h] \tThis usage text.\n \
\t\t[-f] \tcat specified file. \n \
\t\t[-l] \tGo to application log directory with ls. \n \
\t\t[-b] \tGo to application bin directory. \n \
\t\t[-c] \tGo to application config directory.\n \
\t\t[-m] \tGo to application log directory and more log file.\n \
\t\t[-d] \tTurn on debug information.\n \
\t\t[-x] \tTurn off debug information.\n"
LOG=log
CFG=config
BIN=bin
APPLOG=myapp_log
UNAME=`uname -n`
DATE=`date '+%y%m'`
MYAPP_ID=$APPHOME/myapp.sh
######################################################################
major=`egrep "^MAJOR_VER=" $MYAPP_ID | cut -d"=" -f2`
minor=`egrep "^MINOR_VER=" $MYAPP_ID | cut -d"=" -f2`
dot=`egrep "^DOT_VER=" $MYAPP_ID | cut -d"=" -f2`
######################################################################
APPDIR=$APPHOME/myapp.$major.$minor.$dot
LOGDIR=$APPHOME/myapp.$major.$minor.$dot/log
CFGDIR=$APPHOME/myapp.$major.$minor.$dot/config
BINDIR=$APPHOME/myapp.$major.$minor.$dot/bin
######################################################################

if [ "$#" -lt 1 ]
then
echo -e $Usage
fi

OPTIND=1
while getopts lf:bmcdxh ARGS
do
case $ARGS in
l) if [ -d $LOGDIR ] ; then
cd $LOGDIR
/bin/ls
fi
;;
f) FILE=$OPTARG
if [ -f $FILE ]
then
cat $FILE
else
echo $FILE not found. Please try again.
fi
;;
b) if [ -d $BINDIR ] ; then
cd $BINDIR
fi
;;
m) if [ -d $LOGDIR ] ; then
cd $LOGDIR
/bin/more $APPLOG
fi
;;
c) if [ -d $CFGDIR ] ; then
cd $CFGDIR
fi
;;
d) set -x
;;
x) set +x
;;
h) echo -e $Usage
;;
*) echo -e $Usage
#return
;;
esac
done
}
136 changes: 136 additions & 0 deletions Chapter_07/logwatch
@@ -0,0 +1,136 @@
#!/bin/sh
#
# logwatch watches log files for specified strings and notifies
#
# This must run all the time and have a configurable sleep time between runs.
# It must run all the time because it holds the current and previous lenght
# of each monitored file in memory.
#
debug=0
#
# get the filecount information
#
DELAY=10
LOGCHKS="/home/rbpeters/scripts/testlog:somethinG%20dumb:rbp:warn"

while :
do
LOGTOCHK_COUNT=0
for LOGTOCHK in `echo $LOGCHKS`
do
#
# Get all the values from the config file
#
logfile=`echo $LOGTOCHK | cut -d: -f1`
strings="`echo $LOGTOCHK | awk -F: '{print $2}'`"
# Replace the %20's with spaces
strings="`echo $strings | sed -e \"s/%20/ /g\"`"
exceptions=`echo $LOGTOCHK | cut -d: -f3`
# Replace the %20's with spaces
exceptions="`echo $exceptions | sed -e \"s/%20/ /g\"`"
notify=`echo $LOGTOCHK | cut -d: -f4`
test $debug -gt 0 && echo "INFO: Logfile: $logfile Strings: $strings Exceptions: $exceptions Notify: $notify"
#
# LOGTOCHK_COUNT is the number of the entry in the config file. If there
# are 2 log files that are configured to be watched, this value will
# be 1 and 2. Makes watching the same log file multiple times unique.
#
LOGTOCHK_COUNT=`expr $LOGTOCHK_COUNT + 1`
#
# The LOGNAME is just the name of the log file with /'s and .'s replaced
# with _'s. Both the LOGNAME and LOGTOCHK_COUNT are used later to build
# on-the-fly (indirect) variable names.
#
LOGNAME=`echo $logfile | sed -e s/\\\//_/g`
LOGNAME=`echo $LOGNAME | sed -e s/\\\./_/g`
#
# Check to see if the line count of the file is null. This indicates
# that it is the first run through and the counters should be reset.
#
if [ "`eval echo '$COUNT'${LOGNAME}_$LOGTOCHK_COUNT`" = "" ]
then
eval BASE${LOGNAME}_$LOGTOCHK_COUNT=`wc -l $logfile | awk '{ print $1 }'`
fi
#
# Get the line count of the log file
#
if [ -f $logfile ]
then
eval COUNT${LOGNAME}_$LOGTOCHK_COUNT=`wc -l $logfile | awk '{ print $1 }'`
else
test $debug -gt 0 && echo "$logfile does not exist"
echo "You might consider putting in some notification to let you know"
echo "that the file doesn't exist"
fi
#
# If the count is greater than the base value, get the number of lines
# that is greater, tail those lines and egrep for the desired values.
# Also, remove any lines that have the exeptions defined.
#
if [ `eval echo '$COUNT'${LOGNAME}_$LOGTOCHK_COUNT` -gt `eval echo '$BASE'${LOGNAME}_$LOGTOCHK_COUNT` ]
then
LINES=`eval expr '$COUNT'${LOGNAME}_$LOGTOCHK_COUNT - '$BASE'${LOGNAME}_$LOGTOCHK_COUNT`
eval BASE${LOGNAME}_$LOGTOCHK_COUNT='$COUNT'${LOGNAME}_$LOGTOCHK_COUNT

if [ "$exceptions" != "" ]
then
MSGS=`tail -$LINES $logfile | egrep -i "$strings" | egrep -iv "$exceptions"`
test $debug -gt 0 && echo "MSGS is $MSGS"
else
MSGS=`tail -$LINES $logfile | egrep -i "$strings"`
test $debug -gt 0 && echo "MSGS is $MSGS"
fi

#
# If there are any messages found, send the notification based on the
# desired type, warn/error.
#
if [ ! -z "$MSGS" ]
then
if [ "$notify" != "error" ]
then
echo "Replace this with the warning notification code."
else
echo "Replace this with the error notification code."
fi
fi
#
# If the line count of the file is less than the base value (the value
# from the previous loop through the code), reset the base value. This
# is likely because the log just rolled and is now starting over. Also
# check for the strings since messages may be missed when the log rolls.
#
elif [ `eval echo '$COUNT'${LOGNAME}_$LOGTOCHK_COUNT` -lt `eval echo '$BASE'${LOGNAME}_$LOGTOCHK_COUNT` ]
then
if [ "$exceptions" != "" ]
then
MSGS=`egrep -i "\"$strings\"" $logfile | egrep -iv "$exceptions"`
test $debug -gt 0 && echo "MSGS is $MSGS"
else
MSGS=`egrep -i "$strings" $logfile`
test $debug -gt 0 && echo "MSGS is $MSGS"
fi
#
# If there are any messages found, send the notification based on the
# desired type, warn/error.
#
if [ ! -z "$MSGS" ]
then
if [ "$notify" != "error" ]
then
echo "Replace this with the warning notification code."
else
echo "Replace this with the error notification code."
fi
fi
# This resets the tracked size of the log if the log gets smaller
eval BASE${LOGNAME}_$LOGTOCHK_COUNT='$COUNT'${LOGNAME}_$LOGTOCHK_COUNT
#
# If there is no change in the file, do nothing
#
else
test $debug -gt 0 && echo "No change in size of $logfile"
fi
done
sleep $DELAY
done

0 comments on commit a5f5ce7

Please sign in to comment.