Trigger Sql relative to Server or CPU load

Well to start with , We come across various daily needs where in which we find different paths to reach or bring the desired output , this post is one of them .

If you also came across kind of requirement in this post , this is for you ….Well Well , What is that requirement ?????…..Ya I’m coming to that !!

If you want to automate some of you db tasks depending on the server load , If your DB server load crosses 50 …..set this flag to ‘Y’ , if above 80 , set this flag to ‘N’ , if below 80 set the flag again to ‘Y’……………………………..

For doing that we need to primarily catch up the server load which we do with a familiar command top

We have 2 options
Using Top in batch mode (OR)
Using Uptime command


Use this if you want to go with `top` batch mode
top -b -n 1|awk -F: '/load average/ {print $5}'|awk -F, '{print $1}'
Or this if you want to go with `uptime` command
uptime|awk -F: '{print $4;}'|awk -F, '{print $1}'
Here i’m giving you a sample script to complete the task
#! /bin/sh
value=`top -b -n 1|awk -F: '/load average/ {print $5}'|awk -F, '{print $1}'`
#or use this
#value=`uptime|awk -F: '{print $4;}'|awk -F, '{print $1}'`
if [ $value -ge 50 ] ; then
#these are sample stmts i have given to show for understading purpose , replace them with your custom command
/home/db2inst1/sqllib/bin/db2 "connect to sample"
/home/db2inst1/sqllib/bin/db2 "list tables for schema DB2INST1"
else
exit;
fi
You can add else if conditions for any extra constraints checking and customize it to your need , Its not solely for a DB purpose as that we have used DB commands in the loop ,can be replaced with any of our requirement . Just crontab it and that’s it you are done .

No comments:

Post a Comment