"net-snmp-mrtg-freebsd"

Published: Mon 07 September 2020

In content.

  • "blog-posts" tags:
  • "freebsd"
  • "freebsd-jails"
  • "mrtg"
  • "net-snmp"
  • "network"
  • "network-monitoring"

[FreeBSD] SNMP & MRTG

This article was written using FreeBSD 11.3-RELEASE. Assumption is made such that reader knows what is SNMP (Simple Network Management Protocol). At the end of the article, you should have working setup of SNMP & MRTG (Multi Router Traffic Graph) .

There are two packages available for SNMP in FreeBSD, which is bsnmpd and net-snmp. We are going to install the latter. In future article, I will write about bsnmpd. For now let’s focus on net-snmp.

In FreeBSD, there are two ways to install a package, via pkg tool or ports tree. For this article, we are going to use pkg with latest release. To configure that, create a directory under /usr/local/etc/pkg/ called repos. In that folder touch a empty file called FreeBSD.conf and add the following line into the file using your favorite method1.

FreeBSD: {
 url: "pkg+http://pkg.FreeBSD.org/${ABI}/latest"
}

Note: To change to https, install ca-root-nss package. To install, type pkg install ca-root-nss.

Once you have created FreeBSD.conf file with the content, type the following command in order:

pkg update -f pkg upgrade -f This will rebuild the packagesite, update and upgrade installed packages in the system.

Once you have done all above, now it’s time to cheer up for the interesting part, to install and configure net-snmp package.

To install, type pkg install net-snmp. Once it finishes installing, there will be on-screen message to make additional configuration. One of them is adding snmp related configuration. For now, put below content in /etc/rc.conf.

snmpd_enable="YES"
snmpd_flags="-a"
snmpd_conffile="/usr/local/share/snmp/snmpd.conf /etc/snmpd.conf"
snmptrapd_enable="YES"
snmptrapd_flags="-a -p /var/run/snmptrapd.pid"

Once you have done initial configuration, you need to configure net-snmp configuration. By issuing following command, you will answer a few questions related to snmp and it will create snmpd.conf file in current directory. Issue snmpconf -g basic_setup. Once you’re done answering the questions, copy the file from current directory to /usr/local/share/snmp/snmpd.conf and /etc/snmpd.conf. This is to confirm an entry we made earlier in /etc/rc.conf, under snmpd_conffile.

Now we are going to install two packages, apache24 and mrtg. Issue the command pkg install apache24 mrtg. Once you’re done, issue following commands in any order you prefer. Issue sysrc apache24_enable=yes and sysrc mrtg_daemon_enable="YES". What sysrc command will do is, it will add rc.conf options like the ones you have typed. If you’re unsure, you can always view the content of rc.conf.

This will be the 2nd part of awesomeness, as we will be creating mrtg.cfg configuration as well as modifying a bit of mrtg.cfg to include various graph options. Bear in mind at the the time of writing, graph related to Memory is not working. I will update as time passes by. Mean time, you could try following command, snmptranslate .1.3.6.1.2.1.1.3.0 to get the MIB. The MIB will be DISMAN-EVENT-MIB::sysUpTimeInstance. Now to see the value recorded by above MIB, you can run following command snmpget -v 2c -c root localhost DISMAN-EVENT-MIB::sysUpTimeInstance which will produce following output:

root@freebsd-s-1vcpu-2gb-nyc3-01:~ # snmpget -v 2c -c root localhost DISMAN-EVENT-MIB::sysUpTimeInstance
DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (41560597) 4 days, 19:26:45.97

Before we generate mrtg configuration, create a directory under /usr/local/www/apache24/data called mrtg.

Now we are ready to create our first iteration of mrtg.cfg configuration file. Why I am saying this is because as you modify your mrtg.cfg to add new excerpts, new index.html using indexmaker.

Following command will generate mrtg.cfg in /usr/local/etc/mrtg. Make sure that directory is owned, write-able by user mrtg and group mrtg.

cfgmaker --global 'WorkDir: /usr/local/www/apache24/data/mrtg' --global 'Options[_]: growright,unknaszero' --output /usr/local/etc/mrtg/mrtg.cfg root@127.0.0.1

