IPFS Stats

This doesn't serve any real purpose. I just like seeing trends of the tcp/udp connections to the IPFS network. Yes I get lazy at the end with a system call.

#!/usr/bin/perl

use warnings;
use strict;

my %peers;
my $DEST = "INFLXUDBIP";
my $HOSTNAME = "host-foo";
open(FH, "/usr/local/bin/ipfs swarm peers |") || die $!;
while (my $line = <FH>) {
  chomp($line);
  #TCP
  if ($line =~ /\/ip4\/(\d+\.\d+\.\d+\.\d+)\/tcp\/(\d+)\/p2p\/(\S+)/) {
     $peers{"tcp"}++;
  }
  #UDP
  if ($line =~ /\/ip4\/(\d+\.\d+\.\d+\.\d+)\/udp\/(\d+)\/quic\/p2p\/(\S+)/) {
     $peers{"udp"}++;
  }
}

close(FH);

my $poststr = "ipfs,host=$HOSTNAME tcp=$peers{tcp},udp=$peers{udp},total=" . ($peers{tcp}+$peers{udp}) . "";
system("curl -X POST --data-binary \"$poststr\" http://$DEST:8086/write?db=mydb");