Code cleanup
* Complies to perltidy * Avoid useless loops * Always use strict! * Remove unused vars * Use croak instead of die * End every function with return * Use English instead of magic punctuation * Avoid useless variable interpolations * Use functions * Fix POD syntax
This commit is contained in:
parent
d70e72486a
commit
259bef4739
1 changed files with 130 additions and 123 deletions
|
|
@ -1,176 +1,183 @@
|
||||||
#!/usr/bin/env perl
|
#!/usr/bin/env perl
|
||||||
# -*- perl -*-
|
# -*- perl -*-
|
||||||
|
|
||||||
=head1 NAME
|
=head1 NAME
|
||||||
|
|
||||||
freemobile - A plugin to monitor a freemobile subscription
|
freemobile - A plugin to monitor a freemobile subscription
|
||||||
create a link to this script in /etc/munin/plugins/
|
|
||||||
|
|
||||||
=note1
|
=head1 Installation
|
||||||
|
|
||||||
You have to manually create the folder:
|
Create a link to this script in /etc/munin/plugins/
|
||||||
$HOME/.config/weboob/munin/
|
|
||||||
|
I<You have to manually create the folder:>
|
||||||
|
F<$HOME/.config/weboob/munin/>
|
||||||
|
|
||||||
=head1 CONFIGURATION
|
=head1 CONFIGURATION
|
||||||
|
|
||||||
You need to configure the user and the home PATH like that:
|
You need to configure the user and the home PATH like that:
|
||||||
|
|
||||||
[freemobile]
|
[freemobile]
|
||||||
env.HOME /home/florent
|
env.HOME /home/florent
|
||||||
user florent
|
user florent
|
||||||
group florent
|
group florent
|
||||||
|
|
||||||
You can configure the list of monitored data with the line:
|
You can configure the list of monitored data with the line:
|
||||||
env.freemonitored voice sms mms data
|
C<env.freemonitored voice sms mms data>
|
||||||
|
|
||||||
Others monitored options: voicetoint voiceint smsint mmsint dataint
|
Others monitored options: voicetoint voiceint smsint mmsint dataint
|
||||||
specialvoice
|
specialvoice
|
||||||
|
|
||||||
The cache timeout is 3 hours by default. You can change this value
|
The cache timeout is 3 hours by default. You can change this value
|
||||||
with env.cache_expire.
|
with C<env.cache_expire 3600> (seconds).
|
||||||
|
|
||||||
|
=head1 LICENSE
|
||||||
|
|
||||||
|
AGPLv3
|
||||||
|
|
||||||
=cut
|
=cut
|
||||||
|
use strict;
|
||||||
use warnings;
|
use warnings;
|
||||||
# Munin doesn't like utf-8 :-(
|
use Carp;
|
||||||
use encoding "iso-8859-1";
|
use English qw(-no_match_vars);
|
||||||
|
use encoding 'iso-8859-1'; # Munin doesn't like utf-8 :-(
|
||||||
use Encode;
|
use Encode;
|
||||||
|
|
||||||
my @monitored = split(/ /, $ENV{'freemonitored'} || 'voice sms');
|
my @monitored = split / /, $ENV{'freemonitored'} || 'voice sms';
|
||||||
my $cachefile = $ENV{'HOME'} . '/.config/weboob/munin/freemobile-munin';
|
my $cachefile = $ENV{'HOME'} . '/.config/weboob/munin/freemobile-munin';
|
||||||
my $refreshtime = $ENV{'cache_expire'} || 10800;
|
|
||||||
my $weboob = "/usr/bin/env weboob-cli ICapBill get_details coin -f table |";
|
my $refreshtime = $ENV{'cache_expire'} || 10_800;
|
||||||
|
my $weboob = '/usr/bin/env weboob-cli ICapBill get_details coin -f table';
|
||||||
|
my $cache_fh;
|
||||||
|
|
||||||
my %label = (
|
my %label = (
|
||||||
'voice' => "Voix en France (min)\n",
|
'voice' => 'Voix en France (min)',
|
||||||
'voicetoint' => "Voix vers l'international (min)\n",
|
'voicetoint' => 'Voix vers l\'international (min)',
|
||||||
'specialvoice' => "Numéro spéciaux (min) \n",
|
'specialvoice' => 'Numéro spéciaux (min) ',
|
||||||
'sms' => "SMS en France\n",
|
'sms' => 'SMS en France',
|
||||||
'mms' => "MMS en France\n",
|
'mms' => 'MMS en France',
|
||||||
'data' => "Data en France\n",
|
'data' => 'Data en France',
|
||||||
'voiceint' => "Voix à l\'international (min)\n",
|
'voiceint' => 'Voix à l\'international (min)',
|
||||||
'smsint' => "SMS à l\'international\n",
|
'smsint' => 'SMS à l\'international',
|
||||||
'mmsint' => "MMS à l\'international\n",
|
'mmsint' => 'MMS à l\'international',
|
||||||
'dataint' => "Data à l\'international\n"
|
'dataint' => 'Data à l\'international',
|
||||||
);
|
);
|
||||||
|
|
||||||
my %linenum = (
|
my %linenum = (
|
||||||
'voice' => 3,
|
'voice' => 3,
|
||||||
'voicetoint' => 3,
|
'voicetoint' => 3,
|
||||||
'specialvoice' => 4,
|
'specialvoice' => 4,
|
||||||
'sms' => 5,
|
'sms' => 5,
|
||||||
'mms' => 6,
|
'mms' => 6,
|
||||||
'data' => 7,
|
'data' => 7,
|
||||||
'voiceint' => 8,
|
'voiceint' => 8,
|
||||||
'smsint' => 9,
|
'smsint' => 9,
|
||||||
'mmsint' => 10,
|
'mmsint' => 10,
|
||||||
'dataint' => 11
|
'dataint' => 11,
|
||||||
);
|
);
|
||||||
|
|
||||||
my %regexp = (
|
my %regexp = (
|
||||||
'voice' => 'National : (\d+)h(\d+)min(\d+)s',
|
'voice' => 'National : (\d+)h(\d+)min(\d+)s',
|
||||||
'voicetoint' => 'International : (\d+)h(\d+)min(\d+)s',
|
'voicetoint' => 'International : (\d+)h(\d+)min(\d+)s',
|
||||||
'specialvoice' => '\| (\d+)h(\d+) min (\d+)s',
|
'specialvoice' => '\| (\d+)h(\d+) min (\d+)s',
|
||||||
'sms' => 'Conso SMS \| (\d+) \/ (.*)',
|
'sms' => 'Conso SMS \| (\d+) \/ (.*)',
|
||||||
'mms' => 'Vous avez consommé (\d+) MMS',
|
'mms' => 'Vous avez consommé (\d+) MMS',
|
||||||
'data' => 'Vous avez consommé ([\d\-\.]+) Mo',
|
'data' => 'Vous avez consommé ([\d\-\.]+) Mo',
|
||||||
'voiceint' => 'Appels émis (\d+)h(\d+)min(\d+)s',
|
'voiceint' => 'Appels émis (\d+)h(\d+)min(\d+)s',
|
||||||
'smsint' => 'Conso SMS (international) \| (\d+)',
|
'smsint' => 'Conso SMS (international) \| (\d+)',
|
||||||
'mmsint' => 'Vous avez consommé (\d+) MMS',
|
'mmsint' => 'Vous avez consommé (\d+) MMS',
|
||||||
'dataint' => 'Vous avez consommé ([\d\-]+) Mo'
|
'dataint' => 'Vous avez consommé ([\d\-]+) Mo',
|
||||||
);
|
);
|
||||||
|
|
||||||
my %post = (
|
my %post = (
|
||||||
'voice' => 'postvoice',
|
'voice' => 'postvoice',
|
||||||
'voicetoint' => 'postvoice',
|
'voicetoint' => 'postvoice',
|
||||||
'specialvoice' => 'postvoice',
|
'specialvoice' => 'postvoice',
|
||||||
'sms' => 'simplepost',
|
'sms' => 'simplepost',
|
||||||
'mms' => 'simplepost',
|
'mms' => 'simplepost',
|
||||||
'data' => 'simplepost',
|
'data' => 'simplepost',
|
||||||
'voiceint' => 'postvoice',
|
'voiceint' => 'postvoice',
|
||||||
'smsint' => 'simplepost',
|
'smsint' => 'simplepost',
|
||||||
'mmsint' => 'simplepost',
|
'mmsint' => 'simplepost',
|
||||||
'dataint' => 'simplepost'
|
'dataint' => 'simplepost',
|
||||||
);
|
);
|
||||||
|
|
||||||
my @lines;
|
|
||||||
my $cache;
|
|
||||||
my $expr;
|
|
||||||
|
|
||||||
if ($ARGV[0] and $ARGV[0] eq "config") {
|
|
||||||
binmode STDOUT, ":encoding(iso-8859-1)";
|
|
||||||
print "graph_title Conso Free\n";
|
|
||||||
print "graph_vlabel Suivi conso du forfait Free Mobile\n";
|
|
||||||
print "graph_category weboob\n";
|
|
||||||
print "graph_args -l 0\n";
|
|
||||||
foreach (@monitored) {
|
|
||||||
print "$_" . ".label ";
|
|
||||||
print $label{$_};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
my $refresh;
|
|
||||||
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 - $_ > $refreshtime) {
|
|
||||||
$refresh = 0;
|
|
||||||
last;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$refresh = 1;
|
|
||||||
}
|
|
||||||
$first = 0;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
print $_;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
close(CACHE);
|
|
||||||
exit if $cache and $refresh;
|
|
||||||
|
|
||||||
# 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");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
close(CACHE);
|
|
||||||
}
|
|
||||||
|
|
||||||
sub doubleprint {
|
sub doubleprint {
|
||||||
print CACHE $_[0];
|
my $var = shift;
|
||||||
print $_[0];
|
print {$cache_fh} $var;
|
||||||
|
print $var;
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub postvoice {
|
sub postvoice {
|
||||||
my @args = split(/ /, $_[0]);
|
my @args = @_;
|
||||||
my $minutes = $args[0] * 60 + $args[1] + $args[2] / 60;
|
my $minutes = $args[0] * 60 + $args[1] + $args[2] / 60;
|
||||||
doubleprint("$minutes \n");
|
doubleprint "$minutes \n";
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub simplepost {
|
sub simplepost {
|
||||||
my @args = split(/ /,$_[0]);
|
my @args = @_;
|
||||||
doubleprint("$args[0] \n");
|
doubleprint "$args[0] \n";
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub config {
|
||||||
|
binmode STDOUT, ':encoding(iso-8859-1)';
|
||||||
|
print <<'EOF';
|
||||||
|
graph_title Conso Free
|
||||||
|
graph_vlabel Suivi conso du forfait Free Mobile
|
||||||
|
graph_category weboob
|
||||||
|
graph_args -l 0
|
||||||
|
EOF
|
||||||
|
foreach (@monitored) {
|
||||||
|
print "$_.label $label{$_}\n";
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
sub fetch {
|
||||||
|
|
||||||
|
# Check if cache exist and not older than the refresh threshold.
|
||||||
|
if ( open $cache_fh, '<', $cachefile ) {
|
||||||
|
my @cache_data = <$cache_fh>;
|
||||||
|
close $cache_fh or croak "unable to close: $ERRNO";
|
||||||
|
|
||||||
|
# File not empty?
|
||||||
|
if ( @cache_data > 0 ) {
|
||||||
|
|
||||||
|
# Data is still fresh. Display cached values and exit.
|
||||||
|
if ( time - $cache_data[0] < $refreshtime ) {
|
||||||
|
print join q{}, @cache_data[ 1 .. $#cache_data ];
|
||||||
|
exit 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# Open cache for writing and execute weboob
|
||||||
|
open $cache_fh, '>', $cachefile
|
||||||
|
or croak "Failed to open file $cachefile";
|
||||||
|
open my $data, q(-|), $weboob or croak "Couldn't execute program: $ERRNO";
|
||||||
|
my @lines = <$data>;
|
||||||
|
close $data or carp "unable to close: $ERRNO";
|
||||||
|
print {$cache_fh} time . "\n";
|
||||||
|
|
||||||
|
foreach my $monit (@monitored) {
|
||||||
|
doubleprint "$monit.value ";
|
||||||
|
if ( my @results = $lines[ $linenum{$monit} ] =~ /$regexp{$monit}/ ) {
|
||||||
|
my $postfunction = $post{$monit};
|
||||||
|
&{ \&$postfunction }(@results);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
doubleprint "0 \n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
close $cache_fh or croak "unable to close: $ERRNO";
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( $ARGV[0] and $ARGV[0] eq 'config' ) {
|
||||||
|
config;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
fetch;
|
||||||
|
}
|
||||||
|
|
||||||
|
__END__
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue