都内 VDSL GMOBB ONU:VH-1004ES

左は フレッツ光マンション接続サービス (1時間おき) PC直付け
右は フレッツ光v6プラス接続サービス (5分おき) ルータ:WSR-1166DHPL
真ん中の欠落はたまたま。

v6プラス概要 https://www.jpne.co.jp/service/v6plus/
MAP-E詳細 https://www.jpne.co.jp/ebooks/v6plus-ebook.pdf

munin の plugin はこんなん。

#!/usr/local/bin/perl

#%# family=auto
#%# capabilities=autoconf suggest

use strict;
use warnings;
use feature ":5.10";
use Munin::Plugin;
use List::Util qw(first);
use File::Spec;

my $fib = ($0 =~ /_fib(\d+)/) && $1;
my $bin = do {
    my @candidates = (
        "/usr/local/bin/speedtest-cli",
    );
    first { -x $_ } @candidates;
};

sub fibs {
    my @fibs;
    my $max = `sysctl -n net.fibs`;
    for my $f (0..$max-1) {
        if ($f == 0) {
            my $mpd_enable = `sysrc -n mpd_enable` =~ /YES/i ? 1 : 0;
            if ($mpd_enable) {
                push @fibs, "fib$f";
            }
        }
        else {
            if (`setfib $f netstat -nr | grep default`) {
                 if ( $^O =~ /freebsd/ ) {
                      if ( system("setfib $f ping -o -t 3 8.8.8.8 >/dev/null") == 0 ) {
                            push @fibs, "fib$f";
                      }
                 }
                 else {
                      push @fibs, "fib$f";
                 }
            }
        }
    }
    @fibs;
}

if (@ARGV and $ARGV[0] eq "autoconf") {
    my @errors;
    my @fibs = fibs();
    unless (@fibs) {
        push @errors, "no fibs.";
    }
    unless ($bin) {
        push @errors, "speedtest-cli not found.";
    }

    if (@errors) {
        say "no (@{[ join ', ', @errors ]})";
    }
    else {
        say "yes";
    }
    exit 0;
}

if (@ARGV and $ARGV[0] eq 'suggest') {
    say $_ for fibs();
    exit 0;
}

if (@ARGV and $ARGV[0] eq "config") {
    say "graph_title speedtest-cli (fib$fib)";
    say "graph_args --base 1000";
    say "graph_category speedtest";
    say "graph_info 1時間ごとに計測します。";
    say "upload.type GAUGE";
    say "upload.label Upload";
    say "upload.min 0";
    say "download.type GAUGE";
    say "download.label Download";
    say "download.min 0";
    exit 0;
}

my $download = "U";
my $upload   = "U";
eval {
    my $statefile = File::Spec->catfile($ENV{MUNIN_PLUGSTATE}, "speedtest_fib$fib.state");

    if (-s $statefile) {
        my $out = do { local $/; open(my $fh, "<", $statefile); <$fh> };

        if ($out =~ /Download: ([\d.]+) Mbit.*Upload: ([\d.]+) Mbit/s) {
            $download = $1*1000*1000;
            $upload   = $2*1000*1000;
        }
        else {
            die "unexpected $bin output. $out";
        }
    }

    #my $boundary = int(time/3600)*3600-5*60;
    my $boundary = time;
    if (!-s $statefile or (stat($statefile))[9] < $boundary) {
        unless (fork) {
            # https://github.com/munin-monitoring/munin/search?q=run_as_child&unscoped_q=run_as_child
            open STDOUT, '>>/dev/null';
            open STDERR, '>>/dev/null';

            my $tmp = "$statefile.tmp";
            open my $fh, ">", $tmp or die $!;
            use Fcntl qw(:flock);
            if ( flock($fh, LOCK_EX) ) {
                system("sysctl net.inet.icmp.icmplim_output=0 >/dev/null");
                system("setfib $fib $bin --simple > $tmp") == 0 or die $!;
                system("mv $tmp $statefile") == 0 or die $!;
                system("sysctl net.inet.icmp.icmplim_output=1 >/dev/null");
            }
            exit 0;
        }
    }
};
die $@ if $@;

say "download.value $download";
say "upload.value $upload";
exit 0;

__END__

% speedtest-cli  --simple
Ping: 15.214 ms
Download: 447.64 Mbit/s
Upload: 104.26 Mbit/s

夕方〜0時頃の速度低下で5Mbpsも出ないことがあるのは避けられたが、それでも以前はこんなんだった筈。

コメントする

perl adv
perl adv