"Batch issue with "missing operator""

I wrote a batch script for MaxRM that sets off an alarm if the server's clock is not synchronized. The script works perfectly - apart from some annoying output line saying "missing operator".

Here's the code:

@echo off
REM Lennart Schuetz 2017/03
REM Checks the time difference between this machine and pool.ntp.org
REM A difference greater than 60 seconds will cause the check to fail
setlocal
setlocal enabledelayedexpansion
setlocal enableextensions
set timeSkew=0

for /f "tokens=* skip=3 usebackq" %%i in (`w32tm /stripchart /computer:pool.ntp.org /samples:1 /dataonly`) do (
  set line=%%i
  set /a timeSkew=!line:~11,4!*1)
echo Time skew: %timeSkew% seconds
if %timeSkew% LEQ 60 (exit 0) else (exit 1001)

So the script output will not be "Time skew: 10 seconds", but "Missing operator Time skew: 10 seconds".
As far as I see, this comes from the line starting with "set /a", but I have no idea how to get rid of this. I even tried to add "> NUL" at the end of the line, but with no effect. Any ideas?

created 3 months ago
Lennart
 13 1 4
You must be signed in before you can post a fix

Comments:

Thanks, Ben. It's clearly not a firewall issue. On every machine that reports the error, I can run the script again and it will run correctly. I'm having trouble investigating. If I remote into a machine that has failed and create a copy of the script with the Echo On modification, I can run it dozens of times successfully. I have found that in a extremely small number of cases, the script will crash the command window (it runs, then closes the window before I can see the results). This doesn't leave any evidence, sadly. I think the whole problem might be with the dashboard or Max's way of handling scripts. It's hard to say, but I think the coding fo the script is solid. HCHTech 10 days ago
Change the echo off line to echo on, then run the script and past in the results. It may present more info to troubleshoot. I don't personally use this script, but in all of my testing, I have not been able to replicate the issue with receiving "Time skew: rror: seconds". Make sure there are no firewall policies, or network filtering, that could be interfering. BenSantiago 13 days ago
cancel
Scripting Language: Batch

Batch scripting does not support the decimal in a set expression, whole numbers only. I updated your script to pull out the first part of the info from ntp and only show the difference in time, then set the entire amount of time as the timeskew variable. I have verified this errors out appropriately and will trigger an alert in MAX.


@echo off

REM Lennart Schuetz 2017/03
REM Checks the time difference between this machine and pool.ntp.org
REM A difference greater than 60 seconds will cause the check to fail
setlocal
setlocal enabledelayedexpansion
setlocal enableextensions
set timeSkew=0

for /f "tokens=2* skip=3 delims=, usebackq" %%i in (`w32tm /stripchart /computer:pool.ntp.org /samples:1 /dataonly`) do (
  set line=%%i
  set timeSkew=!line:~2,6!
)
echo Time skew: %timeSkew% seconds
if %timeSkew% LEQ 60 (exit /b 0) else (exit 1001)
created 2 months ago
BenSantiago
 26 1 3
modified 1 month ago

Comments:

Change the echo off line to echo on, then run the script and past in the results. It may present more info to troubleshoot. I don't personally use this script, but in all of my testing, I have not been able to replicate the issue with receiving "Time skew: rror: seconds". Make sure there are no firewall policies, or network filtering, that could be interfering. BenSantiago 13 days ago
Ben, I'm trying to deploy this script to the servers I have under management. It works on all of them sometimes. Most days, though at least a few servers will return "Time skew: rror: seconds" to the dashboard. I assume it is not getting a reply from pool.ntp.org. Is there any way to increase the chance of a successful run? Giving the script more time to run doesn't seem to help. HCHTech 13 days ago
I made a slight change to the variable timeSkew. I think the "s" at the end may have been causing your issue. Test it out and let me know how you make out. BenSantiago 1 month ago
Dear Ben, thanks for this update! It seems to work, but triggering an alert is not reliable. I recently had two servers with more than 100 seconds offset and they showed up as green. Could it be that the return code is not generated correctly when the time difference is more than 99 seconds? Lennart 1 month ago
cancel

► Have questions?

► Something not working?

► Suggestions?

Post your questions on the meta site, where we discuss everything related to FixIT Scripts

visit meta now»