My script is sending non-stop - bash

My shell script below is sending non-stop even if the parameters are not being met already:
MAX=85
EMAIL="my#email.com"
PART=sda1
USE=`df -h |grep $PART | awk '{ print $5 }' | cut -d'%' -f1`
if [ $USE -gt $MAX ]; then
echo "Percent used: $USE of /" | mail -s "Server is running out of disk space" $EMAIL
fi

This code is working on my system. Perhaps check to make sure that the output of dh is always adding up to an amount greater than 85. You may have more success not using the -h argument to dh and instead retrieving the actual number in bytes and operating on that instead.
#!/bin/sh
# Change this number accordingly as usual
MAX=100000000
EMAIL="my#email.com"
PART=sda1
USE=$( df | grep $PART | awk '{ print $3 }' )
if [ "$USE" -gt "$MAX" ]
then echo "Successful"
#echo "Percent used: $USE of /" | mail -s "Server is running out of disk space" $EMAIL
fi
Or for the percentage perhaps try POSIX shell string cutting:
#!/bin/sh
MAX=85
EMAIL="my#email.com"
PART=sda1
USE=$( df | grep $PART | awk '{ print $5 }' )
if [ "${USE%%%}" -gt "$MAX" ]
then echo "Successful"
#echo "Percent used: $USE of /" | mail -s "Server is running out of disk space" $EMAIL
fi

This server is running in AWS. I looked at the email's headers and saw a queer IP address. I traced the IP in AWS and found a replica of the original server I thought was sending out the mails. It was cloned by a now-defunct vendor. So, I just terminated that server and problem solved! :D
This is a very good community! The people are very gifted technically and are so whilling to share. Keep it up, guys! I appreciate all those who tried to help me. Thanks!

Related

Email Alerts when service or server automatically comes up

