bash script | URL health check | mail sending option - linux

We need to health check a URL every minute of the clock.
Algorithm of bash script is following:
if (url is not up)
send mail
wait for 5 minutes
if (still not up)
send mail again and keep on doing it
else
nothing
My logic is following:
#!/bin/bash
IP="192.168.XX.XXX"
URL="http://$IP:YYYY"
CCMAIL="abc#def.com"
TOMAIL="abc#def.com"
STATUSCODE=$(curl -o /dev/null --silent --head --write-out '%{http_code}\n' $URL)
NOWENTRY=$(date +"%d-%m-%Y %r")
NOWLOG=$(date +"%d-%m-%Y")
if [ $STATUSCODE -ne "200" ]
then
echo "$IP URL: $URL was down at $NOWENTRY" >> "path/log_$NOWLOG.txt"
#send mail in one line no timing logic out here
fi
Note: I have put this script in crontab to execute every minute of the clock.

if [ "$STATUSCODE" -ne "200" ]
then
msg="$IP URL: $URL was down at $NOWENTRY"
echo "$msg" >> "path/log_$NOWLOG.txt"
# send email
echo "$msg" | mailx -c "$CCEMAIL" -s "$URL down" "$TOEMAIL"
sleep 5m
# re-spawn this script.
exec "$0"
fi

Related

.sh script linux math with numbers 00-59 [duplicate]

