Slack message multi line from UNIX script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Slack message multi line from UNIX script
# 8  
Old 11-08-2018
Quote:
Originally Posted by MadeInGermany
Please add the missing /g (I have correct my post#4):
Code:
escapedText=$(
echo "$text
$text1" | sed 's/"/\\"/g; s/'\''/\\'\''/g'
)

The
Code:
ps -ef | grep smon | grep $db_sid

is error-prone (unsharp grep might catch wrong lines, grep command itself might appear in the ps output)
Could you give an example result of it?
Apologies for delayed reply

The below is smon process shows two db running one is orcl and another one orcltest. So I want program to run for both these and should show its status.

Code:
ps -ef | grep smon

Code:
[oracle@ip-xx-xx-xx-xxx~]$ ps -ef|grep smon
oracle   15093     1  0 Nov08 ?        00:00:30 ora_smon_orcl
oracle   15486     1  0 Nov08 ?        00:00:30 ora_smon_orcltest

# 9  
Old 11-13-2018
Maybe you can do
Code:
db_sid_pro="ora_smon_$db_sid"
if pgrep -u oracle -x "$db_sid_pro" >/dev/null 
then
  tag="true"
  msg="$db_sid database instance is running"
  echo "$db_sid_pro database process is running"
else
  ...
fi

The shell script uses the exit status from pgrep and throws its result away. (Alternatively you could capture the result in a variable and then test it for having a value: pid=`pgrep ...`; if test -n "$pid")
The -u oracle and -x options make the match even more precise.
See man pgrep.
# 10  
Old 11-15-2018
Quote:
Originally Posted by MadeInGermany
Maybe you can do
Code:
db_sid_pro="ora_smon_$db_sid"
if pgrep -u oracle -x "$db_sid_pro" >/dev/null 
then
  tag="true"
  msg="$db_sid database instance is running"
  echo "$db_sid_pro database process is running"
else
  ...
fi

The shell script uses the exit status from pgrep and throws its result away. (Alternatively you could capture the result in a variable and then test it for having a value: pid=`pgrep ...`; if test -n "$pid")
The -u oracle and -x options make the match even more precise.
See man pgrep.
Hi,

I tried to loop in the logic for all db_sid that are currently running and modified logic as below..but program running but wrong results even though database and listners are up when i checked manually
existing oracle instance running
Code:
[oracle@ip-xx-xx-xx-xxx~]$ ps -ef|grep smon
oracle   15093     1  0 Nov08 ?        00:00:30 ora_smon_orcl
oracle   15486     1  0 Nov08 ?        00:00:30 ora_smon_orcltest

Now trying to get each instance that is running ..i.e only db_sid name by omitting ora_smon_ and getting only name

for ora_smon_orcl ==> orcl
for ora_smon_orcltest ==> orcltest
Code:
for db_sid in `ps -ef | grep smon | grep -v grep | awk '{ print $8 }' | cut -d '_' -f3` ; do
echo $db_sid

db_sid_pro="ora_smon_$db_sid"
echo $db_sid_pro
source /home/oracle/$db_sid
ora_home=$ORACLE_HOME
echo $ORACLE_HOME

case "$db_sid" in
"$db_sid_pro")
echo "hi"
tag="true"
echo $tag
msg="$db_sid database instance is running"
echo "$db_sid_pro database process is running"
;;
*)
if [ -e $ora_home/dbs/spfile$db_sid.ora ];then
msg="database is down"
echo "database is down"
tag="false"
else
tag="false"
msg="Incorrect database SID - $db_sid. Please provide the correct database SID"
echo "Incorrect database SID - $db_sid. Please provide the correct database SID"
fi
;;
esac

the output shown on terminal is below: but manually i checked databse is up and listener also up..May be i am missing something in programming logic here not really database issue


Code:
orcl
ora_smon_orcl
/dbusr/app/oracle/product/12102
database is down
Oracle listener is down please make it up
database is down
Oracle listener is down please make it up
orcltest
ora_smon_orcltest
/dbusr/app/oracle/product/12102
database is down
Oracle listener is down please make it up
database is down
Oracle listener is down please make it up

However I find below is causing trouble..because when I put some debug echo its not displaying "hi"

Code:
case "$db_sid" in
"$db_sid_pro")
echo "hi"
tag="true"
echo $tag

