WhyInstallation of an SNMP server providing access to management data such as networking activities, memory use, processes status, … Data will be shared read-only and protected by a simple password (aka: community string).

See also
Build information

Ensure the following options:

net-mgmt/net-snmp
[x] IPV6             IPv6 protocol support
[x] MFD_REWRITES     Use new MFD rewrites of mib modules
[x] PERL             Perl scripting language support
[x] PERL_EMBEDDED    Build embedded perl
[x] SMUX             Build with SNMP multiplexing (SMUX) support

Configuration

The SNMP daemon is configured for a read-only access from the community Password, only the MIBs which are used will be exported.

It’s possible to split the configuration in two separate files, the first one holding a generic configuration (snmpd.conf), the second one holding a configuration specific to the computer (snmpd.local.conf).

snmpd.conf

The information is shared read-only and is only protected by a password: Password. The 2c version of the protocol is used. The version 3 would have allowed a stronger security with the integration of confidentiality, integrity and authentication.

# Access control
######################################################################
#       sec.name   source       community
com2sec readonly   default      Password
 
#       name       sec.model    sec.name
group   ROSystem   v2c          readonly
group   ROSystem   usm          readonly

#                  context  sec.model  sec.level  match  read    write  notif
access  ROSystem   ""       any        noauth     exact  system  none   none

Set the different level of visibility for the MIBs, the information which will be published are about the network interfaces, resources (memory, process, disk) and system status (temperature).

# Views
######################################################################
#       name       incl/excl    subtree                          mask
view    system     included     SNMPv2-MIB::system
view    system     included     IF-MIB::interfaces
view    system     included     IP-MIB::ipAddrTable
view    system     included     IPV6-MIB::ipv6MIB
view    system     included     HOST-RESOURCES-MIB::hrSystem
view    system     included     HOST-RESOURCES-MIB::hrStorage
view    system     included     HOST-RESOURCES-MIB::hrProcessorTable
view    system     included     UCD-SNMP-MIB::memory
view    system     included     UCD-SNMP-MIB::laTable
view    system     included     UCD-SNMP-MIB::systemStats
view    system     included     .1.3.6.1.4.1.8072.999

AgentX (RFC 2741) is a protocol allowing external programs to extend the SNMP agent with their own management information, communication is done through the use of a UNIX socket. A daemon that speaks it registers its own subtree with the agent at run time, and the agent then answers for it; the subtree has to be added to a view before it is visible to a client.

If the agentx group is not present on the system, another appropriate group can be used (wheel, …). Or the group can be created using a free id number, here we use the arbitrary value of 1025:

Adding agentx group
pw groupadd agentx -g Unix group
# AgentX
######################################################################
master       agentx
agentXPerms  0770 0755 root agentx
agentXSocket /var/agentx/master

It is also possible to extend net-snmp with a perl script thank to the embedded interpreter. Adding CPU temperature and frequency is done through this script, under the Net-SNMP enterprise branch (NET-SNMP-MIB::netSnmp, ie: .1.3.6.1.4.1.8072). The exact subtree it registers, .1.3.6.1.4.1.8072.999, is the last line of the view above: an extension the view does not name answers nobody, however correctly it registers itself.

# Extending
######################################################################
perl do "/usr/local/etc/snmp/health.pl"

snmpd.local.conf

syslocation and syscontact entries need to be tailored:

# Server administrative information
syslocation Server location
syscontact Administrator email

# Ignore all disks (avoiding potential timeout)
ignoredisk /dev/*

During disk device analyses, the snmpd daemon can get stuck until a timeout is reached. To avoid this situation the ignoredisk directive is used.

Extending

Temperature

Extending is done under the netSnmp MIB, the Net-SNMP enterprise branch .1.3.6.1.4.1.8072 — not under UCD-SNMP-MIB, which is a different arc (.1.3.6.1.4.1.2021).

health.pl
#!/usr/bin/perl

use BSD::Sysctl 'sysctl';
use NetSNMP::OID; 
use NetSNMP::ASN;
use NetSNMP::agent (':all'); 

my $rootOID  =  '.1.3.6.1.4.1.8072.999'; # netSnmp.999
my $cpu_freq = new NetSNMP::OID($rootOID . ".1.0");
my $cpu_temp = new NetSNMP::OID($rootOID . ".2.0");

sub myhandler {
    my  ($handler, $registration_info, $request_info, $requests) = @_;

    for (my $request = $requests; $request; $request = $request->next()) {
	my $oid  = $request->getOID();
	my $mode = $request_info->getMode();
	if      ($mode == MODE_GET) {
	    if      ($oid == $cpu_freq) {
		$request->setValue(ASN_GAUGE, sysctl('dev.cpu.0.freq'));
	    } elsif ($oid == $cpu_temp) {
		my $temp = sysctl('dev.cpu.0.temperature') / 100;
                $request->setValue(ASN_OCTET_STR, "$temp");
	    }
	} elsif ($mode == MODE_GETNEXT) {
	    if      ($oid < $cpu_freq) {
		$request->setOID($cpu_freq);
		$request->setValue(ASN_GAUGE, sysctl('dev.cpu.0.freq'));
	    } elsif ($oid < $cpu_temp) {
		my $temp = sysctl('dev.cpu.0.temperature') / 100;
		$request->setOID($cpu_temp);
                $request->setValue(ASN_OCTET_STR, "$temp");
	    }
	}
    }
}

my $regoid = new NetSNMP::OID($rootOID); 
my $agent  = new NetSNMP::agent();   # Embedded: attaches to the running snmpd
$agent->register("health", $regoid, \&myhandler);

Startup

To allow automatic startup, the following line is added to the /etc/rc.conf file:

snmpd_enable="YES"

MIBs

Information can be accessed by using a name instead of its numerical representation (OID). For that purpose a MIB definition must be installed.

If the MIB is not already present, it must be added and made available:

  1. Adding (globally or locally):

    Adding to MIBs repository
    cp MIB file /usr/local/share/snmp/mibs/               # Adding globally
    cp MIB file $HOME/.snmp/mibs/                         # Adding locally
    
  2. Making available (one time usage, globally, or locally):

    Notify of new MIBs to use
    export MIBS=+Module name                                     # One time usage
    echo "mibs +Module name" >> /usr/local/etc/snmp/snmpd.conf   # Global usage
    echo "mibs +Module name" >> $HOME/.snmp/snmp.conf            # Local usage