HostedDB - Dedicated UNIX Servers

-->
Internet Security Professional Reference:Understanding and creating
Previous Table of Contents Next


The init command will not report the run level it is operating in after it has switched run levels. For example, when switiching from single-user to multiuser mode, init might report this by printing something like

init: Run-Level 2
}

before continuing. Currently, the only way to see what run level init is in is to use the command who. In addition to showing you who is logged on the system, this command is capable of showing the run level the system is currently operating in. To view the run level, use the option, -r. The output of this command is shown as follows:

$ who -r
. run-level 2 Aug 27 21:31 2 0 S
$

According to this output, the current run level is 2. The date refers to when the system entered that run level; the digits to the right show the current, oldest, and last run level.

Program Listings

Each of the following code lists are programs discussed earlier in this chapter. Fortunately, you do not have to type these lists by hand; they are included on the disc at the back of this book.

Listing 2.1—The dfmon Program

This program and its output are discussed in the section on writing daemons in the Bourne/Korn shell. To use dfmon, install it /usr/local/bin, and add it to one of the system startup scripts. On the next reboot, dfmon will start up and commence monitoring your available disk space.

When installing dfmon.sh, do not execute it as root. Because none of the commands in the script are restricted, it is not necessary for the script to run as root. In fact, allowing it to run as root may contribute to lowering your security level. This is because the dfmon.sh script uses the Bourne/Korn shell source command to load in the configuration file.

This command simply “includes” the contents of the named file into the shell, and could be used to circumvent the system. If you must run dfmon.sh as root, be sure to put the configuration file in a protected directory, and use the appropriate file permissions to prevent non-root users from accessing it.

#!/bin/sh
#
# This is a shell program to monitor the available disk space on  a
  system
# and report when it reaches a lower limit.
#
# The script is intended to run on both System V and BSD systems, and
  handle
# the different forms of df that exist"on both platforms.
#
# Configuration
# CONFIG=>path</dfmon.cfg
CONFIG=./dfmon.cfg
#
# Load hn the configuration file using the source (.) command.
# NOTE
# THE DFMON.SH PROGRAM SHOULD NOT BE EXECUTED AS ROOT.
#
. $CONFIG
#
# With the configuration file loaded, we now start the daemon process.
  To do
# this we run the balance of the command in a subshell, so the parent
# process can exit.
#
echo "`date` Starting Disk Monitor Daemon ...."
(
#
# Ignore TRAPS, so we can't be killed with anything but a kill -9 …
#
# NOTE:
# on HP-UX, traps 11, 18 cannot be specified in the trap list
# on SCO, traps 11, 20-25 cannot be specified in the trap list
trap "" 1 2 3 4 5 6 7 8 10 12 13 14 15 16 17 18 19
#
# Assemble our environment
PATH=/usr/bin:/bin:/etc:/sbin:/usr/sbin
IFS="   "
# Comment this on systems that do not use dynamically loaded libraries
  like
# Sun-based systems.
unset LD_LIBRARY
#
# NOTE:
# Even though the PATH variable has been explicity set, the
  commands
# executed in this script are specified using their exact path.
#
# Even though the intent behind this program is to function as a
  daemon,
# the standard I/O files will not be closed as the standard I/O  path
  is used
# to communicate between the main program and the loaded shell functions
  that
# are found in the configuration file.
#
# We need to get the df output first, and feed it into a while loop for
# processing.
#
# Here we run the correct version of the df_function, so that we get the
# information we want from the non-standard, non-compatible versions of
# df that are in existence. (And they say that there is a standard!)
#
while :
do
for filesystem in `/etc/mount | /usr/bin/cut -d" " -f$MOUNT_POS'
do
    case $DF_TYPE in
        HPUX)
            LOGGER=/usr/bin/logger
            RESULTS=`df_hpux $filesystem';;
        SCO_UNIX)
            LOGGER=/usr/bin/logger
            RESULTS=`df_sco_unix $filesystem';;
        SunOS)
            LOGGER=/usr/ucb/logger
            RESULTS=`df_sunos $filesystem';;
        BSDI)
            LOGGER=/usr/bin/logger
            RESULTS=`df_bsdi $filesystem';;
        LINUX)
            LOGGER=/usr/bin/logger
            RESULTS=`df_linux $filesystem';;
    esac
    set $RESULTS

    FILESYS=$1
    FREE=$2
    TOTAL=$3
    USED=$4

    #
    # We need to check the file system to determine what type of
    # control we want to place upon it.  For example, if the file
      system
    # is root, then the ROOT_LOW and ROOT_CRITICAL values are used
      for the
    # monitoring alarms.
    case "$FILESYS" in
        "/")
            LOW=$ROOT_LOW
            CRITICAL=$ROOT_CRITICAL;;
        "/usr")
            LOW=$USR_LOW
            CRITICAL=$USR_CRITICAL;;
        "/var")
            LOW=$VAR_LOW
            CRITICAL=$VAR_CRITICAL;;
        *)
            LOW=$OTHER_LOW
            CRITICAL=$OTHER_CRITICAL;;
    esac

    #
    # Look at the bytes free versus the total bytes available
    # if the free space is lower than the lower water mark
    # from the config file, then sound the alarm, if and only
    # if the disk filesystem is alarmed.
    #
    # If syslog is in use, use the logger command to send a message
    # and save it in the syslog.  Otherwise, use the name of the
    # log file from the config file, and log the problem to the file,
    # to the console device, and to the user identified in the config
    # file.
    #
    # We will use a special facility so that these messages can be
    # appropriately handled.
    #
    # The CRITICAL level is checked first because it will be the
      lower of
    # the two test values.
    #
    if [ "$FREE" -le "$CRITICAL" ]
    then
        #
        # It is a critical level, so use syslog to record the alarm
        #
        if [ "$USE_SYSLOG" = "YES" ]
        then
            #
            # Use the logger command to send the information to
            # the syslog daemon.
            #
            /usr/bin/logger -t "dfmon" -p $SYSLOG_FAC.crit "CRITICAL
              Alarm : $FILESYS space: Free=$FREE"
            else
            #
            # It is critical, but we do not have syslog, so we
            # use our fake_syslog function.
            #
            fake_syslog "CRITICAL Alarm: $FILESYS space: Free=$FREE"
        fi
    #
    # It isn't crtical, so let's check it against our low level alarm.
    #
    elif [ "$FREE" -le "$LOW" ]
    then
        #
        # Yes - it is a low level alarm, so use syslog to report
        # the alarm.
        #
        if [ "$USE_SYSLOG" = "YES" ]
        then
            #
            # Use the logger command to send the information to
            # the syslog daemon.
            #
            /usr/bin/logger -t "dfmon" -p $SYSLOG_FAC.emerg "WARNING
              Alarm : $FILESYS space: Free=$FREE"
    else
            #
            # syslog is not available, so mimic it using the
            # fake syslog function.
            #
            fake_syslog "CRITICAL Alarm: $FILESYS space: Free=$FREE"
        fi
    fi
done
    #
    # Delay the number of seconds identified by PASS_DELAY in the config
    # file.
    #
    sleep $PASS_DELAY
    #
    # This constitutes the end of the daemon code.  This will execute
      until
    # the system is halted, or until a user kills it.
    #
done
) &
# end of the road


Previous Table of Contents Next