root / plugins / time / ntp_peers_ipv6 @ 81db94e2
Historique | Voir | Annoter | Télécharger (4,52 ko)
| 1 | 9b1dc5e9 | Tony Hoyle, based on script by Chris Hastie | #!/usr/bin/perl -w |
|---|---|---|---|
| 2 | |||
| 3 | # Plugin to monitor offsets to multiple NTP peers. |
||
| 4 | # NB currently only works for IPv4 peers |
||
| 5 | # |
||
| 6 | # (c)2008 Chris Hastie: chris (at) oak (hyphen) wood (dot) co (dot) uk |
||
| 7 | # |
||
| 8 | # Parameters understood: |
||
| 9 | # |
||
| 10 | # config (required) |
||
| 11 | # autoconf (optional - used by munin-node-configure) |
||
| 12 | # |
||
| 13 | # Config variables: |
||
| 14 | # |
||
| 15 | # ntpq - path to ntpq program |
||
| 16 | # |
||
| 17 | # This program is free software: you can redistribute it and/or modify |
||
| 18 | # it under the terms of the GNU General Public License as published by |
||
| 19 | # the Free Software Foundation, either version 3 of the License, or |
||
| 20 | # (at your option) any later version. |
||
| 21 | # |
||
| 22 | # This program is distributed in the hope that it will be useful, |
||
| 23 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 24 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 25 | # GNU General Public License for more details. |
||
| 26 | # |
||
| 27 | # You should have received a copy of the GNU General Public License |
||
| 28 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
| 29 | # |
||
| 30 | # Change log |
||
| 31 | # v1.0.0 2008-07-21 Chris Hastie |
||
| 32 | # initial release |
||
| 33 | # v1.0.1 2009-06-05 Tony Hoyle |
||
| 34 | # ipv6 support. Remove dns lookups. |
||
| 35 | # |
||
| 36 | # |
||
| 37 | # Magic markers - optional - used by installation scripts and |
||
| 38 | # munin-node-configure: |
||
| 39 | # |
||
| 40 | #%# family=contrib |
||
| 41 | #%# capabilities=autoconf |
||
| 42 | # |
||
| 43 | |||
| 44 | use strict; |
||
| 45 | |||
| 46 | my $NTPQ = $ENV{ntpq} || "ntpq";
|
||
| 47 | my $COMMAND = "$NTPQ -nc associations"; |
||
| 48 | |||
| 49 | my $statedir = $ENV{statedir} || '/usr/local/var/munin/plugin-state';
|
||
| 50 | my $statefile = "$statedir/ntp_peers.state"; |
||
| 51 | |||
| 52 | # autoconf |
||
| 53 | if ($ARGV[0] and $ARGV[0] eq "autoconf") {
|
||
| 54 | `$NTPQ -c help >/dev/null 2>/dev/null`; |
||
| 55 | if ($? eq "0") {
|
||
| 56 | if (`$NTPQ -np | wc -l` > 0) {
|
||
| 57 | print "yes\n"; |
||
| 58 | exit 0; |
||
| 59 | } else {
|
||
| 60 | print "no (unable to list peers)\n"; |
||
| 61 | exit 1; |
||
| 62 | } |
||
| 63 | } else {
|
||
| 64 | print "no (ntpq not found)\n"; |
||
| 65 | exit 1; |
||
| 66 | } |
||
| 67 | } |
||
| 68 | |||
| 69 | my %peers; |
||
| 70 | |||
| 71 | # get data from ntpq |
||
| 72 | open(SERVICE, "$COMMAND |") |
||
| 73 | or die("Could not execute '$COMMAND': $!");
|
||
| 74 | |||
| 75 | while (<SERVICE>) {
|
||
| 76 | if(/^\s*\d+\s+(\d+)\s+/) {
|
||
| 77 | my ($name, $offset) = &lookupip($1); |
||
| 78 | $peers{$name} = $offset;
|
||
| 79 | } |
||
| 80 | } |
||
| 81 | close(SERVICE); |
||
| 82 | |||
| 83 | # config |
||
| 84 | if ($ARGV[0] and $ARGV[0] eq 'config') {
|
||
| 85 | print "graph_title NTP peer offsets\n"; |
||
| 86 | print "graph_args --base 1000\n"; |
||
| 87 | print "graph_vlabel ms\n"; |
||
| 88 | 81db94e2 | Gabriele Pohl | print "graph_category time\n"; |
| 89 | 9b1dc5e9 | Tony Hoyle, based on script by Chris Hastie | print "graph_info Offset (in ms) to the server's NTP peers\n"; |
| 90 | print "graph_order "; |
||
| 91 | foreach my $key (sort keys %peers) {
|
||
| 92 | print &sanitize_field($key) . " "; |
||
| 93 | } |
||
| 94 | print "\n"; |
||
| 95 | foreach my $peer (keys %peers) {
|
||
| 96 | print &sanitize_field($peer) . ".label " . $peer . "\n"; |
||
| 97 | } |
||
| 98 | exit 0; |
||
| 99 | } |
||
| 100 | |||
| 101 | # send output |
||
| 102 | foreach my $peer (keys %peers) {
|
||
| 103 | print &sanitize_field($peer) . ".value " . &getpeeroffset($peer) . "\n"; |
||
| 104 | } |
||
| 105 | |||
| 106 | # create a valid munin field name from the hostname |
||
| 107 | sub sanitize_field () {
|
||
| 108 | my $field = shift; |
||
| 109 | |||
| 110 | # replace illegal characters with an underscore |
||
| 111 | $field =~ s/[^A-Za-z0-9_]/_/g; |
||
| 112 | # prepend an underscore if name starts with a number |
||
| 113 | $field =~ s/^([^A-Za-z_])/_$1/; |
||
| 114 | |||
| 115 | # truncate to 19 characters |
||
| 116 | if (length($field) > 19) {
|
||
| 117 | $field = substr($field, 0, 19); |
||
| 118 | } |
||
| 119 | return $field |
||
| 120 | } |
||
| 121 | |||
| 122 | # get an IP address from the underscore escaped |
||
| 123 | # value of env.hostname_<key> |
||
| 124 | sub desanitize_field () {
|
||
| 125 | my $field = shift; |
||
| 126 | $field =~ s/_/\./g; |
||
| 127 | return $field |
||
| 128 | } |
||
| 129 | |||
| 130 | # Get name, offset info for peers |
||
| 131 | # It would be more efficient to use mrv here to avoid rerunning ntpq |
||
| 132 | sub lookupip() {
|
||
| 133 | my $assocID = shift; |
||
| 134 | my $CMD = "$NTPQ -c \"rv $assocID srcadr,offset\""; |
||
| 135 | my $addr=""; |
||
| 136 | my $offset=""; |
||
| 137 | |||
| 138 | # get data from ntpq |
||
| 139 | open(SERVICE2, "$CMD |") |
||
| 140 | or die("Could not execute '$CMD': $!");
|
||
| 141 | |||
| 142 | while(<SERVICE2>) {
|
||
| 143 | if(/^srcadr=([^\s]+),\soffset=(.+)$/) {
|
||
| 144 | $addr = $1; |
||
| 145 | $offset = $2; |
||
| 146 | } |
||
| 147 | } |
||
| 148 | close(SERVICE2); |
||
| 149 | return ($addr, $offset); |
||
| 150 | } |
||
| 151 | |||
| 152 | # returns the offset, or U if it is undefined |
||
| 153 | sub getpeeroffset() {
|
||
| 154 | my $name = shift; |
||
| 155 | my $rtn = 'U'; |
||
| 156 | if (exists($peers{$name})) {
|
||
| 157 | $rtn = $peers{$name};
|
||
| 158 | } |
||
| 159 | return $rtn |
||
| 160 | } |
||
| 161 | |||
| 162 | =pod |
||
| 163 | |||
| 164 | =head1 Description |
||
| 165 | |||
| 166 | ntp_peers - A munin plugin to monitor offsets to multiple NTP peers and |
||
| 167 | graph them on a single graph |
||
| 168 | |||
| 169 | =head1 Parameters understood: |
||
| 170 | |||
| 171 | config (required) |
||
| 172 | autoconf (optional - used by munin-node-configure) |
||
| 173 | |||
| 174 | =head1 Configuration variables: |
||
| 175 | |||
| 176 | All configuration parameters are optional |
||
| 177 | |||
| 178 | ntpq - path to ntpq program |
||
| 179 | statedir - directory in which to place state file |
||
| 180 | hostname_<key> - override hostname for peer <key>. <key> is |
||
| 181 | an IPv4 address with dots replaced by underscores. |
||
| 182 | Useful for reference clocks, eg |
||
| 183 | env.hostname_127_127_43_0 .GPS. |
||
| 184 | |||
| 185 | =head1 Known issues |
||
| 186 | |||
| 187 | ntp_peers will not monitor IPv6 peers |
||
| 188 | |||
| 189 | =cut |
