]> git.sesse.net Git - cubemap/blob - munin/cubemap
8079d6e4d750b86e92ed8f595a535046b76d5503
[cubemap] / munin / cubemap
1 #! /usr/bin/perl
2
3 use strict;
4 use warnings;
5 use Munin::Plugin;
6
7 my $config_filename = $ENV{"cubemap_config"} // "/etc/cubemap.config";
8 my $stats_filename = $ENV{"cubemap_stats"} // "/var/lib/cubemap/cubemap.stats";
9
10 my $mode = $ARGV[0] // "print";
11 if ($mode eq 'config') {
12         print "graph_title Cubemap viewers\n";
13         print "graph_category network\n";
14         print "graph_vlabel viewers\n";
15 }
16
17 my %streams = ();
18
19 open my $config, "<", $config_filename
20         or die "$config_filename: $!";
21 while (<$config>) {
22         chomp;
23         /^stream (\S+) / or next;
24         my $stream = $1;
25         $streams{$stream} = 0;
26
27         my $stream_name = stream_name($stream);
28         if ($mode eq 'config') {        
29                 print "${stream_name}.label Number of viewers of $stream\n";
30                 print "${stream_name}.type GAUGE\n";
31                 print "${stream_name}.min 0\n";
32         }
33 }
34 close $config;
35
36 my $total = 0;  
37 if ($mode eq 'config') {        
38         print "total.label Total number of viewers\n";
39         print "total.type GAUGE\n";
40         print "total.min 0\n";
41 }
42
43 open my $stats, "<", $stats_filename
44         or die "$stats_filename: $!";
45 while (<$stats>) {
46         chomp;
47         my ($ip, $fd, $mark, $stream, $connected_time, $bytes_sent, $loss_bytes, $loss_events) = 
48                 /^(\S+) (\d+) (\d+) (\S+) (\d+) (\d+) (\d+) (\d+)/ or die "Invalid stats format";
49         ++$streams{$stream};
50         ++$total;
51 }
52 close $stats;
53         
54 if ($mode ne 'config') {
55         for my $stream (sort keys %streams) {
56                 my $stream_name = stream_name($stream);
57                 printf "${stream_name}.value %d\n", $streams{$stream};
58         }
59         printf "total.value %d\n", $total;
60 }
61
62 sub stream_name {
63         my $stream = shift;
64         $stream =~ y/a-z0-9/_/c;
65         return $stream;
66 }