2
0
mirror of https://github.com/munin-monitoring/contrib.git synced 2018-11-08 00:59:34 +01:00

Read options from a config file or as the programm arguments, update POD

This commit is contained in:
Alex Mestiashvili 2016-12-06 18:36:33 +01:00
parent 05d4ad0ac7
commit 9c29d49666

View File

@ -1,24 +1,66 @@
#!/usr/bin/perl
use warnings;
use strict;
use NetSNMP::OID;
use NetSNMP::ASN (':all');
use NetSNMP::agent (':all');
use IO::Socket;
use Getopt::Long;
use Pod::Usage;
my %cache = (); # Cache
my @cache_oids = (); # Keys, sorted
my $cache_updated = 0;
my $oidbase = ".1.3.6.1.4.1.123456.100.1.1";
my $delimiter = ' = ';
my $conf = '/etc/munin2snmp.conf';
my %config;
my $pidfile = '/var/run/munin2snmp.pid';
my $usage = "Usage: $0 [options] \
Options:
--help : print help message\
--host [host] : munin-node host, default localhost\
--port [port] : munin-node port, default 4949\
--base_oid [OID] : base oid, default .1.3.6.1.4.1.123456.100.1.1\
Don't forget to update MUNIN-MIB OBJECT IDENTIFIER if you modify base_oid\
--plugins [load,cpu,..] : comma separated list of munin-node plugins, default cpu,load,df\
--pidfile [file path] : pidfile, default /var/run/munin2snmp.pid\
";
sub read_conf {
return if ( ! -e $conf );
open my $conf_fh,'<',$conf or die "Can't open the $conf, $!\n";
while (<$conf_fh>) {
chomp;
my ($param, $val) = split(/\s*=\s*/);
$config{"$param"} = $val;
}
}
# initialize
my %Munin;
$Munin{PORT} = '4949';
$Munin{HOST} = 'localhost';
if ( ! scalar @ARGV ) {
read_conf;
}
GetOptions (
"help" => sub{pod2usage($usage)},
"host=s" => \$config{munin_host},
"port=s" => \$config{munin_port},
"base_oid=s" => \$config{'base_oid'},
"plugins=s" => \$config{'munin_plugins'},
"pidfile=s" => \$pidfile,
);
$Munin{PORT} = $config{'munin_port'} || '4949';
$Munin{HOST} = $config{'munin_host'} || 'localhost';
my $oidbase = $config{'base_oid'} || '.1.3.6.1.4.1.123456.100.1.1';
# See munin plugins dir for more plugins
our @munin_plugins= qw ( load swap users uptime vmstat df );
my @munin_plugins = qw (cpu load df);
@munin_plugins = split(',',$config{'munin_plugins'}) if ( $config{'munin_plugins'});
open my $pidfd,'>',$pidfile or die "Can't open $pidfile, $!\n";
print $pidfd $$;
close $pidfd;
# Update cache
sub update_stats {
@ -59,7 +101,7 @@ sub handle_stats {
if (exists $cache{$noid}) {
$request->setValue(ASN_OCTET_STR, "$cache{$noid}");
}
}
}
elsif ($request_info->getMode() == MODE_GETNEXT) {
# For a GETNEXT, we need to find a best match. This is the
# first match strictly superior to the requested OID.
@ -100,6 +142,7 @@ $agent->shutdown();
sub shutdown {
# Shutdown requested
$running = 0;
unlink $pidfile if -e $pidfile;
}
#muninwalk
@ -147,7 +190,7 @@ munin2snmp - SNMP Agent to query munin-node over snmp
=head1 REQUIREMENTS
Net::SNMP and IO::Socket perl modules, munin-node with some plugins
Net::SNMP, Getopt::Long, Pod::Usage perl modules, munin-node with some plugins
=head2 Example configuration
@ -165,12 +208,28 @@ or another place where snmpd expects to find the MIB files.
See also http://www.net-snmp.org/wiki/index.php/FAQ:MIBs_03
It is possible to start munin2snmp as non-root user, for example
run munin2snmp as Debian-snmp user on Debian Stretch:
fix the /var/agentx permissions:
chmod g+rx /var/agentx
chgrp Debian-snmp /var/agentx
add to /etc/snmp/snmpd.conf:
master agentx
agentXperms 0640 0550 Debian-snmp Debian-snmp
restart snmpd and start the agent as Debian-snmp:
su -l Debian-snmp -s /bin/bash -c "/tmp/munin2snmp.pl --pidfile /tmp/munin2snmp.pid --plugins iostat,vmstat"
=head2 Usage
After setting up snmpd, start the agent:
./munin2snmp.pl
./munin2snmp
Now one can query the agent
@ -179,6 +238,8 @@ Now one can query the agent
where "1.3.6.1.4.1.123456.100.1.1" is example OID selected as the base
tree for the agent.
Change OBJECT IDENTIFIER in the MUNIN-MIB file if you plan to use a different OID.
You might need to change the host, port, oidbase and munin_plugins you want to use.
The defaults:
@ -186,7 +247,17 @@ The defaults:
$Munin{PORT} = '4949';
$Munin{HOST} = 'localhost'
$oidbase = ".1.3.6.1.4.1.123456.100.1.1"
@munin_plugins = qw ( load swap users uptime vmstat df );
@munin_plugins = qw ( load cpu df );
One can override the defaults by creating /etc/munin2snmp.conf file with the following
configuration options:
munin_port = [port]
munin_host = [host]
base_oid = [oid]
munin_plugins = [comma separated list of munin-node plugins]
Or by specifying the parameters, see munin2snmp --help for the usage
=head1 ACKNOWLEDGEMENTS
@ -194,12 +265,20 @@ Heavily inspired by
Vincent Bernat: https://github.com/vincentbernat/extend-netsnmp
and Masahito Zembutsu: https://github.com/zembutsu/muninwalk
=head1 AUTHOR
Alex Mestiashvili <mailatgoogl@gmail.com>
=head1 LICENSE
GPLv2
ISC License (ISC)
Copyright (c) 2016, Alex Mestiashvili <mailatgoogl@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.