This question already has answers here:
Value too great for base (error token is "09")
(7 answers)
Value too great for base (error token is "08") [duplicate]
(1 answer)
Closed 5 months ago.
I have a script that looks at the current time, does some tasks and notifies me 1 minute before the timer ends. For some reason there is a problem and the script does not pass the condition "IF"
I read that it seems like bash interprets numbers not as a decimal number. I tried to solve the problem by removing the first zero from the string. Bash will understand this as a decimal number and perform a subtraction operation.
For example. Turn off the computer after 10 minutes. Now is 01:00:00. Shutdown at 01:10:00
The script makes a timecode file with name "T01H10M.shutdown" and takes all the necessary numbers of hour and minute from it.
When the time comes to 01:09:00, the script notifies me about the imminent completion of the work.
#Search shutdown time from file name
SearchFilenameDateWhenPCgotoShutdown="$(find /root/ -name '*.shutdown')"
SearchTimeHour=$(echo $SearchFilenameDateWhenPCgotoShutdown | cut -f2 -d "T"| cut -f1 -d "H")
SearchTimeMinute=$(echo $SearchFilenameDateWhenPCgotoShutdown | cut -f2 -d "H" | cut -f1 -d "M")
#Calculation of the penultimate minute and сorrection of the subtraction error.
SearchTimeMinuteMinus1=$(($SearchTimeMinute - 1 ))
if [ $SearchTimeMinuteMinus1 -eq -2 ]; then
SearchTimeMinuteMinus1=58;
fi
if [ $SearchTimeMinuteMinus1 -eq -1 ]; then
SearchTimeMinuteMinus1=59
fi
#Determining the current hour and minute
RealTimeHour=$(date "+%H")
RealTimeMinute=$(date "+%M")
if [ $RealTimeMinute = 00 ];
then
RealTimeMinute=0;
else
RealTimeMinute=`echo $RealTimeMinute |sed 's/^0*//'`
fi
#The condition matches the current hour
if [ $SearchTimeHour = $RealTimeHour ]; then
if [ $SearchTimeMinuteMinus1 = $RealTimeMinute ]; then
curl -s "https://api.telegram.org/bla bla bla bla &> /dev/null
fi
And here there is an error that when the current time is in minutes from 00-07 minutes, the condition IF = 00 then = 0 else remove "0" work fine and corrects the number to decimal with a condition. Output 1, 2, 3...7 And when the minute is already 08, the script stops at the stage of checking minutes.
if [ $SearchTimeMinuteMinus1 = $RealTimeMinute ]; then
And I don't understand why!
I tried to do it in a separate script.
B=08
if [ $B = 00 ];
B=0;
else
B=`echo $B |sed 's/^0*//'`
fi
echo $B
He works. Output 8. Why I can't do it in the main script, I don't understand.
Screenshot with each 2 min script run
Help please.
--------------------UPD 4.10.22--------------------
OK! Thank you all for your advice. I fixed the code and it works. But! Another problem :D When the time comes at 59 minutes. An error occurs in stage.
if [ $SearchTimeHour = $RealTimeHour ]; then
Problem
This is very strange! After all, all variables are defined correctly. Any ideas?
Full code:
#!/bin/bash
if ping -c 2 100.100.100.100 | grep "ttl"; then
if ping -c 2 192.168.10.1 | grep "ttl"; then
if ping -c 2 192.168.88.102 | grep "ttl"; then
#OMV name
NameOMV="Standart OMV"
#The option determines how many minutes to turn off the system
ShutDownParameterMinutes=2
#Defining the startmark file in the script
startmark=/root/startmark
#Checking the existence of the startmark file
if [ -f $startmark ]
then
#Recording when to switch off to a variable
SearchFilenameDateWhenPCgotoShutdown="$(find /root/ -name '*.shutdown')"
#Determination of the hour and minute of shutdown
SearchTimeHour=$(echo "$SearchFilenameDateWhenPCgotoShutdown" | cut -f2 -d "T"| cut -f1 -d "H")
SearchTimeMinute=$(echo "$SearchFilenameDateWhenPCgotoShutdown" | cut -f2 -d "H" | cut -f1 -d "M")
#Determination of the hour and minute of shutdown minus 1 minute for notification in telegrams
#Correction of the subtraction error of 2 minutes - 2 minutes becomes 58 minutes, and -1 = 59 minutes
SearchTimeMinuteMinus1=$((SearchTimeMinute - 1 ))
if [ "$SearchTimeMinuteMinus1" -eq -2 ]; then
SearchTimeMinuteMinus1=58
fi
if [ "$SearchTimeMinuteMinus1" -eq -1 ]; then
SearchTimeMinuteMinus1=59
fi
curl -s "https://api.telegram.org/blablabla&text=%F0%9F%93%B7 SearchTimeMinuteMinus1=${SearchTimeMinuteMinus1}" &> /dev/null
#Determining the current hour and minute
RealTimeHour=$(date "+%H")
RealTimeMinute=$(date "+%M")
RealTimeMinute=$((10#$RealTimeMinute))
curl -s "https://api.telegram.org/blablabla&text=%F0%9F%93%B7 RealTimeHour=${RealTimeHour}" &> /dev/null
curl -s "https://api.telegram.org/blablabla&text=%F0%9F%93%B7 RealTimeMinute=${RealTimeMinute}" &> /dev/null
curl -s "https://api.telegram.org/blablabla&text=%F0%9F%93%B7 TICK $(date "+%d.%m.%Y - %H:%M:%S")" &> /dev/null
#The condition matches the current hour
if [ "$SearchTimeHour" -eq "$RealTimeHour" ]; then
curl -s "https://api.telegram.org/blablabla&text=%F0%9F%93%B7 Hour OK" &> /dev/null
if [ "$SearchTimeMinuteMinus1" -eq "$RealTimeMinute" ]; then
curl -s "https://api.telegram.org/blablabla&text=%F0%9F%93%B7 Minute OK" &> /dev/null
rm "$SearchFilenameDateWhenPCgotoShutdown"
rm startmark
curl -s "https://api.telegram.org/blablabla&text=%F0%9F%93%B7 $NameOMV is less 1 minutes left before shutdown" &> /dev/null
fi
fi
else
#Creating a startmark
touch startmark
#Notification in telegram about the start of the shutdown procedure
echo "OrangePi is creating startmark to offline: $(date "+%d.%m.%Y - %H:%M:%S")" >> shutdownlog.txt
#We determine the time when to turn off
DateWhenPCgotoShutdown=$(date +"T""%H""H""%M""M" --date="$ShutDownParameterMinutes minute")
TimeStampStartName="${DateWhenPCgotoShutdown}.shutdown"
touch "$TimeStampStartName"
chmod +x "$TimeStampStartName"
#Notification in telegram when to turn off the computer
TelegramMessage="OrangePi $NameOMV is creating startmark to offline: $(date "+%d.%m.%Y - %H:%M:%S" --date="$ShutDownParameterMinutes minute")"
echo "${TelegramMessage}" >> shutdownlog.txt
curl -s "https://api.telegram.org/blablabla&text=%F0%9F%93%B7 $TelegramMessage" &> /dev/null
#/sbin/shutdown -h +$ShutDownParameterMinutes
echo "SHUTDOWN"
fi
fi
fi
fi
With respect.

checking response control just "connected" or not with wget

I have just trying to write a script which just controls about the response contains "connected" or not
#!/bin/bash
cat control.txt | while read link // control.txt contains http and https urls
do
if [[ $(wget --spider -S $link 2>&1 | grep "connected") =~~ *"connected"* ]];
then echo "OK";
else echo "FAIL";
fi
done
Output:
sh -x portcontrol.sh
portcontrol.sh[2]: Syntax error at line 4 : `=~' is not expected.
If I read your script correctly, you're retrieving the page, but ignoring its contents, and all you want is to see whether wget shows the string 'connected'.
If that is so, your code can be simplified as follows:
if wget --spider -S $link 2>&1 | grep "connected" > /dev/null
then
echo "OK";
else
echo "FAIL";
fi
You don't need to capture wget's output and run a regexp search on it; grep already returns 0 (success) or 1 (not found) when searching for the string you gave.
That return code can be used directly to control the if.
The output of grep is redirected to /dev/null as to not show up on the screen or script output.
If you simply want to see if the connection request succeeded, and the wget output is of the form:
Connecting to <hostname>|<ip_addr>|:<port>... connected.
it should be sufficient to just do:
if [[ $(wget --spider -S $link 2>&1 | grep -c " connected\.") -gt 0 ]];
then echo "OK";
else echo "FAIL";
fi
Checking exit code works too, but it depends on what your requirements really are.

How to send email for failed jobs in shell scripts

I have a shell script. To this script I am passing arguments from a file. This file contains tables names
The script is working fine. I am able execute the command for all the tables in the file.
shell script
#!/bin/bash
[ $# -ne 1 ] && { echo "Usage : $0 input file "; exit 1; }
input_file=$1
TIMESTAMP=`date "+%Y-%m-%d"`
touch /home/$USER/logs/${TIMESTAMP}.success_log
touch /home/$USER/logs/${TIMESTAMP}.fail_log
success_logs=/home/$USER/logs/${TIMESTAMP}.success_log
failed_logs=/home/$USER/logs/${TIMESTAMP}.fail_log
#Function to get the status of the job creation
function log_status
{
status=$1
message=$2
if [ "$status" -ne 0 ]; then
echo "`date +\"%Y-%m-%d %H:%M:%S\"` [ERROR] $message [Status] $status : failed" | tee -a "${failed_logs}"
#echo "Please find the attached log file for more details"
#exit 1
else
echo "`date +\"%Y-%m-%d %H:%M:%S\"` [INFO] $message [Status] $status : success" | tee -a "${success_logs}"
fi
}
while read table ;do
sqoop job --exec $table > /home/$USER/logging/"${table}_log" 2>&1
g_STATUS=$?
log_status $g_STATUS "Sqoop job ${table}"
cp /home/$USER/logging/"${table}_log" /home/$USER/debug/`date "+%Y-%m-%d"`/logs/
done < ${input_file}
Now I want to send emails to my email address for failed jobs.
Requirements
1) Send email for each failed job i.e If `status log` has failed job for one particular table then I want email sent out saying job for that table has failed.
2) Or Send out one email for all the jobs that have failed for one input file.
Which is the best method to approach. I would like the 2nd option atleast it will reduce the no of emails to go through.
But better if I know both methods to do
edited function log_status
#Function to get the status of the job creation
function log_status
{
status=$1
message=$2
if [ "$status" -ne 0 ]; then
echo "`date +\"%Y-%m-%d %H:%M:%S\"` [ERROR] $message [Status] $status : failed" | tee -a "${failed_logs}"
mail -a mail.txt -s "This is the failed job log" user#example.com < /home/$USER/logs/${TIMESTAMP}.fail_log
#exit 1
else
echo "`date +\"%Y-%m-%d %H:%M:%S\"` [INFO] $message [Status] $status : success" | tee -a "${success_logs}"
fi
}
If I do this will I get one email for all the failed jobs.
Also it's possible using sendmail command:
sendmail user#example.com < email.txt
Using mail command:
mail -a mail.txt -s "This is the failed job log" user#example.com
-a is a attachment, -s is a subject
And with many attachments:
$(uuencode file1.txt file2.txt) | mailx -s "Subject" user#example.com
Here is a simple snippet for sending an err.log:
mail -s "stder logs for ps" "name#example.com" < err.log
This appends a subject in the first quotes and mails it to the recipient in the second set. In this case I suppose you would have a code block export the error logs via 3> to a file which I called err.log for readability. I would place the above snippet either inside of your script if you are not worried about being too vocal about the errors or neatly outside of the script if you want to be more discrete about how often you are emailing the recipient.

