initial commit
This commit is contained in:
177
README.txt
Executable file
177
README.txt
Executable file
@@ -0,0 +1,177 @@
|
|||||||
|
README File for Mastering UNIX Shell Scripting - Second Edition
|
||||||
|
|
||||||
|
Author: Randal K. Michael
|
||||||
|
|
||||||
|
========
|
||||||
|
Publisher's note: Please note when downloading the compressed
|
||||||
|
files from our website, some versions of IE may rename the file.
|
||||||
|
If your browser renames the archive file, please make sure you
|
||||||
|
change the name back before extracting the original files
|
||||||
|
from the archive.
|
||||||
|
========
|
||||||
|
|
||||||
|
|
||||||
|
The Mastering_UNIX_SS.tar.Z and Mastering_UNIX_SS.tar.gz files
|
||||||
|
are compressed tar archives that contain sample code from the
|
||||||
|
second edition of Mastering UNIX Shell Scripting. Please note
|
||||||
|
that there is an errata (a description of an error in the book's
|
||||||
|
edited code) posted in the next paragraph, followed by some notes.
|
||||||
|
Please be assured that the shell scripts and functions in this
|
||||||
|
package were never affected by the mistake in the book's edited
|
||||||
|
code pertaining to the word "Bash".
|
||||||
|
|
||||||
|
I hope you gain some knowledge from this book, shell scripts,
|
||||||
|
and the set of functions packaged here.
|
||||||
|
|
||||||
|
ERRATA:
|
||||||
|
|
||||||
|
(This is the errata for the first 4,000 copies of this book.)
|
||||||
|
|
||||||
|
During the course of editing this book, a global search-and-replace was
|
||||||
|
conducted that changed all instances of "bash" to "Bash" in the code.
|
||||||
|
Given that UNIX is case-sensitive, references in the code should be to
|
||||||
|
"bash". Note that all the original code available at the Downloads tab
|
||||||
|
of this page was never affected.
|
||||||
|
|
||||||
|
The result was limited to the first line of Bash shell scripts,
|
||||||
|
defined as:
|
||||||
|
|
||||||
|
#!/bin/Bash
|
||||||
|
|
||||||
|
Instead, the correct declaration should be:
|
||||||
|
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
In the book you may also see some Bash shell script filenames
|
||||||
|
incorrectly named with a ".Bash" filename extension instead of
|
||||||
|
the correct ".bash" filename extension.
|
||||||
|
|
||||||
|
For example, the filename:
|
||||||
|
|
||||||
|
my_shell_script.Bash
|
||||||
|
|
||||||
|
should actually be:
|
||||||
|
|
||||||
|
my_shell_script.bash
|
||||||
|
|
||||||
|
Thanks for your understanding.
|
||||||
|
|
||||||
|
|
||||||
|
AUTHOR NOTES:
|
||||||
|
|
||||||
|
I) Extracting the shell scripts and functions to your system:
|
||||||
|
|
||||||
|
The Mastering_UNIX_SS.tar.Z and Mastering_UNIX_SS.tar.gz files
|
||||||
|
are compressed tar archives of the shell scripts and functions
|
||||||
|
in the book, and some extras in a few areas. When you extract
|
||||||
|
this archive, you will see chapter directories that contain the
|
||||||
|
code in each chapter in the book that has downloadable code.
|
||||||
|
For example, when you untar the archive, the following
|
||||||
|
subdirectories will be created (assuming you have the file
|
||||||
|
permissions, that is).
|
||||||
|
|
||||||
|
chapter1
|
||||||
|
chapter2
|
||||||
|
chapter4
|
||||||
|
chapter5
|
||||||
|
|
||||||
|
Notice there is not a "chapter3" subdirectory. This is because there
|
||||||
|
is no downloadable code in this chapter.
|
||||||
|
|
||||||
|
To extract the archive, first create a directory, or just change
|
||||||
|
the directory to where you want extract the archive.
|
||||||
|
|
||||||
|
Option 1: Extracting the compressed Mastering_UNIX_SS.tar.Z archive.
|
||||||
|
|
||||||
|
Copy the Mastering_UNIX_SS.tar.Z file to your desired top-level
|
||||||
|
directory, and then execute the following commands.
|
||||||
|
|
||||||
|
The following uncompress command will uncompress the archive image file:
|
||||||
|
|
||||||
|
uncompress ./Mastering_UNIX_SS.tar.Z
|
||||||
|
|
||||||
|
The following tar command will untar the archive image file to the
|
||||||
|
current directory:
|
||||||
|
|
||||||
|
tar -xvpf ./Mastering_UNIX_SS.tar
|
||||||
|
|
||||||
|
Option 2: Extracting the gzipped Mastering_UNIX_SS.tar.gz archive.
|
||||||
|
|
||||||
|
Copy the Mastering_UNIX_SS.tar.gz file to your desired top-level
|
||||||
|
directory, and then execute the following commands.
|
||||||
|
|
||||||
|
The following gunzip command will uncompress the archive image file:
|
||||||
|
|
||||||
|
gunzip ./Mastering_UNIX_SS.tar.gz
|
||||||
|
|
||||||
|
The following tar command will untar the archive image file to the
|
||||||
|
current directory:
|
||||||
|
|
||||||
|
tar -xvpf ./Mastering_UNIX_SS.tar
|
||||||
|
|
||||||
|
Both archives contain the same information.
|
||||||
|
|
||||||
|
II) Using the echo command in Bash shell:
|
||||||
|
|
||||||
|
The echo command executes differently depending on the UNIX
|
||||||
|
shell you are executing it in. A specific situation is using the
|
||||||
|
echo command's backslash operators, such as:
|
||||||
|
|
||||||
|
echo "\n" # To produce a newline
|
||||||
|
|
||||||
|
echo "\c" # To continue on the same line without a
|
||||||
|
# carriage return or line feed
|
||||||
|
|
||||||
|
and many others.
|
||||||
|
|
||||||
|
Of all the methods covered in this book to properly use the
|
||||||
|
echo command when Bash shell is involved, the most reliable
|
||||||
|
method is aliasing the echo command for use in a Bash shell
|
||||||
|
environment. Many Korn shell scripts will execute in a default
|
||||||
|
Bash shell if /bin/ksh is not installed.
|
||||||
|
|
||||||
|
By adding the following case statement to the top of your code, it
|
||||||
|
will not be necessary to use echo -e when dealing with the echo
|
||||||
|
command's backslash operators if your ksh script should happen
|
||||||
|
to execute in a Bash shell. This is common on many Linux systems.
|
||||||
|
You can also use this in your Bash scripts if you want to omit
|
||||||
|
adding the -e to your echo statement when writing your shell scripts.
|
||||||
|
|
||||||
|
case $(basename $SHELL) in
|
||||||
|
bash) alias echo="echo -e"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
Now anytime the echo command is used in a script that is
|
||||||
|
executing in Bash shell, echo -e will be substituted,
|
||||||
|
enabling the backslash operators \n, \c, \t, and so on.
|
||||||
|
|
||||||
|
III) How individual functions are separated into individual files:
|
||||||
|
|
||||||
|
When this book's script/function archive is installed to your system,
|
||||||
|
each function that is its own file will have the following filename
|
||||||
|
format:
|
||||||
|
|
||||||
|
"function_" followed by the name of the actual function in the script.
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
If the real function name in the script or book is:
|
||||||
|
|
||||||
|
check_file_system
|
||||||
|
|
||||||
|
Then the filename for this individual function would be:
|
||||||
|
|
||||||
|
function_check_file_system
|
||||||
|
|
||||||
|
----
|
||||||
|
|
||||||
|
Thanks for buying my book. I hope the answer to every scripting problem
|
||||||
|
you encounter will be intuitively obvious when you finish
|
||||||
|
reading this second edition!
|
||||||
|
|
||||||
|
|
||||||
|
Cheers,
|
||||||
|
|
||||||
|
Randy
|
||||||
|
|
||||||
11
chapter1/function_elapsed_time
Executable file
11
chapter1/function_elapsed_time
Executable file
@@ -0,0 +1,11 @@
|
|||||||
|
elapsed_time ()
|
||||||
|
{
|
||||||
|
SEC=$1
|
||||||
|
(( SEC < 60 )) && echo "[Elapsed time: $SEC seconds]\c"
|
||||||
|
|
||||||
|
(( SEC >= 60 && SEC < 3600 )) && echo "[Elapsed time: $(( SEC / 60 )) \
|
||||||
|
min $(( SEC % 60 )) sec]\c"
|
||||||
|
|
||||||
|
(( SEC > 3600 )) && echo "[Elapsed time: $(( SEC / 3600 )) hr \
|
||||||
|
$(( (SEC % 3600) / 60 )) min $(( (SEC % 3600) % 60 )) sec]\c"
|
||||||
|
}
|
||||||
26
chapter1/function_ping_host
Executable file
26
chapter1/function_ping_host
Executable file
@@ -0,0 +1,26 @@
|
|||||||
|
function ping_host
|
||||||
|
{
|
||||||
|
HOST=$1 # Grab the host to ping from ARG1.
|
||||||
|
PING_COUNT=3
|
||||||
|
PACKET_SIZE=54
|
||||||
|
|
||||||
|
# This next case statement executes the correct ping
|
||||||
|
# command based on the Unix flavor
|
||||||
|
|
||||||
|
case $(uname) in
|
||||||
|
|
||||||
|
AIX|OpenBSD|Linux)
|
||||||
|
ping -c${PING_COUNT} $HOST 2>/dev/null
|
||||||
|
;;
|
||||||
|
HP-UX)
|
||||||
|
ping $HOST $PACKET_SIZE $PING_COUNT 2>/dev/null
|
||||||
|
;;
|
||||||
|
SunOS)
|
||||||
|
ping -s $HOST $PACKET_SIZE $PING_COUNT 2>/dev/null
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
echo "\nERROR: Unsupported Operating System - $(uname)"
|
||||||
|
echo "\n\t. . .EXITING. . .\n"
|
||||||
|
exit 1
|
||||||
|
esac
|
||||||
|
}
|
||||||
26
chapter1/function_rotate_line
Executable file
26
chapter1/function_rotate_line
Executable file
@@ -0,0 +1,26 @@
|
|||||||
|
function rotate_line
|
||||||
|
{
|
||||||
|
INTERVAL=1 # Sleep time between "twirls"
|
||||||
|
TCOUNT="0" # For each TCOUNT the line twirls one increment
|
||||||
|
|
||||||
|
while : # Loop forever. . .until this function is killed
|
||||||
|
do
|
||||||
|
TCOUNT=`expr $TCOUNT + 1` # Increment the TCOUNT
|
||||||
|
|
||||||
|
case $TCOUNT in
|
||||||
|
"1") echo .-."\b\c"
|
||||||
|
sleep $INTERVAL
|
||||||
|
;;
|
||||||
|
"2") echo .\\."\b\c"
|
||||||
|
sleep $INTERVAL
|
||||||
|
;;
|
||||||
|
"3") echo "|\b\c"
|
||||||
|
sleep $INTERVAL
|
||||||
|
;;
|
||||||
|
"4") echo "/\b\c"
|
||||||
|
sleep $INTERVAL
|
||||||
|
;;
|
||||||
|
*) TCOUNT="0" ;; # Reset the TCOUNT to "0", zero.
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
}
|
||||||
33
chapter1/generic_rsync.bash
Executable file
33
chapter1/generic_rsync.bash
Executable file
@@ -0,0 +1,33 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
#
|
||||||
|
# SCRIPT: generic_rsync.bash
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 11/18/2007
|
||||||
|
# REV: 1.0
|
||||||
|
# PURPOSE: This is a generic shell script to copy files
|
||||||
|
# using rsync.
|
||||||
|
#
|
||||||
|
# set -n # Uncomment to check script syntax without execution
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
#
|
||||||
|
# REV LIST:
|
||||||
|
#
|
||||||
|
#
|
||||||
|
##############################################
|
||||||
|
# DEFINE FILES AND VARIABLES HERE
|
||||||
|
##############################################
|
||||||
|
|
||||||
|
# Define the source and destination files/directories
|
||||||
|
|
||||||
|
SOURCE_FL="/scripts/"
|
||||||
|
DESTIN_FL="booboo:/scripts"
|
||||||
|
|
||||||
|
##############################################
|
||||||
|
# BEGINNING OF MAIN
|
||||||
|
##############################################
|
||||||
|
|
||||||
|
# Start the rsync copy
|
||||||
|
|
||||||
|
rsync -avz "$SOURCE_FL" "$DESTIN_FL"
|
||||||
|
|
||||||
|
# End of generic_rsync.bash
|
||||||
11
chapter1/keyit.dsa
Executable file
11
chapter1/keyit.dsa
Executable file
@@ -0,0 +1,11 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
#
|
||||||
|
# SCRIPT: keyit.dsa
|
||||||
|
#
|
||||||
|
# PURPOSE: This script is used to set up DSA SSH keys. This script must
|
||||||
|
# be executed by the user who needs the keys setup.
|
||||||
|
|
||||||
|
REM_HOST=$1
|
||||||
|
|
||||||
|
cat $HOME/.ssh/id_dsa.pub | ssh $REM_HOST "cat >> ~/.ssh/authorized_keys"
|
||||||
|
|
||||||
11
chapter1/keyit.rsa
Executable file
11
chapter1/keyit.rsa
Executable file
@@ -0,0 +1,11 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
#
|
||||||
|
# SCRIPT: keyit.rsa
|
||||||
|
#
|
||||||
|
# PURPOSE: This script is used to set up RSA SSH keys.
|
||||||
|
# This script must be executed by the user who needs the keys setup.
|
||||||
|
|
||||||
|
REM_HOST=$1
|
||||||
|
|
||||||
|
cat $HOME/.ssh/id_rsa.pub | ssh $REM_HOST "cat >> ~/.ssh/authorized_keys"
|
||||||
|
|
||||||
50
chapter1/script.stub
Executable file
50
chapter1/script.stub
Executable file
@@ -0,0 +1,50 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
#
|
||||||
|
# SCRIPT: NAME_of_SCRIPT
|
||||||
|
# AUTHOR: AUTHORS_NAME
|
||||||
|
# DATE: DATE_of_CREATION
|
||||||
|
# REV: 1.1.A (Valid are A, B, D, T, Q, and P)
|
||||||
|
# (For Alpha, Beta, Dev, Test, QA, and Production)
|
||||||
|
#
|
||||||
|
# PLATFORM: (SPECIFY: AIX, HP-UX, Linux, OpenBSD, Solaris, other flavor,
|
||||||
|
# or Not platform dependent)
|
||||||
|
#
|
||||||
|
# REQUIREMENTS: If this script has requirements that need to be noted, this
|
||||||
|
# is the place to spell those requirements in detail.
|
||||||
|
#
|
||||||
|
# EXAMPLE: OpenSSH is required for this shell script to work.
|
||||||
|
#
|
||||||
|
# PURPOSE: Give a clear, and if necessary, long, description of the
|
||||||
|
# purpose of the shell script. This will also help you stay
|
||||||
|
# focused on the task at hand.
|
||||||
|
#
|
||||||
|
# REV LIST:
|
||||||
|
# DATE: DATE_of_REVISION
|
||||||
|
# BY: AUTHOR_of_MODIFICATION
|
||||||
|
# MODIFICATION: Describe what was modified, new features, etc--
|
||||||
|
#
|
||||||
|
#
|
||||||
|
# set -n # Uncomment to check script syntax, without execution.
|
||||||
|
# # NOTE: Do not forget to put the # comment back in or
|
||||||
|
# # the shell script will never execute!
|
||||||
|
# set -x # Uncomment to debug this shell script
|
||||||
|
#
|
||||||
|
##########################################################
|
||||||
|
# DEFINE FILES AND VARIABLES HERE
|
||||||
|
##########################################################
|
||||||
|
|
||||||
|
THIS_SCRIPT=$(basename $0)
|
||||||
|
|
||||||
|
##########################################################
|
||||||
|
# DEFINE FUNCTIONS HERE
|
||||||
|
##########################################################
|
||||||
|
|
||||||
|
|
||||||
|
##########################################################
|
||||||
|
# BEGINNING OF MAIN
|
||||||
|
##########################################################
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# End of script
|
||||||
|
|
||||||
72
chapter1/select_system_info_menu.bash
Executable file
72
chapter1/select_system_info_menu.bash
Executable file
@@ -0,0 +1,72 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
#
|
||||||
|
# SCRIPT: select_system_info_menu.bash
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 1/17/2008
|
||||||
|
# REV: 1.0
|
||||||
|
#
|
||||||
|
# PURPOSE: This shell script uses the shell's select
|
||||||
|
# command to create a menu to show system information
|
||||||
|
|
||||||
|
# Clear the screen
|
||||||
|
clear
|
||||||
|
|
||||||
|
# Display the menu title header
|
||||||
|
echo -e "\n\tSYSTEM INFORMATION MENU\n"
|
||||||
|
|
||||||
|
# Define the menu prompt
|
||||||
|
|
||||||
|
PS3="Select an option and press Enter: "
|
||||||
|
|
||||||
|
# The select command defines what the menu
|
||||||
|
# will look like
|
||||||
|
|
||||||
|
select i in OS Host Filesystems Date Users Quit
|
||||||
|
do
|
||||||
|
case $i in
|
||||||
|
OS) echo
|
||||||
|
uname
|
||||||
|
;;
|
||||||
|
Host) echo
|
||||||
|
hostname
|
||||||
|
;;
|
||||||
|
Filesystems)
|
||||||
|
echo
|
||||||
|
df -k | more
|
||||||
|
;;
|
||||||
|
Date) echo
|
||||||
|
date
|
||||||
|
;;
|
||||||
|
Users) echo
|
||||||
|
who
|
||||||
|
;;
|
||||||
|
Quit) break
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
# Setting the select command's REPLY variable
|
||||||
|
# to NULL causes the menu to be redisplayed
|
||||||
|
|
||||||
|
REPLY=
|
||||||
|
|
||||||
|
# Pause before redisplaying the menu
|
||||||
|
|
||||||
|
echo -e "\nPress Enter to Continue...\c"
|
||||||
|
read
|
||||||
|
|
||||||
|
# Ready to redisplay the menu again
|
||||||
|
|
||||||
|
# clear the screen
|
||||||
|
|
||||||
|
clear
|
||||||
|
|
||||||
|
# Display the menu title header
|
||||||
|
|
||||||
|
echo -e "\n\tSYSTEM INFORMATION MENU\n"
|
||||||
|
|
||||||
|
done
|
||||||
|
|
||||||
|
# Clear the screen before exiting
|
||||||
|
|
||||||
|
clear
|
||||||
|
|
||||||
180
chapter1/test_string.ksh
Executable file
180
chapter1/test_string.ksh
Executable file
@@ -0,0 +1,180 @@
|
|||||||
|
#!/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: test_string.ksh
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# REV: 1.0.D - Used for developement
|
||||||
|
# DATE: 10/15/2007
|
||||||
|
# PLATFORM: Not Platform Dependent
|
||||||
|
#
|
||||||
|
# PURPOSE: This script is used to test a character
|
||||||
|
# string, or variable, for its composition.
|
||||||
|
# Examples include numeric, lowercase or uppercase
|
||||||
|
# characters, alpha-numeric characters, and IP address.
|
||||||
|
#
|
||||||
|
# REV LIST:
|
||||||
|
#
|
||||||
|
#
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
# set -n # Uncomment to verify syntax without any execution.
|
||||||
|
# # REMEMBER: Put the comment back or the script will
|
||||||
|
# # NOT EXECUTE!
|
||||||
|
#
|
||||||
|
####################################################
|
||||||
|
############## DEFINE FUNCTIONS HERE ###############
|
||||||
|
####################################################
|
||||||
|
|
||||||
|
test_string ()
|
||||||
|
{
|
||||||
|
# This function tests a character string
|
||||||
|
|
||||||
|
# Must have one argument ($1)
|
||||||
|
|
||||||
|
if (( $# != 1 ))
|
||||||
|
then
|
||||||
|
# This error would be a programming error
|
||||||
|
|
||||||
|
print "ERROR: $(basename $0) requires one argument"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
# Assign arg1 to the variable --> STRING
|
||||||
|
|
||||||
|
STRING=$1
|
||||||
|
|
||||||
|
# This is where the string test begins
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
case $STRING in
|
||||||
|
|
||||||
|
+([0-9]).+([0-9]).+([0-9]).+([0-9]))
|
||||||
|
# Testing for an IP address - valid and invalid
|
||||||
|
INVALID=FALSE
|
||||||
|
|
||||||
|
# Separate the integer portions of the "IP" address
|
||||||
|
# and test to ensure that nothing is greater than 255
|
||||||
|
# or it is an invalid IP address.
|
||||||
|
|
||||||
|
for i in $(echo $STRING | awk -F . '{print $1, $2, $3, $4}')
|
||||||
|
do
|
||||||
|
if (( i > 255 ))
|
||||||
|
then
|
||||||
|
INVALID=TRUE
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
case $INVALID in
|
||||||
|
TRUE) print 'INVALID_IP_ADDRESS'
|
||||||
|
;;
|
||||||
|
FALSE) print 'VALID_IP_ADDRESS'
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
;;
|
||||||
|
+([0-1])) # Testing for 0-1 only
|
||||||
|
print 'BINARY_OR_POSITIVE_INTEGER'
|
||||||
|
;;
|
||||||
|
+([0-7])) # Testing for 0-7 only
|
||||||
|
print 'OCTAL_OR_POSITIVE_INTEGER'
|
||||||
|
;;
|
||||||
|
+([0-9])) # Check for an integer
|
||||||
|
print 'INTEGER'
|
||||||
|
;;
|
||||||
|
+([-0-9])) # Check for a negative whole number
|
||||||
|
print 'NEGATIVE_WHOLE_NUMBER'
|
||||||
|
;;
|
||||||
|
+([0-9]|[.][0-9]))
|
||||||
|
# Check for a positive floating point number
|
||||||
|
print 'POSITIVE_FLOATING_POINT'
|
||||||
|
;;
|
||||||
|
+(+[0-9][.][0-9]))
|
||||||
|
# Check for a positive floating point number
|
||||||
|
# with a + prefix
|
||||||
|
print 'POSITIVE_FLOATING_POINT'
|
||||||
|
;;
|
||||||
|
+(-[0-9][.][0-9]))
|
||||||
|
# Check for a negative floating point number
|
||||||
|
print 'NEGATIVE_FLOATING_POINT'
|
||||||
|
;;
|
||||||
|
+([-.0-9]))
|
||||||
|
# Check for a negative floating point number
|
||||||
|
print 'NEGATIVE_FLOATING_POINT'
|
||||||
|
;;
|
||||||
|
+([+.0-9]))
|
||||||
|
# Check for a positive floating point number
|
||||||
|
print 'POSITIVE_FLOATING_POINT'
|
||||||
|
;;
|
||||||
|
+([a-f])) # Test for hexidecimal or all lowercase characters
|
||||||
|
print 'HEXIDECIMAL_OR_ALL_LOWERCASE'
|
||||||
|
;;
|
||||||
|
+([a-f]|[0-9])) # Test for hexidecimal or all lowercase characters
|
||||||
|
print 'HEXIDECIMAL_OR_ALL_LOWERCASE_ALPHANUMERIC'
|
||||||
|
;;
|
||||||
|
+([A-F])) # Test for hexidecimal or all uppercase characters
|
||||||
|
print 'HEXIDECIMAL_OR_ALL_UPPERCASE'
|
||||||
|
;;
|
||||||
|
+([A-F]|[0-9])) # Test for hexidecimal or all uppercase characters
|
||||||
|
print 'HEXIDECIMAL_OR_ALL_UPPERCASE_ALPHANUMERIC'
|
||||||
|
;;
|
||||||
|
+([a-f]|[A-F]))
|
||||||
|
# Testing for hexidecimal or mixed-case characters
|
||||||
|
print 'HEXIDECIMAL_OR_MIXED_CASE'
|
||||||
|
;;
|
||||||
|
+([a-f]|[A-F]|[0-9]))
|
||||||
|
# Testing for hexidecimal/alpha-numeric strings only
|
||||||
|
print 'HEXIDECIMAL_OR_MIXED_CASE_ALPHANUMERIC'
|
||||||
|
;;
|
||||||
|
+([a-z])) # Testing for all lowercase characters only
|
||||||
|
print 'ALL_LOWERCASE'
|
||||||
|
;;
|
||||||
|
+([A-Z])) # Testing for all uppercase numbers only
|
||||||
|
print 'ALL_UPPERCASE'
|
||||||
|
;;
|
||||||
|
+([a-z]|[A-Z]))
|
||||||
|
# Testing for mixed case alpha strings only
|
||||||
|
print 'MIXED_CASE'
|
||||||
|
;;
|
||||||
|
+([a-z]|[A-Z]|[0-9]))
|
||||||
|
# Testing for any alpha-numeric string only
|
||||||
|
print 'ALPHA-NUMERIC'
|
||||||
|
;;
|
||||||
|
*) # None of the tests matched the string coposition
|
||||||
|
print 'INVALID_STRING_COMPOSITION'
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################################
|
||||||
|
|
||||||
|
usage ()
|
||||||
|
{
|
||||||
|
echo "\nERROR: Please supply one character string or variable\n"
|
||||||
|
echo "USAGE: $THIS_SCRIPT {character string or variable}\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################################
|
||||||
|
############# BEGINNING OF MAIN ####################
|
||||||
|
####################################################
|
||||||
|
|
||||||
|
# Query the system for the name of this shell script.
|
||||||
|
# This is used for the "usage" function.
|
||||||
|
|
||||||
|
THIS_SCRIPT=$(basename $0)
|
||||||
|
|
||||||
|
# Check for exactly one command-line argument
|
||||||
|
|
||||||
|
if (( $# != 1 ))
|
||||||
|
then
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Everything looks okay if we got here. Assign the
|
||||||
|
# single command-line argument to the variable "STRING"
|
||||||
|
|
||||||
|
STRING=$1
|
||||||
|
|
||||||
|
# Call the "test_string" function to test the composition
|
||||||
|
# of the character string stored in the $STRING variable.
|
||||||
|
|
||||||
|
test_string $STRING
|
||||||
|
|
||||||
|
# End of script
|
||||||
21
chapter10/function_mon_proc_end
Executable file
21
chapter10/function_mon_proc_end
Executable file
@@ -0,0 +1,21 @@
|
|||||||
|
mon_proc_end ()
|
||||||
|
{
|
||||||
|
END_RC="0"
|
||||||
|
until (( END_RC != 0 ))
|
||||||
|
do
|
||||||
|
ps aux | grep -v "grep $PROCESS" | grep -v $SCRIPT_NAME \
|
||||||
|
| grep $PROCESS >/dev/null 2>&1
|
||||||
|
|
||||||
|
END_RC=$? # Check the Return Code!!
|
||||||
|
sleep 1 # Needed to reduce CPU load!
|
||||||
|
done
|
||||||
|
|
||||||
|
echo 'N' # Turn the RUN flag off
|
||||||
|
|
||||||
|
# Grab a Timestamp
|
||||||
|
TIMESTAMP=$(date +%D@%T)
|
||||||
|
|
||||||
|
echo "END PROCESS: $PROCESS ended ==> $TIMESTAMP" >> $LOGFILE &
|
||||||
|
echo "END PROCESS: $PROCESS ended ==> $TIMESTAMP" > $TTY
|
||||||
|
}
|
||||||
|
|
||||||
21
chapter10/function_mon_proc_start
Executable file
21
chapter10/function_mon_proc_start
Executable file
@@ -0,0 +1,21 @@
|
|||||||
|
mon_proc_start ()
|
||||||
|
{
|
||||||
|
START_RC="-1" # Initialize to -1
|
||||||
|
until (( START_RC == 0 ))
|
||||||
|
do
|
||||||
|
ps aux | grep -v "grep $PROCESS" | grep -v $SCRIPT_NAME \
|
||||||
|
| grep $PROCESS >/dev/null 2>&1
|
||||||
|
|
||||||
|
START_RC=$? # Check the Return Code!!!
|
||||||
|
sleep 1 # Needed to reduce CPU load!
|
||||||
|
done
|
||||||
|
|
||||||
|
echo 'Y' # Turn the RUN flag on
|
||||||
|
|
||||||
|
# Grab the timestamp
|
||||||
|
TIMESTAMP=$(date +%D@%T)
|
||||||
|
|
||||||
|
echo "START PROCESS: $PROCESS began ==> $TIMESTAMP" >> $LOGFILE &
|
||||||
|
echo "START PROCESS: $PROCESS began ==> $TIMESTAMP" > $TTY
|
||||||
|
}
|
||||||
|
|
||||||
12
chapter10/function_post_event_script
Executable file
12
chapter10/function_post_event_script
Executable file
@@ -0,0 +1,12 @@
|
|||||||
|
post_event_script ()
|
||||||
|
{
|
||||||
|
# Put anything that you want to execute AFTER the
|
||||||
|
# monitored process ENDS in this function
|
||||||
|
|
||||||
|
: # No-OP - Need as a placeholder for an empty function
|
||||||
|
# Comment Out the Above colon, ':'
|
||||||
|
|
||||||
|
POST_RC=$?
|
||||||
|
return $POST_RC
|
||||||
|
}
|
||||||
|
|
||||||
12
chapter10/function_pre_event_script
Executable file
12
chapter10/function_pre_event_script
Executable file
@@ -0,0 +1,12 @@
|
|||||||
|
pre_event_script ()
|
||||||
|
{
|
||||||
|
# Put anything that you want to execute BEFORE the
|
||||||
|
# monitored process STARTS in this function
|
||||||
|
|
||||||
|
: # No-OP - Needed as a placeholder for an empty function
|
||||||
|
# Comment Out the Above colon, ':'
|
||||||
|
|
||||||
|
PRE_RC=$?
|
||||||
|
return $PRE_RC
|
||||||
|
}
|
||||||
|
|
||||||
186
chapter10/function_proc_watch
Executable file
186
chapter10/function_proc_watch
Executable file
@@ -0,0 +1,186 @@
|
|||||||
|
proc_watch ()
|
||||||
|
{
|
||||||
|
# set -x # Uncomment to debug this function
|
||||||
|
# This function does all of the process monitoring!
|
||||||
|
|
||||||
|
while : # Loop Forever!!
|
||||||
|
do
|
||||||
|
case $RUN in
|
||||||
|
'Y')
|
||||||
|
# This will run the startup_event_script, which is a function
|
||||||
|
if [[ $RUN_STARTUP_EVENT = 'Y' ]]
|
||||||
|
then
|
||||||
|
echo "STARTUP EVENT: Executing Startup Event Script..."\
|
||||||
|
> $TTY
|
||||||
|
echo "STARTUP EVENT: Executing Startup Event Script..."\
|
||||||
|
>> $LOGFILE
|
||||||
|
|
||||||
|
startup_event_script # USER DEFINED FUNCTION!!!
|
||||||
|
RC=$? # Check the Return Code!!
|
||||||
|
if (( "RC" == 0 ))
|
||||||
|
then
|
||||||
|
echo "SUCCESS: Startup Event Script Completed RC -\
|
||||||
|
${RC}" > $TTY
|
||||||
|
echo "SUCCESS: Startup Event Script Completed RC -\
|
||||||
|
${RC}" >> $LOGFILE
|
||||||
|
|
||||||
|
else
|
||||||
|
echo "FAILURE: Startup Event Script FAILED RC -\
|
||||||
|
${RC}" > $TTY
|
||||||
|
echo "FAILURE: Startup Event Script FAILED RC -\
|
||||||
|
${RC}" >> $LOGFILE
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
integer PROC_COUNT='-1' # Reset the Counters
|
||||||
|
integer LAST_COUNT='-1'
|
||||||
|
# Loop until the process(es) end(s)
|
||||||
|
|
||||||
|
until (( "PROC_COUNT" == 0 ))
|
||||||
|
do
|
||||||
|
# This function is a Co-Process. $BREAK checks to see if
|
||||||
|
# "Program Interrupt" has taken place. If so BREAK will
|
||||||
|
# be 'Y' and we exit both the loop and function.
|
||||||
|
|
||||||
|
read BREAK
|
||||||
|
if [[ $BREAK = 'Y' ]]
|
||||||
|
then
|
||||||
|
return 3
|
||||||
|
fi
|
||||||
|
|
||||||
|
PROC_COUNT=$(ps aux | grep -v "grep $PROCESS" \
|
||||||
|
| grep -v $SCRIPT_NAME \
|
||||||
|
| grep $PROCESS | wc -l) >/dev/null 2>&1
|
||||||
|
|
||||||
|
if (( "LAST_COUNT" > 0 && "LAST_COUNT" != "PROC_COUNT" ))
|
||||||
|
then
|
||||||
|
# The Process Count has Changed...
|
||||||
|
TIMESTAMP=$(date +%D@%T)
|
||||||
|
# Get a list of the PID of all of the processes
|
||||||
|
PID_LIST=$(ps aux | grep -v "grep $PROCESS" \
|
||||||
|
| grep -v $SCRIPT_NAME \
|
||||||
|
| grep $PROCESS | awk '{print $2}')
|
||||||
|
|
||||||
|
echo "PROCESS COUNT: $PROC_COUNT $PROCESS\
|
||||||
|
Processes Running ==> $TIMESTAMP" >> $LOGFILE &
|
||||||
|
echo "PROCESS COUNT: $PROC_COUNT $PROCESS\
|
||||||
|
Processes Running ==> $TIMESTAMP" > $TTY
|
||||||
|
echo ACTIVE PIDS: $PID_LIST >> $LOGFILE &
|
||||||
|
echo ACTIVE PIDS: $PID_LIST > $TTY
|
||||||
|
fi
|
||||||
|
LAST_COUNT=$PROC_COUNT
|
||||||
|
sleep $INTERVAL # Needed to reduce CPU load!
|
||||||
|
done
|
||||||
|
|
||||||
|
RUN='N' # Turn the RUN Flag Off
|
||||||
|
TIMESTAMP=$(date +%D@%T)
|
||||||
|
echo "ENDING PROCESS: $PROCESS END time ==>\
|
||||||
|
$TIMESTAMP" >> $LOGFILE &
|
||||||
|
echo "ENDING PROCESS: $PROCESS END time ==>\
|
||||||
|
$TIMESTAMP" > $TTY
|
||||||
|
|
||||||
|
# This will run the post_event_script, which is a function
|
||||||
|
|
||||||
|
if [[ $RUN_POST_EVENT = 'Y' ]]
|
||||||
|
then
|
||||||
|
echo "POST EVENT: Executing Post Event Script..."\
|
||||||
|
> $TTY
|
||||||
|
echo "POST EVENT: Executing Post Event Script..."\
|
||||||
|
>> $LOGFILE &
|
||||||
|
|
||||||
|
post_event_script # USER DEFINED FUNCTION!!!
|
||||||
|
integer RC=$?
|
||||||
|
if (( "RC" == 0 ))
|
||||||
|
then
|
||||||
|
echo "SUCCESS: Post Event Script Completed RC -\
|
||||||
|
${RC}" > $TTY
|
||||||
|
echo "SUCCESS: Post Event Script Completed RC -\
|
||||||
|
${RC}" >> $LOGFILE
|
||||||
|
else
|
||||||
|
echo "FAILURE: Post Event Script FAILED RC - ${RC}"\
|
||||||
|
> $TTY
|
||||||
|
echo "FAILURE: Post Event Script FAILED RC - ${RC}"\
|
||||||
|
>> $LOGFILE
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
|
||||||
|
'N')
|
||||||
|
# This will run the pre_event_script, which is a function
|
||||||
|
|
||||||
|
if [[ $RUN_PRE_EVENT = 'Y' ]]
|
||||||
|
then
|
||||||
|
echo "PRE EVENT: Executing Pre Event Script..." > $TTY
|
||||||
|
echo "PRE EVENT: Executing Pre Event Script..." >> $LOGFILE
|
||||||
|
|
||||||
|
pre_event_script # USER DEFINED FUNCTION!!!
|
||||||
|
RC=$? # Check the Return Code!!!
|
||||||
|
if (( "RC" == 0 ))
|
||||||
|
then
|
||||||
|
echo "SUCCESS: Pre Event Script Completed RC - ${RC}"\
|
||||||
|
> $TTY
|
||||||
|
echo "SUCCESS: Pre Event Script Completed RC - ${RC}"\
|
||||||
|
>> $LOGFILE
|
||||||
|
else
|
||||||
|
echo "FAILURE: Pre Event Script FAILED RC - ${RC}"\
|
||||||
|
> $TTY
|
||||||
|
echo "FAILURE: Pre Event Script FAILED RC - ${RC}"\
|
||||||
|
>> $LOGFILE
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "WAITING: Waiting for $PROCESS to \
|
||||||
|
startup...Monitoring..."
|
||||||
|
|
||||||
|
integer PROC_COUNT='-1' # Initialize to a fake value
|
||||||
|
|
||||||
|
# Loop until at least one process starts
|
||||||
|
|
||||||
|
until (( "PROC_COUNT" > 0 ))
|
||||||
|
do
|
||||||
|
# This is a Co-Process. This checks to see if a "Program
|
||||||
|
# Interrupt" has taken place. If so BREAK will be 'Y' and
|
||||||
|
# we exit both the loop and function
|
||||||
|
|
||||||
|
read BREAK
|
||||||
|
if [[ $BREAK = 'Y' ]]
|
||||||
|
then
|
||||||
|
return 3
|
||||||
|
fi
|
||||||
|
|
||||||
|
PROC_COUNT=$(ps aux | grep -v "grep $PROCESS" \
|
||||||
|
| grep -v $SCRIPT_NAME | grep $PROCESS | wc -l) \
|
||||||
|
>/dev/null 2>&1
|
||||||
|
|
||||||
|
sleep $INTERVAL # Needed to reduce CPU load!
|
||||||
|
done
|
||||||
|
|
||||||
|
RUN='Y' # Turn the RUN Flag On
|
||||||
|
|
||||||
|
TIMESTAMP=$(date +%D@%T)
|
||||||
|
|
||||||
|
PID_LIST=$(ps aux | grep -v "grep $PROCESS" \
|
||||||
|
| grep -v $SCRIPT_NAME \
|
||||||
|
| grep $PROCESS | awk '{print $2}')
|
||||||
|
|
||||||
|
if (( "PROC_COUNT" == 1 ))
|
||||||
|
then
|
||||||
|
echo "START PROCESS: $PROCESS START time ==>\
|
||||||
|
$TIMESTAMP" >> $LOGFILE &
|
||||||
|
echo ACTIVE PIDS: $PID_LIST >> $LOGFILE &
|
||||||
|
echo "START PROCESS: $PROCESS START time ==>\
|
||||||
|
$TIMESTAMP" > $TTY
|
||||||
|
echo ACTIVE PIDS: $PID_LIST > $TTY
|
||||||
|
elif (( "PROC_COUNT" > 1 ))
|
||||||
|
then
|
||||||
|
echo "START PROCESS: $PROC_COUNT $PROCESS\
|
||||||
|
Processes Started: START time ==> $TIMESTAMP" >> $LOGFILE &
|
||||||
|
echo ACTIVE PIDS: $PID_LIST >> $LOGFILE &
|
||||||
|
echo "START PROCESS: $PROC_COUNT $PROCESS\
|
||||||
|
Processes Started: START time ==> $TIMESTAMP" > $TTY
|
||||||
|
echo ACTIVE PIDS: $PID_LIST > $TTY
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
12
chapter10/function_startup_event_script
Executable file
12
chapter10/function_startup_event_script
Executable file
@@ -0,0 +1,12 @@
|
|||||||
|
startup_event_script ()
|
||||||
|
{
|
||||||
|
# Put anything that you want to execute WHEN, or AS, the
|
||||||
|
# monitored process STARTS in this function
|
||||||
|
|
||||||
|
: # No-OP - Needed as a placeholder for an empty function
|
||||||
|
# Comment Out the Above colon, ':'
|
||||||
|
|
||||||
|
STARTUP_RC=$?
|
||||||
|
return $STARTUP_RC
|
||||||
|
}
|
||||||
|
|
||||||
29
chapter10/function_test_string
Executable file
29
chapter10/function_test_string
Executable file
@@ -0,0 +1,29 @@
|
|||||||
|
test_string ()
|
||||||
|
{
|
||||||
|
if (( $# != 1 ))
|
||||||
|
then
|
||||||
|
echo 'ERROR'
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
|
||||||
|
C_STRING=$1
|
||||||
|
|
||||||
|
# Test the character string for its composition
|
||||||
|
|
||||||
|
case $C_STRING in
|
||||||
|
|
||||||
|
+([0-9])) echo 'POS_INT' # Integer >= 0
|
||||||
|
;;
|
||||||
|
+([-0-9])) echo 'NEG_INT' # Integer < 0
|
||||||
|
;;
|
||||||
|
+([a-z])) echo 'LOW_CASE' # lower case text
|
||||||
|
;;
|
||||||
|
+([A-Z])) echo 'UP_CASE' # UPPER case text
|
||||||
|
;;
|
||||||
|
+([a-z]|[A-Z])) echo 'MIX_CASE' # MIxed CAse text
|
||||||
|
;;
|
||||||
|
*) echo 'UNKNOWN' # Anything else
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
152
chapter10/proc_mon.ksh
Executable file
152
chapter10/proc_mon.ksh
Executable file
@@ -0,0 +1,152 @@
|
|||||||
|
#!/usr/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: proc_mon.ksh
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 02/14/2007
|
||||||
|
# REV: 1.1.P
|
||||||
|
# PLATFORM: Not Platform Dependent
|
||||||
|
#
|
||||||
|
# PURPOSE: This script is used to monitor a process to end
|
||||||
|
# specified by ARG1 if a single command-line argument is
|
||||||
|
# used. There is also a "verbose" mode where the monitored
|
||||||
|
# process is displayed and ARG2 is monitored.
|
||||||
|
#
|
||||||
|
# USAGE: proc_mon.ksh [-v] process-to-monitor
|
||||||
|
#
|
||||||
|
# EXIT STATUS:
|
||||||
|
# 0 ==> Monitored process has terminated
|
||||||
|
# 1 ==> Script usage error
|
||||||
|
# 2 ==> Target process to monitor is not active
|
||||||
|
# 3 ==> This script exits on a trapped signal
|
||||||
|
#
|
||||||
|
# REV. LIST:
|
||||||
|
#
|
||||||
|
# 02/22/2007 - Added code for a "verbose" mode to output the
|
||||||
|
# results of the .ps aux. command. The verbose
|
||||||
|
# mode is set using a "-v" switch.
|
||||||
|
#
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
# set -n # Uncomment to debug without any command execution
|
||||||
|
|
||||||
|
SCRIPT_NAME=`basename $0`
|
||||||
|
|
||||||
|
########################################################
|
||||||
|
############ DEFINE FUNCTIONS HERE #####################
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
function usage
|
||||||
|
{
|
||||||
|
echo "\n\n"
|
||||||
|
echo "USAGE: $SCRIPT_NAME [-v] {Process_to_monitor}"
|
||||||
|
echo "\nEXAMPLE: $SCRIPT_NAME my_backup\n"
|
||||||
|
echo "OR"
|
||||||
|
echo "\nEXAMPLE: $SCRIPT_NAME -v my_backup\n"
|
||||||
|
echo "Try again...EXITING...\n"
|
||||||
|
}
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
function exit_trap
|
||||||
|
{
|
||||||
|
echo "\n...EXITING on trapped signal...\n"
|
||||||
|
}
|
||||||
|
########################################################
|
||||||
|
################ START OF MAIN##########################
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
################
|
||||||
|
# Set a trap...#
|
||||||
|
################
|
||||||
|
|
||||||
|
trap 'exit_trap; exit 3' 1 2 3 15
|
||||||
|
|
||||||
|
# First Check for the Correct Number of Arguments
|
||||||
|
# One or Two is acceptable
|
||||||
|
|
||||||
|
if (( $# != 1 && $# != 2 ))
|
||||||
|
then
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Parse through the command-line arguments and see if verbose
|
||||||
|
# mode has been specified. NOTICE that we assign the target
|
||||||
|
# process to the PROCESS variable!!!
|
||||||
|
# Embedded case statement...
|
||||||
|
|
||||||
|
case $# in
|
||||||
|
1) case $1 in
|
||||||
|
'-v') usage
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
*) PROCESS=$1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
;;
|
||||||
|
2) case $1 in
|
||||||
|
'-v') continue
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
case $2 in
|
||||||
|
'-v') usage
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
*) PROCESS=$2
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
;;
|
||||||
|
*) usage
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
# Check if the process is running or exit!
|
||||||
|
|
||||||
|
ps aux | grep "$PROCESS" | grep -v "grep $PROCESS" \
|
||||||
|
| grep -v $SCRIPT_NAME >/dev/null
|
||||||
|
|
||||||
|
if (( $? != 0 ))
|
||||||
|
then
|
||||||
|
echo "\n\n$PROCESS is NOT an active process...EXITING...\n"
|
||||||
|
exit 2
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Show verbose mode if specified...
|
||||||
|
|
||||||
|
if (( $# == 2 )) && [[ $1 = "-v" ]]
|
||||||
|
then
|
||||||
|
# Verbose mode has been specified!
|
||||||
|
echo "\n"
|
||||||
|
|
||||||
|
# Extract the columns heading from the ps aux output
|
||||||
|
ps aux | head -n 1
|
||||||
|
|
||||||
|
ps aux | grep "$PROCESS" | grep -v "grep $PROCESS" \
|
||||||
|
| grep -v $SCRIPT_NAME
|
||||||
|
fi
|
||||||
|
|
||||||
|
##### O.K. The process is running, start monitoring...
|
||||||
|
|
||||||
|
SLEEP_TIME="1" # Seconds between monitoring
|
||||||
|
|
||||||
|
RC="0" # RC is the Return Code
|
||||||
|
echo "\n\n" # Give a couple of blank lines
|
||||||
|
|
||||||
|
echo "$PROCESS is currently RUNNING...`date`\n"
|
||||||
|
|
||||||
|
####################################
|
||||||
|
# Loop UNTIL the $PROCESS stops...
|
||||||
|
|
||||||
|
while (( RC == 0 )) # Loop until the return code is not zero
|
||||||
|
do
|
||||||
|
ps aux | grep $PROCESS | grep -v "grep $PROCESS" \
|
||||||
|
| grep -v $SCRIPT_NAME >/dev/null 2>&1
|
||||||
|
if (( $? != 0 )) # Check the Return Code!!!!!
|
||||||
|
then
|
||||||
|
echo "\n...$PROCESS has COMPLETED...`date`\n"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
sleep $SLEEP_TIME # Needed to reduce CPU Load!!!
|
||||||
|
done
|
||||||
|
|
||||||
|
# End of Script
|
||||||
151
chapter10/proc_watch.ksh
Executable file
151
chapter10/proc_watch.ksh
Executable file
@@ -0,0 +1,151 @@
|
|||||||
|
#!/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: proc_watch.ksh
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 09-12-2007
|
||||||
|
# REV: 1.0.P
|
||||||
|
# PLATFORM: Not Platform Dependent
|
||||||
|
#
|
||||||
|
# PURPOSE: This script is used to monitor and log
|
||||||
|
# the status of a process as it starts and stops.
|
||||||
|
#
|
||||||
|
# REV LIST:
|
||||||
|
#
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
# set -n # Uncomment to check syntax without ANY execution
|
||||||
|
#
|
||||||
|
####################################################
|
||||||
|
########## DEFINE FILES AND VARIABLES HERE #########
|
||||||
|
####################################################
|
||||||
|
|
||||||
|
LOGFILE="/tmp/proc_status.log"
|
||||||
|
[[ ! -s $LOGFILE ]] && touch $LOGFILE
|
||||||
|
|
||||||
|
PROCESS="$1" # Process to Monitor
|
||||||
|
SCRIPT_NAME=$(basename $0) # Script Name w/o the PATH
|
||||||
|
TTY=$(tty) # Current tty or pty
|
||||||
|
|
||||||
|
####################################################
|
||||||
|
############# DEFINE FUNCTIONS HERE ################
|
||||||
|
####################################################
|
||||||
|
|
||||||
|
usage ()
|
||||||
|
{
|
||||||
|
echo "\nUSAGE: $SCRIPT_NAME process_to_monitor\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################################
|
||||||
|
|
||||||
|
trap_exit ()
|
||||||
|
{
|
||||||
|
# Log an ending time for process monitoring
|
||||||
|
TIMESTAMP=$(date +%D@%T) # Get a new timestamp...
|
||||||
|
echo "MON_STOP: Monitoring for $PROCESS ended ==> $TIMESTAMP" \
|
||||||
|
| tee -a $LOGFILE
|
||||||
|
|
||||||
|
# Kill all functions
|
||||||
|
kill -9 $(jobs -p) 2>/dev/null
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################################
|
||||||
|
|
||||||
|
mon_proc_end ()
|
||||||
|
{
|
||||||
|
END_RC="0"
|
||||||
|
until (( END_RC != 0 ))
|
||||||
|
do
|
||||||
|
ps aux | grep -v "grep $PROCESS" | grep -v $SCRIPT_NAME \
|
||||||
|
| grep $PROCESS >/dev/null 2>&1
|
||||||
|
|
||||||
|
END_RC=$? # Check the Return Code!!
|
||||||
|
sleep 1 # Needed to reduce CPU load!
|
||||||
|
done
|
||||||
|
|
||||||
|
echo 'N' # Turn the RUN flag off
|
||||||
|
|
||||||
|
# Grab a Timestamp
|
||||||
|
TIMESTAMP=$(date +%D@%T)
|
||||||
|
|
||||||
|
echo "END PROCESS: $PROCESS ended ==> $TIMESTAMP" >> $LOGFILE &
|
||||||
|
echo "END PROCESS: $PROCESS ended ==> $TIMESTAMP" > $TTY
|
||||||
|
}
|
||||||
|
####################################################
|
||||||
|
|
||||||
|
mon_proc_start ()
|
||||||
|
{
|
||||||
|
START_RC="-1" # Initialize to -1
|
||||||
|
until (( START_RC == 0 ))
|
||||||
|
do
|
||||||
|
ps aux | grep -v "grep $PROCESS" | grep -v $SCRIPT_NAME \
|
||||||
|
| grep $PROCESS >/dev/null 2>&1
|
||||||
|
|
||||||
|
START_RC=$? # Check the Return Code!!!
|
||||||
|
sleep 1 # Needed to reduce CPU load!
|
||||||
|
done
|
||||||
|
|
||||||
|
echo 'Y' # Turn the RUN flag on
|
||||||
|
|
||||||
|
# Grab the timestamp
|
||||||
|
TIMESTAMP=$(date +%D@%T)
|
||||||
|
|
||||||
|
echo "START PROCESS: $PROCESS began ==> $TIMESTAMP" >> $LOGFILE &
|
||||||
|
echo "START PROCESS: $PROCESS began ==> $TIMESTAMP" > $TTY
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################################
|
||||||
|
############## START OF MAIN #######################
|
||||||
|
####################################################
|
||||||
|
|
||||||
|
### SET A TRAP ####
|
||||||
|
|
||||||
|
trap 'trap_exit; exit 0' 1 2 3 15
|
||||||
|
|
||||||
|
# Check for the Correct Command Line Argument - Only 1
|
||||||
|
|
||||||
|
if (( $# != 1 ))
|
||||||
|
then
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Get an Initial Process State and Set the RUN Flag
|
||||||
|
|
||||||
|
ps aux | grep -v "grep $PROCESS" | grep -v $SCRIPT_NAME \
|
||||||
|
| grep $PROCESS >/dev/null
|
||||||
|
|
||||||
|
PROC_RC=$? # Check the Return Code!!
|
||||||
|
|
||||||
|
# Give some initial feedback before starting the loop
|
||||||
|
|
||||||
|
if (( PROC_RC == 0 ))
|
||||||
|
then
|
||||||
|
echo "The $PROCESS process is currently running...Monitoring..."
|
||||||
|
RUN="Y" # Set the RUN Flag to YES
|
||||||
|
else
|
||||||
|
echo "The $PROCESS process is not currently running...Monitoring..."
|
||||||
|
RUN="N" # Set the RUN Flag to NO
|
||||||
|
fi
|
||||||
|
|
||||||
|
TIMESTAMP=$(date +%D@%T) # Grab a timestamp for the log
|
||||||
|
|
||||||
|
# Use a "tee -a $#LOGFILE" to send output to both standard output
|
||||||
|
# and to the file referenced by $LOGFILE
|
||||||
|
|
||||||
|
echo "MON_START: Monitoring for $PROCESS began ==> $TIMESTAMP" \
|
||||||
|
| tee -a $LOGFILE
|
||||||
|
|
||||||
|
# Loop Forever!!
|
||||||
|
|
||||||
|
while :
|
||||||
|
do
|
||||||
|
case $RUN in
|
||||||
|
'Y') # Loop Until the Process Ends
|
||||||
|
RUN=$(mon_proc_end)
|
||||||
|
;;
|
||||||
|
'N') # Loop Until the Process Starts
|
||||||
|
RUN=$(mon_proc_start)
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
# End of Script
|
||||||
570
chapter10/proc_watch_timed.ksh
Executable file
570
chapter10/proc_watch_timed.ksh
Executable file
@@ -0,0 +1,570 @@
|
|||||||
|
#!/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: proc_watch_timed.ksh
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 09-14-2007
|
||||||
|
# REV: 1.0.P
|
||||||
|
# PLATFORM: Not Platform Dependent
|
||||||
|
#
|
||||||
|
# PURPOSE: This script is used to monitor and log
|
||||||
|
# the status of a process as it starts and stops.
|
||||||
|
# Command line options are used to identify the target
|
||||||
|
# process to monitor and the length of time to monitor.
|
||||||
|
# Each event is logged to the file defined by the
|
||||||
|
# $LOGFILE variable. This script also has the ability
|
||||||
|
# to execute pre, startup, and post events. These are
|
||||||
|
# controlled by the $RUN_PRE_EVENT, $RUN_STARTUP_EVENT,
|
||||||
|
# and $RUN_POST_EVENT variables. These variables control
|
||||||
|
# execution individually. Whatever is to be executed is to
|
||||||
|
# be placed in either the "pre_event_script",
|
||||||
|
# startup_event_script, or the
|
||||||
|
# "post_event_script" functions, or in any combination.
|
||||||
|
# Timing is controlled on the command line.
|
||||||
|
#
|
||||||
|
# USAGE: $SCRIPT_NAME total_seconds target_process
|
||||||
|
#
|
||||||
|
# Will monitor the specified process for the
|
||||||
|
# specified number of seconds.
|
||||||
|
#
|
||||||
|
# USAGE: $SCRIPT_NAME [-s|-S seconds] [-m|-M minutes]
|
||||||
|
# [-h|-H hours] [-d|-D days]
|
||||||
|
# [-p|-P process]
|
||||||
|
#
|
||||||
|
# Will monitor the specified process for number of
|
||||||
|
# seconds specified within -s seconds, -m minutes,
|
||||||
|
# -h hours, and -d days. Any combination of command
|
||||||
|
# switches can be used.
|
||||||
|
#
|
||||||
|
# REV LIST:
|
||||||
|
#
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
# set -n # Uncomment to check syntax without ANY execution
|
||||||
|
#
|
||||||
|
####################################################
|
||||||
|
########## DEFINE FILES AND VARIABLES HERE #########
|
||||||
|
####################################################
|
||||||
|
|
||||||
|
typeset -u RUN_PRE_EVENT # Force to UPPERCASE
|
||||||
|
typeset -u RUN_STARTUP_EVENT # Force to UPPERCASE
|
||||||
|
typeset -u RUN_POST_EVENT # force to UPPERCASE
|
||||||
|
|
||||||
|
RUN_PRE_EVENT='N' # A 'Y' will execute, anything else will not
|
||||||
|
RUN_STARTUP_EVENT='Y' # A 'Y' will execute, anything else will not
|
||||||
|
RUN_POST_EVENT='Y' # A 'Y' will execute, anything else will not
|
||||||
|
|
||||||
|
LOGFILE="/tmp/proc_status.log"
|
||||||
|
[[ ! -s $LOGFILE ]] && touch $LOGFILE
|
||||||
|
|
||||||
|
SCRIPT_NAME=$(basename $0)
|
||||||
|
TTY=$(tty)
|
||||||
|
INTERVAL="1" # Seconds between sampling
|
||||||
|
JOBS=
|
||||||
|
|
||||||
|
####################################################
|
||||||
|
############# DEFINE FUNCTIONS HERE ################
|
||||||
|
####################################################
|
||||||
|
usage ()
|
||||||
|
{
|
||||||
|
echo "\n\n\t*****USAGE ERROR*****"
|
||||||
|
echo "\n\nUSAGE: $SCRIPT_NAME seconds process"
|
||||||
|
echo "\nWill monitor the specified process for the"
|
||||||
|
echo "specified number of seconds."
|
||||||
|
echo "\nUSAGE: $SCRIPT_NAME [-s|-S seconds] [-m|-M minutes]"
|
||||||
|
echo " [-h|-H hours] [-d|-D days] [-p|-P process]\n"
|
||||||
|
echo "\nWill monitor the specified process for number of"
|
||||||
|
echo "seconds specified within -s seconds, -m minutes,"
|
||||||
|
echo "-h hours and -d days. Any combination of command"
|
||||||
|
echo "switches can be used.\n"
|
||||||
|
echo "\nEXAMPLE: $SCRIPT_NAME 300 dtcalc"
|
||||||
|
echo "\n\nEXAMPLE: $SCRIPT_NAME -m 5 -p dtcalc"
|
||||||
|
echo "\nBoth examples will monitor the dtcalc process"
|
||||||
|
echo "for 5 minutes. Can specify days, hours, minutes"
|
||||||
|
echo "and seconds, using -d, -h, -m and -s\n\n"
|
||||||
|
}
|
||||||
|
####################################################
|
||||||
|
trap_exit ()
|
||||||
|
{
|
||||||
|
# set -x # Uncommant to debug this function
|
||||||
|
# Log an ending time for process monitoring
|
||||||
|
echo "INTERRUPT: Program Received an Interrupt...EXITING..." > $TTY
|
||||||
|
echo "INTERRUPT: Program Received an Interrupt...EXITING..." >> $LOGFILE
|
||||||
|
TIMESTAMP=$(date +%D@%T) # Get a new timestamp...
|
||||||
|
echo "MON_STOPPED: Monitoring for $PROCESS ended ==> $TIMESTAMP\n" \
|
||||||
|
>> $TTY
|
||||||
|
echo "MON_STOPPED: Monitoring for $PROCESS ended ==> $TIMESTAMP\n" \
|
||||||
|
>> $LOGFILE
|
||||||
|
echo "LOGFILE: All Events are Logged ==> $LOGFILE \n" > $TTY
|
||||||
|
|
||||||
|
# Kill all functions
|
||||||
|
JOBS=$(jobs -p)
|
||||||
|
if [[ ! -z $JOBS && $JOBS != '' && $JOBS != '0' ]]
|
||||||
|
then
|
||||||
|
kill $(jobs -p) 2>/dev/null 1>&2
|
||||||
|
fi
|
||||||
|
return 2
|
||||||
|
}
|
||||||
|
####################################################
|
||||||
|
pre_event_script ()
|
||||||
|
{
|
||||||
|
# Put anything that you want to execute BEFORE the
|
||||||
|
# monitored process STARTS in this function
|
||||||
|
|
||||||
|
: # No-OP - Needed as a placeholder for an empty function
|
||||||
|
# Comment Out the Above colon, ':'
|
||||||
|
|
||||||
|
PRE_RC=$?
|
||||||
|
return $PRE_RC
|
||||||
|
}
|
||||||
|
####################################################
|
||||||
|
startup_event_script ()
|
||||||
|
{
|
||||||
|
# Put anything that you want to execute WHEN, or AS, the
|
||||||
|
# monitored process STARTS in this function
|
||||||
|
|
||||||
|
: # No-OP - Needed as a placeholder for an empty function
|
||||||
|
# Comment Out the Above colon, ':'
|
||||||
|
|
||||||
|
STARTUP_RC=$?
|
||||||
|
return $STARTUP_RC
|
||||||
|
}
|
||||||
|
####################################################
|
||||||
|
post_event_script ()
|
||||||
|
{
|
||||||
|
# Put anything that you want to execute AFTER the
|
||||||
|
# monitored process ENDS in this function
|
||||||
|
|
||||||
|
: # No-OP - Need as a placeholder for an empty function
|
||||||
|
# Comment Out the Above colon, ':'
|
||||||
|
|
||||||
|
POST_RC=$?
|
||||||
|
return $POST_RC
|
||||||
|
}
|
||||||
|
####################################################
|
||||||
|
# This function is used to test character strings
|
||||||
|
|
||||||
|
test_string ()
|
||||||
|
{
|
||||||
|
if (( $# != 1 ))
|
||||||
|
then
|
||||||
|
echo 'ERROR'
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
|
||||||
|
C_STRING=$1
|
||||||
|
|
||||||
|
# Test the character string for its composition
|
||||||
|
|
||||||
|
case $C_STRING in
|
||||||
|
|
||||||
|
+([0-9])) echo 'POS_INT' # Integer >= 0
|
||||||
|
;;
|
||||||
|
+([-0-9])) echo 'NEG_INT' # Integer < 0
|
||||||
|
;;
|
||||||
|
+([a-z])) echo 'LOW_CASE' # lower case text
|
||||||
|
;;
|
||||||
|
+([A-Z])) echo 'UP_CASE' # UPPER case text
|
||||||
|
;;
|
||||||
|
+([a-z]|[A-Z])) echo 'MIX_CASE' # MIxed CAse text
|
||||||
|
;;
|
||||||
|
*) echo 'UNKNOWN' # Anything else
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
####################################################
|
||||||
|
proc_watch ()
|
||||||
|
{
|
||||||
|
# set -x # Uncomment to debug this function
|
||||||
|
# This function does all of the process monitoring!
|
||||||
|
|
||||||
|
while : # Loop Forever!!
|
||||||
|
do
|
||||||
|
case $RUN in
|
||||||
|
'Y')
|
||||||
|
# This will run the startup_event_script, which is a function
|
||||||
|
if [[ $RUN_STARTUP_EVENT = 'Y' ]]
|
||||||
|
then
|
||||||
|
echo "STARTUP EVENT: Executing Startup Event Script..."\
|
||||||
|
> $TTY
|
||||||
|
echo "STARTUP EVENT: Executing Startup Event Script..."\
|
||||||
|
>> $LOGFILE
|
||||||
|
|
||||||
|
startup_event_script # USER DEFINED FUNCTION!!!
|
||||||
|
RC=$? # Check the Return Code!!
|
||||||
|
if (( "RC" == 0 ))
|
||||||
|
then
|
||||||
|
echo "SUCCESS: Startup Event Script Completed RC - \
|
||||||
|
${RC}" > $TTY
|
||||||
|
echo "SUCCESS: Startup Event Script Completed RC - \
|
||||||
|
${RC}" >> $LOGFILE
|
||||||
|
|
||||||
|
else
|
||||||
|
echo "FAILURE: Startup Event Script FAILED RC - \
|
||||||
|
${RC}" > $TTY
|
||||||
|
echo "FAILURE: Startup Event Script FAILED RC - \
|
||||||
|
${RC}" >> $LOGFILE
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
integer PROC_COUNT='-1' # Reset the Counters
|
||||||
|
integer LAST_COUNT='-1'
|
||||||
|
# Loop until the process(es) end(s)
|
||||||
|
|
||||||
|
until (( "PROC_COUNT" == 0 ))
|
||||||
|
do
|
||||||
|
# This function is a Co-Process. $BREAK checks to see if
|
||||||
|
# "Program Interrupt" has taken place. If so BREAK will
|
||||||
|
# be 'Y' and we exit both the loop and function.
|
||||||
|
|
||||||
|
read BREAK
|
||||||
|
if [[ $BREAK = 'Y' ]]
|
||||||
|
then
|
||||||
|
return 3
|
||||||
|
fi
|
||||||
|
|
||||||
|
PROC_COUNT=$(ps aux | grep -v "grep $PROCESS" \
|
||||||
|
| grep -v $SCRIPT_NAME \
|
||||||
|
| grep $PROCESS | wc -l) >/dev/null 2>&1
|
||||||
|
|
||||||
|
if (( "LAST_COUNT" > 0 && "LAST_COUNT" != "PROC_COUNT" ))
|
||||||
|
then
|
||||||
|
# The Process Count has Changed...
|
||||||
|
TIMESTAMP=$(date +%D@%T)
|
||||||
|
# Get a list of the PID of all of the processes
|
||||||
|
PID_LIST=$(ps aux | grep -v "grep $PROCESS" \
|
||||||
|
| grep -v $SCRIPT_NAME \
|
||||||
|
| grep $PROCESS | awk '{print $2}')
|
||||||
|
|
||||||
|
echo "PROCESS COUNT: $PROC_COUNT $PROCESS\
|
||||||
|
Processes Running ==> $TIMESTAMP" >> $LOGFILE &
|
||||||
|
echo "PROCESS COUNT: $PROC_COUNT $PROCESS\
|
||||||
|
Processes Running ==> $TIMESTAMP" > $TTY
|
||||||
|
echo ACTIVE PIDS: $PID_LIST >> $LOGFILE &
|
||||||
|
echo ACTIVE PIDS: $PID_LIST > $TTY
|
||||||
|
fi
|
||||||
|
LAST_COUNT=$PROC_COUNT
|
||||||
|
sleep $INTERVAL # Needed to reduce CPU load!
|
||||||
|
done
|
||||||
|
|
||||||
|
RUN='N' # Turn the RUN Flag Off
|
||||||
|
TIMESTAMP=$(date +%D@%T)
|
||||||
|
echo "ENDING PROCESS: $PROCESS END time ==>\
|
||||||
|
$TIMESTAMP" >> $LOGFILE &
|
||||||
|
echo "ENDING PROCESS: $PROCESS END time ==>\
|
||||||
|
$TIMESTAMP" > $TTY
|
||||||
|
|
||||||
|
# This will run the post_event_script, which is a function
|
||||||
|
|
||||||
|
if [[ $RUN_POST_EVENT = 'Y' ]]
|
||||||
|
then
|
||||||
|
echo "POST EVENT: Executing Post Event Script..."\
|
||||||
|
> $TTY
|
||||||
|
echo "POST EVENT: Executing Post Event Script..."\
|
||||||
|
>> $LOGFILE &
|
||||||
|
|
||||||
|
post_event_script # USER DEFINED FUNCTION!!!
|
||||||
|
integer RC=$?
|
||||||
|
if (( "RC" == 0 ))
|
||||||
|
then
|
||||||
|
echo "SUCCESS: Post Event Script Completed RC - \
|
||||||
|
${RC}" > $TTY
|
||||||
|
echo "SUCCESS: Post Event Script Completed RC - \
|
||||||
|
${RC}" >> $LOGFILE
|
||||||
|
else
|
||||||
|
echo "FAILURE: Post Event Script FAILED RC - ${RC}"\
|
||||||
|
> $TTY
|
||||||
|
echo "FAILURE: Post Event Script FAILED RC - ${RC}"\
|
||||||
|
>> $LOGFILE
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
|
||||||
|
'N')
|
||||||
|
# This will run the pre_event_script, which is a function
|
||||||
|
|
||||||
|
if [[ $RUN_PRE_EVENT = 'Y' ]]
|
||||||
|
then
|
||||||
|
echo "PRE EVENT: Executing Pre Event Script..." > $TTY
|
||||||
|
echo "PRE EVENT: Executing Pre Event Script..." >> $LOGFILE
|
||||||
|
|
||||||
|
pre_event_script # USER DEFINED FUNCTION!!!
|
||||||
|
RC=$? # Check the Return Code!!!
|
||||||
|
if (( "RC" == 0 ))
|
||||||
|
then
|
||||||
|
echo "SUCCESS: Pre Event Script Completed RC - ${RC}"\
|
||||||
|
> $TTY
|
||||||
|
echo "SUCCESS: Pre Event Script Completed RC - ${RC}"\
|
||||||
|
>> $LOGFILE
|
||||||
|
else
|
||||||
|
echo "FAILURE: Pre Event Script FAILED RC - ${RC}"\
|
||||||
|
> $TTY
|
||||||
|
echo "FAILURE: Pre Event Script FAILED RC - ${RC}"\
|
||||||
|
>> $LOGFILE
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "WAITING: Waiting for $PROCESS to \
|
||||||
|
startup...Monitoring..."
|
||||||
|
|
||||||
|
integer PROC_COUNT='-1' # Initialize to a fake value
|
||||||
|
|
||||||
|
# Loop until at least one process starts
|
||||||
|
|
||||||
|
until (( "PROC_COUNT" > 0 ))
|
||||||
|
do
|
||||||
|
# This is a Co-Process. This checks to see if a "Program
|
||||||
|
# Interrupt" has taken place. If so BREAK will be 'Y' and
|
||||||
|
# we exit both the loop and function
|
||||||
|
|
||||||
|
read BREAK
|
||||||
|
if [[ $BREAK = 'Y' ]]
|
||||||
|
then
|
||||||
|
return 3
|
||||||
|
fi
|
||||||
|
|
||||||
|
PROC_COUNT=$(ps aux | grep -v "grep $PROCESS" \
|
||||||
|
| grep -v $SCRIPT_NAME | grep $PROCESS | wc -l) \
|
||||||
|
>/dev/null 2>&1
|
||||||
|
|
||||||
|
sleep $INTERVAL # Needed to reduce CPU load!
|
||||||
|
done
|
||||||
|
|
||||||
|
RUN='Y' # Turn the RUN Flag On
|
||||||
|
|
||||||
|
TIMESTAMP=$(date +%D@%T)
|
||||||
|
|
||||||
|
PID_LIST=$(ps aux | grep -v "grep $PROCESS" \
|
||||||
|
| grep -v $SCRIPT_NAME \
|
||||||
|
| grep $PROCESS | awk '{print $2}')
|
||||||
|
|
||||||
|
if (( "PROC_COUNT" == 1 ))
|
||||||
|
then
|
||||||
|
echo "START PROCESS: $PROCESS START time ==>\
|
||||||
|
$TIMESTAMP" >> $LOGFILE &
|
||||||
|
echo ACTIVE PIDS: $PID_LIST >> $LOGFILE &
|
||||||
|
echo "START PROCESS: $PROCESS START time ==>\
|
||||||
|
$TIMESTAMP" > $TTY
|
||||||
|
echo ACTIVE PIDS: $PID_LIST > $TTY
|
||||||
|
elif (( "PROC_COUNT" > 1 ))
|
||||||
|
then
|
||||||
|
echo "START PROCESS: $PROC_COUNT $PROCESS\
|
||||||
|
Processes Started: START time ==> $TIMESTAMP" >> $LOGFILE &
|
||||||
|
echo ACTIVE PIDS: $PID_LIST >> $LOGFILE &
|
||||||
|
echo "START PROCESS: $PROC_COUNT $PROCESS\
|
||||||
|
Processes Started: START time ==> $TIMESTAMP" > $TTY
|
||||||
|
echo ACTIVE PIDS: $PID_LIST > $TTY
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################################
|
||||||
|
############## START OF MAIN #######################
|
||||||
|
####################################################
|
||||||
|
|
||||||
|
### SET A TRAP ####
|
||||||
|
|
||||||
|
trap 'BREAK='Y';print -p $BREAK 2>/dev/null;trap_exit\
|
||||||
|
2>/dev/null;exit 0' 1 2 3 15
|
||||||
|
|
||||||
|
BREAK='N' # The BREAK variable is used in the co-process proc_watch
|
||||||
|
PROCESS= # Initialize to null
|
||||||
|
integer TOTAL_SECONDS=0
|
||||||
|
|
||||||
|
# Check commnand line arguments
|
||||||
|
|
||||||
|
if (( $# > 10 || $# < 2 ))
|
||||||
|
then
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Check to see if only the seconds and a process are
|
||||||
|
# the only arguments
|
||||||
|
|
||||||
|
if [[ ($# -eq 2) && ($1 != -*) && ($2 != -*) ]]
|
||||||
|
then
|
||||||
|
NUM_TEST=$(test_string $1) # Is this an Integer?
|
||||||
|
if [[ "$NUM_TEST" = 'POS_INT' ]]
|
||||||
|
then
|
||||||
|
TOTAL_SECONDS=$1 # Yep - It.s an Integer
|
||||||
|
PROCESS=$2 # Can be anything
|
||||||
|
else
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
# Since getopts does not care what arguments it gets, let.s
|
||||||
|
# do a quick sanity check to make sure that we only have
|
||||||
|
# between 2 and 10 arguments and the first one must start
|
||||||
|
# with a -* (hyphen and anything), else usage error
|
||||||
|
|
||||||
|
case "$#" in
|
||||||
|
[2-10]) if [[ $1 != -* ]]; then
|
||||||
|
usage; exit 1
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
HOURS=0 # Initialize all to zero
|
||||||
|
MINUTES=0
|
||||||
|
SECS=0
|
||||||
|
DAYS=0
|
||||||
|
|
||||||
|
# Use getopts to parse the command line arguments
|
||||||
|
# For each $OPTARG for DAYS, HOURS, MINUTES and DAYS check to see
|
||||||
|
# that each one is an integer by using the check_string function
|
||||||
|
|
||||||
|
while getopts ":h:H:m:M:s:S:d:D:P:p:" OPT_LIST 2>/dev/null
|
||||||
|
do
|
||||||
|
case $OPT_LIST in
|
||||||
|
h|H) [[ $(test_string $OPTARG) != 'POS_INT' ]] && usage && exit 1
|
||||||
|
(( HOURS = $OPTARG * 3600 )) # 3600 seconds per hour
|
||||||
|
;;
|
||||||
|
m|H) [[ $(test_string $OPTARG) != 'POS_INT' ]] && usage && exit 1
|
||||||
|
(( MINUTES = $OPTARG * 60 )) # 60 seconds per minute
|
||||||
|
;;
|
||||||
|
s|S) [[ $(test_string $OPTARG) != 'POS_INT' ]] && usage && exit 1
|
||||||
|
SECS="$OPTARG" # seconds are seconds
|
||||||
|
;;
|
||||||
|
d|D) [[ $(test_string $OPTARG) != 'POS_INT' ]] && usage && exit 1
|
||||||
|
(( DAYS = $OPTARG * 86400 )) # 86400 seconds per day
|
||||||
|
;;
|
||||||
|
p|P) PROCESS=$OPTARG # process can be anything
|
||||||
|
;;
|
||||||
|
\?) usage # USAGE ERROR
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
:) usage
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
*) usage
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
fi
|
||||||
|
|
||||||
|
# We need to make sure that we have a process that
|
||||||
|
# is NOT null or empty! - sanity check - The double quotes are required!
|
||||||
|
|
||||||
|
if [[ -z "$PROCESS" || "$PROCESS" = '' ]]
|
||||||
|
then
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Check to see that TOTAL_SECONDS was not previously set
|
||||||
|
|
||||||
|
if (( TOTAL_SECONDS == 0 ))
|
||||||
|
then
|
||||||
|
# Add everything together if anything is > 0
|
||||||
|
if [[ $SECS -gt 0 || $MINUTES -gt 0 || $HOURS -gt 0 \
|
||||||
|
|| $DAYS -gt 0 ]]
|
||||||
|
then
|
||||||
|
(( TOTAL_SECONDS = SECS + MINUTES + HOURS + DAYS ))
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Last Sanity Check!
|
||||||
|
|
||||||
|
if (( TOTAL_SECONDS <= 0 )) || [ -z $PROCESS ]
|
||||||
|
then
|
||||||
|
# Either There are No Seconds to Count or the
|
||||||
|
# $PROCESS Variable is Null...USAGE ERROR...
|
||||||
|
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
########### START MONITORING HERE!###########
|
||||||
|
|
||||||
|
echo "\nCurrently running $PROCESS processes:\n" > $TTY
|
||||||
|
ps aux | grep -v "grep $PROCESS" | grep -v $SCRIPT_NAME \
|
||||||
|
| grep $PROCESS > $TTY
|
||||||
|
|
||||||
|
PROC_RC=$? # Get the initial state of the monitored function
|
||||||
|
|
||||||
|
echo >$TTY # Send a blank line to the screen
|
||||||
|
|
||||||
|
(( PROC_RC != 0 )) && echo "\nThere are no $PROCESS processes running\n"
|
||||||
|
|
||||||
|
if (( PROC_RC == 0 )) # The Target Process(es) is/are running...
|
||||||
|
then
|
||||||
|
RUN='Y' # Set the RUN flag to true, or yes.
|
||||||
|
|
||||||
|
integer PROC_COUNT # Strips out the "padding" for display
|
||||||
|
|
||||||
|
PROC_COUNT=$(ps aux | grep -v "grep $PROCESS" | grep -v \
|
||||||
|
$SCRIPT_NAME | grep $PROCESS | wc -l) >/dev/null 2>&1
|
||||||
|
if (( PROC_COUNT == 1 ))
|
||||||
|
then
|
||||||
|
echo "The $PROCESS process is currently\
|
||||||
|
running...Monitoring...\n"
|
||||||
|
elif (( PROC_COUNT > 1 ))
|
||||||
|
then
|
||||||
|
print "There are $PROC_COUNT $PROCESS processes currently\
|
||||||
|
running...Monitoring...\n"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo "The $PROCESS process is not currently running...monitoring..."
|
||||||
|
RUN='N' # Set the RUN flag to false, or no.
|
||||||
|
fi
|
||||||
|
|
||||||
|
TIMESTAMP=$(date +%D@%T) # Time that this script started monitoring
|
||||||
|
|
||||||
|
# Get a list of the currently active process IDs
|
||||||
|
|
||||||
|
PID_LIST=$(ps aux | grep -v "grep $PROCESS" \
|
||||||
|
| grep -v $SCRIPT_NAME \
|
||||||
|
| grep $PROCESS | awk '{print $2}')
|
||||||
|
|
||||||
|
echo "MON_STARTED: Monitoring for $PROCESS began ==> $TIMESTAMP" \
|
||||||
|
| tee -a $LOGFILE
|
||||||
|
echo ACTIVE PIDS: $PID_LIST | tee -a $LOGFILE
|
||||||
|
|
||||||
|
##### NOTICE ####
|
||||||
|
# We kick off the "proc_watch" function below as a "Co-Process"
|
||||||
|
# This sets up a two way communications link between the
|
||||||
|
# "proc_watch" background function and this "MAIN BODY" of
|
||||||
|
# the script. This is needed because the function has two
|
||||||
|
# "infinite loops", with one always executing at any given time.
|
||||||
|
# Therefore we need a way to break out of the loop in case of
|
||||||
|
# an interrupt, i.e. CTRL+C, and when the countdown is complete.
|
||||||
|
# The "pipe appersand", |&, creates the background Co-Process
|
||||||
|
# and we use "print -p $VARIABLE" to transfer the variable.s
|
||||||
|
# value back to the background co-process.
|
||||||
|
###################################
|
||||||
|
|
||||||
|
proc_watch |& # Create a Background Co-Process!!
|
||||||
|
WATCH_PID=$! # Get the process ID of the last background job!
|
||||||
|
|
||||||
|
# Start the Count Down!
|
||||||
|
|
||||||
|
integer SECONDS_LEFT=$TOTAL_SECONDS
|
||||||
|
|
||||||
|
while (( SECONDS_LEFT > 0 ))
|
||||||
|
do
|
||||||
|
# Next send the current value of $BREAK to the Co-Process
|
||||||
|
# proc_watch, which was piped to the background...
|
||||||
|
|
||||||
|
print -p $BREAK 2>/dev/null
|
||||||
|
(( SECONDS_LEFT = SECONDS_LEFT - 1 ))
|
||||||
|
sleep 1 # 1 Second Between Counts
|
||||||
|
done
|
||||||
|
|
||||||
|
# Finished - Normal Timeout Exit...
|
||||||
|
TIMESTAMP=$(date +%D@%T) # Get a new timestamp...
|
||||||
|
echo "MON_STOPPED: Monitoring for $PROCESS ended ==> $TIMESTAMP\n" \
|
||||||
|
| tee -a $LOGFILE
|
||||||
|
|
||||||
|
echo "LOGFILE: All Events are Logged ==> $LOGFILE \n"
|
||||||
|
|
||||||
|
# Tell the proc_watch function to break out of the loop and die
|
||||||
|
BREAK='Y'
|
||||||
|
print -p $BREAK 2>/dev/null
|
||||||
|
|
||||||
|
kill $WATCH_PID 2>/dev/null
|
||||||
|
|
||||||
|
exit 0
|
||||||
|
|
||||||
|
# End of Script
|
||||||
17
chapter11/elapsed_time
Executable file
17
chapter11/elapsed_time
Executable file
@@ -0,0 +1,17 @@
|
|||||||
|
elapsed_time ()
|
||||||
|
{
|
||||||
|
# This function is intended for Bash shell scripts
|
||||||
|
|
||||||
|
SEC=$1
|
||||||
|
|
||||||
|
(( SEC < 60 )) && echo -e "[Elasped time: \
|
||||||
|
$SEC seconds]\c"
|
||||||
|
|
||||||
|
(( SEC >= 60 && SEC < 3600 )) && echo -e \
|
||||||
|
"[Elasped time: $(( SEC / 60 )) min $(( SEC % 60 )) sec]\c"
|
||||||
|
|
||||||
|
(( SEC > 3600 )) && echo -e "[Elasped time: \
|
||||||
|
$(( SEC / 3600 )) hr $(( (SEC % 3600) / 60 )) min \
|
||||||
|
$(( (SEC % 3600) % 60 )) sec]\c"
|
||||||
|
}
|
||||||
|
|
||||||
19
chapter11/function_build_random_line
Executable file
19
chapter11/function_build_random_line
Executable file
@@ -0,0 +1,19 @@
|
|||||||
|
build_random_line ()
|
||||||
|
{
|
||||||
|
# This function extracts random characters
|
||||||
|
# from the KEYS array by using the RANDOM
|
||||||
|
# shell variable
|
||||||
|
|
||||||
|
C=1
|
||||||
|
LINE=
|
||||||
|
until (( C > 79 ))
|
||||||
|
do
|
||||||
|
LINE="${LINE}${KEYS[$(($RANDOM % X + 1))]}"
|
||||||
|
(( C = C + 1 ))
|
||||||
|
done
|
||||||
|
|
||||||
|
# Return the line of random characters
|
||||||
|
|
||||||
|
echo "$LINE"
|
||||||
|
}
|
||||||
|
|
||||||
6
chapter11/function_get_date_time_stamp
Executable file
6
chapter11/function_get_date_time_stamp
Executable file
@@ -0,0 +1,6 @@
|
|||||||
|
function get_date_time_stamp
|
||||||
|
{
|
||||||
|
DATE_STAMP=$(date +'%m%d%y.%H%M%S')
|
||||||
|
echo $DATE_STAMP
|
||||||
|
}
|
||||||
|
|
||||||
8
chapter11/function_get_random_number
Executable file
8
chapter11/function_get_random_number
Executable file
@@ -0,0 +1,8 @@
|
|||||||
|
function get_random_number
|
||||||
|
{
|
||||||
|
# This function gets the next random number from the
|
||||||
|
# $RANDOM variable. The range is 0 to 32767.
|
||||||
|
|
||||||
|
echo "$RANDOM"
|
||||||
|
}
|
||||||
|
|
||||||
6
chapter11/function_get_second
Executable file
6
chapter11/function_get_second
Executable file
@@ -0,0 +1,6 @@
|
|||||||
|
function get_second
|
||||||
|
{
|
||||||
|
THIS_SECOND=$(date +%S)
|
||||||
|
echo $THIS_SECOND
|
||||||
|
}
|
||||||
|
|
||||||
27
chapter11/function_in_range_fixed_length_random_number_typeset
Executable file
27
chapter11/function_in_range_fixed_length_random_number_typeset
Executable file
@@ -0,0 +1,27 @@
|
|||||||
|
function in_range_fixed_length_random_number_typeset
|
||||||
|
{
|
||||||
|
# Create a pseudo-random number less than or equal
|
||||||
|
# to the $UPPER_LIMIT value, which is user defined.
|
||||||
|
# This function will also pad the output with leading
|
||||||
|
# zeros to keep the number of digits consistent using
|
||||||
|
# the typeset command.
|
||||||
|
|
||||||
|
# Find the length of each character string
|
||||||
|
|
||||||
|
UL_LENGTH=$(echo ${#UPPER_LIMIT})
|
||||||
|
|
||||||
|
# Fix the length of the RANDOM_NUMBER variable to
|
||||||
|
# the length of the UPPER_LIMIT variable, specified
|
||||||
|
# by the $UL_LENGTH variable.
|
||||||
|
|
||||||
|
typeset -Z$UL_LENGTH RANDOM_NUMBER
|
||||||
|
|
||||||
|
# Create a fixed length pseudo-random number
|
||||||
|
|
||||||
|
RANDOM_NUMBER=$(($RANDOM % $UPPER_LIMIT + 1))
|
||||||
|
|
||||||
|
# Return the value of the fixed length $RANDOM_NUMBER
|
||||||
|
|
||||||
|
echo $RANDOM_NUMBER
|
||||||
|
}
|
||||||
|
|
||||||
10
chapter11/function_in_range_random_number
Executable file
10
chapter11/function_in_range_random_number
Executable file
@@ -0,0 +1,10 @@
|
|||||||
|
function in_range_random_number
|
||||||
|
{
|
||||||
|
# Create a pseudo-random number less than or equal
|
||||||
|
# to the $UPPER_LIMIT value, which is user defined
|
||||||
|
|
||||||
|
RANDOM_NUMBER=$(($RANDOM % $UPPER_LIMIT + 1))
|
||||||
|
|
||||||
|
echo "$RANDOM_NUMBER"
|
||||||
|
}
|
||||||
|
|
||||||
15
chapter11/function_load_default_keyboard
Executable file
15
chapter11/function_load_default_keyboard
Executable file
@@ -0,0 +1,15 @@
|
|||||||
|
load_default_keyboard ()
|
||||||
|
{
|
||||||
|
# Loop through each character in the following list and
|
||||||
|
# append each character to the $CHAR_FILE file. This
|
||||||
|
# produces a file with one character on each line.
|
||||||
|
|
||||||
|
for CHAR in 1 2 3 4 5 6 7 8 9 0 q w e r t y u i o \
|
||||||
|
p a s d f g h j k l z x c v b n m \
|
||||||
|
Q W E R T Y U I O P A S D F G H J K L \
|
||||||
|
Z X C V B N M 0 1 2 3 4 5 6 7 8 9
|
||||||
|
do
|
||||||
|
echo "$CHAR" >> $CHAR_FILE
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
11
chapter11/function_my_program
Executable file
11
chapter11/function_my_program
Executable file
@@ -0,0 +1,11 @@
|
|||||||
|
function my_program
|
||||||
|
{
|
||||||
|
# Put anything you want to process in this function. I
|
||||||
|
# recommend that you specify an external program of shell
|
||||||
|
# script to execute.
|
||||||
|
|
||||||
|
echo "HELLO WORLD - $DATE_ST" > $UNIQUE_FN &
|
||||||
|
|
||||||
|
# : # No-Op - Does nothing but has a return code of zero
|
||||||
|
}
|
||||||
|
|
||||||
168
chapter11/mk_unique_filename.ksh
Executable file
168
chapter11/mk_unique_filename.ksh
Executable file
@@ -0,0 +1,168 @@
|
|||||||
|
#!/usr/bin/ksh
|
||||||
|
#
|
||||||
|
# AUTHOR: Randy Micahel
|
||||||
|
# SCRIPT: mk_unique_filename.ksh
|
||||||
|
# DATE: 11/12/2007
|
||||||
|
# REV: 1.2.P
|
||||||
|
#
|
||||||
|
# PLATFORM: Not Platform Dependent
|
||||||
|
#
|
||||||
|
# EXIT CODES:
|
||||||
|
# 0 - Normal script execution
|
||||||
|
# 1 - Usage error
|
||||||
|
#
|
||||||
|
# REV LIST:
|
||||||
|
#
|
||||||
|
#
|
||||||
|
# set -x # Uncomment to debug
|
||||||
|
# set -n # Uncomment to debug without any execution
|
||||||
|
#
|
||||||
|
####################################################
|
||||||
|
########## DEFINE FUNCTIONS HERE ###################
|
||||||
|
####################################################
|
||||||
|
|
||||||
|
function usage
|
||||||
|
{
|
||||||
|
echo "\nUSAGE: $SCRIPT_NAME base_file_name\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################################
|
||||||
|
|
||||||
|
function get_date_time_stamp
|
||||||
|
{
|
||||||
|
DATE_STAMP=$(date +'%m%d%y.%H%M%S')
|
||||||
|
echo $DATE_STAMP
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################################
|
||||||
|
|
||||||
|
function get_second
|
||||||
|
{
|
||||||
|
THIS_SECOND=$(date +%S)
|
||||||
|
echo $THIS_SECOND
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################################
|
||||||
|
|
||||||
|
function in_range_fixed_length_random_number_typeset
|
||||||
|
{
|
||||||
|
# Create a pseudo-random number less than or equal
|
||||||
|
# to the $UPPER_LIMIT value, which is user defined.
|
||||||
|
# This function will also pad the output with leading
|
||||||
|
# zeros to keep the number of digits consistent using
|
||||||
|
# the typeset command.
|
||||||
|
|
||||||
|
# Find the length of each character string
|
||||||
|
|
||||||
|
UL_LENGTH=$(echo ${#UPPER_LIMIT})
|
||||||
|
|
||||||
|
# Fix the length of the RANDOM_NUMBER variable to
|
||||||
|
# the length of the UPPER_LIMIT variable, specified
|
||||||
|
# by the $UL_LENGTH variable.
|
||||||
|
|
||||||
|
typeset -Z$UL_LENGTH RANDOM_NUMBER
|
||||||
|
|
||||||
|
# Create a fixed length pseudo-random number
|
||||||
|
|
||||||
|
RANDOM_NUMBER=$(($RANDOM % $UPPER_LIMIT + 1))
|
||||||
|
|
||||||
|
# Return the value of the fixed length $RANDOM_NUMBER
|
||||||
|
|
||||||
|
echo $RANDOM_NUMBER
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################################
|
||||||
|
|
||||||
|
function my_program
|
||||||
|
{
|
||||||
|
# Put anything you want to process in this function. I
|
||||||
|
# recommend that you specify an external program of shell
|
||||||
|
# script to execute.
|
||||||
|
|
||||||
|
echo "HELLO WORLD - $DATE_ST" > $UNIQUE_FN &
|
||||||
|
|
||||||
|
# : # No-Op - Does nothing but has a return code of zero
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################################
|
||||||
|
################ BEGINNING OF MAIN #################
|
||||||
|
####################################################
|
||||||
|
|
||||||
|
SCRIPT_NAME=$(basename $0) # Query the system for this script name
|
||||||
|
|
||||||
|
# Check for the correct number of arguments - exactly 1
|
||||||
|
|
||||||
|
if (( $# != 1 ))
|
||||||
|
then
|
||||||
|
echo "\nERROR: Usage error...EXITING..."
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# What filename do we need to make unique?
|
||||||
|
|
||||||
|
BASE_FN=$1 # Get the BASE filenaname to make unique
|
||||||
|
|
||||||
|
RANDOM=$$ # Initialize the RANDOM environment variable
|
||||||
|
# with the current process ID (PID)
|
||||||
|
|
||||||
|
UPPER_LIMIT=32767 # Set the UPPER_LIMIT
|
||||||
|
|
||||||
|
CURRENT_SECOND=99 # Initialize to a non-second
|
||||||
|
LAST_SECOND=98 # Initialize to a non-second
|
||||||
|
|
||||||
|
USED_NUMBERS= # Initialize to null
|
||||||
|
|
||||||
|
PROCESSING="TRUE" # Initialize to run mode
|
||||||
|
|
||||||
|
while [[ $PROCESSING = "TRUE" ]]
|
||||||
|
do
|
||||||
|
DATE_ST=$(get_date_time_stamp) # Get the current date/time
|
||||||
|
CURRENT_SECOND=$(get_second) # Get the current second
|
||||||
|
|
||||||
|
RN=$(in_range_fixed_length_random_number_typeset) # Get a new number
|
||||||
|
|
||||||
|
# Check to see if we have already used this number this second
|
||||||
|
|
||||||
|
if (( CURRENT_SECOND == LAST_SECOND ))
|
||||||
|
then
|
||||||
|
UNIQUE=FALSE # Initialize to FALSE
|
||||||
|
while [[ "$UNIQUE" != "TRUE" ]] && [[ ! -z $UNIQUE ]]
|
||||||
|
do
|
||||||
|
# Has this number already been used this second?
|
||||||
|
echo $USED_NUMBERS | grep $RN >/dev/null 2>&1
|
||||||
|
if (( $? == 0 ))
|
||||||
|
then
|
||||||
|
# Has been used...Get another number
|
||||||
|
RN=$(in_range_fixed_length_random_number_typeset)
|
||||||
|
else
|
||||||
|
# Number is unique this second...
|
||||||
|
UNIQUE=TRUE
|
||||||
|
# Add this numner to the used number list
|
||||||
|
USED_NUMBERS="$USED_NUMBERS $RN"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
else
|
||||||
|
USED_NUMBERS= # New second...Reinitialize to null
|
||||||
|
fi
|
||||||
|
# Assign the unique filename to the UNIQUE_FN variable
|
||||||
|
|
||||||
|
UNIQUE_FN=${BASE_FN}.${DATE_ST}.$RN
|
||||||
|
|
||||||
|
echo $UNIQUE_FN # Comment out this line!!
|
||||||
|
|
||||||
|
LAST_SECOND=$CURRENT_SECOND # Save the last second value
|
||||||
|
|
||||||
|
# We have a unique filename...
|
||||||
|
#
|
||||||
|
# PROCESS SOMETHING HERE AND REDIRECT OUTPUT TO $UNIQUE_FN
|
||||||
|
#
|
||||||
|
my_program
|
||||||
|
#
|
||||||
|
# IF PROCESSING IS FINISHED ASSIGN "FALSE" to the PROCESSING VARIABLE
|
||||||
|
#
|
||||||
|
# if [[ $MY_PROCESS = "done" ]]
|
||||||
|
# then
|
||||||
|
# PROCESSING="FALSE"
|
||||||
|
# fi
|
||||||
|
done
|
||||||
204
chapter11/random_file.bash
Executable file
204
chapter11/random_file.bash
Executable file
@@ -0,0 +1,204 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
#
|
||||||
|
# SCRIPT: random_file.bash
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 8/3/2007
|
||||||
|
# REV: 1.0
|
||||||
|
# PLATFORM: Not Platform Dependent
|
||||||
|
#
|
||||||
|
# PURPOSE: This script is used to create a
|
||||||
|
# specific size file of random characters.
|
||||||
|
# The methods used in this script include
|
||||||
|
# loading an array with alphanumeric
|
||||||
|
# characters, then using the /dev/random
|
||||||
|
# character special file to seed the RANDOM
|
||||||
|
# shell variable, which in turn is used to
|
||||||
|
# extract a random set of characters from the
|
||||||
|
# KEYS array to build the OUTFILE of a
|
||||||
|
# specified MB size.
|
||||||
|
#
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
#
|
||||||
|
# set -n # Uncomment to check script syntax
|
||||||
|
# # without aby execution. Do not forget
|
||||||
|
# # to put the comment back in or the
|
||||||
|
# # script will never execute.
|
||||||
|
#
|
||||||
|
##########################################
|
||||||
|
# DEFINE FILES AND VARIABLES HERE
|
||||||
|
##########################################
|
||||||
|
|
||||||
|
typeset -i MB_SIZE=$1
|
||||||
|
typeset -i RN
|
||||||
|
typeset -i i=1
|
||||||
|
typeset -i X=0
|
||||||
|
WORKDIR=/scripts
|
||||||
|
OUTFILE=${WORKDIR}/largefile.random.txt
|
||||||
|
>$OUTFILE
|
||||||
|
THIS_SCRIPT=$(basename $0)
|
||||||
|
CHAR_FILE=${WORKDIR}/char_file.txt
|
||||||
|
|
||||||
|
##########################################
|
||||||
|
# DEFINE FUNCTIONS HERE
|
||||||
|
##########################################
|
||||||
|
|
||||||
|
build_random_line ()
|
||||||
|
{
|
||||||
|
# This function extracts random characters
|
||||||
|
# from the KEYS array by using the RANDOM
|
||||||
|
# shell variable
|
||||||
|
|
||||||
|
C=1
|
||||||
|
LINE=
|
||||||
|
until (( C > 79 ))
|
||||||
|
do
|
||||||
|
LINE="${LINE}${KEYS[$(($RANDOM % X + 1))]}"
|
||||||
|
(( C = C + 1 ))
|
||||||
|
done
|
||||||
|
|
||||||
|
# Return the line of random characters
|
||||||
|
|
||||||
|
echo "$LINE"
|
||||||
|
}
|
||||||
|
|
||||||
|
##########################################
|
||||||
|
|
||||||
|
elasped_time ()
|
||||||
|
{
|
||||||
|
SEC=$1
|
||||||
|
|
||||||
|
(( SEC < 60 )) && echo -e "[Elasped time: \
|
||||||
|
$SEC seconds]\c"
|
||||||
|
|
||||||
|
(( SEC >= 60 && SEC < 3600 )) && echo -e \
|
||||||
|
"[Elasped time: $(( SEC / 60 )) min $(( SEC % 60 )) sec]\c"
|
||||||
|
|
||||||
|
(( SEC > 3600 )) && echo -e "[Elasped time: \
|
||||||
|
$(( SEC / 3600 )) hr $(( (SEC % 3600) / 60 )) min \
|
||||||
|
$(( (SEC % 3600) % 60 )) sec]\c"
|
||||||
|
}
|
||||||
|
|
||||||
|
##########################################
|
||||||
|
|
||||||
|
load_default_keyboard ()
|
||||||
|
{
|
||||||
|
# Loop through each character in the following list and
|
||||||
|
# append each character to the $CHAR_FILE file. This
|
||||||
|
# produces a file with one character on each line.
|
||||||
|
|
||||||
|
for CHAR in 1 2 3 4 5 6 7 8 9 0 q w e r t y u i o \
|
||||||
|
p a s d f g h j k l z x c v b n m \
|
||||||
|
Q W E R T Y U I O P A S D F G H J K L \
|
||||||
|
Z X C V B N M 0 1 2 3 4 5 6 7 8 9
|
||||||
|
do
|
||||||
|
echo "$CHAR" >> $CHAR_FILE
|
||||||
|
done
|
||||||
|
}
|
||||||
|
##########################################
|
||||||
|
|
||||||
|
usage ()
|
||||||
|
{
|
||||||
|
echo -e "\nUSAGE: $THIS_SCRIPT Mb_size"
|
||||||
|
echo -e "Where Mb_size is the size of the file to build\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
##########################################
|
||||||
|
# BEGINNING OF MAIN
|
||||||
|
##########################################
|
||||||
|
|
||||||
|
if (( $# != 1 ))
|
||||||
|
then
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Test for an integer value
|
||||||
|
|
||||||
|
case $MB_SIZE in
|
||||||
|
[0-9]) : # Do nothing
|
||||||
|
;;
|
||||||
|
*) usage
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
# Test for the $CHAR_FILE
|
||||||
|
|
||||||
|
if [ ! -s "$CHAR_FILE" ]
|
||||||
|
then
|
||||||
|
echo -e "\nNOTE: $CHAR_FILE does not esist"
|
||||||
|
echo "Loading default keyboard data."
|
||||||
|
echo -e "Creating $CHAR_FILE...\c"
|
||||||
|
load_default_keyboard
|
||||||
|
echo "Done"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Load Character Array
|
||||||
|
|
||||||
|
echo -e "\nLoading array with alphanumeric character elements"
|
||||||
|
|
||||||
|
while read ARRAY_ELEMENT
|
||||||
|
do
|
||||||
|
(( X = X + 1 ))
|
||||||
|
KEYS[$X]=$ARRAY_ELEMENT
|
||||||
|
|
||||||
|
done < $CHAR_FILE
|
||||||
|
|
||||||
|
echo "Total Array Character Elements: $X"
|
||||||
|
|
||||||
|
# Use /dev/random to seed the shell variable RANDOM
|
||||||
|
|
||||||
|
echo "Querying the kernel random number generator for a random seed"
|
||||||
|
|
||||||
|
RN=$(dd if=/dev/random count=1 2>/dev/null \
|
||||||
|
| od -t u4 | awk '{print $2}'| head -n 1)
|
||||||
|
|
||||||
|
# The shell variable RANDOM is limited to 32767
|
||||||
|
|
||||||
|
echo "Reducing the random seed value to between 1 and 32767"
|
||||||
|
|
||||||
|
RN=$(( RN % 32767 + 1 ))
|
||||||
|
|
||||||
|
# Initialize RANDOM with a new seed
|
||||||
|
|
||||||
|
echo "Assigning a new seed to the RANDOM shell variable"
|
||||||
|
|
||||||
|
RANDOM=$RN
|
||||||
|
|
||||||
|
echo "Building a $MB_SIZE MB random character file ==> $OUTFILE"
|
||||||
|
echo "Please be patient, this may take some time to complete..."
|
||||||
|
echo -e "Executing: .\c"
|
||||||
|
|
||||||
|
# Reset the shell SECONDS variable to zero seconds.
|
||||||
|
|
||||||
|
SECONDS=0
|
||||||
|
|
||||||
|
TOT_LINES=$(( MB_SIZE * 12800 ))
|
||||||
|
|
||||||
|
until (( i > TOT_LINES ))
|
||||||
|
do
|
||||||
|
build_random_line >> $OUTFILE
|
||||||
|
(( $(( i % 100 )) == 0 )) && echo -e ".\c"
|
||||||
|
(( i = i + 1 ))
|
||||||
|
done
|
||||||
|
|
||||||
|
# Capture the total seconds
|
||||||
|
|
||||||
|
TOT_SEC=$SECONDS
|
||||||
|
|
||||||
|
echo -e "\n\nSUCCESS: $OUTFILE created at $MB_SIZE MB\n"
|
||||||
|
|
||||||
|
elasped_time $TOT_SEC
|
||||||
|
|
||||||
|
# Calculate the bytes/second file creation rate
|
||||||
|
|
||||||
|
(( MB_SEC = ( MB_SIZE * 1024000 ) / TOT_SEC ))
|
||||||
|
|
||||||
|
echo -e "\n\nFile Creation Rate: $MB_SEC bytes/second\n"
|
||||||
|
|
||||||
|
echo -e "File size:\n"
|
||||||
|
ls -l $OUTFILE
|
||||||
|
echo
|
||||||
|
|
||||||
|
##########################################
|
||||||
|
# END OF random_file.bash SCRIPT
|
||||||
|
##########################################
|
||||||
150
chapter11/random_number.ksh
Executable file
150
chapter11/random_number.ksh
Executable file
@@ -0,0 +1,150 @@
|
|||||||
|
#!/usr/bin/ksh
|
||||||
|
#
|
||||||
|
# AUTHOR: Randy Micahel
|
||||||
|
# SCRIPT: random_number.ksh
|
||||||
|
# DATE: 11/12/2007
|
||||||
|
# REV: 1.2.P
|
||||||
|
#
|
||||||
|
# PLATFORM: Not Platform Dependent
|
||||||
|
#
|
||||||
|
# EXIT CODES:
|
||||||
|
# 0 - Normal script execution
|
||||||
|
# 1 - Usage error
|
||||||
|
#
|
||||||
|
# REV LIST:
|
||||||
|
#
|
||||||
|
#
|
||||||
|
# set -x # Uncomment to debug
|
||||||
|
# set -n # Uncomment to check syntax without any command execution
|
||||||
|
#
|
||||||
|
####################################################
|
||||||
|
########## DEFINE FUNCTIONS HERE ###################
|
||||||
|
####################################################
|
||||||
|
|
||||||
|
function usage
|
||||||
|
{
|
||||||
|
echo "\nUSAGE: $SCRIPT_NAME [-f] [upper_number_range]"
|
||||||
|
echo "\nEXAMPLE: $SCRIPT_NAME"
|
||||||
|
echo "Will return a random number between 0 and 32767"
|
||||||
|
echo "\nEXAMPLE: $SCRIPT_NAME 1000"
|
||||||
|
echo "Will return a random number between 1 and 1000"
|
||||||
|
echo "\nEXAMPLE: $SCRIPT_NAME -f 1000"
|
||||||
|
echo "Will add leading zeros to a random number from"
|
||||||
|
echo "1 to 1000, which keeps the number of digits consistant\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################################
|
||||||
|
|
||||||
|
function get_random_number
|
||||||
|
{
|
||||||
|
# This function gets the next random number from the
|
||||||
|
# $RANDOM variable. The range is 0 to 32767.
|
||||||
|
|
||||||
|
echo "$RANDOM"
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################################
|
||||||
|
|
||||||
|
function in_range_random_number
|
||||||
|
{
|
||||||
|
# Create a pseudo-random number less than or equal
|
||||||
|
# to the $UPPER_LIMIT value, which is user defined
|
||||||
|
|
||||||
|
RANDOM_NUMBER=$(($RANDOM % $UPPER_LIMIT + 1))
|
||||||
|
|
||||||
|
echo "$RANDOM_NUMBER"
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################################
|
||||||
|
|
||||||
|
function in_range_fixed_length_random_number_typeset
|
||||||
|
{
|
||||||
|
# Create a pseudo-random number less than or equal
|
||||||
|
# to the $UPPER_LIMIT value, which is user defined.
|
||||||
|
# This function will also pad the output with leading
|
||||||
|
# zeros to keep the number of digits consistent using
|
||||||
|
# the typeset command.
|
||||||
|
|
||||||
|
# Find the length of each character string
|
||||||
|
|
||||||
|
UL_LENGTH=$(echo ${#UPPER_LIMIT})
|
||||||
|
|
||||||
|
# Fix the length of the RANDOM_NUMBER variable to
|
||||||
|
# the length of the UPPER_LIMIT variable, specified
|
||||||
|
# by the $UL_LENGTH variable.
|
||||||
|
|
||||||
|
typeset -Z$UL_LENGTH RANDOM_NUMBER
|
||||||
|
|
||||||
|
# Create a fixed length pseudo-random number
|
||||||
|
|
||||||
|
RANDOM_NUMBER=$(($RANDOM % $UPPER_LIMIT + 1))
|
||||||
|
|
||||||
|
# Return the value of the fixed length $RANDOM_NUMBER
|
||||||
|
|
||||||
|
echo $RANDOM_NUMBER
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################################
|
||||||
|
############## BEGINNING OF MAIN ###################
|
||||||
|
####################################################
|
||||||
|
|
||||||
|
SCRIPT_NAME=`basename $0`
|
||||||
|
|
||||||
|
RANDOM=$$ # Initialize the RANDOM environment variable
|
||||||
|
# using the PID as the initial seed
|
||||||
|
|
||||||
|
case $# in
|
||||||
|
0) get_random_number
|
||||||
|
;;
|
||||||
|
|
||||||
|
1) UPPER_LIMIT="$1"
|
||||||
|
|
||||||
|
# Test to see if $UPPER_LIMIT is a number
|
||||||
|
|
||||||
|
case $UPPER_LIMIT in
|
||||||
|
+([0-9])) : # Do Nothing...It's a number
|
||||||
|
# NOTE: A colon (:) is a no-op in Korn shell
|
||||||
|
;;
|
||||||
|
*) echo "\nERROR: $UPPER_LIMIT is not a number..."
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
in_range_random_number
|
||||||
|
;;
|
||||||
|
|
||||||
|
2) # Check for the -f switch to fix the length.
|
||||||
|
|
||||||
|
if [[ $1 = '-f' ]] || [[ $1 = '-F' ]]
|
||||||
|
then
|
||||||
|
|
||||||
|
UPPER_LIMIT="$2"
|
||||||
|
|
||||||
|
# Test to see if $UPPER_LIMIT is a number
|
||||||
|
|
||||||
|
case $UPPER_LIMIT in
|
||||||
|
+([0-9])) : # Do Nothing...It's a number
|
||||||
|
# NOTE: A colon (:) is a no-op in Korn shell
|
||||||
|
;;
|
||||||
|
*) echo "\nERROR: $UPPER_LIMIT is not a number..."
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
in_range_fixed_length_random_number_typeset
|
||||||
|
|
||||||
|
else
|
||||||
|
echo "\nInvalid argument $1, see usage below..."
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
|
||||||
|
*) usage
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
# End of random_number.ksh Shell Script
|
||||||
22
chapter11/random_number_testing.bash
Executable file
22
chapter11/random_number_testing.bash
Executable file
@@ -0,0 +1,22 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
#
|
||||||
|
# SCRIPT: random_number_testing.bash
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 8/8/2007
|
||||||
|
|
||||||
|
|
||||||
|
# For each 1K count we ger 64 lines of random numbers
|
||||||
|
|
||||||
|
dd if=/dev/urandom count=1k bs=1 2>/dev/null | od -t u2 \
|
||||||
|
| awk '{print $2}' | sed /^$/d > 64random_numbers.txt
|
||||||
|
|
||||||
|
# For each 1M count we get 65536 lines of random numbers
|
||||||
|
|
||||||
|
dd if=/dev/urandom count=1M bs=1 2>/dev/null | od -t u2 \
|
||||||
|
| awk '{print $2}' | sed /^$/d > 65536random_numbers.txt
|
||||||
|
|
||||||
|
# The opposite is /dev/zero which will create a null
|
||||||
|
# file of a specific size
|
||||||
|
|
||||||
|
dd if=/dev/zero of=1MBemptyfile count=1M bs=1 2>/dev/null
|
||||||
|
|
||||||
36
chapter12/function_build_manager_password_report
Executable file
36
chapter12/function_build_manager_password_report
Executable file
@@ -0,0 +1,36 @@
|
|||||||
|
function build_manager_password_report
|
||||||
|
{
|
||||||
|
# Build a file to print for the secure envelope
|
||||||
|
(
|
||||||
|
$ECHO "\n RESTRICTED USE!!!"
|
||||||
|
$ECHO "\n\n\tImmediately send an e-mail to:\n"
|
||||||
|
|
||||||
|
$ECHO " $NOTIFICATION_LIST"
|
||||||
|
|
||||||
|
$ECHO "\n\tif this password is revealed!"
|
||||||
|
$ECHO "\n\tAIX root password: $PW\n"
|
||||||
|
|
||||||
|
$ECHO "\n\n"
|
||||||
|
|
||||||
|
$ECHO "\n RESTRICTED USE!!!"
|
||||||
|
$ECHO "\n\n\tImmediately send an e-mail to:\n"
|
||||||
|
|
||||||
|
$ECHO " $NOTIFICATION_LIST"
|
||||||
|
|
||||||
|
$ECHO "\n\tif this password is revealed!"
|
||||||
|
$ECHO "\n\tAIX root password: $PW\n"
|
||||||
|
|
||||||
|
$ECHO "\n\n"
|
||||||
|
|
||||||
|
$ECHO "\n RESTRICTED USE!!!"
|
||||||
|
$ECHO "\n\n\tImmediately send an e-mail to:\n"
|
||||||
|
|
||||||
|
$ECHO " $NOTIFICATION_LIST"
|
||||||
|
|
||||||
|
$ECHO "\n\tif this password is revealed!"
|
||||||
|
$ECHO "\n\tAIX root password: $PW\n"
|
||||||
|
|
||||||
|
) > $OUTFILE
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
32
chapter12/function_check_for_and_create_keyboard_file
Executable file
32
chapter12/function_check_for_and_create_keyboard_file
Executable file
@@ -0,0 +1,32 @@
|
|||||||
|
function check_for_and_create_keyboard_file
|
||||||
|
{
|
||||||
|
# If the $KEYBOARD_FILE does not exist then
|
||||||
|
# ask the user to load the "standard" keyboard
|
||||||
|
# layout, which is done with the load_default_keyboard
|
||||||
|
# function.
|
||||||
|
|
||||||
|
if [ ! -s $KEYBOARD_FILE ]
|
||||||
|
then
|
||||||
|
$ECHO "\n\nERROR: Missing Keyboard File"
|
||||||
|
$ECHO "\n\nWould You Like to Load the"
|
||||||
|
$ECHO "Default Keyboard Layout?"
|
||||||
|
$ECHO "\n\t(Y/N): \c"
|
||||||
|
typeset -u REPLY=FALSE
|
||||||
|
read REPLY
|
||||||
|
if [ $REPLY != Y ]
|
||||||
|
then
|
||||||
|
$ECHO "\n\nERROR: This shell script cannot operate"
|
||||||
|
$ECHO "without a keyboard data file located in"
|
||||||
|
$ECHO "\n==> $KEYBOARD_FILE\n"
|
||||||
|
$ECHO "\nThis file expects one character per line."
|
||||||
|
$ECHO "\n\t...EXITING...\n"
|
||||||
|
exit 3
|
||||||
|
else
|
||||||
|
load_default_keyboard
|
||||||
|
$ECHO "\nPress ENTER when you are you ready to continue: \c"
|
||||||
|
read REPLY
|
||||||
|
clear
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
14
chapter12/function_in_range_random_number
Executable file
14
chapter12/function_in_range_random_number
Executable file
@@ -0,0 +1,14 @@
|
|||||||
|
function in_range_random_number
|
||||||
|
{
|
||||||
|
# Create a pseudo-random number less than or equal
|
||||||
|
# to the $UPPER_LIMIT value, which is defined in the
|
||||||
|
# main body of the shell script.
|
||||||
|
|
||||||
|
RN=$(dd if=/dev/urandom count=1 2>/dev/null \
|
||||||
|
| od -t u2 | awk '{print $2}'| head -n 1)
|
||||||
|
|
||||||
|
RANDOM_NUMBER=$(( RN % UPPER_LIMIT + 1))
|
||||||
|
|
||||||
|
echo "$RANDOM_NUMBER"
|
||||||
|
}
|
||||||
|
|
||||||
42
chapter12/function_load_default_keyboard
Executable file
42
chapter12/function_load_default_keyboard
Executable file
@@ -0,0 +1,42 @@
|
|||||||
|
function load_default_keyboard
|
||||||
|
{
|
||||||
|
# If a keyboard data file does not exist then the user
|
||||||
|
# prompted to load the standard keyboard data into the
|
||||||
|
# $KEYBOARD_FILE, which is defined in the main body of
|
||||||
|
# the shell script.
|
||||||
|
|
||||||
|
clear # Clear the screen
|
||||||
|
|
||||||
|
$ECHO "\nLoad the default keyboard data file? (Y/N): \c"
|
||||||
|
read REPLY
|
||||||
|
|
||||||
|
case $REPLY in
|
||||||
|
y|Y) :
|
||||||
|
;;
|
||||||
|
*) $ECHO "\nSkipping the load of the default keyboard file...\n"
|
||||||
|
return
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
cat /dev/null > $KEYBOARD_FILE
|
||||||
|
|
||||||
|
$ECHO "\nLoading the Standard Keyboard File...\c"
|
||||||
|
|
||||||
|
# Loop through each character in the following list and
|
||||||
|
# append each character to the $KEYBOARD_FILE file. This
|
||||||
|
# produces a file with one character on each line.
|
||||||
|
|
||||||
|
for CHAR in \` 1 2 3 4 5 6 7 8 9 0 - = \\ q w e r t y u i o \
|
||||||
|
p \[ \] a s d f g h j k l \; \' z x c v b n m \, \
|
||||||
|
\. \/ \\ \~ \! \@ \# \$ \% \^ \& \* \( \) _ \+ \| \
|
||||||
|
Q W E R T Y U I O P \{ \} A S D F G H J K L \: \" \
|
||||||
|
Z X C V B N M \< \> \? \| \. 0 1 2 3 4 5 6 7 8 9 \/ \
|
||||||
|
\* \- \+
|
||||||
|
do
|
||||||
|
$ECHO "$CHAR" >> $KEYBOARD_FILE
|
||||||
|
done
|
||||||
|
$ECHO "\n\n\t...Done...\n"
|
||||||
|
|
||||||
|
sleep 1
|
||||||
|
}
|
||||||
|
|
||||||
463
chapter12/mk_passwd.ksh
Executable file
463
chapter12/mk_passwd.ksh
Executable file
@@ -0,0 +1,463 @@
|
|||||||
|
#!/bin/ksh
|
||||||
|
#
|
||||||
|
# AUTHOR: Randy Micahel
|
||||||
|
# SCRIPT: mk_passwd.ksh
|
||||||
|
# DATE: 11/12/2007
|
||||||
|
# REV: 2.5.P
|
||||||
|
#
|
||||||
|
# PLATFORM: Not Platform Dependent
|
||||||
|
#
|
||||||
|
# PURPOSE: This script is used to create pseudo-random passwords.
|
||||||
|
# An extrernal keyboard data file is utilized, which is
|
||||||
|
# defined by the KEYBOARD_FILE variable. This keyboard
|
||||||
|
# file is expected to have one character on each line.
|
||||||
|
# These characters are loaded into an array, and using
|
||||||
|
# pseudo-random numbers generated, the characters are
|
||||||
|
# "randomly" put together to form a string of characters.
|
||||||
|
# By default, this script produces 8 character passwords,
|
||||||
|
# but this length can be changed on the command line by
|
||||||
|
# adding an integer value after the script name. There are
|
||||||
|
# two command line options, -n, which creates the default
|
||||||
|
# KEYBOARD_FILE, and -m, which prints the manager's
|
||||||
|
# password report. This password report is intended
|
||||||
|
# to be lock in a safe for safe keeping.
|
||||||
|
#
|
||||||
|
# EXIT CODES:
|
||||||
|
# 0 - Normal script execution
|
||||||
|
# 1 - Usage error
|
||||||
|
# 2 - Trap exit
|
||||||
|
# 3 - Missing Keyboard data file
|
||||||
|
# 4 - $DEFAULT_PRINTER is NULL
|
||||||
|
# 5 - /dev/urandom file does not exist
|
||||||
|
#
|
||||||
|
# REV LIST:
|
||||||
|
# 6/26/2007: Added two command line options, -n, which
|
||||||
|
# creates a new $KEYBOARD_FILE, and -m, which prints
|
||||||
|
# the managers password report.
|
||||||
|
#
|
||||||
|
# 8/8/2007: Changed the random number method from the
|
||||||
|
# shell RANDOM variable to now use the /dev/urandom
|
||||||
|
# character special file in concert with dd and od.
|
||||||
|
#
|
||||||
|
# set -x # Uncomment to debug
|
||||||
|
# set -n # Uncomment to check syntax without any command execution
|
||||||
|
#
|
||||||
|
####################################################
|
||||||
|
########### DEFINE SOME VARIABLES HERE #############
|
||||||
|
####################################################
|
||||||
|
|
||||||
|
LENGTH=8 # Default Password Length
|
||||||
|
|
||||||
|
# Notification List for Printing the Manager's
|
||||||
|
# Password Report for Locking Away Passwords
|
||||||
|
# Just in Case You are Unavaliable.
|
||||||
|
|
||||||
|
NOTIFICATION_LIST="Donald Duck, Yogi Bear, and Mr. Ranger"
|
||||||
|
|
||||||
|
# Define the Default Printer for Printing the Manager's
|
||||||
|
# Password Report. The user has a chance to change this
|
||||||
|
# printer at execution time.
|
||||||
|
|
||||||
|
DEFAULT_PRINTER="hp4@yogi"
|
||||||
|
|
||||||
|
SCRIPT=$(basename $0)
|
||||||
|
|
||||||
|
OUTFILE="/tmp/tmppdw.file"
|
||||||
|
|
||||||
|
KEYBOARD_FILE=/scripts/keyboard.keys
|
||||||
|
|
||||||
|
PRINT_PASSWORD_MANAGER_REPORT="TO_BE_SET"
|
||||||
|
|
||||||
|
# This next test ensures we use the correct
|
||||||
|
# echo command based on the executing shell
|
||||||
|
|
||||||
|
ECHO="echo -e"
|
||||||
|
[[ $(basename $SHELL) = ksh ]] && ECHO=echo
|
||||||
|
|
||||||
|
####################################################
|
||||||
|
########## DEFINE FUNCTIONS HERE ###################
|
||||||
|
####################################################
|
||||||
|
#
|
||||||
|
function in_range_random_number
|
||||||
|
{
|
||||||
|
# Create a pseudo-random number less than or equal
|
||||||
|
# to the $UPPER_LIMIT value, which is defined in the
|
||||||
|
# main body of the shell script.
|
||||||
|
|
||||||
|
RN=$(dd if=/dev/urandom count=1 2>/dev/null \
|
||||||
|
| od -t u2 | awk '{print $2}'| head -n 1)
|
||||||
|
|
||||||
|
RANDOM_NUMBER=$(( RN % UPPER_LIMIT + 1))
|
||||||
|
|
||||||
|
echo "$RANDOM_NUMBER"
|
||||||
|
}
|
||||||
|
#
|
||||||
|
####################################################
|
||||||
|
#
|
||||||
|
function load_default_keyboard
|
||||||
|
{
|
||||||
|
# If a keyboard data file does not exist then the user
|
||||||
|
# prompted to load the standard keyboard data into the
|
||||||
|
# $KEYBOARD_FILE, which is defined in the main body of
|
||||||
|
# the shell script.
|
||||||
|
|
||||||
|
clear # Clear the screen
|
||||||
|
|
||||||
|
$ECHO "\nLoad the default keyboard data file? (Y/N): \c"
|
||||||
|
read REPLY
|
||||||
|
|
||||||
|
case $REPLY in
|
||||||
|
y|Y) :
|
||||||
|
;;
|
||||||
|
*) $ECHO "\nSkipping the load of the default keyboard file...\n"
|
||||||
|
return
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
cat /dev/null > $KEYBOARD_FILE
|
||||||
|
|
||||||
|
$ECHO "\nLoading the Standard Keyboard File...\c"
|
||||||
|
|
||||||
|
# Loop through each character in the following list and
|
||||||
|
# append each character to the $KEYBOARD_FILE file. This
|
||||||
|
# produces a file with one character on each line.
|
||||||
|
|
||||||
|
for CHAR in \` 1 2 3 4 5 6 7 8 9 0 - = \\ q w e r t y u i o \
|
||||||
|
p \[ \] a s d f g h j k l \; \' z x c v b n m \, \
|
||||||
|
\. \/ \\ \~ \! \@ \# \$ \% \^ \& \* \( \) _ \+ \| \
|
||||||
|
Q W E R T Y U I O P \{ \} A S D F G H J K L \: \" \
|
||||||
|
Z X C V B N M \< \> \? \| \. 0 1 2 3 4 5 6 7 8 9 \/ \
|
||||||
|
\* \- \+
|
||||||
|
do
|
||||||
|
$ECHO "$CHAR" >> $KEYBOARD_FILE
|
||||||
|
done
|
||||||
|
$ECHO "\n\n\t...Done...\n"
|
||||||
|
|
||||||
|
sleep 1
|
||||||
|
}
|
||||||
|
#
|
||||||
|
####################################################
|
||||||
|
#
|
||||||
|
function check_for_and_create_keyboard_file
|
||||||
|
{
|
||||||
|
# If the $KEYBOARD_FILE does not exist then
|
||||||
|
# ask the user to load the "standard" keyboard
|
||||||
|
# layout, which is done with the load_default_keyboard
|
||||||
|
# function.
|
||||||
|
|
||||||
|
if [ ! -s $KEYBOARD_FILE ]
|
||||||
|
then
|
||||||
|
$ECHO "\n\nERROR: Missing Keyboard File"
|
||||||
|
$ECHO "\n\nWould You Like to Load the"
|
||||||
|
$ECHO "Default Keyboard Layout?"
|
||||||
|
$ECHO "\n\t(Y/N): \c"
|
||||||
|
typeset -u REPLY=FALSE
|
||||||
|
read REPLY
|
||||||
|
if [ $REPLY != Y ]
|
||||||
|
then
|
||||||
|
$ECHO "\n\nERROR: This shell script cannot operate"
|
||||||
|
$ECHO "without a keyboard data file located in"
|
||||||
|
$ECHO "\n==> $KEYBOARD_FILE\n"
|
||||||
|
$ECHO "\nThis file expects one character per line."
|
||||||
|
$ECHO "\n\t...EXITING...\n"
|
||||||
|
exit 3
|
||||||
|
else
|
||||||
|
load_default_keyboard
|
||||||
|
$ECHO "\nPress ENTER when you are you ready to continue: \c"
|
||||||
|
read REPLY
|
||||||
|
clear
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
#
|
||||||
|
####################################################
|
||||||
|
#
|
||||||
|
function build_manager_password_report
|
||||||
|
{
|
||||||
|
# Build a file to print for the secure envelope
|
||||||
|
(
|
||||||
|
$ECHO "\n RESTRICTED USE!!!"
|
||||||
|
$ECHO "\n\n\tImmediately send an e-mail to:\n"
|
||||||
|
|
||||||
|
$ECHO " $NOTIFICATION_LIST"
|
||||||
|
|
||||||
|
$ECHO "\n\tif this password is revealed!"
|
||||||
|
$ECHO "\n\tAIX root password: $PW\n"
|
||||||
|
|
||||||
|
$ECHO "\n\n"
|
||||||
|
|
||||||
|
$ECHO "\n RESTRICTED USE!!!"
|
||||||
|
$ECHO "\n\n\tImmediately send an e-mail to:\n"
|
||||||
|
|
||||||
|
$ECHO " $NOTIFICATION_LIST"
|
||||||
|
|
||||||
|
$ECHO "\n\tif this password is revealed!"
|
||||||
|
$ECHO "\n\tAIX root password: $PW\n"
|
||||||
|
|
||||||
|
$ECHO "\n\n"
|
||||||
|
|
||||||
|
$ECHO "\n RESTRICTED USE!!!"
|
||||||
|
$ECHO "\n\n\tImmediately send an e-mail to:\n"
|
||||||
|
|
||||||
|
$ECHO " $NOTIFICATION_LIST"
|
||||||
|
|
||||||
|
$ECHO "\n\tif this password is revealed!"
|
||||||
|
$ECHO "\n\tAIX root password: $PW\n"
|
||||||
|
|
||||||
|
) > $OUTFILE
|
||||||
|
|
||||||
|
}
|
||||||
|
#
|
||||||
|
####################################################
|
||||||
|
#
|
||||||
|
function usage
|
||||||
|
{
|
||||||
|
$ECHO "\nUSAGE: $SCRIPT [-m] [-n] [password_length]\n"
|
||||||
|
$ECHO " Where:
|
||||||
|
|
||||||
|
-m Creates a password printout for Security
|
||||||
|
|
||||||
|
-n Loads the default keyboard data keys file
|
||||||
|
|
||||||
|
password_length - Interger value that overrides
|
||||||
|
the default 8 character
|
||||||
|
password length.\n"
|
||||||
|
}
|
||||||
|
#
|
||||||
|
####################################################
|
||||||
|
#
|
||||||
|
function trap_exit
|
||||||
|
{
|
||||||
|
rm -f $OUTFILE >/dev/null 2>&1
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################################
|
||||||
|
########## END OF FUNCTION DEFINITIONS #############
|
||||||
|
####################################################
|
||||||
|
|
||||||
|
####################################################
|
||||||
|
########## ENSURE /dev/urandom EXISTS ##############
|
||||||
|
####################################################
|
||||||
|
|
||||||
|
if ! [ -c /dev/urandom ]
|
||||||
|
then
|
||||||
|
echo "\nERROR: This version of $(uname) does not have a /dev/urandom
|
||||||
|
character special file. This script requires this file to create
|
||||||
|
pseudo random numbers."
|
||||||
|
echo "\n...EXITING...\n"
|
||||||
|
usage
|
||||||
|
exit 5
|
||||||
|
fi
|
||||||
|
|
||||||
|
####################################################
|
||||||
|
####### VALIDATE EACH COMMAND LINE ARGUMENT ########
|
||||||
|
####################################################
|
||||||
|
|
||||||
|
# Check command line arguments - $# < 3
|
||||||
|
|
||||||
|
if (($# > 3))
|
||||||
|
then
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
####################################################
|
||||||
|
#
|
||||||
|
# Test for valid command line arguments -
|
||||||
|
# Valid auguments are "-n, -N, -m, -M, and any integer
|
||||||
|
|
||||||
|
if (($# != 0))
|
||||||
|
then
|
||||||
|
for CMD_ARG in $@
|
||||||
|
do
|
||||||
|
case $CMD_ARG in
|
||||||
|
+([-0-9]))
|
||||||
|
# The '+([-0-9]))' test notation is looking for
|
||||||
|
# an integer. Any integer is assigned to the
|
||||||
|
# length of password variable, LENGTH
|
||||||
|
|
||||||
|
LENGTH=$CMD_ARG
|
||||||
|
;;
|
||||||
|
-n) :
|
||||||
|
;;
|
||||||
|
-N) :
|
||||||
|
;;
|
||||||
|
-m) :
|
||||||
|
;;
|
||||||
|
-M) :
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
fi
|
||||||
|
|
||||||
|
####################################################
|
||||||
|
#
|
||||||
|
# Ensure that the $LENGTH variable is an integer
|
||||||
|
|
||||||
|
case $LENGTH in
|
||||||
|
+([0-9])) : # The '+([-0]))' test notation is looking for
|
||||||
|
# an integer. If an integer then the
|
||||||
|
# No-Op, specified by a colon, (Do Nothina)i
|
||||||
|
# command is executed, otherwise this script
|
||||||
|
# exits with a return code of 1, one.
|
||||||
|
;;
|
||||||
|
*) usage
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
####################################################
|
||||||
|
#
|
||||||
|
# Use the getopts function to parse the command
|
||||||
|
# line arguments.
|
||||||
|
|
||||||
|
while getopts ":nNmM" ARGUMENT 2>/dev/null
|
||||||
|
do
|
||||||
|
case $ARGUMENT in
|
||||||
|
n|N)
|
||||||
|
# Create a new Keyboard Data file
|
||||||
|
load_default_keyboard
|
||||||
|
$ECHO "\nPress ENTER when you are you ready to continue: \c"
|
||||||
|
read REPLY
|
||||||
|
clear
|
||||||
|
;;
|
||||||
|
m|M)
|
||||||
|
# Print the Manager Password Report
|
||||||
|
PRINT_PASSWORD_MANAGER_REPORT=TRUE
|
||||||
|
;;
|
||||||
|
\?) # Show the usage message
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
####################################################
|
||||||
|
################ START OF MAIN #####################
|
||||||
|
####################################################
|
||||||
|
|
||||||
|
# Set a trap
|
||||||
|
|
||||||
|
trap 'trap_exit;exit 2' 1 2 3 15
|
||||||
|
|
||||||
|
####################################################
|
||||||
|
#
|
||||||
|
# Check for a keyboard data file
|
||||||
|
|
||||||
|
check_for_and_create_keyboard_file
|
||||||
|
|
||||||
|
####################################################
|
||||||
|
############### LOAD THE ARRAY #####################
|
||||||
|
####################################################
|
||||||
|
|
||||||
|
X=0 # Initialize the array counter to zero
|
||||||
|
|
||||||
|
# Load the array called "KEYS" with keyboard elements
|
||||||
|
# located in the $KEYBOARD_FILE.
|
||||||
|
|
||||||
|
while read ARRAY_ELEMENT
|
||||||
|
do
|
||||||
|
((X = X + 1)) # Increment the counter by 1
|
||||||
|
|
||||||
|
# Load an array element in the the array
|
||||||
|
|
||||||
|
KEYS[$X]=$ARRAY_ELEMENT
|
||||||
|
|
||||||
|
done < $KEYBOARD_FILE
|
||||||
|
|
||||||
|
|
||||||
|
UPPER_LIMIT=$X # Random Number Upper Limit
|
||||||
|
|
||||||
|
####################################################
|
||||||
|
#
|
||||||
|
# Create the pseudo-random password in this section
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
clear # Clear the screen
|
||||||
|
|
||||||
|
PW= # Initialize the password to NULL
|
||||||
|
|
||||||
|
# Build the password using random numbers to grab array
|
||||||
|
# elements from the KEYS array.
|
||||||
|
|
||||||
|
X=0
|
||||||
|
while ((X < LENGTH))
|
||||||
|
do
|
||||||
|
# Increment the password length counter
|
||||||
|
(( X = X + 1 ))
|
||||||
|
|
||||||
|
# Build the password one char at a time in this loop
|
||||||
|
PW=${PW}${KEYS[$(in_range_random_number $UPPER_LIMIT)]}
|
||||||
|
done
|
||||||
|
|
||||||
|
# Done building the password
|
||||||
|
|
||||||
|
####################################################
|
||||||
|
#
|
||||||
|
# Display the new pseudo-random password to the screen
|
||||||
|
|
||||||
|
$ECHO "\n\n The new $LENGTH character password is:\n"
|
||||||
|
$ECHO "\n ${PW}\n"
|
||||||
|
|
||||||
|
####################################################
|
||||||
|
#
|
||||||
|
# Print the Manager's password report, if specified
|
||||||
|
# on the command with the -m command switch.
|
||||||
|
|
||||||
|
if [ $PRINT_PASSWORD_MANAGER_REPORT = TRUE ]
|
||||||
|
then
|
||||||
|
|
||||||
|
typeset -u REPLY=N
|
||||||
|
|
||||||
|
$ECHO "\nPrint Password Sheet for the Secure Envelope? (Y/N)? \c"
|
||||||
|
read REPLY
|
||||||
|
|
||||||
|
if [[ $REPLY = 'Y' ]]
|
||||||
|
then
|
||||||
|
build_manager_password_report
|
||||||
|
|
||||||
|
REPLY= # Set REPLY to NULL
|
||||||
|
|
||||||
|
$ECHO "\nPrint to the Default Printer ${DEFAULT_PRINTER} (Y/N)? \c"
|
||||||
|
read REPLY
|
||||||
|
if [[ $REPLY = 'Y' ]]
|
||||||
|
then
|
||||||
|
$ECHO "\nPrinting to $DEFAULT_PRINTER\n"
|
||||||
|
lp -c -d $DEFAULT_PRINTER $OUTFILE
|
||||||
|
|
||||||
|
else
|
||||||
|
$ECHO "\nNEW PRINT QUEUE: \c"
|
||||||
|
read DEFAULT_PRINTER
|
||||||
|
if [ -z "$DEFAULT_PRINTER" ]
|
||||||
|
then
|
||||||
|
echo "ERROR: Default printer canot be NULL...Exiting..."
|
||||||
|
exit 5
|
||||||
|
fi
|
||||||
|
$ECHO "\nPrinting to $DEFAULT_PRINTER\n"
|
||||||
|
lp -c -d $DEFAULT_PRINTER $OUTFILE
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
$ECHO "\n\n\tO.K. - Printing Skipped..."
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
####################################################
|
||||||
|
#
|
||||||
|
# Remove the $OUTFILE, if it exists and has a size
|
||||||
|
# greater than zero bytes.
|
||||||
|
|
||||||
|
[ -s $OUTFILE ] && rm $OUTFILE
|
||||||
|
|
||||||
|
####################################################
|
||||||
|
#
|
||||||
|
# Clear the screen and exit
|
||||||
|
|
||||||
|
$ECHO "\n\nPress ENTER to Clear the Screen and EXIT: \c"
|
||||||
|
read X
|
||||||
|
clear
|
||||||
|
|
||||||
|
# End of mk_passwd.ksh shell script
|
||||||
234
chapter13/float_add.ksh
Executable file
234
chapter13/float_add.ksh
Executable file
@@ -0,0 +1,234 @@
|
|||||||
|
#!/usr/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: float_add.ksh
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 03/01/2007
|
||||||
|
# REV: 1.1.A
|
||||||
|
#
|
||||||
|
# PURPOSE: This shell script is used to add a list of numbers
|
||||||
|
# together. The numbers can be either integers or floating-
|
||||||
|
# point numbers. For floating-point numbers the user has
|
||||||
|
# the option of specifying a scale of the number of digits to
|
||||||
|
# the right of the decimal point. The scale is set by adding
|
||||||
|
# a -s or -S followed by an integer number.
|
||||||
|
#
|
||||||
|
# EXIT CODES:
|
||||||
|
# 0 ==> This script completed without error
|
||||||
|
# 1 ==> Usage error
|
||||||
|
# 2 ==> This script exited on a trapped signal
|
||||||
|
#
|
||||||
|
# REV. LIST:
|
||||||
|
#
|
||||||
|
#
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
# set -n # Uncomment to debug without any command execution
|
||||||
|
#
|
||||||
|
########################################################
|
||||||
|
############## DEFINE VARIABLE HERE ####################
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
SCRIPT_NAME=$(basename $0) # The name of this shell script
|
||||||
|
SCALE="0" # Initialize the scale value to zero
|
||||||
|
NUM_LIST= # Initialize the NUM_LIST variable to NULL
|
||||||
|
COUNT=0 # Initialize the counter to zero
|
||||||
|
MAX_COUNT=$# # Set MAX_COUNT to the total number of
|
||||||
|
# command-line arguments.
|
||||||
|
|
||||||
|
########################################################
|
||||||
|
################ FUNCTIONS #############################
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
function usage
|
||||||
|
{
|
||||||
|
echo "\nPURPOSE: Adds a list of numbers together\n"
|
||||||
|
echo "USAGE: $SCRIPT_NAME [-s scale_value] N1 N2...Nn"
|
||||||
|
echo "\nFor an integer result without any significant decimal places..."
|
||||||
|
echo "\nEXAMPLE: $SCRIPT_NAME 2048.221 65536 \n"
|
||||||
|
echo "OR for 4 significant decimal places"
|
||||||
|
echo "\nEXAMPLE: $SCRIPT_NAME -s 4 8.09838 2048 65536 42.632"
|
||||||
|
echo "\n\t...EXITING...\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
function exit_trap
|
||||||
|
{
|
||||||
|
echo "\n...EXITING on trapped signal...\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
########################################################
|
||||||
|
################# START OF MAIN ########################
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
###### Set a Trap ######
|
||||||
|
|
||||||
|
trap 'exit_trap; exit 2' 1 2 3 15
|
||||||
|
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
# Check for at least two command-line arguments
|
||||||
|
|
||||||
|
if [ $# -lt 2 ]
|
||||||
|
then
|
||||||
|
echo "\nERROR: Please provide a list of numbers to add"
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Parse the command-line arguments to find the scale value, if present.
|
||||||
|
|
||||||
|
while getopts ":s:S:" ARGUMENT
|
||||||
|
do
|
||||||
|
case $ARGUMENT in
|
||||||
|
s|S) SCALE=$OPTARG
|
||||||
|
;;
|
||||||
|
\?) # Because we may have negative numbers we need
|
||||||
|
# to test to see if the ARGUMENT that begins with a
|
||||||
|
# hyphen (-) is a number, and not an invalid switch!!!
|
||||||
|
|
||||||
|
for TST_ARG in $*
|
||||||
|
do
|
||||||
|
if [[ $(echo $TST_ARG | cut -c1) = '-' ]] \
|
||||||
|
&& [ $TST_ARG != '-s' -a $TST_ARG != '-S' ]
|
||||||
|
then
|
||||||
|
case $TST_ARG in
|
||||||
|
+([-0-9])) : # No-op, do nothing
|
||||||
|
;;
|
||||||
|
+([-0-9].[0-9]))
|
||||||
|
: # No-op, do nothing
|
||||||
|
;;
|
||||||
|
+([-.0-9])) : # No-op, do nothing
|
||||||
|
;;
|
||||||
|
*) echo "\nERROR: Invalid argument \
|
||||||
|
on the command line"
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
# Parse through the command-line arguments and gather a list
|
||||||
|
# of numbers to add together and test each value.
|
||||||
|
|
||||||
|
while ((COUNT < MAX_COUNT))
|
||||||
|
do
|
||||||
|
((COUNT = COUNT + 1))
|
||||||
|
TOKEN=$1 # Grab a command-line argument on each loop iteration
|
||||||
|
case $TOKEN in # Test each value and look for a scale value.
|
||||||
|
-s|-S) shift 2
|
||||||
|
((COUNT = COUNT + 1))
|
||||||
|
;;
|
||||||
|
-s${SCALE}) shift
|
||||||
|
;;
|
||||||
|
-S${SCALE}) shift
|
||||||
|
;;
|
||||||
|
*) # Add the number ($TOKEN) to the list
|
||||||
|
NUM_LIST="${NUM_LIST} $TOKEN"
|
||||||
|
((COUNT < MAX_COUNT)) && shift
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
# Ensure that the scale is an integer value
|
||||||
|
|
||||||
|
case $SCALE in
|
||||||
|
# Test for an integer
|
||||||
|
+([0-9])) : # No-Op - Do Nothing
|
||||||
|
;;
|
||||||
|
*) echo "\nERROR: Invalid scale - $SCALE - \
|
||||||
|
Must be an integer"
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
# Check each number supplied to ensure that the "numbers"
|
||||||
|
# are either integers or floating-point numbers.
|
||||||
|
|
||||||
|
for NUM in $NUM_LIST
|
||||||
|
do
|
||||||
|
case $NUM in
|
||||||
|
+([0-9])) # Check for an integer
|
||||||
|
: # No-op, do nothing.
|
||||||
|
;;
|
||||||
|
+([-0-9])) # Check for a negative whole number
|
||||||
|
: # No-op, do nothing
|
||||||
|
;;
|
||||||
|
+([0-9]|[.][0-9]))
|
||||||
|
# Check for a positive floating-point number
|
||||||
|
: # No-op, do nothing
|
||||||
|
;;
|
||||||
|
+(+[0-9][.][0-9]))
|
||||||
|
# Check for a positive floating-point number
|
||||||
|
# with a + prefix
|
||||||
|
: # No-op, do nothing
|
||||||
|
;;
|
||||||
|
+(-[0-9][.][0-9]))
|
||||||
|
# Check for a negative floating-point number
|
||||||
|
: # No-op, do nothing
|
||||||
|
;;
|
||||||
|
+([-.0-9]))
|
||||||
|
# Check for a negative floating-point number
|
||||||
|
: # No-op, do nothing
|
||||||
|
;;
|
||||||
|
+([+.0-9]))
|
||||||
|
# Check for a positive floating-point number
|
||||||
|
: # No-op, do nothing
|
||||||
|
;;
|
||||||
|
*) echo "\nERROR: $NUM is NOT a valid number"
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
# Build the list of numbers to add
|
||||||
|
|
||||||
|
ADD= # Initialize the ADD variable to NULL
|
||||||
|
PLUS= # Initialize the PLUS variable to NULL
|
||||||
|
|
||||||
|
# Loop through each number and build a math statement that
|
||||||
|
# will add all of the numbers together.
|
||||||
|
|
||||||
|
for X in $NUM_LIST
|
||||||
|
do
|
||||||
|
# If the number has a + prefix, remove it!
|
||||||
|
if [[ $(echo $X | cut -c1) = '+' ]]
|
||||||
|
then
|
||||||
|
X=$(echo $X | cut -c2-)
|
||||||
|
fi
|
||||||
|
ADD="$ADD $PLUS $X"
|
||||||
|
PLUS="+"
|
||||||
|
done
|
||||||
|
|
||||||
|
########################################################
|
||||||
|
# Do the math here by using a here document to supply
|
||||||
|
# input to the bc command. The sum of the numbers is
|
||||||
|
# assigned to the SUM variable.
|
||||||
|
|
||||||
|
echo "\nSCALE is $SCALE\n"
|
||||||
|
|
||||||
|
|
||||||
|
SUM=$(bc <<EOF
|
||||||
|
scale=$SCALE
|
||||||
|
(${ADD})
|
||||||
|
EOF)
|
||||||
|
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
# Present the result of the addition to the user.
|
||||||
|
|
||||||
|
echo "\nThe sum of: $ADD"
|
||||||
|
echo "\nis: ${SUM}\n"
|
||||||
131
chapter13/float_average.ksh
Executable file
131
chapter13/float_average.ksh
Executable file
@@ -0,0 +1,131 @@
|
|||||||
|
#!/usr/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: float_aaverage.ksh
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 03/01/2007
|
||||||
|
# REV: 1.1.A
|
||||||
|
#
|
||||||
|
# PURPOSE: This script is used to average a list of
|
||||||
|
# floating-point numbers.
|
||||||
|
#
|
||||||
|
# EXIT STATUS:
|
||||||
|
# 0 ==> This script completed without error
|
||||||
|
# 1 ==> Usage error
|
||||||
|
# 2 ==> This script exited on a trapped signal
|
||||||
|
#
|
||||||
|
# REV. LIST:
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
# set -n # Uncomment to debug without any command execution
|
||||||
|
|
||||||
|
SCRIPT_NAME=`basename $0`
|
||||||
|
ARG1="$1"
|
||||||
|
ARG2="$2"
|
||||||
|
ARG_LIST="$*"
|
||||||
|
TOTAL_TOKENS="$#"
|
||||||
|
SCALE="0"
|
||||||
|
|
||||||
|
########################################################
|
||||||
|
################ FUNCTIONS #############################
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
function usage
|
||||||
|
{
|
||||||
|
echo "\n\n"
|
||||||
|
echo "Numbers to average...\n"
|
||||||
|
echo "USAGE: $SCRIPT_NAME [-s scale_value] N1 N2...N#"
|
||||||
|
echo "\nFor an integer result without any significant decimal places..."
|
||||||
|
echo "\nEXAMPLE: $SCRIPT_NAME 2048.221 65536 \n"
|
||||||
|
echo "OR for 4 significant decimal places"
|
||||||
|
echo "\nEXAMPLE: $SCRIPT_NAME -s 4 8.09838 2048 65536 42.632\n"
|
||||||
|
echo "Try again...EXITING...\n"
|
||||||
|
}
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
function exit_trap
|
||||||
|
{
|
||||||
|
echo "\n...EXITING on trapped signal...\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
########################################################
|
||||||
|
################ START OF MAIN #########################
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
###### Set a Trap ######
|
||||||
|
|
||||||
|
trap 'exit_trap; exit 2' 1 2 3 15
|
||||||
|
|
||||||
|
########################
|
||||||
|
|
||||||
|
if [ $# -lt 2 ]
|
||||||
|
then
|
||||||
|
echo "\nIncorrect number of command auguments...Nothing to average..."
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ $ARG1 = "-s" ] || [ $ARG1 = "-S" ]
|
||||||
|
then
|
||||||
|
echo $ARG2 | grep [[:digit:]] >/dev/null 2>&1
|
||||||
|
if [ $? -ne "0" ]
|
||||||
|
then
|
||||||
|
echo "\nERROR: Invalid argument - $ARG2 ... Must be an integer...\n"
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
echo $ARG2 | grep "\." >/dev/null 2>&1
|
||||||
|
if [ $? -ne "0" ]
|
||||||
|
then
|
||||||
|
SCALE="$ARG2"
|
||||||
|
NUM_LIST=`echo $ARG_LIST | cut -f 3- -d " "`
|
||||||
|
(( TOTAL_TOKENS = $TOTAL_TOKENS - 2 ))
|
||||||
|
else
|
||||||
|
echo "\nERROR: Invalid scale - $ARG2 ... Scale must be an integer...\n"
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
NUM_LIST=$ARG_LIST
|
||||||
|
fi
|
||||||
|
|
||||||
|
for TOKEN in $NUM_LIST
|
||||||
|
do
|
||||||
|
echo $TOKEN | grep [[:digit:]] >/dev/null 2>&1
|
||||||
|
RC=$?
|
||||||
|
case $RC in
|
||||||
|
0) cat /dev/null
|
||||||
|
;;
|
||||||
|
*) echo "\n$TOKEN is not a number...Invalid argument list"
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
|
||||||
|
# Build the list of numbers to add for averaging...
|
||||||
|
|
||||||
|
ADD=""
|
||||||
|
PLUS=""
|
||||||
|
for X in $NUM_LIST
|
||||||
|
do
|
||||||
|
ADD="$ADD $PLUS $X"
|
||||||
|
PLUS="+"
|
||||||
|
done
|
||||||
|
|
||||||
|
# Do the math here
|
||||||
|
|
||||||
|
AVERAGE=`bc <<EOF
|
||||||
|
scale=$SCALE
|
||||||
|
(${ADD}) / $TOTAL_TOKENS
|
||||||
|
EOF`
|
||||||
|
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
# Present the result of the average to the user.
|
||||||
|
|
||||||
|
echo "\nThe averaging equation: (${ADD}) / $TOTAL_TOKENS"
|
||||||
|
echo "\nto a scale of $SCALE is: ${AVERAGE}\n"
|
||||||
|
|
||||||
227
chapter13/float_divide.ksh
Executable file
227
chapter13/float_divide.ksh
Executable file
@@ -0,0 +1,227 @@
|
|||||||
|
#!/usr/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: float_divide.ksh
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 02/23/2007
|
||||||
|
# REV: 1.1.A
|
||||||
|
#
|
||||||
|
# PURPOSE: This shell script is used to divide two numbers.
|
||||||
|
# The numbers can be either integers or floating-point
|
||||||
|
# numbers. For floating-point numbers the user has the
|
||||||
|
# option to specify a scale of the number of digits to
|
||||||
|
# the right of the decimal point. The scale is set by
|
||||||
|
# adding a -s or -S followed by an integer number.
|
||||||
|
#
|
||||||
|
# EXIT STATUS:
|
||||||
|
# 0 ==> This script exited normally
|
||||||
|
# 1 ==> Usage or syntax error
|
||||||
|
# 2 ==> This script exited on a trapped signal
|
||||||
|
#
|
||||||
|
# REV. LIST:
|
||||||
|
#
|
||||||
|
#
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
# set -n # Uncomment to debug without any command execution
|
||||||
|
#
|
||||||
|
########################################################
|
||||||
|
############## DEFINE VARIABLE HERE ####################
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
SCRIPT_NAME=`basename $0`
|
||||||
|
SCALE="0" # Initialize the scale value to zero
|
||||||
|
NUM_LIST= # Initialize the NUM_LIST to NULL
|
||||||
|
COUNT=0 # Initialize the counter to zero
|
||||||
|
MAX_COUNT=$# # Set MAX_COUNT to the total number of
|
||||||
|
# command-line arguments
|
||||||
|
|
||||||
|
########################################################
|
||||||
|
################ FUNCTIONS #############################
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
function usage
|
||||||
|
{
|
||||||
|
echo "\nPURPOSE: Divides two numbers\n"
|
||||||
|
echo "USAGE: $SCRIPT_NAME [-s scale_value] N1 N2"
|
||||||
|
echo "\nFor an integer result without any significant decimal places..."
|
||||||
|
echo "\nEXAMPLE: $SCRIPT_NAME 2048.221 65536 \n"
|
||||||
|
echo "OR for 4 significant decimal places"
|
||||||
|
echo "\nEXAMPLE: $SCRIPT_NAME -s 4 2048.221 65536"
|
||||||
|
echo "\n\t...EXITING...\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
function exit_trap
|
||||||
|
{
|
||||||
|
echo "\n...EXITING on trapped signal...\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
########################################################
|
||||||
|
################ START OF MAIN #########################
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
###### Set a Trap ######
|
||||||
|
|
||||||
|
trap 'exit_trap; exit 2' 1 2 3 15
|
||||||
|
|
||||||
|
########################
|
||||||
|
|
||||||
|
# Check for at least two command-line arguments
|
||||||
|
# and not more than four
|
||||||
|
|
||||||
|
if (($# < 2))
|
||||||
|
then
|
||||||
|
echo "\nERROR: Too few command line arguments"
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
elif (($# > 4))
|
||||||
|
then
|
||||||
|
echo "\nERROR: Too many command line arguments"
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Parse the command-line arguments to find the scale value, if present.
|
||||||
|
|
||||||
|
while getopts ":s:S:" ARGUMENT
|
||||||
|
do
|
||||||
|
case $ARGUMENT in
|
||||||
|
s|S) SCALE=$OPTARG
|
||||||
|
;;
|
||||||
|
\?) # Because we may have negative numbers we need
|
||||||
|
# to test to see if the ARGUMENT that begins with a
|
||||||
|
# hyphen (-) is a number, and not an invalid switch!!!
|
||||||
|
|
||||||
|
for TST_ARG in $*
|
||||||
|
do
|
||||||
|
if [[ $(echo $TST_ARG | cut -c1) = '-' ]] \
|
||||||
|
&& [ $TST_ARG != '-s' -a $TST_ARG != '-S' ]
|
||||||
|
then
|
||||||
|
case $TST_ARG in
|
||||||
|
+([-0-9])) : # No-op, do nothing
|
||||||
|
;;
|
||||||
|
+([-0-9].[0-9]))
|
||||||
|
: # No-op, do nothing
|
||||||
|
;;
|
||||||
|
+([-.0-9])) : # No-op, do nothing
|
||||||
|
;;
|
||||||
|
*) echo "\nERROR: $TST_ARG is an invalid argument\n"
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
# Parse through the command-line arguments and gather a list
|
||||||
|
# of numbers to divide.
|
||||||
|
|
||||||
|
TOTAL_NUMBERS=0
|
||||||
|
|
||||||
|
while ((COUNT < MAX_COUNT))
|
||||||
|
do
|
||||||
|
((COUNT = COUNT + 1))
|
||||||
|
TOKEN=$1
|
||||||
|
case $TOKEN in
|
||||||
|
-s|-S) shift 2
|
||||||
|
((COUNT = COUNT + 1))
|
||||||
|
;;
|
||||||
|
-s${SCALE}) shift
|
||||||
|
;;
|
||||||
|
-S${SCALE}) shift
|
||||||
|
;;
|
||||||
|
*) ((TOTAL_NUMBERS = TOTAL_NUMBERS + 1))
|
||||||
|
if ((TOTAL_NUMBERS == 1))
|
||||||
|
then
|
||||||
|
DIVIDEND=$TOKEN
|
||||||
|
elif ((TOTAL_NUMBERS == 2))
|
||||||
|
then
|
||||||
|
DIVISOR=$TOKEN
|
||||||
|
else
|
||||||
|
echo "ERROR: Too many numbers to divide"
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
NUM_LIST="$NUM_LIST $TOKEN"
|
||||||
|
((COUNT < MAX_COUNT)) && shift
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
# Ensure that the scale is an integer value
|
||||||
|
|
||||||
|
case $SCALE in
|
||||||
|
+([0-9])) : # No-op - Do Nothing
|
||||||
|
;;
|
||||||
|
*) echo "\nERROR: Invalid scale - $SCALE - Must be an integer"
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
# Check each number supplied to ensure that the "numbers"
|
||||||
|
# are either integers or floating-point numbers.
|
||||||
|
|
||||||
|
for NUM in $NUM_LIST
|
||||||
|
do
|
||||||
|
case $NUM in
|
||||||
|
+([0-9])) # Check for an integer
|
||||||
|
: # No-op, do nothing.
|
||||||
|
;;
|
||||||
|
+([-0-9])) # Check for a negative whole number
|
||||||
|
: # No-op, do nothing
|
||||||
|
;;
|
||||||
|
+([0-9]|[.][0-9]))
|
||||||
|
# Check for a positive floating-point number
|
||||||
|
: # No-op, do nothing
|
||||||
|
;;
|
||||||
|
+(+[0-9]|[.][0-9]))
|
||||||
|
# Check for a positive floating-point number
|
||||||
|
# with a + prefix
|
||||||
|
: # No-op, do nothing
|
||||||
|
;;
|
||||||
|
+([-0-9]|.[0-9]))
|
||||||
|
# Check for a negative floating-point number
|
||||||
|
: # No-op, do nothing
|
||||||
|
;;
|
||||||
|
+(-.[0-9]))
|
||||||
|
# Check for a negative floating-point number
|
||||||
|
: # No-op, do nothing
|
||||||
|
;;
|
||||||
|
+([+.0-9]))
|
||||||
|
# Check for a positive floating-point number
|
||||||
|
: # No-op, do nothing
|
||||||
|
;;
|
||||||
|
*) echo "\nERROR: $NUM is NOT a valid number"
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
# Do the math here by using a here document to supply
|
||||||
|
# input to the bc command. The quotient of the division is
|
||||||
|
# assigned to the QUOTIENT variable.
|
||||||
|
|
||||||
|
QUOTIENT=$(bc <<EOF
|
||||||
|
scale=$SCALE
|
||||||
|
$DIVIDEND / $DIVISOR
|
||||||
|
EOF)
|
||||||
|
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
# Present the result of the division to the user.
|
||||||
|
|
||||||
|
echo "\nThe quotient of: $DIVIDEND / $DIVISOR"
|
||||||
|
echo "\nto a scale of $SCALE is ${QUOTIENT}\n"
|
||||||
233
chapter13/float_multiply.ksh
Executable file
233
chapter13/float_multiply.ksh
Executable file
@@ -0,0 +1,233 @@
|
|||||||
|
#!/usr/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: float_multiply.ksh
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 02/23/2007
|
||||||
|
# REV: 1.1.P
|
||||||
|
#
|
||||||
|
# PURPOSE: This shell script is used to multiply a list of numbers
|
||||||
|
# together. The numbers can be either integers or floating-
|
||||||
|
# point numbers. For floating-point numbers the user has
|
||||||
|
# the option of specifying a scale of the number of digits to
|
||||||
|
# the right of the decimal point. The scale is set by adding
|
||||||
|
# a -s or -S followed by an integer number.
|
||||||
|
#
|
||||||
|
# EXIT STATUS:
|
||||||
|
# 0 ==> This script/function exited normally
|
||||||
|
# 1 ==> Usage or syntax error
|
||||||
|
# 2 ==> This script/function exited on a trapped signal
|
||||||
|
#
|
||||||
|
# REV. LIST:
|
||||||
|
#
|
||||||
|
#
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
# set -n # Uncomment to debug without any command execution
|
||||||
|
#
|
||||||
|
########################################################
|
||||||
|
############## DEFINE VARIABLE HERE ####################
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
SCRIPT_NAME=$(basename $0) # The name of this shell script
|
||||||
|
SCALE="0" # Initialize the scale value to zero
|
||||||
|
NUM_LIST= # Initialize the NUM_LIST to NULL
|
||||||
|
COUNT=0 # Initialize the counter to zero
|
||||||
|
MAX_COUNT=$# # Set MAX_COUNT to the total number of
|
||||||
|
# command-line arguments
|
||||||
|
|
||||||
|
########################################################
|
||||||
|
################ FUNCTIONS #############################
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
function usage
|
||||||
|
{
|
||||||
|
echo "\nPURPOSE: Multiplies a list of numbers together\n"
|
||||||
|
echo "USAGE: $SCRIPT_NAME [-s scale_value] N1 N2...Nn"
|
||||||
|
echo "\nFor an integer result without any significant decimal places..."
|
||||||
|
echo "\nEXAMPLE: $SCRIPT_NAME 2048.221 65536 \n"
|
||||||
|
echo "OR for 4 significant decimal places"
|
||||||
|
echo "\nEXAMPLE: $SCRIPT_NAME -s 4 8.09838 2048 65536 42.632"
|
||||||
|
echo "\n\t...EXITING...\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
function exit_trap
|
||||||
|
{
|
||||||
|
echo "\n...EXITING on trapped signal...\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
########################################################
|
||||||
|
################# START OF MAIN ########################
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
###### Set a Trap ######
|
||||||
|
|
||||||
|
trap 'exit_trap; exit 2' 1 2 3 15
|
||||||
|
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
# Check for at least two command-line arguments
|
||||||
|
|
||||||
|
if (($# < 2))
|
||||||
|
then
|
||||||
|
echo "\nERROR: Please provide a list of numbers to multiply"
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
# Parse the command-line arguments to find the scale value, if present.
|
||||||
|
|
||||||
|
while getopts ":s:S:" ARGUMENT
|
||||||
|
do
|
||||||
|
case $ARGUMENT in
|
||||||
|
s|S) SCALE=$OPTARG
|
||||||
|
;;
|
||||||
|
\?) # Because we may have negative numbers we need
|
||||||
|
# to test to see if the ARGUMENT that begins with a
|
||||||
|
# hyphen (-) is a number, and not an invalid switch!!!
|
||||||
|
|
||||||
|
for TST_ARG in $*
|
||||||
|
do
|
||||||
|
if [[ $(echo $TST_ARG | cut -c1) = '-' ]] \
|
||||||
|
&& [ $TST_ARG != '-s' -a $TST_ARG != '-S' ]
|
||||||
|
then
|
||||||
|
case $TST_ARG in
|
||||||
|
+([-0-9])) : # No-op, do nothing
|
||||||
|
;;
|
||||||
|
+([-0-9].[0-9]))
|
||||||
|
: # No-op, do nothing
|
||||||
|
;;
|
||||||
|
+([-.0-9])) : # No-op, do nothing
|
||||||
|
;;
|
||||||
|
*) echo "\nERROR: $TST_ARG is an invalid argument\n"
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
# Parse through the command-line arguments and gather a list
|
||||||
|
# of numbers to multiply together.
|
||||||
|
|
||||||
|
while ((COUNT < MAX_COUNT))
|
||||||
|
do
|
||||||
|
((COUNT = COUNT + 1))
|
||||||
|
TOKEN=$1
|
||||||
|
case $TOKEN in
|
||||||
|
-s|-S) shift 2
|
||||||
|
((COUNT = COUNT + 1))
|
||||||
|
;;
|
||||||
|
-s${SCALE}) shift
|
||||||
|
;;
|
||||||
|
-S${SCALE}) shift
|
||||||
|
;;
|
||||||
|
*) NUM_LIST="${NUM_LIST} $TOKEN"
|
||||||
|
((COUNT < MAX_COUNT)) && shift
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
# Ensure that the scale is an integer value
|
||||||
|
|
||||||
|
case $SCALE in
|
||||||
|
+([0-9])) : # No-Op - Do Nothing
|
||||||
|
;;
|
||||||
|
*) echo "\nERROR: Invalid scale - $SCALE - Must be an integer"
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
# Check each number supplied to ensure that the "numbers"
|
||||||
|
# are either integers or floating-point numbers.
|
||||||
|
|
||||||
|
for NUM in $NUM_LIST
|
||||||
|
do
|
||||||
|
case $NUM in
|
||||||
|
+([0-9])) # Check for an integer
|
||||||
|
: # No-op, do nothing.
|
||||||
|
;;
|
||||||
|
+([-0-9])) # Check for a negative whole number
|
||||||
|
: # No-op, do nothing
|
||||||
|
;;
|
||||||
|
+([0-9]|[.][0-9]))
|
||||||
|
# Check for a positive floating-point number
|
||||||
|
: # No-op, do nothing
|
||||||
|
;;
|
||||||
|
+(+[0-9]|[.][0-9]))
|
||||||
|
# Check for a positive floating-point number
|
||||||
|
# with a + prefix
|
||||||
|
: # No-op, do nothing
|
||||||
|
;;
|
||||||
|
+([-0-9]|.[0-9]))
|
||||||
|
# Check for a negative floating-point number
|
||||||
|
: # No-op, do nothing
|
||||||
|
;;
|
||||||
|
+(-.[0-9]))
|
||||||
|
# Check for a negative floating-point number
|
||||||
|
: # No-op, do nothing
|
||||||
|
;;
|
||||||
|
+([+.0-9]))
|
||||||
|
# Check for a positive floating-point number
|
||||||
|
: # No-op, do nothing
|
||||||
|
;;
|
||||||
|
*) echo "\nERROR: $NUM is NOT a valid number"
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
# Build the list of numbers to multiply
|
||||||
|
|
||||||
|
MULTIPLY= # Initialize the MULTIPLY variable to NULL
|
||||||
|
TIMES= # Initialize the TIMES variable to NULL
|
||||||
|
|
||||||
|
# Loop through each number and build a math statement that
|
||||||
|
# will multiply all of the numbers together.
|
||||||
|
|
||||||
|
for X in $NUM_LIST
|
||||||
|
do
|
||||||
|
# If the number has a + prefix, remove it!
|
||||||
|
if [[ $(echo $X | cut -c1) = '+' ]]
|
||||||
|
then
|
||||||
|
X=$(echo $X | cut -c2-)
|
||||||
|
fi
|
||||||
|
MULTIPLY="$MULTIPLY $TIMES $X"
|
||||||
|
TIMES='*'
|
||||||
|
done
|
||||||
|
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
# Do the math here by using a here document to supply
|
||||||
|
# input to the bc command. The product of the multiplication
|
||||||
|
# of the numbers is assigned to the PRODUCT variable.
|
||||||
|
|
||||||
|
echo "\nSCALE is $SCALE\n"
|
||||||
|
|
||||||
|
|
||||||
|
PRODUCT=$(bc <<EOF
|
||||||
|
scale=$SCALE
|
||||||
|
$MULTIPLY
|
||||||
|
EOF)
|
||||||
|
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
# Present the result of the multiplication to the user.
|
||||||
|
|
||||||
|
echo "\nThe product of: $MULTIPLY"
|
||||||
|
echo "\nto a scale of $SCALE is ${PRODUCT}\n"
|
||||||
226
chapter13/float_subtract.ksh
Executable file
226
chapter13/float_subtract.ksh
Executable file
@@ -0,0 +1,226 @@
|
|||||||
|
#!/usr/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: float_subtract.ksh
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 02/23/2007
|
||||||
|
# REV: 1.1.A
|
||||||
|
#
|
||||||
|
# PURPOSE: This shell script is used to subtract a list of numbers.
|
||||||
|
# The numbers can be either integers or floating-point
|
||||||
|
# numbers. For floating-point numbers the user has the
|
||||||
|
# option to specify a scale of the number of digits to
|
||||||
|
# the right of the decimal point. The scale is set by
|
||||||
|
# adding a -s or -S followed by an integer number.
|
||||||
|
#
|
||||||
|
# EXIT STATUS:
|
||||||
|
# 0 ==> This script completed without error
|
||||||
|
# 1 ==> Usage error
|
||||||
|
# 2 ==> This script exited on a trapped signal
|
||||||
|
#
|
||||||
|
# REV. LIST:
|
||||||
|
#
|
||||||
|
#
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
# set -n # Uncomment to debug without any command execution
|
||||||
|
#
|
||||||
|
########################################################
|
||||||
|
############## DEFINE VARIABLE HERE ####################
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
SCRIPT_NAME=`basename $0` # The name of this shell script
|
||||||
|
SCALE="0" # Initialize the scale value to zero
|
||||||
|
NUM_LIST= # Initialize the NUM_LIST to NULL
|
||||||
|
COUNT=0 # Initialize the counter to zero
|
||||||
|
MAX_COUNT=$# # Set MAX_COUNT to the total number of
|
||||||
|
# command-line arguments
|
||||||
|
|
||||||
|
########################################################
|
||||||
|
################ FUNCTIONS #############################
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
function usage
|
||||||
|
{
|
||||||
|
echo "\nPURPOSE: Subtracts a list of numbers\n"
|
||||||
|
echo "USAGE: $SCRIPT_NAME [-s scale_value] N1 N2...Nn"
|
||||||
|
echo "\nFor an integer result without any significant decimal places..."
|
||||||
|
echo "\nEXAMPLE: $SCRIPT_NAME 2048.221 65536 \n"
|
||||||
|
echo "OR for 4 significant decimal places"
|
||||||
|
echo "\nEXAMPLE: $SCRIPT_NAME -s 4 8.09838 2048 65536 42.632"
|
||||||
|
echo "\n\t...EXITING...\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
function exit_trap
|
||||||
|
{
|
||||||
|
echo "\n...EXITING on trapped signal...\n"
|
||||||
|
}
|
||||||
|
########################################################
|
||||||
|
################ START OF MAIN #########################
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
###### Set a Trap ######
|
||||||
|
|
||||||
|
trap 'exit_trap; exit 2' 1 2 3 15
|
||||||
|
|
||||||
|
########################
|
||||||
|
|
||||||
|
# Check for at least two command-line arguments
|
||||||
|
|
||||||
|
if (($# < 2))
|
||||||
|
then
|
||||||
|
echo "\nERROR: Please provide a list of numbers to subtract"
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
# Parse the command-line arguments to find the scale value, if present.
|
||||||
|
|
||||||
|
while getopts ":s:S:" ARGUMENT
|
||||||
|
do
|
||||||
|
case $ARGUMENT in
|
||||||
|
s|S) SCALE=$OPTARG
|
||||||
|
;;
|
||||||
|
\?) # Because we may have negative numbers we need
|
||||||
|
# to test to see if the ARGUMENT that begins with a
|
||||||
|
# hyphen (-) is a number, and not an invalid switch!!!
|
||||||
|
|
||||||
|
for TST_ARG in $*
|
||||||
|
do
|
||||||
|
if [[ $(echo $TST_ARG | cut -c1) = '-' ]] \
|
||||||
|
&& [ $TST_ARG != '-s' -a $TST_ARG != '-S' ]
|
||||||
|
then
|
||||||
|
case $TST_ARG in
|
||||||
|
+([-0-9])) : # No-op, do nothing
|
||||||
|
;;
|
||||||
|
+([-0-9].[0-9]))
|
||||||
|
: # No-op, do nothing
|
||||||
|
;;
|
||||||
|
+([-.0-9])) : # No-op, do nothing
|
||||||
|
;;
|
||||||
|
*) echo "\nERROR: Invalid argument on the command line"
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
# Parse through the command-line arguments and gather a list
|
||||||
|
# of numbers to subtract.
|
||||||
|
|
||||||
|
while ((COUNT < MAX_COUNT))
|
||||||
|
do
|
||||||
|
((COUNT = COUNT + 1))
|
||||||
|
TOKEN=$1
|
||||||
|
case $TOKEN in
|
||||||
|
-s|-S) shift 2
|
||||||
|
((COUNT = COUNT + 1))
|
||||||
|
;;
|
||||||
|
-s${SCALE}) shift
|
||||||
|
;;
|
||||||
|
-S${SCALE}) shift
|
||||||
|
;;
|
||||||
|
*) NUM_LIST="${NUM_LIST} $TOKEN"
|
||||||
|
((COUNT < MAX_COUNT)) && shift
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
# Ensure that the scale is an integer value
|
||||||
|
|
||||||
|
case $SCALE in
|
||||||
|
+([0-9])) : # No-Op - Do Nothing
|
||||||
|
;;
|
||||||
|
*) echo "\nERROR: Invalid scale - $SCALE - Must be an integer"
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
# Check each number supplied to ensure that the "numbers"
|
||||||
|
# are either integers or floating-point numbers.
|
||||||
|
|
||||||
|
for NUM in $NUM_LIST
|
||||||
|
do
|
||||||
|
case $NUM in
|
||||||
|
+([0-9])) # Check for an integer
|
||||||
|
: # No-op, do nothing.
|
||||||
|
;;
|
||||||
|
+([-0-9])) # Check for a negative whole number
|
||||||
|
: # No-op, do nothing
|
||||||
|
;;
|
||||||
|
+([0-9]|[.][0-9]))
|
||||||
|
# Check for a positive floating-point number
|
||||||
|
: # No-op, do nothing
|
||||||
|
;;
|
||||||
|
+(+[0-9]|[.][0-9]))
|
||||||
|
# Check for a positive floating-point number
|
||||||
|
# with a + prefix
|
||||||
|
: # No-op, do nothing
|
||||||
|
;;
|
||||||
|
+([-0-9]|.[0-9]))
|
||||||
|
# Check for a negative floating-point number
|
||||||
|
: # No-op, do nothing
|
||||||
|
;;
|
||||||
|
+(-[.][0-9]))
|
||||||
|
# Check for a negative floating-point number
|
||||||
|
: # No-op, do nothing
|
||||||
|
;;
|
||||||
|
+([+.0-9]))
|
||||||
|
# Check for a positive floating-point number
|
||||||
|
: # No-op, do nothing
|
||||||
|
;;
|
||||||
|
*) echo "\nERROR: $NUM is NOT a valid number"
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
# Build the list of numbers to subtract
|
||||||
|
|
||||||
|
SUBTRACT= # Initialize the SUBTRACT variable to NULL
|
||||||
|
MINUS= # Initialize the MINUS variable to NULL
|
||||||
|
|
||||||
|
# Loop through each number and build a math statement that
|
||||||
|
# will subtract the numbers in the list.
|
||||||
|
|
||||||
|
for X in $NUM_LIST
|
||||||
|
do
|
||||||
|
# If the number has a + prefix, remove it!
|
||||||
|
if [[ $(echo $X | cut -c1) = '+' ]]
|
||||||
|
then
|
||||||
|
X=$(echo $X | cut -c2-)
|
||||||
|
fi
|
||||||
|
SUBTRACT="$SUBTRACT $MINUS $X"
|
||||||
|
MINUS='-'
|
||||||
|
done
|
||||||
|
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
# Do the math here by using a here document to supply
|
||||||
|
# input to the bc command. The difference of the numbers is
|
||||||
|
# assigned to the DIFFERENCE variable.
|
||||||
|
|
||||||
|
DIFFERENCE=$(bc <<EOF
|
||||||
|
scale=$SCALE
|
||||||
|
(${SUBTRACT})
|
||||||
|
EOF)
|
||||||
|
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
# Present the result of the subtraction to the user.
|
||||||
|
|
||||||
|
echo "\nThe difference of: $SUBTRACT"
|
||||||
|
echo "\nis: ${DIFFERENCE}\n"
|
||||||
117
chapter14/chg_base.ksh
Executable file
117
chapter14/chg_base.ksh
Executable file
@@ -0,0 +1,117 @@
|
|||||||
|
#!/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: chg_base.ksh
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 10/4/2007
|
||||||
|
# REV: 1.1.A
|
||||||
|
#
|
||||||
|
# PURPOSE: This script converts numbers between base
|
||||||
|
# 2 through base 36. The user is prompted for input.
|
||||||
|
#
|
||||||
|
# NOTE: Numbers are in the following format:
|
||||||
|
#
|
||||||
|
# base#number
|
||||||
|
#
|
||||||
|
# EXAMPLE: 16#264bf
|
||||||
|
#
|
||||||
|
##########################################
|
||||||
|
# DEFINE FILES AND VARIABLES HERE
|
||||||
|
##########################################
|
||||||
|
|
||||||
|
# Setup the correct awk usage. Solaris needs to
|
||||||
|
# use nawk instead of awk.
|
||||||
|
|
||||||
|
case $(uname) in
|
||||||
|
SunOS) AWK="nawk"
|
||||||
|
;;
|
||||||
|
*) AWK="awk"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
# Set up the correct echo command usage. Many Linux
|
||||||
|
# distributions will execute in Bash even if the
|
||||||
|
# script specifies Korn shell. Bash shell requires
|
||||||
|
# we use echo -e when we use \n, \c, etc.
|
||||||
|
|
||||||
|
case $SHELL in
|
||||||
|
*/bin/bash) alias echo="echo -e"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
##########################################
|
||||||
|
# BEGINNING OF MAIN
|
||||||
|
##########################################
|
||||||
|
|
||||||
|
# Prompt the user for an input number with base
|
||||||
|
|
||||||
|
echo "\nInput the number to convert in the following format:
|
||||||
|
|
||||||
|
Format: base#number
|
||||||
|
|
||||||
|
Example: 16#264BF \n"
|
||||||
|
|
||||||
|
read ibase_num
|
||||||
|
|
||||||
|
# Extract the base from the ibase_num variable
|
||||||
|
|
||||||
|
ibase=$(echo $ibase_num | $AWK -F '#' '{print $1}')
|
||||||
|
|
||||||
|
# Test to ensure the input base is an integer
|
||||||
|
|
||||||
|
case $ibase in
|
||||||
|
[0-9]*) : # do nothing
|
||||||
|
;;
|
||||||
|
*) echo "\nERROR: $ibase is not a valid number for input base\n"
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
# Test to ensure the input base is between 2 and 36
|
||||||
|
|
||||||
|
if (( ibase < 2 || ibase > 36 ))
|
||||||
|
then
|
||||||
|
echo "\nERROR: Input base must be between 2 and 36\n"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Ask the user for the output base
|
||||||
|
|
||||||
|
echo "\nWhat base do you want to convert $ibase_num to?
|
||||||
|
NOTE: base 2 through 36 are valid\n"
|
||||||
|
echo "Output base: \c"
|
||||||
|
read obase
|
||||||
|
|
||||||
|
# Test to ensure the output base is an integer
|
||||||
|
|
||||||
|
case $obase in
|
||||||
|
[0-9]*) : # do nothing
|
||||||
|
;;
|
||||||
|
*) echo "\nERROR: $obase is not a valid number\n"
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
# Test to ensure the output base is between 2 and 36
|
||||||
|
|
||||||
|
if (( obase < 2 || obase > 36 ))
|
||||||
|
then
|
||||||
|
echo "\nERROR: Output base must be between 2 and 36\n"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Save the input number before changing the base
|
||||||
|
|
||||||
|
in_base_num=$ibase_num
|
||||||
|
|
||||||
|
# Convert the input number to the desire output base
|
||||||
|
|
||||||
|
typeset -i$obase ibase_num
|
||||||
|
|
||||||
|
# Assign the output base number to an appropriate variable name
|
||||||
|
|
||||||
|
obase_num=$ibase_num
|
||||||
|
|
||||||
|
# Display the result
|
||||||
|
|
||||||
|
echo "\nInput number $in_base_num is equivalent to $obase_num\n"
|
||||||
|
|
||||||
111
chapter14/chg_base_bc.bash
Executable file
111
chapter14/chg_base_bc.bash
Executable file
@@ -0,0 +1,111 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
#
|
||||||
|
# SCRIPT: chg_base_bc.bash
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 10/4/2007
|
||||||
|
# REV: 1.1.A
|
||||||
|
#
|
||||||
|
# PURPOSE: This script converts numbers between base
|
||||||
|
# 2 through base 16 using the bc utility. The user
|
||||||
|
# is prompted for input.
|
||||||
|
#
|
||||||
|
# NOTE: Numbers are in the following format:
|
||||||
|
#
|
||||||
|
# base#number
|
||||||
|
#
|
||||||
|
# EXAMPLE: 16#264bf
|
||||||
|
#
|
||||||
|
##########################################
|
||||||
|
# DEFINE FILES AND VARIABLES HERE
|
||||||
|
##########################################
|
||||||
|
|
||||||
|
# Setup the correct awk usage. Solaris needs to
|
||||||
|
# use nawk instead of awk.
|
||||||
|
|
||||||
|
case $(uname) in
|
||||||
|
SunOS) AWK="nawk"
|
||||||
|
;;
|
||||||
|
*) AWK="awk"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
##########################################
|
||||||
|
# BEGINNING OF MAIN
|
||||||
|
##########################################
|
||||||
|
|
||||||
|
# Prompt the user for an input number with base
|
||||||
|
|
||||||
|
echo -e "\nInput the number to convert in the following format:
|
||||||
|
|
||||||
|
Format: base#number
|
||||||
|
|
||||||
|
Example: 16#264BF \n"
|
||||||
|
|
||||||
|
read IBASE_NUM
|
||||||
|
|
||||||
|
# Extract the base from the ibase_num variable
|
||||||
|
|
||||||
|
IBASE=$(echo $IBASE_NUM | $AWK -F '#' '{print $1}')
|
||||||
|
INUMBER=$(echo $IBASE_NUM | $AWK -F '#' '{print $2}')
|
||||||
|
|
||||||
|
# Test to ensure the input base is between 2 and 16
|
||||||
|
|
||||||
|
if (( IBASE < 2 || IBASE > 16 ))
|
||||||
|
then
|
||||||
|
echo -e "\nERROR: Input base must be between 2 and 16\n"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# The bc utility requires all number bases greater
|
||||||
|
# than 10 use uppercase characters for all
|
||||||
|
# non-numeric character numbers, i.e. hex numbers.
|
||||||
|
# We use the tr command to upcase all lowercase
|
||||||
|
# characters.
|
||||||
|
|
||||||
|
if (( IBASE > 10 ))
|
||||||
|
then
|
||||||
|
INUMBER=$(echo $INUMBER | tr '[a-z]' '[A-Z]')
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Ask the user for the output base
|
||||||
|
|
||||||
|
echo -e "\nWhat base do you want to convert $IBASE_NUM to?
|
||||||
|
NOTE: base 2 through 16 are valid\n"
|
||||||
|
echo -e "Output base: \c"
|
||||||
|
read OBASE
|
||||||
|
|
||||||
|
# Test to ensure the output base is an integer
|
||||||
|
|
||||||
|
case $OBASE in
|
||||||
|
[0-9]*) : # do nothing
|
||||||
|
;;
|
||||||
|
*) echo -e "\nERROR: $obase is not a valid number\n"
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
# Test to ensure the output base is between 2 and 16
|
||||||
|
|
||||||
|
if (( OBASE < 2 || OBASE > 16 ))
|
||||||
|
then
|
||||||
|
echo -e "\nERROR: Output base must be between 2 and 16\n"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Save the input number before changing the base
|
||||||
|
|
||||||
|
IN_BASE_NUM=$IBASE_NUM
|
||||||
|
|
||||||
|
# Convert the input number to decimal
|
||||||
|
|
||||||
|
DEC_EQUIV=$(echo "ibase=$IBASE; $INUMBER" | bc)
|
||||||
|
|
||||||
|
# Convert the number to the desired output base
|
||||||
|
|
||||||
|
RESULT=$(echo "obase=$OBASE; $DEC_EQUIV" | bc)
|
||||||
|
|
||||||
|
# Display the result
|
||||||
|
|
||||||
|
echo -e "\nInput number $IN_BASE_NUM is equivalent to \
|
||||||
|
${OBASE}#${RESULT}\n"
|
||||||
|
|
||||||
177
chapter14/equate_any_base.ksh
Executable file
177
chapter14/equate_any_base.ksh
Executable file
@@ -0,0 +1,177 @@
|
|||||||
|
#!/usr/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: equate_any_base.ksh
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 07/07/2007
|
||||||
|
# REV: 1.2.P
|
||||||
|
#
|
||||||
|
# PURPOSE: This script is used to convert a number to any
|
||||||
|
# supported number base, which is at least base 36.
|
||||||
|
# This script requires that two command line
|
||||||
|
# auguments and the "number" to be converted
|
||||||
|
# are present on the command line. An example
|
||||||
|
# number base conversion is shown here:
|
||||||
|
#
|
||||||
|
# equate_any_base.ksh -f16 -t2 e245c
|
||||||
|
# 2#11100010010001011100
|
||||||
|
#
|
||||||
|
# This example converts the base 16 number, e245c, to
|
||||||
|
# the base 2 equivalent, 2#11100010010001011100.
|
||||||
|
# The 2#, which precedes the binary number, shows
|
||||||
|
# the base of the number represented.
|
||||||
|
#
|
||||||
|
# EXIT CODES:
|
||||||
|
# 0 - Normal script execution
|
||||||
|
# 1 - Usage error
|
||||||
|
#
|
||||||
|
# set -x # Uncomment to debug this shell script
|
||||||
|
# set -n # Uncomment to check syntax without any execution
|
||||||
|
#
|
||||||
|
######################################################
|
||||||
|
# DEFINE FILES AND VARIABLES HERE
|
||||||
|
######################################################
|
||||||
|
|
||||||
|
SCRIPT_NAME=$(basename $0)
|
||||||
|
COUNT=0
|
||||||
|
MAX_COUNT=$#
|
||||||
|
|
||||||
|
# Setup the correct echo command usage. Many Linux
|
||||||
|
# distributions will execute in BASH even if the
|
||||||
|
# script specifies Korn shell. BASH shell requires
|
||||||
|
# we use echo -e when we use \n, \c, etc.
|
||||||
|
|
||||||
|
case $SHELL in
|
||||||
|
*/bin/bash) alias echo="echo -e"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
######################################################
|
||||||
|
# DEFINE FUNCTIONS HERE
|
||||||
|
######################################################
|
||||||
|
|
||||||
|
function usage
|
||||||
|
{
|
||||||
|
echo "\nUSAGE: $SCRIPT_NAME -f{starting base} -t{ending base} NUMBER"
|
||||||
|
echo "\nEXAMPLE: $SCRIPT_NAME -f16 -t10 FC23"
|
||||||
|
echo "\nWill return the decimal base 10 number 64547"
|
||||||
|
echo "\t ...EXITING...\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
######################################################
|
||||||
|
# CHECK COMMAND LINE AUGUMENTS HERE
|
||||||
|
######################################################
|
||||||
|
|
||||||
|
# The maximum number of command line arguments is five
|
||||||
|
# and the minimum number is three.
|
||||||
|
|
||||||
|
if (($# > 5))
|
||||||
|
then
|
||||||
|
echo "\nERROR: Too many command line arguments\n"
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
elif (($# < 3))
|
||||||
|
then
|
||||||
|
echo "\nERROR: Too few command-line arguments\n"
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Check to see if the command-line switches are present
|
||||||
|
|
||||||
|
echo $* | grep -q '\-f' || (usage; exit 1)
|
||||||
|
echo $* | grep -q '\-t' || (usage; exit 1)
|
||||||
|
|
||||||
|
# Use getopts to parse the command line arguments
|
||||||
|
|
||||||
|
while getopts ":f:t:" ARGUMENT
|
||||||
|
do
|
||||||
|
case $ARGUMENT in
|
||||||
|
f) START_BASE="$OPTARG"
|
||||||
|
;;
|
||||||
|
t) END_BASE="$OPTARG"
|
||||||
|
;;
|
||||||
|
\?) usage
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
# Ensure that the START_BASE and END_BASE variables
|
||||||
|
# are not NULL.
|
||||||
|
|
||||||
|
if [ -z "$START_BASE" ] || [ "$START_BASE" = '' ] \
|
||||||
|
|| [ -z "$END_BASE" ] || [ "$END_BASE" = '' ]
|
||||||
|
then
|
||||||
|
echo "\nERROR: Base number conversion fields are empty\n"
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Ensure that the START_BASE and END_BASE variables
|
||||||
|
# have integer values for the number base conversion.
|
||||||
|
|
||||||
|
case $START_BASE in
|
||||||
|
+([0-9])) : # Do nothing - Colon is a no-op.
|
||||||
|
;;
|
||||||
|
*) echo "\nERROR: $START_BASE is not an integer value"
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
case $END_BASE in
|
||||||
|
+([0-9])) : # Do nothing - Colon is a no-op.
|
||||||
|
;;
|
||||||
|
*) echo "\nERROR: $END_BASE is not an integer value"
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
######################################################
|
||||||
|
# BEGINNING OF MAIN
|
||||||
|
######################################################
|
||||||
|
|
||||||
|
# Begin by finding the BASE_NUM to be converted.
|
||||||
|
|
||||||
|
# Count from 1 to the max number of command line auguments
|
||||||
|
|
||||||
|
while ((COUNT < MAX_COUNT))
|
||||||
|
do
|
||||||
|
((COUNT == COUNT + 1))
|
||||||
|
TOKEN=$1
|
||||||
|
case $TOKEN in
|
||||||
|
-f) shift; shift
|
||||||
|
((COUNT == COUNT + 1))
|
||||||
|
;;
|
||||||
|
-f${START_BASE}) shift
|
||||||
|
;;
|
||||||
|
-t) shift; shift
|
||||||
|
((COUNT == COUNT + 1))
|
||||||
|
;;
|
||||||
|
-t${END_BASE}) shift
|
||||||
|
;;
|
||||||
|
*) BASE_NUM=$TOKEN
|
||||||
|
break
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
# Typeset the RESULT variable to the target number base
|
||||||
|
|
||||||
|
typeset -i$END_BASE RESULT
|
||||||
|
|
||||||
|
# Assign the BASE_NUM variable to the RESULT variable
|
||||||
|
# and add the starting number base with a pound sign (#)
|
||||||
|
# as a prefix for the conversion to take place.
|
||||||
|
# NOTE: If an invalid number is entered a system error
|
||||||
|
# will be displayed. An example is inputting 1114400 as
|
||||||
|
# a binary number, which is invalid for a binary number.
|
||||||
|
|
||||||
|
RESULT="${START_BASE}#${BASE_NUM}"
|
||||||
|
|
||||||
|
# Display the result to the user or calling program.
|
||||||
|
|
||||||
|
echo "$RESULT"
|
||||||
|
|
||||||
|
# End of script...
|
||||||
115
chapter14/equate_base_10_to_16.ksh
Executable file
115
chapter14/equate_base_10_to_16.ksh
Executable file
@@ -0,0 +1,115 @@
|
|||||||
|
#!/usr/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: equate_base_10_to_16.ksh
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 07/07/2007
|
||||||
|
# REV: 1.2.P
|
||||||
|
#
|
||||||
|
# PURPOSE: This script is used to convert a base 10 number
|
||||||
|
# to a base 16 hexadecimal representation.
|
||||||
|
# This script expects that a base 10 number
|
||||||
|
# is supplied as a single argument.
|
||||||
|
#
|
||||||
|
# EXIT CODES:
|
||||||
|
# 0 - Normal script execution
|
||||||
|
# 1 - Usage error
|
||||||
|
#
|
||||||
|
#
|
||||||
|
# REV LIST:
|
||||||
|
#
|
||||||
|
#
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
# set -n # Uncomment to check command syntax without any execution
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#################################################
|
||||||
|
# DEFINE FILES AND VARIABLES HERE
|
||||||
|
#################################################
|
||||||
|
|
||||||
|
SCRIPT_NAME=`basename $0`
|
||||||
|
|
||||||
|
# Set up the correct echo command usage. Many Linux
|
||||||
|
# distributions will execute in Bash even if the
|
||||||
|
# script specifies Korn shell. Bash shell requires
|
||||||
|
# we use echo -e when we use \n, \c, etc.
|
||||||
|
|
||||||
|
case $(basename $SHELL) in
|
||||||
|
bash) alias echo="echo -e"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
#################################################
|
||||||
|
# DEFINE FUNCTIONS HERE
|
||||||
|
#################################################
|
||||||
|
function usage
|
||||||
|
{
|
||||||
|
echo "\nUSAGE: $SCRIPT_NAME {base 10 number}"
|
||||||
|
echo "\nEXAMPLE: $SCRIPT_NAME 694"
|
||||||
|
echo "\nWill return the hexadecimal number 2b6"
|
||||||
|
echo "\n\t...EXITING...\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
#################################################
|
||||||
|
# BEGINNING OF MAIN
|
||||||
|
#################################################
|
||||||
|
|
||||||
|
# Check for a single command-line argument
|
||||||
|
|
||||||
|
if [ $# -ne 1 ]
|
||||||
|
then
|
||||||
|
echo "\nERROR: A base 10 number must be supplied..."
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Check that this single command-line argument is a base 10 number!
|
||||||
|
case $1 in
|
||||||
|
+([0-9])) BASE_10_NUM=$1
|
||||||
|
;;
|
||||||
|
*) echo "\nERROR: $1 is NOT a base 10 number"
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
# Assign the base 10 number to the BASE_16_NUM variable
|
||||||
|
|
||||||
|
BASE_16_NUM=10#$BASE_10_NUM
|
||||||
|
|
||||||
|
# NOTE: Since base 10 is implied by default,
|
||||||
|
# the 10# may be omitted as a prefix. Both notations
|
||||||
|
# are supported
|
||||||
|
|
||||||
|
# Now typeset the BASE_16_NUM variable to base 16.
|
||||||
|
# This step converts the base 10 number to a base 16 number.
|
||||||
|
|
||||||
|
typeset -i16 BASE_16_NUM
|
||||||
|
|
||||||
|
# Display the resulting base 16 number representation
|
||||||
|
|
||||||
|
echo $BASE_16_NUM
|
||||||
|
|
||||||
|
# This following code is optional. It removes the number base
|
||||||
|
# prefix. This may be helpful if using this script with
|
||||||
|
# other programs and scripts.
|
||||||
|
#
|
||||||
|
# Set up the correct awk usage. Solaris needs to
|
||||||
|
# use nawk instead of awk.
|
||||||
|
#
|
||||||
|
# case $(uname) in
|
||||||
|
# SunOS) AWK="nawk"
|
||||||
|
# ;;
|
||||||
|
# *) AWK="awk"
|
||||||
|
# ;;
|
||||||
|
# esac
|
||||||
|
#
|
||||||
|
# Strip out the base prefix and the pound sign (#). (Optional)
|
||||||
|
#
|
||||||
|
# echo $BASE_16_NUM | grep -q "#"
|
||||||
|
#
|
||||||
|
# if (($? == 0))
|
||||||
|
# then
|
||||||
|
# echo $BASE_16_NUM | $AWK -F '#' '{print $2}'
|
||||||
|
# else
|
||||||
|
# echo $BASE_16_NUM
|
||||||
|
# fi
|
||||||
75
chapter14/equate_base_10_to_2.ksh
Executable file
75
chapter14/equate_base_10_to_2.ksh
Executable file
@@ -0,0 +1,75 @@
|
|||||||
|
#!/usr/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: equate_base_10_to_2.ksh
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 07/07/2007
|
||||||
|
# REV: 1.2.P
|
||||||
|
#
|
||||||
|
# PURPOSE: This script is used to convert a base 10 number
|
||||||
|
# to a base 2 binary representation.
|
||||||
|
# This scripts expects that a base 10 number
|
||||||
|
# is supplied as a single argument.
|
||||||
|
#
|
||||||
|
# EXIT CODES:
|
||||||
|
# 0 - Normal script execution
|
||||||
|
# 1 - Usage error
|
||||||
|
#
|
||||||
|
# REV LIST:
|
||||||
|
#
|
||||||
|
#
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
# set -n # Uncomment to check command syntax without any execution
|
||||||
|
#
|
||||||
|
#################################################
|
||||||
|
# DEFINE FILES AND VARIABLES HERE
|
||||||
|
#################################################
|
||||||
|
|
||||||
|
SCRIPT_NAME=`basename $0`
|
||||||
|
typeset -i2 BASE_2_NUM
|
||||||
|
typeset -i BASE_10_NUM
|
||||||
|
|
||||||
|
# Set up the correct echo command usage. Many Linux
|
||||||
|
# distributions will execute in Bash even if the
|
||||||
|
# script specifies Korn shell. Bash shell requires
|
||||||
|
# we use echo -e when we use \n, \c, etc.
|
||||||
|
|
||||||
|
case $SHELL in
|
||||||
|
*/bin/bash) alias echo="echo -e"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
#################################################
|
||||||
|
# DEFINE FUNCTIONS HERE
|
||||||
|
#################################################
|
||||||
|
|
||||||
|
function usage
|
||||||
|
{
|
||||||
|
echo "\nUSAGE: $SCRIPT_NAME base_10_number"
|
||||||
|
echo "\nEXAMPLE: $SCRIPT_NAME 14"
|
||||||
|
echo "\nWill return the decimal base 2 number 1110 ...EXITING...\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
#################################################
|
||||||
|
# BEGINNING OF MAIN
|
||||||
|
#################################################
|
||||||
|
|
||||||
|
# Check for a single command line argument
|
||||||
|
|
||||||
|
if [ $# -ne 1 ]
|
||||||
|
then
|
||||||
|
echo "\nERROR: A base 10 number must be supplied..."
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
BASE_10_NUM="$1"
|
||||||
|
|
||||||
|
BASE_2_NUM=$((10#${BASE_10_NUM}))
|
||||||
|
|
||||||
|
echo $BASE_2_NUM | grep "#" >/dev/null 2>&1
|
||||||
|
if [ $? -eq 0 ]
|
||||||
|
then
|
||||||
|
echo $BASE_2_NUM | cut -f2 -d "#"
|
||||||
|
else
|
||||||
|
echo $BASE_2_NUM
|
||||||
|
fi
|
||||||
74
chapter14/equate_base_10_to_8.ksh
Executable file
74
chapter14/equate_base_10_to_8.ksh
Executable file
@@ -0,0 +1,74 @@
|
|||||||
|
#!/usr/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: equate_base_10_to_8.ksh
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 07/07/2007
|
||||||
|
# REV: 1.2.P
|
||||||
|
#
|
||||||
|
# PURPOSE: This script is used to convert a base 10 number
|
||||||
|
# to a base 8 octal representation.
|
||||||
|
# This scripts expects that a base 10 number
|
||||||
|
# is supplied as a single argument.
|
||||||
|
#
|
||||||
|
# EXIT CODES:
|
||||||
|
# 0 - Normal script execution
|
||||||
|
# 1 - Usage error
|
||||||
|
#
|
||||||
|
# REV LIST:
|
||||||
|
#
|
||||||
|
#
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
# set -n # Uncomment to check command syntax without any execution
|
||||||
|
#
|
||||||
|
#################################################
|
||||||
|
# DEFINE FILES AND VARIABLES HERE
|
||||||
|
#################################################
|
||||||
|
|
||||||
|
SCRIPT_NAME=`basename $0`
|
||||||
|
typeset -i8 BASE_8_NUM
|
||||||
|
typeset -i BASE_10_NUM
|
||||||
|
|
||||||
|
# Set up the correct echo command usage. Many Linux
|
||||||
|
# distributions will execute in Bash even if the
|
||||||
|
# script specifies Korn shell. Bash shell requires
|
||||||
|
# we use echo -e when we use \n, \c, etc.
|
||||||
|
|
||||||
|
case $SHELL in
|
||||||
|
*/bin/bash) alias echo="echo -e"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
#################################################
|
||||||
|
# DEFINE FUNCTIONS HERE
|
||||||
|
#################################################
|
||||||
|
|
||||||
|
function usage
|
||||||
|
{
|
||||||
|
echo "\nUSAGE: $SCRIPT_NAME base_10_number"
|
||||||
|
echo "\nEXAMPLE: $SCRIPT_NAME 946"
|
||||||
|
echo "\nWill return the octal base 8 number 1662 ...EXITING...\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
#################################################
|
||||||
|
# BEGINNING OF MAIN
|
||||||
|
#################################################
|
||||||
|
|
||||||
|
# Check for a single command line argument
|
||||||
|
|
||||||
|
if [ $# -ne 1 ]
|
||||||
|
then
|
||||||
|
echo "\nERROR: A base 10 number must be supplied..."
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
BASE_10_NUM="$1"
|
||||||
|
|
||||||
|
BASE_8_NUM=$((10#${BASE_10_NUM}))
|
||||||
|
echo $BASE_8_NUM | grep "#" >/dev/null 2>&1
|
||||||
|
if [ $? -eq 0 ]
|
||||||
|
then
|
||||||
|
echo $BASE_8_NUM | cut -f2 -d "#"
|
||||||
|
else
|
||||||
|
echo $BASE_8_NUM
|
||||||
|
fi
|
||||||
66
chapter14/equate_base_16_to_10.ksh
Executable file
66
chapter14/equate_base_16_to_10.ksh
Executable file
@@ -0,0 +1,66 @@
|
|||||||
|
#!/usr/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: equate_base_16_to_10.ksh
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 07/07/2007
|
||||||
|
# REV: 1.2.P
|
||||||
|
#
|
||||||
|
# PURPOSE: This script is used to convert a base 16 number
|
||||||
|
# to a base 10 decimal representation.
|
||||||
|
# This scripts expects that a base 16 number
|
||||||
|
# is supplied as a single argument.
|
||||||
|
#
|
||||||
|
# EXIT CODES:
|
||||||
|
# 0 - Normal script execution
|
||||||
|
# 1 - Usage error
|
||||||
|
#
|
||||||
|
# REV LIST:
|
||||||
|
#
|
||||||
|
#
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
# set -n # Uncomment to check command syntax without any execution
|
||||||
|
#
|
||||||
|
#################################################
|
||||||
|
# DEFINE FILES AND VARIABLES HERE
|
||||||
|
#################################################
|
||||||
|
|
||||||
|
SCRIPT_NAME=`basename $0`
|
||||||
|
|
||||||
|
# Set up the correct echo command usage. Many Linux
|
||||||
|
# distributions will execute in Bash even if the
|
||||||
|
# script specifies Korn shell. Bash shell requires
|
||||||
|
# we use echo -e when we use \n, \c, etc.
|
||||||
|
|
||||||
|
case $SHELL in
|
||||||
|
*/bin/bash) alias echo="echo -e"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
#################################################
|
||||||
|
# DEFINE FUNCTIONS HERE
|
||||||
|
#################################################
|
||||||
|
|
||||||
|
function usage
|
||||||
|
{
|
||||||
|
echo "\nUSAGE: $SCRIPT_NAME base_16_number"
|
||||||
|
echo "\nEXAMPLE: $SCRIPT_NAME FC23"
|
||||||
|
echo "\nWill return the decimal base 10 number 64547 ...EXITING...\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
#################################################
|
||||||
|
# BEGINNING OF MAIN
|
||||||
|
#################################################
|
||||||
|
|
||||||
|
# Check for a single command line argument
|
||||||
|
|
||||||
|
if [ $# -ne 1 ]
|
||||||
|
then
|
||||||
|
echo "\nERROR: A base 16 number must be supplied..."
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
BASE_16_NUM="$1"
|
||||||
|
|
||||||
|
BASE_10_NUM=$((16#${BASE_16_NUM}))
|
||||||
|
echo $BASE_10_NUM
|
||||||
74
chapter14/equate_base_16_to_2.ksh
Executable file
74
chapter14/equate_base_16_to_2.ksh
Executable file
@@ -0,0 +1,74 @@
|
|||||||
|
#!/usr/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: equate_base_16_to_2.ksh
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 07/07/2007
|
||||||
|
# REV: 1.2.P
|
||||||
|
#
|
||||||
|
# PURPOSE: This script is used to convert a base 16 hexidecimal
|
||||||
|
# number to a base 2 binary representation.
|
||||||
|
# This scripts expects that a base 16 number
|
||||||
|
# is supplied as a single argument.
|
||||||
|
#
|
||||||
|
# EXIT CODES:
|
||||||
|
# 0 - Normal script execution
|
||||||
|
# 1 - Usage error
|
||||||
|
#
|
||||||
|
# REV LIST:
|
||||||
|
#
|
||||||
|
#
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
# set -n # Uncomment to check command syntax without any execution
|
||||||
|
#
|
||||||
|
#################################################
|
||||||
|
# DEFINE FILES AND VARIABLES HERE
|
||||||
|
#################################################
|
||||||
|
|
||||||
|
SCRIPT_NAME=`basename $0`
|
||||||
|
|
||||||
|
# Set up the correct echo command usage. Many Linux
|
||||||
|
# distributions will execute in Bash even if the
|
||||||
|
# script specifies Korn shell. Bash shell requires
|
||||||
|
# we use echo -e when we use \n, \c, etc.
|
||||||
|
|
||||||
|
case $SHELL in
|
||||||
|
*/bin/bash) alias echo="echo -e"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
#################################################
|
||||||
|
# DEFINE FUNCTIONS HERE
|
||||||
|
#################################################
|
||||||
|
|
||||||
|
function usage
|
||||||
|
{
|
||||||
|
echo "\nUSAGE: $SCRIPT_NAME base_16_number"
|
||||||
|
echo "\nEXAMPLE: $SCRIPT_NAME FC23"
|
||||||
|
echo "\nWill return the binary base 2 number 64547 ...EXITING...\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
#################################################
|
||||||
|
# BEGINNING OF MAIN
|
||||||
|
#################################################
|
||||||
|
|
||||||
|
# Check for a single command line argument
|
||||||
|
|
||||||
|
if [ $# -ne 1 ]
|
||||||
|
then
|
||||||
|
echo "\nERROR: A base 16 number must be supplied..."
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Check for a hexidecimal number!
|
||||||
|
|
||||||
|
case $1 in
|
||||||
|
+([-0-9]|[a-f]|[A-F])) BASE_16_NUM=$1
|
||||||
|
;;
|
||||||
|
*) usage
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
BASE_2_NUM=$((16#${BASE_16_NUM}))
|
||||||
|
typeset -i2 BASE_2_NUM
|
||||||
|
echo $BASE_2_NUM
|
||||||
82
chapter14/equate_base_2_to_10.ksh
Executable file
82
chapter14/equate_base_2_to_10.ksh
Executable file
@@ -0,0 +1,82 @@
|
|||||||
|
#!/usr/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: equate_base_2_to_10.ksh
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 07/07/2007
|
||||||
|
# REV: 1.2.P
|
||||||
|
#
|
||||||
|
# PURPOSE: This script is used to convert a base 2 number
|
||||||
|
# to a base 10 decimal representation.
|
||||||
|
# This scripts expects that a base 2 number
|
||||||
|
# is supplied as a single argument.
|
||||||
|
#
|
||||||
|
# EXIT CODES:
|
||||||
|
# 0 - Normal script execution
|
||||||
|
# 1 - Usage error
|
||||||
|
#
|
||||||
|
# REV LIST:
|
||||||
|
#
|
||||||
|
#
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
# set -n # Uncomment to check command syntax without any execution
|
||||||
|
#
|
||||||
|
#################################################
|
||||||
|
# DEFINE FILES AND VARIABLES HERE
|
||||||
|
#################################################
|
||||||
|
|
||||||
|
SCRIPT_NAME=`basename $0`
|
||||||
|
typeset -i BASE_2_NUM
|
||||||
|
typeset -i BASE_10_NUM
|
||||||
|
|
||||||
|
# Set up the correct echo command usage. Many Linux
|
||||||
|
# distributions will execute in Bash even if the
|
||||||
|
# script specifies Korn shell. Bash shell requires
|
||||||
|
# we use echo -e when we use \n, \c, etc.
|
||||||
|
|
||||||
|
case $SHELL in
|
||||||
|
*/bin/bash) alias echo="echo -e"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
#################################################
|
||||||
|
# DEFINE FUNCTIONS HERE
|
||||||
|
#################################################
|
||||||
|
|
||||||
|
function usage
|
||||||
|
{
|
||||||
|
echo "\nUSAGE: $SCRIPT_NAME base_2_number"
|
||||||
|
echo "\nEXAMPLE: $SCRIPT_NAME 1110"
|
||||||
|
echo "\nWill return the decimal base 10 number 14 ...EXITING...\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
#################################################
|
||||||
|
# BEGINNING OF MAIN
|
||||||
|
#################################################
|
||||||
|
|
||||||
|
# Check for a single command line argument
|
||||||
|
|
||||||
|
if [ $# -ne 1 ]
|
||||||
|
then
|
||||||
|
echo "\nERROR: A base 2 number must be supplied..."
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Check that this single command line argument is a binary number!
|
||||||
|
|
||||||
|
case $1 in
|
||||||
|
+([-0-1])) BASE_2_NUM=$1
|
||||||
|
;;
|
||||||
|
*) echo "\nERROR: $1 is NOT a base 2 number"
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
# Assign the base 2 number to the BASE_10_NUM variable
|
||||||
|
|
||||||
|
BASE_2_NUM="$1"
|
||||||
|
|
||||||
|
BASE_10_NUM=$((2#${BASE_2_NUM}))
|
||||||
|
|
||||||
|
echo $BASE_10_NUM
|
||||||
87
chapter14/equate_base_2_to_16.ksh
Executable file
87
chapter14/equate_base_2_to_16.ksh
Executable file
@@ -0,0 +1,87 @@
|
|||||||
|
#!/usr/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: equate_base_2_to_16.ksh
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 07/07/2007
|
||||||
|
# REV: 1.2.P
|
||||||
|
#
|
||||||
|
# PURPOSE: This script is used to convert a base 2 number
|
||||||
|
# to a base 16 hexadecimal representation.
|
||||||
|
# This scripts expects that a base 2 number
|
||||||
|
# is supplied as a single argument.
|
||||||
|
#
|
||||||
|
# EXIT CODES:
|
||||||
|
# 0 - Normal script execution
|
||||||
|
# 1 - Usage error
|
||||||
|
#
|
||||||
|
# REV LIST:
|
||||||
|
#
|
||||||
|
#
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
# set -n # Uncomment to check command syntax without any execution
|
||||||
|
#
|
||||||
|
#################################################
|
||||||
|
# DEFINE FILES AND VARIABLES HERE
|
||||||
|
#################################################
|
||||||
|
|
||||||
|
SCRIPT_NAME=`basename $0`
|
||||||
|
|
||||||
|
# Set up the correct echo command usage. Many Linux
|
||||||
|
# distributions will execute in Bash even if the
|
||||||
|
# script specifies Korn shell. Bash shell requires
|
||||||
|
# we use echo -e when we use \n, \c, etc.
|
||||||
|
|
||||||
|
case $SHELL in
|
||||||
|
*/bin/bash) alias echo="echo -e"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
#################################################
|
||||||
|
# DEFINE FUNCTIONS HERE
|
||||||
|
#################################################
|
||||||
|
|
||||||
|
function usage
|
||||||
|
{
|
||||||
|
echo "\nUSAGE: $SCRIPT_NAME {base 2 number}"
|
||||||
|
echo "\nEXAMPLE: $SCRIPT_NAME 1100101101"
|
||||||
|
echo "\nWill return the hexadecimal base 16 number 32d"
|
||||||
|
echo "\n\t ...EXITING...\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
#################################################
|
||||||
|
# BEGINNING OF MAIN
|
||||||
|
#################################################
|
||||||
|
|
||||||
|
# Check for a single command line argument
|
||||||
|
|
||||||
|
if (($# != 1))
|
||||||
|
then
|
||||||
|
echo "\nERROR: A base 2 number must be supplied..."
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Check that this single command line argument is a binary number!
|
||||||
|
|
||||||
|
case $1 in
|
||||||
|
+([-0-1])) BASE_2_NUM=$1
|
||||||
|
;;
|
||||||
|
*) echo "\nERROR: $1 is NOT a base 2 number"
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
# Assign the base 2 number to the BASE_16_NUM variable
|
||||||
|
|
||||||
|
BASE_16_NUM=$((2#${BASE_2_NUM}))
|
||||||
|
|
||||||
|
# Now typeset the BASE_16_NUM variable to base 16.
|
||||||
|
# This step converts the base 2 number to a base 16 number.
|
||||||
|
|
||||||
|
typeset -i16 BASE_16_NUM
|
||||||
|
|
||||||
|
# Display the resulting base 16 representation
|
||||||
|
|
||||||
|
echo $BASE_16_NUM
|
||||||
|
|
||||||
68
chapter14/equate_base_8_to_10.ksh
Executable file
68
chapter14/equate_base_8_to_10.ksh
Executable file
@@ -0,0 +1,68 @@
|
|||||||
|
#!/usr/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: equate_base_8_to_10.ksh
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 07/07/2007
|
||||||
|
# REV: 1.2.P
|
||||||
|
#
|
||||||
|
# PURPOSE: This script is used to convert a base 8 number
|
||||||
|
# to a base 10 decimal representation.
|
||||||
|
# This scripts expects that a base 8 number
|
||||||
|
# is supplied as a single argument.
|
||||||
|
#
|
||||||
|
# EXIT CODES:
|
||||||
|
# 0 - Normal script execution
|
||||||
|
# 1 - Usage error
|
||||||
|
#
|
||||||
|
# REV LIST:
|
||||||
|
#
|
||||||
|
#
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
# set -n # Uncomment to check command syntax without any execution
|
||||||
|
#
|
||||||
|
#################################################
|
||||||
|
# DEFINE FILES AND VARIABLES HERE
|
||||||
|
#################################################
|
||||||
|
|
||||||
|
SCRIPT_NAME=`basename $0`
|
||||||
|
typeset -i BASE_8_NUM
|
||||||
|
typeset -i BASE_10_NUM
|
||||||
|
|
||||||
|
#################################################
|
||||||
|
# DEFINE FUNCTIONS HERE
|
||||||
|
#################################################
|
||||||
|
|
||||||
|
function usage
|
||||||
|
{
|
||||||
|
echo "\nUSAGE: $SCRIPT_NAME base_8_number"
|
||||||
|
echo "\nEXAMPLE: $SCRIPT_NAME 2774"
|
||||||
|
echo "\nWill return the decimal base 10 number 1532 ...EXITING...\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
#################################################
|
||||||
|
# BEGINNING OF MAIN
|
||||||
|
#################################################
|
||||||
|
|
||||||
|
# Check for a single command line argument
|
||||||
|
|
||||||
|
if [ $# -ne 1 ]
|
||||||
|
then
|
||||||
|
echo "\nERROR: A base 8 number must be supplied..."
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Check that this single command line argument is a octal number!
|
||||||
|
|
||||||
|
case $1 in
|
||||||
|
+([-0-8])) BASE_8_NUM=$1
|
||||||
|
;;
|
||||||
|
*) echo "\nERROR: $1 is NOT a base 7 number"
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
BASE_10_NUM=$((8#${BASE_8_NUM}))
|
||||||
|
|
||||||
|
echo $BASE_10_NUM
|
||||||
103
chapter14/mk_swkey.ksh
Executable file
103
chapter14/mk_swkey.ksh
Executable file
@@ -0,0 +1,103 @@
|
|||||||
|
#!/usr/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: mk_swkey.ksh
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 07/07/2007
|
||||||
|
# REV: 1.2.P
|
||||||
|
#
|
||||||
|
# PURPOSE: This script is used to create a software
|
||||||
|
# license key based on the IP address of the
|
||||||
|
# system that this shell script is executed on.
|
||||||
|
# The system is queried for the system's IP
|
||||||
|
# address. The IP address is striped of the
|
||||||
|
# dots (.) and each number is converted to
|
||||||
|
# hexadecimal. Then each hex string is combined
|
||||||
|
# into a single hex string, which is the software
|
||||||
|
# license key.
|
||||||
|
#
|
||||||
|
# REV LIST:
|
||||||
|
#
|
||||||
|
#
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
# set -n # Uncomment to check command syntax without any execution
|
||||||
|
#
|
||||||
|
#################################################
|
||||||
|
# DEFINE FILES AND VARIABLES HERE
|
||||||
|
#################################################
|
||||||
|
|
||||||
|
# Set up the correct echo command usage. Many Linux
|
||||||
|
# distributions will execute in Bash even if the
|
||||||
|
# script specifies Korn shell. Bash shell requires
|
||||||
|
# we use echo -e when we use \n, \c, etc.
|
||||||
|
|
||||||
|
case $SHELL in
|
||||||
|
*/bin/bash) alias echo="echo -e"
|
||||||
|
echo -e "\nWARNING: This script is executing in Bash shell."
|
||||||
|
echo "This script may fail because of an unsupported typeset"
|
||||||
|
echo -e "command option that is only supported in Korn shell\n"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
# Set up the correct awk usage. Solaris needs to
|
||||||
|
# use nawk instead of awk.
|
||||||
|
|
||||||
|
case $(uname) in
|
||||||
|
SunOS) alias awk="nawk"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
#################################################
|
||||||
|
# DEFINE FUNCTIONS HERE
|
||||||
|
#################################################
|
||||||
|
|
||||||
|
function convert_base_10_to_16
|
||||||
|
{
|
||||||
|
# set -x # Uncomment to debug this function
|
||||||
|
|
||||||
|
typeset -i16 BASE_16_NUM
|
||||||
|
|
||||||
|
BASE_10_NUM=$1
|
||||||
|
|
||||||
|
BASE_16_NUM=$((10#${BASE_10_NUM}))
|
||||||
|
|
||||||
|
# Strip the number base prefix from the hexadecimal
|
||||||
|
# number. This prefix is not needed here.
|
||||||
|
|
||||||
|
echo $BASE_16_NUM | grep -q '#'
|
||||||
|
if (($? == 0))
|
||||||
|
then
|
||||||
|
echo $BASE_16_NUM | awk -F '#' '{print $2}'
|
||||||
|
else
|
||||||
|
echo $BASE_16_NUM
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
#############################################################
|
||||||
|
# BEGINNING OF MAIN
|
||||||
|
#############################################################
|
||||||
|
|
||||||
|
# Query the system for the IP address using the "host $(hostname)"
|
||||||
|
# command substitution.
|
||||||
|
|
||||||
|
IP=$(host $(hostname) | awk '{print $3}' | awk -F ',' '{print $1}')
|
||||||
|
|
||||||
|
# Field delimit the IP address on the dots (.) and assign each
|
||||||
|
# number to a separate variable in a "while read" loop.
|
||||||
|
|
||||||
|
echo $IP | awk -F '.' '{print $1, $2, $3, $4}' | while read a b c d junk
|
||||||
|
do
|
||||||
|
# Convert each of the numbers in the IP address
|
||||||
|
# into hexadecimal by calling the "convert_base_10_to16"
|
||||||
|
# function.
|
||||||
|
|
||||||
|
FIRST=$(convert_base_10_to_16 $a)
|
||||||
|
SECOND=$(convert_base_10_to_16 $b)
|
||||||
|
THIRD=$(convert_base_10_to_16 $c)
|
||||||
|
FORTH=$(convert_base_10_to_16 $d)
|
||||||
|
done
|
||||||
|
|
||||||
|
# Combine all of the hexadecimal strings into a single
|
||||||
|
# hexadecimal string, which represents the software key.
|
||||||
|
|
||||||
|
echo "${FIRST}${SECOND}${THIRD}${FORTH}"
|
||||||
|
|
||||||
206
chapter15/hgrep.bash
Executable file
206
chapter15/hgrep.bash
Executable file
@@ -0,0 +1,206 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
#
|
||||||
|
# SCRIPT: hgrep.bash
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 07/09/2007
|
||||||
|
# REV 3.0
|
||||||
|
#
|
||||||
|
# PLATFORM: Not Platform Dependent
|
||||||
|
#
|
||||||
|
# PURPOSE: This script is used to highlight text in a file.
|
||||||
|
# Given a text string and a file the script will search
|
||||||
|
# the file for the specified string pattern and highlight
|
||||||
|
# each occurrence. For standard input the script pages a
|
||||||
|
# temporary file which has the string text highlighted.
|
||||||
|
#
|
||||||
|
# REV LIST:
|
||||||
|
#
|
||||||
|
# set -x # Uncomment to debug
|
||||||
|
# set -n # Uncomment to check script syntax without execution
|
||||||
|
#
|
||||||
|
# EXIT CODES:
|
||||||
|
# 0 ==> Script exited normally
|
||||||
|
# 1 ==> Usage error
|
||||||
|
# 2 ==> Input File Error
|
||||||
|
# 3 ==> Pattern not found in the file
|
||||||
|
#
|
||||||
|
# REV LIST:
|
||||||
|
# 03/12/2001 - Randy Michael - Sr. Sys. Admin.
|
||||||
|
# Added code to just exit if the string is not in
|
||||||
|
# the target file.
|
||||||
|
#
|
||||||
|
# 03/13/2001 - Randy Michael - Sr. Sys. Admin.
|
||||||
|
# Added code to ensure the target file is a readable
|
||||||
|
# "regular" non-zero file.
|
||||||
|
#
|
||||||
|
# 03/13/2001 - Randy Michael - Sr. Sys. Admin.
|
||||||
|
# Added code to highlight the text string and filename
|
||||||
|
# in the error and information messages.
|
||||||
|
#
|
||||||
|
# 08-22-2001 - Randy Michael - Sr. Sys. Admin
|
||||||
|
# Changed the code to allow this script to accept standard
|
||||||
|
# input from a pipe. This makes the script work more like the
|
||||||
|
# grep command
|
||||||
|
#
|
||||||
|
# 7/02/2007 - Randy Michael - Sr. Sys. Admin
|
||||||
|
# Converted this script to BASH shell.
|
||||||
|
#
|
||||||
|
##############################################
|
||||||
|
# DEFINE FILES AND VARIABLES HERE
|
||||||
|
##############################################
|
||||||
|
|
||||||
|
SCRIPT_NAME=$(basename $0)
|
||||||
|
OUTPUT_FILE="/tmp/highlightfile.out"
|
||||||
|
>$OUTPUT_FILE
|
||||||
|
|
||||||
|
##############################################
|
||||||
|
# DEFINE FUNCTIONS HERE
|
||||||
|
##############################################
|
||||||
|
|
||||||
|
function usage
|
||||||
|
{
|
||||||
|
echo -e "\nUSAGE: $SCRIPT_NAME pattern [filename]\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
##############################################
|
||||||
|
# CHECK COMMAND SYNTAX
|
||||||
|
##############################################
|
||||||
|
|
||||||
|
# Input coming from standard input
|
||||||
|
|
||||||
|
if (( $# == 1 ))
|
||||||
|
then
|
||||||
|
# Input coming from standard input
|
||||||
|
|
||||||
|
PATTERN="$1" # Pattern to highlight
|
||||||
|
FILENAME= # Assign NULL to FILENAME
|
||||||
|
|
||||||
|
elif (( $# == 2 ))
|
||||||
|
then
|
||||||
|
# Input coming from $FILENAME file
|
||||||
|
|
||||||
|
PATTERN="$1" # Pattern to highlight
|
||||||
|
FILENAME="$2" # File to use as input
|
||||||
|
|
||||||
|
# Does the file exist as a "regular" file?
|
||||||
|
|
||||||
|
if [ ! -f "$FILENAME" ]
|
||||||
|
then
|
||||||
|
echo -e "\nERROR: $FILENAME does not exist...\n"
|
||||||
|
usage
|
||||||
|
exit 2
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Is the file empty?
|
||||||
|
|
||||||
|
if [ ! -s "$FILENAME" ]
|
||||||
|
then
|
||||||
|
echo -e "\nERROR: \c"
|
||||||
|
tput smso
|
||||||
|
echo -e "$FILENAME\c"
|
||||||
|
tput sgr0
|
||||||
|
echo -e " file size is zero...nothing to search\n"
|
||||||
|
usage
|
||||||
|
exit 2
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Is the file readable by this script?
|
||||||
|
|
||||||
|
if [ ! -r "$FILENAME" ]
|
||||||
|
then
|
||||||
|
echo -e "\nERROR: \c"
|
||||||
|
tput smso
|
||||||
|
echo -e "${FILENAME}\c"
|
||||||
|
tput sgr0
|
||||||
|
echo -e " is not readable to this program...\n"
|
||||||
|
usage
|
||||||
|
exit 2
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Is the pattern anywhere in the file?
|
||||||
|
|
||||||
|
grep -q "$PATTERN" "$FILENAME"
|
||||||
|
if (( $? != 0 ))
|
||||||
|
then
|
||||||
|
echo -e "\nSORRY: The string \c"
|
||||||
|
tput smso
|
||||||
|
echo -e "${PATTERN}\c"
|
||||||
|
tput sgr0
|
||||||
|
echo -e " was not found in \c"
|
||||||
|
tput smso
|
||||||
|
echo -e "${FILENAME}\c"
|
||||||
|
tput sgr0
|
||||||
|
echo -e "\n\n....EXITING...\n"
|
||||||
|
exit 3
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
# Incorrect number of command-line arguments
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
##############################################
|
||||||
|
# BEGINNING OF MAIN
|
||||||
|
##############################################
|
||||||
|
|
||||||
|
# There is no $FILENAME if we get input from a pipe...
|
||||||
|
|
||||||
|
if [[ ! -z "$FILENAME" && "$FILENAME" != '' ]]
|
||||||
|
then
|
||||||
|
# Using $FILENAME as input
|
||||||
|
|
||||||
|
case $(uname) in
|
||||||
|
AIX|HP-UX)
|
||||||
|
# This is a fancy "pg" command. It acts similar to the
|
||||||
|
# "more" command but instead of showing the percentage
|
||||||
|
# displayed it shows the page number of the file
|
||||||
|
|
||||||
|
sed s/"${PATTERN}"/$(tput smso)"${PATTERN}"$(tput sgr0)/g \
|
||||||
|
"$FILENAME" | pg -csn -p"Page %d:"
|
||||||
|
exit 0
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
sed s/"${PATTERN}"/$(tput smso)"${PATTERN}"$(tput sgr0)/g \
|
||||||
|
"$FILENAME" | more
|
||||||
|
exit 0
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
else
|
||||||
|
# Input is from standard input...
|
||||||
|
sed s/"${PATTERN}"/$(tput smso)"${PATTERN}"$(tput sgr0)/g \
|
||||||
|
> $OUTPUT_FILE
|
||||||
|
|
||||||
|
# Is the pattern anywhere in the file?
|
||||||
|
grep -q "$PATTERN" $OUTPUT_FILE
|
||||||
|
if (( $? != 0 ))
|
||||||
|
then
|
||||||
|
echo -e "\nERROR: The string \c"
|
||||||
|
tput smso
|
||||||
|
echo -e "${PATTERN}\c"
|
||||||
|
tput sgr0
|
||||||
|
echo -e " was not found in standard input\c"
|
||||||
|
echo -e "\n\n....EXITING...\n"
|
||||||
|
exit 3
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Check the operating system, on AIX and HP-UX we need to
|
||||||
|
# use the "pg", or "page" command. The "more" command does
|
||||||
|
# not work to highlight the text, it will only show the
|
||||||
|
# characters that make up the escape sequence. All
|
||||||
|
# other operating system usr the "more" command.
|
||||||
|
|
||||||
|
case $(uname) in
|
||||||
|
AIX|HP-UX)
|
||||||
|
|
||||||
|
# This is a fancy "pg" command. It acts similar to the
|
||||||
|
# "more" command but instead of showing the percentage
|
||||||
|
# displayed it shows the page number of the file
|
||||||
|
|
||||||
|
cat $OUTPUT_FILE | pg -csn -p"Page %d:"
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
cat $OUTPUT_FILE | more
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
195
chapter15/hgrep.ksh
Executable file
195
chapter15/hgrep.ksh
Executable file
@@ -0,0 +1,195 @@
|
|||||||
|
#!/usr/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: hgrep.ksh
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 03/09/2001
|
||||||
|
# REV 2.1.P
|
||||||
|
#
|
||||||
|
# PLATFORM: Not Platform Dependent
|
||||||
|
#
|
||||||
|
# PURPOSE: This script is used to highlight text in a file. Given a text
|
||||||
|
# string and a file the script will search the file for the specified
|
||||||
|
# string and highlight each occurrence of the string by paging
|
||||||
|
# a temporary file which has the string text highlighted.
|
||||||
|
#
|
||||||
|
# REV LIST:
|
||||||
|
#
|
||||||
|
# set -x # Uncomment to debug
|
||||||
|
# set -n # Uncomment to check command syntax without execution
|
||||||
|
#
|
||||||
|
# EXIT CODES:
|
||||||
|
#
|
||||||
|
# 0 ==> Script exited normally
|
||||||
|
# 1 ==> Usage error
|
||||||
|
# 2 ==> Input File Error
|
||||||
|
# 3 ==> Pattern not found in the file
|
||||||
|
#
|
||||||
|
# REV LIST:
|
||||||
|
# 03/12/2001 - Randy Michael - Sr. Sys. Admin.
|
||||||
|
# Added code to just exit if the string is not in
|
||||||
|
# the target file.
|
||||||
|
#
|
||||||
|
# 03/13/2001 - Randy Michael - Sr. Sys. Admin.
|
||||||
|
# Added code to ensure the target file is a readable "regular"
|
||||||
|
# non-zero file.
|
||||||
|
#
|
||||||
|
# 03/13/2001 - Randy Michael - Sr. Sys. Admin.
|
||||||
|
# Added code to highlight the text string and filename
|
||||||
|
# in the error and information messages.
|
||||||
|
#
|
||||||
|
# 08-22-2001 - Randy Michael - Sr. Sys. Admin
|
||||||
|
# Changed the code to allow this script to accept standard
|
||||||
|
# input from a pipe. This makes the script work more like the
|
||||||
|
# grep command
|
||||||
|
|
||||||
|
SCRIPT_NAME=`basename $0`
|
||||||
|
|
||||||
|
##############################################
|
||||||
|
########### DEFINE FUNCTIONS HERE ############
|
||||||
|
##############################################
|
||||||
|
|
||||||
|
function usage
|
||||||
|
{
|
||||||
|
echo "\nUSAGE: $SCRIPT_NAME pattern [filename]\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
##############################################
|
||||||
|
########### CHECK COMMAND SYNTAX #############
|
||||||
|
##############################################
|
||||||
|
|
||||||
|
|
||||||
|
# Input coming from standard input
|
||||||
|
|
||||||
|
if [ $# -eq 1 ]
|
||||||
|
then
|
||||||
|
# Input coming from standard input
|
||||||
|
|
||||||
|
PATTERN="$1" # Pattern to highlight
|
||||||
|
FILENAME= # Assign NULL to FILENAME
|
||||||
|
|
||||||
|
elif [ $# -eq 2 ]
|
||||||
|
then
|
||||||
|
# Input coming from $FILENAME file
|
||||||
|
|
||||||
|
PATTERN="$1" # Pattern to highlight
|
||||||
|
FILENAME="$2" # File to use as input
|
||||||
|
|
||||||
|
# Does the file exist as a "regular" file?
|
||||||
|
|
||||||
|
if [ ! -f $FILENAME ]
|
||||||
|
then
|
||||||
|
echo "\nERROR: $FILENAME does not exist...\n"
|
||||||
|
usage
|
||||||
|
exit 2
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Is the file empty?
|
||||||
|
|
||||||
|
if [ ! -s $FILENAME ]
|
||||||
|
then
|
||||||
|
echo "\nERROR: \c"
|
||||||
|
tput smso
|
||||||
|
echo "$FILENAME\c"
|
||||||
|
tput sgr0
|
||||||
|
echo " file size is zero...nothing to search\n"
|
||||||
|
usage
|
||||||
|
exit 2
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Is the file readable by this script?
|
||||||
|
|
||||||
|
if [ ! -r $FILENAME ]
|
||||||
|
then
|
||||||
|
echo "\nERROR: \c"
|
||||||
|
tput smso
|
||||||
|
echo "${FILENAME}\c"
|
||||||
|
tput sgr0
|
||||||
|
echo " is not readable to this program...\n"
|
||||||
|
usage
|
||||||
|
exit 2
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Is the pattern anywhere in the file?
|
||||||
|
|
||||||
|
grep "$PATTERN" $FILENAME >/dev/null 2>&1
|
||||||
|
if [ $? -ne 0 ]
|
||||||
|
then
|
||||||
|
echo "\nSORRY: The string \c"
|
||||||
|
tput smso
|
||||||
|
echo "${PATTERN}\c"
|
||||||
|
tput sgr0
|
||||||
|
echo " was not found in \c"
|
||||||
|
tput smso
|
||||||
|
echo "${FILENAME}\c"
|
||||||
|
tput sgr0
|
||||||
|
echo "\n\n....EXITING...\n"
|
||||||
|
exit 3
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
# Incorrect number of command line arguments
|
||||||
|
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
##############################################
|
||||||
|
########### DEFINE VARIABLES HERE ############
|
||||||
|
##############################################
|
||||||
|
|
||||||
|
OUTPUT_FILE="/tmp/highlightfile.out"
|
||||||
|
>$OUTPUT_FILE
|
||||||
|
|
||||||
|
##############################################
|
||||||
|
############ START OF MAIN ###################
|
||||||
|
##############################################
|
||||||
|
|
||||||
|
# There is no $FILENAME if we get input from a pipe...
|
||||||
|
|
||||||
|
if [[ ! -z "$FILENAME" && $FILENAME != '' ]]
|
||||||
|
then
|
||||||
|
# Using $FILENAME as input
|
||||||
|
|
||||||
|
cat "$FILENAME" \
|
||||||
|
| sed s/"${PATTERN}"/$(tput smso)"${PATTERN}"$(tput sgr0)/g \
|
||||||
|
> $OUTPUT_FILE
|
||||||
|
else
|
||||||
|
# Input is from standard input...
|
||||||
|
|
||||||
|
sed s/"${PATTERN}"/$(tput smso)"${PATTERN}"$(tput sgr0)/g \
|
||||||
|
> $OUTPUT_FILE
|
||||||
|
|
||||||
|
# Is the pattern anywhere in the file?
|
||||||
|
|
||||||
|
grep "$PATTERN" $OUTPUT_FILE >/dev/null 2>&1
|
||||||
|
if [ $? -ne 0 ]
|
||||||
|
then
|
||||||
|
echo "\nSORRY: The string \c"
|
||||||
|
tput smso
|
||||||
|
echo "${PATTERN}\c"
|
||||||
|
tput sgr0
|
||||||
|
echo " was not found in standard input\c"
|
||||||
|
echo "\n\n....EXITING...\n"
|
||||||
|
exit 3
|
||||||
|
fi
|
||||||
|
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Check the operating system, on AIX and HP-UX we need to
|
||||||
|
# use the "pg", or "page" command. The "more" command does
|
||||||
|
# not work to highlight the text, it will only show the
|
||||||
|
# characters that make up the escape sequence. All
|
||||||
|
# other operating system usr the "more" command.
|
||||||
|
|
||||||
|
case $(uname) in
|
||||||
|
AIX|HP-UX)
|
||||||
|
|
||||||
|
# This is a fancy "pg" command. It acts similar to the
|
||||||
|
# "more" command but instead of showing the percentage
|
||||||
|
# displayed it shows the page number of the file
|
||||||
|
|
||||||
|
cat $OUTPUT_FILE | pg -csn -p"Page %d:"
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
cat $OUTPUT_FILE | more
|
||||||
|
;;
|
||||||
|
esac
|
||||||
35
chapter16/function_check_HTTP_server
Executable file
35
chapter16/function_check_HTTP_server
Executable file
@@ -0,0 +1,35 @@
|
|||||||
|
check_HTTP_server ()
|
||||||
|
{
|
||||||
|
LINX="/usr/local/bin/lynx" # Define the location of the linx program
|
||||||
|
URL=$1 # Capture the target URL in the $1 position
|
||||||
|
URLFILE=/tmp/HTTP.$$ # Define a file to hold the URL output
|
||||||
|
|
||||||
|
###########################################
|
||||||
|
|
||||||
|
$LINX "$URL" > $URLFILE # Attempt to reach the target URL
|
||||||
|
|
||||||
|
if (($? != 0)) # If the URL is unreachable - No Connection
|
||||||
|
then
|
||||||
|
echo "\n$URL - Unable to connect\n"
|
||||||
|
cat $URLFILE
|
||||||
|
else # Else the URL was found
|
||||||
|
|
||||||
|
while read VER RC STATUS # This while loop is fed from the bottom
|
||||||
|
# after the "done" using input redirection
|
||||||
|
do
|
||||||
|
case $RC in # Check the return code in the $URLFILE
|
||||||
|
|
||||||
|
200|401|301|302) # These are valid return codes!
|
||||||
|
echo "\nHTTP Server is OK\n"
|
||||||
|
;;
|
||||||
|
*) # Anything else is not a valid return code
|
||||||
|
|
||||||
|
echo "\nERROR: HTTP Server Error\n"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
done < $URLFILE
|
||||||
|
fi
|
||||||
|
|
||||||
|
rm -f $URLFILE
|
||||||
|
}
|
||||||
197
chapter16/keyit
Executable file
197
chapter16/keyit
Executable file
@@ -0,0 +1,197 @@
|
|||||||
|
#!/usr/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: keyit
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 7/31/2007
|
||||||
|
# REV: 1.0
|
||||||
|
# PLATFORM: Not platform dependent
|
||||||
|
# REQUIREMENTS: OpenSSH
|
||||||
|
#
|
||||||
|
# PURPOSE: This script is used to set up
|
||||||
|
# encryption keypairs between two hosts.
|
||||||
|
#
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
#
|
||||||
|
# set -n # Uncomment to check script syntax
|
||||||
|
# # without any execution. Do not
|
||||||
|
# # forget to add the comment back,
|
||||||
|
# # or the script will never execute.
|
||||||
|
#
|
||||||
|
# USAGE: keyit remote_host username
|
||||||
|
#
|
||||||
|
#######################################
|
||||||
|
# DEFINE FILES AND VARIABLES HERE
|
||||||
|
#######################################
|
||||||
|
|
||||||
|
RHOST=$1
|
||||||
|
THIS_USER=$2
|
||||||
|
THIS_SCRIPT=$(basename $0)
|
||||||
|
THIS_HOST=$(hostname)
|
||||||
|
|
||||||
|
#######################################
|
||||||
|
# DEFINE FUNCTIONS HERE
|
||||||
|
#######################################
|
||||||
|
|
||||||
|
usage ()
|
||||||
|
{
|
||||||
|
echo "\nUSAGE: $THIS_SCRIPT \
|
||||||
|
remote_host username\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
#######################################
|
||||||
|
|
||||||
|
success_message ()
|
||||||
|
{
|
||||||
|
KTYPE=$1
|
||||||
|
echo "\nSUCCESS: $KTYPE key pairs configured for $THIS_USER on $RHOST"
|
||||||
|
echo "\n$THIS_USER should no longer require an SSH password on $RHOST"
|
||||||
|
echo "when logging in directly, however, using the ssh commands:\n"
|
||||||
|
echo "\tssh -l $THIS_USER $RHOST\nAND\n\tssh ${THIS_USER}@${RHOST}"
|
||||||
|
echo "\nWHILE LOGGED IN LOCALLY AS ANOTHER USER will still not work"
|
||||||
|
echo "without a valid user password\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
#######################################
|
||||||
|
|
||||||
|
failure_message ()
|
||||||
|
{
|
||||||
|
echo "\nERROR: Setting up the $KEYTYPE key pairs failed"
|
||||||
|
echo "Ensure that OpenSSH is installed and running"
|
||||||
|
echo "on both hosts. Then ensure that the user has"
|
||||||
|
echo "a .ssh directory in their \$HOME directory."
|
||||||
|
echo "See the man page on ssh and ssh-keygen"
|
||||||
|
echo "for more details and manual setup\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
#######################################
|
||||||
|
|
||||||
|
keyit_dsa ()
|
||||||
|
{
|
||||||
|
# Append the local public key to the same user's
|
||||||
|
# authorized_users file
|
||||||
|
|
||||||
|
cat ~${THIS_USER}/.ssh/id_dsa.pub | ssh ${THIS_USER}@$RHOST \
|
||||||
|
"cat >> ~${THIS_USER}/.ssh/authorized_keys"
|
||||||
|
|
||||||
|
if (( $? == 0 ))
|
||||||
|
then
|
||||||
|
success_message dsa
|
||||||
|
else
|
||||||
|
failure_message
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
#######################################
|
||||||
|
|
||||||
|
keyit_rsa ()
|
||||||
|
{
|
||||||
|
# Append the local public key to the same user's
|
||||||
|
# authorized_users file
|
||||||
|
|
||||||
|
cat ~${THIS_USER}/.ssh/id_rsa.pub | ssh ${THIS_USER}@$RHOST \
|
||||||
|
"cat >> ~${THIS_USER}/.ssh/authorized_keys"
|
||||||
|
|
||||||
|
if (( $? == 0 ))
|
||||||
|
then
|
||||||
|
success_message rsa
|
||||||
|
else
|
||||||
|
failure_message
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
#######################################
|
||||||
|
# BEGINNING OF MAIN
|
||||||
|
#######################################
|
||||||
|
|
||||||
|
# Ensure the user $THIS_USER exists on the local system
|
||||||
|
|
||||||
|
if ! $(/usr/bin/id $THIS_USER >/dev/null 2>&1)
|
||||||
|
then
|
||||||
|
echo "\nERROR: $THIS_USER is not a valid user on $THIS_HOST\n"
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Ensure ssh is installed locally
|
||||||
|
|
||||||
|
if [[ ! -x /usr/bin/ssh && ! -x /usr/local/bin/ssh ]]
|
||||||
|
then
|
||||||
|
echo "\nERROR: SSH does not appear to be installed on this machine"
|
||||||
|
echo "This script requires SSH...Exiting...\n"
|
||||||
|
usage
|
||||||
|
exit 2
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Check for proper usage
|
||||||
|
|
||||||
|
if ! [ $2 ]
|
||||||
|
then
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Ping the remote host 1 ping
|
||||||
|
|
||||||
|
if ! $(ping -c1 $RHOST >/dev/null 2>&1)
|
||||||
|
then
|
||||||
|
echo "\nERROR: $RHOST is not pingable...Exiting...\n"
|
||||||
|
exit 2
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Set up the key pairs for the configured key(s)
|
||||||
|
|
||||||
|
SET=0
|
||||||
|
|
||||||
|
if [ -s ~$THIS_USER/.ssh/id_dsa.pub ]
|
||||||
|
then
|
||||||
|
keyit_dsa
|
||||||
|
SET=1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -s ~$THIS_USER/.ssh/id_rsa.pub ]
|
||||||
|
then
|
||||||
|
keyit_rsa
|
||||||
|
SET=2
|
||||||
|
fi
|
||||||
|
|
||||||
|
if (( SET == 0 ))
|
||||||
|
then
|
||||||
|
echo "\nERROR: SSH public key is not set for $THIS_USER..."
|
||||||
|
echo "\nTo Configure Run: ssh-keygen -t type"
|
||||||
|
echo "Where type is rsa or dsa encryption\n"
|
||||||
|
echo "Would you like to set up the keys now? (y/n): \c"
|
||||||
|
read REPLY
|
||||||
|
case $REPLY in
|
||||||
|
y|Y) if $(id $THIS_USER >/dev/null 2>&1)
|
||||||
|
then
|
||||||
|
echo "\nEncryption Type: (dsa or rsa?): \c"
|
||||||
|
read KEYTYPE
|
||||||
|
case "$KEYTYPE" in
|
||||||
|
+([d|Ds|Sa|A])) KEYTYPE=dsa
|
||||||
|
;;
|
||||||
|
+([r|Rs|Sa|A])) KEYTYPE=rsa
|
||||||
|
;;
|
||||||
|
*) echo "\nERROR: Invalid entry...Exiting..."
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
echo "\nAccept the defaults and do not enter a passphrase...\n"
|
||||||
|
su - $THIS_USER "-c ssh-keygen -t $KEYTYPE"
|
||||||
|
if (( $? == 0 ))
|
||||||
|
then
|
||||||
|
echo "\nSuccess, keying $THIS_USER on $RHOST\n"
|
||||||
|
keyit_${KEYTYPE} $KEYTYPE
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo "\nERROR: $THIS_USER username does not exist\n"
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
*) # Do nothing
|
||||||
|
: # A colon, :, is a "no-op"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
fi
|
||||||
|
|
||||||
|
#########################################
|
||||||
|
# END OF KEYIT SCRIPT
|
||||||
|
#########################################
|
||||||
152
chapter16/proc_mon.ksh
Executable file
152
chapter16/proc_mon.ksh
Executable file
@@ -0,0 +1,152 @@
|
|||||||
|
#!/usr/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: proc_mon.ksh
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 02/14/2007
|
||||||
|
# REV: 1.1.P
|
||||||
|
# PLATFORM: Not Platform Dependent
|
||||||
|
#
|
||||||
|
# PURPOSE: This script is used to monitor a process to end
|
||||||
|
# specified by ARG1 if a single command-line argument is
|
||||||
|
# used. There is also a "verbose" mode where the monitored
|
||||||
|
# process is displayed and ARG2 is monitored.
|
||||||
|
#
|
||||||
|
# USAGE: proc_mon.ksh [-v] process-to-monitor
|
||||||
|
#
|
||||||
|
# EXIT STATUS:
|
||||||
|
# 0 ==> Monitored process has terminated
|
||||||
|
# 1 ==> Script usage error
|
||||||
|
# 2 ==> Target process to monitor is not active
|
||||||
|
# 3 ==> This script exits on a trapped signal
|
||||||
|
#
|
||||||
|
# REV. LIST:
|
||||||
|
#
|
||||||
|
# 02/22/2007 - Added code for a "verbose" mode to output the
|
||||||
|
# results of the .ps aux. command. The verbose
|
||||||
|
# mode is set using a "-v" switch.
|
||||||
|
#
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
# set -n # Uncomment to debug without any command execution
|
||||||
|
|
||||||
|
SCRIPT_NAME=`basename $0`
|
||||||
|
|
||||||
|
########################################################
|
||||||
|
############ DEFINE FUNCTIONS HERE #####################
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
function usage
|
||||||
|
{
|
||||||
|
echo "\n\n"
|
||||||
|
echo "USAGE: $SCRIPT_NAME [-v] {Process_to_monitor}"
|
||||||
|
echo "\nEXAMPLE: $SCRIPT_NAME my_backup\n"
|
||||||
|
echo "OR"
|
||||||
|
echo "\nEXAMPLE: $SCRIPT_NAME -v my_backup\n"
|
||||||
|
echo "Try again...EXITING...\n"
|
||||||
|
}
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
function exit_trap
|
||||||
|
{
|
||||||
|
echo "\n...EXITING on trapped signal...\n"
|
||||||
|
}
|
||||||
|
########################################################
|
||||||
|
################ START OF MAIN##########################
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
################
|
||||||
|
# Set a trap...#
|
||||||
|
################
|
||||||
|
|
||||||
|
trap 'exit_trap; exit 3' 1 2 3 15
|
||||||
|
|
||||||
|
# First Check for the Correct Number of Arguments
|
||||||
|
# One or Two is acceptable
|
||||||
|
|
||||||
|
if (( $# != 1 && $# != 2 ))
|
||||||
|
then
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Parse through the command-line arguments and see if verbose
|
||||||
|
# mode has been specified. NOTICE that we assign the target
|
||||||
|
# process to the PROCESS variable!!!
|
||||||
|
# Embedded case statement...
|
||||||
|
|
||||||
|
case $# in
|
||||||
|
1) case $1 in
|
||||||
|
'-v') usage
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
*) PROCESS=$1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
;;
|
||||||
|
2) case $1 in
|
||||||
|
'-v') continue
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
case $2 in
|
||||||
|
'-v') usage
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
*) PROCESS=$2
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
;;
|
||||||
|
*) usage
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
# Check if the process is running or exit!
|
||||||
|
|
||||||
|
ps aux | grep "$PROCESS" | grep -v "grep $PROCESS" \
|
||||||
|
| grep -v $SCRIPT_NAME >/dev/null
|
||||||
|
|
||||||
|
if (( $? != 0 ))
|
||||||
|
then
|
||||||
|
echo "\n\n$PROCESS is NOT an active process...EXITING...\n"
|
||||||
|
exit 2
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Show verbose mode if specified...
|
||||||
|
|
||||||
|
if (( $# == 2 )) && [[ $1 = "-v" ]]
|
||||||
|
then
|
||||||
|
# Verbose mode has been specified!
|
||||||
|
echo "\n"
|
||||||
|
|
||||||
|
# Extract the columns heading from the ps aux output
|
||||||
|
ps aux | head -n 1
|
||||||
|
|
||||||
|
ps aux | grep "$PROCESS" | grep -v "grep $PROCESS" \
|
||||||
|
| grep -v $SCRIPT_NAME
|
||||||
|
fi
|
||||||
|
|
||||||
|
##### O.K. The process is running, start monitoring...
|
||||||
|
|
||||||
|
SLEEP_TIME="1" # Seconds between monitoring
|
||||||
|
|
||||||
|
RC="0" # RC is the Return Code
|
||||||
|
echo "\n\n" # Give a couple of blank lines
|
||||||
|
|
||||||
|
echo "$PROCESS is currently RUNNING...`date`\n"
|
||||||
|
|
||||||
|
####################################
|
||||||
|
# Loop UNTIL the $PROCESS stops...
|
||||||
|
|
||||||
|
while (( RC == 0 )) # Loop until the return code is not zero
|
||||||
|
do
|
||||||
|
ps aux | grep $PROCESS | grep -v "grep $PROCESS" \
|
||||||
|
| grep -v $SCRIPT_NAME >/dev/null 2>&1
|
||||||
|
if (( $? != 0 )) # Check the Return Code!!!!!
|
||||||
|
then
|
||||||
|
echo "\n...$PROCESS has COMPLETED...`date`\n"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
sleep $SLEEP_TIME # Needed to reduce CPU Load!!!
|
||||||
|
done
|
||||||
|
|
||||||
|
# End of Script
|
||||||
78
chapter16/proc_wait.ksh
Executable file
78
chapter16/proc_wait.ksh
Executable file
@@ -0,0 +1,78 @@
|
|||||||
|
#!/usr/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: proc_wait.ksh
|
||||||
|
#
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
#
|
||||||
|
# DATE: 02/14/2007
|
||||||
|
#
|
||||||
|
# REV: 1.1.A
|
||||||
|
#
|
||||||
|
# PURPOSE: This script is used to wait for a process to start.
|
||||||
|
# The process, specified by ARG 1 as passed to this script, should not
|
||||||
|
# currently be running when this is started. This script waits for the
|
||||||
|
# process to start and exits.
|
||||||
|
#
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
# set -n # Uncomment to debug without any command execution
|
||||||
|
|
||||||
|
################ FUNCTIONS #############################
|
||||||
|
########################################################
|
||||||
|
function usage
|
||||||
|
{
|
||||||
|
echo "\n\n"
|
||||||
|
echo "USAGE: `basename $0` {Process_to_monitor}"
|
||||||
|
echo "\nEXAMPLE: `basename $0` bffcreate\n"
|
||||||
|
echo "Try again...EXITING...\n"
|
||||||
|
}
|
||||||
|
########################################################
|
||||||
|
function exit_trap
|
||||||
|
{
|
||||||
|
echo "\n...EXITING on trapped signal...\n"
|
||||||
|
}
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
|
||||||
|
################ START OF MAIN##########################
|
||||||
|
|
||||||
|
if [ $# -ne 1 ]
|
||||||
|
then
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
else
|
||||||
|
ARG1="$1"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Set a trap...
|
||||||
|
trap 'exit_trap; exit 2' 1 2 3 15
|
||||||
|
|
||||||
|
# Check to execute or just exit...
|
||||||
|
ps -ef | grep $ARG1 | grep -v "grep $ARG1" | grep -v `basename $0` \
|
||||||
|
>/dev/null
|
||||||
|
|
||||||
|
if [ $? -eq 0 ]
|
||||||
|
then
|
||||||
|
echo "\n\n$ARG1 is an active process...EXITING...\n"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
##### O.K. The process is NOT running, start monitoring for startup...
|
||||||
|
|
||||||
|
SLEEP_TIME="1" # Seconds between monitoring
|
||||||
|
RC="1" # RC is the Return Code
|
||||||
|
echo "\n\n" # Give a couple of blank lines
|
||||||
|
|
||||||
|
echo "WAITING for $ARG1 to start...`date`\n"
|
||||||
|
|
||||||
|
until [ $RC -eq 0 ] # Loop until the return code is zero
|
||||||
|
do
|
||||||
|
ps -ef | grep $ARG1 | egrep -v "grep $ARG1" | grep -v `basename $0` \
|
||||||
|
>/dev/null 2>&1
|
||||||
|
RC="$?"
|
||||||
|
if [ $RC -eq 0 ]
|
||||||
|
then
|
||||||
|
echo "$ARG1 is RUNNING...`date`\n"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
sleep $SLEEP_TIME
|
||||||
|
done
|
||||||
151
chapter16/proc_watch.ksh
Executable file
151
chapter16/proc_watch.ksh
Executable file
@@ -0,0 +1,151 @@
|
|||||||
|
#!/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: proc_watch.ksh
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 09-12-2007
|
||||||
|
# REV: 1.0.P
|
||||||
|
# PLATFORM: Not Platform Dependent
|
||||||
|
#
|
||||||
|
# PURPOSE: This script is used to monitor and log
|
||||||
|
# the status of a process as it starts and stops.
|
||||||
|
#
|
||||||
|
# REV LIST:
|
||||||
|
#
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
# set -n # Uncomment to check syntax without ANY execution
|
||||||
|
#
|
||||||
|
####################################################
|
||||||
|
########## DEFINE FILES AND VARIABLES HERE #########
|
||||||
|
####################################################
|
||||||
|
|
||||||
|
LOGFILE="/tmp/proc_status.log"
|
||||||
|
[[ ! -s $LOGFILE ]] && touch $LOGFILE
|
||||||
|
|
||||||
|
PROCESS="$1" # Process to Monitor
|
||||||
|
SCRIPT_NAME=$(basename $0) # Script Name w/o the PATH
|
||||||
|
TTY=$(tty) # Current tty or pty
|
||||||
|
|
||||||
|
####################################################
|
||||||
|
############# DEFINE FUNCTIONS HERE ################
|
||||||
|
####################################################
|
||||||
|
|
||||||
|
usage ()
|
||||||
|
{
|
||||||
|
echo "\nUSAGE: $SCRIPT_NAME process_to_monitor\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################################
|
||||||
|
|
||||||
|
trap_exit ()
|
||||||
|
{
|
||||||
|
# Log an ending time for process monitoring
|
||||||
|
TIMESTAMP=$(date +%D@%T) # Get a new timestamp...
|
||||||
|
echo "MON_STOP: Monitoring for $PROCESS ended ==> $TIMESTAMP" \
|
||||||
|
| tee -a $LOGFILE
|
||||||
|
|
||||||
|
# Kill all functions
|
||||||
|
kill -9 $(jobs -p) 2>/dev/null
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################################
|
||||||
|
|
||||||
|
mon_proc_end ()
|
||||||
|
{
|
||||||
|
END_RC="0"
|
||||||
|
until (( END_RC != 0 ))
|
||||||
|
do
|
||||||
|
ps aux | grep -v "grep $PROCESS" | grep -v $SCRIPT_NAME \
|
||||||
|
| grep $PROCESS >/dev/null 2>&1
|
||||||
|
|
||||||
|
END_RC=$? # Check the Return Code!!
|
||||||
|
sleep 1 # Needed to reduce CPU load!
|
||||||
|
done
|
||||||
|
|
||||||
|
echo 'N' # Turn the RUN flag off
|
||||||
|
|
||||||
|
# Grab a Timestamp
|
||||||
|
TIMESTAMP=$(date +%D@%T)
|
||||||
|
|
||||||
|
echo "END PROCESS: $PROCESS ended ==> $TIMESTAMP" >> $LOGFILE &
|
||||||
|
echo "END PROCESS: $PROCESS ended ==> $TIMESTAMP" > $TTY
|
||||||
|
}
|
||||||
|
####################################################
|
||||||
|
|
||||||
|
mon_proc_start ()
|
||||||
|
{
|
||||||
|
START_RC="-1" # Initialize to -1
|
||||||
|
until (( START_RC == 0 ))
|
||||||
|
do
|
||||||
|
ps aux | grep -v "grep $PROCESS" | grep -v $SCRIPT_NAME \
|
||||||
|
| grep $PROCESS >/dev/null 2>&1
|
||||||
|
|
||||||
|
START_RC=$? # Check the Return Code!!!
|
||||||
|
sleep 1 # Needed to reduce CPU load!
|
||||||
|
done
|
||||||
|
|
||||||
|
echo 'Y' # Turn the RUN flag on
|
||||||
|
|
||||||
|
# Grab the timestamp
|
||||||
|
TIMESTAMP=$(date +%D@%T)
|
||||||
|
|
||||||
|
echo "START PROCESS: $PROCESS began ==> $TIMESTAMP" >> $LOGFILE &
|
||||||
|
echo "START PROCESS: $PROCESS began ==> $TIMESTAMP" > $TTY
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################################
|
||||||
|
############## START OF MAIN #######################
|
||||||
|
####################################################
|
||||||
|
|
||||||
|
### SET A TRAP ####
|
||||||
|
|
||||||
|
trap 'trap_exit; exit 0' 1 2 3 15
|
||||||
|
|
||||||
|
# Check for the Correct Command Line Argument - Only 1
|
||||||
|
|
||||||
|
if (( $# != 1 ))
|
||||||
|
then
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Get an Initial Process State and Set the RUN Flag
|
||||||
|
|
||||||
|
ps aux | grep -v "grep $PROCESS" | grep -v $SCRIPT_NAME \
|
||||||
|
| grep $PROCESS >/dev/null
|
||||||
|
|
||||||
|
PROC_RC=$? # Check the Return Code!!
|
||||||
|
|
||||||
|
# Give some initial feedback before starting the loop
|
||||||
|
|
||||||
|
if (( PROC_RC == 0 ))
|
||||||
|
then
|
||||||
|
echo "The $PROCESS process is currently running...Monitoring..."
|
||||||
|
RUN="Y" # Set the RUN Flag to YES
|
||||||
|
else
|
||||||
|
echo "The $PROCESS process is not currently running...Monitoring..."
|
||||||
|
RUN="N" # Set the RUN Flag to NO
|
||||||
|
fi
|
||||||
|
|
||||||
|
TIMESTAMP=$(date +%D@%T) # Grab a timestamp for the log
|
||||||
|
|
||||||
|
# Use a "tee -a $#LOGFILE" to send output to both standard output
|
||||||
|
# and to the file referenced by $LOGFILE
|
||||||
|
|
||||||
|
echo "MON_START: Monitoring for $PROCESS began ==> $TIMESTAMP" \
|
||||||
|
| tee -a $LOGFILE
|
||||||
|
|
||||||
|
# Loop Forever!!
|
||||||
|
|
||||||
|
while :
|
||||||
|
do
|
||||||
|
case $RUN in
|
||||||
|
'Y') # Loop Until the Process Ends
|
||||||
|
RUN=$(mon_proc_end)
|
||||||
|
;;
|
||||||
|
'N') # Loop Until the Process Starts
|
||||||
|
RUN=$(mon_proc_start)
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
# End of Script
|
||||||
570
chapter16/proc_watch_timed.ksh
Executable file
570
chapter16/proc_watch_timed.ksh
Executable file
@@ -0,0 +1,570 @@
|
|||||||
|
#!/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: proc_watch_timed.ksh
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 09-14-2007
|
||||||
|
# REV: 1.0.P
|
||||||
|
# PLATFORM: Not Platform Dependent
|
||||||
|
#
|
||||||
|
# PURPOSE: This script is used to monitor and log
|
||||||
|
# the status of a process as it starts and stops.
|
||||||
|
# Command line options are used to identify the target
|
||||||
|
# process to monitor and the length of time to monitor.
|
||||||
|
# Each event is logged to the file defined by the
|
||||||
|
# $LOGFILE variable. This script also has the ability
|
||||||
|
# to execute pre, startup, and post events. These are
|
||||||
|
# controlled by the $RUN_PRE_EVENT, $RUN_STARTUP_EVENT,
|
||||||
|
# and $RUN_POST_EVENT variables. These variables control
|
||||||
|
# execution individually. Whatever is to be executed is to
|
||||||
|
# be placed in either the "pre_event_script",
|
||||||
|
# startup_event_script, or the
|
||||||
|
# "post_event_script" functions, or in any combination.
|
||||||
|
# Timing is controlled on the command line.
|
||||||
|
#
|
||||||
|
# USAGE: $SCRIPT_NAME total_seconds target_process
|
||||||
|
#
|
||||||
|
# Will monitor the specified process for the
|
||||||
|
# specified number of seconds.
|
||||||
|
#
|
||||||
|
# USAGE: $SCRIPT_NAME [-s|-S seconds] [-m|-M minutes]
|
||||||
|
# [-h|-H hours] [-d|-D days]
|
||||||
|
# [-p|-P process]
|
||||||
|
#
|
||||||
|
# Will monitor the specified process for number of
|
||||||
|
# seconds specified within -s seconds, -m minutes,
|
||||||
|
# -h hours, and -d days. Any combination of command
|
||||||
|
# switches can be used.
|
||||||
|
#
|
||||||
|
# REV LIST:
|
||||||
|
#
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
set -n # Uncomment to check syntax without ANY execution
|
||||||
|
#
|
||||||
|
####################################################
|
||||||
|
########## DEFINE FILES AND VARIABLES HERE #########
|
||||||
|
####################################################
|
||||||
|
|
||||||
|
typeset -u RUN_PRE_EVENT # Force to UPPERCASE
|
||||||
|
typeset -u RUN_STARTUP_EVENT # Force to UPPERCASE
|
||||||
|
typeset -u RUN_POST_EVENT # force to UPPERCASE
|
||||||
|
|
||||||
|
RUN_PRE_EVENT='N' # A 'Y' will execute, anything else will not
|
||||||
|
RUN_STARTUP_EVENT='Y' # A 'Y' will execute, anything else will not
|
||||||
|
RUN_POST_EVENT='Y' # A 'Y' will execute, anything else will not
|
||||||
|
|
||||||
|
LOGFILE="/tmp/proc_status.log"
|
||||||
|
[[ ! -s $LOGFILE ]] && touch $LOGFILE
|
||||||
|
|
||||||
|
SCRIPT_NAME=$(basename $0)
|
||||||
|
TTY=$(tty)
|
||||||
|
INTERVAL="1" # Seconds between sampling
|
||||||
|
JOBS=
|
||||||
|
|
||||||
|
####################################################
|
||||||
|
############# DEFINE FUNCTIONS HERE ################
|
||||||
|
####################################################
|
||||||
|
usage ()
|
||||||
|
{
|
||||||
|
echo "\n\n\t*****USAGE ERROR*****"
|
||||||
|
echo "\n\nUSAGE: $SCRIPT_NAME seconds process"
|
||||||
|
echo "\nWill monitor the specified process for the"
|
||||||
|
echo "specified number of seconds."
|
||||||
|
echo "\nUSAGE: $SCRIPT_NAME [-s|-S seconds] [-m|-M minutes]"
|
||||||
|
echo " [-h|-H hours] [-d|-D days] [-p|-P process]\n"
|
||||||
|
echo "\nWill monitor the specified process for number of"
|
||||||
|
echo "seconds specified within -s seconds, -m minutes,"
|
||||||
|
echo "-h hours and -d days. Any combination of command"
|
||||||
|
echo "switches can be used.\n"
|
||||||
|
echo "\nEXAMPLE: $SCRIPT_NAME 300 dtcalc"
|
||||||
|
echo "\n\nEXAMPLE: $SCRIPT_NAME -m 5 -p dtcalc"
|
||||||
|
echo "\nBoth examples will monitor the dtcalc process"
|
||||||
|
echo "for 5 minutes. Can specify days, hours, minutes"
|
||||||
|
echo "and seconds, using -d, -h, -m and -s\n\n"
|
||||||
|
}
|
||||||
|
####################################################
|
||||||
|
trap_exit ()
|
||||||
|
{
|
||||||
|
# set -x # Uncommant to debug this function
|
||||||
|
# Log an ending time for process monitoring
|
||||||
|
echo "INTERRUPT: Program Received an Interrupt...EXITING..." > $TTY
|
||||||
|
echo "INTERRUPT: Program Received an Interrupt...EXITING..." >> $LOGFILE
|
||||||
|
TIMESTAMP=$(date +%D@%T) # Get a new timestamp...
|
||||||
|
echo "MON_STOPPED: Monitoring for $PROCESS ended ==> $TIMESTAMP\n" \
|
||||||
|
>> $TTY
|
||||||
|
echo "MON_STOPPED: Monitoring for $PROCESS ended ==> $TIMESTAMP\n" \
|
||||||
|
>> $LOGFILE
|
||||||
|
echo "LOGFILE: All Events are Logged ==> $LOGFILE \n" > $TTY
|
||||||
|
|
||||||
|
# Kill all functions
|
||||||
|
JOBS=$(jobs -p)
|
||||||
|
if [[ ! -z $JOBS && $JOBS != '' && $JOBS != '0' ]]
|
||||||
|
then
|
||||||
|
kill $(jobs -p) 2>/dev/null 1>&2
|
||||||
|
fi
|
||||||
|
return 2
|
||||||
|
}
|
||||||
|
####################################################
|
||||||
|
pre_event_script ()
|
||||||
|
{
|
||||||
|
# Put anything that you want to execute BEFORE the
|
||||||
|
# monitored process STARTS in this function
|
||||||
|
|
||||||
|
: # No-OP - Needed as a placeholder for an empty function
|
||||||
|
# Comment Out the Above colon, ':'
|
||||||
|
|
||||||
|
PRE_RC=$?
|
||||||
|
return $PRE_RC
|
||||||
|
}
|
||||||
|
####################################################
|
||||||
|
startup_event_script ()
|
||||||
|
{
|
||||||
|
# Put anything that you want to execute WHEN, or AS, the
|
||||||
|
# monitored process STARTS in this function
|
||||||
|
|
||||||
|
: # No-OP - Needed as a placeholder for an empty function
|
||||||
|
# Comment Out the Above colon, ':'
|
||||||
|
|
||||||
|
STARTUP_RC=$?
|
||||||
|
return $STARTUP_RC
|
||||||
|
}
|
||||||
|
####################################################
|
||||||
|
post_event_script ()
|
||||||
|
{
|
||||||
|
# Put anything that you want to execute AFTER the
|
||||||
|
# monitored process ENDS in this function
|
||||||
|
|
||||||
|
: # No-OP - Need as a placeholder for an empty function
|
||||||
|
# Comment Out the Above colon, ':'
|
||||||
|
|
||||||
|
POST_RC=$?
|
||||||
|
return $POST_RC
|
||||||
|
}
|
||||||
|
####################################################
|
||||||
|
# This function is used to test character strings
|
||||||
|
|
||||||
|
test_string ()
|
||||||
|
{
|
||||||
|
if (( $# != 1 ))
|
||||||
|
then
|
||||||
|
echo 'ERROR'
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
|
||||||
|
C_STRING=$1
|
||||||
|
|
||||||
|
# Test the character string for its composition
|
||||||
|
|
||||||
|
case $C_STRING in
|
||||||
|
|
||||||
|
+([0-9])) echo 'POS_INT' # Integer >= 0
|
||||||
|
;;
|
||||||
|
+([-0-9])) echo 'NEG_INT' # Integer < 0
|
||||||
|
;;
|
||||||
|
+([a-z])) echo 'LOW_CASE' # lower case text
|
||||||
|
;;
|
||||||
|
+([A-Z])) echo 'UP_CASE' # UPPER case text
|
||||||
|
;;
|
||||||
|
+([a-z]|[A-Z])) echo 'MIX_CASE' # MIxed CAse text
|
||||||
|
;;
|
||||||
|
*) echo 'UNKNOWN' # Anything else
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
####################################################
|
||||||
|
proc_watch ()
|
||||||
|
{
|
||||||
|
# set -x # Uncomment to debug this function
|
||||||
|
# This function does all of the process monitoring!
|
||||||
|
|
||||||
|
while : # Loop Forever!!
|
||||||
|
do
|
||||||
|
case $RUN in
|
||||||
|
'Y')
|
||||||
|
# This will run the startup_event_script, which is a function
|
||||||
|
if [[ $RUN_STARTUP_EVENT = 'Y' ]]
|
||||||
|
then
|
||||||
|
echo "STARTUP EVENT: Executing Startup Event Script..."\
|
||||||
|
> $TTY
|
||||||
|
echo "STARTUP EVENT: Executing Startup Event Script..."\
|
||||||
|
>> $LOGFILE
|
||||||
|
|
||||||
|
startup_event_script # USER DEFINED FUNCTION!!!
|
||||||
|
RC=$? # Check the Return Code!!
|
||||||
|
if (( "RC" == 0 ))
|
||||||
|
then
|
||||||
|
echo "SUCCESS: Startup Event Script Completed RC -\
|
||||||
|
${RC}" > $TTY
|
||||||
|
echo "SUCCESS: Startup Event Script Completed RC -\
|
||||||
|
${RC}" >> $LOGFILE
|
||||||
|
|
||||||
|
else
|
||||||
|
echo "FAILURE: Startup Event Script FAILED RC -\
|
||||||
|
${RC}" > $TTY
|
||||||
|
echo "FAILURE: Startup Event Script FAILED RC -\
|
||||||
|
${RC}" >> $LOGFILE
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
integer PROC_COUNT='-1' # Reset the Counters
|
||||||
|
integer LAST_COUNT='-1'
|
||||||
|
# Loop until the process(es) end(s)
|
||||||
|
|
||||||
|
until (( "PROC_COUNT" == 0 ))
|
||||||
|
do
|
||||||
|
# This function is a Co-Process. $BREAK checks to see if
|
||||||
|
# "Program Interrupt" has taken place. If so BREAK will
|
||||||
|
# be 'Y' and we exit both the loop and function.
|
||||||
|
|
||||||
|
read BREAK
|
||||||
|
if [[ $BREAK = 'Y' ]]
|
||||||
|
then
|
||||||
|
return 3
|
||||||
|
fi
|
||||||
|
|
||||||
|
PROC_COUNT=$(ps aux | grep -v "grep $PROCESS" \
|
||||||
|
| grep -v $SCRIPT_NAME \
|
||||||
|
| grep $PROCESS | wc -l) >/dev/null 2>&1
|
||||||
|
|
||||||
|
if (( "LAST_COUNT" > 0 && "LAST_COUNT" != "PROC_COUNT" ))
|
||||||
|
then
|
||||||
|
# The Process Count has Changed...
|
||||||
|
TIMESTAMP=$(date +%D@%T)
|
||||||
|
# Get a list of the PID of all of the processes
|
||||||
|
PID_LIST=$(ps aux | grep -v "grep $PROCESS" \
|
||||||
|
| grep -v $SCRIPT_NAME \
|
||||||
|
| grep $PROCESS | awk '{print $2}')
|
||||||
|
|
||||||
|
echo "PROCESS COUNT: $PROC_COUNT $PROCESS\
|
||||||
|
Processes Running ==> $TIMESTAMP" >> $LOGFILE &
|
||||||
|
echo "PROCESS COUNT: $PROC_COUNT $PROCESS\
|
||||||
|
Processes Running ==> $TIMESTAMP" > $TTY
|
||||||
|
echo ACTIVE PIDS: $PID_LIST >> $LOGFILE &
|
||||||
|
echo ACTIVE PIDS: $PID_LIST > $TTY
|
||||||
|
fi
|
||||||
|
LAST_COUNT=$PROC_COUNT
|
||||||
|
sleep $INTERVAL # Needed to reduce CPU load!
|
||||||
|
done
|
||||||
|
|
||||||
|
RUN='N' # Turn the RUN Flag Off
|
||||||
|
TIMESTAMP=$(date +%D@%T)
|
||||||
|
echo "ENDING PROCESS: $PROCESS END time ==>\
|
||||||
|
$TIMESTAMP" >> $LOGFILE &
|
||||||
|
echo "ENDING PROCESS: $PROCESS END time ==>\
|
||||||
|
$TIMESTAMP" > $TTY
|
||||||
|
|
||||||
|
# This will run the post_event_script, which is a function
|
||||||
|
|
||||||
|
if [[ $RUN_POST_EVENT = 'Y' ]]
|
||||||
|
then
|
||||||
|
echo "POST EVENT: Executing Post Event Script..."\
|
||||||
|
> $TTY
|
||||||
|
echo "POST EVENT: Executing Post Event Script..."\
|
||||||
|
>> $LOGFILE &
|
||||||
|
|
||||||
|
post_event_script # USER DEFINED FUNCTION!!!
|
||||||
|
integer RC=$?
|
||||||
|
if (( "RC" == 0 ))
|
||||||
|
then
|
||||||
|
echo "SUCCESS: Post Event Script Completed RC -\
|
||||||
|
${RC}" > $TTY
|
||||||
|
echo "SUCCESS: Post Event Script Completed RC -\
|
||||||
|
${RC}" >> $LOGFILE
|
||||||
|
else
|
||||||
|
echo "FAILURE: Post Event Script FAILED RC - ${RC}"\
|
||||||
|
> $TTY
|
||||||
|
echo "FAILURE: Post Event Script FAILED RC - ${RC}"\
|
||||||
|
>> $LOGFILE
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
|
||||||
|
'N')
|
||||||
|
# This will run the pre_event_script, which is a function
|
||||||
|
|
||||||
|
if [[ $RUN_PRE_EVENT = 'Y' ]]
|
||||||
|
then
|
||||||
|
echo "PRE EVENT: Executing Pre Event Script..." > $TTY
|
||||||
|
echo "PRE EVENT: Executing Pre Event Script..." >> $LOGFILE
|
||||||
|
|
||||||
|
pre_event_script # USER DEFINED FUNCTION!!!
|
||||||
|
RC=$? # Check the Return Code!!!
|
||||||
|
if (( "RC" == 0 ))
|
||||||
|
then
|
||||||
|
echo "SUCCESS: Pre Event Script Completed RC - ${RC}"\
|
||||||
|
> $TTY
|
||||||
|
echo "SUCCESS: Pre Event Script Completed RC - ${RC}"\
|
||||||
|
>> $LOGFILE
|
||||||
|
else
|
||||||
|
echo "FAILURE: Pre Event Script FAILED RC - ${RC}"\
|
||||||
|
> $TTY
|
||||||
|
echo "FAILURE: Pre Event Script FAILED RC - ${RC}"\
|
||||||
|
>> $LOGFILE
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "WAITING: Waiting for $PROCESS to \
|
||||||
|
startup...Monitoring..."
|
||||||
|
|
||||||
|
integer PROC_COUNT='-1' # Initialize to a fake value
|
||||||
|
|
||||||
|
# Loop until at least one process starts
|
||||||
|
|
||||||
|
until (( "PROC_COUNT" > 0 ))
|
||||||
|
do
|
||||||
|
# This is a Co-Process. This checks to see if a "Program
|
||||||
|
# Interrupt" has taken place. If so BREAK will be 'Y' and
|
||||||
|
# we exit both the loop and function
|
||||||
|
|
||||||
|
read BREAK
|
||||||
|
if [[ $BREAK = 'Y' ]]
|
||||||
|
then
|
||||||
|
return 3
|
||||||
|
fi
|
||||||
|
|
||||||
|
PROC_COUNT=$(ps aux | grep -v "grep $PROCESS" \
|
||||||
|
| grep -v $SCRIPT_NAME | grep $PROCESS | wc -l) \
|
||||||
|
>/dev/null 2>&1
|
||||||
|
|
||||||
|
sleep $INTERVAL # Needed to reduce CPU load!
|
||||||
|
done
|
||||||
|
|
||||||
|
RUN='Y' # Turn the RUN Flag On
|
||||||
|
|
||||||
|
TIMESTAMP=$(date +%D@%T)
|
||||||
|
|
||||||
|
PID_LIST=$(ps aux | grep -v "grep $PROCESS" \
|
||||||
|
| grep -v $SCRIPT_NAME \
|
||||||
|
| grep $PROCESS | awk '{print $2}')
|
||||||
|
|
||||||
|
if (( "PROC_COUNT" == 1 ))
|
||||||
|
then
|
||||||
|
echo "START PROCESS: $PROCESS START time ==>\
|
||||||
|
$TIMESTAMP" >> $LOGFILE &
|
||||||
|
echo ACTIVE PIDS: $PID_LIST >> $LOGFILE &
|
||||||
|
echo "START PROCESS: $PROCESS START time ==>\
|
||||||
|
$TIMESTAMP" > $TTY
|
||||||
|
echo ACTIVE PIDS: $PID_LIST > $TTY
|
||||||
|
elif (( "PROC_COUNT" > 1 ))
|
||||||
|
then
|
||||||
|
echo "START PROCESS: $PROC_COUNT $PROCESS\
|
||||||
|
Processes Started: START time ==> $TIMESTAMP" >> $LOGFILE &
|
||||||
|
echo ACTIVE PIDS: $PID_LIST >> $LOGFILE &
|
||||||
|
echo "START PROCESS: $PROC_COUNT $PROCESS\
|
||||||
|
Processes Started: START time ==> $TIMESTAMP" > $TTY
|
||||||
|
echo ACTIVE PIDS: $PID_LIST > $TTY
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################################
|
||||||
|
############## START OF MAIN #######################
|
||||||
|
####################################################
|
||||||
|
|
||||||
|
### SET A TRAP ####
|
||||||
|
|
||||||
|
trap 'BREAK='Y';print -p $BREAK 2>/dev/null;trap_exit\
|
||||||
|
2>/dev/null;exit 0' 1 2 3 15
|
||||||
|
|
||||||
|
BREAK='N' # The BREAK variable is used in the co-process proc_watch
|
||||||
|
PROCESS= # Initialize to null
|
||||||
|
integer TOTAL_SECONDS=0
|
||||||
|
|
||||||
|
# Check commnand line arguments
|
||||||
|
|
||||||
|
if (( $# > 10 || $# < 2 ))
|
||||||
|
then
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Check to see if only the seconds and a process are
|
||||||
|
# the only arguments
|
||||||
|
|
||||||
|
if [[ ($# -eq 2) && ($1 != -*) && ($2 != -*) ]]
|
||||||
|
then
|
||||||
|
NUM_TEST=$(test_string $1) # Is this an Integer?
|
||||||
|
if [[ "$NUM_TEST" = 'POS_INT' ]]
|
||||||
|
then
|
||||||
|
TOTAL_SECONDS=$1 # Yep - It.s an Integer
|
||||||
|
PROCESS=$2 # Can be anything
|
||||||
|
else
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
# Since getopts does not care what arguments it gets, let.s
|
||||||
|
# do a quick sanity check to make sure that we only have
|
||||||
|
# between 2 and 10 arguments and the first one must start
|
||||||
|
# with a -* (hyphen and anything), else usage error
|
||||||
|
|
||||||
|
case "$#" in
|
||||||
|
[2-10]) if [[ $1 != -* ]]; then
|
||||||
|
usage; exit 1
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
HOURS=0 # Initialize all to zero
|
||||||
|
MINUTES=0
|
||||||
|
SECS=0
|
||||||
|
DAYS=0
|
||||||
|
|
||||||
|
# Use getopts to parse the command line arguments
|
||||||
|
# For each $OPTARG for DAYS, HOURS, MINUTES and DAYS check to see
|
||||||
|
# that each one is an integer by using the check_string function
|
||||||
|
|
||||||
|
while getopts ":h:H:m:M:s:S:d:D:P:p:" OPT_LIST 2>/dev/null
|
||||||
|
do
|
||||||
|
case $OPT_LIST in
|
||||||
|
h|H) [[ $(test_string $OPTARG) != 'POS_INT' ]] && usage && exit 1
|
||||||
|
(( HOURS = $OPTARG * 3600 )) # 3600 seconds per hour
|
||||||
|
;;
|
||||||
|
m|H) [[ $(test_string $OPTARG) != 'POS_INT' ]] && usage && exit 1
|
||||||
|
(( MINUTES = $OPTARG * 60 )) # 60 seconds per minute
|
||||||
|
;;
|
||||||
|
s|S) [[ $(test_string $OPTARG) != 'POS_INT' ]] && usage && exit 1
|
||||||
|
SECS="$OPTARG" # seconds are seconds
|
||||||
|
;;
|
||||||
|
d|D) [[ $(test_string $OPTARG) != 'POS_INT' ]] && usage && exit 1
|
||||||
|
(( DAYS = $OPTARG * 86400 )) # 86400 seconds per day
|
||||||
|
;;
|
||||||
|
p|P) PROCESS=$OPTARG # process can be anything
|
||||||
|
;;
|
||||||
|
\?) usage # USAGE ERROR
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
:) usage
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
*) usage
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
fi
|
||||||
|
|
||||||
|
# We need to make sure that we have a process that
|
||||||
|
# is NOT null or empty! - sanity check - The double quotes are required!
|
||||||
|
|
||||||
|
if [[ -z "$PROCESS" || "$PROCESS" = '' ]]
|
||||||
|
then
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Check to see that TOTAL_SECONDS was not previously set
|
||||||
|
|
||||||
|
if (( TOTAL_SECONDS == 0 ))
|
||||||
|
then
|
||||||
|
# Add everything together if anything is > 0
|
||||||
|
if [[ $SECS -gt 0 || $MINUTES -gt 0 || $HOURS -gt 0 \
|
||||||
|
|| $DAYS -gt 0 ]]
|
||||||
|
then
|
||||||
|
(( TOTAL_SECONDS = SECS + MINUTES + HOURS + DAYS ))
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Last Sanity Check!
|
||||||
|
|
||||||
|
if (( TOTAL_SECONDS <= 0 )) || [ -z $PROCESS ]
|
||||||
|
then
|
||||||
|
# Either There are No Seconds to Count or the
|
||||||
|
# $PROCESS Variable is Null...USAGE ERROR...
|
||||||
|
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
########### START MONITORING HERE!###########
|
||||||
|
|
||||||
|
echo "\nCurrently running $PROCESS processes:\n" > $TTY
|
||||||
|
ps aux | grep -v "grep $PROCESS" | grep -v $SCRIPT_NAME \
|
||||||
|
| grep $PROCESS > $TTY
|
||||||
|
|
||||||
|
PROC_RC=$? # Get the initial state of the monitored function
|
||||||
|
|
||||||
|
echo >$TTY # Send a blank line to the screen
|
||||||
|
|
||||||
|
(( PROC_RC != 0 )) && echo "\nThere are no $PROCESS processes running\n"
|
||||||
|
|
||||||
|
if (( PROC_RC == 0 )) # The Target Process(es) is/are running...
|
||||||
|
then
|
||||||
|
RUN='Y' # Set the RUN flag to true, or yes.
|
||||||
|
|
||||||
|
integer PROC_COUNT # Strips out the "padding" for display
|
||||||
|
|
||||||
|
PROC_COUNT=$(ps aux | grep -v "grep $PROCESS" | grep -v \
|
||||||
|
$SCRIPT_NAME | grep $PROCESS | wc -l) >/dev/null 2>&1
|
||||||
|
if (( PROC_COUNT == 1 ))
|
||||||
|
then
|
||||||
|
echo "The $PROCESS process is currently\
|
||||||
|
running...Monitoring...\n"
|
||||||
|
elif (( PROC_COUNT > 1 ))
|
||||||
|
then
|
||||||
|
print "There are $PROC_COUNT $PROCESS processes currently\
|
||||||
|
running...Monitoring...\n"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo "The $PROCESS process is not currently running...monitoring..."
|
||||||
|
RUN='N' # Set the RUN flag to false, or no.
|
||||||
|
fi
|
||||||
|
|
||||||
|
TIMESTAMP=$(date +%D@%T) # Time that this script started monitoring
|
||||||
|
|
||||||
|
# Get a list of the currently active process IDs
|
||||||
|
|
||||||
|
PID_LIST=$(ps aux | grep -v "grep $PROCESS" \
|
||||||
|
| grep -v $SCRIPT_NAME \
|
||||||
|
| grep $PROCESS | awk '{print $2}')
|
||||||
|
|
||||||
|
echo "MON_STARTED: Monitoring for $PROCESS began ==> $TIMESTAMP" \
|
||||||
|
| tee -a $LOGFILE
|
||||||
|
echo ACTIVE PIDS: $PID_LIST | tee -a $LOGFILE
|
||||||
|
|
||||||
|
##### NOTICE ####
|
||||||
|
# We kick off the "proc_watch" function below as a "Co-Process"
|
||||||
|
# This sets up a two way communications link between the
|
||||||
|
# "proc_watch" background function and this "MAIN BODY" of
|
||||||
|
# the script. This is needed because the function has two
|
||||||
|
# "infinite loops", with one always executing at any given time.
|
||||||
|
# Therefore we need a way to break out of the loop in case of
|
||||||
|
# an interrupt, i.e. CTRL+C, and when the countdown is complete.
|
||||||
|
# The "pipe appersand", |&, creates the background Co-Process
|
||||||
|
# and we use "print -p $VARIABLE" to transfer the variable.s
|
||||||
|
# value back to the background co-process.
|
||||||
|
###################################
|
||||||
|
|
||||||
|
proc_watch |& # Create a Background Co-Process!!
|
||||||
|
WATCH_PID=$! # Get the process ID of the last background job!
|
||||||
|
|
||||||
|
# Start the Count Down!
|
||||||
|
|
||||||
|
integer SECONDS_LEFT=$TOTAL_SECONDS
|
||||||
|
|
||||||
|
while (( SECONDS_LEFT > 0 ))
|
||||||
|
do
|
||||||
|
# Next send the current value of $BREAK to the Co-Process
|
||||||
|
# proc_watch, which was piped to the background...
|
||||||
|
|
||||||
|
print -p $BREAK 2>/dev/null
|
||||||
|
(( SECONDS_LEFT = SECONDS_LEFT - 1 ))
|
||||||
|
sleep 1 # 1 Second Between Counts
|
||||||
|
done
|
||||||
|
|
||||||
|
# Finished - Normal Timeout Exit...
|
||||||
|
TIMESTAMP=$(date +%D@%T) # Get a new timestamp...
|
||||||
|
echo "MON_STOPPED: Monitoring for $PROCESS ended ==> $TIMESTAMP\n" \
|
||||||
|
| tee -a $LOGFILE
|
||||||
|
|
||||||
|
echo "LOGFILE: All Events are Logged ==> $LOGFILE \n"
|
||||||
|
|
||||||
|
# Tell the proc_watch function to break out of the loop and die
|
||||||
|
BREAK='Y'
|
||||||
|
print -p $BREAK 2>/dev/null
|
||||||
|
|
||||||
|
kill $WATCH_PID 2>/dev/null
|
||||||
|
|
||||||
|
exit 0
|
||||||
|
|
||||||
|
# End of Script
|
||||||
53
chapter17/fs_mon_AIX.ksh
Executable file
53
chapter17/fs_mon_AIX.ksh
Executable file
@@ -0,0 +1,53 @@
|
|||||||
|
#!/usr/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: fs_mon_AIX.ksh
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 08-22-2007
|
||||||
|
# REV: 1.1.P
|
||||||
|
# PURPOSE: This script is used to monitor for full filesystems,
|
||||||
|
# which is defined as "exceeding" the FSMAX value.
|
||||||
|
# A message is displayed for all "full" filesystems.
|
||||||
|
#
|
||||||
|
# PLATFORM: AIX
|
||||||
|
#
|
||||||
|
# REV LIST:
|
||||||
|
#
|
||||||
|
# set -n # Uncomment to check syntax without any execution
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
#
|
||||||
|
##### DEFINE FILES AND VARIABLES HERE ####
|
||||||
|
|
||||||
|
FSMAX="85" # Max. FS percentage value
|
||||||
|
|
||||||
|
WORKFILE="/tmp/df.work" # Holds filesystem data
|
||||||
|
>$WORKFILE # Initialize to empty
|
||||||
|
OUTFILE="/tmp/df.outfile" # Output display file
|
||||||
|
>$OUTFILE # Initialize to empty
|
||||||
|
THISHOST=`hostname` # Hostname of this machine
|
||||||
|
|
||||||
|
######## START OF MAIN #############
|
||||||
|
|
||||||
|
# Get the data of interest by stripping out /dev/cd#,
|
||||||
|
# /proc rows and keeping columns 1, 4 and 7
|
||||||
|
|
||||||
|
df -k | tail +2 | egrep -v '/dev/cd[0-9]|/proc' \
|
||||||
|
| awk '{print $1, $4, $7}' > $WORKFILE
|
||||||
|
|
||||||
|
# Loop through each line of the file and compare column 2
|
||||||
|
|
||||||
|
while read FSDEVICE FSVALUE FSMOUNT
|
||||||
|
do
|
||||||
|
FSVALUE=$(echo "$FSVALUE" | sed s/\%//g) # Remove the % sign
|
||||||
|
if [ "$FSVALUE" -gt "$FSMAX" ]
|
||||||
|
then
|
||||||
|
echo "$FSDEVICE mounted on $FSMOUNT is ${FSVALUE}%" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
done < $WORKFILE
|
||||||
|
|
||||||
|
if [[ -s $OUTFILE ]]
|
||||||
|
then
|
||||||
|
echo "\nFull Filesystem(s) on $THISHOST\n"
|
||||||
|
cat $OUTFILE
|
||||||
|
print
|
||||||
|
fi
|
||||||
61
chapter17/fs_mon_AIX_MBFREE.ksh
Executable file
61
chapter17/fs_mon_AIX_MBFREE.ksh
Executable file
@@ -0,0 +1,61 @@
|
|||||||
|
#!/usr/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: fs_mon_AIX_MBFREE.ksh
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 08-22-2007
|
||||||
|
# REV: 1.5.P
|
||||||
|
# PURPOSE: This script is used to monitor for full filesystems,
|
||||||
|
# which is defined as "exceeding" the FSMAX value.
|
||||||
|
# A message is displayed for all "full" filesystems.
|
||||||
|
#
|
||||||
|
# PLATFORM: AIX
|
||||||
|
#
|
||||||
|
# REV LIST:
|
||||||
|
# Randy Michael - 08-27-2007
|
||||||
|
# Changed the code to use MB of free space instead of
|
||||||
|
# the %Used method.
|
||||||
|
#
|
||||||
|
# set -n # Uncomment to check syntax without any execution
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
#
|
||||||
|
##### DEFINE FILES AND VARIABLES HERE ####
|
||||||
|
|
||||||
|
MIN_MB_FREE="50MB" # Min. MB of Free FS Space
|
||||||
|
|
||||||
|
WORKFILE="/tmp/df.work" # Holds filesystem data
|
||||||
|
>$WORKFILE # Initialize to empty
|
||||||
|
OUTFILE="/tmp/df.outfile" # Output display file
|
||||||
|
>$OUTFILE # Initialize to empty
|
||||||
|
THISHOST=`hostname` # Hostname of this machine
|
||||||
|
|
||||||
|
######## START OF MAIN #############
|
||||||
|
|
||||||
|
# Get the data of interest by stripping out /dev/cd#,
|
||||||
|
# /proc rows and keeping columns 1, 4 and 7
|
||||||
|
|
||||||
|
df -k | tail +2 | egrep -v '/dev/cd[0-9]|/proc' \
|
||||||
|
| awk '{print $1, $3, $7}' > $WORKFILE
|
||||||
|
|
||||||
|
# Format Variables
|
||||||
|
(( MIN_MB_FREE = $(echo $MIN_MB_FREE | sed s/MB//g) * 1024 ))
|
||||||
|
|
||||||
|
# Loop through each line of the file and compare column 2
|
||||||
|
|
||||||
|
while read FSDEVICE FSMB_FREE FSMOUNT
|
||||||
|
do
|
||||||
|
FSMB_FREE=$(echo $FSMB_FREE | sed s/MB//g) # Remove the "MB"
|
||||||
|
if (( $FSMB_FREE < $MIN_MB_FREE ))
|
||||||
|
then
|
||||||
|
(( FS_FREE_OUT = $FSMB_FREE / 1000 ))
|
||||||
|
echo "$FSDEVICE mounted on $FSMOUNT has \
|
||||||
|
${FS_FREE_OUT}MB Free" >> $OUTFILE
|
||||||
|
fi
|
||||||
|
|
||||||
|
done < $WORKFILE # Feed the while loop from the bottom!!
|
||||||
|
|
||||||
|
if [[ -s $OUTFILE ]]
|
||||||
|
then
|
||||||
|
echo "\nFull Filesystem(s) on $THISHOST\n"
|
||||||
|
cat $OUTFILE
|
||||||
|
print
|
||||||
|
fi
|
||||||
131
chapter17/fs_mon_AIX_MBFREE_excep.ksh
Executable file
131
chapter17/fs_mon_AIX_MBFREE_excep.ksh
Executable file
@@ -0,0 +1,131 @@
|
|||||||
|
#!/usr/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: fs_mon_AIX_MBFREE_excep.ksh
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 08-22-2007
|
||||||
|
# REV: 2.1.P
|
||||||
|
# PURPOSE: This script is used to monitor for full filesystems,
|
||||||
|
# which is defined as "exceeding" the FSMAX value.
|
||||||
|
# A message is displayed for all "full" filesystems.
|
||||||
|
#
|
||||||
|
# PLATFORM: AIX
|
||||||
|
#
|
||||||
|
# REV LIST:
|
||||||
|
# Randy Michael - 08-27-2007
|
||||||
|
# Changed the code to use MB of free space instead of
|
||||||
|
# the %Used method.
|
||||||
|
#
|
||||||
|
# Randy Michael - 08-27-2007
|
||||||
|
# Added code to allow you to override the set script default
|
||||||
|
# for MIN_MB_FREE of FS Space
|
||||||
|
#
|
||||||
|
# set -n # Uncomment to check syntax without any execution
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
#
|
||||||
|
##### DEFINE FILES AND VARIABLES HERE ####
|
||||||
|
|
||||||
|
MIN_MB_FREE="50MB" # Min. MB of Free FS Space
|
||||||
|
|
||||||
|
WORKFILE="/tmp/df.work" # Holds filesystem data
|
||||||
|
>$WORKFILE # Initialize to empty
|
||||||
|
OUTFILE="/tmp/df.outfile" # Output display file
|
||||||
|
>$OUTFILE # Initialize to empty
|
||||||
|
EXCEPTIONS="/usr/local/bin/exceptions" # Override data file
|
||||||
|
DATA_EXCEPTIONS="/tmp/dfdata.out" # Exceptions file w/o # rows
|
||||||
|
THISHOST=`hostname` # Hostname of this machine
|
||||||
|
|
||||||
|
####### DEFINE FUNCTIONS HERE ########
|
||||||
|
|
||||||
|
function check_exceptions
|
||||||
|
{
|
||||||
|
# set -x # Uncomment to debug this function
|
||||||
|
|
||||||
|
while read FSNAME FSLIMIT
|
||||||
|
do
|
||||||
|
# Do an NFS sanity check
|
||||||
|
echo $FSNAME | grep ":" >/dev/null \
|
||||||
|
&& FSNAME=$(echo $FSNAME | cut -d ":" -f2)
|
||||||
|
if [[ ! -z "$FSLIMIT" && "$FSLIMIT" != '' ]] # Check for empty/null
|
||||||
|
then
|
||||||
|
(( FSLIMIT = $(echo $FSLIMIT | sed s/MB//g) * 1024 ))
|
||||||
|
if [[ $FSNAME = $FSMOUNT ]]
|
||||||
|
then
|
||||||
|
# Get rid of the "MB" if it exists
|
||||||
|
FSLIMIT=$(echo $FSLIMIT | sed s/MB//g)
|
||||||
|
if (( $FSMB_FREE < $FSLIMIT ))
|
||||||
|
then
|
||||||
|
return 1 # Found out of limit
|
||||||
|
else
|
||||||
|
return 2 # Found OK
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done < $DATA_EXCEPTIONS # Feed the loop from the bottom!!!
|
||||||
|
|
||||||
|
return 3 # Not found in $EXCEPTIONS file
|
||||||
|
}
|
||||||
|
|
||||||
|
######## START OF MAIN #############
|
||||||
|
|
||||||
|
# Load the $EXCEPTIONS file if it exists
|
||||||
|
|
||||||
|
if [[ -s $EXCEPTIONS ]]
|
||||||
|
then
|
||||||
|
# Ignore all lines beginning with a pound sign, #
|
||||||
|
# and omit all blank lines
|
||||||
|
cat $EXCEPTIONS | grep -v "^#" | sed /^$/d > $DATA_EXCEPTIONS
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Get the data of interest by stripping out /dev/cd#,
|
||||||
|
# /proc rows and keeping columns 1, 4 and 7
|
||||||
|
|
||||||
|
df -k | tail +2 | egrep -v '/dev/cd[0-9]|/proc' \
|
||||||
|
| awk '{print $1, $3, $7}' > $WORKFILE
|
||||||
|
|
||||||
|
# Format Variables for the proper MB value
|
||||||
|
(( MIN_MB_FREE = $(echo $MIN_MB_FREE | sed s/MB//g) * 1024 ))
|
||||||
|
|
||||||
|
# Loop through each line of the file and compare column 2
|
||||||
|
|
||||||
|
while read FSDEVICE FSMB_FREE FSMOUNT
|
||||||
|
do
|
||||||
|
if [[ -s $EXCEPTIONS ]]
|
||||||
|
then
|
||||||
|
check_exceptions
|
||||||
|
RC="$?"
|
||||||
|
if (( RC == 1 )) # Found out of exceptions limit
|
||||||
|
then
|
||||||
|
(( FS_FREE_OUT = $FSMB_FREE / 1000 ))
|
||||||
|
echo "$FSDEVICE mounted on $FSMOUNT only has ${FS_FREE_OUT}MB Free" >> $OUTFILE
|
||||||
|
elif (( $RC == 2 )) # Found in exceptions to be OK
|
||||||
|
then # Just a sanity check - We really do nothing here...
|
||||||
|
# The colon, :, is a NO-OP operator in KSH
|
||||||
|
|
||||||
|
: # No-Op - Do Nothing!
|
||||||
|
|
||||||
|
elif [ $RC -eq 3 ] # Not found in the exceptions file
|
||||||
|
then
|
||||||
|
FSMB_FREE=$(echo $FSMB_FREE | sed s/MB//g) # Remove the "MB"
|
||||||
|
if (( $FSMB_FREE < $MIN_MB_FREE ))
|
||||||
|
then
|
||||||
|
(( FS_FREE_OUT = $FSMB_FREE / 1000 ))
|
||||||
|
echo "$FSDEVICE mounted on $FSMOUNT only has ${FS_FREE_OUT}MB Free" >> $OUTFILE
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
else # No Exceptions file use the script default
|
||||||
|
FSMB_FREE=$(echo $FSMB_FREE | sed s/MB//g) # Remove the "MB"
|
||||||
|
if (( $FSMB_FREE < $MIN_MB_FREE ))
|
||||||
|
then
|
||||||
|
(( FS_FREE_OUT = FSMB_FREE / 1000 ))
|
||||||
|
echo "$FSDEVICE mounted on $FSMOUNT only has ${FS_FREE_OUT}MB Free" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done < $WORKFILE
|
||||||
|
|
||||||
|
if [[ -s $OUTFILE ]]
|
||||||
|
then
|
||||||
|
echo "\nFull Filesystem(s) on $THISHOST\n"
|
||||||
|
cat $OUTFILE
|
||||||
|
print
|
||||||
|
fi
|
||||||
280
chapter17/fs_mon_AIX_PC_MBFREE.ksh
Executable file
280
chapter17/fs_mon_AIX_PC_MBFREE.ksh
Executable file
@@ -0,0 +1,280 @@
|
|||||||
|
#!/usr/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: fs_mon_AIX_PC_MBFREE.ksh
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 08-22-2007
|
||||||
|
# REV: 4.3.P
|
||||||
|
# PURPOSE: This script is used to monitor for full filesystems,
|
||||||
|
# which is defined as "exceeding" the MAX_PERCENT value.
|
||||||
|
# A message is displayed for all "full" filesystems.
|
||||||
|
#
|
||||||
|
# PLATFORM: AIX
|
||||||
|
#
|
||||||
|
# REV LIST:
|
||||||
|
# Randy Michael - 08-27-2007
|
||||||
|
# Changed the code to use MB of free space instead of
|
||||||
|
# the %Used method.
|
||||||
|
#
|
||||||
|
# Randy Michael - 08-27-2007
|
||||||
|
# Added code to allow you to override the set script default
|
||||||
|
# for MIN_MB_FREE of FS Space
|
||||||
|
#
|
||||||
|
# Randy Michael - 08-28-2007
|
||||||
|
# Changed the code to handle both %Used and MB of Free Space.
|
||||||
|
# It does an "auto-detection" but has override capability
|
||||||
|
# of both the trigger level and the monitoring method using
|
||||||
|
# the exceptions file pointed to by the $EXCEPTIONS variable
|
||||||
|
#
|
||||||
|
# set -n # Uncomment to check syntax without any execution
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
#
|
||||||
|
##### DEFINE FILES AND VARIABLES HERE ####
|
||||||
|
|
||||||
|
WORKFILE="/tmp/df.work" # Holds filesystem data
|
||||||
|
>$WORKFILE # Initialize to empty
|
||||||
|
OUTFILE="/tmp/df.outfile" # Output display file
|
||||||
|
>$OUTFILE # Initialize to empty
|
||||||
|
EXCEPTIONS="/usr/local/bin/exceptions" # Override data file
|
||||||
|
DATA_EXCEPTIONS="/tmp/dfdata.out" # Exceptions file w/o # rows
|
||||||
|
EXCEPT_FILE="N" # Assume no $EXCEPTIONS FILE
|
||||||
|
THISHOST=`hostname` # Hostname of this machine
|
||||||
|
|
||||||
|
MIN_MB_FREE="100MB" # Min. MB of Free FS Space
|
||||||
|
MAX_PERCENT="85%" # Max. FS percentage value
|
||||||
|
FSTRIGGER="1000MB" # Trigger to switch from % Used to MB Free
|
||||||
|
|
||||||
|
|
||||||
|
###### FORMAT VARIABLES HERE ######
|
||||||
|
|
||||||
|
# Both of these variables need to multiplied by 1024 blocks
|
||||||
|
(( MIN_MB_FREE = $(echo $MIN_MB_FREE | sed s/MB//g) * 1024 ))
|
||||||
|
(( FSTRIGGER = $(echo $FSTRIGGER | sed s/MB//g) * 1024 ))
|
||||||
|
|
||||||
|
####### DEFINE FUNCTIONS HERE ########
|
||||||
|
|
||||||
|
function check_exceptions
|
||||||
|
{
|
||||||
|
# set -x # Uncomment to debug this function
|
||||||
|
|
||||||
|
while read FSNAME FSLIMIT
|
||||||
|
do
|
||||||
|
IN_FILE="N"
|
||||||
|
|
||||||
|
# Do an NFS sanity check and get rid of any ":".
|
||||||
|
# If this is found it is actaully an error entry
|
||||||
|
# but we will try to resolve it. It will only
|
||||||
|
# work if it is an NFS cross mount to the same
|
||||||
|
# mount point on both machines.
|
||||||
|
echo $FSNAME | grep ':' >/dev/null \
|
||||||
|
&& FSNAME=$(echo $FSNAME | cut -d ':' -f2)
|
||||||
|
|
||||||
|
# Check for empty and null variable
|
||||||
|
if [[ ! -z $FSLIMIT && $FSLIMIT != '' ]]
|
||||||
|
then
|
||||||
|
if [[ $FSNAME = $FSMOUNT ]] # Found it!
|
||||||
|
then
|
||||||
|
# Check for "MB" Characters...Set IN_FILE=MB
|
||||||
|
echo $FSLIMIT | grep MB >/dev/null && IN_FILE="MB" \
|
||||||
|
&& (( FSLIMIT = $(echo $FSLIMIT \
|
||||||
|
| sed s/MB//g) * 1024 ))
|
||||||
|
# check for "%" Character...Set IN_FILE=PC, for %
|
||||||
|
echo $FSLIMIT | grep "%" >/dev/null && IN_FILE="PC" \
|
||||||
|
&& FSLIMIT=$(echo $FSLIMIT | sed s/\%//g)
|
||||||
|
|
||||||
|
case $IN_FILE in
|
||||||
|
MB)
|
||||||
|
# Up-case the characters, if they exist
|
||||||
|
FSLIMIT=$(echo $FSLIMIT | tr '[a-z]' '[A-Z]')
|
||||||
|
# Get rid of the "MB" if it exists
|
||||||
|
FSLIMIT=$(echo $FSLIMIT | sed s/MB//g)
|
||||||
|
# Test for blank and null values
|
||||||
|
if [[ ! -z $FSLIMIT && $FSLIMIT != '' ]]
|
||||||
|
then
|
||||||
|
# Test for a valid filesystem "MB" limit
|
||||||
|
if (( $FSLIMIT >= 0 && $FSLIMIT < $FSSIZE ))
|
||||||
|
then
|
||||||
|
if (( $FSMB_FREE < $FSLIMIT ))
|
||||||
|
then
|
||||||
|
return 1 # Found out of limit using MB Free method
|
||||||
|
else
|
||||||
|
return 3 # Found OK
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo "\nERROR: Invalid filesystem MAX for $FSMOUNT - $FSLIMIT"
|
||||||
|
echo " Exceptions file value must be less than or"
|
||||||
|
echo " equal to the size of the filesystem measured"
|
||||||
|
echo " in 1024 bytes\n"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo "\nERROR: Null value specified in excepeptions file"
|
||||||
|
echo " for the $FSMOUNT mount point.\n"
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
PC)
|
||||||
|
# Strip out the % sign if it exists
|
||||||
|
PC_USED=$(echo $PC_USED | sed s/\%//g)
|
||||||
|
# Test for blank and null values
|
||||||
|
if [[ ! -z $FSLIMIT && $FSLIMIT != '' ]]
|
||||||
|
then
|
||||||
|
# Test for a valid percentage, i.e. 0-100
|
||||||
|
if (( $FSLIMIT >= 0 && $FSLIMIT <= 100 ))
|
||||||
|
then
|
||||||
|
if (( $PC_USED > $FSLIMIT ))
|
||||||
|
then
|
||||||
|
return 2 # Found exceeded by % Used method
|
||||||
|
else
|
||||||
|
return 3 # Found OK
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo "\nERROR: Invalid percentage for $FSMOUNT - $FSLIMIT"
|
||||||
|
echo " Exceptions file values must be"
|
||||||
|
echo " between 0 and 100%\n"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo "\nERROR: Null value specified in excepeptions file"
|
||||||
|
echo " for the $FSMOUNT mount point.\n"
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
N)
|
||||||
|
# Method Not Specified - Use Script Defaults
|
||||||
|
if (( $FSSIZE >= $FSTRIGGER ))
|
||||||
|
then # This is a "large" filesystem
|
||||||
|
if (( $FSMB_FREE < $MIN_MB_FREE ))
|
||||||
|
then
|
||||||
|
return 1 # Found out of limit using MB Free method
|
||||||
|
else
|
||||||
|
return 3 # Found OK
|
||||||
|
fi
|
||||||
|
else # This is a standard filesystem
|
||||||
|
PC_USED=$(echo $PC_USED | sed s/\%//g) # Remove the %
|
||||||
|
FSLIMIT=$(echo $FSLIMIT | sed s/\%//g) # Remove the %
|
||||||
|
if (( $PC_USED > $FSLIMIT ))
|
||||||
|
then
|
||||||
|
return 2 # Found exceeded by % Used method
|
||||||
|
else
|
||||||
|
return 3 # Found OK
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done < $DATA_EXCEPTIONS # Feed the loop from the bottom!!!
|
||||||
|
|
||||||
|
return 4 # Not found in $EXCEPTIONS file
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################
|
||||||
|
|
||||||
|
function display_output
|
||||||
|
{
|
||||||
|
if [[ -s $OUTFILE ]]
|
||||||
|
then
|
||||||
|
echo "\nFull Filesystem(s) on $THISHOST\n"
|
||||||
|
cat $OUTFILE
|
||||||
|
print
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################
|
||||||
|
|
||||||
|
function load_EXCEPTIONS_data
|
||||||
|
{
|
||||||
|
# Ingore any line that begins with a pound sign, #
|
||||||
|
|
||||||
|
cat $EXCEPTIONS | grep -v "^#" > $DATA_EXCEPTIONS
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################
|
||||||
|
|
||||||
|
function load_FS_data
|
||||||
|
{
|
||||||
|
df -k | tail +2 | egrep -v '/dev/cd[0-9]|/proc' \
|
||||||
|
| awk '{print $1, $2, $3, $4, $7}' > $WORKFILE
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################
|
||||||
|
######### START OF MAIN ############
|
||||||
|
####################################
|
||||||
|
|
||||||
|
load_FS_data
|
||||||
|
|
||||||
|
# Do we have a non-zero size $EXCEPTIONS file?
|
||||||
|
|
||||||
|
if [[ -s $EXCEPTIONS ]]
|
||||||
|
then # Found a non-empty $EXCEPTIONS file
|
||||||
|
|
||||||
|
load_EXCEPTIONS_data
|
||||||
|
EXCEP_FILE="Y"
|
||||||
|
fi
|
||||||
|
|
||||||
|
while read FSDEVICE FSSIZE FSMB_FREE PC_USED FSMOUNT
|
||||||
|
do
|
||||||
|
if [[ $EXCEP_FILE = "Y" ]]
|
||||||
|
then
|
||||||
|
check_exceptions
|
||||||
|
CE_RC="$?" # Check Exceptions Return Code (CE_RC)
|
||||||
|
|
||||||
|
case $CE_RC in
|
||||||
|
1) # Found exceeded in exceptions file by MB Method
|
||||||
|
(( FS_FREE_OUT = $FSMB_FREE / 1000 ))
|
||||||
|
echo "$FSDEVICE mounted on $FSMOUNT has ${FS_FREE_OUT}MB Free" \
|
||||||
|
>> $OUTFILE
|
||||||
|
;;
|
||||||
|
2) # Found exceeded in exceptions file by %Used method
|
||||||
|
echo "$FSDEVICE mount on $FSMOUNT is ${PC_USED}%" \
|
||||||
|
>> $OUTFILE
|
||||||
|
;;
|
||||||
|
3) # Found OK in exceptions file
|
||||||
|
: # NO-OP Do Nothing
|
||||||
|
;;
|
||||||
|
|
||||||
|
4) # Not found in exceptions file - Use Default Triggers
|
||||||
|
if (( $FSSIZE >= $FSTRIGGER ))
|
||||||
|
then # This is a "large" filesystem
|
||||||
|
FSMB_FREE=$(echo $FSMB_FREE | sed s/MB//g) # Remove the "MB"
|
||||||
|
if (( $FSMB_FREE < $MIN_MB_FREE ))
|
||||||
|
then
|
||||||
|
(( FS_FREE_OUT = $FSMB_FREE / 1000 ))
|
||||||
|
echo "$FSDEVICE mounted on $FSMOUNT has ${FS_FREE_OUT}MB Free" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
else # This is a standard filesystem
|
||||||
|
PC_USED=$(echo $PC_USED | sed s/\%//g)
|
||||||
|
MAX_PERCENT=$(echo $MAX_PERCENT | sed s/\%//g)
|
||||||
|
if (( $PC_USED > $MAX_PERCENT ))
|
||||||
|
then
|
||||||
|
echo "$FSDEVICE mount on $FSMOUNT is ${PC_USED}%" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
|
||||||
|
else # NO $EXECPTIONS FILE USE DEFAULT TRIGGER VALUES
|
||||||
|
|
||||||
|
if (( $FSSIZE >= $FSTRIGGER ))
|
||||||
|
then # This is a "large" filesystem - Use MB Free Method
|
||||||
|
FSMB_FREE=$(echo $FSMB_FREE | sed s/MB//g) # Remove the "MB"
|
||||||
|
if (( $FSMB_FREE < $MIN_MB_FREE ))
|
||||||
|
then
|
||||||
|
(( FS_FREE_OUT = $FSMB_FREE / 1000 ))
|
||||||
|
echo "$FSDEVICE mounted on $FSMOUNT has ${FS_FREE_OUT}MB Free" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
else # This is a standard filesystem - Use % Used Method
|
||||||
|
PC_USED=$(echo $PC_USED | sed s/\%//g)
|
||||||
|
MAX_PERCENT=$(echo $MAX_PERCENT | sed s/\%//g)
|
||||||
|
if (( $PC_USED > $MAX_PERCENT ))
|
||||||
|
then
|
||||||
|
echo "$FSDEVICE mount on $FSMOUNT is ${PC_USED}%" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done < $WORKFILE
|
||||||
|
|
||||||
|
display_output
|
||||||
|
|
||||||
|
# End of Script
|
||||||
287
chapter17/fs_mon_AIX_PC_MBFREE_excep.ksh
Executable file
287
chapter17/fs_mon_AIX_PC_MBFREE_excep.ksh
Executable file
@@ -0,0 +1,287 @@
|
|||||||
|
#!/usr/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: fs_mon_AIX_PC_MBFREE_excep.ksh`
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 08-22-2007
|
||||||
|
# REV: 4.3.P
|
||||||
|
# PURPOSE: This script is used to monitor for full filesystems,
|
||||||
|
# which is defined as "exceeding" the MAX_PERCENT value.
|
||||||
|
# A message is displayed for all "full" filesystems.
|
||||||
|
#
|
||||||
|
# REV LIST:
|
||||||
|
# Randy Michael - 08-27-2007
|
||||||
|
# Changed the code to use MB of free space instead of
|
||||||
|
# the %Used method.
|
||||||
|
#
|
||||||
|
# Randy Michael - 08-27-2007
|
||||||
|
# Added code to allow you to override the set script default
|
||||||
|
# for MIN_MB_FREE of FS Space
|
||||||
|
#
|
||||||
|
# Randy Michael - 08-28-2007
|
||||||
|
# Changed the code to handle both %Used and MB of Free Space.
|
||||||
|
# It does an "auto-detection" but has override capability
|
||||||
|
# of both the trigger level and the monitoring method using
|
||||||
|
# the exceptions file pointed to by the $EXCEPTIONS variable
|
||||||
|
#
|
||||||
|
# set -n # Uncomment to check syntax without any execution
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
#
|
||||||
|
##### DEFINE FILES AND VARIABLES HERE ####
|
||||||
|
|
||||||
|
MIN_MB_FREE="100MB" # Min. MB of Free FS Space
|
||||||
|
MAX_PERCENT="85%" # Max. FS percentage value
|
||||||
|
FSTRIGGER="1000MB" # Trigger to switch from % Used to MB Free
|
||||||
|
|
||||||
|
WORKFILE="/tmp/df.work" # Holds filesystem data
|
||||||
|
>$WORKFILE # Initialize to empty
|
||||||
|
OUTFILE="/tmp/df.outfile" # Output display file
|
||||||
|
>$OUTFILE # Initialize to empty
|
||||||
|
EXCEPTIONS="/usr/local/bin/exceptions" # Override data file
|
||||||
|
DATA_EXCEPTIONS="/tmp/dfdata.out" # Exceptions file w/o # rows
|
||||||
|
>$DATA_EXCEPTIONS
|
||||||
|
EXCEPT_FILE="N" # Assume no $EXCEPTIONS FILE
|
||||||
|
THISHOST=`hostname` # Hostname of this machine
|
||||||
|
|
||||||
|
###### FORMAT VARIABLES HERE ######
|
||||||
|
|
||||||
|
# Both of these variables need to multiplied by 1024 blocks
|
||||||
|
(( MIN_MB_FREE = $(echo $MIN_MB_FREE | sed s/MB//g) * 1024 ))
|
||||||
|
(( FSTRIGGER = $(echo $FSTRIGGER | sed s/MB//g) * 1024 ))
|
||||||
|
|
||||||
|
####### DEFINE FUNCTIONS HERE ########
|
||||||
|
|
||||||
|
function check_exceptions
|
||||||
|
{
|
||||||
|
# set -x # Uncomment to debug this function
|
||||||
|
|
||||||
|
while read FSNAME FSLIMIT
|
||||||
|
do
|
||||||
|
IN_FILE="N"
|
||||||
|
|
||||||
|
# Do an NFS sanity check and get rid of any ":".
|
||||||
|
# If this is found it is actaully an error entry
|
||||||
|
# but we will try to resolve it. It will only
|
||||||
|
# work if it is an NFS cross mount to the same
|
||||||
|
# mount point on both machines.
|
||||||
|
echo $FSNAME | grep ':' >/dev/null \
|
||||||
|
&& FSNAME=$(echo $FSNAME | cut -d ':' -f2)
|
||||||
|
|
||||||
|
# Check for empty and null variable
|
||||||
|
if [[ ! -z $FSLIMIT && $FSLIMIT != '' ]]
|
||||||
|
then
|
||||||
|
if [[ $FSNAME = $FSMOUNT ]] # Found it!
|
||||||
|
then
|
||||||
|
# Check for "MB" Characters...Set IN_FILE=MB
|
||||||
|
echo $FSLIMIT | grep MB >/dev/null && IN_FILE="MB" \
|
||||||
|
&& (( FSLIMIT = $(echo $FSLIMIT \
|
||||||
|
| sed s/MB//g) * 1024 ))
|
||||||
|
# check for "%" Character...Set IN_FILE=PC, for %
|
||||||
|
echo $FSLIMIT | grep "%" >/dev/null && IN_FILE="PC" \
|
||||||
|
&& FSLIMIT=$(echo $FSLIMIT | sed s/\%//g)
|
||||||
|
|
||||||
|
case $IN_FILE in
|
||||||
|
MB) # Use Megabytes of free space to test
|
||||||
|
# Up-case the characters, if they exist
|
||||||
|
FSLIMIT=$(echo $FSLIMIT | tr '[a-z]' '[A-Z]')
|
||||||
|
# Get rid of the "MB" if it exists
|
||||||
|
FSLIMIT=$(echo $FSLIMIT | sed s/MB//g)
|
||||||
|
# Test for blank and null values
|
||||||
|
if [[ ! -z $FSLIMIT && $FSLIMIT != '' ]]
|
||||||
|
then
|
||||||
|
# Test for a valid filesystem "MB" limit
|
||||||
|
if (( $FSLIMIT >= 0 && $FSLIMIT < $FSSIZE ))
|
||||||
|
then
|
||||||
|
if (( $FSMB_FREE < $FSLIMIT ))
|
||||||
|
then
|
||||||
|
return 1 # Found out of limit
|
||||||
|
# using MB Free method
|
||||||
|
else
|
||||||
|
return 3 # Found OK
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo "\nERROR: Invalid filesystem MAX for $FSMOUNT - $FSLIMIT"
|
||||||
|
echo " Exceptions file value must be less than or"
|
||||||
|
echo " equal to the size of the filesystem measured"
|
||||||
|
echo " in 1024 bytes\n"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo "\nERROR: Null value specified in excepeptions file"
|
||||||
|
echo " for the $FSMOUNT mount point.\n"
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
PC)
|
||||||
|
# Strip out the % sign if it exists
|
||||||
|
PC_USED=$(echo $PC_USED | sed s/\%//g)
|
||||||
|
# Test for blank and null values
|
||||||
|
if [[ ! -z $FSLIMIT && $FSLIMIT != '' ]]
|
||||||
|
then
|
||||||
|
# Test for a valid percentage, i.e. 0-100
|
||||||
|
if (( $FSLIMIT >= 0 && $FSLIMIT <= 100 ))
|
||||||
|
then
|
||||||
|
if (( $PC_USED > $FSLIMIT ))
|
||||||
|
then
|
||||||
|
return 2 # Found exceeded by % Used method
|
||||||
|
else
|
||||||
|
return 3 # Found OK
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo "\nERROR: Invalid percentage for $FSMOUNT - $FSLIMIT"
|
||||||
|
echo " Exceptions file values must be"
|
||||||
|
echo " between 0 and 100%\n"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo "\nERROR: Null value specified in excepeptions file"
|
||||||
|
echo " for the $FSMOUNT mount point.\n"
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
N) # Test type not specified in exception file, use default
|
||||||
|
|
||||||
|
# Inform the user of the exceptions file error...
|
||||||
|
echo "\nERROR: Missing testing type in exceptions file"
|
||||||
|
echo " for the $FSMOUNT mount point. A \"%\" or"
|
||||||
|
echo " \"MB\" must be a suffix to the numerical"
|
||||||
|
echo " entry. Using script default values...\n"
|
||||||
|
|
||||||
|
# Method Not Specified - Use Script Defaults
|
||||||
|
if (( $FSSIZE >= $FSTRIGGER ))
|
||||||
|
then # This is a "large" filesystem
|
||||||
|
if (( $FSMB_FREE < $MIN_MB_FREE ))
|
||||||
|
then
|
||||||
|
return 1 # Found out of limit using MB Free method
|
||||||
|
else
|
||||||
|
return 3 # Found OK
|
||||||
|
fi
|
||||||
|
else # This is a standard filesystem
|
||||||
|
PC_USED=$(echo $PC_USED | sed s/\%//g) # Remove the %
|
||||||
|
FSLIMIT=$(echo $FSLIMIT | sed s/\%//g) # Remove the %
|
||||||
|
if (( $PC_USED > $FSLIMIT ))
|
||||||
|
then
|
||||||
|
return 2 # Found exceeded by % Used method
|
||||||
|
else
|
||||||
|
return 3 # Found OK
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done < $DATA_EXCEPTIONS # Feed the loop from the bottom!!!
|
||||||
|
|
||||||
|
return 4 # Not found in $EXCEPTIONS file
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################
|
||||||
|
|
||||||
|
function display_output
|
||||||
|
{
|
||||||
|
if [[ -s $OUTFILE ]]
|
||||||
|
then
|
||||||
|
echo "\nFull Filesystem(s) on $THISHOST\n"
|
||||||
|
cat $OUTFILE
|
||||||
|
print
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################
|
||||||
|
|
||||||
|
function load_EXCEPTIONS_data
|
||||||
|
{
|
||||||
|
# Ingore any line that begins with a pound sign, #
|
||||||
|
# and omit all blank lines
|
||||||
|
|
||||||
|
cat $EXCEPTIONS | grep -v "^#" | sed /^$/d > $DATA_EXCEPTIONS
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################
|
||||||
|
|
||||||
|
function load_FS_data
|
||||||
|
{
|
||||||
|
df -k | tail +2 | egrep -v '/dev/cd[0-9] | /proc' \
|
||||||
|
| awk '{print $1, $2, $3, $4, $7}' > $WORKFILE
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################
|
||||||
|
######### START OF MAIN ############
|
||||||
|
####################################
|
||||||
|
|
||||||
|
load_FS_data
|
||||||
|
|
||||||
|
# Do we have a non-zero size $EXCEPTIONS file?
|
||||||
|
|
||||||
|
if [[ -s $EXCEPTIONS ]]
|
||||||
|
then # Found a non-empty $EXCEPTIONS file
|
||||||
|
|
||||||
|
load_EXCEPTIONS_data
|
||||||
|
EXCEP_FILE="Y"
|
||||||
|
fi
|
||||||
|
|
||||||
|
while read FSDEVICE FSSIZE FSMB_FREE PC_USED FSMOUNT
|
||||||
|
do
|
||||||
|
if [[ $EXCEP_FILE = "Y" ]]
|
||||||
|
then
|
||||||
|
check_exceptions
|
||||||
|
CE_RC="$?" # Check Exceptions Return Code (CE_RC)
|
||||||
|
|
||||||
|
case $CE_RC in
|
||||||
|
1) # Found exceeded in exceptions file by MB Method
|
||||||
|
(( FS_FREE_OUT = $FSMB_FREE / 1000 ))
|
||||||
|
echo "$FSDEVICE mounted on $FSMOUNT has ${FS_FREE_OUT}MB Free" \
|
||||||
|
>> $OUTFILE
|
||||||
|
;;
|
||||||
|
2) # Found exceeded in exceptions file by %Used method
|
||||||
|
echo "$FSDEVICE mount on $FSMOUNT is ${PC_USED}%" \
|
||||||
|
>> $OUTFILE
|
||||||
|
;;
|
||||||
|
3) # Found OK in exceptions file
|
||||||
|
: # NO-OP Do Nothing
|
||||||
|
;;
|
||||||
|
4) # Not found in exceptions file - Use Default Triggers
|
||||||
|
if (( $FSSIZE >= $FSTRIGGER ))
|
||||||
|
then # This is a "large" filesystem
|
||||||
|
# Remove the "MB", if it exists
|
||||||
|
FSMB_FREE=$(echo $FSMB_FREE | sed s/MB//g)
|
||||||
|
typeset -i FSMB_FREE
|
||||||
|
if (( $FSMB_FREE < $MIN_MB_FREE ))
|
||||||
|
then
|
||||||
|
(( FS_FREE_OUT = $FSMB_FREE / 1000 ))
|
||||||
|
echo "$FSDEVICE mounted on $FSMOUNT has ${FS_FREE_OUT}MB Free" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
else # This is a standard filesystem
|
||||||
|
PC_USED=$(echo $PC_USED | sed s/\%//g)
|
||||||
|
MAX_PERCENT=$(echo $MAX_PERCENT | sed s/\%//g)
|
||||||
|
if (( $PC_USED > $MAX_PERCENT ))
|
||||||
|
then
|
||||||
|
echo "$FSDEVICE mount on $FSMOUNT is ${PC_USED}%" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
else # NO $EXECPTIONS FILE USE DEFAULT TRIGGER VALUES
|
||||||
|
|
||||||
|
if (( $FSSIZE >= $FSTRIGGER ))
|
||||||
|
then # This is a "large" filesystem - Use MB Free Method
|
||||||
|
FSMB_FREE=$(echo $FSMB_FREE | sed s/MB//g) # Remove the "MB"
|
||||||
|
if (( $FSMB_FREE < $MIN_MB_FREE ))
|
||||||
|
then
|
||||||
|
(( FS_FREE_OUT = $FSMB_FREE / 1000 ))
|
||||||
|
echo "$FSDEVICE mounted on $FSMOUNT has ${FS_FREE_OUT}MB Free" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
else # This is a standard filesystem - Use % Used Method
|
||||||
|
PC_USED=$(echo $PC_USED | sed s/\%//g)
|
||||||
|
MAX_PERCENT=$(echo $MAX_PERCENT | sed s/\%//g)
|
||||||
|
if (( $PC_USED > $MAX_PERCENT ))
|
||||||
|
then
|
||||||
|
echo "$FSDEVICE mount on $FSMOUNT is ${PC_USED}%" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done < $WORKFILE
|
||||||
|
|
||||||
|
display_output
|
||||||
|
|
||||||
|
# End of Script
|
||||||
127
chapter17/fs_mon_AIX_excep.ksh
Executable file
127
chapter17/fs_mon_AIX_excep.ksh
Executable file
@@ -0,0 +1,127 @@
|
|||||||
|
#!/usr/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: fs_mon_AIX_excep.ksh
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 08-22-2007
|
||||||
|
# REV: 2.1.P
|
||||||
|
#
|
||||||
|
# PURPOSE: This script is used to monitor for full filesystems,
|
||||||
|
# which is defined as "exceeding" the FSMAX value.
|
||||||
|
# A message is displayed for all "full" filesystems.
|
||||||
|
#
|
||||||
|
# PLATFORM: AIX
|
||||||
|
#
|
||||||
|
# REV LIST:
|
||||||
|
# 08-23-2007 - Randy Michael
|
||||||
|
# Added code to override the default FSMAX script threshold
|
||||||
|
# using an "exceptions" file, defined by the $EXCEPTIONS
|
||||||
|
# variable, that list /mount_point and NEW_MAX%
|
||||||
|
#
|
||||||
|
# set -n # Uncomment to check syntax without any execution
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
#
|
||||||
|
##### DEFINE FILES AND VARIABLES HERE ####
|
||||||
|
|
||||||
|
FSMAX="85" # Max. FS percentage value
|
||||||
|
|
||||||
|
WORKFILE="/tmp/df.work" # Holds filesystem data
|
||||||
|
>$WORKFILE # Initialize to empty
|
||||||
|
OUTFILE="/tmp/df.outfile" # Output display file
|
||||||
|
>$OUTFILE # Initialize to empty
|
||||||
|
BINDIR="/usr/local/bin" # Local bin directory
|
||||||
|
THISHOST=`hostname` # Hostname of this machine
|
||||||
|
|
||||||
|
EXCEPTIONS="${BINDIR}/exceptions" # Overrides $FSMAX
|
||||||
|
DATA_EXCEPTIONS="/tmp/dfdata.out" # Exceptions file w/o #, comments
|
||||||
|
|
||||||
|
####### DEFINE FUNCTIONS HERE #####
|
||||||
|
|
||||||
|
function load_EXCEPTIONS_file
|
||||||
|
{
|
||||||
|
# Ignore any line that begins with a pound sign, #
|
||||||
|
# and omit all blank lines
|
||||||
|
|
||||||
|
cat $EXCEPTIONS | grep -v "^#" | sed /^$/d > $DATA_EXCEPTIONS
|
||||||
|
}
|
||||||
|
|
||||||
|
###################################
|
||||||
|
|
||||||
|
function check_exceptions
|
||||||
|
{
|
||||||
|
# set -x # Uncomment to debug this function
|
||||||
|
|
||||||
|
while read FSNAME NEW_MAX # Feeding Ddata from Bottom of Loop!!!
|
||||||
|
do
|
||||||
|
if [[ $FSNAME = $FSMOUNT ]] # Correct /mount_point?
|
||||||
|
then # Get rid of the % sign, if it exists!
|
||||||
|
NEW_MAX=$(echo $NEW_MAX | sed s/\%//g)
|
||||||
|
|
||||||
|
if [ $FSVALUE -gt $NEW_MAX ]
|
||||||
|
then # Over Limit...Return a "0", zero
|
||||||
|
return 0 # FOUND MAX OUT - Return 0
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
done < $DATA_EXCEPTIONS # Feed from the bottom of the loop!!
|
||||||
|
|
||||||
|
return 1 # Not found in File
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################
|
||||||
|
######## START OF MAIN #############
|
||||||
|
####################################
|
||||||
|
|
||||||
|
# If there is an exceptions file...load it...
|
||||||
|
|
||||||
|
[[ -s $EXCEPTIONS ]] && load_EXCEPTIONS_file
|
||||||
|
|
||||||
|
# Get the data of interest by stripping out /dev/cd#,
|
||||||
|
# /proc rows and keeping columns 1, 4 and 7
|
||||||
|
|
||||||
|
df -k | tail +2 | egrep -v '/dev/cd[0-9]|/proc' \
|
||||||
|
| awk '{print $1, $4, $7}' > $WORKFILE
|
||||||
|
|
||||||
|
# Loop through each line of the file and compare column 2
|
||||||
|
|
||||||
|
while read FSDEVICE FSVALUE FSMOUNT
|
||||||
|
do # Feeding the while loop from the BOTTOM!!
|
||||||
|
|
||||||
|
# Strip out the % sign if it exists
|
||||||
|
FSVALUE=$(echo $FSVALUE | sed s/\%//g) # Remove the % sign
|
||||||
|
if [[ -s $EXCEPTIONS ]] # Do we have a non-empty file?
|
||||||
|
then # Found it!
|
||||||
|
|
||||||
|
# Look for the current $FSMOUNT value in the file
|
||||||
|
# using the check_exceptions function defined above.
|
||||||
|
|
||||||
|
check_exceptions
|
||||||
|
RC=$? # Get the return code from the function
|
||||||
|
if [ $RC -eq 0 ] # Found Exceeded in Exceptions File!!
|
||||||
|
then
|
||||||
|
echo "$FSDEVICE mount on $FSMOUNT is ${FSVALUE}%" \
|
||||||
|
>> $OUTFILE
|
||||||
|
elif [ $RC -eq 1 ] # Not found in exceptions, use defaults
|
||||||
|
then
|
||||||
|
if [ $FSVALUE -gt $FSMAX ] # Use Script Default
|
||||||
|
then
|
||||||
|
echo "$FSDEVICE mount on $FSMOUNT is ${FSVALUE}%" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
else # No exceptions file use the script default
|
||||||
|
if [ $FSVALUE -gt $FSMAX ] # Use Script Default
|
||||||
|
then
|
||||||
|
echo "$FSDEVICE mount on $FSMOUNT is ${FSVALUE}%" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done < $WORKFILE # Feed the while loop from the bottom...
|
||||||
|
|
||||||
|
# Display output if anything is exceeded...
|
||||||
|
|
||||||
|
if [[ -s $OUTFILE ]]
|
||||||
|
then
|
||||||
|
echo "\nFull Filesystem(s) on $THISHOST\n"
|
||||||
|
cat $OUTFILE
|
||||||
|
print
|
||||||
|
fi
|
||||||
381
chapter17/fs_mon_ALL_OS.ksh
Executable file
381
chapter17/fs_mon_ALL_OS.ksh
Executable file
@@ -0,0 +1,381 @@
|
|||||||
|
#!/usr/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: fs_mon_ALL_OS.ksh
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 09-25-2007
|
||||||
|
# REV: 6.0.P
|
||||||
|
#
|
||||||
|
# PURPOSE: This script is used to monitor for full filesystems,
|
||||||
|
# which are defined as .exceeding. the MAX_PERCENT value.
|
||||||
|
# A message is displayed for all .full. filesystems.
|
||||||
|
#
|
||||||
|
# PLATFORM: AIX, Linux, HP-UX, OpenBSD, and Solaris
|
||||||
|
#
|
||||||
|
# REV LIST:
|
||||||
|
# Randy Michael - 08-27-2007
|
||||||
|
# Changed the code to use MB of free space instead of
|
||||||
|
# the %Used method.
|
||||||
|
#
|
||||||
|
# Randy Michael - 08-27-2007
|
||||||
|
# Added code to allow you to override the set script default
|
||||||
|
# for MIN_MB_FREE of FS Space
|
||||||
|
#
|
||||||
|
# Randy Michael - 08-28-2007
|
||||||
|
# Changed the code to handle both %Used and MB of Free Space.
|
||||||
|
# It does an .auto-detection. but has override capability
|
||||||
|
# of both the trigger level and the monitoring method using
|
||||||
|
# the exceptions file pointed to by the $EXCEPTIONS variable
|
||||||
|
#
|
||||||
|
# Randy Michael - 08-28-2007
|
||||||
|
# Added code to allow this script to be executed on
|
||||||
|
# AIX, Linux, HP-UX, and Solaris
|
||||||
|
#
|
||||||
|
# Randy Michael - 09=25=2007
|
||||||
|
# Added code for OpenBSD support
|
||||||
|
#
|
||||||
|
# set -n # Uncomment to check syntax without any execution
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
#
|
||||||
|
##### DEFINE FILES AND VARIABLES HERE ####
|
||||||
|
|
||||||
|
MIN_MB_FREE="100MB" # Min. MB of Free FS Space
|
||||||
|
MAX_PERCENT="85%" # Max. FS percentage value
|
||||||
|
FSTRIGGER="1000MB" # Trigger to switch from % Used to MB Free
|
||||||
|
|
||||||
|
WORKFILE="/tmp/df.work" # Holds filesystem data
|
||||||
|
>$WORKFILE # Initialize to empty
|
||||||
|
OUTFILE="/tmp/df.outfile" # Output display file
|
||||||
|
>$OUTFILE # Initialize to empty
|
||||||
|
EXCEPTIONS="/usr/local/bin/exceptions" # Override data file
|
||||||
|
DATA_EXCEPTIONS="/tmp/dfdata.out" # Exceptions file w/o # rows
|
||||||
|
EXCEPT_FILE="N" # Assume no $EXCEPTIONS FILE
|
||||||
|
THISHOST=`hostname` # Hostname of this machine
|
||||||
|
|
||||||
|
###### FORMAT VARIABLES HERE ######
|
||||||
|
|
||||||
|
# Both of these variables need to be multiplied by 1024 blocks
|
||||||
|
(( MIN_MB_FREE = $(echo $MIN_MB_FREE | sed s/MB//g) * 1024 ))
|
||||||
|
(( FSTRIGGER = $(echo $FSTRIGGER | sed s/MB//g) * 1024 ))
|
||||||
|
|
||||||
|
###### SHELL SPECIFIC VARIABLES ######
|
||||||
|
|
||||||
|
# Set the correct usage for the echo command for found $SHELL
|
||||||
|
|
||||||
|
case $SHELL in
|
||||||
|
*/bin/bash) ECHO="echo -e"
|
||||||
|
;;
|
||||||
|
*/bin/ksh) ECHO="echo"
|
||||||
|
;;
|
||||||
|
*/bin/sh) ECHO="echo -e"
|
||||||
|
;;
|
||||||
|
*) ECHO="echo"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
######################################
|
||||||
|
####### DEFINE FUNCTIONS HERE ########
|
||||||
|
######################################
|
||||||
|
|
||||||
|
function get_OS_info
|
||||||
|
{
|
||||||
|
# For a few commands it is necessary to know the OS and its level
|
||||||
|
# to execute the proper command syntax. This will always return
|
||||||
|
# the OS in UPPERCASE
|
||||||
|
|
||||||
|
typeset -u OS # Use the UPPERCASE values for the OS variable
|
||||||
|
OS=`uname` # Grab the Operating system, i.e. AIX, HP-UX
|
||||||
|
print $OS # Send back the UPPERCASE value
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################
|
||||||
|
|
||||||
|
function check_exceptions
|
||||||
|
{
|
||||||
|
# set -x # Uncomment to debug this function
|
||||||
|
|
||||||
|
while read FSNAME FSLIMIT
|
||||||
|
do
|
||||||
|
IN_FILE="N"
|
||||||
|
|
||||||
|
# Do an NFS sanity check and get rid of any .:..
|
||||||
|
# If this is found it is actually an error entry
|
||||||
|
# but we will try to resolve it. It will only
|
||||||
|
# work if it is an NFS cross mount to the same
|
||||||
|
# mount point on both machines.
|
||||||
|
$ECHO $FSNAME | grep ':' >/dev/null \
|
||||||
|
&& FSNAME=$($ECHO $FSNAME | cut -d ':' -f2)
|
||||||
|
|
||||||
|
# Check for empty and null variable
|
||||||
|
if [[ ! -z $FSLIMIT && $FSLIMIT != '' ]]
|
||||||
|
then
|
||||||
|
if [[ $FSNAME = $FSMOUNT ]] # Found it!
|
||||||
|
then
|
||||||
|
# Check for "MB" Characters...Set IN_FILE=MB
|
||||||
|
$ECHO $FSLIMIT | grep MB >/dev/null && IN_FILE="MB" \
|
||||||
|
&& (( FSLIMIT = $($ECHO $FSLIMIT \
|
||||||
|
| sed s/MB//g) * 1024 ))
|
||||||
|
# check for '%' Character...Set IN_FILE=PC, for %
|
||||||
|
$ECHO $FSLIMIT | grep '%' >/dev/null && IN_FILE="PC" \
|
||||||
|
&& FSLIMIT=$($ECHO $FSLIMIT | sed s/\%//g)
|
||||||
|
|
||||||
|
case $IN_FILE in
|
||||||
|
MB) # Use MB of Free Space Method
|
||||||
|
# Up-case the characters, if they exist
|
||||||
|
FSLIMIT=$($ECHO $FSLIMIT | tr '[a-z]' '[A-Z]')
|
||||||
|
# Get rid of the 'MB' if it exists
|
||||||
|
FSLIMIT=$($ECHO $FSLIMIT | sed s/MB//g)
|
||||||
|
# Test for blank and null values
|
||||||
|
if [[ ! -z $FSLIMIT && $FSLIMIT != '' ]]
|
||||||
|
then
|
||||||
|
# Test for a valid filesystem 'MB' limit
|
||||||
|
if (( FSLIMIT >= 0 && FSLIMIT < FSSIZE ))
|
||||||
|
then
|
||||||
|
if (( FSMB_FREE < FSLIMIT ))
|
||||||
|
then
|
||||||
|
return 1 # Found out of limit
|
||||||
|
# using MB Free method
|
||||||
|
else
|
||||||
|
return 3 # Found OK
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
$ECHO "\nERROR: Invalid filesystem MAX for\
|
||||||
|
$FSMOUNT - $FSLIMIT"
|
||||||
|
$ECHO " Exceptions file value must be less\
|
||||||
|
than or"
|
||||||
|
$ECHO " equal to the size of the filesystem\
|
||||||
|
measured"
|
||||||
|
$ECHO " in 1024 bytes\n"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
$ECHO "\nERROR: Null value specified in exceptions\
|
||||||
|
file"
|
||||||
|
$ECHO " for the $FSMOUNT mount point.\n"
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
PC) # Use Filesystem %Used Method
|
||||||
|
# Strip out the % sign if it exists
|
||||||
|
PC_USED=$($ECHO $PC_USED | sed s/\%//g)
|
||||||
|
# Test for blank and null values
|
||||||
|
if [[ ! -z "$FSLIMIT" && "$FSLIMIT" != '' ]]
|
||||||
|
then
|
||||||
|
# Test for a valid percentage, i.e. 0-100
|
||||||
|
if (( FSLIMIT >= 0 && FSLIMIT <= 100 ))
|
||||||
|
then
|
||||||
|
if (( $PC_USED > $FSLIMIT ))
|
||||||
|
then
|
||||||
|
return 2 # Found exceeded by % Used method
|
||||||
|
else
|
||||||
|
return 3 # Found OK
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
$ECHO "\nERROR: Invalid percentage for $FSMOUNT -\
|
||||||
|
$FSLIMIT"
|
||||||
|
$ECHO " Exceptions file values must be"
|
||||||
|
$ECHO " between 0 and 100%\n"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
$ECHO "\nERROR: Null value specified in exceptions\
|
||||||
|
file"
|
||||||
|
$ECHO " for the $FSMOUNT mount point.\n"
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
N) # Method Not Specified - Use Script Defaults
|
||||||
|
if (( FSSIZE >= FSTRIGGER ))
|
||||||
|
then # This is a "large" filesystem
|
||||||
|
if (( FSMB_FREE < MIN_MB_FREE ))
|
||||||
|
then
|
||||||
|
return 1 # Found out of limit
|
||||||
|
# using MB Free method
|
||||||
|
else
|
||||||
|
return 3 # Found OK
|
||||||
|
fi
|
||||||
|
else # This is a standard filesystem
|
||||||
|
PC_USED=$($ECHO $PC_USED | sed s/\%//g) # Remove %
|
||||||
|
FSLIMIT=$($ECHO $FSLIMIT | sed s/\%//g) # Remove %
|
||||||
|
if (( PC_USED > FSLIMIT ))
|
||||||
|
then
|
||||||
|
return 2 # Found exceeded by % Used method
|
||||||
|
else
|
||||||
|
return 3 # Found OK
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done < $DATA_EXCEPTIONS # Feed the loop from the bottom!!!
|
||||||
|
|
||||||
|
return 4 # Not found in $EXCEPTIONS file
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################
|
||||||
|
|
||||||
|
function display_output
|
||||||
|
{
|
||||||
|
if [[ -s $OUTFILE ]]
|
||||||
|
then
|
||||||
|
$ECHO "\nFull Filesystem(s) on $THISHOST\n"
|
||||||
|
cat $OUTFILE
|
||||||
|
print
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################
|
||||||
|
|
||||||
|
function load_EXCEPTIONS_data
|
||||||
|
{
|
||||||
|
# Ignore any line that begins with a pound sign, #
|
||||||
|
# and omit all blank lines
|
||||||
|
|
||||||
|
cat $EXCEPTIONS | grep -v "^#" | sed /^$/d > $DATA_EXCEPTIONS
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################
|
||||||
|
|
||||||
|
function load_AIX_FS_data
|
||||||
|
{
|
||||||
|
|
||||||
|
df -k | tail +2 | egrep -v "/dev/cd[0-9]|/proc" \
|
||||||
|
| awk '{print $1, $2, $3, $4, $7}' > $WORKFILE
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################
|
||||||
|
|
||||||
|
function load_HP_UX_FS_data
|
||||||
|
{
|
||||||
|
|
||||||
|
bdf | tail +2 | egrep -v "/cdrom" \
|
||||||
|
| awk '{print $1, $2, $4, $5, $6}' > $WORKFILE
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################
|
||||||
|
|
||||||
|
function load_LINUX_FS_data
|
||||||
|
{
|
||||||
|
|
||||||
|
df -k | tail -n +2 | egrep -v "/cdrom"\
|
||||||
|
| awk '{print $1, $2, $4, $5, $6}' > $WORKFILE
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################
|
||||||
|
|
||||||
|
function load_OpenBSD_FS_data
|
||||||
|
{
|
||||||
|
df -k | tail +2 | egrep -v "/mnt/cdrom"\
|
||||||
|
| awk '{print $1, $2, $4, $5, $6}' > $WORKFILE
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################
|
||||||
|
|
||||||
|
function load_Solaris_FS_data
|
||||||
|
{
|
||||||
|
|
||||||
|
df -k | tail +2 | egrep -v "/dev/fd|/etc/mnttab|/proc"\
|
||||||
|
| awk '{print $1, $2, $4, $5, $6}' > $WORKFILE
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################
|
||||||
|
######### START OF MAIN ############
|
||||||
|
####################################
|
||||||
|
|
||||||
|
# Query the operating system to find the Unix flavor, then
|
||||||
|
# load the correct filesystem data for the resident OS
|
||||||
|
|
||||||
|
case $(get_OS_info) in
|
||||||
|
AIX) # Load filesystem data for AIX
|
||||||
|
load_AIX_FS_data
|
||||||
|
;;
|
||||||
|
HP-UX) # Load filesystem data for HP-UX
|
||||||
|
load_HP_UX_FS_data
|
||||||
|
;;
|
||||||
|
LINUX) # Load filesystem data for Linux
|
||||||
|
load_LINUX_FS_data
|
||||||
|
;;
|
||||||
|
OPENBSD) # Load filesystem data for OpenBSD
|
||||||
|
load_OpenBSD_FS_data
|
||||||
|
;;
|
||||||
|
SUNOS) # Load filesystem data for Solaris
|
||||||
|
load_Solaris_FS_data
|
||||||
|
;;
|
||||||
|
*) # Unsupported in script
|
||||||
|
$ECHO "\nUnsupported Operating System for this Script...EXITING\n"
|
||||||
|
exit 1
|
||||||
|
esac
|
||||||
|
|
||||||
|
# Do we have a nonzero size $EXCEPTIONS file?
|
||||||
|
|
||||||
|
if [[ -s $EXCEPTIONS ]]
|
||||||
|
then # Found a nonempty $EXCEPTIONS file
|
||||||
|
|
||||||
|
load_EXCEPTIONS_data
|
||||||
|
EXCEP_FILE="Y"
|
||||||
|
fi
|
||||||
|
|
||||||
|
while read FSDEVICE FSSIZE FSMB_FREE PC_USED FSMOUNT
|
||||||
|
do
|
||||||
|
if [[ $EXCEP_FILE = "Y" ]]
|
||||||
|
then
|
||||||
|
check_exceptions
|
||||||
|
CE_RC="$?" # Check Exceptions Return Code (CE_RC)
|
||||||
|
|
||||||
|
case $CE_RC in
|
||||||
|
1) # Found exceeded in exceptions file by MB Method
|
||||||
|
(( FS_FREE_OUT = FSMB_FREE / 1000 ))
|
||||||
|
$ECHO "$FSDEVICE mounted on $FSMOUNT has ${FS_FREE_OUT}MB Free" >> $OUTFILE
|
||||||
|
;;
|
||||||
|
2) # Found exceeded in exceptions file by %Used method
|
||||||
|
$ECHO "$FSDEVICE mount on $FSMOUNT is ${PC_USED}%" \
|
||||||
|
>> $OUTFILE
|
||||||
|
;;
|
||||||
|
3) # Found OK in exceptions file
|
||||||
|
: # NO-OP Do Nothing. A ':' is a no-op!
|
||||||
|
;;
|
||||||
|
|
||||||
|
4) # Not found in exceptions file - Use Default Triggers
|
||||||
|
if (( FSSIZE >= FSTRIGGER ))
|
||||||
|
then # This is a "large" filesystem
|
||||||
|
FSMB_FREE=$($ECHO $FSMB_FREE | sed s/MB//g) # Remove the "MB"
|
||||||
|
if (( FSMB_FREE < MIN_MB_FREE ))
|
||||||
|
then
|
||||||
|
(( FS_FREE_OUT = FSMB_FREE / 1000 ))
|
||||||
|
$ECHO "$FSDEVICE mounted on $FSMOUNT has {FS_FREE_OUT}MB Free" >> $OUTFILE
|
||||||
|
fi
|
||||||
|
else # This is a standard filesystem
|
||||||
|
PC_USED=$($ECHO $PC_USED | sed s/\%//g)
|
||||||
|
MAX_PERCENT=$($ECHO $MAX_PERCENT | sed s/\%//g)
|
||||||
|
if (( PC_USED > MAX_PERCENT ))
|
||||||
|
then
|
||||||
|
$ECHO "$FSDEVICE mount on $FSMOUNT is ${PC_USED}%" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
else # NO $EXCEPTIONS FILE USE DEFAULT TRIGGER VALUES
|
||||||
|
|
||||||
|
if (( FSSIZE >= FSTRIGGER ))
|
||||||
|
then # This is a "large" filesystem - Use MB Free Method
|
||||||
|
FSMB_FREE=$($ECHO $FSMB_FREE | sed s/MB//g) # Remove the 'MB'
|
||||||
|
if (( FSMB_FREE < MIN_MB_FREE ))
|
||||||
|
then
|
||||||
|
(( FS_FREE_OUT = FSMB_FREE / 1000 ))
|
||||||
|
$ECHO "$FSDEVICE mounted on $FSMOUNT has ${FS_FREE_OUT}MB Free" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
else # This is a standard filesystem - Use % Used Method
|
||||||
|
PC_USED=$($ECHO $PC_USED | sed s/\%//g)
|
||||||
|
MAX_PERCENT=$($ECHO $MAX_PERCENT | sed s/\%//g)
|
||||||
|
if (( PC_USED > MAX_PERCENT ))
|
||||||
|
then
|
||||||
|
$ECHO "$FSDEVICE mount on $FSMOUNT is ${PC_USED}%" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done < $WORKFILE # Feed the while loop from the bottom!!!!!
|
||||||
|
|
||||||
|
display_output
|
||||||
|
|
||||||
|
# End of Script
|
||||||
|
|
||||||
278
chapter17/fs_mon_COMBO.ksh
Executable file
278
chapter17/fs_mon_COMBO.ksh
Executable file
@@ -0,0 +1,278 @@
|
|||||||
|
#!/usr/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: fs_mon.ksh
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 08-22-2007
|
||||||
|
# REV: 4.3.P
|
||||||
|
# PURPOSE: This script is used to monitor for full filesystems,
|
||||||
|
# which is defined as "exceeding" the MAX_PERCENT value.
|
||||||
|
# A message is displayed for all "full" filesystems.
|
||||||
|
#
|
||||||
|
# REV LIST:
|
||||||
|
# Randy Michael - 08-27-2007
|
||||||
|
# Changed the code to use MB of free space instead of
|
||||||
|
# the %Used method.
|
||||||
|
#
|
||||||
|
# Randy Michael - 08-27-2007
|
||||||
|
# Added code to allow you to override the set script default
|
||||||
|
# for MIN_MB_FREE of FS Space
|
||||||
|
#
|
||||||
|
# Randy Michael - 08-28-2007
|
||||||
|
# Changed the code to handle both %Used and MB of Free Space.
|
||||||
|
# It does an "auto-detection" but has override capability
|
||||||
|
# of both the trigger level and the monitoring method using
|
||||||
|
# the exceptions file pointed to by the $EXCEPTIONS variable
|
||||||
|
#
|
||||||
|
# set -n # Uncomment to check syntax without any execution
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
#
|
||||||
|
##### DEFINE FILES AND VARIABLES HERE ####
|
||||||
|
|
||||||
|
WORKFILE="/tmp/df.work" # Holds filesystem data
|
||||||
|
>$WORKFILE # Initialize to empty
|
||||||
|
OUTFILE="/tmp/df.outfile" # Output display file
|
||||||
|
>$OUTFILE # Initialize to empty
|
||||||
|
EXCEPTIONS="/usr/local/bin/exceptions" # Override data file
|
||||||
|
DATA_EXCEPTIONS="/tmp/dfdata.out" # Exceptions file w/o # rows
|
||||||
|
EXCEPT_FILE="N" # Assume no $EXCEPTIONS FILE
|
||||||
|
THISHOST=`hostname` # Hostname of this machine
|
||||||
|
|
||||||
|
MIN_MB_FREE="100MB" # Min. MB of Free FS Space
|
||||||
|
MAX_PERCENT="85%" # Max. FS percentage value
|
||||||
|
FSTRIGGER="1000MB" # Trigger to switch from % Used to MB Free
|
||||||
|
|
||||||
|
|
||||||
|
###### FORMAT VARIABLES HERE ######
|
||||||
|
|
||||||
|
# Both of these variables need to multiplied by 1024 blocks
|
||||||
|
(( MIN_MB_FREE = $(echo $MIN_MB_FREE | sed s/MB//g) * 1024 ))
|
||||||
|
(( FSTRIGGER = $(echo $FSTRIGGER | sed s/MB//g) * 1024 ))
|
||||||
|
|
||||||
|
####### DEFINE FUNCTIONS HERE ########
|
||||||
|
|
||||||
|
function check_exceptions
|
||||||
|
{
|
||||||
|
# set -x # Uncomment to debug this function
|
||||||
|
|
||||||
|
while read FSNAME FSLIMIT
|
||||||
|
do
|
||||||
|
IN_FILE="N"
|
||||||
|
|
||||||
|
# Do an NFS sanity check and get rid of any ":".
|
||||||
|
# If this is found it is actaully an error entry
|
||||||
|
# but we will try to resolve it. It will only
|
||||||
|
# work if it is an NFS cross mount to the same
|
||||||
|
# mount point on both machines.
|
||||||
|
echo $FSNAME | grep ':' >/dev/null \
|
||||||
|
&& FSNAME=$(echo $FSNAME | cut -d ':' -f2)
|
||||||
|
|
||||||
|
# Check for empty and null variable
|
||||||
|
if [[ ! -z $FSLIMIT && $FSLIMIT != '' ]]
|
||||||
|
then
|
||||||
|
if [[ $FSNAME = $FSMOUNT ]] # Found it!
|
||||||
|
then
|
||||||
|
# Check for "MB" Characters...Set IN_FILE=MB
|
||||||
|
echo $FSLIMIT | grep MB >/dev/null && IN_FILE="MB" \
|
||||||
|
&& (( FSLIMIT = $(echo $FSLIMIT \
|
||||||
|
| sed s/MB//g) * 1024 ))
|
||||||
|
# check for "%" Character...Set IN_FILE=PC, for %
|
||||||
|
echo $FSLIMIT | grep "%" >/dev/null && IN_FILE="PC" \
|
||||||
|
&& FSLIMIT=$(echo $FSLIMIT | sed s/\%//g)
|
||||||
|
|
||||||
|
case $IN_FILE in
|
||||||
|
MB)
|
||||||
|
# Up-case the characters, if they exist
|
||||||
|
FSLIMIT=$(echo $FSLIMIT | tr '[a-z]' '[A-Z]')
|
||||||
|
# Get rid of the "MB" if it exists
|
||||||
|
FSLIMIT=$(echo $FSLIMIT | sed s/MB//g)
|
||||||
|
# Test for blank and null values
|
||||||
|
if [[ ! -z $FSLIMIT && $FSLIMIT != '' ]]
|
||||||
|
then
|
||||||
|
# Test for a valid filesystem "MB" limit
|
||||||
|
if (( $FSLIMIT >= 0 && $FSLIMIT < $FSSIZE ))
|
||||||
|
then
|
||||||
|
if (( $FSMB_FREE < $FSLIMIT ))
|
||||||
|
then
|
||||||
|
return 1 # Found out of limit using MB Free method
|
||||||
|
else
|
||||||
|
return 3 # Found OK
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo "\nERROR: Invalid filesystem MAX for $FSMOUNT - $FSLIMIT"
|
||||||
|
echo " Exceptions file value must be less than or"
|
||||||
|
echo " equal to the size of the filesystem measured"
|
||||||
|
echo " in 1024 bytes\n"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo "\nERROR: Null value specified in excepeptions file"
|
||||||
|
echo " for the $FSMOUNT mount point.\n"
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
PC)
|
||||||
|
# Strip out the % sign if it exists
|
||||||
|
PC_USED=$(echo $PC_USED | sed s/\%//g)
|
||||||
|
# Test for blank and null values
|
||||||
|
if [[ ! -z $FSLIMIT && $FSLIMIT != '' ]]
|
||||||
|
then
|
||||||
|
# Test for a valid percentage, i.e. 0-100
|
||||||
|
if (( $FSLIMIT >= 0 && $FSLIMIT <= 100 ))
|
||||||
|
then
|
||||||
|
if (( $PC_USED > $FSLIMIT ))
|
||||||
|
then
|
||||||
|
return 2 # Found exceeded by % Used method
|
||||||
|
else
|
||||||
|
return 3 # Found OK
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo "\nERROR: Invalid percentage for $FSMOUNT - $FSLIMIT"
|
||||||
|
echo " Exceptions file values must be"
|
||||||
|
echo " between 0 and 100%\n"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo "\nERROR: Null value specified in excepeptions file"
|
||||||
|
echo " for the $FSMOUNT mount point.\n"
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
N)
|
||||||
|
# Method Not Specified - Use Script Defaults
|
||||||
|
if (( $FSSIZE >= $FSTRIGGER ))
|
||||||
|
then # This is a "large" filesystem
|
||||||
|
if (( $FSMB_FREE < $MIN_MB_FREE ))
|
||||||
|
then
|
||||||
|
return 1 # Found out of limit using MB Free method
|
||||||
|
else
|
||||||
|
return 3 # Found OK
|
||||||
|
fi
|
||||||
|
else # This is a standard filesystem
|
||||||
|
PC_USED=$(echo $PC_USED | sed s/\%//g) # Remove the %
|
||||||
|
FSLIMIT=$(echo $FSLIMIT | sed s/\%//g) # Remove the %
|
||||||
|
if (( $PC_USED > $FSLIMIT ))
|
||||||
|
then
|
||||||
|
return 2 # Found exceeded by % Used method
|
||||||
|
else
|
||||||
|
return 3 # Found OK
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done < $DATA_EXCEPTIONS # Feed the loop from the bottom!!!
|
||||||
|
|
||||||
|
return 4 # Not found in $EXCEPTIONS file
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################
|
||||||
|
|
||||||
|
function display_output
|
||||||
|
{
|
||||||
|
if [[ -s $OUTFILE ]]
|
||||||
|
then
|
||||||
|
echo "\nFull Filesystem(s) on $THISHOST\n"
|
||||||
|
cat $OUTFILE
|
||||||
|
print
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################
|
||||||
|
|
||||||
|
function load_EXCEPTIONS_data
|
||||||
|
{
|
||||||
|
# Ingore any line that begins with a pound sign, #
|
||||||
|
|
||||||
|
cat $EXCEPTIONS | grep -v "^#" > $DATA_EXCEPTIONS
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################
|
||||||
|
|
||||||
|
function load_FS_data
|
||||||
|
{
|
||||||
|
df -k | tail +2 | egrep -v '/dev/cd[0-9] | /proc' \
|
||||||
|
| awk '{print $1, $2, $3, $4, $7}' > $WORKFILE
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################
|
||||||
|
######### START OF MAIN ############
|
||||||
|
####################################
|
||||||
|
|
||||||
|
load_FS_data
|
||||||
|
|
||||||
|
# Do we have a non-zero size $EXCEPTIONS file?
|
||||||
|
|
||||||
|
if [[ -s $EXCEPTIONS ]]
|
||||||
|
then # Found a non-empty $EXCEPTIONS file
|
||||||
|
|
||||||
|
load_EXCEPTIONS_data
|
||||||
|
EXCEP_FILE="Y"
|
||||||
|
fi
|
||||||
|
|
||||||
|
while read FSDEVICE FSSIZE FSMB_FREE PC_USED FSMOUNT
|
||||||
|
do
|
||||||
|
if [[ $EXCEP_FILE = "Y" ]]
|
||||||
|
then
|
||||||
|
check_exceptions
|
||||||
|
CE_RC="$?" # Check Exceptions Return Code (CE_RC)
|
||||||
|
|
||||||
|
case $CE_RC in
|
||||||
|
1) # Found exceeded in exceptions file by MB Method
|
||||||
|
(( FS_FREE_OUT = $FSMB_FREE / 1000 ))
|
||||||
|
echo "$FSDEVICE mounted on $FSMOUNT has ${FS_FREE_OUT}MB Free" \
|
||||||
|
>> $OUTFILE
|
||||||
|
;;
|
||||||
|
2) # Found exceeded in exceptions file by %Used method
|
||||||
|
echo "$FSDEVICE mount on $FSMOUNT is ${PC_USED}%" \
|
||||||
|
>> $OUTFILE
|
||||||
|
;;
|
||||||
|
3) # Found OK in exceptions file
|
||||||
|
: # NO-OP Do Nothing
|
||||||
|
;;
|
||||||
|
|
||||||
|
4) # Not found in exceptions file - Use Default Triggers
|
||||||
|
if (( $FSSIZE >= $FSTRIGGER ))
|
||||||
|
then # This is a "large" filesystem
|
||||||
|
FSMB_FREE=$(echo $FSMB_FREE | sed s/MB//g) # Remove the "MB"
|
||||||
|
if (( $FSMB_FREE < $MIN_MB_FREE ))
|
||||||
|
then
|
||||||
|
(( FS_FREE_OUT = $FSMB_FREE / 1000 ))
|
||||||
|
echo "$FSDEVICE mounted on $FSMOUNT has ${FS_FREE_OUT}MB Free" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
else # This is a standard filesystem
|
||||||
|
PC_USED=$(echo $PC_USED | sed s/\%//g)
|
||||||
|
MAX_PERCENT=$(echo $MAX_PERCENT | sed s/\%//g)
|
||||||
|
if (( $PC_USED > $MAX_PERCENT ))
|
||||||
|
then
|
||||||
|
echo "$FSDEVICE mount on $FSMOUNT is ${PC_USED}%" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
|
||||||
|
else # NO $EXECPTIONS FILE USE DEFAULT TRIGGER VALUES
|
||||||
|
|
||||||
|
if (( $FSSIZE >= $FSTRIGGER ))
|
||||||
|
then # This is a "large" filesystem - Use MB Free Method
|
||||||
|
FSMB_FREE=$(echo $FSMB_FREE | sed s/MB//g) # Remove the "MB"
|
||||||
|
if (( $FSMB_FREE < $MIN_MB_FREE ))
|
||||||
|
then
|
||||||
|
(( FS_FREE_OUT = $FSMB_FREE / 1000 ))
|
||||||
|
echo "$FSDEVICE mounted on $FSMOUNT has ${FS_FREE_OUT}MB Free" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
else # This is a standard filesystem - Use % Used Method
|
||||||
|
PC_USED=$(echo $PC_USED | sed s/\%//g)
|
||||||
|
MAX_PERCENT=$(echo $MAX_PERCENT | sed s/\%//g)
|
||||||
|
if (( $PC_USED > $MAX_PERCENT ))
|
||||||
|
then
|
||||||
|
echo "$FSDEVICE mount on $FSMOUNT is ${PC_USED}%" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done < $WORKFILE
|
||||||
|
|
||||||
|
display_output
|
||||||
|
|
||||||
|
# End of Script
|
||||||
52
chapter17/fs_mon_HPUX.ksh
Executable file
52
chapter17/fs_mon_HPUX.ksh
Executable file
@@ -0,0 +1,52 @@
|
|||||||
|
#!/usr/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: fs_mon_HPUX.ksh
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 08-22-2007
|
||||||
|
# REV: 1.1.P
|
||||||
|
# PURPOSE: This script is used to monitor for full filesystems,
|
||||||
|
# which is defined as "exceeding" the FSMAX value.
|
||||||
|
# A message is displayed for all "full" filesystems.
|
||||||
|
#
|
||||||
|
# REV LIST:
|
||||||
|
#
|
||||||
|
# PLATFORM: HP-UX
|
||||||
|
#
|
||||||
|
# set -n # Uncomment to check syntax without any execution
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
#
|
||||||
|
##### DEFINE FILES AND VARIABLES HERE ####
|
||||||
|
|
||||||
|
WORKFILE="/tmp/df.work" # Holds filesystem data
|
||||||
|
>$WORKFILE # Initialize to empty
|
||||||
|
OUTFILE="/tmp/df.outfile" # Output display file
|
||||||
|
>$OUTFILE # Initialize to empty
|
||||||
|
THISHOST=`hostname` # Hostname of this machine
|
||||||
|
FSMAX="85" # Max. FS percentage value
|
||||||
|
|
||||||
|
######## START OF MAIN #############
|
||||||
|
|
||||||
|
# Get the data of interest by stripping out the
|
||||||
|
# /cdrom row and keeping columns 1, 5 and 6
|
||||||
|
|
||||||
|
bdf | tail +2 | egrep -v '/cdrom' \
|
||||||
|
| awk '{print $1, $5, $6}' > $WORKFILE
|
||||||
|
|
||||||
|
# Loop through each line of the file and compare column 2
|
||||||
|
|
||||||
|
while read FSDEVICE FSVALUE FSMOUNT
|
||||||
|
do
|
||||||
|
FSVALUE=$(echo $FSVALUE | sed s/\%//g) # Remove the % sign
|
||||||
|
if [ $FSVALUE -gt $FSMAX ]
|
||||||
|
then
|
||||||
|
echo "$FSDEVICE mounted on $FSMOUNT is ${FSVALUE}%" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
done < $WORKFILE
|
||||||
|
|
||||||
|
if [[ -s $OUTFILE ]]
|
||||||
|
then
|
||||||
|
echo "\nFull Filesystem(s) on $THISHOST\n"
|
||||||
|
cat $OUTFILE
|
||||||
|
print
|
||||||
|
fi
|
||||||
59
chapter17/fs_mon_HPUX_MBFREE.ksh
Executable file
59
chapter17/fs_mon_HPUX_MBFREE.ksh
Executable file
@@ -0,0 +1,59 @@
|
|||||||
|
#!/usr/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: fs_mon_HPUX_MBFREE.ksh
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 08-22-2007
|
||||||
|
# REV: 1.5.P
|
||||||
|
# PURPOSE: This script is used to monitor for full filesystems,
|
||||||
|
# which is defined as "exceeding" the FSMAX value.
|
||||||
|
# A message is displayed for all "full" filesystems.
|
||||||
|
#
|
||||||
|
# PLATFORM: HP-UX
|
||||||
|
#
|
||||||
|
# REV LIST:
|
||||||
|
# Randy Michael - 08-27-2007
|
||||||
|
# Changed the code to use MB of free space instead of
|
||||||
|
# the %Used method.
|
||||||
|
#
|
||||||
|
# set -n # Uncomment to check syntax without any execution
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
#
|
||||||
|
##### DEFINE FILES AND VARIABLES HERE ####
|
||||||
|
|
||||||
|
WORKFILE="/tmp/df.work" # Holds filesystem data
|
||||||
|
>$WORKFILE # Initialize to empty
|
||||||
|
OUTFILE="/tmp/df.outfile" # Output display file
|
||||||
|
>$OUTFILE # Initialize to empty
|
||||||
|
THISHOST=`hostname` # Hostname of this machine
|
||||||
|
MIN_MB_FREE="50MB" # Min. MB of Free FS Space
|
||||||
|
|
||||||
|
######## START OF MAIN #############
|
||||||
|
|
||||||
|
# Get the data of interest by stripping out the
|
||||||
|
# /cdrom row and keeping columns 1, 4 and 6
|
||||||
|
|
||||||
|
bdf | tail +2 | egrep -v '/cdrom' \
|
||||||
|
| awk '{print $1, $4, $6}' > $WORKFILE
|
||||||
|
|
||||||
|
# Format Variables
|
||||||
|
(( MIN_MB_FREE = $(echo $MIN_MB_FREE | sed s/MB//g) * 1024 ))
|
||||||
|
|
||||||
|
# Loop through each line of the file and compare column 2
|
||||||
|
|
||||||
|
while read FSDEVICE FSMB_FREE FSMOUNT
|
||||||
|
do
|
||||||
|
FSMB_FREE=$(echo $FSMB_FREE | sed s/MB//g) # Remove the "MB"
|
||||||
|
if (( $FSMB_FREE < $MIN_MB_FREE ))
|
||||||
|
then
|
||||||
|
(( FS_FREE_OUT = $FSMB_FREE / 1000 ))
|
||||||
|
echo "$FSDEVICE mounted on $FSMOUNT has ${FS_FREE_OUT}MB Free" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
done < $WORKFILE
|
||||||
|
|
||||||
|
if [[ -s $OUTFILE ]]
|
||||||
|
then
|
||||||
|
echo "\nFull Filesystem(s) on $THISHOST\n"
|
||||||
|
cat $OUTFILE
|
||||||
|
print
|
||||||
|
fi
|
||||||
129
chapter17/fs_mon_HPUX_MBFREE_excep.ksh
Executable file
129
chapter17/fs_mon_HPUX_MBFREE_excep.ksh
Executable file
@@ -0,0 +1,129 @@
|
|||||||
|
#!/usr/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: fs_mon_HPUX_MBFREE_excep.ksh
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 08-22-2007
|
||||||
|
# REV: 2.1.P
|
||||||
|
# PURPOSE: This script is used to monitor for full filesystems,
|
||||||
|
# which is defined as "exceeding" the FSMAX value.
|
||||||
|
# A message is displayed for all "full" filesystems.
|
||||||
|
#
|
||||||
|
# PLATFORM: HP-UX
|
||||||
|
#
|
||||||
|
# REV LIST:
|
||||||
|
# Randy Michael - 08-27-2007
|
||||||
|
# Changed the code to use MB of free space instead of
|
||||||
|
# the %Used method.
|
||||||
|
#
|
||||||
|
# Randy Michael - 08-27-2007
|
||||||
|
# Added code to allow you to override the set script default
|
||||||
|
# for MIN_MB_FREE of FS Space
|
||||||
|
#
|
||||||
|
# set -n # Uncomment to check syntax without any execution
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
#
|
||||||
|
##### DEFINE FILES AND VARIABLES HERE ####
|
||||||
|
|
||||||
|
WORKFILE="/tmp/df.work" # Holds filesystem data
|
||||||
|
>$WORKFILE # Initialize to empty
|
||||||
|
OUTFILE="/tmp/df.outfile" # Output display file
|
||||||
|
>$OUTFILE # Initialize to empty
|
||||||
|
EXCEPTIONS="/usr/local/bin/exceptions" # Override data file
|
||||||
|
DATA_EXCEPTIONS="/tmp/dfdata.out" # Exceptions file w/o # rows
|
||||||
|
THISHOST=`hostname` # Hostname of this machine
|
||||||
|
MIN_MB_FREE="50MB" # Min. MB of Free FS Space
|
||||||
|
|
||||||
|
####### DEFINE FUNCTIONS HERE ########
|
||||||
|
|
||||||
|
function check_exceptions
|
||||||
|
{
|
||||||
|
# set -x # Uncomment to debug this function
|
||||||
|
|
||||||
|
while read FSNAME FSLIMIT
|
||||||
|
do
|
||||||
|
# Do an NFS sanity check
|
||||||
|
echo $FSNAME | grep ":" >/dev/null \
|
||||||
|
&& FSNAME=$(echo $FSNAME | cut -d ":" -f2)
|
||||||
|
if [[ ! -z "$FSLIMIT" && "$FSLIMIT" != '' ]]
|
||||||
|
then
|
||||||
|
(( FSLIMIT = $(echo $FSLIMIT | sed s/MB//g) * 1024 ))
|
||||||
|
if [[ $FSNAME = $FSMOUNT ]]
|
||||||
|
then
|
||||||
|
# Get rid of the "MB" if it exists
|
||||||
|
FSLIMIT=$(echo $FSLIMIT | sed s/MB//g)
|
||||||
|
if (( $FSMB_FREE < $FSLIMIT ))
|
||||||
|
then
|
||||||
|
return 1 # Found out of limit
|
||||||
|
else
|
||||||
|
return 2 # Found OK
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done < $DATA_EXCEPTIONS # Feed the loop from the bottom!!!
|
||||||
|
|
||||||
|
return 3 # Not found in $EXCEPTIONS file
|
||||||
|
}
|
||||||
|
|
||||||
|
######## START OF MAIN #############
|
||||||
|
|
||||||
|
if [[ -s $EXCEPTIONS ]]
|
||||||
|
then
|
||||||
|
# Ignore all line beginning with a pound sign, #.
|
||||||
|
cat $EXCEPTIONS | grep -v "^#" > $DATA_EXCEPTIONS
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Get the data of interest by stripping out the
|
||||||
|
# /cdrom row and keeping columns 1, 4 and 6
|
||||||
|
|
||||||
|
bdf | tail +2 | egrep -v '/cdrom' \
|
||||||
|
| awk '{print $1, $4, $6}' > $WORKFILE
|
||||||
|
|
||||||
|
# Format Variables for the proper MB value
|
||||||
|
(( MIN_MB_FREE = $(echo $MIN_MB_FREE | sed s/MB//g) * 1024 ))
|
||||||
|
|
||||||
|
# Loop through each line of the file and compare column 2
|
||||||
|
|
||||||
|
while read FSDEVICE FSMB_FREE FSMOUNT
|
||||||
|
do
|
||||||
|
if [[ -s $EXCEPTIONS ]]
|
||||||
|
then
|
||||||
|
check_exceptions
|
||||||
|
RC="$?"
|
||||||
|
if [ $RC -eq 1 ] # Found out of exceptions limit
|
||||||
|
then
|
||||||
|
(( FS_FREE_OUT = $FSMB_FREE / 1000 ))
|
||||||
|
echo "$FSDEVICE mounted on $FSMOUNT only has ${FS_FREE_OUT}MB Free" \
|
||||||
|
>> $OUTFILE
|
||||||
|
elif [ $RC -eq 2 ] # Found in exceptions to be OK
|
||||||
|
then # Just a sanity check - We really do nothing here...
|
||||||
|
# The colon, :, is a NO-OP operator in KSH
|
||||||
|
|
||||||
|
: # No-Op - Do Nothing!
|
||||||
|
|
||||||
|
elif [ $RC -eq 3 ] # Not found in the exceptions file
|
||||||
|
then
|
||||||
|
FSMB_FREE=$(echo $FSMB_FREE | sed s/MB//g) # Remove the "MB"
|
||||||
|
if (( $FSMB_FREE < $MIN_MB_FREE ))
|
||||||
|
then
|
||||||
|
(( FS_FREE_OUT = $FSMB_FREE / 1000 ))
|
||||||
|
echo "$FSDEVICE mounted on $FSMOUNT only has ${FS_FREE_OUT}MB Free" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
else # No Exceptions file use the script default
|
||||||
|
FSMB_FREE=$(echo $FSMB_FREE | sed s/MB//g) # Remove the "MB"
|
||||||
|
if (( $FSMB_FREE < $MIN_MB_FREE ))
|
||||||
|
then
|
||||||
|
(( FS_FREE_OUT = $FSMB_FREE / 1000 ))
|
||||||
|
echo "$FSDEVICE mounted on $FSMOUNT only has ${FS_FREE_OUT}MB Free" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done < $WORKFILE
|
||||||
|
|
||||||
|
if [[ -s $OUTFILE ]]
|
||||||
|
then
|
||||||
|
echo "\nFull Filesystem(s) on $THISHOST\n"
|
||||||
|
cat $OUTFILE
|
||||||
|
print
|
||||||
|
fi
|
||||||
280
chapter17/fs_mon_HPUX_PC_MBFREE.ksh
Executable file
280
chapter17/fs_mon_HPUX_PC_MBFREE.ksh
Executable file
@@ -0,0 +1,280 @@
|
|||||||
|
#!/usr/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: fs_mon_HPUX_PC_MBFREE.ksh
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 08-22-2007
|
||||||
|
# REV: 4.3.P
|
||||||
|
# PURPOSE: This script is used to monitor for full filesystems,
|
||||||
|
# which is defined as "exceeding" the MAX_PERCENT value.
|
||||||
|
# A message is displayed for all "full" filesystems.
|
||||||
|
#
|
||||||
|
# PLATFORM: HP-UX
|
||||||
|
#
|
||||||
|
# REV LIST:
|
||||||
|
# Randy Michael - 08-27-2007
|
||||||
|
# Changed the code to use MB of free space instead of
|
||||||
|
# the %Used method.
|
||||||
|
#
|
||||||
|
# Randy Michael - 08-27-2007
|
||||||
|
# Added code to allow you to override the set script default
|
||||||
|
# for MIN_MB_FREE of FS Space
|
||||||
|
#
|
||||||
|
# Randy Michael - 08-28-2007
|
||||||
|
# Changed the code to handle both %Used and MB of Free Space.
|
||||||
|
# It does an "auto-detection" but has override capability
|
||||||
|
# of both the trigger level and the monitoring method using
|
||||||
|
# the exceptions file pointed to by the $EXCEPTIONS variable
|
||||||
|
#
|
||||||
|
# set -n # Uncomment to check syntax without any execution
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
#
|
||||||
|
##### DEFINE FILES AND VARIABLES HERE ####
|
||||||
|
|
||||||
|
WORKFILE="/tmp/df.work" # Holds filesystem data
|
||||||
|
>$WORKFILE # Initialize to empty
|
||||||
|
OUTFILE="/tmp/df.outfile" # Output display file
|
||||||
|
>$OUTFILE # Initialize to empty
|
||||||
|
EXCEPTIONS="/usr/local/bin/exceptions" # Override data file
|
||||||
|
DATA_EXCEPTIONS="/tmp/dfdata.out" # Exceptions file w/o # rows
|
||||||
|
EXCEPT_FILE="N" # Assume no $EXCEPTIONS FILE
|
||||||
|
THISHOST=`hostname` # Hostname of this machine
|
||||||
|
|
||||||
|
MIN_MB_FREE="100MB" # Min. MB of Free FS Space
|
||||||
|
MAX_PERCENT="85%" # Max. FS percentage value
|
||||||
|
FSTRIGGER="1000MB" # Trigger to switch from % Used to MB Free
|
||||||
|
|
||||||
|
|
||||||
|
###### FORMAT VARIABLES HERE ######
|
||||||
|
|
||||||
|
# Both of these variables need to multiplied by 1024 blocks
|
||||||
|
(( MIN_MB_FREE = $(echo $MIN_MB_FREE | sed s/MB//g) * 1024 ))
|
||||||
|
(( FSTRIGGER = $(echo $FSTRIGGER | sed s/MB//g) * 1024 ))
|
||||||
|
|
||||||
|
####### DEFINE FUNCTIONS HERE ########
|
||||||
|
|
||||||
|
function check_exceptions
|
||||||
|
{
|
||||||
|
# set -x # Uncomment to debug this function
|
||||||
|
|
||||||
|
while read FSNAME FSLIMIT
|
||||||
|
do
|
||||||
|
IN_FILE="N"
|
||||||
|
|
||||||
|
# Do an NFS sanity check and get rid of any ":".
|
||||||
|
# If this is found it is actaully an error entry
|
||||||
|
# but we will try to resolve it. It will only
|
||||||
|
# work if it is an NFS cross mount to the same
|
||||||
|
# mount point on both machines.
|
||||||
|
echo $FSNAME | grep ':' >/dev/null \
|
||||||
|
&& FSNAME=$(echo $FSNAME | cut -d ':' -f2)
|
||||||
|
|
||||||
|
# Check for empty and null variable
|
||||||
|
if [[ ! -z $FSLIMIT && $FSLIMIT != '' ]]
|
||||||
|
then
|
||||||
|
if [[ $FSNAME = $FSMOUNT ]] # Found it!
|
||||||
|
then
|
||||||
|
# Check for "MB" Characters...Set IN_FILE=MB
|
||||||
|
echo $FSLIMIT | grep MB >/dev/null && IN_FILE="MB" \
|
||||||
|
&& (( FSLIMIT = $(echo $FSLIMIT \
|
||||||
|
| sed s/MB//g) * 1024 ))
|
||||||
|
# check for "%" Character...Set IN_FILE=PC, for %
|
||||||
|
echo $FSLIMIT | grep "%" >/dev/null && IN_FILE="PC" \
|
||||||
|
&& FSLIMIT=$(echo $FSLIMIT | sed s/\%//g)
|
||||||
|
|
||||||
|
case $IN_FILE in
|
||||||
|
MB)
|
||||||
|
# Up-case the characters, if they exist
|
||||||
|
FSLIMIT=$(echo $FSLIMIT | tr '[a-z]' '[A-Z]')
|
||||||
|
# Get rid of the "MB" if it exists
|
||||||
|
FSLIMIT=$(echo $FSLIMIT | sed s/MB//g)
|
||||||
|
# Test for blank and null values
|
||||||
|
if [[ ! -z $FSLIMIT && $FSLIMIT != '' ]]
|
||||||
|
then
|
||||||
|
# Test for a valid filesystem "MB" limit
|
||||||
|
if (( $FSLIMIT >= 0 && $FSLIMIT < $FSSIZE ))
|
||||||
|
then
|
||||||
|
if (( $FSMB_FREE < $FSLIMIT ))
|
||||||
|
then
|
||||||
|
return 1 # Found out of limit using MB Free method
|
||||||
|
else
|
||||||
|
return 3 # Found OK
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo "\nERROR: Invalid filesystem MAX for $FSMOUNT - $FSLIMIT"
|
||||||
|
echo " Exceptions file value must be less than or"
|
||||||
|
echo " equal to the size of the filesystem measured"
|
||||||
|
echo " in 1024 bytes\n"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo "\nERROR: Null value specified in excepeptions file"
|
||||||
|
echo " for the $FSMOUNT mount point.\n"
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
PC)
|
||||||
|
# Strip out the % sign if it exists
|
||||||
|
PC_USED=$(echo $PC_USED | sed s/\%//g)
|
||||||
|
# Test for blank and null values
|
||||||
|
if [[ ! -z $FSLIMIT && $FSLIMIT != '' ]]
|
||||||
|
then
|
||||||
|
# Test for a valid percentage, i.e. 0-100
|
||||||
|
if (( $FSLIMIT >= 0 && $FSLIMIT <= 100 ))
|
||||||
|
then
|
||||||
|
if (( $PC_USED > $FSLIMIT ))
|
||||||
|
then
|
||||||
|
return 2 # Found exceeded by % Used method
|
||||||
|
else
|
||||||
|
return 3 # Found OK
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo "\nERROR: Invalid percentage for $FSMOUNT - $FSLIMIT"
|
||||||
|
echo " Exceptions file values must be"
|
||||||
|
echo " between 0 and 100%\n"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo "\nERROR: Null value specified in excepeptions file"
|
||||||
|
echo " for the $FSMOUNT mount point.\n"
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
N)
|
||||||
|
# Method Not Specified - Use Script Defaults
|
||||||
|
if (( $FSSIZE >= $FSTRIGGER ))
|
||||||
|
then # This is a "large" filesystem
|
||||||
|
if (( $FSMB_FREE < $MIN_MB_FREE ))
|
||||||
|
then
|
||||||
|
return 1 # Found out of limit using MB Free method
|
||||||
|
else
|
||||||
|
return 3 # Found OK
|
||||||
|
fi
|
||||||
|
else # This is a standard filesystem
|
||||||
|
PC_USED=$(echo $PC_USED | sed s/\%//g) # Remove the %
|
||||||
|
FSLIMIT=$(echo $FSLIMIT | sed s/\%//g) # Remove the %
|
||||||
|
if (( $PC_USED > $FSLIMIT ))
|
||||||
|
then
|
||||||
|
return 2 # Found exceeded by % Used method
|
||||||
|
else
|
||||||
|
return 3 # Found OK
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done < $DATA_EXCEPTIONS # Feed the loop from the bottom!!!
|
||||||
|
|
||||||
|
return 4 # Not found in $EXCEPTIONS file
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################
|
||||||
|
|
||||||
|
function display_output
|
||||||
|
{
|
||||||
|
if [[ -s $OUTFILE ]]
|
||||||
|
then
|
||||||
|
echo "\nFull Filesystem(s) on $THISHOST\n"
|
||||||
|
cat $OUTFILE
|
||||||
|
print
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################
|
||||||
|
|
||||||
|
function load_EXCEPTIONS_data
|
||||||
|
{
|
||||||
|
# Ingore any line that begins with a pound sign, #
|
||||||
|
|
||||||
|
cat $EXCEPTIONS | grep -v "^#" > $DATA_EXCEPTIONS
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################
|
||||||
|
|
||||||
|
function load_FS_data
|
||||||
|
{
|
||||||
|
bdf | tail +2 | egrep -v '/cdrom' \
|
||||||
|
| awk '{print $1, $2, $4, $5, $6}' > $WORKFILE
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################
|
||||||
|
######### START OF MAIN ############
|
||||||
|
####################################
|
||||||
|
|
||||||
|
load_FS_data
|
||||||
|
|
||||||
|
# Do we have a non-zero size $EXCEPTIONS file?
|
||||||
|
|
||||||
|
if [[ -s $EXCEPTIONS ]]
|
||||||
|
then # Found a non-empty $EXCEPTIONS file
|
||||||
|
|
||||||
|
load_EXCEPTIONS_data
|
||||||
|
EXCEP_FILE="Y"
|
||||||
|
fi
|
||||||
|
|
||||||
|
while read FSDEVICE FSSIZE FSMB_FREE PC_USED FSMOUNT
|
||||||
|
do
|
||||||
|
if [[ $EXCEP_FILE = "Y" ]]
|
||||||
|
then
|
||||||
|
check_exceptions
|
||||||
|
CE_RC="$?" # Check Exceptions Return Code (CE_RC)
|
||||||
|
|
||||||
|
case $CE_RC in
|
||||||
|
1) # Found exceeded in exceptions file by MB Method
|
||||||
|
(( FS_FREE_OUT = $FSMB_FREE / 1000 ))
|
||||||
|
echo "$FSDEVICE mounted on $FSMOUNT has ${FS_FREE_OUT}MB Free" \
|
||||||
|
>> $OUTFILE
|
||||||
|
;;
|
||||||
|
2) # Found exceeded in exceptions file by %Used method
|
||||||
|
echo "$FSDEVICE mount on $FSMOUNT is ${PC_USED}%" \
|
||||||
|
>> $OUTFILE
|
||||||
|
;;
|
||||||
|
3) # Found OK in exceptions file
|
||||||
|
: # NO-OP Do Nothing
|
||||||
|
;;
|
||||||
|
|
||||||
|
4) # Not found in exceptions file - Use Default Triggers
|
||||||
|
if (( $FSSIZE >= $FSTRIGGER ))
|
||||||
|
then # This is a "large" filesystem
|
||||||
|
FSMB_FREE=$(echo $FSMB_FREE | sed s/MB//g) # Remove the "MB"
|
||||||
|
if (( $FSMB_FREE < $MIN_MB_FREE ))
|
||||||
|
then
|
||||||
|
(( FS_FREE_OUT = $FSMB_FREE / 1000 ))
|
||||||
|
echo "$FSDEVICE mounted on $FSMOUNT has ${FS_FREE_OUT}MB Free" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
else # This is a standard filesystem
|
||||||
|
PC_USED=$(echo $PC_USED | sed s/\%//g)
|
||||||
|
MAX_PERCENT=$(echo $MAX_PERCENT | sed s/\%//g)
|
||||||
|
if (( $PC_USED > $MAX_PERCENT ))
|
||||||
|
then
|
||||||
|
echo "$FSDEVICE mount on $FSMOUNT is ${PC_USED}%" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
|
||||||
|
else # NO $EXECPTIONS FILE USE DEFAULT TRIGGER VALUES
|
||||||
|
|
||||||
|
if (( $FSSIZE >= $FSTRIGGER ))
|
||||||
|
then # This is a "large" filesystem - Use MB Free Method
|
||||||
|
FSMB_FREE=$(echo $FSMB_FREE | sed s/MB//g) # Remove the "MB"
|
||||||
|
if (( $FSMB_FREE < $MIN_MB_FREE ))
|
||||||
|
then
|
||||||
|
(( FS_FREE_OUT = $FSMB_FREE / 1000 ))
|
||||||
|
echo "$FSDEVICE mounted on $FSMOUNT has ${FS_FREE_OUT}MB Free" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
else # This is a standard filesystem - Use % Used Method
|
||||||
|
PC_USED=$(echo $PC_USED | sed s/\%//g)
|
||||||
|
MAX_PERCENT=$(echo $MAX_PERCENT | sed s/\%//g)
|
||||||
|
if (( $PC_USED > $MAX_PERCENT ))
|
||||||
|
then
|
||||||
|
echo "$FSDEVICE mount on $FSMOUNT is ${PC_USED}%" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done < $WORKFILE
|
||||||
|
|
||||||
|
display_output
|
||||||
|
|
||||||
|
# End of Script
|
||||||
113
chapter17/fs_mon_HPUX_excep.ksh
Executable file
113
chapter17/fs_mon_HPUX_excep.ksh
Executable file
@@ -0,0 +1,113 @@
|
|||||||
|
#!/usr/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: fs_mon_HPUX_excep.ksh
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 08-22-2007
|
||||||
|
# REV: 2.1.P
|
||||||
|
# PURPOSE: This script is used to monitor for full filesystems,
|
||||||
|
# which is defined as "exceeding" the FSMAX value.
|
||||||
|
# A message is displayed for all "full" filesystems.
|
||||||
|
#
|
||||||
|
# PLATFORM: HP-UX
|
||||||
|
#
|
||||||
|
# REV LIST:
|
||||||
|
# 08-23-2007 - Randy Michael
|
||||||
|
# Added code to override the default FSMAX script threshold
|
||||||
|
# using an "exceptions" file, defined by the $EXCEPTIONS
|
||||||
|
# variable, that list /mount_point and NEW_MAX%
|
||||||
|
#
|
||||||
|
# set -n # Uncomment to check syntax without any execution
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
#
|
||||||
|
##### DEFINE FILES AND VARIABLES HERE ####
|
||||||
|
|
||||||
|
WORKFILE="/tmp/df.work" # Holds filesystem data
|
||||||
|
>$WORKFILE # Initialize to empty
|
||||||
|
OUTFILE="/tmp/df.outfile" # Output display file
|
||||||
|
>$OUTFILE # Initialize to empty
|
||||||
|
BINDIR="/usr/local/bin" # Local bin directory
|
||||||
|
THISHOST=`hostname` # Hostname of this machine
|
||||||
|
FSMAX="85" # Max. FS percentage value
|
||||||
|
EXCEPTIONS="${BINDIR}/exceptions" # Overrides $FSMAX
|
||||||
|
|
||||||
|
####### DEFINE FUNCTIONS HERE #####
|
||||||
|
|
||||||
|
function check_exceptions
|
||||||
|
{
|
||||||
|
|
||||||
|
# set -x # Uncomment to debug this function
|
||||||
|
|
||||||
|
# Define a data file
|
||||||
|
|
||||||
|
DATA_EXCEPTIONS="/tmp/dfdata.out"
|
||||||
|
|
||||||
|
# Ingore any line that begins with a pound sign, #
|
||||||
|
|
||||||
|
cat $EXCEPTIONS | grep -v "^#" > $DATA_EXCEPTIONS
|
||||||
|
|
||||||
|
while read FSNAME NEW_MAX # Feeding Ddata from Bottom of Loop!!!
|
||||||
|
do
|
||||||
|
if [[ $FSNAME = $FSMOUNT ]] # Correct /mount_point?
|
||||||
|
then # Get rid of the % sign, if it exists!
|
||||||
|
NEW_MAX=$(echo $NEW_MAX | sed s/\%//g)
|
||||||
|
|
||||||
|
if [ $FSVALUE -gt $NEW_MAX ]
|
||||||
|
then # Over Limit...Return a "0", zero
|
||||||
|
return 0 # FOUND MAX OUT - Return 0
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
done < $DATA_EXCEPTIONS # Feed from the bottom of the loop!!
|
||||||
|
|
||||||
|
return 1 # Not found in File
|
||||||
|
}
|
||||||
|
|
||||||
|
######## START OF MAIN #############
|
||||||
|
|
||||||
|
# Get the data of interest by stripping out the
|
||||||
|
# /cdrom row and keeping columns 1, 5 and 6
|
||||||
|
|
||||||
|
bdf | tail +2 | egrep -v '/cdrom' \
|
||||||
|
| awk '{print $1, $5, $6}' > $WORKFILE
|
||||||
|
|
||||||
|
# Loop through each line of the file and compare column 2
|
||||||
|
|
||||||
|
while read FSDEVICE FSVALUE FSMOUNT
|
||||||
|
do
|
||||||
|
# Strip out the % sign if it exists
|
||||||
|
FSVALUE=$(echo $FSVALUE | sed s/\%//g) # Remove the % sign
|
||||||
|
if [[ -s $EXCEPTIONS ]] # Do we have a non-empty file?
|
||||||
|
then # Found it!
|
||||||
|
|
||||||
|
# Look for the current $FSMOUNT value in the file
|
||||||
|
# using the check_exceptions function defined above.
|
||||||
|
|
||||||
|
check_exceptions
|
||||||
|
if [ $? -eq 0 ] # Found it Exceeded!!
|
||||||
|
then
|
||||||
|
echo "$FSDEVICE mount on $FSMOUNT is ${FSVALUE}%" \
|
||||||
|
>> $OUTFILE
|
||||||
|
else # Not exceeded in the file use the script default
|
||||||
|
if [ $FSVALUE -gt $FSMAX ] # Use Script Default
|
||||||
|
then
|
||||||
|
echo "$FSDEVICE mount on $FSMOUNT is ${FSVALUE}%" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
else # No exceptions file use the script default
|
||||||
|
if [ $FSVALUE -gt $FSMAX ] # Use Script Default
|
||||||
|
then
|
||||||
|
echo "$FSDEVICE mount on $FSMOUNT is ${FSVALUE}%" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done < $WORKFILE # Feed the while loop from the bottom...
|
||||||
|
|
||||||
|
# Display output if anything is exceeded...
|
||||||
|
|
||||||
|
if [[ -s $OUTFILE ]]
|
||||||
|
then
|
||||||
|
echo "\nFull Filesystem(s) on $THISHOST\n"
|
||||||
|
cat $OUTFILE
|
||||||
|
print
|
||||||
|
fi
|
||||||
52
chapter17/fs_mon_LINUX.ksh
Executable file
52
chapter17/fs_mon_LINUX.ksh
Executable file
@@ -0,0 +1,52 @@
|
|||||||
|
#!/usr/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: fs_mon_LINUX.ksh
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 08-22-2007
|
||||||
|
# REV: 1.1.P
|
||||||
|
# PURPOSE: This script is used to monitor for full filesystems,
|
||||||
|
# which is defined as "exceeding" the FSMAX value.
|
||||||
|
# A message is displayed for all "full" filesystems.
|
||||||
|
#
|
||||||
|
# PLATFORM: Linux
|
||||||
|
#
|
||||||
|
# REV LIST:
|
||||||
|
#
|
||||||
|
# set -n # Uncomment to check syntax without any execution
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
#
|
||||||
|
##### DEFINE FILES AND VARIABLES HERE ####
|
||||||
|
|
||||||
|
WORKFILE="/tmp/df.work" # Holds filesystem data
|
||||||
|
>$WORKFILE # Initialize to empty
|
||||||
|
OUTFILE="/tmp/df.outfile" # Output display file
|
||||||
|
>$OUTFILE # Initialize to empty
|
||||||
|
THISHOST=`hostname` # Hostname of this machine
|
||||||
|
FSMAX="85" # Max. FS percentage value
|
||||||
|
|
||||||
|
######## START OF MAIN #############
|
||||||
|
|
||||||
|
# Get the data of interest by stripping out the rows that
|
||||||
|
# are not monitored and keeping columns 1, 5 and 6
|
||||||
|
|
||||||
|
df -k | tail +2 | egrep -v '/cdrom' \
|
||||||
|
| awk '{print $1, $5, $6}' > $WORKFILE
|
||||||
|
|
||||||
|
# Loop through each line of the file and compare column 2
|
||||||
|
|
||||||
|
while read FSDEVICE FSVALUE FSMOUNT
|
||||||
|
do
|
||||||
|
FSVALUE=$(echo $FSVALUE | sed s/\%//g) # Remove the % sign
|
||||||
|
if [ $FSVALUE -gt $FSMAX ]
|
||||||
|
then
|
||||||
|
echo "$FSDEVICE mounted on $FSMOUNT is ${FSVALUE}%" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
done < $WORKFILE
|
||||||
|
|
||||||
|
if [[ -s $OUTFILE ]]
|
||||||
|
then
|
||||||
|
echo "\nFull Filesystem(s) on $THISHOST\n"
|
||||||
|
cat $OUTFILE
|
||||||
|
print
|
||||||
|
fi
|
||||||
59
chapter17/fs_mon_LINUX_MBFREE.ksh
Executable file
59
chapter17/fs_mon_LINUX_MBFREE.ksh
Executable file
@@ -0,0 +1,59 @@
|
|||||||
|
#!/usr/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: fs_mon_LINUX_MBFREE.ksh
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 08-22-2007
|
||||||
|
# REV: 1.5.P
|
||||||
|
# PURPOSE: This script is used to monitor for full filesystems,
|
||||||
|
# which is defined as "exceeding" the FSMAX value.
|
||||||
|
# A message is displayed for all "full" filesystems.
|
||||||
|
#
|
||||||
|
# PLATFORM: Linux
|
||||||
|
#
|
||||||
|
# REV LIST:
|
||||||
|
# Randy Michael - 08-27-2007
|
||||||
|
# Changed the code to use MB of free space instead of
|
||||||
|
# the %Used method.
|
||||||
|
#
|
||||||
|
# set -n # Uncomment to check syntax without any execution
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
#
|
||||||
|
##### DEFINE FILES AND VARIABLES HERE ####
|
||||||
|
|
||||||
|
WORKFILE="/tmp/df.work" # Holds filesystem data
|
||||||
|
>$WORKFILE # Initialize to empty
|
||||||
|
OUTFILE="/tmp/df.outfile" # Output display file
|
||||||
|
>$OUTFILE # Initialize to empty
|
||||||
|
THISHOST=`hostname` # Hostname of this machine
|
||||||
|
MIN_MB_FREE="50MB" # Min. MB of Free FS Space
|
||||||
|
|
||||||
|
######## START OF MAIN #############
|
||||||
|
|
||||||
|
# Get the data of interest by stripping out the
|
||||||
|
# /cdrom row and keeping columns 1, 4 and 6
|
||||||
|
|
||||||
|
df -k | tail +2 | egrep -v '/cdrom' \
|
||||||
|
| awk '{print $1, $4, $6}' > $WORKFILE
|
||||||
|
|
||||||
|
# Format Variables
|
||||||
|
(( MIN_MB_FREE = $(echo $MIN_MB_FREE | sed s/MB//g) * 1024 ))
|
||||||
|
|
||||||
|
# Loop through each line of the file and compare column 2
|
||||||
|
|
||||||
|
while read FSDEVICE FSMB_FREE FSMOUNT
|
||||||
|
do
|
||||||
|
FSMB_FREE=$(echo $FSMB_FREE | sed s/MB//g) # Remove the "MB"
|
||||||
|
if (( $FSMB_FREE < $MIN_MB_FREE ))
|
||||||
|
then
|
||||||
|
(( FS_FREE_OUT = $FSMB_FREE / 1000 ))
|
||||||
|
echo "$FSDEVICE mounted on $FSMOUNT has ${FS_FREE_OUT}MB Free" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
done < $WORKFILE
|
||||||
|
|
||||||
|
if [[ -s $OUTFILE ]]
|
||||||
|
then
|
||||||
|
echo "\nFull Filesystem(s) on $THISHOST\n"
|
||||||
|
cat $OUTFILE
|
||||||
|
print
|
||||||
|
fi
|
||||||
129
chapter17/fs_mon_LINUX_MBFREE_excep.ksh
Executable file
129
chapter17/fs_mon_LINUX_MBFREE_excep.ksh
Executable file
@@ -0,0 +1,129 @@
|
|||||||
|
#!/usr/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: fs_mon_LINUX_MBFREE_excep.ksh
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 08-22-2007
|
||||||
|
# REV: 2.1.P
|
||||||
|
# PURPOSE: This script is used to monitor for full filesystems,
|
||||||
|
# which is defined as "exceeding" the FSMAX value.
|
||||||
|
# A message is displayed for all "full" filesystems.
|
||||||
|
#
|
||||||
|
# PLATFORM: Linux
|
||||||
|
#
|
||||||
|
# REV LIST:
|
||||||
|
# Randy Michael - 08-27-2007
|
||||||
|
# Changed the code to use MB of free space instead of
|
||||||
|
# the %Used method.
|
||||||
|
#
|
||||||
|
# Randy Michael - 08-27-2007
|
||||||
|
# Added code to allow you to override the set script default
|
||||||
|
# for MIN_MB_FREE of FS Space
|
||||||
|
#
|
||||||
|
# set -n # Uncomment to check syntax without any execution
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
#
|
||||||
|
##### DEFINE FILES AND VARIABLES HERE ####
|
||||||
|
|
||||||
|
WORKFILE="/tmp/df.work" # Holds filesystem data
|
||||||
|
>$WORKFILE # Initialize to empty
|
||||||
|
OUTFILE="/tmp/df.outfile" # Output display file
|
||||||
|
>$OUTFILE # Initialize to empty
|
||||||
|
EXCEPTIONS="/usr/local/bin/exceptions" # Override data file
|
||||||
|
DATA_EXCEPTIONS="/tmp/dfdata.out" # Exceptions file w/o # rows
|
||||||
|
THISHOST=`hostname` # Hostname of this machine
|
||||||
|
MIN_MB_FREE="50MB" # Min. MB of Free FS Space
|
||||||
|
|
||||||
|
####### DEFINE FUNCTIONS HERE ########
|
||||||
|
|
||||||
|
function check_exceptions
|
||||||
|
{
|
||||||
|
# set -x # Uncomment to debug this function
|
||||||
|
|
||||||
|
while read FSNAME FSLIMIT
|
||||||
|
do
|
||||||
|
# Do an NFS sanity check
|
||||||
|
echo $FSNAME | grep ":" >/dev/null \
|
||||||
|
&& FSNAME=$(echo $FSNAME | cut -d ":" -f2)
|
||||||
|
if [[ ! -z "$FSLIMIT" && "$FSLIMIT" != '' ]]
|
||||||
|
then
|
||||||
|
(( FSLIMIT = $(echo $FSLIMIT | sed s/MB//g) * 1024 ))
|
||||||
|
if [[ $FSNAME = $FSMOUNT ]]
|
||||||
|
then
|
||||||
|
# Get rid of the "MB" if it exists
|
||||||
|
FSLIMIT=$(echo $FSLIMIT | sed s/MB//g)
|
||||||
|
if (( $FSMB_FREE < $FSLIMIT ))
|
||||||
|
then
|
||||||
|
return 1 # Found out of limit
|
||||||
|
else
|
||||||
|
return 2 # Found OK
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done < $DATA_EXCEPTIONS # Feed the loop from the bottom!!!
|
||||||
|
|
||||||
|
return 3 # Not found in $EXCEPTIONS file
|
||||||
|
}
|
||||||
|
|
||||||
|
######## START OF MAIN #############
|
||||||
|
|
||||||
|
if [[ -s $EXCEPTIONS ]]
|
||||||
|
then
|
||||||
|
# Ignore all line beginning with a pound sign, #.
|
||||||
|
cat $EXCEPTIONS | grep -v "^#" > $DATA_EXCEPTIONS
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Get the data of interest by stripping out the
|
||||||
|
# /cdrom row and keeping columns 1, 4 and 6
|
||||||
|
|
||||||
|
df -k | tail +2 | egrep -v '/cdrom' \
|
||||||
|
| awk '{print $1, $4, $6}' > $WORKFILE
|
||||||
|
|
||||||
|
# Format Variables for the proper MB value
|
||||||
|
(( MIN_MB_FREE = $(echo $MIN_MB_FREE | sed s/MB//g) * 1024 ))
|
||||||
|
|
||||||
|
# Loop through each line of the file and compare column 2
|
||||||
|
|
||||||
|
while read FSDEVICE FSMB_FREE FSMOUNT
|
||||||
|
do
|
||||||
|
if [[ -s $EXCEPTIONS ]]
|
||||||
|
then
|
||||||
|
check_exceptions
|
||||||
|
RC="$?"
|
||||||
|
if [ $RC -eq 1 ] # Found out of exceptions limit
|
||||||
|
then
|
||||||
|
(( FS_FREE_OUT = $FSMB_FREE / 1000 ))
|
||||||
|
echo "$FSDEVICE mounted on $FSMOUNT only has ${FS_FREE_OUT}MB Free" \
|
||||||
|
>> $OUTFILE
|
||||||
|
elif [ $RC -eq 2 ] # Found in exceptions to be OK
|
||||||
|
then # Just a sanity check - We really do nothing here...
|
||||||
|
# The colon, :, is a NO-OP operator in KSH
|
||||||
|
|
||||||
|
: # No-Op - Do Nothing!
|
||||||
|
|
||||||
|
elif [ $RC -eq 3 ] # Not found in the exceptions file
|
||||||
|
then
|
||||||
|
FSMB_FREE=$(echo $FSMB_FREE | sed s/MB//g) # Remove the "MB"
|
||||||
|
if (( $FSMB_FREE < $MIN_MB_FREE ))
|
||||||
|
then
|
||||||
|
(( FS_FREE_OUT = $FSMB_FREE / 1000 ))
|
||||||
|
echo "$FSDEVICE mounted on $FSMOUNT only has ${FS_FREE_OUT}MB Free" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
else # No Exceptions file use the script default
|
||||||
|
FSMB_FREE=$(echo $FSMB_FREE | sed s/MB//g) # Remove the "MB"
|
||||||
|
if (( $FSMB_FREE < $MIN_MB_FREE ))
|
||||||
|
then
|
||||||
|
(( FS_FREE_OUT = $FSMB_FREE / 1000 ))
|
||||||
|
echo "$FSDEVICE mounted on $FSMOUNT only has ${FS_FREE_OUT}MB Free" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done < $WORKFILE
|
||||||
|
|
||||||
|
if [[ -s $OUTFILE ]]
|
||||||
|
then
|
||||||
|
echo "\nFull Filesystem(s) on $THISHOST\n"
|
||||||
|
cat $OUTFILE
|
||||||
|
print
|
||||||
|
fi
|
||||||
280
chapter17/fs_mon_LINUX_PC_MBFREE.ksh
Executable file
280
chapter17/fs_mon_LINUX_PC_MBFREE.ksh
Executable file
@@ -0,0 +1,280 @@
|
|||||||
|
#!/usr/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: fs_mon_LINUX_PC_MBFREE.ksh
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 08-22-2007
|
||||||
|
# REV: 4.3.P
|
||||||
|
# PURPOSE: This script is used to monitor for full filesystems,
|
||||||
|
# which is defined as "exceeding" the MAX_PERCENT value.
|
||||||
|
# A message is displayed for all "full" filesystems.
|
||||||
|
#
|
||||||
|
# PLATFORM: Linux
|
||||||
|
#
|
||||||
|
# REV LIST:
|
||||||
|
# Randy Michael - 08-27-2007
|
||||||
|
# Changed the code to use MB of free space instead of
|
||||||
|
# the %Used method.
|
||||||
|
#
|
||||||
|
# Randy Michael - 08-27-2007
|
||||||
|
# Added code to allow you to override the set script default
|
||||||
|
# for MIN_MB_FREE of FS Space
|
||||||
|
#
|
||||||
|
# Randy Michael - 08-28-2007
|
||||||
|
# Changed the code to handle both %Used and MB of Free Space.
|
||||||
|
# It does an "auto-detection" but has override capability
|
||||||
|
# of both the trigger level and the monitoring method using
|
||||||
|
# the exceptions file pointed to by the $EXCEPTIONS variable
|
||||||
|
#
|
||||||
|
# set -n # Uncomment to check syntax without any execution
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
#
|
||||||
|
##### DEFINE FILES AND VARIABLES HERE ####
|
||||||
|
|
||||||
|
WORKFILE="/tmp/df.work" # Holds filesystem data
|
||||||
|
>$WORKFILE # Initialize to empty
|
||||||
|
OUTFILE="/tmp/df.outfile" # Output display file
|
||||||
|
>$OUTFILE # Initialize to empty
|
||||||
|
EXCEPTIONS="/usr/local/bin/exceptions" # Override data file
|
||||||
|
DATA_EXCEPTIONS="/tmp/dfdata.out" # Exceptions file w/o # rows
|
||||||
|
EXCEPT_FILE="N" # Assume no $EXCEPTIONS FILE
|
||||||
|
THISHOST=`hostname` # Hostname of this machine
|
||||||
|
|
||||||
|
MIN_MB_FREE="100MB" # Min. MB of Free FS Space
|
||||||
|
MAX_PERCENT="85%" # Max. FS percentage value
|
||||||
|
FSTRIGGER="1000MB" # Trigger to switch from % Used to MB Free
|
||||||
|
|
||||||
|
|
||||||
|
###### FORMAT VARIABLES HERE ######
|
||||||
|
|
||||||
|
# Both of these variables need to multiplied by 1024 blocks
|
||||||
|
(( MIN_MB_FREE = $(echo $MIN_MB_FREE | sed s/MB//g) * 1024 ))
|
||||||
|
(( FSTRIGGER = $(echo $FSTRIGGER | sed s/MB//g) * 1024 ))
|
||||||
|
|
||||||
|
####### DEFINE FUNCTIONS HERE ########
|
||||||
|
|
||||||
|
function check_exceptions
|
||||||
|
{
|
||||||
|
# set -x # Uncomment to debug this function
|
||||||
|
|
||||||
|
while read FSNAME FSLIMIT
|
||||||
|
do
|
||||||
|
IN_FILE="N"
|
||||||
|
|
||||||
|
# Do an NFS sanity check and get rid of any ":".
|
||||||
|
# If this is found it is actaully an error entry
|
||||||
|
# but we will try to resolve it. It will only
|
||||||
|
# work if it is an NFS cross mount to the same
|
||||||
|
# mount point on both machines.
|
||||||
|
echo $FSNAME | grep ':' >/dev/null \
|
||||||
|
&& FSNAME=$(echo $FSNAME | cut -d ':' -f2)
|
||||||
|
|
||||||
|
# Check for empty and null variable
|
||||||
|
if [[ ! -z $FSLIMIT && $FSLIMIT != '' ]]
|
||||||
|
then
|
||||||
|
if [[ $FSNAME = $FSMOUNT ]] # Found it!
|
||||||
|
then
|
||||||
|
# Check for "MB" Characters...Set IN_FILE=MB
|
||||||
|
echo $FSLIMIT | grep MB >/dev/null && IN_FILE="MB" \
|
||||||
|
&& (( FSLIMIT = $(echo $FSLIMIT \
|
||||||
|
| sed s/MB//g) * 1024 ))
|
||||||
|
# check for "%" Character...Set IN_FILE=PC, for %
|
||||||
|
echo $FSLIMIT | grep "%" >/dev/null && IN_FILE="PC" \
|
||||||
|
&& FSLIMIT=$(echo $FSLIMIT | sed s/\%//g)
|
||||||
|
|
||||||
|
case $IN_FILE in
|
||||||
|
MB)
|
||||||
|
# Up-case the characters, if they exist
|
||||||
|
FSLIMIT=$(echo $FSLIMIT | tr '[a-z]' '[A-Z]')
|
||||||
|
# Get rid of the "MB" if it exists
|
||||||
|
FSLIMIT=$(echo $FSLIMIT | sed s/MB//g)
|
||||||
|
# Test for blank and null values
|
||||||
|
if [[ ! -z $FSLIMIT && $FSLIMIT != '' ]]
|
||||||
|
then
|
||||||
|
# Test for a valid filesystem "MB" limit
|
||||||
|
if (( $FSLIMIT >= 0 && $FSLIMIT < $FSSIZE ))
|
||||||
|
then
|
||||||
|
if (( $FSMB_FREE < $FSLIMIT ))
|
||||||
|
then
|
||||||
|
return 1 # Found out of limit using MB Free method
|
||||||
|
else
|
||||||
|
return 3 # Found OK
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo "\nERROR: Invalid filesystem MAX for $FSMOUNT - $FSLIMIT"
|
||||||
|
echo " Exceptions file value must be less than or"
|
||||||
|
echo " equal to the size of the filesystem measured"
|
||||||
|
echo " in 1024 bytes\n"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo "\nERROR: Null value specified in excepeptions file"
|
||||||
|
echo " for the $FSMOUNT mount point.\n"
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
PC)
|
||||||
|
# Strip out the % sign if it exists
|
||||||
|
PC_USED=$(echo $PC_USED | sed s/\%//g)
|
||||||
|
# Test for blank and null values
|
||||||
|
if [[ ! -z $FSLIMIT && $FSLIMIT != '' ]]
|
||||||
|
then
|
||||||
|
# Test for a valid percentage, i.e. 0-100
|
||||||
|
if (( $FSLIMIT >= 0 && $FSLIMIT <= 100 ))
|
||||||
|
then
|
||||||
|
if (( $PC_USED > $FSLIMIT ))
|
||||||
|
then
|
||||||
|
return 2 # Found exceeded by % Used method
|
||||||
|
else
|
||||||
|
return 3 # Found OK
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo "\nERROR: Invalid percentage for $FSMOUNT - $FSLIMIT"
|
||||||
|
echo " Exceptions file values must be"
|
||||||
|
echo " between 0 and 100%\n"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo "\nERROR: Null value specified in excepeptions file"
|
||||||
|
echo " for the $FSMOUNT mount point.\n"
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
N)
|
||||||
|
# Method Not Specified - Use Script Defaults
|
||||||
|
if (( $FSSIZE >= $FSTRIGGER ))
|
||||||
|
then # This is a "large" filesystem
|
||||||
|
if (( $FSMB_FREE < $MIN_MB_FREE ))
|
||||||
|
then
|
||||||
|
return 1 # Found out of limit using MB Free method
|
||||||
|
else
|
||||||
|
return 3 # Found OK
|
||||||
|
fi
|
||||||
|
else # This is a standard filesystem
|
||||||
|
PC_USED=$(echo $PC_USED | sed s/\%//g) # Remove the %
|
||||||
|
FSLIMIT=$(echo $FSLIMIT | sed s/\%//g) # Remove the %
|
||||||
|
if (( $PC_USED > $FSLIMIT ))
|
||||||
|
then
|
||||||
|
return 2 # Found exceeded by % Used method
|
||||||
|
else
|
||||||
|
return 3 # Found OK
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done < $DATA_EXCEPTIONS # Feed the loop from the bottom!!!
|
||||||
|
|
||||||
|
return 4 # Not found in $EXCEPTIONS file
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################
|
||||||
|
|
||||||
|
function display_output
|
||||||
|
{
|
||||||
|
if [[ -s $OUTFILE ]]
|
||||||
|
then
|
||||||
|
echo "\nFull Filesystem(s) on $THISHOST\n"
|
||||||
|
cat $OUTFILE
|
||||||
|
print
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################
|
||||||
|
|
||||||
|
function load_EXCEPTIONS_data
|
||||||
|
{
|
||||||
|
# Ingore any line that begins with a pound sign, #
|
||||||
|
|
||||||
|
cat $EXCEPTIONS | grep -v "^#" > $DATA_EXCEPTIONS
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################
|
||||||
|
|
||||||
|
function load_FS_data
|
||||||
|
{
|
||||||
|
df -k | tail +2 | egrep -v '/cdrom' \
|
||||||
|
| awk '{print $1, $2, $4, $5, $6}' > $WORKFILE
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################
|
||||||
|
######### START OF MAIN ############
|
||||||
|
####################################
|
||||||
|
|
||||||
|
load_FS_data
|
||||||
|
|
||||||
|
# Do we have a non-zero size $EXCEPTIONS file?
|
||||||
|
|
||||||
|
if [[ -s $EXCEPTIONS ]]
|
||||||
|
then # Found a non-empty $EXCEPTIONS file
|
||||||
|
|
||||||
|
load_EXCEPTIONS_data
|
||||||
|
EXCEP_FILE="Y"
|
||||||
|
fi
|
||||||
|
|
||||||
|
while read FSDEVICE FSSIZE FSMB_FREE PC_USED FSMOUNT
|
||||||
|
do
|
||||||
|
if [[ $EXCEP_FILE = "Y" ]]
|
||||||
|
then
|
||||||
|
check_exceptions
|
||||||
|
CE_RC="$?" # Check Exceptions Return Code (CE_RC)
|
||||||
|
|
||||||
|
case $CE_RC in
|
||||||
|
1) # Found exceeded in exceptions file by MB Method
|
||||||
|
(( FS_FREE_OUT = $FSMB_FREE / 1000 ))
|
||||||
|
echo "$FSDEVICE mounted on $FSMOUNT has ${FS_FREE_OUT}MB Free" \
|
||||||
|
>> $OUTFILE
|
||||||
|
;;
|
||||||
|
2) # Found exceeded in exceptions file by %Used method
|
||||||
|
echo "$FSDEVICE mount on $FSMOUNT is ${PC_USED}%" \
|
||||||
|
>> $OUTFILE
|
||||||
|
;;
|
||||||
|
3) # Found OK in exceptions file
|
||||||
|
: # NO-OP Do Nothing
|
||||||
|
;;
|
||||||
|
|
||||||
|
4) # Not found in exceptions file - Use Default Triggers
|
||||||
|
if (( $FSSIZE >= $FSTRIGGER ))
|
||||||
|
then # This is a "large" filesystem
|
||||||
|
FSMB_FREE=$(echo $FSMB_FREE | sed s/MB//g) # Remove the "MB"
|
||||||
|
if (( $FSMB_FREE < $MIN_MB_FREE ))
|
||||||
|
then
|
||||||
|
(( FS_FREE_OUT = $FSMB_FREE / 1000 ))
|
||||||
|
echo "$FSDEVICE mounted on $FSMOUNT has ${FS_FREE_OUT}MB Free" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
else # This is a standard filesystem
|
||||||
|
PC_USED=$(echo $PC_USED | sed s/\%//g)
|
||||||
|
MAX_PERCENT=$(echo $MAX_PERCENT | sed s/\%//g)
|
||||||
|
if (( $PC_USED > $MAX_PERCENT ))
|
||||||
|
then
|
||||||
|
echo "$FSDEVICE mount on $FSMOUNT is ${PC_USED}%" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
|
||||||
|
else # NO $EXECPTIONS FILE USE DEFAULT TRIGGER VALUES
|
||||||
|
|
||||||
|
if (( $FSSIZE >= $FSTRIGGER ))
|
||||||
|
then # This is a "large" filesystem - Use MB Free Method
|
||||||
|
FSMB_FREE=$(echo $FSMB_FREE | sed s/MB//g) # Remove the "MB"
|
||||||
|
if (( $FSMB_FREE < $MIN_MB_FREE ))
|
||||||
|
then
|
||||||
|
(( FS_FREE_OUT = $FSMB_FREE / 1000 ))
|
||||||
|
echo "$FSDEVICE mounted on $FSMOUNT has ${FS_FREE_OUT}MB Free" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
else # This is a standard filesystem - Use % Used Method
|
||||||
|
PC_USED=$(echo $PC_USED | sed s/\%//g)
|
||||||
|
MAX_PERCENT=$(echo $MAX_PERCENT | sed s/\%//g)
|
||||||
|
if (( $PC_USED > $MAX_PERCENT ))
|
||||||
|
then
|
||||||
|
echo "$FSDEVICE mount on $FSMOUNT is ${PC_USED}%" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done < $WORKFILE
|
||||||
|
|
||||||
|
display_output
|
||||||
|
|
||||||
|
# End of Script
|
||||||
113
chapter17/fs_mon_LINUX_excep.ksh
Executable file
113
chapter17/fs_mon_LINUX_excep.ksh
Executable file
@@ -0,0 +1,113 @@
|
|||||||
|
#!/usr/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: fs_mon_LINUX_excep.ksh
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 08-22-2007
|
||||||
|
# REV: 2.1.P
|
||||||
|
# PURPOSE: This script is used to monitor for full filesystems,
|
||||||
|
# which is defined as "exceeding" the FSMAX value.
|
||||||
|
# A message is displayed for all "full" filesystems.
|
||||||
|
#
|
||||||
|
# PLATFORM: Linux
|
||||||
|
#
|
||||||
|
# REV LIST:
|
||||||
|
# 08-23-2007 - Randy Michael
|
||||||
|
# Added code to override the default FSMAX script threshold
|
||||||
|
# using an "exceptions" file, defined by the $EXCEPTIONS
|
||||||
|
# variable, that list /mount_point and NEW_MAX%
|
||||||
|
#
|
||||||
|
# set -n # Uncomment to check syntax without any execution
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
#
|
||||||
|
##### DEFINE FILES AND VARIABLES HERE ####
|
||||||
|
|
||||||
|
WORKFILE="/tmp/df.work" # Holds filesystem data
|
||||||
|
>$WORKFILE # Initialize to empty
|
||||||
|
OUTFILE="/tmp/df.outfile" # Output display file
|
||||||
|
>$OUTFILE # Initialize to empty
|
||||||
|
BINDIR="/usr/local/bin" # Local bin directory
|
||||||
|
THISHOST=`hostname` # Hostname of this machine
|
||||||
|
FSMAX="85" # Max. FS percentage value
|
||||||
|
EXCEPTIONS="${BINDIR}/exceptions" # Overrides $FSMAX
|
||||||
|
|
||||||
|
####### DEFINE FUNCTIONS HERE #####
|
||||||
|
|
||||||
|
function check_exceptions
|
||||||
|
{
|
||||||
|
|
||||||
|
# set -x # Uncomment to debug this function
|
||||||
|
|
||||||
|
# Define a data file
|
||||||
|
|
||||||
|
DATA_EXCEPTIONS="/tmp/dfdata.out"
|
||||||
|
|
||||||
|
# Ingore any line that begins with a pound sign, #
|
||||||
|
|
||||||
|
cat $EXCEPTIONS | grep -v "^#" > $DATA_EXCEPTIONS
|
||||||
|
|
||||||
|
while read FSNAME NEW_MAX # Feeding Ddata from Bottom of Loop!!!
|
||||||
|
do
|
||||||
|
if [[ $FSNAME = $FSMOUNT ]] # Correct /mount_point?
|
||||||
|
then # Get rid of the % sign, if it exists!
|
||||||
|
NEW_MAX=$(echo $NEW_MAX | sed s/\%//g)
|
||||||
|
|
||||||
|
if [ $FSVALUE -gt $NEW_MAX ]
|
||||||
|
then # Over Limit...Return a "0", zero
|
||||||
|
return 0 # FOUND MAX OUT - Return 0
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
done < $DATA_EXCEPTIONS # Feed from the bottom of the loop!!
|
||||||
|
|
||||||
|
return 1 # Not found in File
|
||||||
|
}
|
||||||
|
|
||||||
|
######## START OF MAIN #############
|
||||||
|
|
||||||
|
# Get the data of interest by stripping out the
|
||||||
|
# /cdrom row and keeping columns 1, 5 and 6
|
||||||
|
|
||||||
|
df -k | tail +2 | egrep -v '/cdrom' \
|
||||||
|
| awk '{print $1, $5, $6}' > $WORKFILE
|
||||||
|
|
||||||
|
# Loop through each line of the file and compare column 2
|
||||||
|
|
||||||
|
while read FSDEVICE FSVALUE FSMOUNT
|
||||||
|
do
|
||||||
|
# Strip out the % sign if it exists
|
||||||
|
FSVALUE=$(echo $FSVALUE | sed s/\%//g) # Remove the % sign
|
||||||
|
if [[ -s $EXCEPTIONS ]] # Do we have a non-empty file?
|
||||||
|
then # Found it!
|
||||||
|
|
||||||
|
# Look for the current $FSMOUNT value in the file
|
||||||
|
# using the check_exceptions function defined above.
|
||||||
|
|
||||||
|
check_exceptions
|
||||||
|
if [ $? -eq 0 ] # Found it Exceeded!!
|
||||||
|
then
|
||||||
|
echo "$FSDEVICE mount on $FSMOUNT is ${FSVALUE}%" \
|
||||||
|
>> $OUTFILE
|
||||||
|
else # Not exceeded in the file use the script default
|
||||||
|
if [ $FSVALUE -gt $FSMAX ] # Use Script Default
|
||||||
|
then
|
||||||
|
echo "$FSDEVICE mount on $FSMOUNT is ${FSVALUE}%" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
else # No exceptions file use the script default
|
||||||
|
if [ $FSVALUE -gt $FSMAX ] # Use Script Default
|
||||||
|
then
|
||||||
|
echo "$FSDEVICE mount on $FSMOUNT is ${FSVALUE}%" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done < $WORKFILE # Feed the while loop from the bottom...
|
||||||
|
|
||||||
|
# Display output if anything is exceeded...
|
||||||
|
|
||||||
|
if [[ -s $OUTFILE ]]
|
||||||
|
then
|
||||||
|
echo "\nFull Filesystem(s) on $THISHOST\n"
|
||||||
|
cat $OUTFILE
|
||||||
|
print
|
||||||
|
fi
|
||||||
57
chapter17/fs_mon_MBFREE.ksh
Executable file
57
chapter17/fs_mon_MBFREE.ksh
Executable file
@@ -0,0 +1,57 @@
|
|||||||
|
#!/usr/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: fs_mon.ksh
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 08-22-2007
|
||||||
|
# REV: 1.5.P
|
||||||
|
# PURPOSE: This script is used to monitor for full filesystems,
|
||||||
|
# which is defined as "exceeding" the FSMAX value.
|
||||||
|
# A message is displayed for all "full" filesystems.
|
||||||
|
#
|
||||||
|
# REV LIST:
|
||||||
|
# Randy Michael - 08-27-2007
|
||||||
|
# Changed the code to use MB of free space instead of
|
||||||
|
# the %Used method.
|
||||||
|
#
|
||||||
|
# set -n # Uncomment to check syntax without any execution
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
#
|
||||||
|
##### DEFINE FILES AND VARIABLES HERE ####
|
||||||
|
|
||||||
|
WORKFILE="/tmp/df.work" # Holds filesystem data
|
||||||
|
>$WORKFILE # Initialize to empty
|
||||||
|
OUTFILE="/tmp/df.outfile" # Output display file
|
||||||
|
>$OUTFILE # Initialize to empty
|
||||||
|
THISHOST=`hostname` # Hostname of this machine
|
||||||
|
MIN_MB_FREE="50MB" # Min. MB of Free FS Space
|
||||||
|
|
||||||
|
######## START OF MAIN #############
|
||||||
|
|
||||||
|
# Get the data of interest by stripping out /dev/cd#,
|
||||||
|
# /proc rows and keeping columns 1, 4 and 7
|
||||||
|
|
||||||
|
df -k | tail +2 | egrep -v '/dev/cd[0-9] | /proc' \
|
||||||
|
| awk '{print $1, $3, $7}' > $WORKFILE
|
||||||
|
|
||||||
|
# Format Variables
|
||||||
|
(( MIN_MB_FREE = $(echo $MIN_MB_FREE | sed s/MB//g) * 1024 ))
|
||||||
|
|
||||||
|
# Loop through each line of the file and compare column 2
|
||||||
|
|
||||||
|
while read FSDEVICE FSMB_FREE FSMOUNT
|
||||||
|
do
|
||||||
|
FSMB_FREE=$(echo $FSMB_FREE | sed s/MB//g) # Remove the % sign
|
||||||
|
if (( $FSMB_FREE < $MIN_MB_FREE ))
|
||||||
|
then
|
||||||
|
(( FS_FREE_OUT = $FSMB_FREE / 1000 ))
|
||||||
|
echo "$FSDEVICE mounted on $FSMOUNT only has ${FS_FREE_OUT}MB Free" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
done < $WORKFILE
|
||||||
|
|
||||||
|
if [[ -s $OUTFILE ]]
|
||||||
|
then
|
||||||
|
echo "\nFull Filesystem(s) on $THISHOST\n"
|
||||||
|
cat $OUTFILE
|
||||||
|
print
|
||||||
|
fi
|
||||||
127
chapter17/fs_mon_MBFREE_excep.ksh
Executable file
127
chapter17/fs_mon_MBFREE_excep.ksh
Executable file
@@ -0,0 +1,127 @@
|
|||||||
|
#!/usr/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: fs_mon.ksh
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 08-22-2007
|
||||||
|
# REV: 2.1.P
|
||||||
|
# PURPOSE: This script is used to monitor for full filesystems,
|
||||||
|
# which is defined as "exceeding" the FSMAX value.
|
||||||
|
# A message is displayed for all "full" filesystems.
|
||||||
|
#
|
||||||
|
# REV LIST:
|
||||||
|
# Randy Michael - 08-27-2007
|
||||||
|
# Changed the code to use MB of free space instead of
|
||||||
|
# the %Used method.
|
||||||
|
#
|
||||||
|
# Randy Michael - 08-27-2007
|
||||||
|
# Added code to allow you to override the set script default
|
||||||
|
# for MIN_MB_FREE of FS Space
|
||||||
|
#
|
||||||
|
# set -n # Uncomment to check syntax without any execution
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
#
|
||||||
|
##### DEFINE FILES AND VARIABLES HERE ####
|
||||||
|
|
||||||
|
WORKFILE="/tmp/df.work" # Holds filesystem data
|
||||||
|
>$WORKFILE # Initialize to empty
|
||||||
|
OUTFILE="/tmp/df.outfile" # Output display file
|
||||||
|
>$OUTFILE # Initialize to empty
|
||||||
|
EXCEPTIONS="/usr/local/bin/exceptions" # Override data file
|
||||||
|
DATA_EXCEPTIONS="/tmp/dfdata.out" # Exceptions file w/o # rows
|
||||||
|
THISHOST=`hostname` # Hostname of this machine
|
||||||
|
MIN_MB_FREE="50MB" # Min. MB of Free FS Space
|
||||||
|
|
||||||
|
####### DEFINE FUNCTIONS HERE ########
|
||||||
|
|
||||||
|
function check_exceptions
|
||||||
|
{
|
||||||
|
# set -x # Uncomment to debug this function
|
||||||
|
|
||||||
|
while read FSNAME FSLIMIT
|
||||||
|
do
|
||||||
|
# Do an NFS sanity check
|
||||||
|
echo $FSNAME | grep ":" >/dev/null \
|
||||||
|
&& FSNAME=$(echo $FSNAME | cut -d ":" -f2)
|
||||||
|
if [[ ! -z $FSLIMIT && $FSLIMIT != '' ]]
|
||||||
|
then
|
||||||
|
(( FSLIMIT = $(echo $FSLIMIT | sed s/MB//g) * 1024 ))
|
||||||
|
if [[ $FSNAME = $FSMOUNT ]]
|
||||||
|
then
|
||||||
|
# Get rid of the "MB" if it exists
|
||||||
|
FSLIMIT=$(echo $FSLIMIT | sed s/MB//g)
|
||||||
|
if (( $FSMB_FREE < $FSLIMIT ))
|
||||||
|
then
|
||||||
|
return 1 # Found out of limit
|
||||||
|
else
|
||||||
|
return 2 # Found OK
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done < $DATA_EXCEPTIONS # Feed the loop from the bottom!!!
|
||||||
|
|
||||||
|
return 3 # Not found in $EXCEPTIONS file
|
||||||
|
}
|
||||||
|
|
||||||
|
######## START OF MAIN #############
|
||||||
|
|
||||||
|
if [[ -s $EXCEPTIONS ]]
|
||||||
|
then
|
||||||
|
# Ignore all line beginning with a pound sign, #.
|
||||||
|
cat $EXCEPTIONS | grep -v "^#" > $DATA_EXCEPTIONS
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Get the data of interest by stripping out /dev/cd#,
|
||||||
|
# /proc rows and keeping columns 1, 4 and 7
|
||||||
|
|
||||||
|
df -k | tail +2 | egrep -v '/dev/cd[0-9] | /proc' \
|
||||||
|
| awk '{print $1, $3, $7}' > $WORKFILE
|
||||||
|
|
||||||
|
# Format Variables for the proper MB value
|
||||||
|
(( MIN_MB_FREE = $(echo $MIN_MB_FREE | sed s/MB//g) * 1024 ))
|
||||||
|
|
||||||
|
# Loop through each line of the file and compare column 2
|
||||||
|
|
||||||
|
while read FSDEVICE FSMB_FREE FSMOUNT
|
||||||
|
do
|
||||||
|
if [[ -s $EXCEPTIONS ]]
|
||||||
|
then
|
||||||
|
check_exceptions
|
||||||
|
RC="$?"
|
||||||
|
if [ $RC -eq 1 ] # Found out of exceptions limit
|
||||||
|
then
|
||||||
|
(( FS_FREE_OUT = $FSMB_FREE / 1000 ))
|
||||||
|
echo "$FSDEVICE mounted on $FSMOUNT only has ${FS_FREE_OUT}MB Free" \
|
||||||
|
>> $OUTFILE
|
||||||
|
elif [ $RC -eq 2 ] # Found in exceptions to be OK
|
||||||
|
then # Just a sanity check - We really do nothing here...
|
||||||
|
# The colon, :, is a NO-OP operator in KSH
|
||||||
|
|
||||||
|
: # No-Op - Do Nothing!
|
||||||
|
|
||||||
|
elif [ $RC -eq 3 ] # Not found in the exceptions file
|
||||||
|
then
|
||||||
|
FSMB_FREE=$(echo $FSMB_FREE | sed s/MB//g) # Remove the "MB"
|
||||||
|
if (( $FSMB_FREE < $MIN_MB_FREE ))
|
||||||
|
then
|
||||||
|
(( FS_FREE_OUT = $FSMB_FREE / 1000 ))
|
||||||
|
echo "$FSDEVICE mounted on $FSMOUNT only has ${FS_FREE_OUT}MB Free" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
else # No Exceptions file use the script default
|
||||||
|
FSMB_FREE=$(echo $FSMB_FREE | sed s/MB//g) # Remove the "MB"
|
||||||
|
if (( $FSMB_FREE < $MIN_MB_FREE ))
|
||||||
|
then
|
||||||
|
(( FS_FREE_OUT = $FSMB_FREE / 1000 ))
|
||||||
|
echo "$FSDEVICE mounted on $FSMOUNT only has ${FS_FREE_OUT}MB Free" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done < $WORKFILE
|
||||||
|
|
||||||
|
if [[ -s $OUTFILE ]]
|
||||||
|
then
|
||||||
|
echo "\nFull Filesystem(s) on $THISHOST\n"
|
||||||
|
cat $OUTFILE
|
||||||
|
print
|
||||||
|
fi
|
||||||
52
chapter17/fs_mon_SUNOS.ksh
Executable file
52
chapter17/fs_mon_SUNOS.ksh
Executable file
@@ -0,0 +1,52 @@
|
|||||||
|
#!/usr/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: fs_mon_SUNOS.ksh
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 08-22-2007
|
||||||
|
# REV: 1.1.P
|
||||||
|
# PURPOSE: This script is used to monitor for full filesystems,
|
||||||
|
# which is defined as "exceeding" the FSMAX value.
|
||||||
|
# A message is displayed for all "full" filesystems.
|
||||||
|
#
|
||||||
|
# PLATFORM: SUN Solaris
|
||||||
|
#
|
||||||
|
# REV LIST:
|
||||||
|
#
|
||||||
|
# set -n # Uncomment to check syntax without any execution
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
#
|
||||||
|
##### DEFINE FILES AND VARIABLES HERE ####
|
||||||
|
|
||||||
|
WORKFILE="/tmp/df.work" # Holds filesystem data
|
||||||
|
>$WORKFILE # Initialize to empty
|
||||||
|
OUTFILE="/tmp/df.outfile" # Output display file
|
||||||
|
>$OUTFILE # Initialize to empty
|
||||||
|
THISHOST=`hostname` # Hostname of this machine
|
||||||
|
FSMAX="85" # Max. FS percentage value
|
||||||
|
|
||||||
|
######## START OF MAIN #############
|
||||||
|
|
||||||
|
# Get the data of interest by stripping out rows
|
||||||
|
# to be ignored and keeping columns 1, 5 and 6
|
||||||
|
|
||||||
|
df -k | tail +2 | egrep -v '/dev/fd|/etc/mnttab|/proc|/cdrom' \
|
||||||
|
| awk '{print $1, $5, $6}' > $WORKFILE
|
||||||
|
|
||||||
|
# Loop through each line of the file and compare column 2
|
||||||
|
|
||||||
|
while read FSDEVICE FSVALUE FSMOUNT
|
||||||
|
do
|
||||||
|
FSVALUE=$(echo $FSVALUE | sed s/\%//g) # Remove the % sign
|
||||||
|
if [ $FSVALUE -gt $FSMAX ]
|
||||||
|
then
|
||||||
|
echo "$FSDEVICE mounted on $FSMOUNT is ${FSVALUE}%" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
done < $WORKFILE
|
||||||
|
|
||||||
|
if [[ -s $OUTFILE ]]
|
||||||
|
then
|
||||||
|
echo "\nFull Filesystem(s) on $THISHOST\n"
|
||||||
|
cat $OUTFILE
|
||||||
|
print
|
||||||
|
fi
|
||||||
59
chapter17/fs_mon_SUNOS_MBFREE.ksh
Executable file
59
chapter17/fs_mon_SUNOS_MBFREE.ksh
Executable file
@@ -0,0 +1,59 @@
|
|||||||
|
#!/usr/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: fs_mon_SUNOS_MBFREE.ksh
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 08-22-2007
|
||||||
|
# REV: 1.5.P
|
||||||
|
# PURPOSE: This script is used to monitor for full filesystems,
|
||||||
|
# which is defined as "exceeding" the FSMAX value.
|
||||||
|
# A message is displayed for all "full" filesystems.
|
||||||
|
#
|
||||||
|
# PLATFORM: SUN Solaris
|
||||||
|
#
|
||||||
|
# REV LIST:
|
||||||
|
# Randy Michael - 08-27-2007
|
||||||
|
# Changed the code to use MB of free space instead of
|
||||||
|
# the %Used method.
|
||||||
|
#
|
||||||
|
# set -n # Uncomment to check syntax without any execution
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
#
|
||||||
|
##### DEFINE FILES AND VARIABLES HERE ####
|
||||||
|
|
||||||
|
WORKFILE="/tmp/df.work" # Holds filesystem data
|
||||||
|
>$WORKFILE # Initialize to empty
|
||||||
|
OUTFILE="/tmp/df.outfile" # Output display file
|
||||||
|
>$OUTFILE # Initialize to empty
|
||||||
|
THISHOST=`hostname` # Hostname of this machine
|
||||||
|
MIN_MB_FREE="50MB" # Min. MB of Free FS Space
|
||||||
|
|
||||||
|
######## START OF MAIN #############
|
||||||
|
|
||||||
|
# Get the data of interest by stripping out the rows
|
||||||
|
# to not monitor and keeping columns 1, 4 and 6
|
||||||
|
|
||||||
|
df -k | tail +2 | egrep -v 'fd|mnttab|proc|cdrom|objfs' \
|
||||||
|
| awk '{print $1, $4, $6}' > $WORKFILE
|
||||||
|
|
||||||
|
# Format Variables
|
||||||
|
(( MIN_MB_FREE = $(echo $MIN_MB_FREE | sed s/MB//g) * 1024 ))
|
||||||
|
|
||||||
|
# Loop through each line of the file and compare column 2
|
||||||
|
|
||||||
|
while read FSDEVICE FSMB_FREE FSMOUNT
|
||||||
|
do
|
||||||
|
FSMB_FREE=$(echo $FSMB_FREE | sed s/MB//g) # Remove the "MB"
|
||||||
|
if (( $FSMB_FREE < $MIN_MB_FREE ))
|
||||||
|
then
|
||||||
|
(( FS_FREE_OUT = $FSMB_FREE / 1000 ))
|
||||||
|
echo "$FSDEVICE mounted on $FSMOUNT has ${FS_FREE_OUT}MB Free" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
done < $WORKFILE
|
||||||
|
|
||||||
|
if [[ -s $OUTFILE ]]
|
||||||
|
then
|
||||||
|
echo "\nFull Filesystem(s) on $THISHOST\n"
|
||||||
|
cat $OUTFILE
|
||||||
|
print
|
||||||
|
fi
|
||||||
129
chapter17/fs_mon_SUNOS_MBFREE_excep.ksh
Executable file
129
chapter17/fs_mon_SUNOS_MBFREE_excep.ksh
Executable file
@@ -0,0 +1,129 @@
|
|||||||
|
#!/usr/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: fs_mon_SUNOS_MBFREE_excep.ksh
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 08-22-2007
|
||||||
|
# REV: 2.1.P
|
||||||
|
# PURPOSE: This script is used to monitor for full filesystems,
|
||||||
|
# which is defined as "exceeding" the $MIN_MB_FREE value.
|
||||||
|
# A message is displayed for all "full" filesystems.
|
||||||
|
#
|
||||||
|
# PLATFORM: SUN Solaris
|
||||||
|
#
|
||||||
|
# REV LIST:
|
||||||
|
# Randy Michael - 08-27-2007
|
||||||
|
# Changed the code to use MB of free space instead of
|
||||||
|
# the %Used method.
|
||||||
|
#
|
||||||
|
# Randy Michael - 08-27-2007
|
||||||
|
# Added code to allow you to override the set script default
|
||||||
|
# for MIN_MB_FREE of FS Space
|
||||||
|
#
|
||||||
|
# set -n # Uncomment to check syntax without any execution
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
#
|
||||||
|
##### DEFINE FILES AND VARIABLES HERE ####
|
||||||
|
|
||||||
|
WORKFILE="/tmp/df.work" # Holds filesystem data
|
||||||
|
>$WORKFILE # Initialize to empty
|
||||||
|
OUTFILE="/tmp/df.outfile" # Output display file
|
||||||
|
>$OUTFILE # Initialize to empty
|
||||||
|
EXCEPTIONS="/usr/local/bin/exceptions" # Override data file
|
||||||
|
DATA_EXCEPTIONS="/tmp/dfdata.out" # Exceptions file w/o # rows
|
||||||
|
THISHOST=`hostname` # Hostname of this machine
|
||||||
|
MIN_MB_FREE="50MB" # Min. MB of Free FS Space
|
||||||
|
|
||||||
|
####### DEFINE FUNCTIONS HERE ########
|
||||||
|
|
||||||
|
function check_exceptions
|
||||||
|
{
|
||||||
|
# set -x # Uncomment to debug this function
|
||||||
|
|
||||||
|
while read FSNAME FSLIMIT
|
||||||
|
do
|
||||||
|
# Do an NFS sanity check
|
||||||
|
echo $FSNAME | grep ":" >/dev/null \
|
||||||
|
&& FSNAME=$(echo $FSNAME | cut -d ":" -f2)
|
||||||
|
if [[ ! -z "$FSLIMIT" && "$FSLIMIT" != '' ]]
|
||||||
|
then
|
||||||
|
(( FSLIMIT = $(echo $FSLIMIT | sed s/MB//g) * 1024 ))
|
||||||
|
if [[ $FSNAME = $FSMOUNT ]]
|
||||||
|
then
|
||||||
|
# Get rid of the "MB" if it exists
|
||||||
|
FSLIMIT=$(echo $FSLIMIT | sed s/MB//g)
|
||||||
|
if (( $FSMB_FREE < $FSLIMIT ))
|
||||||
|
then
|
||||||
|
return 1 # Found out of limit
|
||||||
|
else
|
||||||
|
return 2 # Found OK
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done < $DATA_EXCEPTIONS # Feed the loop from the bottom!!!
|
||||||
|
|
||||||
|
return 3 # Not found in $EXCEPTIONS file
|
||||||
|
}
|
||||||
|
|
||||||
|
######## START OF MAIN #############
|
||||||
|
|
||||||
|
if [[ -s $EXCEPTIONS ]]
|
||||||
|
then
|
||||||
|
# Ignore all line beginning with a pound sign, #.
|
||||||
|
cat $EXCEPTIONS | grep -v "^#" > $DATA_EXCEPTIONS
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Get the data of interest by stripping out rows that
|
||||||
|
# are not monitored and keeping columns 1, 4 and 6
|
||||||
|
|
||||||
|
df -k | tail +2 | egrep -v '/dev/fd|/etc/mnttab|/proc|/cdrom' \
|
||||||
|
| awk '{print $1, $4, $6}' > $WORKFILE
|
||||||
|
|
||||||
|
# Format Variables for the proper MB value
|
||||||
|
(( MIN_MB_FREE = $(echo $MIN_MB_FREE | sed s/MB//g) * 1024 ))
|
||||||
|
|
||||||
|
# Loop through each line of the file and compare column 2
|
||||||
|
|
||||||
|
while read FSDEVICE FSMB_FREE FSMOUNT
|
||||||
|
do
|
||||||
|
if [[ -s $EXCEPTIONS ]]
|
||||||
|
then
|
||||||
|
check_exceptions
|
||||||
|
RC="$?"
|
||||||
|
if [ $RC -eq 1 ] # Found out of exceptions limit
|
||||||
|
then
|
||||||
|
(( FS_FREE_OUT = $FSMB_FREE / 1000 ))
|
||||||
|
echo "$FSDEVICE mounted on $FSMOUNT only has ${FS_FREE_OUT}MB Free" \
|
||||||
|
>> $OUTFILE
|
||||||
|
elif [ $RC -eq 2 ] # Found in exceptions to be OK
|
||||||
|
then # Just a sanity check - We really do nothing here...
|
||||||
|
# The colon, :, is a NO-OP operator in KSH
|
||||||
|
|
||||||
|
: # No-Op - Do Nothing!
|
||||||
|
|
||||||
|
elif [ $RC -eq 3 ] # Not found in the exceptions file
|
||||||
|
then
|
||||||
|
FSMB_FREE=$(echo $FSMB_FREE | sed s/MB//g) # Remove the "MB"
|
||||||
|
if (( $FSMB_FREE < $MIN_MB_FREE ))
|
||||||
|
then
|
||||||
|
(( FS_FREE_OUT = $FSMB_FREE / 1000 ))
|
||||||
|
echo "$FSDEVICE mounted on $FSMOUNT only has ${FS_FREE_OUT}MB Free" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
else # No Exceptions file use the script default
|
||||||
|
FSMB_FREE=$(echo $FSMB_FREE | sed s/MB//g) # Remove the "MB"
|
||||||
|
if (( $FSMB_FREE < $MIN_MB_FREE ))
|
||||||
|
then
|
||||||
|
(( FS_FREE_OUT = $FSMB_FREE / 1000 ))
|
||||||
|
echo "$FSDEVICE mounted on $FSMOUNT only has ${FS_FREE_OUT}MB Free" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done < $WORKFILE
|
||||||
|
|
||||||
|
if [[ -s $OUTFILE ]]
|
||||||
|
then
|
||||||
|
echo "\nFull Filesystem(s) on $THISHOST\n"
|
||||||
|
cat $OUTFILE
|
||||||
|
print
|
||||||
|
fi
|
||||||
280
chapter17/fs_mon_SUNOS_PC_MBFREE.ksh
Executable file
280
chapter17/fs_mon_SUNOS_PC_MBFREE.ksh
Executable file
@@ -0,0 +1,280 @@
|
|||||||
|
#!/usr/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: fs_mon_SUNOS_PC_MBFREE.ksh
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 08-22-2007
|
||||||
|
# REV: 4.3.P
|
||||||
|
# PURPOSE: This script is used to monitor for full filesystems,
|
||||||
|
# which is defined as "exceeding" the MAX_PERCENT value.
|
||||||
|
# A message is displayed for all "full" filesystems.
|
||||||
|
#
|
||||||
|
# PLATFORM: SUN Solaris
|
||||||
|
#
|
||||||
|
# REV LIST:
|
||||||
|
# Randy Michael - 08-27-2007
|
||||||
|
# Changed the code to use MB of free space instead of
|
||||||
|
# the %Used method.
|
||||||
|
#
|
||||||
|
# Randy Michael - 08-27-2007
|
||||||
|
# Added code to allow you to override the set script default
|
||||||
|
# for MIN_MB_FREE of FS Space
|
||||||
|
#
|
||||||
|
# Randy Michael - 08-28-2007
|
||||||
|
# Changed the code to handle both %Used and MB of Free Space.
|
||||||
|
# It does an "auto-detection" but has override capability
|
||||||
|
# of both the trigger level and the monitoring method using
|
||||||
|
# the exceptions file pointed to by the $EXCEPTIONS variable
|
||||||
|
#
|
||||||
|
# set -n # Uncomment to check syntax without any execution
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
#
|
||||||
|
##### DEFINE FILES AND VARIABLES HERE ####
|
||||||
|
|
||||||
|
WORKFILE="/tmp/df.work" # Holds filesystem data
|
||||||
|
>$WORKFILE # Initialize to empty
|
||||||
|
OUTFILE="/tmp/df.outfile" # Output display file
|
||||||
|
>$OUTFILE # Initialize to empty
|
||||||
|
EXCEPTIONS="/usr/local/bin/exceptions" # Override data file
|
||||||
|
DATA_EXCEPTIONS="/tmp/dfdata.out" # Exceptions file w/o # rows
|
||||||
|
EXCEPT_FILE="N" # Assume no $EXCEPTIONS FILE
|
||||||
|
THISHOST=`hostname` # Hostname of this machine
|
||||||
|
|
||||||
|
MIN_MB_FREE="100MB" # Min. MB of Free FS Space
|
||||||
|
MAX_PERCENT="85%" # Max. FS percentage value
|
||||||
|
FSTRIGGER="1000MB" # Trigger to switch from % Used to MB Free
|
||||||
|
|
||||||
|
|
||||||
|
###### FORMAT VARIABLES HERE ######
|
||||||
|
|
||||||
|
# Both of these variables need to multiplied by 1024 blocks
|
||||||
|
(( MIN_MB_FREE = $(echo $MIN_MB_FREE | sed s/MB//g) * 1024 ))
|
||||||
|
(( FSTRIGGER = $(echo $FSTRIGGER | sed s/MB//g) * 1024 ))
|
||||||
|
|
||||||
|
####### DEFINE FUNCTIONS HERE ########
|
||||||
|
|
||||||
|
function check_exceptions
|
||||||
|
{
|
||||||
|
# set -x # Uncomment to debug this function
|
||||||
|
|
||||||
|
while read FSNAME FSLIMIT
|
||||||
|
do
|
||||||
|
IN_FILE="N"
|
||||||
|
|
||||||
|
# Do an NFS sanity check and get rid of any ":".
|
||||||
|
# If this is found it is actaully an error entry
|
||||||
|
# but we will try to resolve it. It will only
|
||||||
|
# work if it is an NFS cross mount to the same
|
||||||
|
# mount point on both machines.
|
||||||
|
echo $FSNAME | grep ':' >/dev/null \
|
||||||
|
&& FSNAME=$(echo $FSNAME | cut -d ':' -f2)
|
||||||
|
|
||||||
|
# Check for empty and null variable
|
||||||
|
if [[ ! -z $FSLIMIT && $FSLIMIT != '' ]]
|
||||||
|
then
|
||||||
|
if [[ $FSNAME = $FSMOUNT ]] # Found it!
|
||||||
|
then
|
||||||
|
# Check for "MB" Characters...Set IN_FILE=MB
|
||||||
|
echo $FSLIMIT | grep MB >/dev/null && IN_FILE="MB" \
|
||||||
|
&& (( FSLIMIT = $(echo $FSLIMIT \
|
||||||
|
| sed s/MB//g) * 1024 ))
|
||||||
|
# check for "%" Character...Set IN_FILE=PC, for %
|
||||||
|
echo $FSLIMIT | grep "%" >/dev/null && IN_FILE="PC" \
|
||||||
|
&& FSLIMIT=$(echo $FSLIMIT | sed s/\%//g)
|
||||||
|
|
||||||
|
case $IN_FILE in
|
||||||
|
MB)
|
||||||
|
# Up-case the characters, if they exist
|
||||||
|
FSLIMIT=$(echo $FSLIMIT | tr '[a-z]' '[A-Z]')
|
||||||
|
# Get rid of the "MB" if it exists
|
||||||
|
FSLIMIT=$(echo $FSLIMIT | sed s/MB//g)
|
||||||
|
# Test for blank and null values
|
||||||
|
if [[ ! -z $FSLIMIT && $FSLIMIT != '' ]]
|
||||||
|
then
|
||||||
|
# Test for a valid filesystem "MB" limit
|
||||||
|
if (( $FSLIMIT >= 0 && $FSLIMIT < $FSSIZE ))
|
||||||
|
then
|
||||||
|
if (( $FSMB_FREE < $FSLIMIT ))
|
||||||
|
then
|
||||||
|
return 1 # Found out of limit using MB Free method
|
||||||
|
else
|
||||||
|
return 3 # Found OK
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo "\nERROR: Invalid filesystem MAX for $FSMOUNT - $FSLIMIT"
|
||||||
|
echo " Exceptions file value must be less than or"
|
||||||
|
echo " equal to the size of the filesystem measured"
|
||||||
|
echo " in 1024 bytes\n"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo "\nERROR: Null value specified in excepeptions file"
|
||||||
|
echo " for the $FSMOUNT mount point.\n"
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
PC)
|
||||||
|
# Strip out the % sign if it exists
|
||||||
|
PC_USED=$(echo $PC_USED | sed s/\%//g)
|
||||||
|
# Test for blank and null values
|
||||||
|
if [[ ! -z $FSLIMIT && $FSLIMIT != '' ]]
|
||||||
|
then
|
||||||
|
# Test for a valid percentage, i.e. 0-100
|
||||||
|
if (( $FSLIMIT >= 0 && $FSLIMIT <= 100 ))
|
||||||
|
then
|
||||||
|
if (( $PC_USED > $FSLIMIT ))
|
||||||
|
then
|
||||||
|
return 2 # Found exceeded by % Used method
|
||||||
|
else
|
||||||
|
return 3 # Found OK
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo "\nERROR: Invalid percentage for $FSMOUNT - $FSLIMIT"
|
||||||
|
echo " Exceptions file values must be"
|
||||||
|
echo " between 0 and 100%\n"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo "\nERROR: Null value specified in excepeptions file"
|
||||||
|
echo " for the $FSMOUNT mount point.\n"
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
N)
|
||||||
|
# Method Not Specified - Use Script Defaults
|
||||||
|
if (( $FSSIZE >= $FSTRIGGER ))
|
||||||
|
then # This is a "large" filesystem
|
||||||
|
if (( $FSMB_FREE < $MIN_MB_FREE ))
|
||||||
|
then
|
||||||
|
return 1 # Found out of limit using MB Free method
|
||||||
|
else
|
||||||
|
return 3 # Found OK
|
||||||
|
fi
|
||||||
|
else # This is a standard filesystem
|
||||||
|
PC_USED=$(echo $PC_USED | sed s/\%//g) # Remove the %
|
||||||
|
FSLIMIT=$(echo $FSLIMIT | sed s/\%//g) # Remove the %
|
||||||
|
if (( $PC_USED > $FSLIMIT ))
|
||||||
|
then
|
||||||
|
return 2 # Found exceeded by % Used method
|
||||||
|
else
|
||||||
|
return 3 # Found OK
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done < $DATA_EXCEPTIONS # Feed the loop from the bottom!!!
|
||||||
|
|
||||||
|
return 4 # Not found in $EXCEPTIONS file
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################
|
||||||
|
|
||||||
|
function display_output
|
||||||
|
{
|
||||||
|
if [[ -s $OUTFILE ]]
|
||||||
|
then
|
||||||
|
echo "\nFull Filesystem(s) on $THISHOST\n"
|
||||||
|
cat $OUTFILE
|
||||||
|
print
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################
|
||||||
|
|
||||||
|
function load_EXCEPTIONS_data
|
||||||
|
{
|
||||||
|
# Ingore any line that begins with a pound sign, #
|
||||||
|
|
||||||
|
cat $EXCEPTIONS | grep -v "^#" > $DATA_EXCEPTIONS
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################
|
||||||
|
|
||||||
|
function load_FS_data
|
||||||
|
{
|
||||||
|
df -k | tail +2 | egrep -v '/dev/fd|/etc/mnttab|/proc|/cdrom' \
|
||||||
|
| awk '{print $1, $2, $4, $5, $6}' > $WORKFILE
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################
|
||||||
|
######### START OF MAIN ############
|
||||||
|
####################################
|
||||||
|
|
||||||
|
load_FS_data
|
||||||
|
|
||||||
|
# Do we have a non-zero size $EXCEPTIONS file?
|
||||||
|
|
||||||
|
if [[ -s $EXCEPTIONS ]]
|
||||||
|
then # Found a non-empty $EXCEPTIONS file
|
||||||
|
|
||||||
|
load_EXCEPTIONS_data
|
||||||
|
EXCEP_FILE="Y"
|
||||||
|
fi
|
||||||
|
|
||||||
|
while read FSDEVICE FSSIZE FSMB_FREE PC_USED FSMOUNT
|
||||||
|
do
|
||||||
|
if [[ $EXCEP_FILE = "Y" ]]
|
||||||
|
then
|
||||||
|
check_exceptions
|
||||||
|
CE_RC="$?" # Check Exceptions Return Code (CE_RC)
|
||||||
|
|
||||||
|
case $CE_RC in
|
||||||
|
1) # Found exceeded in exceptions file by MB Method
|
||||||
|
(( FS_FREE_OUT = $FSMB_FREE / 1000 ))
|
||||||
|
echo "$FSDEVICE mounted on $FSMOUNT has ${FS_FREE_OUT}MB Free" \
|
||||||
|
>> $OUTFILE
|
||||||
|
;;
|
||||||
|
2) # Found exceeded in exceptions file by %Used method
|
||||||
|
echo "$FSDEVICE mount on $FSMOUNT is ${PC_USED}%" \
|
||||||
|
>> $OUTFILE
|
||||||
|
;;
|
||||||
|
3) # Found OK in exceptions file
|
||||||
|
: # NO-OP Do Nothing
|
||||||
|
;;
|
||||||
|
|
||||||
|
4) # Not found in exceptions file - Use Default Triggers
|
||||||
|
if (( $FSSIZE >= $FSTRIGGER ))
|
||||||
|
then # This is a "large" filesystem
|
||||||
|
FSMB_FREE=$(echo $FSMB_FREE | sed s/MB//g) # Remove the "MB"
|
||||||
|
if (( $FSMB_FREE < $MIN_MB_FREE ))
|
||||||
|
then
|
||||||
|
(( FS_FREE_OUT = $FSMB_FREE / 1000 ))
|
||||||
|
echo "$FSDEVICE mounted on $FSMOUNT has ${FS_FREE_OUT}MB Free" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
else # This is a standard filesystem
|
||||||
|
PC_USED=$(echo $PC_USED | sed s/\%//g)
|
||||||
|
MAX_PERCENT=$(echo $MAX_PERCENT | sed s/\%//g)
|
||||||
|
if (( $PC_USED > $MAX_PERCENT ))
|
||||||
|
then
|
||||||
|
echo "$FSDEVICE mount on $FSMOUNT is ${PC_USED}%" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
|
||||||
|
else # NO $EXECPTIONS FILE USE DEFAULT TRIGGER VALUES
|
||||||
|
|
||||||
|
if (( $FSSIZE >= $FSTRIGGER ))
|
||||||
|
then # This is a "large" filesystem - Use MB Free Method
|
||||||
|
FSMB_FREE=$(echo $FSMB_FREE | sed s/MB//g) # Remove the "MB"
|
||||||
|
if (( $FSMB_FREE < $MIN_MB_FREE ))
|
||||||
|
then
|
||||||
|
(( FS_FREE_OUT = $FSMB_FREE / 1000 ))
|
||||||
|
echo "$FSDEVICE mounted on $FSMOUNT has ${FS_FREE_OUT}MB Free" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
else # This is a standard filesystem - Use % Used Method
|
||||||
|
PC_USED=$(echo $PC_USED | sed s/\%//g)
|
||||||
|
MAX_PERCENT=$(echo $MAX_PERCENT | sed s/\%//g)
|
||||||
|
if (( $PC_USED > $MAX_PERCENT ))
|
||||||
|
then
|
||||||
|
echo "$FSDEVICE mount on $FSMOUNT is ${PC_USED}%" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done < $WORKFILE
|
||||||
|
|
||||||
|
display_output
|
||||||
|
|
||||||
|
# End of Script
|
||||||
113
chapter17/fs_mon_SUNOS_excep.ksh
Executable file
113
chapter17/fs_mon_SUNOS_excep.ksh
Executable file
@@ -0,0 +1,113 @@
|
|||||||
|
#!/usr/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: fs_mon_SUNOS_excep.ksh
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 08-22-2007
|
||||||
|
# REV: 2.1.P
|
||||||
|
# PURPOSE: This script is used to monitor for full filesystems,
|
||||||
|
# which is defined as "exceeding" the FSMAX value.
|
||||||
|
# A message is displayed for all "full" filesystems.
|
||||||
|
#
|
||||||
|
# PLATFORM: SUN Solaris
|
||||||
|
#
|
||||||
|
# REV LIST:
|
||||||
|
# 08-23-2007 - Randy Michael
|
||||||
|
# Added code to override the default FSMAX script threshold
|
||||||
|
# using an "exceptions" file, defined by the $EXCEPTIONS
|
||||||
|
# variable, that list /mount_point and NEW_MAX%
|
||||||
|
#
|
||||||
|
# set -n # Uncomment to check syntax without any execution
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
#
|
||||||
|
##### DEFINE FILES AND VARIABLES HERE ####
|
||||||
|
|
||||||
|
WORKFILE="/tmp/df.work" # Holds filesystem data
|
||||||
|
>$WORKFILE # Initialize to empty
|
||||||
|
OUTFILE="/tmp/df.outfile" # Output display file
|
||||||
|
>$OUTFILE # Initialize to empty
|
||||||
|
BINDIR="/usr/local/bin" # Local bin directory
|
||||||
|
THISHOST=`hostname` # Hostname of this machine
|
||||||
|
FSMAX="85" # Max. FS percentage value
|
||||||
|
EXCEPTIONS="${BINDIR}/exceptions" # Overrides $FSMAX
|
||||||
|
|
||||||
|
####### DEFINE FUNCTIONS HERE #####
|
||||||
|
|
||||||
|
function check_exceptions
|
||||||
|
{
|
||||||
|
|
||||||
|
# set -x # Uncomment to debug this function
|
||||||
|
|
||||||
|
# Define a data file
|
||||||
|
|
||||||
|
DATA_EXCEPTIONS="/tmp/dfdata.out"
|
||||||
|
|
||||||
|
# Ingore any line that begins with a pound sign, #
|
||||||
|
|
||||||
|
cat $EXCEPTIONS | grep -v "^#" > $DATA_EXCEPTIONS
|
||||||
|
|
||||||
|
while read FSNAME NEW_MAX # Feeding Ddata from Bottom of Loop!!!
|
||||||
|
do
|
||||||
|
if [[ $FSNAME = $FSMOUNT ]] # Correct /mount_point?
|
||||||
|
then # Get rid of the % sign, if it exists!
|
||||||
|
NEW_MAX=$(echo $NEW_MAX | sed s/\%//g)
|
||||||
|
|
||||||
|
if [ $FSVALUE -gt $NEW_MAX ]
|
||||||
|
then # Over Limit...Return a "0", zero
|
||||||
|
return 0 # FOUND MAX OUT - Return 0
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
done < $DATA_EXCEPTIONS # Feed from the bottom of the loop!!
|
||||||
|
|
||||||
|
return 1 # Not found in File
|
||||||
|
}
|
||||||
|
|
||||||
|
######## START OF MAIN #############
|
||||||
|
|
||||||
|
# Get the data of interest by stripping out the rows that
|
||||||
|
# are not monitored and keeping columns 1, 4 and 7
|
||||||
|
|
||||||
|
df -k | tail +2 | egrep -v '/dev/fd|/etc/mnttab|/proc|/cdrom' \
|
||||||
|
| awk '{print $1, $5, $6}' > $WORKFILE
|
||||||
|
|
||||||
|
# Loop through each line of the file and compare column 2
|
||||||
|
|
||||||
|
while read FSDEVICE FSVALUE FSMOUNT
|
||||||
|
do
|
||||||
|
# Strip out the % sign if it exists
|
||||||
|
FSVALUE=$(echo $FSVALUE | sed s/\%//g) # Remove the % sign
|
||||||
|
if [[ -s $EXCEPTIONS ]] # Do we have a non-empty file?
|
||||||
|
then # Found it!
|
||||||
|
|
||||||
|
# Look for the current $FSMOUNT value in the file
|
||||||
|
# using the check_exceptions function defined above.
|
||||||
|
|
||||||
|
check_exceptions
|
||||||
|
if [ $? -eq 0 ] # Found it Exceeded!!
|
||||||
|
then
|
||||||
|
echo "$FSDEVICE mount on $FSMOUNT is ${FSVALUE}%" \
|
||||||
|
>> $OUTFILE
|
||||||
|
else # Not exceeded in the file use the script default
|
||||||
|
if [ $FSVALUE -gt $FSMAX ] # Use Script Default
|
||||||
|
then
|
||||||
|
echo "$FSDEVICE mount on $FSMOUNT is ${FSVALUE}%" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
else # No exceptions file use the script default
|
||||||
|
if [ $FSVALUE -gt $FSMAX ] # Use Script Default
|
||||||
|
then
|
||||||
|
echo "$FSDEVICE mount on $FSMOUNT is ${FSVALUE}%" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done < $WORKFILE # Feed the while loop from the bottom...
|
||||||
|
|
||||||
|
# Display output if anything is exceeded...
|
||||||
|
|
||||||
|
if [[ -s $OUTFILE ]]
|
||||||
|
then
|
||||||
|
echo "\nFull Filesystem(s) on $THISHOST\n"
|
||||||
|
cat $OUTFILE
|
||||||
|
print
|
||||||
|
fi
|
||||||
113
chapter17/fs_mon_excep.ksh
Executable file
113
chapter17/fs_mon_excep.ksh
Executable file
@@ -0,0 +1,113 @@
|
|||||||
|
#!/usr/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: fs_mon.ksh
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 08-22-2007
|
||||||
|
# REV: 2.1.P
|
||||||
|
# PURPOSE: This script is used to monitor for full filesystems,
|
||||||
|
# which is defined as "exceeding" the FSMAX value.
|
||||||
|
# A message is displayed for all "full" filesystems.
|
||||||
|
#
|
||||||
|
# PLATFORM: AIX, at least
|
||||||
|
#
|
||||||
|
# REV LIST:
|
||||||
|
# 08-23-2007 - Randy Michael
|
||||||
|
# Added code to override the default FSMAX script threshold
|
||||||
|
# using an "exceptions" file, defined by the $EXCEPTIONS
|
||||||
|
# variable, that list /mount_point and NEW_MAX%
|
||||||
|
#
|
||||||
|
# set -n # Uncomment to check syntax without any execution
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
#
|
||||||
|
##### DEFINE FILES AND VARIABLES HERE ####
|
||||||
|
|
||||||
|
WORKFILE="/tmp/df.work" # Holds filesystem data
|
||||||
|
>$WORKFILE # Initialize to empty
|
||||||
|
OUTFILE="/tmp/df.outfile" # Output display file
|
||||||
|
>$OUTFILE # Initialize to empty
|
||||||
|
BINDIR="/usr/local/bin" # Local bin directory
|
||||||
|
THISHOST=`hostname` # Hostname of this machine
|
||||||
|
FSMAX="85" # Max. FS percentage value
|
||||||
|
EXCEPTIONS="${BINDIR}/exceptions" # Overrides $FSMAX
|
||||||
|
|
||||||
|
####### DEFINE FUNCTIONS HERE #####
|
||||||
|
|
||||||
|
function check_exceptions
|
||||||
|
{
|
||||||
|
|
||||||
|
# set -x # Uncomment to debug this function
|
||||||
|
|
||||||
|
# Define a data file
|
||||||
|
|
||||||
|
DATA_EXCEPTIONS="/tmp/dfdata.out"
|
||||||
|
|
||||||
|
# Ingore any line that begins with a pound sign, #
|
||||||
|
|
||||||
|
cat $EXCEPTIONS | grep -v "^#" > $DATA_EXCEPTIONS
|
||||||
|
|
||||||
|
while read FSNAME NEW_MAX # Feeding Data from Bottom of Loop!!!
|
||||||
|
do
|
||||||
|
if [[ $FSNAME = $FSMOUNT ]] # Correct /mount_point?
|
||||||
|
then # Get rid of the % sign, if it exists!
|
||||||
|
NEW_MAX=$(echo $NEW_MAX | sed s/\%//g)
|
||||||
|
|
||||||
|
if [ $FSVALUE -gt $NEW_MAX ]
|
||||||
|
then # Over Limit...Return a "0", zero
|
||||||
|
return 0 # FOUND MAX OUT - Return 0
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
done < $DATA_EXCEPTIONS # Feed from the bottom of the loop!!
|
||||||
|
|
||||||
|
return 1 # Not found in File
|
||||||
|
}
|
||||||
|
|
||||||
|
######## START OF MAIN #############
|
||||||
|
|
||||||
|
# Get the data of interest by stripping out /dev/cd#,
|
||||||
|
# /proc rows and keeping columns 1, 4 and 7
|
||||||
|
|
||||||
|
df -k | tail +2 | egrep -v '/dev/cd[0-9] | /proc' \
|
||||||
|
| awk '{print $1, $4, $7}' > $WORKFILE
|
||||||
|
|
||||||
|
# Loop through each line of the file and compare column 2
|
||||||
|
|
||||||
|
while read FSDEVICE FSVALUE FSMOUNT
|
||||||
|
do
|
||||||
|
# Strip out the % sign if it exists
|
||||||
|
FSVALUE=$(echo $FSVALUE | sed s/\%//g) # Remove the % sign
|
||||||
|
if [[ -s $EXCEPTIONS ]] # Do we have a non-empty file?
|
||||||
|
then # Found it!
|
||||||
|
|
||||||
|
# Look for the current $FSMOUNT value in the file
|
||||||
|
# using the check_exceptions function defined above.
|
||||||
|
|
||||||
|
check_exceptions
|
||||||
|
if [ $? -eq 0 ] # Found it Exceeded!!
|
||||||
|
then
|
||||||
|
echo "$FSDEVICE mount on $FSMOUNT is ${FSVALUE}%" \
|
||||||
|
>> $OUTFILE
|
||||||
|
else # Not exceeded in the file use the script default
|
||||||
|
if [ $FSVALUE -gt $FSMAX ] # Use Script Default
|
||||||
|
then
|
||||||
|
echo "$FSDEVICE mount on $FSMOUNT is ${FSVALUE}%" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
else # No exceptions file use the script default
|
||||||
|
if [ $FSVALUE -gt $FSMAX ] # Use Script Default
|
||||||
|
then
|
||||||
|
echo "$FSDEVICE mount on $FSMOUNT is ${FSVALUE}%" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done < $WORKFILE # Feed the while loop from the bottom...
|
||||||
|
|
||||||
|
# Display output if anything is exceeded...
|
||||||
|
|
||||||
|
if [[ -s $OUTFILE ]]
|
||||||
|
then
|
||||||
|
echo "\nFull Filesystem(s) on $THISHOST\n"
|
||||||
|
cat $OUTFILE
|
||||||
|
print
|
||||||
|
fi
|
||||||
182
chapter17/fs_mon_except_MB.ksh
Executable file
182
chapter17/fs_mon_except_MB.ksh
Executable file
@@ -0,0 +1,182 @@
|
|||||||
|
#!/usr/bin/ksh
|
||||||
|
#
|
||||||
|
# SCRIPT: fs_mon.ksh
|
||||||
|
# AUTHOR: Randy Michael
|
||||||
|
# DATE: 08-22-2007
|
||||||
|
# REV: 3.1.P
|
||||||
|
# PURPOSE: This script is used to monitor for full filesystems,
|
||||||
|
# which is defined as "exceeding" the FSMAX value.
|
||||||
|
# A message is displayed for all "full" filesystems.
|
||||||
|
#
|
||||||
|
# PLATFORM: AIX, at least
|
||||||
|
#
|
||||||
|
# REV LIST:
|
||||||
|
# 08-23-2007 - Randy Michael
|
||||||
|
# Added code to override the default FSMAX script threshold
|
||||||
|
# using an "exceptions" file, defined by the $EXCEPTIONS
|
||||||
|
# variable, that list /mount_point and NEW_MAX%
|
||||||
|
#
|
||||||
|
# 08-26-2007 - Randy Michael
|
||||||
|
# Added code to check both %USED and MB of Free space
|
||||||
|
# with auto detection to switch from %USED to MB Free
|
||||||
|
# for filesystems of 3GB and greater.
|
||||||
|
#
|
||||||
|
# set -n # Uncomment to check syntax without any execution
|
||||||
|
# set -x # Uncomment to debug this script
|
||||||
|
#
|
||||||
|
##### DEFINE FILES AND VARIABLES HERE ####
|
||||||
|
|
||||||
|
typeset -i FSTOTMB
|
||||||
|
|
||||||
|
WORKFILE="/tmp/df.work" # Holds filesystem data
|
||||||
|
>$WORKFILE # Initialize to empty file
|
||||||
|
OUTFILE="/tmp/df.outfile" # Output display file
|
||||||
|
>$OUTFILE # Initialize to empty file
|
||||||
|
BINDIR="/usr/local/bin" # Local bin directory
|
||||||
|
THISHOST=`hostname` # Hostname of this machine
|
||||||
|
FSMAX="85%" # Max. FS percentage value
|
||||||
|
EXCEPTIONS="${BINDIR}/exceptions" # Overrides $FSMAX
|
||||||
|
|
||||||
|
MBSWITCH="1000MB" # Number of MB to switch from %USED to MB Free
|
||||||
|
MBTRIGGER="40MB" # Minimum MB free before trigger
|
||||||
|
|
||||||
|
|
||||||
|
####### DEFINE FUNCTIONS HERE #####
|
||||||
|
|
||||||
|
function check_exceptions
|
||||||
|
{
|
||||||
|
|
||||||
|
# set -x # Uncomment to debug this function
|
||||||
|
|
||||||
|
# Define a data file
|
||||||
|
|
||||||
|
DATA_EXCEPTIONS="/tmp/dfdata.out"
|
||||||
|
|
||||||
|
# Ingore any line that begins with a pound sign, #
|
||||||
|
|
||||||
|
cat $EXCEPTIONS | grep -v "^#" > $DATA_EXCEPTIONS
|
||||||
|
|
||||||
|
while read FSNAME NEW_MAX # Feeding Ddata from Bottom of Loop!!!
|
||||||
|
do
|
||||||
|
if [[ "$FSNAME" = "$FSMOUNT" ]] # Correct /mount_point?
|
||||||
|
then
|
||||||
|
if (( $FSTOTMB >= $MBSWITCH ))
|
||||||
|
then
|
||||||
|
# MB Free Space - Get rid of "MB" if exists!
|
||||||
|
NEW_MIN_MB=$(echo $NEW_MAX | sed s/MB//g)
|
||||||
|
(( NEW_MIN_MB = $NEW_MIN_MB * 1024000 )) # In bytes
|
||||||
|
|
||||||
|
if (( $FSFREEMB < $NEW_MIN_MB ))
|
||||||
|
then
|
||||||
|
return 2 # Found below MB Trigger Level
|
||||||
|
else
|
||||||
|
return 3 # Found but OK
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
# Get rid of the % sign, if it exists!
|
||||||
|
NEW_MAX=$(echo $NEW_MAX | sed s/\%//g)
|
||||||
|
|
||||||
|
if [ $FSVALUE -gt $NEW_MAX ]
|
||||||
|
then # Over Limit...Return a "0", zero
|
||||||
|
return 0 # FOUND MAX OUT - Return 0
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
done < $DATA_EXCEPTIONS # Feed from the bottom of the loop!!
|
||||||
|
|
||||||
|
return 1 # Not found in File
|
||||||
|
}
|
||||||
|
|
||||||
|
######## START OF MAIN #############
|
||||||
|
|
||||||
|
####### FORMAT VARIABLES ###########
|
||||||
|
|
||||||
|
# Get rib of the "MB" letters if they exist
|
||||||
|
MBTRIGGER=$(echo $MBTRIGGER | sed s/MB//g )
|
||||||
|
MBSWITCH=$(echo $MBSWITCH | sed s/MB//g )
|
||||||
|
|
||||||
|
# Get rid of the "%" if it exists
|
||||||
|
FSMAX=$(echo $FSMAX | sed s/\%//g )
|
||||||
|
|
||||||
|
# Get an "actual" value in Mega Bytes
|
||||||
|
(( MBSWITCH = $MBSWITCH * 1024000 ))
|
||||||
|
(( MBTRIGGER = $MBTRIGGER * 1024000 ))
|
||||||
|
|
||||||
|
####################################
|
||||||
|
# Get the data of interest by stripping out /dev/cd#,
|
||||||
|
# /proc rows and keeping columns 1, 4 and 7
|
||||||
|
|
||||||
|
df -k | tail +2 | egrep -v '/dev/cd[0-9] | /proc' \
|
||||||
|
| awk '{print $1, $2, $3, $4, $7}' > $WORKFILE
|
||||||
|
|
||||||
|
####################################
|
||||||
|
# Loop through each line of the file and compare column 2
|
||||||
|
|
||||||
|
while read FSDEVICE FSTOTKB FSFREEKB FSVALUE FSMOUNT
|
||||||
|
do
|
||||||
|
# Strip out the % sign if it exists
|
||||||
|
FSVALUE=$(echo $FSVALUE | sed s/\%//g) # Remove the % sign
|
||||||
|
(( FSTOTMB = $FSTOTKB * 1000 ))
|
||||||
|
(( FSFREEMB = $FSFREEKB * 1000 ))
|
||||||
|
if [[ -s $EXCEPTIONS ]] # Do we have a non-empty file?
|
||||||
|
then # Found it!
|
||||||
|
|
||||||
|
# Look for the current $FSMOUNT value in the file
|
||||||
|
# using the check_exceptions function defined above.
|
||||||
|
|
||||||
|
check_exceptions
|
||||||
|
RC="$?"
|
||||||
|
if [ $RC -eq 0 ] # Found it Exceeded by Percentage
|
||||||
|
then
|
||||||
|
echo "$FSDEVICE mounted on $FSMOUNT is ${FSVALUE}%" \
|
||||||
|
>> $OUTFILE
|
||||||
|
elif [ $RC -eq 2 ] # Found it Exceeded by MB Free
|
||||||
|
then
|
||||||
|
(( FSFREEMB = $FSFREEMB / 1000000 ))
|
||||||
|
echo "$FSDEVICE mounted on $FSMOUNT only has ${FSFREEMB}MB Free" \
|
||||||
|
>> $OUTFILE
|
||||||
|
elif [ $RC -ne 3 ]
|
||||||
|
then
|
||||||
|
if (( $FSTOTMB >= $MBSWITCH ))
|
||||||
|
then
|
||||||
|
if (( $FSFREEMB < $MBTRIGGER )) # Use MB of Free Space?
|
||||||
|
then
|
||||||
|
(( FSFREEMB = $FSFREEMB / 1000000 ))
|
||||||
|
echo "$FSDEVICE mounted on $FSMOUNT only has ${FSFREEMB}MB Free" \
|
||||||
|
>> $OUTFILE
|
||||||
|
elif [ $FSVALUE -gt $FSMAX ] # Use Script Default of % Used
|
||||||
|
then
|
||||||
|
echo "$FSDEVICE mounted on $FSMOUNT is ${FSVALUE}%" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
else # No exceptions file use the script default
|
||||||
|
if (( $FSTOTMB >= $MBSWITCH ))
|
||||||
|
then
|
||||||
|
if (( $FSFREEMB < $MBTRIGGER ))
|
||||||
|
then
|
||||||
|
(( FSFREEMB = $FSFREEMB / 1000000 ))
|
||||||
|
echo "$FSDEVICE mounted on $FSMOUNT only has ${FSFREEMB}MB Free" \
|
||||||
|
>> $OUTFILE
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
if [ $FSVALUE -gt $FSMAX ] # Use Script Default
|
||||||
|
then
|
||||||
|
echo "$FSDEVICE mounted on $FSMOUNT is ${FSVALUE}%" \
|
||||||
|
>> $OUTFILE
|
||||||
|
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done < $WORKFILE # Feed the while loop from the bottom...
|
||||||
|
|
||||||
|
# Display output if anything is exceeded...
|
||||||
|
|
||||||
|
if [[ -s $OUTFILE ]]
|
||||||
|
then
|
||||||
|
echo "\nFull Filesystem(s) on $THISHOST\n"
|
||||||
|
cat $OUTFILE
|
||||||
|
print
|
||||||
|
fi
|
||||||
119
chapter17/function_check_exceptions
Executable file
119
chapter17/function_check_exceptions
Executable file
@@ -0,0 +1,119 @@
|
|||||||
|
function check_exceptions
|
||||||
|
{
|
||||||
|
# set -x # Uncomment to debug this function
|
||||||
|
|
||||||
|
while read FSNAME FSLIMIT
|
||||||
|
do
|
||||||
|
IN_FILE="N"
|
||||||
|
|
||||||
|
# Do an NFS sanity check and get rid of any .:..
|
||||||
|
# If this is found it is actually an error entry
|
||||||
|
# but we will try to resolve it. It will only
|
||||||
|
# work if it is an NFS cross mount to the same
|
||||||
|
# mount point on both machines.
|
||||||
|
$ECHO $FSNAME | grep ':' >/dev/null \
|
||||||
|
&& FSNAME=$($ECHO $FSNAME | cut -d ':' -f2)
|
||||||
|
|
||||||
|
# Check for empty and null variable
|
||||||
|
if [[ ! -z $FSLIMIT && $FSLIMIT != '' ]]
|
||||||
|
then
|
||||||
|
if [[ $FSNAME = $FSMOUNT ]] # Found it!
|
||||||
|
then
|
||||||
|
# Check for "MB" Characters...Set IN_FILE=MB
|
||||||
|
$ECHO $FSLIMIT | grep MB >/dev/null && IN_FILE="MB" \
|
||||||
|
&& (( FSLIMIT = $($ECHO $FSLIMIT \
|
||||||
|
| sed s/MB//g) * 1024 ))
|
||||||
|
# check for '%' Character...Set IN_FILE=PC, for %
|
||||||
|
$ECHO $FSLIMIT | grep '%' >/dev/null && IN_FILE="PC" \
|
||||||
|
&& FSLIMIT=$($ECHO $FSLIMIT | sed s/\%//g)
|
||||||
|
|
||||||
|
case $IN_FILE in
|
||||||
|
MB) # Use MB of Free Space Method
|
||||||
|
# Up-case the characters, if they exist
|
||||||
|
FSLIMIT=$($ECHO $FSLIMIT | tr '[a-z]' '[A-Z]')
|
||||||
|
# Get rid of the 'MB' if it exists
|
||||||
|
FSLIMIT=$($ECHO $FSLIMIT | sed s/MB//g)
|
||||||
|
# Test for blank and null values
|
||||||
|
if [[ ! -z $FSLIMIT && $FSLIMIT != '' ]]
|
||||||
|
then
|
||||||
|
# Test for a valid filesystem 'MB' limit
|
||||||
|
if (( FSLIMIT >= 0 && FSLIMIT < FSSIZE ))
|
||||||
|
then
|
||||||
|
if (( FSMB_FREE < FSLIMIT ))
|
||||||
|
then
|
||||||
|
return 1 # Found out of limit
|
||||||
|
# using MB Free method
|
||||||
|
else
|
||||||
|
return 3 # Found OK
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
$ECHO "\nERROR: Invalid filesystem MAX for\
|
||||||
|
$FSMOUNT - $FSLIMIT"
|
||||||
|
$ECHO " Exceptions file value must be less\
|
||||||
|
than or"
|
||||||
|
$ECHO " equal to the size of the filesystem\
|
||||||
|
measured"
|
||||||
|
$ECHO " in 1024 bytes\n"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
$ECHO "\nERROR: Null value specified in exceptions\
|
||||||
|
file"
|
||||||
|
$ECHO " for the $FSMOUNT mount point.\n"
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
PC) # Use Filesystem %Used Method
|
||||||
|
# Strip out the % sign if it exists
|
||||||
|
PC_USED=$($ECHO $PC_USED | sed s/\%//g)
|
||||||
|
# Test for blank and null values
|
||||||
|
if [[ ! -z "$FSLIMIT" && "$FSLIMIT" != '' ]]
|
||||||
|
then
|
||||||
|
# Test for a valid percentage, i.e. 0-100
|
||||||
|
if (( FSLIMIT >= 0 && FSLIMIT <= 100 ))
|
||||||
|
then
|
||||||
|
if (( $PC_USED > $FSLIMIT ))
|
||||||
|
then
|
||||||
|
return 2 # Found exceeded by % Used method
|
||||||
|
else
|
||||||
|
return 3 # Found OK
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
$ECHO "\nERROR: Invalid percentage for $FSMOUNT -\
|
||||||
|
$FSLIMIT"
|
||||||
|
$ECHO " Exceptions file values must be"
|
||||||
|
$ECHO " between 0 and 100%\n"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
$ECHO "\nERROR: Null value specified in exceptions\
|
||||||
|
file"
|
||||||
|
$ECHO " for the $FSMOUNT mount point.\n"
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
N) # Method Not Specified - Use Script Defaults
|
||||||
|
if (( FSSIZE >= FSTRIGGER ))
|
||||||
|
then # This is a "large" filesystem
|
||||||
|
if (( FSMB_FREE < MIN_MB_FREE ))
|
||||||
|
then
|
||||||
|
return 1 # Found out of limit
|
||||||
|
# using MB Free method
|
||||||
|
else
|
||||||
|
return 3 # Found OK
|
||||||
|
fi
|
||||||
|
else # This is a standard filesystem
|
||||||
|
PC_USED=$($ECHO $PC_USED | sed s/\%//g) # Remove %
|
||||||
|
FSLIMIT=$($ECHO $FSLIMIT | sed s/\%//g) # Remove %
|
||||||
|
if (( PC_USED > FSLIMIT ))
|
||||||
|
then
|
||||||
|
return 2 # Found exceeded by % Used method
|
||||||
|
else
|
||||||
|
return 3 # Found OK
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done < $DATA_EXCEPTIONS # Feed the loop from the bottom!!!
|
||||||
|
|
||||||
|
return 4 # Not found in $EXCEPTIONS file
|
||||||
|
}
|
||||||
|
|
||||||
10
chapter17/function_display_output
Executable file
10
chapter17/function_display_output
Executable file
@@ -0,0 +1,10 @@
|
|||||||
|
function display_output
|
||||||
|
{
|
||||||
|
if [[ -s $OUTFILE ]]
|
||||||
|
then
|
||||||
|
$ECHO "\nFull Filesystem(s) on $THISHOST\n"
|
||||||
|
cat $OUTFILE
|
||||||
|
print
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
11
chapter17/function_get_OS_info
Executable file
11
chapter17/function_get_OS_info
Executable file
@@ -0,0 +1,11 @@
|
|||||||
|
function get_OS_info
|
||||||
|
{
|
||||||
|
# For a few commands it is necessary to know the OS and its level
|
||||||
|
# to execute the proper command syntax. This will always return
|
||||||
|
# the OS in UPPERCASE
|
||||||
|
|
||||||
|
typeset -u OS # Use the UPPERCASE values for the OS variable
|
||||||
|
OS=`uname` # Grab the Operating system, i.e. AIX, HP-UX
|
||||||
|
print $OS # Send back the UPPERCASE value
|
||||||
|
}
|
||||||
|
|
||||||
7
chapter17/function_load_AIX_FS_data
Executable file
7
chapter17/function_load_AIX_FS_data
Executable file
@@ -0,0 +1,7 @@
|
|||||||
|
function load_AIX_FS_data
|
||||||
|
{
|
||||||
|
|
||||||
|
df -k | tail +2 | egrep -v "/dev/cd[0-9]|/proc" \
|
||||||
|
| awk '{print $1, $2, $3, $4, $7}' > $WORKFILE
|
||||||
|
}
|
||||||
|
|
||||||
8
chapter17/function_load_EXCEPTIONS_data
Executable file
8
chapter17/function_load_EXCEPTIONS_data
Executable file
@@ -0,0 +1,8 @@
|
|||||||
|
function load_EXCEPTIONS_data
|
||||||
|
{
|
||||||
|
# Ingore any line that begins with a pound sign, #
|
||||||
|
# and omit all blank lines
|
||||||
|
|
||||||
|
cat $EXCEPTIONS | grep -v "^#" | sed /^$/d > $DATA_EXCEPTIONS
|
||||||
|
}
|
||||||
|
|
||||||
6
chapter17/function_load_FS_data
Executable file
6
chapter17/function_load_FS_data
Executable file
@@ -0,0 +1,6 @@
|
|||||||
|
function load_FS_data
|
||||||
|
{
|
||||||
|
df -k | tail +2 | egrep -v '/dev/cd[0-9] | /proc' \
|
||||||
|
| awk '{print $1, $2, $3, $4, $7}' > $WORKFILE
|
||||||
|
}
|
||||||
|
|
||||||
7
chapter17/function_load_HP_UX_FS_data
Executable file
7
chapter17/function_load_HP_UX_FS_data
Executable file
@@ -0,0 +1,7 @@
|
|||||||
|
function load_HP_UX_FS_data
|
||||||
|
{
|
||||||
|
|
||||||
|
bdf | tail +2 | egrep -v "/cdrom" \
|
||||||
|
| awk '{print $1, $2, $4, $5, $6}' > $WORKFILE
|
||||||
|
}
|
||||||
|
|
||||||
7
chapter17/function_load_LINUX_FS_data
Executable file
7
chapter17/function_load_LINUX_FS_data
Executable file
@@ -0,0 +1,7 @@
|
|||||||
|
function load_LINUX_FS_data
|
||||||
|
{
|
||||||
|
|
||||||
|
df -k | tail -n +2 | egrep -v "/cdrom"\
|
||||||
|
| awk '{print $1, $2, $4, $5, $6}' > $WORKFILE
|
||||||
|
}
|
||||||
|
|
||||||
6
chapter17/function_load_OpenBSD_FS_data
Executable file
6
chapter17/function_load_OpenBSD_FS_data
Executable file
@@ -0,0 +1,6 @@
|
|||||||
|
function load_OpenBSD_FS_data
|
||||||
|
{
|
||||||
|
df -k | tail +2 | egrep -v "/mnt/cdrom"\
|
||||||
|
| awk '{print $1, $2, $4, $5, $6}' > $WORKFILE
|
||||||
|
}
|
||||||
|
|
||||||
7
chapter17/function_load_Solaris_FS_data
Executable file
7
chapter17/function_load_Solaris_FS_data
Executable file
@@ -0,0 +1,7 @@
|
|||||||
|
function load_Solaris_FS_data
|
||||||
|
{
|
||||||
|
|
||||||
|
df -k | tail +2 | egrep -v "/dev/fd|/etc/mnttab|/proc"\
|
||||||
|
| awk '{print $1, $2, $4, $5, $6}' > $WORKFILE
|
||||||
|
}
|
||||||
|
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user