Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts

Wednesday, August 8, 2018

FileSystem Monitoring shell script

#!/bin/bash -xv
#
export PATH=/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/oracle/bin:/home/oracle/bin::/mnt/mnt02/app/oracle/product/12.2.0.1/db_1/bin:/mnt/mnt02/app/oracle/product/12.2.0.1/db_1/OPatch

# To debug uncomment the below line
#set -x
#
# Set Environment Variables
#
TODAY="at $(date '+%H:%M on %d-%b-%y')"
OutputFilename=$(date +"%b_%d_%Y".html)
LastReport=/tmp/LastReport.txt
NowReport=/tmp/NowReport.txt
CurDate=`date +%D\ %T\ %Z`
#
# Set Alert Type according to Percentage
#
CriticalPercentage=90
WarningPercentage=85
NormalPercentage=70
#
# Get IP Address set from hosts file
#
IPADDRESS=`head -n 1 /etc/hosts | awk ' { print $1 }'`
#
# Remove Output File, if it already exists
#
if [ -f /tmp/${OutputFilename} ]; then
 rm -f /tmp/${OutputFilename}
fi
if [ -f ${NowReport} ]; then
 mv ${NowReport} ${LastReport}
 cp /dev/null ${NowReport}
fi
#
# Find out Difference Previous and Current Report for each partition
#
df -Ph | grep -vE "^Filesystem|tmpfs|cdrom" | awk 'NR>0 && NF==6' | awk '{print $3}' > ${NowReport}
if [ -f ${LastReport} ]; then
 DiffValue=(`awk '{ getline getdifference < "/tmp/LastReport.txt"; $1 -= getdifference; print }' /tmp/NowReport.txt`)
