Solved

Linux Shell script help

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Linux Mint 18 31 89
Powershell Timeout - Exchange 3 44
AWS Central Authentication 1 54
Quickest way to query Windows Event ID from a Linux Device 3 27
Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
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 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 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.

785 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