08 April 2019

MRTG on Raspberry Pi 3B+ with Ubuntu Mate 18.04

I have installed and configure MRTG for system monitoring on a Raspberry Pi 3B+ running Ubuntu Mate 18.04.

I have also tested a similar setup (without CPU temperature) on a PC running Ubuntu 18.10.

1. Install SNMP Daemon for Organizing and Collecting Device Information

Install the package.
  $ sudo apt install snmpd

Backup existing /etc/snmpd.conf.
  $ sudo mv /etc/snmp/snmpd.conf /etc/snmp/snmpd.conf.ORIG

Create /etc/snmpd.conf.
  $ sudo vi /etc/snmp/snmpd.conf
  agentAddress    127.0.0.1:161
  rocommunity     public
(Yes, only these two lines.)

Restart snmpd.
  $ sudo systemctl restart snmpd

2. Install SNMP Utilities

Install the package.
  $ sudo apt install snmp

Check whether the setup works by trying to get the free CPU percentage.
  $ snmpget -v 1 -c public localhost .1.3.6.1.4.1.2021.11.11.0
  iso.3.6.1.4.1.2021.11.11.0 = INTEGER: 97

There is no well known OID (Object Identifier) for CPU temperature, so we are going to write a custom script and feed the value to SNMP.
  $ vi ~/snmp-cpu-temp.sh
#!/bin/bash
if [ "$1" = "-g" ]
then
    echo .1.3.6.1.2.1.25.1.8
    echo gauge
    cat /sys/class/thermal/thermal_zone0/temp
    # The above is for Raspberry Pi 3B+. For Ubuntu PC,
    # I don't know a portable way applicable for all.
    # Tell me if you know one.
    # This ugly hack works for mine:
    #sensors -u 2>/dev/null | sed -n '4p' | sed -e 's/temp1_input: \(.*\)/scale=0; 1000 * \1 \/ 1/g' | bc -l
fi
exit 0
  $ chmod +x ~/snmp-cpu-temp.sh
  $ ./snmp-cpu-temp.sh -g
  .1.3.6.1.2.1.25.1.8
  gauge
  49768

Tell SNMP how to handle this OID by appending a line to /etc/snmp/snmpd.conf.
  $ sudo vi /etc/snmp/snmpd.conf
(append)
  pass .1.3.6.1.2.1.25.1.8 /bin/sh /home/username/snmp-cpu-temp.sh
  $ sudo systemctl snmpd restart
(Change username above to reflect yours.)

Check if it works.
  $ snmpget -v 1 -c public localhost .1.3.6.1.2.1.25.1.8
  iso.3.6.1.2.1.25.1.8 = Gauge32: 41856

3. Install MRTG

Install the package.
  $ sudo apt install mrtg

Backup existing /etc/mrtg.cfg.
  $ sudo mv /etc/mrtg.cfg /etc/mrtg.cfg.ORIG

Create /etc/mrtg.cfg. I am more interested in monitor the system health, including CPU load, memory usage, and CPU temperature.
  $ sudo vi /etc/mrtg.cfg
WorkDir: /var/www/mrtg
EnableIPv6: no 

Title[cpuload]: CPU Load
Target[cpuload]: 100 - .1.3.6.1.4.1.2021.11.11.0&.1.3.6.1.4.1.2021.11.11.0:public@localhost
Options[cpuload]: gauge, nopercent, growright, unknaszero, noo
MaxBytes[cpuload]: 100
YLegend[cpuload]: %
ShortLegend[cpuload]: %
LegendI[cpuload]: CPU
Legend1[cpuload]: CPU usage
PageTop[cpuload]: <H1>CPU Load</H1>

Title[memory]: Memory Usage
Target[memory]: .1.3.6.1.2.1.25.2.3.1.6.1&.1.3.6.1.2.1.25.2.3.1.6.3:public@localhost 
Options[memory]: gauge, nopercent, growright, unknaszero
Factor[memory]: 1024
kilo[memory]: 1024
kMG[memory]: ,,Mi,Gi,Ti,Pi,Ei,Zi,Yi
MaxBytes[memory]: 100000000000
YTicsFactor[memory]: 1024
YLegend[memory]: Bytes
ShortLegend[memory]: B 
LegendI[memory]: Physical 
LegendO[memory]: Virtual 
Legend1[memory]: Physical Memory
Legend2[memory]: Virtual Memory 
PageTop[memory]: <H1>Memory Usage</H1>

Title[cputemp]: CPU Temperature
Target[cputemp]: .1.3.6.1.2.1.25.1.7.0&.1.3.6.1.2.1.25.1.8:public@localhost 
Options[cputemp]: gauge, nopercent, growright, noi 
Factor[cputemp]: 0.001
MaxBytes[cputemp]: 100000
YTicsFactor[cputemp]: 0.001
YLegend[cputemp]: Celsius
ShortLegend[cputemp]: Celsius
LegendO[cputemp]: CPU temperature 
Legend2[cputemp]: CPU temperature in Celsius
PageTop[cputemp]: <H1>CPU Temperature</H1>

Create the corresponding web directory for MRTG. Here we use apache2, so install the apache2 package first if you haven't.
  $ sudo mkdir /var/www/mrtg
  $ sudo vi /etc/apache2/sites-available/mrtg.conf
Alias /mrtg "/var/www/mrtg/"
<Directory "/var/www/mrtg/">
  Options FollowSymLinks
  AllowOverride None
  Require all granted
</Directory>
  $ sudo a2ensite mrtg
  $ sudo systemctl restart snmpd

Use the indexmaker helper to create the main page index.html.
  $ sudo indexmaker /etc/mrtg.cfg --title='System Monitor' --columns=1 --output=/var/www/mrtg/index.html

It should work now! So surf http://address/mrtg/ and you should see something.
(Change address above to reflect yours.)



Notes:
  • The mrtg package installs /etc/cron.d/mrtg to run mrtg every 5 minutes.
  • The cfgmaker helper can create a draft /etc/mrtg.cfg based on what it detects, including network traffics. I don't have much luck with it, but you can try it if you want.
  • We can improve the MRTG setup further with RRDTool. But the CGI scripts I found that can work nicely with MRTG+RRDTool are outdated and do not run well with my setup.

No comments:

Post a Comment