fi
#
# Defining HTML Table Format & Fields
#
(
 echo '<HTML><HEAD><TITLE>Disk Usage Statistics</TITLE></HEAD>'
 echo '<BODY>'
 echo '<H3><font color="#5555E7" face="verdana" size="3">Disk Usage Report for server - '$(uname -n) for ${IPADDRESS}'</font></H3>'
 echo '<P style="color:#5555E7" face="verdana" size="3">Report Generated '${TODAY}'</P>'
 echo '<TABLE BORDER=1 CELLSPACING=0 CELLPADDING=2 bordercolor=#DEDEE2>'
echo '<style>
 body {
     font-family: verdana;
         font: 16px verdana, sans-serif;
}
tbody th { text-align:right; background: #F8F8FA; color:red}
</style>'
# echo '<TR BGCOLOR=\"#BBFFFF\"> <TH><font color="#5555E7"><font face="verdana"><b>Filesystem</b></font></TH> <TH>Total</TH> <TH>Disk Used</TH>
#<TH>Available</TH> <TH>Percentage Info</TH> <TH>Mounted On</TH> <TH>Critical Alert</TH>
#<TH>Report Date</TH> <TH>12 Hrs Difference </TH><//TR>'
echo '<TR BGCOLOR=\"#4863A0\"> <TH><font color="#5555E7" face="verdana" size="2"><b>Filesystem</b></font></TH> <TH><font color="#5555E7" face="verdana" size="2"><b>Total</b></font></TH> <TH><font color="#5555E7" face="verdana" size="2"><b>Disk Used</b></font></TH><TH><font color="#5555E7" size="2" face="verdana"><b>Available</b></font></TH> <TH><font color="#5555E7" size="2" face="verdana"><b>Percentage Info</b></font></TH> <TH><font color="#5555E7" size="2" face="verdana"><b>Mounted On</b></font></TH> <TH><font color="#5555E7" size="2" face="verdana"><b>Critical Alert</b></font></TH></TR>'

#
# Extract Disk Usage Information
# Suppress Listing of “FileSystem, tmpfs and cdrom” Information
#
ArryCount=0
df -Ph | grep -vE "^Filesystem|tmpfs|cdrom" | awk 'NR>0 && NF==6'|sort|while read FileSystem Size DiskUsed DiskFree DiskPercentUsed MountPoint
 do
 PERCENT=${DiskPercentUsed%%%}
#
# Calculate the Difference between previous run and current run
#
 TDiffValue=(`awk '{ getline getdifference < "/tmp/LastReport.txt"; $1 -= getdifference; print }' /tmp/NowReport.txt`)
#
# Verify if disk usage is greater equal to than set threshold limit - 90%
#
 if [[ ${PERCENT} -ge ${CriticalPercentage} ]];
 then
 COLOR=red
 CRITICALALERT="Yes, Notify"
 elif [ ${PERCENT} -ge ${WarningPercentage} ] && [ ${PERCENT} -le 90 ];
 then
 COLOR=orange
 CRITICALALERT=No
 else
 COLOR=green
 CRITICALALERT=NA
 fi
 echo '<TR><TD>'$FileSystem'</TD><TD ALIGN=RIGHT>'$Size'</TD>'
 echo '<TD ALIGN=RIGHT>'$DiskUsed'</TD><TD ALIGN=RIGHT>'$DiskFree'</TD>'
 echo '<TD><TABLE BORDER=0 CELLSPACING=3 CELLPADDING=0>'
 echo '<TR><TD WIDTH='$((2 * $PERCENT))' BGCOLOR="'$COLOR'"></TD>'
 echo '<TD WIDTH='$((2 * (100 - $PERCENT)))' BGCOLOR="gray"></TD>'
 echo '<TD><FONT FONT-WEIGHT="bold" SIZE=-1
COLOR="'$COLOR'">'$DiskPercentUsed'</FONT></TD>'
 echo '<TR></TABLE><TD>'$MountPoint'</TD>'
 echo '<TD><FONT font-weight="bold">'$CRITICALALERT'</TD></FONT>'
# echo '<TD><FONT font-weight="bold">'`date`'</TD></FONT>'
3 echo '<TD><FONT font-weight="bold">'${TDiffValue[ArryCount]} \(in bytes\)'</TD></FONT></TR>'
 echo $DiskUsed >> `hostname`.usage.txt
 ArryCount=$ArryCount+1
 done
 echo '</TABLE>'
 echo '</P><BR>'
 echo '<TABLE BORDER=1 CELLSPACING=3 CELLPADDING=0>'
 echo '<TR><TH FONT font-weight="bold">Legend Information</TH></TR>'
 echo '<TR><TD FONT color="white" BGCOLOR="RED">Critical Alert</TD></TR>'
 echo '<TR><TD FONT color="white" BGCOLOR="ORANGE">Warning Alert</TD></TR>'
 echo '<TR><TD FONT color="white" BGCOLOR="GREEN">No Action Alert</TD></TR>'
 echo '</TABLE>'
 echo '<BODY></HTML>'
 echo '<P><FONT font-weight="bold">Report Generated by DBA Team</P>'
) | tee `hostname`_${0##*/}.html
#
# Sending E-Mail Notification
#
(
 echo To: "#DL-OracleSYSDBA@XXXX.com"
 echo From: oracle@XXXXX.localdomain
 echo "Content-Type: text/html; "
 echo Subject: Disk Usage Report for server `hostname` 'for' $IPADDRESS
 echo
 cat `hostname`_${0##*/}.html
) | sendmail -t
echo -e "Report Generation is Completed... \n\a"

Sample Output


How to Clear Cache on Linux?

Every Linux System has three options to clear cache without interrupting any processes or services.
1. Clear PageCache only.
# sync; echo 1 > /proc/sys/vm/drop_caches
2. Clear dentries and inodes.
# sync; echo 2 > /proc/sys/vm/drop_caches
3. Clear PageCache, dentries and inodes.
# sync; echo 3 > /proc/sys/vm/drop_caches
Explanation 
sync will flush the file system buffer. Command Separated by ";" run sequentially. The shell wait for each command to terminate before executing the next command in the sequence. As mentioned in kernel documentation, writing to drop_cache will clean cache without killing any application/service, command echo is doing the job of writing to file.
As a root user




Monday, February 22, 2016

Sending Email Alerts Through Cron

Cron is the Linux task scheduler that is responsible for making sure scripts run at their specified times. Cron is often used for backup scripts and running custom scripts. In the event a task runs into problems or errors Cron generally tries to email the local administrator of the machine. this means it tries to send an email to itself.
We can change this default behavior by changing the MAILTO variable (It won't work if you have not setup an email server or SMTP)
Setting MAILTO variable
Edit the crontab by entering crontab -e and add
MAILTO=your.email@your_provider.com
It should look something as below
Specify email for each script
If we don’t want all output to go to the same email address we can specify the output of a particular script to go to a different email address
0 8 * * * /mnt/dbbkup/Databkup/FULL_SCHEMA_BACKUPS/ETL/SQLs/load.sh | mail -s "QA Load 8AM" someaddress@email.com

If you don't want email alerts add ">/dev/null 2>&1" at the end of the command
0 8 * * * /mnt/dbbkup/Databkup/FULL_SCHEMA_BACKUPS/ETL/SQLs/load.sh >/dev/null 2>&1

Wednesday, December 9, 2015

How to find out Linux System is 32-bit or 64-bit

$ arch
x86_64
$ getconf LONG_BIT
64
$ file /sbin/init
/sbin/init: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, stripped

Tuesday, December 8, 2015

CRONTAB

# 1. Entry: Minute when the process will be started [0-60]
# 2. Entry: Hour when the process will be started [0-23]
# 3. Entry: Day of the month when the process will be started [1-28/29/30/31]
# 4. Entry: Month of the year when the process will be started [1-12]
# 5. Entry: Weekday when the process will be started [0-6] [0 is Sunday]

# Minute   Hour   Day of Month       Month          Day of Week        Command    
# (0-59)  (0-23)     (1-31)    (1-12 or Jan-Dec)  (0-6 or Sun-Sat)   
    0           2          12                   *                       *              /usr/bin/find


Use the command "crontab -e" to edit your crontab file.

Use the command "crontab -l" to list your crontab file.

Sunday, December 6, 2015

SCP as a background process without using password

SCP as a background process without using password

To execute any linux command in background we use nohup as follows:

$ nohup SOME_COMMAND &
But the problem with scp command is that it prompts for the password (if password authentication is used). So to make scp execute as a background process do this:

$ nohup scp file_to_copy user@server:/path/to/copy/the/file > nohup.out 2>&1
Then press ctrl + z which will temporarily suspend the command, then enter the command:

$ bg
This will start executing the command in backgroud

Monday, November 16, 2015

Linux: Delete Files Older than x number of days

Delete files older than x number of days (in my case 5 days)

find /temp -type f -name '*.dmp' -mtime +5 -exec rm -rf {} \;