Solved

Linux Shell script help

Posted on 2013-11-27
6
355 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 78

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 78

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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 

Author Comment

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

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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…
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…

685 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