Linux find cpu information in details:

Script to get cpu info in details.

 

PHYSICAL_CPU=`grep ‘physical id’ /proc/cpuinfo | sort | uniq | wc -l`
VIRTUAL_CPU=`grep ^processor /proc/cpuinfo | wc -l`
CORES=`grep ‘cpu cores’ /proc/cpuinfo | head -1 | cut -f2 -d “:”`
MEMORY=`cat /proc/meminfo  | head -1 | awk ‘{printf “%.0f”,$2/(1024*1024)}’`
CPU_SPEED=`grep “^cpu MHz” /proc/cpuinfo | head -1 | awk -F”:” ‘{printf “%0.2f”,($2/1000)}’`
CPU_CACHE_SIZE=`grep “^cache size” /proc/cpuinfo| head -1 | awk -F”:” ‘{print $2}’`
KERNEL=`uname -m`

ARCHS=`grep flags /proc/cpuinfo | uniq | egrep -o -w “tm|lm” | wc -l`

if [ ${ARCHS} -eq 2 ]
then
SUPPORTED_ARCH=”x86_64,x86″
else
SUPPORTED_ARCH=”x86″
fi

echo “Hostname: $HOSTNAME”
echo -n “Physical Processors: ”
echo ${PHYSICAL_CPU}

echo -n “Virtual Processors: ”
echo ${VIRTUAL_CPU}

echo -n “CPU Cores: ”
echo ${CORES}

echo -n “CPU Speed: ”
echo “${CPU_SPEED} GHz”

echo -n “Cache Size: ”
echo “${CPU_CACHE_SIZE}”

echo -e “Memory: ${MEMORY}G”

echo “Kernel Arch: ${KERNEL}”

echo “CPU Arch: ${SUPPORTED_ARCH}”

echo “Notes:”
if [ ${CORES} -eq 1 -a ${VIRTUAL_CPU} -gt ${PHYSICAL_CPU} ]
then
echo -e “\tCPU is Hyperthreading”
fi

if [ ${ARCHS} -eq 2 -a `echo ${SUPPORTED_ARCH} | grep -c ${KERNEL}` -eq 0 ]
then
echo -e “\tHardware is 64-bit while installed kernel is 32-bit”
fi

How to catch 500 error from error logs in apache

A. Enable cgi for your apache. Add following.

1) LoadModule cgid_module modules/mod_cgid.so

2)

<Directory “/appl/apache2/cgi-bin”>

AllowOverride None

Options ExecCGI

Order allow,deny

Allow from all

</Directory>

3)

ScriptAlias /cgi-bin/ “/appl/apache2/cgi-bin/”

AddHandler cgi-script .cgi

ErrorDocument 413 /cgi-bin/error.cgi

4) Restart apache.

B. Set up the following python script to catch this error, send an email to admin and give the custome message to users.

/appl/apche/cgi-bin/error.cgi

chmod +x /appl/apache/cgi-bin/error.cgi

