how to send the mail alert regarding disk space in bash - linux

i am able to send mail of simple test in Linux
echo "body" | mail -s "test" xxx#yahoo.com
but below code is not working
#!/bin/bash
VAR1=$(df / | grep / | awk '{ print $5}' | sed 's/%//g')
VAR2=$(df /dev/sda1 | grep /dev/sda1 | awk '{ print $5}' | sed 's/%//g')
VAR3=$(df /dev/mapper/centos-var | grep /dev/mapper/centos-var | awk '{ print $5}' | sed 's/%//g')
THRESHOLD=50
TODAY=$(date)
if [ "$VAR1" -gt "$THRESHOLD" ] ; then
mail -s 'Disk Space Alert' rupendra#3ess.in << EOF
Date of $TODAY
Your system partition remaining free space is critically low.
/ partition used is $VAR1%
/ usr partition used is $VAR2%
/ var partition used is $VAR3%

What error did you receive?
The following one worked for me,
#!/bin/bash
set -x
VAR1=$(df / | grep / | awk '{ print $5}' | sed 's/%//g')
VAR2=$(df /dev/sda1 | grep /dev/sda1 | awk '{ print $5}' | sed 's/%//g')
VAR3=$(df /dev/mapper/centos-var | grep /dev/mapper/centos-var | awk '{ print $5}' | sed 's/%//g')
THRESHOLD=50
TODAY=$(date)
echo "VAR1=$VAR1, THRESHOLD=$THRESHOLD"
if [ "$VAR1" -gt "$THRESHOLD" ] ; then
mail -s 'Disk Space Alert' abcd#xyz.com << EOF
Date of $TODAY
Your system partition remaining free space is critically low.
/ partition used is $VAR1%
/ usr partition used is $VAR2%
/ var partition used is $VAR3%
EOF
fi

Related

Searching a specific file system in bash

I have a task which asks to write a script which displays all partitions formatted with a specific file system, given as parameter.
I have written the script but when i run it it displays '0'. What am i doing wrong?
This is my code:
#!/bin/bash
n=sudo parted -l | tail -n +8 | awk '{print $5}' | wc | awk '{print $2}'
m=sudo parted -l | tail -n +8 | awk '{print $5}'
q=sudo parted -l | tail -n +8
for i in $n; do
if [ "[ $m | sed -n ip ]" = "$1" ]; then
echo "$q | sed -n ip"
fi
done
Different approach from yours, but does it do what you need?
lsblk -f | awk '$0 ~ fs {print $NF}' fs=ext2

display used disk space including reserve disk space in bash

I need show used disk space as (used+reserved),I have created below script and planning to add used and reserved,Is there a better way to do this?
I need to display "disk total used available" in this format in GB.
#!/bin/sh
output=`df -h --output=source,size,used,avail /dev/vd* /dev/disk/* -x devtmpfs | grep -v 'Filesystem' | awk '{printf $1 "\t" $2 "\t" $3 "\t" $4 "\n" }'`
while read -r line; do
diskname=`echo $line|awk -F " " '{print $1}'`
reserved=`tune2fs -l $diskname|grep -i "Reserved block count"|awk -F ":" '{print $2}'`
reservedInGB=`echo "$((((( $reserved * 4096 ) / 1024 ) / 1024 )))"|bc -l`
total=`echo $line|awk -F " " '{print $2}'`
used=`echo $line|awk -F " " '{print $3}'`
free=`echo $line|awk -F " " '{print $4}'`
echo $diskname $total $used $free $reservedInGB
done <<< "$output"
My local emulation doesn't do --output, but try something like this - tweak to spec.
df -PB 1GB -x devtmpfs /tmp | grep -v ^Filesystem |
while read mnt size used avail cap disk
do printf "%-10s %4d %4d %4d\n" $disk $size $used $avail
done
Note that embedded spaces in the mount break this, but it handles converting to GB right in the data generation with df. Since I can't do --output I saw no reason not to use -P to make sure the mount point and its data came out on the same line. Doing a read, so reordering is easy as well, as long as the fields land correctly.
Try something like that
df -h --output=source,size,used,avail | tail -n +2 | \
while read line; \
do printf "%s\t%s\n" "$line" \
"your calc with tune2fs and ${line%%[[:space:]]*}";done

Echo result from Linux file with dynamic filename

