Solved

Linux Shell script help

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

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 76

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

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

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
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…
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.

758 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

21 Experts available now in Live!

Get 1:1 Help Now