LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Shell script help needed to parse ntpd -q output (https://www.linuxquestions.org/questions/programming-9/shell-script-help-needed-to-parse-ntpd-q-output-937204/)

gkasica 03-29-2012 11:13 PM

Shell script help needed to parse ntpd -q output
 
1 Attachment(s)
I am runing an ntp server here with hte shm PPS drivers and occasionaly the driver hangs causing the GPS signal and timing to fail...the driver is still running so I can't test for it with psmon...what I'd like to do is as follows but I don't know nearly enough awk/sed/shell programming to pull it all off.

Code:

My Main problem is:

Shell script to go out and look at Colums 5 & 6 of "ntpq -p" output - specifically the 3rd row - SHM(0) and compare the whn (col 5) and poll (Col 6) and

when (col 5) values and if when is larger than poll then kill and restart ntpd and shm drivers


ntpq -p
    remote          refid      st t when poll reach  delay  offset    disp
==============================================================================
*SHM(0)          .PPS.            0 l    2  16  377    0.00  -1.878    4.12
+saturn          .PPS.            1 u  57  64    7    0.69    1.346    5.26
-apollo          saturn          2 u  55  64    7    0.18    3.937    5.70
xtime-B.timefreq .ACTS.          1 u  59  64    7    80.45  -13.765    5.31
-time.nist.gov  .ACTS.          1 u  53  64    7    78.11  -7.228    5.36
+otc1.psu.edu    .WWV.            1 u  56  64    7    58.39    1.836    5.61


ntpq -p | /usr/bin/awk '{print $5}'

when
 
12
51
49
53
47
50

ntpq -p | /usr/bin/awk '{print $6}'                       

poll
 
16
64
64
64
64
64


IF ROW SHM(0) WHEN > POLL THEN


killall -9 ntpd
sleep 3
ps -efw | grep 'shm_splc2' | grep -v grep |  /usr/bin/awk '{print $2}' | /usr/local/bin/xargs kill -9
sleep 3
ntpd -q -g
sleep 3
/usr/local/bin/ntpd
sleep 3
cd /Linux/shmpps/shmpps
./start.sh
sleep 3
killall -9 ntpd
sleep 3
/usr/local/bin/ntpd

Thanks in advance for any help you can give.

George

catkin 03-30-2012 01:13 AM

Something like
Code:

#!/bin/bash

i=0
while read -r _ _ _ _ when poll _
do
    (( i++ ))
    if (( i == 3 )); then
        if (( when > poll )); then
          echo "doing stuff"
        fi 
        break
    fi 
done <<< "$( ntp -q )"


gkasica 03-30-2012 06:31 AM

That looks like it will do it.

One note I had to change the last tine to done <<< "$( ntpq -p )"

---------- Post added 03-30-12 at 06:31 AM ----------

I'll test it out and report back by end of day hopeflly.

catkin 03-30-2012 10:20 PM

Quote:

Originally Posted by gkasica (Post 4640547)
One note I had to change the last tine to done <<< "$( ntpq -p )

Sorry about that. I tested using the ntpq -p output you provided (very good LQ netiqette :)) rather than the command itself because it allowed setting "when" > "poll".

gkasica 03-31-2012 06:19 AM

No problem. What I have here isworking well.
Thank you very much. I'll be markingit solved with your solution.


All times are GMT -5. The time now is 02:18 AM.