If you’re unsure what is root@localhost in the context of snmp, it is access control setup for snmp which will define who is allowed to talk/send queries to your running snmpd agent. You can see the comments in snmpd.conf.

Now, make a backup of the file, and edit /usr/local/etc/mrtg/mrtg.cfg to include the following lines to the end of the file.

Target[cpu]: 1.3.6.1.4.1.2021.10.1.5.1&1.3.6.1.4.1.2021.10.1.5.2:root@localhost
MaxBytes[cpu]: 100
AbsMax[cpu]: 1000
Options[cpu]: gauge,nopercent,growright
Unscaled[cpu]: dwmy
Title[cpu]:  Cpu Load
XSize[cpu]: 400
#ShortLegend[cpu]: %
#XSize[cpu]: 380
#YSize[cpu]: 100
YLegend[cpu]: Percent Load
Legend1[cpu]: CPU Load
Legend2[cpu]: 5 min CPU Load
Legend3[cpu]: Maximal 5 Minute CPU Load
Legend4[cpu]:
LegendI[cpu]:  CPU Load
LegendO[cpu]:  5min CPU Load
PageTop[cpu]: <h1> CPU Load 5min</h1>
 <TABLE>
   <TR><TD>System:</TD>freebsd<TD></TD></TR>
   <TR><TD>Maintainer:</TD><TD>sysadmin</TD></TR>
   <TR><TD>Interface:</TD><TD>cpu usage</TD></TR>
   <TR><TD>CPU Usage (percent):</TD>
       <TD></TD></TR>
  </TABLE>

#####
PageTop[disk]: <h1> Disk Usage /usr/local/www/apache24/data </h1>
 <TABLE>
   <TR><TD>System:</TD>     <TD>freebsd</TD></TR>
   <TR><TD>Maintainer:</TD> <TD>sysadmin</TD></TR>
   <TR><TD>Disk Usage (percent):</TD>
       <TD></TD></TR>
  </TABLE>

Target[disk]: 1.3.6.1.4.1.2021.9.1.9.1&1.3.6.1.4.1.2021.9.1.9.1:root@localhost
MaxBytes[disk]: 100
Options[disk]: gauge,nopercent,growright
Unscaled[disk]: dwmy
XSize[disk]: 400
Title[disk]: Disk Usage /usr/local/www/apache24/data
ShortLegend[disk]: %
#XSize[disk]: 380
#YSize[disk]: 100
YLegend[disk]: Disk Usage
Legend1[disk]: Disk Usage
Legend2[disk]:
Legend3[disk]:
Legend4[disk]:
LegendI[disk]: &nbsp;DiskUsage
LegendO[disk]:

PageTop[disk]: <h1> Disk Usage /usr/local/www/apache24/data </h1>
 <TABLE>
   <TR><TD>System:</TD>     <TD>freebsd</TD></TR>
   <TR><TD>Maintainer:</TD> <TD>sysadmin</TD></TR>
   <TR><TD>Disk Usage (percent):</TD>
       <TD></TD></TR>
  </TABLE>  

Target[server.cpu]:ssCpuRawUser.0&ssCpuRawUser.0:root@localhost + ssCpuRawSystem.0&ssCpuRawSystem.0:root@localhost + ssCpuRawNice.0&ssCpuRawNice.0:root@localhost
Title[server.cpu]: Server CPU Load
PageTop[server.cpu]: <h1>CPU Load - System, User and Nice Processes</h1>
MaxBytes[server.cpu]: 100
ShortLegend[server.cpu]: %
YLegend[server.cpu]: CPU Utilization
Legend1[server.cpu]: Current CPU percentage load
LegendI[server.cpu]: Used
LegendO[server.cpu]:
Options[server.cpu]: growright,nopercent
Unscaled[server.cpu]: ymwd

Target[server.newconns]: tcpPassiveOpens.0&tcpActiveOpens.0:root@localhost
Title[server.newconns]: Newly Created TCP Connections
PageTop[server.newconns]: <h1>New TCP Connections</h1>
MaxBytes[server.newconns]: 10000000000
ShortLegend[server.newconns]: c/s
YLegend[server.newconns]: Conns / Min
LegendI[server.newconns]: In
LegendO[server.newconns]: Out
Legend1[server.newconns]: New inbound connections
Legend2[server.newconns]: New outbound connections
Options[server.newconns]: growright,nopercent,perminute

