Munin plugin: ESXi CPU/memory usage

Munin plugin: ESXi CPU/memory usage

What is Munin?

Munin is a networked resource monitoring tool that can help analyze resource trends and “what just happened to kill our performance?” problems. It is designed to be very plug and play. A default installation provides a lot of graphs with almost no work.

Munin can be installed on any Linux system (and there is a basic Windows version) and works with the plain old server-agent idea. The reason behind this is that you can create custom scripts which will be converted into graphs. Munin is written in Perl and plugins are easy to write, you can use any language for this as long as the output is data which the Munin master can understand. “Porting” to or from any Unix platform is quite easy if you have some Perl/shell/sysadmin experience. Currently there are plugins for Linux, FreeBSD, NetBSD, Solaris, AIX and of course cross-platform plugins.

How does it work?

The main Munin server will ask the agents for data and then convert this into RRD files which generates graphs. These are then displayed using a basic webpage. This page will contain a list of hosts you have in your Munin config and each of theses hosts will have the service graphs listed which you monitor. There are plenty of guides out there on how to install Munin so I won’t go into details on this.

What do I need?

To use this plugin you will need the following:

  • 1 Munin master server (munin and munin-node service installed)
  • VMware vSphere SDK for Perl installed on this server
  • 1 or more ESXi hosts
  • This script

What does it do?

This plugin will make a connection with an ESXi host and gather 2 things:

  • CPU usage (percentage)
  • Memory usage (percentage)

How to install this plugin?

To install this plugin you will need to do the following steps.

cd /usr/share/munin/plugins
wget https://raw.github.com/nielsengelen/vmware-munin/master/esxi_cpu_mem_
cd /etc/munin/plugins
ln -s /usr/share/munin/plugins/esxi_cpu_mem_ esxi_cpu_mem_<FQDN>

In the above replace with either the hostname or IP from the ESXi you want to monitor. Example:

ln -s /usr/share/munin/plugins/esxi_cpu_mem_ esxi_cpu_mem_192.168.1.150

Next we will add the username and password for the ESXi in the munin configuration which can be found under: /etc/munin/plugin-conf.dmunin-node. Add the following code.

[esxi_cpu_mem_<FQDN>]
env.username <ESXi USER>
env.password <ESXi PASS>

In the above replace with either the hostname or IP from the ESXi you want to monitor as you have created the symlink file, next add the username and password. Example:

[esxi_cpu_mem_192.168.1.150]
env.username root
env.password vmware

After this restart munin-node.

/etc/init.d/munin-node restart
service munin-node restart

The code

The code can be also found on GitHub.

#!/usr/bin/perl -w
# ESXi host CPU/memory plugin for Munin by Niels Engelen
# https://www.foonet.be

use strict;
use warnings;
use VMware::VIRuntime;
use VMware::VILib;

$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0;

my $esxihost = basename($0);
$esxihost =~ s/esxi\_cpu\_mem\_//;
my $username = Opts::set_option ('username', $ENV{'username'});
my $password = Opts::set_option ('password', $ENV{'password'});
my $url = Opts::set_option ('url', "https://$esxihost/sdk/webService");
my ($host_info);

if ( exists $ARGV[0] and $ARGV[0] eq "config" ) {
  print "graph_title ESXi CPU/Memory ".$esxihost." \n";
  print "graph_args --base 1000 -r --lower-limit 0 --upper-limit 100 \n";
  print "graph_vlabel % \n";
  print "graph_category ESXi CPU/Memory\n";
  print "graph_scale no\n";
  print "mem.label MEM used\n";
  print "mem.draw AREA\n";
  print "cpu.label CPU used\n";
  exit 0;
} else {
  Opts::parse();
  Opts::validate();
  Util::connect();
  $host_info = Vim::find_entity_view(view_type => 'HostSystem', properties => ['summary.hardware', 'summary.quickStats'], filter => {'summary.runtime.connectionState' => 'connected'});
  &get_host_mem_info($host_info);
  &get_host_cpu_info($host_info);
  Util::disconnect();
  exit 0;
}
 
sub get_host_mem_info {
      	if ($host_info) {
		my $percentMemoryUsed = ($host_info->{'summary.quickStats'}->overallMemoryUsage * 1024 * 1024 / $host_info->{'summary.hardware'}->memorySize) * 100;
		printf "mem.value %.2f\n",$percentMemoryUsed;
	}
}
 
sub get_host_cpu_info {
	if ($host_info) {
		my $percentCpuUsed = ($host_info->{'summary.quickStats'}->overallCpuUsage / ($host_info->{'summary.hardware'}->cpuMhz * $host_info->{'summary.hardware'}->numCpuCores)) * 100;
		printf "cpu.value %.2f\n",$percentCpuUsed;
    	}
}

The result

Munin ESXi CPU Memory

Niels Engelen on GithubNiels Engelen on Twitter
Niels Engelen
Working as a Principal Analyst in Product Management for Veeam Software with an interest in anything virtual and cloud with a strong focus on AWS, Azure and Microsoft 365. He is also a VMware Certified Professional, a Veeam Certified Architect and gained the VMware vExpert award (2012-2022).

7 thoughts on “Munin plugin: ESXi CPU/memory usage

Comments are closed.

Comments are closed.