#!/usr/bin/python
import sys, os
SENDMAIL = “/usr/sbin/sendmail” # sendmail location
print “Content-Type: text/htmlnn”;
if (os.environ[“REDIRECT_STATUS”] == “413”) or (os.environ[“REDIRECT_STATUS”] == “500”):
stats = “<table border=1><tr><td>Variable</td><td>Value</td></tr>”
for name, value in os.environ.items():
stats += “<tr><td>%s</td><td>%s</td></tr>” % (name,value)
stats += “</table>”
sendmail_location = “/usr/sbin/sendmail” # sendmail location
p = os.popen(“%s -t” % “/usr/sbin/sendmail”, “w”)
p.write(“From: %sn” % “error-reporter@domain.com”)
p.write(“To: %sn” % “mail@domain.com”)
p.write(“Content-Type: text/htmln”)
p.write(“Subject: Error %s in accessing n” % os.environ[“REDIRECT_STATUS”])
p.write(“n”) # blank line separating headers from body
p.write(stats)
status = p.close()
”’print “<H3><center>Inconvenience Regretted.  Team has been notified of this issue</center></h3>””’
cookieclearjs=”””
<script language=’JavaScript’>
var todate = new Date ( );
todate.setTime ( todate.getTime() – 100000 );
var domain_url_del = window.location.href;
var domain_Name_url_del = domain_url_del.split(“http://”);
var domain_Name_temp_del = domain_Name_url_del[1].split(“/”);
var domain_Name_del = domain_Name_temp_del[0];
var cookieList = document.cookie.split(‘;’);
for(var i=0;i < cookieList.length;i++)
{
var name = cookieList[i];
if(name.indexOf(“MyLinks”)!=-1)
{
document.cookie = ”+name+’=; path=//APPLICATION/PATH; domain=.’ + domain_Name_del + ‘; expires=’ + todate.toGMTString();
document.cookie = ”+name+’=; path=//APPLICATION/PATH; domain=.’ + domain_Name_del + ‘; expires=’ + todate.toGMTString();
}
}
</script>
“””
print cookieclearjs
print “<script language=’JavaScript’>window.location=’%s'</script>” % os.environ[“REDIRECT_SCRIPT_URI”]
else:
print “<H3><center>What you are looking for, is not here</center></h3>”

Script for oracle Database monitoring

shell script for DB monitoring.

[root@MY_SERVER1 monitor]#
#!/bin/bash
count=0;
SCRIPT_HOME=”/Your/folder/”
MONITOR_SCRIPT=”${SCRIPT_HOME}/MyEnv_db_monitor.pl”
LOCKFILE=/tmp/MyEnv_db_monitor.lock

if [ -f “${LOCKFILE}” ]
then
(
echo “${LOCKFILE} ALREADY EXISTS.. DB monitor will not run”
ls -la ${LOCKFILE}
) | mail -s “MY ENVIRONMENTDB Monitor is not running. Lock” MY_EMAIL@DOMAIN.com
else
touch ${LOCKFILE}
dblisteners=(IP2:PORT2 IP2:PORT1)
len=${#dblisteners[*]} #Num elements in array

#echo “dblisteners are $len members.They are:”
i=0
while [ $i -lt $len ]; do
#       echo “${dblisteners[$i]}”
server=`echo “${dblisteners[$i]}” | cut -d: -f1`
port=`echo “${dblisteners[$i]}” | cut -d: -f2`
#       echo $server $port
${MONITOR_SCRIPT} $server $port > /dev/null 2>&1
if [ $? -eq 0 ]
then
count=$((count+1))
fi

let i++
done
# ${MONITOR_SCRIPT} > /dev/null 2>&1
if  [ $count -eq 2 ]
then
rm ${LOCKFILE}
fi
fi

——————————————————————

[root@MY_SERVER1 monitor]# cat /Your/folder/MyEnv_db_monitor.sh
#!/bin/bash
count=0;
SCRIPT_HOME=”/Your/Folder”
MONITOR_SCRIPT=”${SCRIPT_HOME}/MyEnv_db_monitor.pl”
LOCKFILE=/tmp/MyEnv_db_monitor.lock

if [ -f “${LOCKFILE}” ]
then
(
echo “${LOCKFILE} ALREADY EXISTS.. DB monitor will not run”
ls -la ${LOCKFILE}
) | mail -s “MY ENVIRONMENTStage DB Monitor is not running. Lock” MY_EMAIL@DOMAIN.com
else
touch ${LOCKFILE}
dblisteners=(IP1:PORT1 IP2:PORT2)
len=${#dblisteners[*]} #Num elements in array

#echo “dblisteners are $len members.They are:”
i=0
while [ $i -lt $len ]; do
#       echo “${dblisteners[$i]}”
server=`echo “${dblisteners[$i]}” | cut -d: -f1`
port=`echo “${dblisteners[$i]}” | cut -d: -f2`
#       echo $server $port
${MONITOR_SCRIPT} $server $port > /dev/null 2>&1
if [ $? -eq 0 ]
then
count=$((count+1))
fi

let i++
done
# ${MONITOR_SCRIPT} > /dev/null 2>&1
if  [ $count -eq 2 ]
then
rm ${LOCKFILE}
fi
fi
[root@MY_SERVER1 monitor]# cat /Your/folder/MyEnv_db_monitor.pl
#!/usr/bin/perl -w

use warnings;
use strict;
use DBI;

BEGIN {
$ENV{ORACLE_HOME}=’/opt/oracle/product/9.2.0′;
$ENV{LD_LIBRARY_PATH}=’/opt/oracle/product/9.2.0/lib’;
}

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
my $timestamp = sprintf(“%4d-%02d-%02d %02d:%02d:%02dn”,$year+1900,$mon+1,$mday,$hour,$min,$sec);
chomp $timestamp;
print “The time is $timestampn”;

my $server = $ARGV[0];
my $appsport = $ARGV[1];
chomp $server; chomp $appsport;
print “The server is $server connecting to $appsport n”;
my $mail = “/usr/lib/sendmail -t”;
my $recipient = “MY_EMAIL@DOMAIN.COM”;
my $from = “MyEnv_db_monitor@DOMAIN.COM”;
my $timeOut = 60; # wait 60 seconds for connection
my $db = “DBNAME”;
my ($username, $password) = (‘DB_SCHEMA_NAME’,’DB_PASSWORD’);
#my $db_conn_dsn=”dbi:Oracle:host=DB_SERVER_NAME;port=PORT;sid=$db”;
my $db_conn_dsn=”dbi:Oracle:host=DB_SERVER_NAME;port=${appsport};sid=$db”;
my $flag = “/tmp/dbdbflag2.txt$server”;
my $qflag = “/tmp/dbtaskqueflag2.txt$server”;
my $rowcount = “”;
my $dummyflag = “/tmp/dbdummyflag2.txt$server”;

if ( -e $flag ) { # Check for DB status flag file
print “DB Flag exists.n”;
} else { # Create flag file
system “/bin/touch $flag”;
}

if ( -e $qflag ) { # Check for task que status flag file
print “Task que flag exists.n”;
} else { # Create flag file
system “/bin/touch $qflag”;
}

my $flagstatus = `/bin/cat $flag`;
chomp $flagstatus;
print “The status of the db flag is $flagstatusn”;

my $qflagstatus = `/bin/cat $qflag`;
chomp $qflagstatus;
print “The status of the task que flag is $qflagstatusn”;

if ( -e $dummyflag ) { # Check for Dummy status flag file
print “Dummy Flag exists.n”;
system “/bin/touch $dummyflag”;
} else { # Create flag file
system “/bin/touch $dummyflag”;
}

my $mtime_qflag = (stat($qflag))[9];
my $mtime_dflag = (stat($dummyflag))[9];

print “The mtime for qflag is $mtime_qflag secsn”;
print “The mtime for dflag is $mtime_dflag secsn”;

my $refresh_interval = ($mtime_dflag-$mtime_qflag)/3600;

print “The que flag has been refreshed since $refresh_interval hoursn”;

if (( $refresh_interval > 24 ) && ( $qflagstatus ne “LESS” )) {
system “echo BLANK > $qflag”;
} else {
print “The refresh interval is $refresh_interval hours and the flag status is $qflagstatusn”;
}

if ($db) {
print “checking $dbn”;
my $dbh=”;
eval {
#set alarm to timeout current operation
local $SIG{ALRM} = sub {die “connection timeoutn”};
alarm $timeOut;
print “Trying to connect to $db with $username and $passwordn”;
$dbh = DBI->connect($db_conn_dsn,$username,$password, { RaiseError => 1 } ) || die “Database connection not made: $DBI::errstr”;
};
alarm 0; # reset the alarm
if ($dbh) { # success
print “Connection succeeded for $dbn”;

my $sql = “select count(*) from vap_default.taskqueue”;
my $sth = $dbh->prepare($sql) or die “Cannot prepare sql statement” . $dbh->errstr(). “n”;
$sth->execute() or die “cannot execute sql query” . $sth->errstr() . “n”;
($rowcount) = $sth->fetchrow_array();
$sth->finish;
print “The task queue row count is $rowcountn”;

$dbh->disconnect;

print “DB is now up.n”;
if ( $flagstatus eq “DOWN” ) {
system “echo ‘RECOVERED’ > $flag”;
print “The DB has just recovered at $timestamp.n”;
&upmail;
} else {
print “Do nothing. DB was anyways up.n”;
}

print “Just outside failure blockn”;

} else { # failure
print “Inside failure blockn”;
if ( $flagstatus eq “DOWN” ) {
print “Do nothing. DB is still downn”;
} else {
print “Error connecting to $db at $timestampn”;
&downmail;
system “echo ‘DOWN’ > $flag”;
}
}
}

sub downmail {
open(MAIL, “|$mail”);
print MAIL “To: $recipientn”;
print MAIL “From: $fromn”;
print MAIL “Subject: MY ENVIRONMENTStage DATABASE_NAME on  Database Connection Failure $server $appsportn”;
print MAIL “$timestamp: FAILED CONNECTION TO MY ENVIRONMENT DATABASE.nnTurning Big-IP monitors OFF. $server $appsport”;
close MAIL;
system(“/Your/folder/disable_remote_tomcats.sh $server $appsport”);
}

sub upmail {
open(MAIL, “|$mail”);
print MAIL “To: $recipientn”;
print MAIL “From: $fromn”;
print MAIL “Subject: MY ENVIRONMENT DATABASE_NAME on  Database Connection Recovered $server $appsportn”;
#print MAIL “$timestamp: RECOVERED CONNECTION TO MY ENVIRONMENT DATABASE.nnTurning Big-IP monitors ON”;
print MAIL “$timestamp: RECOVERED CONNECTION TO MY ENVIRONMENTDATABASE.”;
close MAIL;
system(“/Your/folder/enable_remote_tomcats.sh $server $appsport”);
}

sub qmoremail {
open(MAIL, “|$mail”);
print MAIL “To: $recipientn”;
print MAIL “From: $fromn”;
print MAIL “Subject: MY ENVIRONMENTDATABASE_NAMEon  Vignette Task Queue Record Countn”;
print MAIL “$timestamp: TASK QUEUE RECORD COUNT = $rowcountnn”;
close MAIL;
}

sub qlessmail {
open(MAIL, “|$mail”);
print MAIL “To: $recipientn”;
print MAIL “From: $fromn”;
print MAIL “Subject: MY ENVIRONMENTDATABASE_NAMEon  Vignette Task Queue Record Countn”;
print MAIL “$timestamp: TASK QUEUE RECORD COUNT = $rowcountnn”;
close MAIL;
}

——————————————————————————————

Not Mendatary– but require in my environment thats why I added this.. In order to enable disaster recovery site.

[root@MY_SERVER1 monitor]# cat “/Your/folder/disable_remote_tomcats.sh”
#!/bin/bash

tomcat_user=”tomcat0″;
tomcat_node=$1;
#for tomcat_node in ${nodes}
#do
for tomcats in `seq 0 1`
do
ssh ${tomcat_user}@${tomcat_node} “mv /appl/tomcat${tomcats}/webapps/portal/Status/Status.jsp /appl/tomcat${tomcats}/webapps/portal/Status/Status.jsp.old”
done
#done

echo “${tomcat_node}  tomcat nodes are disabled in BigiP.” | mail -s “${tomcat_node} tomcat nodes are disabled” MY_EMAIL@DOMAIN.com
#echo “All tomcat nodes are disabled in BigiP. Enabling DR nodes” | mail -s “[DR Enabled] MY ENVIRONMENTApp Status Pages” YOUR_EMAIL@DOMAIN.COM
#/Your/folder/enable_dr.sh

[root@MY_SERVER1 monitor]#

[root@MY_SERVER1 monitor]# cat /Your/folder/enable_dr.sh
#!/bin/bash
drnodes=”MY_DR_NODE1 MY_DR_NODE2″

for dr_node in ${drnodes}
do
/usr/bin/ssh root@${dr_node} “mv /appl/webdocs/portal/Status/Status.jsp.down /appl/webdocs/portal/Status/Status.jsp”
done

echo “enabling DR nodes on `/bin/date`.” | /bin/mail -s “enabling DR nodes” YOUR_EMAIL@DOMAIN.COM MY_EMAIL@DOMAIN.COM

[root@MY_SERVER1 monitor]#

——————————————————————————————
[root@MY_SERVER1 monitor]# cat “/Your/folder/enable_remote_tomcats.sh”
#!/bin/bash

#nodes=”MY_SERVER1 MY_SERVER2″
tomcat_user=”tomcat0″;
tomcat_node=$1;

#for tomcat_node in ${nodes}
#do
for tomcats in `seq 0 1`
do
ssh ${tomcat_user}@${tomcat_node} “mv /appl/tomcat${tomcats}/webapps/portal/Status/Status.jsp.old /appl/tomcat${tomcats}/webapps/portal/Status/Status.jsp”
done
#done

#echo “${tomcat_node} All Tomcat Status pages are up. Trying to handle DR nodes…” | mail -s “[Restored] MY ENVIRONMENT App Status Pages” MY_EMAIL@DOMAIN.com
echo “${tomcat_node} All Tomcat Status pages are up.” | mail -s “[Restored] MY ENVIRONMENT App Status Pages” MY_EMAIL@DOMAIN.com
#/Your/folder/manage_dr.sh

[root@MY_SERVER1 monitor]#

[root@MY_SERVER1 monitor]# cat /Your/folder/manage_dr.sh
#!/bin/bash
#set -o xtrace
drnodes=”MY_DR_NODE2 MY_DR_NODE1″
poolcount1=0;
poolcount2=0;
STATUS_URI=”portal/Status/Status.jsp”
pool1=”URL1
URL2″
pool2=”URL3
URL4″

for MyEnv_node in ${pool1}
do
status=`/usr/bin/lynx –connect_timeout=3 –dump http://${MyEnv_node}.corporate.ge.com/${STATUS_URI} | grep “ITS_UP” | wc -l`
poolcount228=$((poolcount228+status))
done

for MyEnv_node in ${pool2}
do
status=`/usr/bin/lynx –connect_timeout=3 –dump http://${MyEnv_node}/${STATUS_URI} | grep “ITS_UP” | wc -l`
pool2count=$((pool2count+status))
done

#if [ $pool1count -ge 2 -a ${pool2count} -ge 2 ]
if [ $pool2count -ge 2 ]
then
for dr_node in ${drnodes}
do
/usr/bin/ssh root@${dr_node} “mv /appl/webdocs/portal/Status/Status.jsp /appl/webdocs/portal/Status/Status.jsp.down”
done
echo “disabling DR nodes on `date`. Will be re-enabled in 20 mins” | mail -s “Disabling DR nodes” YOUR_EMAIL@DOMAIN.COM MY_EMAIL@DOMAIN.COM
/usr/bin/at -f /Your/folder/enable_dr.sh  now + 20 min
else
echo -e “DR Nodes still kept UP, since dynamic nodes are not upnWill retry in 5 mins from now” | mail -s “DR nodes still UP” YOUR_EMAIL@DOMAIN.COM MY_EMAIL@DOMAIN.COM
/usr/bin/at -f /Your/folder/manage_dr.sh  now + 5 min
fi

[root@MY_SERVER1 monitor]#