Script that checks a URL for specific text

Looking for a bash script that will accomplish the followings:
Check a URL (ex. www.google.com)
Looks for a specific text string
if it exists, it does nothing
if it doesnt, then sends out an email to alert someone
I tried the following script, it doesnt do anything, I dont get any email or anything.
#!/bin/sh
URL="URL"
TMPFILE=`mktemp /string_watch.XXXXXX`
curl -s -o ${TMPFILE} ${URL} 2>/dev/null
if [ "$?" -ne "0" ];
then
echo "Unable to connect to ${URL}"
exit 2
fi
RES=`grep -i "StringToLookFor" ${TMPFILE}`
if [ "$?" -ne "0" ];
then
echo "String not found in ${URL}" | mail -s "Alert" your#email
exit 1
fi
echo "String found"
exit 0;
The command
mail -s "Alert" your#email
is pausing to let you enter the text of your email. If you want to send an email with the indicated subject and no text you need to do
mail -s "Alert" your#email < /dev/null

Linux script with curl to check webservice is up

I have a webservice provided at http://localhost/test/testweb
I want to write a script to check if webservice is up with curl
If there a curl parameter given, returns 200 OK ok true false so that I can use it is if-else block in linux script
curl -sL -w "%{http_code}\\n" "http://www.google.com/" -o /dev/null
-s = Silent cURL's output
-L = Follow redirects
-w = Custom output format
-o = Redirects the HTML output to /dev/null
Example:
[~]$ curl -sL -w "%{http_code}\\n" "http://www.google.com/" -o /dev/null
200
I would probably remove the \\n if I were to capture the output.
I use:
curl -f -s -I "http://example.com" &>/dev/null && echo OK || echo FAIL
-f --fail Fail silently (no output at all) on HTTP errors
-s --silent Silent mode
-I --head Show document info only
Note:
depending on needs you can also remove the "-I" because in some cases you need to do a GET and not a HEAD
Same as #burhan-khalid, but added --connect-timeout 3 and --max-time 5.
test_command='curl -sL \
-w "%{http_code}\\n" \
"http://www.google.com:8080/" \
-o /dev/null \
--connect-timeout 3 \
--max-time 5'
if [ $(test_command) == "200" ] ;
then
echo "OK" ;
else
echo "KO" ;
fi
That will check the headers via wget 2>&1pipes the stderr to stdout
grep filters
-O /dev/null just throws the content of the page
if [ "\`wget http://example.org/ -O /dev/null -S --quiet 2>&1 | grep '200 OK'\`" != "" ];
then
echo Hello;
fi;
I know not curl, but still a solution
I needed a better answer to this, so I wrote the script below.
The fakePhrase is used to detect ISP "Search Assist" adware HTTP resposnes.
#!/bin/bash
fakePhrase="verizon"
siteList=(
'http://google.com'
'https://google.com'
'http://wikipedia.org'
'https://wikipedia.org'
'http://cantgettherefromhere'
'http://searchassist.verizon.com'
)
exitStatus=0
function isUp {
http=`curl -sL -w "%{http_code}" "$1" -o temp_isUp`
fakeResponse=`cat temp_isUp | grep $fakePhrase`
if [ -n "$fakeResponse" ]; then
http=$fakePhrase
fi
case $http in
[2]*)
;;
[3]*)
echo 'Redirect'
;;
[4]*)
exitStatus=4
echo "$1 is DENIED with ${http}"
;;
[5]*)
exitStatus=5
echo "$1 is ERROR with ${http}"
;;
*)
exitStatus=6
echo "$1 is NO RESPONSE with ${http}"
;;
esac
}
for var in "${siteList[#]}"
do
isUp $var
done
if [ "$exitStatus" -eq "0" ]; then
echo 'All up'
fi
rm temp_isUp
exit $exitStatus
Use this:
curl -o $CURL_OUTPUT -s -w %{http_code}\\n%{time_total}\\n $URL > $TMP_FILE 2>&1
cat $TMP_FILE

Resources