How to Monitor CPU and Memory on Ubiquiti Unifi Devices

This is a guest blog post by Patrik Nordlund, Infrastructure Manager at Retune AB. Retune AB is a Managed Service Provider based out of Stockholm, Sweden, and has been using LogicMonitor to help customers modernize monitoring for hybrid infrastructure. Patrik has been working in IT since 2003, handling everything from clients to servers to networking.

Retune AB manages a variety of Ubiquiti devices — wireless data communication products for enterprise and wireless broadband providers. Naturally, we wanted to bring these in under monitoring. However, Ubiquiti does not expose real-time CPU or memory metrics through SNMP in a way that we found reliable and these are some of the key values needed to verify the health of the device. We have had incidents where memory and CPU have spiked and stayed there and only after a reboot were the resources released. We use the alerts triggered in LogicMonitor so that we can take action as quickly as possible.

After a quick web search, we found that other Ubiquiti users had found some unofficial OIDs to get average usage at one minute, five minute, and fifteen-minute intervals for CPU, but these values did not work correctly on all our devices. Also, we were completely blind when trying to view memory. In the official UBNT-Unifi-MIB, there is no mention of CPU nor Memory.

Thanks to LogicMonitor’s extensibility, we were able to find an easy workaround by running a PowerShell script from the Collector. Here’s how we did it:

  • Install the POSH-module, posh-ssh, on the collector so SSH can be used in PowerShell
  • Connect to the access point
    • Authentication is done with an SSH-key.pair. The public key is uploaded to the AP (via Unifi controller) and the private key is stored in the Collector, protected with a strong passphrase
    • You can edit $keyFile in the script, or put your private key in your Collector’s installation directory at: LogicMonitor\Agent\bin\Local_Disk_On_Collector\privatekey_LM.key
    • Passphrase and username are added in LogicMonitor as properties unifi.sshuser and unifi.sshpassphrase.key so they are not exposed directly in the script
  • Use the native Linux commands to get metrics for CPU and Memory
  • Collected data is formatted to calculate percentage value and then returned to LogicMonitor

Script for CPU Usage

$username = "##unifi.sshuser##"
$passwd = "##unifi.sshpassphrase.key##"
$secpasswd = ConvertTo-SecureString $passwd -AsPlainText -Force
$device = "##system.ips##"
$keyFile = "Local_Disk_On_Collector\privatekey_LM.key"
$creds = New-Object System.Management.Automation.PSCredential ($username, $secpasswd)
# SSH-session to device
New-SSHSession -ComputerName $device -Credential $creds -Keyfile $keyFile -AcceptKey | Out-Null
# Get CPU stats
$cpuAll = Invoke-SSHCommand -index (Get-SSHSession -host $device).sessionid -Command "cat /proc/stat | grep '^cpu '"
# Drop session
Remove-SSHSession (Get-SSHSession -host $device) | Out-Null
# Calculate CPU usage
$cpuArray = $cpuAll.Output -split " +"
$cpuTotal = ([int]$cpuArray[1]) + ([int]$cpuArray[2]) + ([int]$cpuArray[3]) + ([int]$cpuArray[4]) + ([int]$cpuArray[5]) + ([int]$cpuArray[6]) + ([int]$cpuArray[7]) + ([int]$cpuArray[8]) + ([int]$cpuArray[9]) + ([int]$cpuArray[10])
$cpuIdle = ([int]$cpuArray[4])
$cpuUsage = ($cpuTotal-$cpuIdle)/$cpuTotal
$cpuUsedPercent = $cpuUsage*100
$cpuPercent = ([int]$cpuUsedPercent)
Write-Host "CPUUsage=${cpuPercent}"
Exit 0

Script for Memory Usage

$username = "##unifi.sshuser##"
$passwd = "##unifi.sshpassphrase.key##"
$secpasswd = ConvertTo-SecureString $passwd -AsPlainText -Force
$device = "##system.ips##"
$keyFile = "Local_Disk_On_Collector\privatekey_LM.key"
$creds = New-Object System.Management.Automation.PSCredential ($username, $secpasswd)
# SSH-session to device
New-SSHSession -ComputerName $device -Credential $creds -Keyfile $keyFile -AcceptKey | Out-Null
# Get Memory stats
$memAll = Invoke-SSHCommand -index (Get-SSHSession -host $device).sessionid -Command "free | grep 'Mem:'"
# Drop session
Remove-SSHSession (Get-SSHSession -host $device) | Out-Null
# Calculate memory usage
$memArray = $memAll.Output -split " +"
$memTotal = $memArray[1]
$memUsed = $memArray[2]
if ($memTotal -eq "Mem:"){
    $memTotal = $memArray[2]
    $memUsed = $memArray[3]
}
$memUsedPercent = [int](($memUsed/$memTotal)*100)
Write-Host "MemoryUsed=${memUsedPercent}"
Exit 0

Our team noticed there were slight differences in the output from the Linux commands, which means that you might have to tweak the scripts to suit your specific devices.

It is possible to get these metrics from the Unifi controller via the API. However, in this case, each device will generate a question to the controller causing an influx of data as well as a single point of failure for your network monitoring. Thanks to LogicMonitor, we have the capability to monitor what is important to our business and now confidence in the metrics we are receiving for Ubiquiti devices.

These modules have been published and are available via the LM Exchange, a repository for customers to exchange datasources, and can be found using the codes 6H7TE3 and J9NFZJ. If you’re interested in learning more about how LogicMonitor can help in your environment, sign up here for a free trial or demo.