Solved

Linux Shell script help

Posted on 2013-11-27
6
350 Views
Last Modified: 2014-01-17
Hi Scripting Gurus,

I need to modify below script to send email it's out put. (to one or two addresses)

And I need it two ways;

1. Send an email for any event
2. Send email when only an ERROR or an ALERT triggers

My OS is Ubuntu server and I can install "mailutils"

Any support would be highly appreciated.

while getopts "m:s:b:" opt;do  
 case $opt in  
 m)  
   h1=`echo $OPTARG|cut -d":" -f1`  
   p1=`echo $OPTARG|cut -d":" -f2`;;  
 s)  
   h2=`echo $OPTARG|cut -d":" -f1`  
   p2=`echo $OPTARG|cut -d":" -f2`;;  
 b) PGHOME="$OPTARG"  
esac  
done  
PSQL=$PGHOME/psql  
  
  
function usage()  
{  
 if [  -z $h1 ];then  
   echo "USAGE: "  
   echo "$0 -m master:port -s slave:port -b pg bin directory"  
   exit 1  
 fi  
 if [  -z $h2 ];then  
   echo "USAGE: "  
   echo "$0 -m master:port -s slave:port -b pg bin directory"  
   exit 1  
 fi  
 if [  -z $p1 ];then  
   echo "USAGE: "  
   echo "$0 -m master:port -s slave:port -b pg bin directory"  
   exit 1  
 fi  
 if [  -z $p2 ];then  
   echo "USAGE: "  
   echo "$0 -m master:port -s slave:port -b pg bin directory"  
   exit 1  
 fi  
 if [  -z $PGHOME ];then  
   echo "USAGE: "  
   echo "$0 -m master:port -s slave:port -b pg bin directory"  
   exit 1  
 fi  
}  
  
function verifybin_connect()  
{  
  if [ -f $PGHOME/psql ];then  
    :  
  else  
      echo "ERROR: psql Not Found!"  
      exit 1  
  fi  
  Q="SELECT 'ping';"  
  $PGHOME/psql -h $1 -p $2 -c "$Q1" >/dev/null 2>/dev/null  
   if [ $? -ne 0 ];then  
       echo "ERROR: Master is not pinging on $h1"  
       exit 1  
  fi  
   $PGHOME/psql -h $3 -p $4 -c "$Q1" >/dev/null 2>/dev/null  
   if [ $? -ne 0 ];then  
       echo "ERROR: Slave is not pinging on $h2"  
       exit 1  
  fi  
}  
  
function verify_is_recovery()  
{  
  Q="select pg_is_in_recovery()::int;"  
  status=`$PGHOME/psql -c "$Q" -t -h $1 -p $2 template1|sed '/^$/d'`  
  if [ $status -eq 1 ];then  
     echo "MESSAGE: PG is in Recovery Mode"  
  else  
      echo "ERROR: Slave is out of Recovery Mode"  
      exit 1  
  fi  
}  
function convert_decimal()  
{  
  decimalval=`echo "ibase=16;obase=A;$1"|bc`  
  echo $decimalval  
}  
  
function get_xlog_name()  
{  
 Q1="select pg_xlogfile_name('$1');"  
 xlogname=`$PSQL -h $2 -p $3 -t -c "$Q1" template1|sed '/^$/d'`  
 echo $xlogname  
}  
  
function main()  
{  
 verifybin_connect $1 $2 $3 $4  
 verify_is_recovery $3 $4  
 Q1="select pg_current_xlog_location();"  
 Q2="select pg_last_xlog_receive_location();"  
 Primxlog=`$PSQL -t -c "$Q1" -h $1 -p $2 template1|sed '/^$/d'`  
 Secxlog=`$PSQL -t -c "$Q2" -h $3 -p $4 template1|sed '/^$/d'`  
 secwal=`get_xlog_name $Secxlog $1 $2`  
 primwal=`get_xlog_name $Primxlog $1 $2`  
 primloc=`convert_decimal $primwal`  
 secloc=`convert_decimal $secwal`  
 result=`echo $primloc - $secloc|bc`  
 if [ $result -ne 0 ];then  
     echo "ALERT:: Seconday is lagging behind by $result files"  
 else  
     echo "Streaming Replication between $h1 -> $h2 is in Sync"  
 fi  
  
}  
usage  
main $h1 $p1 $h2 $p2

Open in new window

0
Comment
Question by:Shakthi777
  • 3
  • 3
6 Comments
 
LVL 77

Expert Comment

by:arnold
ID: 39681572
not sure which shell you are using

Presumably your local server has a mail server installed, sendmail, postfix, exim, etc. such that /usr/sbin/sendmail points to the installed version.

you would create a function to which you can pass: recipient/s message
$recipient can be a comma separated list of recipients
echo "To: $recipient_list
From: $sender
Subject: $subject

$message

" | /usr/sbin/sendmail -oi -f$sender -t 

Open in new window


whenever a condition that you want to trigger the email, you would call this function.
0
 

Author Comment

by:Shakthi777
ID: 39683025
Thanks a lot for the response.

Can you make a example using the given script? I'm not families with functions etc.

Please help me !
0
 
LVL 77

Expert Comment

by:arnold
ID: 39683922
Which shell are you in?  What is the first line of the script?
#!/bin/sh
#!/bin/bash
#!/bin/zsh
#!/bin/tcsh
#!/bin/ksh
etc.

Are you looking for different messages if it is an alarm versus an error or other ....?
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:Shakthi777
ID: 39684621
#!/bin/bash
0
 
LVL 77

Accepted Solution

by:
arnold earned 500 total points
ID: 39684733
Add:
function mail_me()
{
#usage for function  mail_me "recipient" "subject" "message"
sender="your_email_address"
recipient="$1"
subject="$2"
message="$3"

echo "To: $recipient
From: $sender
Subject: $subject

$message

" | /usr/sbin/sendmail -oi -f$sender -t

}#end mail_me function

Open in new window


Give it a try.

Add
mail_me "email_address" "Alert: issue" "the message you want included in the email for the alert"

Where your alert events are.  Repeat for the other notifications to match.
0
 

Author Closing Comment

by:Shakthi777
ID: 39787921
tnx !
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

839 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