#!/usr/bin/perl -w
###############################################################################
# Webmin Sysstats - ntp.pl
#
# Webmin Sysstats Module
# Copyright (C) 2003 by Eric Gerbier
# Bug reports to: gerbier@users.sourceforge.net
# $Id: ntp.pl 2975 2009-01-01 21:21:27Z gerbier $
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
###############################################################################
use strict;
use warnings;
no warnings 'redefine';
use RRDs;
use English qw(-no_match_vars);
use FindBin;
# to be used alone or from sysstat.pl without warning
# because require verify the path
if ( !exists $ENV{WEBMINSTAT_TEMP} ) {
use lib "$FindBin::Bin/../..";
## no critic (RequireBarewordIncludes)
require 'sysstats-aquisition-lib.pl';
## use critic
}
my $module_name;
my $SPACE = SPACE();
###############################################################################
# cf doc on http://tech.kulish.com/2007/10/30/ntp-ntpq-output-explained/
#remote refid st t when poll reach delay offset disp
#==========================================================
#+128.252.19.1 .GPS. 1 u 495 1024 377 30.90 -6.366 8.26
#*139.78.133.139 .USNO. 1 u 936 1024 377 48.43 -2.906 5.20
sub get_ntp_status($$) {
my $ntpq_bin = shift @_;
my $host = shift @_; # /dev/sda
my $ntpq_cmd = "$ntpq_bin -p $host";
my $pipe = read_pipe_stream($ntpq_cmd);
while (<$pipe>) {
chomp;
# select current time source
if (m/^[*](.*)/) {
my @tab = split_awk($1);
# populate hash table
my %hash = (
# remote => $tab[0], # not interesting
# refid => $tab[1], # not numerical
stratum => $tab[2],
# t => $tab[3], # not numerical
when => $tab[4],
poll => $tab[5],
reach => $tab[6],
delay => $tab[7],
offset => $tab[8],
disp => $tab[9],
);
return %hash;
}
}
debug("get_ntp_status : no value for device $host");
return;
}
###############################################################################
my %config;
read_file_cached( 'config', \%config );
$module_name = $config{'name'};
debug( "run $module_name version " . $config{'version'} );
my %codes = action_load( \%config );
my $pre = $config{'pre'};
my $vol_num = 1;
my $ntpq_cmd = $config{'ntpq'};
if ( !$ntpq_cmd ) {
warning('ntpq command not configured');
goto FIN;
}
elsif ( !-x $ntpq_cmd ) {
warning("ntpq command $ntpq_cmd not found");
goto FIN;
}
my %result; # data cache
while ( exists $config{ $pre . $vol_num } ) {
my @tab = split /,/, $config{ $pre . $vol_num };
my $real_vol_num = $tab[0];
my $vol = $tab[1];
my $runstop = $tab[3];
# todo : look at smart module
my $value = 'U';
if ( is_param_runstop($runstop) ) {
my ( $host, $parm ) = decode_param($vol);
my %data;
if ( exists $result{$host} ) {
# get from cache
%data = %{ $result{$host} };
}
else {
%data = get_ntp_status( $ntpq_cmd, $host );
%{ $result{$host} } = %data;
}
if ( exists $data{$parm} ) {
my $val = $data{$parm};
if ( is_signed_real($val) ) {
$value = $val;
}
else {
debug("strange value $val for $parm");
}
}
else {
warning("can not find $parm on $host");
}
debug("$module_name $host $parm ($real_vol_num) : value=$value");
action_param( $parm, $value, \%codes );
}
else {
debug("aquisition for $vol stopped");
}
my $rrdbase = $real_vol_num . '.rrd';
RRDs::update( $rrdbase, "N:$value" );
my $ERR = RRDs::error();
warning("ERROR while updating $rrdbase database : $ERR") if $ERR;
$vol_num++;
}
# a label to quit
#I can not use return when executing this script standalone,
# so I had to use goto
FIN: