ntpq -p の結果が出るのが遅いので調べた

時刻合わせをしたくて ntp のデーモンを入れたはいいけど ntpq -p の結果が返ってくるまでの時間が結構かかっているマシンがありました。
当初は結果が出ないわけではないので放置していたのですが、 Zabbix で ntp の状況をモニタできるようにしようとしたところ、Zabbix Agent が ntpq を実行したときに値を取得できない状態となってしまいました(おそらくタイムアウトが発生して返り値が null になってる)。

結果を先に書くと ntpq -p ではなく ntpq -pn を実行するか、DNS なり hosts ファイルを編集すれば結果は早く返ってくるタイプの問題でした。
早く結果が返ってくれば Zabbix Agent が値をとれますね。

自分でやった解決手順をおおむねそのまま残しておきます。
環境は debian 7.10 x86、ntpq 4.2.6p5 です。

ntp.conf の設定はこんな感じ

$ grep -v -e "^#" -e "^$" /etc/ntp.conf
driftfile /var/lib/ntp/ntp.drift
statistics loopstats peerstats clockstats
filegen loopstats file loopstats type day enable
filegen peerstats file peerstats type day enable
filegen clockstats file clockstats type day enable
server 192.168.10.11
restrict -4 default kod notrap nomodify nopeer noquery
restrict -6 default kod notrap nomodify nopeer noquery
restrict 127.0.0.1
restrict ::1

ローカルの ntp サーバーを見に行ってます。

とりあえず ntpq の man からオプションを見てみる。

$ man ntpq
<snip>
   -4   Force  DNS  resolution of following host names on the command line to the IPv4 namespace.                                                             
                                                                                          
   -6   Force DNS resolution of following host names on the command line  to  the IPv6 namespace.                                                             
                                                                                          
   -c   The  following  argument  is interpreted as an interactive format command and is added to the list of commands to  be  executed  on  the  specified host(s). Multiple -c options may be given.                                  
                                                                                          
   -i   Force ntpq to operate in interactive mode. Prompts will be written to the standard output and commands read from the standard input.                  
                                                                                          
   -n   Output all host addresses in dotted-quad numeric format rather than  con‐verting to the canonical host names.                                        
                                                                                          
   -p   Print  a  list  of  the peers known to the server as well as a summary of their state. This is equivalent to the peers interactive command.
<snip>

-n オプションが名前の解決に関係するオプション。
ssh でも接続はできるけど接続までに時間かかるときには名前解決をさせないことで接続までの時間を短縮できたのでその辺かな。

そんなわけで実行してみる。

$ time ntpq -p
<snip>
real    0m5.035s
user    0m0.016s
sys     0m0.008s
$ time ntpq -pn
<snip>
real    0m0.027s
user    0m0.016s
sys     0m0.004s

明らかに早くなったので名前解決の関係で遅かったようです。

Zabbix で値をとれるようにするには

  1. Zabbix のテンプレートを編集して ntpq -p ではなく ntpq -pn を実行させる
  2. DNS サーバーをセットして正しく名前解決できるようにする
  3. hosts ファイルを編集してとりあえず名前解決できるようにする

1番目のテンプレート編集は、何ヶ所も書き換えないといけないのでやめておく。
2番目の DNS サーバーに関してはそもそもローカル DNS サーバーは置いてないし、参照しているプロバイダの DNS にローカルネットワークの情報は書き込めるわけがないのでやめておく。
消去法で3番目の hosts ファイル編集に落ち着きました。
マシンが名前解決に時間かかる根本の原因を押さえた方がいいかもしれませんが。

$ sudo vi /etc/hosts
192.168.10.11 X60s
$ time ntpq -p
<snip>
real    0m0.026s
user    0m0.016s
sys     0m0.008s

これでしばらく待っていると Zabbix フロントエンドからグラフを見られるようになったので、ちゃんと Zabbix Agent が値を取れるようになったみたいです。

おしまい。