and also i have manually checked for below line.. files are indeed exist as per below conditon

Code:
if [ -e $ora_home/dbs/spfile$db_sid.ora ];then

Any suggestion

Last edited by onenessboy; 11-15-2018 at 12:04 PM..
# 11  
Old 11-16-2018
If the loop goes over the running processes, how are the not running processes detected then?
The following loops over the files to get the DB instances, then looks them up in the ps output:
Code:
# turn debugging on
#set -x

ora_home=$ORACLE_HOME

#pscope="-e"
pscope="-u oracle"
ps_prefix=ora_smon_
# ora_ps is a newline-separated list of matching ps args
ora_ps=$(
  ps $pscope -o args= | awk -v prefix="$ps_prefix" '$1~prefix {print $1}'
)
# prepend and append a newline separator for an exact lookup:
ora_ps_lu="
$ora_ps
"
#echo "The oracle processes are:$ora_ps_lu"

# For each DB there is a file - but perhaps no process
# So it makes sense to loop over the files

spfile_prefix=$ora_home/dbs/spfile
spfile_suffix=.ora
for file in "$spfile_prefix"?*"$spfile_suffix"
do
  # ensure it's a file
  [ -f "$file" ] || continue
  # dig out the db_sid
  db_sid=${file#$spfile_prefix}
  db_sid=${db_sid%$spfile_suffix}
  db_sid_pro=${ps_prefix}${db_sid}
  # for an exact lookup prepend and append the separator (newline)
  db_sid_pro_lu="
$db_sid_pro
"
  # match in the $ora_ps_lu
  case "$ora_ps_lu" in
  *"$db_sid_pro_lu"* )
    echo "$db_sid database instance is running"
    echo "$db_sid_pro process is running"
  ;;
  * )
    echo "$db_sid database instance is down"
    echo "No $db_sid_pro process"
  ;;
  esac
done

It avoids bash4 arrays - instead it does a lookup in a simple variable.

Last edited by MadeInGermany; 11-16-2018 at 01:17 PM..
This User Gave Thanks to MadeInGermany For This Post:
# 12  
Old 11-17-2018
Quote:
Originally Posted by MadeInGermany
If the loop goes over the running processes, how are the not running processes detected then?
The following loops over the files to get the DB instances, then looks them up in the ps output:
Code:
# turn debugging on
#set -x

ora_home=$ORACLE_HOME

#pscope="-e"
pscope="-u oracle"
ps_prefix=ora_smon_
# ora_ps is a newline-separated list of matching ps args
ora_ps=$(
  ps $pscope -o args= | awk -v prefix="$ps_prefix" '$1~prefix {print $1}'
)
# prepend and append a newline separator for an exact lookup:
ora_ps_lu="
$ora_ps
"
#echo "The oracle processes are:$ora_ps_lu"

# For each DB there is a file - but perhaps no process
# So it makes sense to loop over the files

spfile_prefix=$ora_home/dbs/spfile
spfile_suffix=.ora
for file in "$spfile_prefix"?*"$spfile_suffix"
do
  # ensure it's a file
  [ -f "$file" ] || continue
  # dig out the db_sid
  db_sid=${file#$spfile_prefix}
  db_sid=${db_sid%$spfile_suffix}
  db_sid_pro=${ps_prefix}${db_sid}
  # for an exact lookup prepend and append the separator (newline)
  db_sid_pro_lu="
$db_sid_pro
"
  # match in the $ora_ps_lu
  case "$ora_ps_lu" in
  *"$db_sid_pro_lu"* )
    echo "$db_sid database instance is running"
    echo "$db_sid_pro process is running"
  ;;
  * )
    echo "$db_sid database instance is down"
    echo "No $db_sid_pro process"
  ;;
  esac
done

It avoids bash4 arrays - instead it does a lookup in a simple variable.
Thank you very much

Its worked like a charm. bit modified to display output like below and terminal output is like:

Code:
==============================================
Oracle Host: development_box
==============================================
orcl:
Database UP -- OK
Listener UP -- OK
Database Read&Write Mode -- OK
Instance Status -- OK
Instance Login Allowed -- OK
---------------------------------------------
orcltest:
Database UP -- OK
Listener UP -- OK
Database Read&Write Mode -- OK
Instance Status -- OK
Instance Login Allowed -- OK
---------------------------------------------

However, as you suggested I am using below line to slack...however only first two line going into slack..

the below is code currently being used for slack post:

Code:
token="xxxxxxxxxxxxxxxxxxxxx"

if [[ $token == "" ]]
then
        echo "No token specified"
        exit 1
fi

shift
channel="#are_you_listening"
if [[ $channel == "" ]]
then
        echo "No channel specified"
        exit 1
fi

shift

text="$msg"
text1="$lmsg"

if [[ $text == "" ]]
then
        echo "No text specified"
        exit 1
fi

#escapedText=$(echo $text | $text1 | sed 's/"/\"/g' | sed "s/'/\'/g" )
escapedText=$(
echo "$text
$text1" | sed 's/"/\\"/g; s/'\''/\\'\''/g'
)
json="{\"channel\": \"#$channel\", \"text\": \"$escapedText\"}"

curl -X POST --data-urlencode "payload=$json" "https://hooks.slack.com/services/xxxx/xxx/xxxxxx

On terminal I am getting all output but, on slack i get only the below values

Code:
orcltest Database UP -- OK
Listener UP -- OK

no clue why it printing only these lines.. do I need to print instead of echo ?. any suggestion pls
Is there any way to send terminal screen output same format to slack as it is?

------ Post updated at 01:13 PM ------

Quote:
Originally Posted by onenessboy
Thank you very much

Its worked like a charm. bit modified to display output like below and terminal output is like:

Code:
==============================================
Oracle Host: development_box
==============================================
orcl:
Database UP -- OK
Listener UP -- OK
Database Read&Write Mode -- OK
Instance Status -- OK
Instance Login Allowed -- OK
---------------------------------------------
orcltest:
Database UP -- OK
Listener UP -- OK
Database Read&Write Mode -- OK
Instance Status -- OK
Instance Login Allowed -- OK
---------------------------------------------

However, as you suggested I am using below line to slack...however only first two line going into slack..

the below is code currently being used for slack post:

Code:
token="xxxxxxxxxxxxxxxxxxxxx"

if [[ $token == "" ]]
then
        echo "No token specified"
        exit 1
fi

shift
channel="#are_you_listening"
if [[ $channel == "" ]]
then
        echo "No channel specified"
        exit 1
fi

shift

text="$msg"
text1="$lmsg"

if [[ $text == "" ]]
then
        echo "No text specified"
        exit 1
fi

#escapedText=$(echo $text | $text1 | sed 's/"/\"/g' | sed "s/'/\'/g" )
escapedText=$(
echo "$text
$text1" | sed 's/"/\\"/g; s/'\''/\\'\''/g'
)
json="{\"channel\": \"#$channel\", \"text\": \"$escapedText\"}"

curl -X POST --data-urlencode "payload=$json" "https://hooks.slack.com/services/xxxx/xxx/xxxxxx

On terminal I am getting all output but, on slack i get only the below values

Code:
orcltest Database UP -- OK
Listener UP -- OK

no clue why it printing only these lines.. do I need to print instead of echo ?. any suggestion pls
Is there any way to send terminal screen output same format to slack as it is?
Hi,

Some how i am able to output all values in terminal.. Up to this point i am able to achieve...now problem is with posting it to slack..even though I mention \n its not printing line by line in slack...

Terminal output (I have stored into variable, so echo it):

Code:
echo "$text"

==============================================
Oracle Host: xxxxxxxxx
==============================================
orcl:
Database UP -- OK
Listener UP -- OK
Database Read&Write Mode -- OK
Instance Status -- OK
Instance Login Allowed -- OK
---------------------------------------------
orcltest:
Database UP -- OK
Listener UP -- OK
Database Read&Write Mode -- OK
Instance Status -- OK
Instance Login Allowed -- OK
---------------------------------------------

Now trying to read line by line from this variable with below code:

Code:
while IFS= read -r line; do
  #printf '%s\n' "$line"
  text="$text$line\n"
done <<< "$text"

