Send SMS using Shell Script, checking Transaction Count of Table in Oracle

Being a DBA requires us many a times to step outside our role and solve a problem in a smart way. One of the issues we faced recently with one of our application, was the application server hanging and not being able to function properly. The application team did not have any means of getting a quick alert and depending on contact center  to get alerts about the application being down. This required the application server to be monitored round the clock using SMS alerts. Now the application itself wasn’t smart enough to do this. So i did a bit of analysis on the application to figure out a way . Now the Oracle database on Exadata rarely if ever goes down (A very Good DBA team 🙂 ) So to find out the application has a problem, we figured to keep the database as a reference point of availability would be best.

The application writes to a table called authorizations. This being a payment gateway application the transaction count is very high. We receive 24/7 about 10TPS. So in a minute the transaction count will never be below 500-600 anytime . So the first step in creating this script is to create a query to check the transaction count and spool it to file called smscard.txt

select count(*) from authorizations where ltimestamp > sysdate – 5/1440 and I039_RSP_CD=’00’;

SQL>
COUNT(*)
———-
177
This will give us the transaction count in the last 5 minutes

Next We create a logic to check this count with a defined threshold. Using AWK we can remove the garbage information and get the count(*) output

####### AWK BLOCK #########
awk ‘NF=5’ /u03/scripts/smscard.txt > /u03/scripts/trimoutput
pattern=”$(awk ‘NR==5 {print;exit}’ /u03/scripts/trimoutput)”
if [[ “$pattern” -lt 3 ]]
then
sh /u03/scripts/newftptsys.sh
fi

First Line Removes the Extra spaces from the smscard.txt file and writes to file “trimoutput”

Second Prints Line 5 and writes to variable “pattern” from  “trimoutput” file

Third Line checks IF pattern is less than threshold. If condition is true it sends an SMS file to the SMS gateway using FTP. Our SMS gateway requires a text file with mobile numbers to be send to a FTP server.

So there are 3 scripts. A FTP script, a SQL script and the Master script. The Master script calls the SQL script and the FTP script and sends the SMS file based on the IF THEN logic. Below are the 3 scripts

FTP Script : /u03/scripts/newftptsys.sh

#!/bin/bash
USER=abc
PASSWD=abc
FILE=SMSTSYS.TXT
HOST=prodlive
ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
lcd /u03/scripts
cd /QDLS/SMSDTA
put $FILE
quit
END_SCRIPT
exit 0

SQL Script : /u03/scripts/counttsys.sql

select count(*) from authorizations where ltimestamp > sysdate – 5/1440 and I039_RESP_CD=’00’;

Main/Master Script : /u03/scripts/smsTSYS.sh

#!/bin/bash
current_time=$(date “+%Y%m%d%H%M%S”)
export ORACLE_HOME=/u03/app/oracle/product/12.1.0.2/payapps
cd /u03/scripts
$ORACLE_HOME/bin/sqlplus xyz/******@onlnprd << EOF
whenever sqlerror exit sqlcode
set serveroutput on size 1000000
set feedback off
set line 200
spool /u03/scripts/smscard.txt
@/u03/scripts/counttsys.sql
spool off
exit
EOF
rm -f /u03/scripts/SMSTSYS.TXT
touch /u03/scripts/SMSTSYS.TXT
chmod 777 /u03/scripts/SMSTYS.TXT
echo “$current_time,ACQUIRER,33578971,30116935″>/u03/scripts/output
cat /u03/scripts/output > /u03/scripts/SMSTSYS.TXT
####### AWK BLOCK #########
awk ‘NF=5’ /u03/scripts/smscard.txt > /u03/scripts/trimoutput
pattern=”$(awk ‘NR==5 {print;exit}’ /u03/scripts/trimoutput)”
if [[ “$pattern” -lt 3 ]]
then
sh /u03/scripts/newftptsys.sh
fi

The Final Script will generate an SMS file called SMSTYS.TXT in format with mobile numbers and send to the SMS ftp folder based on the count(*) output being less than threshold

20161129124001,ACQUIRER,33578971,30116935

Summary:

Using a little bit of logic and the magic of Shell scripts we created a customized SMS probe for a very critical business application. Even an Email alert can be configured using a similar mechanism. A bit of innovation goes a long way to solve a business problem  🙂

Advertisements

Monitor Tablespace Script Oracle 12c 11g 10g

The Below Script will generate output if percentage of tablespace space left free is less tan 20%. You can easily put in a shell script and schedule it on crontab to send alerts on a daily basis.

 

select df.tablespace_name tspace,
round(sum(fs.bytes)/(df.bytes) * 100) “%_free”,
round(sum(fs.bytes)/(1024*1024)) free_ts_size,
df.bytes/(1024*1024) tot_ts_size
from dba_free_space fs, (select tablespace_name, sum(bytes) bytes from dba_data_files
group by tablespace_name ) df
where fs.tablespace_name = df.tablespace_name
group by df.tablespace_name, df.bytes
having round(sum(fs.bytes)/(df.bytes) * 100) < 20;

Simple Shell Script to Check Diskspace and Send Email on AIX

The below simple shell script will check all filesystems and create output file for only filesystems which are more than 80% full and send the log by email.

The below has been checked on AIX7.1 , not too Sure if it works on Solaris. I suppose the -P flag does not work on Solaris.


#!/bin/ksh
df -P | grep -v Capacity | awk '{if ($5 >= 80) {print $5 $6;}}' > /home/root/diskspacelog
mailx -s "DiskSpace for Server Esb1" </home/root/diskspacelog sysadmin@company.com

Shell Script to Monitor AIX Filesystem and Send Email

The Below Shell Script checks the Filesystem mount points and using AWK outputs all filesystem exceeding 90% space to a file called diskspacepoll. Once that is done the sed command removes any special character like ‘%’ from the output file and cleans it to a file called output.log

The next important logic is in the AWK block. Here a variable called pattern is defined using the threshold of 90%. Another variable called var is defined. This is your baseline metric. So it value of pattern exceeds var then the mail is dispatched else the script does nothing. You can put this in crontab as a every 5 minute job to continuously poll the filesystems and incase the threshold is exceed it will dispatch an email immediately to the admin


#!/bin/ksh
df -P | grep -v Capacity | awk '{if ($5 >= 90) {print $5;}}' > /home/root/diskspacepoll
sed 's/[!@#\$%^&*()]//g' /home/root/diskspacepoll > /home/root/output.log
####### AWK LOGICAL BLOCK #########
pattern=$(awk '$1 > 90 {print $1}' /home/root/output.log)
var=90
if [[ $pattern > $var ]]
then
echo "Please Check with System Administrator" | mailx -s "90% Threshold of DiskSpace exceeded on Server 1 (ESB1)" sysadmin@company.com
fi