Add a script to monitor freemobile subscription with munin
This commit is contained in:
parent
2ec85f0bfc
commit
916dc58cc2
1 changed files with 161 additions and 0 deletions
161
contrib/freemobile-munin
Executable file
161
contrib/freemobile-munin
Executable file
|
|
@ -0,0 +1,161 @@
|
||||||
|
#!/usr/bin/env perl
|
||||||
|
# -*- perl -*-
|
||||||
|
|
||||||
|
=head1 NAME
|
||||||
|
|
||||||
|
freemobile - Plugin to monitor a freemobile subscription
|
||||||
|
copy this script to /etc/munin/plugins/freemobile
|
||||||
|
|
||||||
|
=note1
|
||||||
|
|
||||||
|
You have to manually create the folder:
|
||||||
|
$HOME/.config/weboob/munin/
|
||||||
|
|
||||||
|
=head1 CONFIGURATION
|
||||||
|
|
||||||
|
You need to configure the user and the home PATH like that:
|
||||||
|
|
||||||
|
[freemobile]
|
||||||
|
env.HOME /home/florent
|
||||||
|
user florent
|
||||||
|
group florent
|
||||||
|
|
||||||
|
You can configure the list of monitored data with the ligne:
|
||||||
|
env.freemonitored voice sms mms data
|
||||||
|
|
||||||
|
Others monitored options: voicetoint voiceint smsint mmsint dataint
|
||||||
|
|
||||||
|
WARNING: Some monitored options are *not* tested by author
|
||||||
|
|
||||||
|
=cut
|
||||||
|
use warnings;
|
||||||
|
|
||||||
|
my @monitored = split(/ /, $ENV{'freemonitored'} || 'voice sms');
|
||||||
|
my $cachefile = $ENV{'HOME'} . '/.config/weboob/munin/freemobile-munin';
|
||||||
|
my $weboob = "/usr/local/bin/weboob-cli ICapBill get_details coin -f table |";
|
||||||
|
|
||||||
|
my %label = (
|
||||||
|
'voice' => "Voix en France (min)\n",
|
||||||
|
'voicetoint' => "Voix vers l'international (min)\n",
|
||||||
|
'sms' => "SMS en France\n",
|
||||||
|
'mms' => "MMS en France",
|
||||||
|
'data' => "Data en France\n",
|
||||||
|
'voiceint' => "Voix à l\'international (min)\n",
|
||||||
|
'smsint' => "SMS à l\'international\n",
|
||||||
|
'mmsint' => "MMS à l\'international\n",
|
||||||
|
'dataint' => "Data à l\'international\n"
|
||||||
|
);
|
||||||
|
|
||||||
|
my %linenum = (
|
||||||
|
'voice' => 3,
|
||||||
|
'voicetoint' => 3,
|
||||||
|
'sms' => 5,
|
||||||
|
'mms' => 6,
|
||||||
|
'data' => 7,
|
||||||
|
'voiceint' => 8,
|
||||||
|
'smsint' => 9,
|
||||||
|
'mmsint' => 10,
|
||||||
|
'dataint' => 11
|
||||||
|
);
|
||||||
|
|
||||||
|
my %regexp = (
|
||||||
|
'voice' => 'Consommation : (\d+)h(\d+)min(\d+)s',
|
||||||
|
'voicetoint' => 'International : (\d+)h(\d+)min(\d+)s',
|
||||||
|
'sms' => 'Conso SMS \| (\d+) \/ (\d+)',
|
||||||
|
'mms' => 'Vous avez consommé (\d+) MMS',
|
||||||
|
'data' => 'Vous avez consommé (\d+) o',
|
||||||
|
'voiceint' => 'Appels émis (\d+)h(\d+)min(\d+)s',
|
||||||
|
'smsint' => 'Conso SMS (international) \| (\d+)',
|
||||||
|
'mmsint' => 'Vous avez consommé (\d+) MMS',
|
||||||
|
'dataint' => 'Vous avez consommé (\d+) o'
|
||||||
|
);
|
||||||
|
|
||||||
|
my %post = (
|
||||||
|
'voice' => 'postvoice',
|
||||||
|
'voicetoint' => 'postvoice',
|
||||||
|
'sms' => 'simplepost',
|
||||||
|
'mms' => 'simplepost',
|
||||||
|
'data' => 'simplepost',
|
||||||
|
'voiceint' => 'postvoice',
|
||||||
|
'smsint' => 'simplepost',
|
||||||
|
'mmsint' => 'simplepost',
|
||||||
|
'dataint' => 'simplepost'
|
||||||
|
);
|
||||||
|
|
||||||
|
my @lines;
|
||||||
|
my $cache;
|
||||||
|
my $expr;
|
||||||
|
|
||||||
|
if ($ARGV[0] and $ARGV[0] eq "config") {
|
||||||
|
print "graph_title Conso Free\n";
|
||||||
|
print "graph_vlabel Suivi conso du forfait 2 euros\n";
|
||||||
|
print "graph_category weboob\n";
|
||||||
|
print "graph_args -l 0\n";
|
||||||
|
foreach (@monitored) {
|
||||||
|
print "$_" . ".label ";
|
||||||
|
print $label{$_};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
my $first = 1;
|
||||||
|
my $time = time();
|
||||||
|
# Check if cache exist and not older than 3 hours
|
||||||
|
if (open(CACHE, "< " . $cachefile)) {
|
||||||
|
$cache = 1;
|
||||||
|
LOOP: while(<CACHE>) {
|
||||||
|
if ($first) {
|
||||||
|
if ($time - $_ > 10800) {
|
||||||
|
$cache = 0;
|
||||||
|
last;
|
||||||
|
}
|
||||||
|
$first = 0;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
print $_;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
close(CACHE);
|
||||||
|
exit if $cache;
|
||||||
|
|
||||||
|
# Open cache for writing and execute weboob
|
||||||
|
open(CACHE, "> " . $cachefile) or die "Failed to open file $cachefile";
|
||||||
|
open(DATA, $weboob) or die "Couldn't execute program: $!";
|
||||||
|
LOOP: while (<DATA>) {
|
||||||
|
push(@lines,$_);
|
||||||
|
}
|
||||||
|
close(DATA);
|
||||||
|
print CACHE time(). "\n";
|
||||||
|
|
||||||
|
foreach $monit (@monitored) {
|
||||||
|
doubleprint("$monit" . ".value ");
|
||||||
|
if ($lines[$linenum{$monit}] =~ /$regexp{$monit}/ ) {
|
||||||
|
my $string;
|
||||||
|
foreach $expr (1..$#-) {
|
||||||
|
$string = $string . "${$expr} ";
|
||||||
|
}
|
||||||
|
my $postfunction = $post{$monit};
|
||||||
|
&$postfunction($string);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
doubleprint("0 \n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sub doubleprint {
|
||||||
|
print CACHE $_[0];
|
||||||
|
print $_[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
sub postvoice {
|
||||||
|
my @args = split(/ /, $_[0]);
|
||||||
|
my $minutes = $args[0] * 60 + $args[1] + $args[2] / 60;
|
||||||
|
doubleprint("$minutes \n");
|
||||||
|
}
|
||||||
|
|
||||||
|
sub simplepost {
|
||||||
|
my @args = split(/ /,$_[0]);
|
||||||
|
doubleprint("$args[0] \n");
|
||||||
|
}
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue