Move munin scripts in munin folder
This commit is contained in:
parent
6c9bb44c4b
commit
3493cd0e9d
3 changed files with 0 additions and 0 deletions
230
contrib/munin/boobank-munin
Executable file
230
contrib/munin/boobank-munin
Executable file
|
|
@ -0,0 +1,230 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
# vim: ft=python et softtabstop=4 cinoptions=4 shiftwidth=4 ts=4 ai
|
||||
|
||||
# Copyright(C) 2010-2011 Romain Bignon
|
||||
#
|
||||
# This file is part of weboob.
|
||||
#
|
||||
# weboob is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# weboob is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import os
|
||||
import sys
|
||||
import locale
|
||||
import time
|
||||
import logging
|
||||
from weboob.core import Weboob, CallErrors
|
||||
from weboob.capabilities.bank import CapBank
|
||||
from weboob.tools.browser import BrowserIncorrectPassword
|
||||
|
||||
class BoobankMuninPlugin(object):
|
||||
def __init__(self):
|
||||
if 'weboob_path' in os.environ:
|
||||
self.weboob = Weboob(os.environ['weboob_path'])
|
||||
else:
|
||||
self.weboob = Weboob()
|
||||
self.monitored_accounts = None
|
||||
if 'boobank_monitored' in os.environ:
|
||||
self.monitored_accounts = os.environ['boobank_monitored'].split(' ')
|
||||
self.cache_expire = long(os.environ.get('boobank_cache_expire', 3600))
|
||||
self.add_coming = int(os.environ.get('boobank_add_coming', 1))
|
||||
self.cumulate = int(os.environ.get('boobank_cumulate', 1))
|
||||
self.cache = None
|
||||
|
||||
def display_help(self):
|
||||
print 'boobank-munin is a plugin for munin'
|
||||
print ''
|
||||
print 'Copyright(C) 2010-2011 Romain Bignon'
|
||||
print ''
|
||||
print 'To use it, create a symlink /etc/munin/plugins/boobank to this script'
|
||||
print 'and add this section in /etc/munin/plugin-conf.d/munin-node:'
|
||||
print ''
|
||||
print '[boobank]'
|
||||
print 'user romain'
|
||||
print 'group romain'
|
||||
print 'env.HOME /home/romain'
|
||||
print '# The weboob directory path.'
|
||||
print 'env.weboob_path /home/romain/.config/weboob/'
|
||||
print '# Monitored accounts. If this parameter is missing, all accounts'
|
||||
print '# will be displayed.'
|
||||
print 'env.boobank_monitored 0125XXXXXXXXXXXX@bnporc 0125XXXXXXXXXXXX@bnporc'
|
||||
print '# To prevent mass connections to bank websites, results are cached.'
|
||||
print '# You can set here the expiration delay (in seconds).'
|
||||
print 'env.boobank_cache_expire 7200'
|
||||
print '# If enabled, coming operations are added to the value of accounts\''
|
||||
print '# balance.'
|
||||
print 'env.boobank_add_coming 1'
|
||||
print '# Cumulate accounts values'
|
||||
print 'env.boobank_cumulate 1'
|
||||
print ''
|
||||
print 'When you change configuration, you can use this command to reset cache:'
|
||||
print '$ boobank-munin --reset'
|
||||
|
||||
def clear_cache(self):
|
||||
for name in ('boobank-munin', 'boobank-munin-config'):
|
||||
try:
|
||||
os.unlink(self.cachepath(name))
|
||||
except IOError:
|
||||
pass
|
||||
|
||||
def cachepath(self, name):
|
||||
tmpdir = os.path.join(self.weboob.workdir, "munin")
|
||||
if not os.path.isdir(tmpdir):
|
||||
os.makedirs(tmpdir)
|
||||
|
||||
return os.path.join(tmpdir, name)
|
||||
|
||||
def check_cache(self, name):
|
||||
return self.print_cache(name, check=True)
|
||||
|
||||
def print_cache(self, name, check=False):
|
||||
try:
|
||||
f = open(self.cachepath(name), 'r')
|
||||
except IOError:
|
||||
return False
|
||||
|
||||
try:
|
||||
last = int(f.readline().strip())
|
||||
except ValueError:
|
||||
return False
|
||||
|
||||
if check and (last + self.cache_expire) < time.time():
|
||||
return False
|
||||
|
||||
for line in f.xreadlines():
|
||||
sys.stdout.write(line)
|
||||
return True
|
||||
|
||||
def new_cache(self, name):
|
||||
os.umask(0077)
|
||||
new_name = '%s.new' % name
|
||||
filename = self.cachepath(new_name)
|
||||
try:
|
||||
f = open(filename, 'w')
|
||||
except IOError, e:
|
||||
print >>sys.stderr, 'Unable to create the cache file %s: %s' % (filename, e)
|
||||
return
|
||||
|
||||
self.cache = f
|
||||
self.cache.write('%d\n' % time.time())
|
||||
|
||||
def flush_cache(self):
|
||||
old_name = self.cache.name
|
||||
new_name = self.cache.name[:-4]
|
||||
self.cache.close()
|
||||
os.rename(old_name, new_name)
|
||||
|
||||
def write_output(self, line):
|
||||
sys.stdout.write('%s\n' % line)
|
||||
if self.cache:
|
||||
self.cache.write('%s\n' % line)
|
||||
|
||||
def config(self):
|
||||
if self.check_cache('boobank-munin-config'):
|
||||
return
|
||||
|
||||
self.new_cache('boobank-munin-config')
|
||||
self.weboob.load_backends(CapBank)
|
||||
self.write_output('graph_title Bank accounts')
|
||||
self.write_output('graph_vlabel balance')
|
||||
self.write_output('graph_category weboob')
|
||||
self.write_output('graph_args -l 0')
|
||||
try:
|
||||
accounts = []
|
||||
if self.monitored_accounts is not None:
|
||||
d = {}
|
||||
for backend, account in self.weboob.do('iter_accounts'):
|
||||
if self.monitored(account):
|
||||
d['%s@%s' % (account.id, account.backend)] = account
|
||||
|
||||
for id in self.monitored_accounts:
|
||||
try:
|
||||
accounts.append(d[id])
|
||||
except KeyError:
|
||||
pass
|
||||
else:
|
||||
accounts = reversed([a for b, a in self.weboob.do('iter_accounts')])
|
||||
|
||||
first = True
|
||||
for account in accounts:
|
||||
id = self.account2id(account)
|
||||
type = 'STACK'
|
||||
if first:
|
||||
type = 'AREA'
|
||||
first = False
|
||||
self.write_output('%s.label %s' % (id, account.label.encode('iso-8859-15')))
|
||||
if self.cumulate:
|
||||
self.write_output('%s.draw %s' % (id, type))
|
||||
except CallErrors, errors:
|
||||
self.print_errors(errors)
|
||||
self.print_cache('boobank-munin-config')
|
||||
else:
|
||||
self.flush_cache()
|
||||
|
||||
def monitored(self, account):
|
||||
return not self.monitored_accounts or ('%s@%s' % (account.id, account.backend)) in self.monitored_accounts
|
||||
|
||||
def account2id(self, account):
|
||||
return '%s_%s' % (account.backend, account.id)
|
||||
|
||||
def print_errors(self, errors):
|
||||
for backend, err, backtrace in errors:
|
||||
print >>sys.stderr, (u'%s(%s): %s' % (type(err).__name__, backend.name, err)).encode(sys.stdout.encoding or locale.getpreferredencoding(), 'replace')
|
||||
if isinstance(err, BrowserIncorrectPassword):
|
||||
self.weboob.backends_config.edit_backend(backend.name, backend.NAME, {'_enabled': False})
|
||||
|
||||
def execute(self):
|
||||
if self.check_cache('boobank-munin'):
|
||||
return
|
||||
|
||||
self.new_cache('boobank-munin')
|
||||
self.weboob.load_backends(CapBank)
|
||||
try:
|
||||
for backend, account in self.weboob.do('iter_accounts'):
|
||||
if self.monitored(account):
|
||||
balance = account.balance
|
||||
if account.coming and self.add_coming:
|
||||
balance += account.coming
|
||||
self.write_output('%s.value %d' % (self.account2id(account), balance))
|
||||
except CallErrors, errors:
|
||||
self.print_errors(errors)
|
||||
self.print_cache('boobank-munin')
|
||||
else:
|
||||
self.flush_cache()
|
||||
|
||||
def run(self):
|
||||
cmd = (len(sys.argv) > 1 and sys.argv[1]) or "execute"
|
||||
if cmd == 'execute':
|
||||
self.execute()
|
||||
elif cmd == 'config':
|
||||
self.config()
|
||||
elif cmd == 'autoconf':
|
||||
print 'no'
|
||||
sys.exit(1)
|
||||
elif cmd == 'suggest':
|
||||
sys.exit(1)
|
||||
elif cmd == 'help' or cmd == '-h' or cmd == '--help':
|
||||
self.display_help()
|
||||
elif cmd == 'reload' or cmd == '--reload' or \
|
||||
cmd == 'reset' or cmd == '--reset':
|
||||
self.clear_cache()
|
||||
|
||||
if self.cache:
|
||||
self.cache.close()
|
||||
|
||||
sys.exit(0)
|
||||
|
||||
if __name__ == '__main__':
|
||||
logging.basicConfig()
|
||||
BoobankMuninPlugin().run()
|
||||
229
contrib/munin/freemobile-munin
Executable file
229
contrib/munin/freemobile-munin
Executable file
|
|
@ -0,0 +1,229 @@
|
|||
#!/usr/bin/env perl
|
||||
# -*- perl -*-
|
||||
=head1 NAME
|
||||
|
||||
freemobile - A plugin to monitor a freemobile subscription
|
||||
|
||||
=head1 INSTALLATION
|
||||
|
||||
Create a link to this script in /etc/munin/plugins/ :
|
||||
$ ln -s /path/to/freemobile-munin /etc/munin/plugins/freemobile
|
||||
|
||||
|
||||
=head1 CONFIGURATION
|
||||
|
||||
You need to configure the plugin like that:
|
||||
|
||||
[freemobile]
|
||||
user florent
|
||||
env.HOME /home/florent
|
||||
env.phonenumber 06ABCDEFGH
|
||||
env.freemonitored voice sms mms data specialvoice
|
||||
env.cache_expire 3600
|
||||
timeout 30
|
||||
|
||||
C<user> I<required>: user with freemobile backend configured
|
||||
|
||||
C<env.HOME> I<required>: path to user home
|
||||
|
||||
C<env.phonenumber> (optional): add your phone number if you have more than one
|
||||
subscription for this account.
|
||||
|
||||
C<env.freemonitored> (optional): default only 'voice sms'
|
||||
The full list of monitored options is :
|
||||
* voice sms mms data specialvoice voicetoint voiceint smsint mmsint dataint
|
||||
|
||||
C<env.cache_expire 3600> (optional): cache interval in second, or time
|
||||
between two connection to the website. The cache interval is 3 hours by default.
|
||||
|
||||
C<timeout 30> (optional): Munin internal option. The plugin can be slow,
|
||||
30s is recommended.
|
||||
|
||||
=head1 LICENSE
|
||||
|
||||
AGPLv3
|
||||
|
||||
=cut
|
||||
use strict;
|
||||
use warnings;
|
||||
use Carp;
|
||||
use English qw(-no_match_vars);
|
||||
use encoding 'iso-8859-1'; # Munin doesn't like utf-8 :-(
|
||||
use Encode;
|
||||
|
||||
my @monitored = split / /, $ENV{'freemonitored'} || 'voice sms';
|
||||
my $cachedir = $ENV{'HOME'} . '/.config/weboob/munin/';
|
||||
my $cachefile = "$cachedir/freemobile-munin";
|
||||
|
||||
my $refreshtime = $ENV{'cache_expire'} || 10_800;
|
||||
|
||||
my $phone = $ENV{'phonenumber'};
|
||||
my $account = '';
|
||||
|
||||
if (length($phone) > 0) {
|
||||
$account = $phone . '@freemobile';
|
||||
}
|
||||
|
||||
my $weboob = 'boobill -f table -b freemobile details ' . $account;
|
||||
my $cache_fh;
|
||||
|
||||
my %label = (
|
||||
'voice' => 'Voix en France (min)',
|
||||
'voicetoint' => 'Voix vers l\'international (min)',
|
||||
'specialvoice' => 'Numéros spéciaux (min)',
|
||||
'sms' => 'SMS en France',
|
||||
'mms' => 'MMS en France',
|
||||
'data' => 'Data en France',
|
||||
'voiceint' => 'Voix à l\'international (min)',
|
||||
'smsint' => 'SMS à l\'international',
|
||||
'mmsint' => 'MMS à l\'international',
|
||||
'dataint' => 'Data à l\'international',
|
||||
);
|
||||
|
||||
my %linenum = (
|
||||
'voice' => 3,
|
||||
'voicetoint' => 3,
|
||||
'specialvoice' => 4,
|
||||
'sms' => 5,
|
||||
'mms' => 6,
|
||||
'data' => 7,
|
||||
'voiceint' => 8,
|
||||
'smsint' => 9,
|
||||
'mmsint' => 10,
|
||||
'dataint' => 11,
|
||||
);
|
||||
|
||||
my %regexp = (
|
||||
'voice' => 'National : (\d+)h(\d+)min(\d+)s',
|
||||
'voicetoint' => 'International : (\d+)h(\d+)min(\d+)s',
|
||||
'specialvoice' => '\| (\d+)h(\d+) min (\d+)s',
|
||||
'sms' => 'Conso SMS \s+ \| (\d+) \/ (.*)',
|
||||
'mms' => 'Vous avez consommé (\d+) MMS',
|
||||
'data' => 'Vous avez consommé ([\d\-\.]+) (Mo|Go)',
|
||||
'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\-\.]+) (Mo|Go)',
|
||||
);
|
||||
|
||||
my %post = (
|
||||
'voice' => 'postvoice',
|
||||
'voicetoint' => 'postvoice',
|
||||
'specialvoice' => 'postvoice',
|
||||
'sms' => 'simplepost',
|
||||
'mms' => 'simplepost',
|
||||
'data' => 'datapost',
|
||||
'voiceint' => 'postvoice',
|
||||
'smsint' => 'simplepost',
|
||||
'mmsint' => 'simplepost',
|
||||
'dataint' => 'datapost',
|
||||
);
|
||||
|
||||
sub doubleprint {
|
||||
my $var = shift;
|
||||
print {$cache_fh} $var;
|
||||
print $var;
|
||||
return 0;
|
||||
}
|
||||
|
||||
sub postvoice {
|
||||
my @args = @_;
|
||||
my $minutes = $args[0] * 60 + $args[1] + $args[2] / 60;
|
||||
doubleprint "$minutes \n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
sub simplepost {
|
||||
my @args = @_;
|
||||
doubleprint "$args[0] \n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
sub datapost {
|
||||
my @args = @_;
|
||||
my $multi = 1;
|
||||
my $unit = $args[1];
|
||||
if ($unit eq "Go") {
|
||||
$multi = 1024;
|
||||
}
|
||||
$multi = $args[0] * $multi;
|
||||
doubleprint "$multi \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 {
|
||||
my @cache_data;
|
||||
# Check if cache exist and not older than the refresh threshold.
|
||||
if ( open $cache_fh, '<', $cachefile ) {
|
||||
@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;
|
||||
}
|
||||
}
|
||||
}
|
||||
# execute weboob
|
||||
open my $data, q(-|), $weboob or croak "Couldn't execute program: $ERRNO";
|
||||
my @lines = <$data>;
|
||||
close $data or carp "unable to close: $ERRNO";
|
||||
# If error output, print the cache (if exist) and exit
|
||||
if ( @lines == 0 ) {
|
||||
if ( @cache_data > 0 ) {
|
||||
print join q{}, @cache_data[ 1 .. $#cache_data ];
|
||||
exit 0;
|
||||
}
|
||||
exit 1;
|
||||
}
|
||||
|
||||
# Open cache for writing
|
||||
open $cache_fh, '>', $cachefile
|
||||
or croak "Failed to open file $cachefile";
|
||||
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;
|
||||
}
|
||||
|
||||
# Create the munin cache dir if missing
|
||||
if ( !-d $cachedir ) {
|
||||
mkdir $cachedir;
|
||||
}
|
||||
|
||||
if ( $ARGV[0] and $ARGV[0] eq 'config' ) {
|
||||
config;
|
||||
}
|
||||
else {
|
||||
fetch;
|
||||
}
|
||||
|
||||
__END__
|
||||
201
contrib/munin/nettokom-munin
Executable file
201
contrib/munin/nettokom-munin
Executable file
|
|
@ -0,0 +1,201 @@
|
|||
#!/usr/bin/env perl
|
||||
# -*- perl -*-
|
||||
=head1 NAME
|
||||
|
||||
nettokom - A plugin to monitor a nettokom subscription
|
||||
|
||||
=head1 INSTALLATION
|
||||
|
||||
Create a link to this script in /etc/munin/plugins/ :
|
||||
$ ln -s /path/to/nettokom-munin /etc/munin/plugins/nettokom
|
||||
|
||||
|
||||
=head1 CONFIGURATION
|
||||
|
||||
You need to configure the plugin like that:
|
||||
|
||||
[nettokom]
|
||||
user florent
|
||||
env.HOME /home/florent
|
||||
env.monitored 0177-XXXXXXXX
|
||||
env.exclude 02
|
||||
env.cache_expire 10800
|
||||
timeout 30
|
||||
|
||||
C<user> I<required>: user with nettokom backend configured
|
||||
|
||||
C<env.HOME> I<required>: path to user home
|
||||
|
||||
C<env.monitored> I<required>: phone number to generated the weboob ID
|
||||
|
||||
C<env.exclude> (optional): default nothing
|
||||
We can exclude some boobill output. The filter is on the label.
|
||||
Some examples:
|
||||
* festnetz community sms smsinsausland O2
|
||||
|
||||
C<env.cache_expire 10800> (optional): cache interval in second, or time
|
||||
between two connection to the website. The cache interval is 3 hours by default.
|
||||
Be aware that the website is only daily updated
|
||||
|
||||
C<timeout 30> (optional): Munin internal option. The plugin can be slow,
|
||||
30s is recommended.
|
||||
|
||||
=head1 LICENSE
|
||||
|
||||
AGPLv3
|
||||
|
||||
=cut
|
||||
use strict;
|
||||
use warnings;
|
||||
use Carp;
|
||||
use English qw(-no_match_vars);
|
||||
use encoding 'iso-8859-1'; # Munin doesn't like utf-8 :-(
|
||||
use Encode;
|
||||
|
||||
my $id = $ENV{'monitored'} || '';
|
||||
if ($id eq '') {
|
||||
print STDERR "Error: env.monitored not exist \n";
|
||||
exit 2;
|
||||
}
|
||||
$id = $id . '@nettokom';
|
||||
|
||||
my @exclude = split / /, $ENV{'exclude'} || '';
|
||||
my $cachedir = $ENV{'HOME'} . '/.config/weboob/munin/';
|
||||
my $cachefile = "$cachedir/nettokom-munin";
|
||||
my $cacheconfigfile = "$cachedir/nettokom-munin-config";
|
||||
|
||||
my $refreshtime = $ENV{'cache_expire'} || 10800;
|
||||
my $weboob = "/usr/bin/env boobill details $id -f table";
|
||||
my @knowlabels = ("AbgehendeSMSimAusland", "AnkommendeGespr_cheimAusland",
|
||||
"Community", "D1", "D2", "Deutschland", "Festnetz",
|
||||
"FreeCall", "Mailbox", "O2", "SMS", "SMSinsAusland");
|
||||
|
||||
my $cache_fh;
|
||||
|
||||
sub config {
|
||||
execute($cacheconfigfile, 1);
|
||||
}
|
||||
|
||||
sub fetch {
|
||||
execute($cachefile);
|
||||
}
|
||||
|
||||
sub doubleprint {
|
||||
my $var = shift;
|
||||
print {$cache_fh} $var;
|
||||
print $var;
|
||||
return 0;
|
||||
}
|
||||
|
||||
sub printconfig {
|
||||
my @lines = @_;
|
||||
|
||||
doubleprint <<'EOF';
|
||||
graph_title Conso Nettokom
|
||||
graph_vlabel Nettokom Verbindungs
|
||||
graph_category weboob
|
||||
graph_args -l 0
|
||||
EOF
|
||||
|
||||
my $first = 1;
|
||||
for my $label (@knowlabels) {
|
||||
my $shortlabel = $label;
|
||||
if (!(grep {$_ == $shortlabel} @exclude)) {
|
||||
doubleprint "$shortlabel.label $label\n";
|
||||
if ($first > 0) {
|
||||
doubleprint "$shortlabel.draw AREA\n";
|
||||
$first = 0;
|
||||
}
|
||||
else {
|
||||
doubleprint "$shortlabel.draw STACK\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
for my $line (@lines) {
|
||||
if ($line =~ /nettokom \| (.*) \| (\d)(.*) \| (\d).(\d)/) {
|
||||
my $label = $1;
|
||||
my $shortlabel = $label;
|
||||
$shortlabel =~ s/\s+//g;
|
||||
if (!(grep {$_ == $shortlabel} @exclude)) {
|
||||
if (!(grep {$_ == $shortlabel} @knowlabels)) {
|
||||
doubleprint "$shortlabel.label $label\n";
|
||||
doubleprint "$shortlabel.draw STACK\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sub printvalue {
|
||||
my @lines = @_;
|
||||
for my $line (@lines) {
|
||||
if ($line =~ /nettokom \| (.*) \| (\d)(.*) \| (\d).(\d)/) {
|
||||
my $label = $1;
|
||||
my $value = $2;
|
||||
my $shortlabel = $label;
|
||||
$shortlabel =~ s/\s+//g;
|
||||
if (!(grep {$_ == $shortlabel} @exclude)) {
|
||||
doubleprint "$shortlabel.value $value\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sub execute {
|
||||
my @cache_data;
|
||||
my $cachefile = $_[0];
|
||||
my $doconfig = $_[1];
|
||||
# Check if cache exist and not older than the refresh threshold.
|
||||
if ( open $cache_fh, '<', $cachefile ) {
|
||||
@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;
|
||||
}
|
||||
}
|
||||
}
|
||||
# execute weboob
|
||||
open my $data, q(-|), $weboob or croak "Couldn't execute program: $ERRNO";
|
||||
my @lines = <$data>;
|
||||
close $data or carp "unable to close: $ERRNO";
|
||||
# If error output, print the cache (if exist) and exit
|
||||
if ( @lines == 0 ) {
|
||||
if ( @cache_data > 0 ) {
|
||||
print join q{}, @cache_data[ 1 .. $#cache_data ];
|
||||
exit 0;
|
||||
}
|
||||
exit 1;
|
||||
}
|
||||
|
||||
# Open cache for writing
|
||||
open $cache_fh, '>', $cachefile
|
||||
or croak "Failed to open file $cachefile";
|
||||
print {$cache_fh} time . "\n";
|
||||
|
||||
if ($doconfig) {
|
||||
printconfig(@lines);
|
||||
}
|
||||
else {
|
||||
printvalue(@lines);
|
||||
}
|
||||
}
|
||||
|
||||
# Create the munin cache dir if missing
|
||||
if ( !-d $cachedir ) {
|
||||
mkdir $cachedir;
|
||||
}
|
||||
|
||||
if ( $ARGV[0] and $ARGV[0] eq 'config' ) {
|
||||
config;
|
||||
}
|
||||
else {
|
||||
fetch;
|
||||
}
|
||||
|
||||
__END__
|
||||
Loading…
Add table
Add a link
Reference in a new issue