escapedText=$(echo $text | sed 's/"/\\"/g; s/'\''/\\'\''/g' )
json="{\"channel\": \"#$channel\", \"text\": \"$escapedText\"}"

curl -X POST --data-urlencode "payload=$json" "https://hooks.slack.com/services/xxxxx/xxxx/xxxxx"

its showing error like:
Code:
missing_text_or_fallback_or_attachments

but in above code I guess while loop is not working properly...is that correct way of reading multi line variable and pass on to payload in curl statement

Last edited by onenessboy; 11-17-2018 at 06:35 AM..
# 13  
Old 11-19-2018
Quote:
Originally Posted by onenessboy
Thank you very much

Its worked like a charm. bit modified to display output like below and terminal output is like:

Code:
==============================================
Oracle Host: development_box
==============================================
orcl:
Database UP -- OK
Listener UP -- OK
Database Read&Write Mode -- OK
Instance Status -- OK
Instance Login Allowed -- OK
---------------------------------------------
orcltest:
Database UP -- OK
Listener UP -- OK
Database Read&Write Mode -- OK
Instance Status -- OK
Instance Login Allowed -- OK
---------------------------------------------

However, as you suggested I am using below line to slack...however only first two line going into slack..

the below is code currently being used for slack post:

Code:
token="xxxxxxxxxxxxxxxxxxxxx"

if [[ $token == "" ]]
then
        echo "No token specified"
        exit 1
fi

shift
channel="#are_you_listening"
if [[ $channel == "" ]]
then
        echo "No channel specified"
        exit 1
fi

shift

text="$msg"
text1="$lmsg"

if [[ $text == "" ]]
then
        echo "No text specified"
        exit 1
fi

#escapedText=$(echo $text | $text1 | sed 's/"/\"/g' | sed "s/'/\'/g" )
escapedText=$(
echo "$text
$text1" | sed 's/"/\\"/g; s/'\''/\\'\''/g'
)
json="{\"channel\": \"#$channel\", \"text\": \"$escapedText\"}"

curl -X POST --data-urlencode "payload=$json" "https://hooks.slack.com/services/xxxx/xxx/xxxxxx

On terminal I am getting all output but, on slack i get only the below values

Code:
orcltest Database UP -- OK
Listener UP -- OK

no clue why it printing only these lines.. do I need to print instead of echo ?. any suggestion pls
Is there any way to send terminal screen output same format to slack as it is?

------ Post updated at 01:13 PM ------



Hi,

Some how i am able to output all values in terminal.. Up to this point i am able to achieve...now problem is with posting it to slack..even though I mention \n its not printing line by line in slack...

Terminal output (I have stored into variable, so echo it):

Code:
echo "$text"

==============================================
Oracle Host: xxxxxxxxx
==============================================
orcl:
Database UP -- OK
Listener UP -- OK
Database Read&Write Mode -- OK
Instance Status -- OK
Instance Login Allowed -- OK
---------------------------------------------
orcltest:
Database UP -- OK
Listener UP -- OK
Database Read&Write Mode -- OK
Instance Status -- OK
Instance Login Allowed -- OK
---------------------------------------------

Now trying to read line by line from this variable with below code:

Code:
while IFS= read -r line; do
  #printf '%s\n' "$line"
  text="$text$line\n"
done <<< "$text"

escapedText=$(echo $text | sed 's/"/\\"/g; s/'\''/\\'\''/g' )
json="{\"channel\": \"#$channel\", \"text\": \"$escapedText\"}"

curl -X POST --data-urlencode "payload=$json" "https://hooks.slack.com/services/xxxxx/xxxx/xxxxx"

its showing error like:
Code:
missing_text_or_fallback_or_attachments

but in above code I guess while loop is not working properly...is that correct way of reading multi line variable and pass on to payload in curl statement
This is resolved now. Omitting loop for text variable and updating escapedtext = "$text" does the trick..

Thank you for all help and patience..
# 14  
Old 11-20-2018
Shell variables in command arguments must always in "quotes", for example: echo "$text" (otherwise the shell tries expansions on it.)
Maybe you can add another sed command that changes newlines to \n (two characters):
Code:
printf "%s\n" "$text" "$text1" | sed '
s/"/\\"/g; s/'\''/\\'\''/g
H;1h;$!d;x;s/\n/\\n/g
'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Merge multi-lines into one single line using shell script or Linux command

Hi, Can anyone help me for merge the following multi-line log which beginning with a " and line ending with ": into one line. *****Original Log***** 087;2008-12-06;084403;"mc;;SYHLR6AP1D\LNZW;AD-703;1;12475;SYHLR6AP1B;1.1.1.1;0000000062;HGPDI:MSISDN=12345678,APNID=1,EQOSID=365;... (3 Replies)
Discussion started by: rajeshlinux2010
3 Replies

2. Shell Programming and Scripting

Help with reformat single-line multi-fasta into multi-line multi-fasta

Input File: >Seq1 ASDADAFASFASFADGSDGFSDFSDFSDFSDFSDFSDFSDFSDFSDFSDFSD >Seq2 SDASDAQEQWEQeqAdfaasd >Seq3 ASDSALGHIUDFJANCAGPATHLACJHPAUTYNJKG ...... Desired Output File >Seq1 ASDADAFASF ASFADGSDGF SDFSDFSDFS DFSDFSDFSD FSDFSDFSDF SD >Seq2 (4 Replies)
Discussion started by: patrick87
4 Replies

3. UNIX for Beginners Questions & Answers

Script to accept Multi line inputs

Hi there, I'm trying to create a script that will accept multiple inputs by copying and pasting the strings from a notepad, hit Enter key and output the string to a text file.I'm thinking of using the read command however it will just simply get the first line. Apologies but got no idea how... (7 Replies)
Discussion started by: norbie.lopez
7 Replies

4. Shell Programming and Scripting

Reading Multi Line SQL in UNIX

Hello, Currently, I am reading few queries from the below .sql file --SOURCE TABLE NAME --SOURCE QUERY SEL COL1, COL2, COL3, COL4, COL5, COL6, COL7 WHERE COL5 = '2015-11-04 16:24:00.000000' FROM SOURCE TABLE; --TARGET TABLE NAME --TARGET QUERY SEL COLUMN1, COLUMN2, COLUMN3, COLUMN4,... (4 Replies)
Discussion started by: ronitreddy
4 Replies

5. Shell Programming and Scripting

Sed: deleting last line prevents '$' address from working in the multi-script invocation

It looks like if matching and deleting the last line confuses 'sed' so it does not recognize '$' address. Consider: sed -e '/^3/d' -e '$ a text' supposed to delete a line starting with '3' and then append 'text' after the last line of input. But, if it is the last line of input which starts... (2 Replies)
Discussion started by: msz59
2 Replies

6. Shell Programming and Scripting

Removing SAS multi line comments in UNIX

i have to remove the commented (/* . . . .*/) part which starts in one line and ends in other.help me with generic code because i have 1000 to 10k lines code which i have to remove. data one; set work.temp; input name age; infile filename; /* dfsdf dsfs sdfdf dsdd sdfsf sdfsf sfs... (4 Replies)
Discussion started by: saaisiva
4 Replies

7. Shell Programming and Scripting

Multi-line filtering based on multi-line pattern in a file

I have a file with data records separated by multiple equals signs, as below. ========== RECORD 1 ========== RECORD 2 DATA LINE ========== RECORD 3 ========== RECORD 4 DATA LINE ========== RECORD 5 DATA LINE ========== I need to filter out all data from this file where the... (2 Replies)
Discussion started by: Finja
2 Replies

8. Shell Programming and Scripting

SH script to parse string and return multi-line file

Hello all, I have been asked to exercise my shell scripting and it has been 10 plus years since I used to do it so I can not remember hardly anything and ask for your help. What I need to do is copy a line out of a file that can be 10 to 100 characters long, I then need to parse this line into... (3 Replies)
Discussion started by: Alivadoro
3 Replies

9. Shell Programming and Scripting

Multi line variable script... needs help.

I am trying to write a script that will help me put a file into excel with little manipulation. Below is a sample of the file im using. Group1:*:gid1:user,user Group2:*:gid2:user,user Group3:*:gid3:user,user,user,user,user,user,user Group4:*:gid4:user,user I marked in red the part that is... (1 Reply)
Discussion started by: rookieuxixsa
1 Replies

10. Programming

message queues and multi-process

Hi, Am supposed to use message queues to send and receive messages between the processes. when i was working on that i realised that the message qid and the message queue related data should be maintained in a shared memory so that it can be accessed by all the processes. Could anybody refer... (10 Replies)
Discussion started by: rvan
10 Replies
Login or Register to Ask a Question