LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-05-2018, 08:13 AM   #61
1s440
Member
 
Registered: Mar 2018
Posts: 266

Original Poster
Rep: Reputation: Disabled

Quote:
Originally Posted by michaelk View Post
The date command -u option (date -u "+%H") displays the hours as UTC which is the same as GMT which should match the header time. In your code your forgot the "$" in your if statement variables and you should use [[ instead of ((.

Code:
if [[ $Date == $VAR ]]; then
   echo "Hours match"
else 
   echo "Hours not match"
fi
Code:
#!/bin/bash

wtime=$(curl -i http://google.com/ 2>/dev/null | grep Date | sed 's/Date: //')
t3=$(cut -d' ' -f5 <<< "$wtime")
hr=$(cut -d':' -f1 <<< "$t3")
ltime=$(date -u "+%H:%M:%S")
lhr=$(date -u "+%H")
echo "Web Server time=$t3, Server time=$ltime"
if [[ $lhr == $hr ]]; then
   echo "Hours match"
else
   echo "Hours not match"
fi
I've added additional output to display the web server HTML header time and your server time as a visual display.
hmm. this works. Thanks a lot but with GMT timings.
 
Old 12-05-2018, 08:24 AM   #62
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,727

Rep: Reputation: 5919Reputation: 5919Reputation: 5919Reputation: 5919Reputation: 5919Reputation: 5919Reputation: 5919Reputation: 5919Reputation: 5919Reputation: 5919Reputation: 5919
Code:
VAR=$(curl -i http://server_url/ 2>/dev/null | awk 'BEGIN{FS="[ :]"} /Date: /{print $7}')
Oops, in addition to what I posted above since the VAR is basically a string that contains a line feed character using (( )) in your if statement fails.

Quote:
Thanks a lot but with GMT timings.
Not quite. The linux system clock is referenced to UTC (GMT) and it is the timezone setting that converts it to local time that is displayed on the desktop and via the date command.

Last edited by michaelk; 12-05-2018 at 08:28 AM.
 
Old 12-05-2018, 08:40 AM   #63
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,897

Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
I would rather convert the time into long int format (date %s) and check based on that.
 
Old 12-10-2018, 08:57 AM   #64
1s440
Member
 
Registered: Mar 2018
Posts: 266

Original Poster
Rep: Reputation: Disabled
is it also possible to display with other timezones

Last edited by 1s440; 12-10-2018 at 08:59 AM.
 
Old 12-10-2018, 09:33 AM   #65
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
Quote:
Originally Posted by 1s440 View Post
is it also possible to display with other timezones
You can play with "TZ" environment variable
 
Old 12-20-2018, 03:04 AM   #66
1s440
Member
 
Registered: Mar 2018
Posts: 266

Original Poster
Rep: Reputation: Disabled
on the nagios monitoring it shows me the wrong output
Quote:
user@nagios:/usr/local/nagios/plugins# ./check_date.sh -H www.myserver.com
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 980 100 980 0 0 20736 0 --:--:-- --:--:-- --:--:-- 20851
Today date is we, 19 Dec 2018
Output is
Date is wrong

Last edited by 1s440; 12-20-2018 at 04:13 AM.
 
Old 12-20-2018, 03:56 AM   #67
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
^ Please provide us with check_date.sh script content
 
Old 12-20-2018, 04:03 AM   #68
1s440
Member
 
Registered: Mar 2018
Posts: 266

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by l0f4r0 View Post
^ Please provide us with check_date.sh script content
But on the server it displays correct when i use it on the nagios and check it shows me wrong. confused
Code:
#!/bin/bash 
myDate=$(date "+%a, %d %b %Y")
VAR=$(curl -i http://myserver.com/sample.htm | sed -n '/Date/s/Date: //p')
echo "Today date is $myDate"
echo "Output is $VAR"
if [[ ${VAR%% [[:digit:]][[:digit:]]:*} == $myDate ]]; then
echo "Date is correct"
else
echo "Date is wrong"
fi

Last edited by 1s440; 12-20-2018 at 04:13 AM.
 
Old 12-20-2018, 04:28 AM   #69
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
In check_date.sh, did you replace the following bogus instruction?
Code:
curl -i http://myserver.com/sample.htm
Plus, why are you using switch -H when you are calling your script? And why are you specifying a parameter as your script doesn't deal with any (the URL is hard-coded inside the script itself)?
 
Old 12-20-2018, 04:51 AM   #70
1s440
Member
 
Registered: Mar 2018
Posts: 266

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by l0f4r0 View Post
In check_date.sh, did you replace the following bogus instruction?
Code:
curl -i http://myserver.com/sample.htm
Plus, why are you using switch -H when you are calling your script? And why are you specifying a parameter as your script doesn't deal with any (the URL is hard-coded inside the script itself)?
Hi I wanted to run a script on the host from nagios server thats why i am specifiying hostname where the script should be tested. I placed my script under /usr/local/nagios/plugins/ and executing on hostname www.myserver.com with the help of -H

Last edited by 1s440; 12-20-2018 at 04:56 AM.
 
Old 12-20-2018, 04:53 AM   #71
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,727

Rep: Reputation: 5919Reputation: 5919Reputation: 5919Reputation: 5919Reputation: 5919Reputation: 5919Reputation: 5919Reputation: 5919Reputation: 5919Reputation: 5919Reputation: 5919
Quote:
Today date is we, 19 Dec 2018
Output is
$VAR is empty. I assume you are using the same website as previously? Is the format different i.e sed is failing to find the string Date:? Running from the nagios server isn't working the same.

Last edited by michaelk; 12-20-2018 at 04:55 AM.
 
Old 12-20-2018, 05:26 AM   #72
1s440
Member
 
Registered: Mar 2018
Posts: 266

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
$VAR is empty. I assume you are using the same website as previously? Is the format different i.e sed is failing to find the string Date:? Running from the nagios server isn't working the same.
yes the output is empty, I am using the same website.
sorry seems my fault the script is placed on nagios plugins not on the host plugins. i think i should add service config

Last edited by 1s440; 12-20-2018 at 05:30 AM.
 
Old 12-20-2018, 05:36 AM   #73
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,897

Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
the script you posted does not accept -H <server> arguments, therefore hard to say anything.
From the other hand you need to save stderr and check if there was anything in it. This would be the best place to find the reason.
 
Old 12-21-2018, 02:35 AM   #74
1s440
Member
 
Registered: Mar 2018
Posts: 266

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
the script you posted does not accept -H <server> arguments, therefore hard to say anything.
From the other hand you need to save stderr and check if there was anything in it. This would be the best place to find the reason.
Hi modified the script curl command and now i am able to match.

Quote:
curl -v --silent "http://myserver.com/sample.htm/" 2>/dev/null \ | grep Date | sed -e 's/< Date: //'); date "+%a, %d %b %Y"

Last edited by 1s440; 12-21-2018 at 05:29 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script - incrementing a filename in a script tslinux Programming 10 08-05-2003 11:58 PM
can't run a script script from icon in konqueror scottsteibel Linux - Software 1 08-02-2003 07:59 PM
bash script prob: how can i tell the script that a 'dd' has finished? Frustin Linux - General 2 04-02-2003 05:34 AM
E-Mail notification to users via SMS (gateway script ok, but notification script?!?) Riku2015 Linux - Networking 10 03-08-2002 10:16 AM
Including methods from a perl script into another perl script gene_gEnie Programming 3 01-31-2002 05:03 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration