?
Solved

Linux Shell script help

Posted on 2013-11-27
6
Medium Priority
?
383 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 79

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 79

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
Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

 

Author Comment

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

Accepted Solution

by:
arnold earned 2000 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

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…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses

719 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