]> git.sesse.net Git - cubemap/blob - munin/cubemap
Silence an irrelevant Coverity Scan warning.
[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 my $unknown = 0;
38 if ($mode eq 'config') {        
39         print "total.label Total number of viewers\n";
40         print "total.type GAUGE\n";
41         print "total.min 0\n";
42         print "unknown.label Number of connections not watching a stream\n";
43         print "unknown.type GAUGE\n";
44         print "unknown.min 0\n";
45 }
46
47 open my $stats, "<", $stats_filename
48         or die "$stats_filename: $!";
49 while (<$stats>) {
50         chomp;
51         my ($ip, $fd, $mark, $stream, $connected_time, $bytes_sent, $loss_bytes, $loss_events) = 
52                 /^(\S+) (\d+) (\d+) (\S+) (\d+) (\d+) (\d+) (\d+)/ or die "Invalid stats format";
53         $stream =~ s/\?frag=.*//;
54         if (defined($streams{$stream})) {
55                 ++$streams{$stream};
56                 ++$total;
57         } else {
58                 ++$unknown;
59         }
60 }
61 close $stats;
62         
63 if ($mode ne 'config') {
64         for my $stream (sort keys %streams) {
65                 my $stream_name = stream_name($stream);
66                 printf "${stream_name}.value %d\n", $streams{$stream};
67         }
68         printf "total.value %d\n", $total;
69         printf "unknown.value %d\n", $unknown;
70 }
71
72 sub stream_name {
73         my $stream = shift;
74         $stream =~ y/a-z0-9/_/c;
75         return $stream;
76 }