I have a tomcat server I am trying to get a list of info on for a project. I need to get the results from /etc/default/tomcat file. However some of my servers are tomcat6 and some are tomcat7 so hardcoding the filename is not going to work.
How would I dynamically insert the filename in this batch script.
#!/bin/bash
echo Server Name: `hostname`
echo CPU: `top -b -n1 | grep "Cpu(s)" | awk '{print $2 + $4}'`
FREE_DATA=`free -m | grep Mem`
CURRENT=`echo $FREE_DATA | cut -f3 -d' '`
TOTAL=`echo $FREE_DATA | cut -f2 -d' '`
echo Internal IP : `ifconfig eth0 2>/dev/null|awk '/inet addr:/ {print $2}'|sed 's/addr://'`
echo OS Memory: `cat /proc/meminfo | grep MemTotal | awk '{ print $2 }'`
echo Operating System: `uname -mrs`
***echo Tomcat Memory: `cat /etc/default/tomcat6 | grep Xmx | awk '{ print $5}'`***
Your last command can be simplified to single awk like this:
awk '/Xmx/{print "Tomcat Memory:", $5}' "$tomcatFile"
Pass "$tomcatFile" whatever tomcat filename from ver6 or ver7.
You can get output from both tomcat files in same command using:
awk '/Xmx/{print "Tomcat Memory:", $5}' /etc/default/tomcat[67]

Read variable from AWK

I'm trying to get memory info by this command:
#!/bin/bash
set -x
cat /proc/meminfo | grep "MemFree" | tail -n 1 | awk '{ print $2 $4 }' | read numA numB
echo $numA
I'm getting this
+ awk '{ print $2 $4 }'
+ read numA numB
+ tail -n 1
+ grep MemFree
+ cat /proc/meminfo
+ echo
My attempts to read these data to variable were unsuccessful. My question is how I can read this to variables? I want to read how many memory is free like: 90841312 KB
Regards
Assign the output directly to your variable:
var=$(cat /proc/meminfo | grep "MemFree" | tail -n 1 | awk '{ print $2 $4 }')
echo $var
Using BASH you can reduce your complex commands to this:
read -r _ numA _ numB < <(grep MemFree /proc/meminfo | tail -n 1)
BTW: If you print multiple vales from awk, you need a separator:
$ echo "11 22" | awk '{print $1 }'
11
$ echo "11 22" | awk '{print $2}'
22
$ echo "11 22" | awk '{print $1 $2}'
1122
^ note no space there...
You either need a comma:
$ echo "11 22" | awk '{print $1,$2}'
11 22
Or physicality:
$ echo "11 22" | awk '{print $1" "$2}'
11 22
Because without separation, the command substitution not read what you intend:
$ read -r f1 f2 <<< $(echo "11 22" | awk '{print $1 $2}')
$ echo $f1
1122
echo $f2
# f1 got two fields and nada for f2
arr=( $(awk '/MemFree/{split($0,a)} END{print a[2], a[4]}' /proc/meminfo) )
echo "${arr[0]}"
echo "${arr[1]}"

Getting error while running script to find disk space

I am running below script:-
#!/bin/bash
threshold="20"
i=2
result=`df -kh |grep -v “Filesystem” | awk ‘{ print $5 }’ | sed ‘s/%//g’`
for percent in $result; do
if ((percent > threshold))
then
partition=`df -kh | head -$i | tail -1| awk ‘{print $1}’`
echo “$partition at $(hostname -f) is ${percent}% full”
fi
let i=$i+1
done
But I get the following error:
awk: ‘{
awk: ^ invalid char '▒' in expression
sed: -e expression #1, char 1: unknown command: `▒'
Please help me to resolve this.
What awk does not work? (your script does work fine on my Ubuntu)
This line:
result=`df -kh |grep -v "Filesystem" | awk '{ print $5 }' | sed 's/%//g'`
could be changed to:
result=$(df -kh | awk '!/Filesystem/ {print $5+0}')
Avoid using old and outdated backtics if parentheses works like this: var=$(code...)
This:
partition=`df -kh | head -$i | tail -1| awk '{print $1}'`
could be changed to:
partition=$(df -kh | awk -v line="$i" 'NR==line {print $1}')
This
let i=$i+1
could be change to:
((i++))
This would then give some like this:
#!/bin/bash
threshold="20"
i=2
result=$(df -kh | awk '!/Filesystem/ {print $5+0}')
for percent in $result; do
if ((percent > threshold))
then
partition=$(df -kh | awk -v line="$i" 'NR==line {print $1}')
echo "$partition at $(hostname -f) is ${percent}% full"
fi
((i++))
done
You're using ‘ for a single quote not '. Try re-encoding your file with an editor.
You got the answer to your syntax error, now re-write the whole script as just:
#!/bin/bash
df -kh |
awk -v t=20 -v h="$(hostname -f)" '(NR>1)&&($5+0>t){printf "%s at %s is %s full\n",$1,h,$5}'

Resources