Skip to content

Instantly share code, notes, and snippets.

@mdklapwijk
Created August 17, 2018 11:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdklapwijk/ca06195b3e70d3cdf1aa61382eee1c66 to your computer and use it in GitHub Desktop.
Save mdklapwijk/ca06195b3e70d3cdf1aa61382eee1c66 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
# ntpd for observium-agent v0.1
#
# (c)2018 Marcel Klapwijk
# Please leave suggestions and bug reports at:
# - https://gist.github.com/mdklapwijk/
#
# 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.
#
# you should have received a copy of the GNU General Public License
# along with this program (or with Nagios); if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA
#
# Purpose:
# Provide ntpd statistics to Observium via the UNIX Agent.
#
#
# Note:
# NO WARRANTY and NO ERROR_HANDLING, so use at you own risk!
#
#
# Credits:
# Inspired by the php version done by Dennis de Houx
#
#
# Version history:
# - 0.1) Initial version.
use strict;
use warnings;
my(%values);
sub main()
{
my(@output_keys)=qw(version stratum offset frequency jitter noise stability server uptime buffer_recv buffer_free buffer_used packets_drop packets_ignore packets_recv packets_sent);
open(NTPQ,"ntpq -c rv|");
while(<NTPQ>)
{
my(@elements)=split(/,/,$_);
foreach(@elements)
{
s/^ //;
s/\n//;
my($key,$value)=split(/=/,$_);
if($key&&$value)
{
$values{$key}=$value;
}
}
}
close(NTPQ);
open(NTPDC,"ntpdc -c iostats|");
while(<NTPDC>)
{
s/:\s+/:/;
s/\n//;
s/^time since reset/uptime/;
s/^receive buffers/buffer_recv/;
s/^free receive buffers/buffer_free/;
s/^used receive buffers/buffer_used/;
s/^dropped packets/packets_drop/;
s/^ignored packets/packets_ignore/;
s/^received packets/packets_recv/;
s/^packets sent/packets_sent/;
my($key,$value)=split(/:/,$_);
$values{$key}=$value;
}
close(NTPDC);
printf("<<<app-ntpd>>>\n");
foreach(@output_keys)
{
&print_keypair("$_");
}
}
sub print_keypair()
{
my($key)=$_[0];
my($value);
for($key)
{
if(/version/) {$value='"N/A"';}
if(/server/) {$value=0;}
else {$value='"U"';}
}
for($key)
{
if (/jitter/ && $values{'clk_jitter'}) {$value=$values{'clk_jitter'};}
elsif (/noise/ && $values{'sys_jitter'}) {$value=$values{'sys_jitter'};}
elsif (/stability/ && $values{'clk_wander'}) {$value=$values{'clk_wander'};}
elsif (/server/ && $values{'uptime'} && $values{'stratum'}<16) {$value=1;}
elsif (defined($values{$key})) {$value=$values{$key};}
}
printf("%s:%s\n",$key,$value);
}
&main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment