Solved

Sun Solaris Unix Shell script that adds 2 to a string numeric and checks last value for change before emailing

Posted on 2009-05-08
5
575 Views
Last Modified: 2013-12-27
Currently our Sun E450 could detect room temperature (from "prtdiag -v" and I've
observed that it's consistently reporting about 1.5-2.0 Celsius lower than the
general DC room temperature :

# prtdiag -v | grep -i ambie
AMBIENT    19

"date" command in this Solaris box's format is :
# date
Sat May  9 06:06:58 SGT 2009


I'll need a Shell script that run every 1 minute (I'll put this into the crontab as
1,2,...59 * * * * /path/script_name) that will do the following :

I'll need this Shell script to be able to notify/email me when room temperature
(>= 21  AND there's a change from the last value)   or
(<= 20  AND the temperature recorded one minute ago is >= 21)

Above script basically notifies me once only (not repeatedly) when temperature
hits 21 C  but if temperature keep going up (at levels above 21 C) it will keep emailing/
notifying me.
This script only notify me once when temperature go below 21 C (but only notify
once and not repeatedly and it will stop notifying if room temperature stays below
20 C)  as the pseudo codes below describe :

Currentdatetime=yyyymmdd:hhmm   (24 hr format)
echo RoomTemp + " " + Currentdatetime  >> yyyymmdd_outputfile
add 2 to the value given by "prtdiag -v | grep AMBIENT" (call this variable RoomTemp
  if RoomTemp equals or exceeds 21
    check RoomTemp vs last_temperature recorded in last line of yyyymmdd_outputfile
        if RoomTemp equal or exceed 21 AND is higher than last_temperature
           email to myemail the value Currentdatetime+" "+":Room temp is now "+RoomTemp
        endif
  if last_temperature in last line of yyyymmdd_outputfile >= 21 and RoomTemp is <=20
    email to myemail the value Currentdatetime+" "+":Room temp now is down to "+RoomTemp
  endif
exit

0
Comment
Question by:sunhux
  • 2
  • 2
5 Comments
 
LVL 51

Accepted Solution

by:
ahoffmann earned 200 total points
ID: 24343987
#! /bin/sh
last_temp=0
last=/tmp/last_temp
[ -e $last ] &&  last_temp=`cat $last`
temp=`prtdiag -v |awk '/AMBIENT/{print $2}'`
echo $tmp >$last
[ $temp -ge 21 ] && mail -s21+ me@some.where
[ $temp -le 20 -o $last -ge 21 ] && mail -slast21+ me@some.where
exit 0
# not that the script relies on your cron to satisify your "one minute ago" condition
0
 
LVL 4

Assisted Solution

by:docbert
docbert earned 100 total points
ID: 24618196
ahoffmann's script misses one of your requirements - that it only sends a mail when "there's a change from the last value". As it is it will continue to email you once a minute until the temperature drops.

You can add the following below the "echo" statement to add that requirement :

[ $temp -eq $last_temp ] && exit

The 2nd last line is also wrong - it's using a -o (or) where it should be and, plus it's using $last rather than $last_temp.  I'd replace this line with :

[ $temp -le 20 ] && [ $last_temp -ge 21 ] && mail -slast21+ me@some.where
0
 
LVL 26

Assisted Solution

by:arober11
arober11 earned 200 total points
ID: 25158678
On the crontab front, you dont need:

1,2,...59 * * * * /path/script_name

As the following are equivalent:

0-59 * * * * /path/script_name

Or:
* * * * * /path/script_name

0
 
LVL 51

Assisted Solution

by:ahoffmann
ahoffmann earned 200 total points
ID: 25159181
> As the following are equivalent:
no, except you mean that a vixie cron is equivalent to a vixie cron ;-)
0
 
LVL 26

Assisted Solution

by:arober11
arober11 earned 200 total points
ID: 25160061
Well yes, using either variant above, assuming your using a cron daemon that supports Paul Vixie 1987 syntax, will save having to add an entry along the lines :

1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,34,35,36,37,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59 * * * * /path/script_name


You might also want to look at redirecting the output from the cron, to save the output being mailed to owning user, by appending something like this to the line:

 >/var/log/some_log_file 2>&1
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now