Foil 05 - Periodic monitoring of a system

 1 #*********************************************************************
 2 # Running periodic system or process monitoring without using cron.  *
 3 #*********************************************************************
 4 monitorpass()
 5 {
 6 percent=`lsps -s | awk '!/Total/{print $2}' | sed 's/%//'`
 7 
 8 if [ "$percent" -ne "$last_percent" ]
 9   then
10     stamp=`date "+%y%m%d %H%M%S"`
11     echo "${stamp} ${percent}"
12     last_percent="$percent"
13   fi
14 
15 } # End of subroutine monitorpass
16 #---------------------------------------------------------------------
17 last_percent="00"
18 pass=1
19 while [ $pass -le 10000 ]
20   do
21     monitorpass $1 2>&1 | tee -a /tmp/lsps.anal.log
22     sleep  899          # 15 min
23     pass=`expr $pass + 1`
24   done                                                             #05

This example shows how to build custom tools for periodically monitoring a process, system, or network. Any variety and complexity of commands may be added to fit your situation.

  • Lines 4-15 define a subroutine with the monitoring commands. This separates the monitor commands from the timing and control functions.
  • Lines 17-24 control the execution of the monitor.
  • Line 6 shows how the output of a command can be parsed and assigned to a variable. The output of lsps filtered so only the second field of the Total line is reported. The sed command deletes a percent sign.
  • Lines 1, 12, and 17 show how results can be reported only when something changes, saving wasted space in log files. This could also check for the presense or absence of a process, disk usage, or other criteria.
  • Lines 18, 19, and 23 control the monitor by a counter. Other criteria could be used.
  • Line 21 executes the monitoring commands and sends the standard and error output to a log file and the terminal.
  • Line 22 controls the speed of the monitor. It is one second short of the specified time as a guess for the time needed to run the monitors. This is not synchronized to the quarter hour and may drift slightly.

    Previous   Next   Index