I am working on a bash script that helps to ping and get the network interface level status of the host machines and services.
This script will send a email alerts in case of failure.
#!/bin/bash
HOSTS="192.168.8.200"
COUNT=4
for myHost in $HOSTS
do
count=$(ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
if [ $count -eq 0 ]; then
# 100% failed
echo -e "HOST:$myHost is down (ping failed) at $(date)" | mailx -A gmail -s “Mail subject” anymail#anydomain.com
fi
done
This works fine.
But need help to get a one single email alert when host automatically comes up (ping success).
You need to save the state of the host (up/down) during the calls of your script.
if the host is "up" and the former state was "down" then you need to send an email.
You can just write the result of the "check command" to a file in /tmp/
if the check returns that the server is up you read the content of the file. if the state is "down" in the file, then send an email an write "up" to the file.
on the next check if the server is up, there will be no additional email sent, because the server was also "up" before.
#!/bin/bash
HOSTS="192.168.8.200 192.168.8.201 192.168.122.1"
COUNT=4
STATE="/tmp/ping_state.txt"
for myHost in $HOSTS
do
count=$(ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
if [ $count -eq 0 ]; then
# 100% failed
#echo -e "HOST:$myHost is down (ping failed) at $(date)" | mailx -A gmail -s “Mail subject” anymail#anydomain.com
echo "host $myHost down"
#delete all previous entries of that ip
sed -i "/$myHost/d" $STATE
#mark host as down
echo "$myHost - down" >> $STATE
else
CHECK=`grep "$myHost" $STATE | grep -o "down"`
if [ "$CHECK" = "down" ]; then
echo "host $myHost up again"
#insert email for host up here
fi
#delete all previous entries of that ip
sed -i "/$myHost/d" $STATE
echo "$myHost - up" >> $STATE
fi
done
for simple testing I just used an echo statement instead of sending an email.

Check return code in bash while capturing text

When running an ldapsearch we get a return code indicating success or failure. This way we can use an if statement to check success.
On failure when using debug it prints if the cert validation failed. How can I capture the output of the command while checking the sucess or failure of ldapsearch?
ldapIP=`nslookup corpadssl.glb.intel.com | awk '/^Address: / { print $2 }' | cut -d' ' -f2`
server=`nslookup $ldapIP | awk -F"= " '/name/{print $2}'`
ldap='ldapsearch -x -d8 -H "ldaps://$ldapIP" -b "dc=corp,dc=xxxxx,dc=com" -D "name#am.corp.com" -w "366676" (mailNickname=sdent)"'
while true; do
if [[ $ldap ]] <-- capture text output here ??
then
:
else
echo $server $ldapIP `date` >> fail.txt
fi
sleep 5
done
As #codeforester suggested, you can use $? to check the return code of the last command.
ldapIP=`nslookup corpadssl.glb.intel.com | awk '/^Address: / { print $2 }' | cut -d' ' -f2`
server=`nslookup $ldapIP | awk -F"= " '/name/{print $2}'`
while true; do
captured=$(ldapsearch -x -d8 -H "ldaps://$ldapIP" -b "dc=corp,dc=xxxxx,dc=com" -D "name#am.corp.com" -w "366676" "(mailNickname=sdent)")
if [ $? -eq 0 ]
then
echo "${captured}"
else
echo "$server $ldapIP `date`" >> fail.txt
fi
sleep 5
done
EDIT: at #rici suggestion (and because I forgot to do it)... ldap needs to be run before the if.
EDIT2: at #Charles Duffy suggestion (we will get there), we don't need to store the command in a variable.

DD-WRT Bash script at startup issue

Hey all I have the following BASH script running at startup on my WRT1900ac linksys:
USER="admin"
PASS="passhere"
PROTOCOL="http"
ROUTER_IP="192.168.1.1"
# Port to connect to which will provide the JSON data.
PORT=9898
while [ 1 ]
do
# Grab connected device MAC addresses through router status page.
MACS=$(curl -s --user $USER:$PASS $PROTOCOL://$ROUTER_IP/Status_Wireless.live.asp)
# clear temp JSON file
echo > temp.log
# Get hostname and IP (just in case there is no hostname).
for MAC in $(echo $MACS | grep -oE "wl_mac::[a-z0-9]{2}:[a-z0-9]{2}:[a-z0-9]{2}:[a-z0-9]{2}:[a-z0-9]{2}:[a-z0-9]{2}" | cut -c 9-);
do
grep 0x /proc/net/arp | awk '{print $1 " " $4}' | while IFS= read -r line
do
IP=$(echo $line | cut -d' ' -f1)
MACTEMP=$(echo $line | cut -d' ' -f2)
HOST=$(arp -a | grep $IP | cut -d' ' -f1)
# if no hostname exists, just use IP.
if [ "$HOST" == "" ]
then
HOST=$IP
fi
if [ "$MAC" == "$MACTEMP" ]
then
JSON="{'hostname' : '$HOST', 'mac_address' : '$MAC'}"
echo $JSON >> temp.log
fi
done
done
# Provide the JSON formatted output on $PORT of router.
# This allows one connection before closing the port (connect, receive data, close).
# Port will reopen every 5 minutes with new data as setup in a cron job.
echo -e "HTTP/1.1 200 OK\n\n $(cat temp.log)" | nc -l -p$PORT >/dev/null
# Wait for 10 seconds and do it all over.
sleep 10
done
And for some reason when I reboot the router and then try to visit http://192.168.1.1:9898 it just shows a blank page even though I have my android cell phone connected via wifi to the router and the router shows the MAC address on the status page.
What should be on that page is all the wireless MAC address that are currently connected to the router and displaying them out in JSON form.
Any BASH guru's here that can help spot the problem?
I think it should be
echo -e "HTTP/1.1 200 OK\n\n $(cat temp.log)" | nc -l -p$PORT 0.0.0.0 >/dev/null

how to split a line into array in shell

below script is used for drop a mail while ping dropped in network
subject="Ping failed"
Email="test1#server.abc.com"
awk '{print $1}' < b.txt | while read ip;do
CNT=$(ping -c 1 $ip | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
if [ $CNT -eq 0 ]; then
echo "Host : $ip is down (ping failed) at $(date)"| mail -s "$subject" $Email
fi
done
This script is working fine. Input file has the following content..
192.2.165.1 ttcn
192.3.4.23 dct
192.3.4.24 abc
I want to split lines of this file into 0 and 1 index form like array and mail format should be
Host : $ip ttcn is down (ping failed) at $(date)"
can anyone help me to get this?
To read a line into an array use read -a arr and then access the elements using ${arr[0]}, ${arr[1]} etc.
Also, you don't need to parse the output of ping to check if the host responded. Just use the exit status instead.
Here is the revised version:
while read -r -a arr
do
ip="${arr[0]}"
if ! ping -q -c 1 "$ip" > /dev/null
then
mail -s "$subject" "$email" <<< "Host $ip is down (ping failed) at $(date)"
fi
done < b.txt
Give multiple arguments to read, and each column will be read into the corresponding variable:
while read ip name;do
CNT=$(ping -c 1 $ip | awk -F',' '/received/ { split($2, a, " "); print a[1]}')
if [ $CNT -eq 0 ]; then
echo "Host : $ip $name is down (ping failed) at $(date)"| mail -s "$subject" $Email
fi
done < b.txt
you can use awk
echo "192.2.165.1 ttcn" | awk ' { split($0,a,"");ip=a[1]; print $ip}'

How to send email on my shell script condition in jenkins

i had created a job and in build step i had given below mentioned shell script
# Shell script to monitor or watch the disk space
# It will send an email to $ADMIN, if the (free avilable) percentage
# of space is >= 70%
# -------------------------------------------------------------------------
# set admin email so that you can get email
# set alert level 70% is default
ALERT=70
EXCLUDE_LIST="/net|/home|devfs"
if [ "$EXCLUDE_LIST" != "" ] ; then
df -H | grep -vE "^Filesystem|Users|${EXCLUDE_LIST}" | awk '{ print $5 " " $1 }' | while read output;
do
#echo $output
usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1 )
partition=$(echo $output | awk '{ print $2 }' )
if [ $usep -ge $ALERT ]; then
echo "Running out of space \"$partition ($usep%)\" on $(hostname) as on $(date)" > /Users/Shared/Jenkins/disk_space.txt
else
echo "space \"$partition ($usep%)\" on $(hostname) as on $(date)" > /Users/Shared/Jenkins/space.txt
fi
done
fi
After this script executed the email need to be triggered in jenkins if the condition satisfied. otherwise job should run but email should not trigger.
Looks like a standard question on how to send an email from a shell script: Have a look at the following links.
Shell script to send email
http://theos.in/shell-scripting/send-mail-bash-script/
http://www.cyberciti.biz/faq/linux-unix-bash-ksh-csh-sendingfiles-mail-attachments/
One way may be to call another jenkins job within the if loop, using jenkins cli.You can have another job configured which will trigger a mail with certain content. And that job will be triggered within the success part of your if loopMore info on jenkins CLI can be found at here
May be this will be helpful.

Resources