{ 6 comments… read them below or add one }

1 will May 30, 2008

Doesn’t appear to work?

Even when the value from bc is 0 it still sends a email?

2 kunthar May 28, 2010

Linux xxx.com 2.6.18-164.6.1.el5 #1 SMP Tue Nov 3 16:18:27 EST 2009 i686 i686 i386 GNU/Linux

(standard_in) 1: illegal character: |
(standard_in) 1: parse error
(standard_in) 1: illegal character: ‘
(standard_in) 1: illegal character: ‘

error.

3 Anonymous May 28, 2010

Solved.

#F5M=”$(uptime | awk -F “$FTEXT” ‘{ print $2 }’ | cut -d, -f1) | sed ‘s/ //g’”
#F10M=”$(uptime | awk -F “$FTEXT” ‘{ print $2 }’ | cut -d, -f2) | sed ‘s/ //g’”
#F15M=”$(uptime | awk -F “$FTEXT” ‘{ print $2 }’ | cut -d, -f3) | sed ‘s/ //g’”

lines should be
F5M=”$(uptime | awk -F “$FTEXT” ‘{ print $2 }’ | cut -d, -f1)”
# 10 min
F10M=”$(uptime | awk -F “$FTEXT” ‘{ print $2 }’ | cut -d, -f2)”
# 15 min
F15M=”$(uptime | awk -F “$FTEXT” ‘{ print $2 }’ | cut -d, -f3)”

with
bash –version
GNU bash, version 3.2.25(1)-release (i686-redhat-linux-gnu)

bc confused about comparison results.

4 Lang Zerner February 5, 2011

The correction in the comment above simply removes the sed command from the end of the pipeline on all three lines:

| sed ‘s/ //g’

As it turns out, the error is also corrected by moving the closing right paren so that it includes the sed command within the main pipeline. For example, change:

F15M="$(uptime | awk -F "$FTEXT" '{ print $2 }' | cut -d, -f3) | sed 's/ //g'"

to:

F15M="$(uptime | awk -F "$FTEXT" '{ print $2 }' | cut -d, -f3 | sed 's/ //g')"

(and the same for the F5 and F10 lines, in case you use them)

5 Lang Zerner February 5, 2011

The quotes and double-quotes in the corrected lines originally submitted by Anonymous are not simple straight quotes, but curly “smart quotes” (most likely inserted by some MS Word or Outlook editor not designed for code).

Your script will fail if you simply copy and paste those lines from the web page into your text editor, as curly quotes are not valid delimiters in any shell.

Here is the corrected script as it is working on my FreeBSD system. I hope this will appear here unaltered:

#!/bin/bash
#
# Script to notify admin user if Linux,FreeBSD load crossed certain limit
# It will send an email notification to admin.
#
# Copyright 2005 (c) nixCraft project
# This is free script under GNU GPL version 2.0 or above.
# Support/FeedBack/comment :  http://cyberciti.biz/fb/
# Tested os:
# * RedHat Linux
# * Debain Linux
# * FreeBSD
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
# Set up limit below
NOTIFY="6.0"
# admin user email id
EMAIL="root"
# Subject for email
SUBJECT="Alert $(hostname) load average"
# -----------------------------------------------------------------
# OS-specifc tweaks - do not change anything below
OS="$(uname)"
TRUE="1"
if [ "$OS" == "FreeBSD" ]; then
        TEMPFILE="$(mktemp /tmp/$(basename $0).tmp.XXX)"
        FTEXT='load averages:'
elif [ "$OS" == "Linux" ]; then
        TEMPFILE="$(mktemp)"
        FTEXT='load average:'
fi
# get first 5 min load
F5M="$(uptime | awk -F "$FTEXT" '{ print $2 }' | cut -d, -f1 | sed 's/ //g')"
# 10 min
F10M="$(uptime | awk -F "$FTEXT" '{ print $2 }' | cut -d, -f2 | sed 's/ //g')"
# 15 min
F15M="$(uptime | awk -F "$FTEXT" '{ print $2 }' | cut -d, -f3 | sed 's/ //g')"
# mail message
# keep it short coz we may send it to page or as an short message (SMS)
echo "Load average Crossed allowed limit $NOTIFY." >> $TEMPFILE
echo "Hostname: $(hostname)" >> $TEMPFILE
echo "Local Date & Time : $(date)" >> $TEMPFILE
# Look if it crossed limit
# compare it with last 15 min load average
RESULT=$(echo "$F15M > $NOTIFY" | bc)
# if so send an email
if [ "$RESULT" == "$TRUE" ]; then
        mail -s "$SUBJECT" "$EMAIL" < $TEMPFILE
fi
# remove file
rm -f $TEMPFILE
6 gsmtools September 1, 2011

working great
thank you

Leave a Comment

You can use these HTML tags and attributes for UNIX commands or shell scripts: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 11 + 14 ?
Please leave these two fields as-is:
Are you a human being? Solve the simple math so we know that you are a human and not a script.



Tagged as: awk command, basename, bash script, date time, debain linux, email notification, free script, freebsd, linux, linux system, load averages, mail command, mail email, message sms, shell script, short message, support feedback, sysem load, unix linux, uptime command

Previous Script:

Next Script: