#!/bin/bash
#
# test_ntp
#
# check ntp status
#
#############################################
#
. `dirname $0`/uls_header
#
export PATH=/usr/sbin:$PATH
#
if /etc/init.d/ntp status &>/dev/null || systemctl status ntpd &>/dev/null
 then
  if which ntpq &>/dev/null
   then
    ( echo "D;$DT;$ULSHOSTNAME;System;ntp;"
      ntpq -pn | awk '
        BEGIN { count = 0
                sum_offset = 0
                min_offset = 100000
                max_offset = 0
              }
         0 < $3 && $3 < 10 { count++
                             o = $9 < 0 ? -$9 : $9
                             sum_offset += o
                             if( o < min_offset )
                               min_offset = o
                             if( o > max_offset )
                               max_offset = o
                           }
         END { if( count > 0 )
               { printf("V;;;;;Min Offset;%.3f;ms\n", min_offset)
                 printf("V;;;;;Avg Offset;%.3f;ms\n", sum_offset/count)
                 printf("V;;;;;Max Offset;%.3f;ms\n", max_offset)
               }
             }'
    ) | send_test_tab
  fi
fi