Target[server.estabcons]: tcpCurrEstab.0&tcpCurrEstab.0:root@localhost
Title[server.estabcons]: Currently Established TCP Connections
PageTop[server.estabcons]: <h1>Established TCP Connections</h1>
MaxBytes[server.estabcons]: 10000000000
ShortLegend[server.estabcons]: c/s
YLegend[server.estabcons]: Connections
LegendI[server.estabcons]: In
LegendO[server.estabcons]:
Legend1[server.estabcons]: Established connections
Legend2[server.estabcons]:
Options[server.estabcons]: growright,nopercent,gauge

Replace root@localhost with appropriate values you set for cfgmaker command earlier in the article.

Now, you need to generate the index.html for mrtg. Issue the command, indexmaker --output=/usr/local/www/apache24/data/mrtg/index.html /usr/local/etc/mrtg/mrtg.cfg. It will generate necessary files in /usr/local/www/apache24/data/mrtg. After this, you need to run the mrtg daemon to populate the graphs. In order to do that, we are going to add following in crontab. Issue crontab -e , enter following string:

*/1 * * * * /usr/bin/env LANG=C /usr/local/bin/mrtg /usr/local/etc/mrtg/mrtg.cfg --logging /var/log/mrtg.log

What it does is that it invokes /usr/local/bin/mrtg with path to mrtg.cfg and log in mrtg.log every 1 minute. You can choose desired interval to run the command.

That’s it. Now start apache24 service by issuing service apache24 start and then run the command from crontab manually once to populate your graphs. If you visit the your <SITE_IPADDR/FQDN>/mrtg/index.html, you should see some pretty graphs generated.

Additional Note:

  • As you add new snmp configs to mrtg.cfg, you need to re-generate index.html by issuing indexmaker.
  • create a file under /usr/local/bin/memstat.sh and paste following content. Make sure you have freecolor package installed2.
#!/usr/bin/env sh

/usr/local/bin/freecolor -m -o | /usr/bin/awk 'NR==2 {memUsed = $3 } NR==3{swapUsed = $3 } END {print swapUsed "\n" memUsed"\n0\n0"}'
  • Now, edit mrtg.cfg and add following lines to the end of the file.
Target[localmem]: `/usr/local/bin/memstat.sh`
Title[localmem]: Mem and Swap Usage
PageTop[localmem]: <h1>Memory and Swap Usage</h1>
MaxBytes[localmem]: 100000000000
ShortLegend[localmem]: B
YLegend[localmem]: Memory
LegendI[localmem]: Swap
LegendO[localmem]: Mem
Legend1[localmem]: Swap
Legend2[localmem]: Mem
Options[localmem]: gauge,growright,nopercent
kMG[localmem]: k,M,G,T,P,X
Colours[localmem]: RED#bb0000,BLUE#1000ff,GREEN#006600,VIOLET#ff00ff

Reference

  1. # Using MRTG for monitoring host CPU, memory, disk etc
  2. # Quick HOWTO : Ch23 : Advanced MRTG for Linux
  3. # MRTG : Get Memory Usage
  4. # FreeBSD: System Monitoring with MRTG
  5. # Sample mrtg.cfg from log.up-link.ro
  6. # Setting up MRTG on FreeBSD
  7. Tutorials - Net-SNMP Wiki
  8. # How to Install and Configure MRTG on FreeBSD
  9. # FreeBSD jails and net-snmp
  10. # How to Install and Configure MRTG on FreeBSD
  11. # Getting snmpwalk to talk to snmpd on FreeBSD](https://dan.langille.org/2014/10/22/getting-snmpwalk-to-talk-to-snmpd-on-freebsd/)
  12. # MRTG Configuration in Debian
  13. How to obtain a basic MRTG report page

Written with StackEdit.


  1. # Newer packages not able to be installed ↩︎
  2. # # Monitor your memory usage with MRTG ↩︎

social