#! /usr/bin/perl use strict; use warnings; use Munin::Plugin; my $config_filename = $ENV{"cubemap_config"} // "/etc/cubemap.config"; my $stats_filename = $ENV{"cubemap_stats"} // "/var/lib/cubemap/cubemap.stats"; my $mode = $ARGV[0] // "print"; if ($mode eq 'config') { print "graph_title Cubemap viewers\n"; print "graph_category network\n"; print "graph_vlabel viewers\n"; } my %streams = (); open my $config, "<", $config_filename or die "$config_filename: $!"; while (<$config>) { chomp; /^stream (\S+) / or next; my $stream = $1; $streams{$stream} = 0; my $stream_name = stream_name($stream); if ($mode eq 'config') { print "${stream_name}.label Number of viewers of $stream\n"; print "${stream_name}.type GAUGE\n"; print "${stream_name}.min 0\n"; } } close $config; my $total = 0; if ($mode eq 'config') { print "total.label Total number of viewers\n"; print "total.type GAUGE\n"; print "total.min 0\n"; } open my $stats, "<", $stats_filename or die "$stats_filename: $!"; while (<$stats>) { chomp; my ($ip, $fd, $mark, $stream, $connected_time, $bytes_sent, $loss_bytes, $loss_events) = /^(\S+) (\d+) (\d+) (\S+) (\d+) (\d+) (\d+) (\d+)/ or die "Invalid stats format"; ++$streams{$stream}; ++$total; } close $stats; if ($mode ne 'config') { for my $stream (sort keys %streams) { my $stream_name = stream_name($stream); printf "${stream_name}.value %d\n", $streams{$stream}; } printf "total.value %d\n", $total; } sub stream_name { my $stream = shift; $stream =~ y/a-z0-9/_/c; return $stream; }