Send vCenter statistics to Graphite/Grafana with PowerShell

In this article I’ll walk you through setting up VMPerf-To-Graphite PowerShell script written by Matthias and available on GitHub. This will provide the ability to graph metrics such as CPU memory, IOPS, read latency, and write latency on a per-VM basis. It’s extremely useful information that will provide insight for troubleshooting resource contention. And the best part, it’s Open Source.

If you don’t already have Grafana and Graphite, see the article Installing and configuring Grafana on Ubuntu.

Prerequisites

There are a few requirements before you will be able to get started.

  • A working Graphite and Grafana install. See the article Installing and configuring Grafana on Ubuntu for more information on getting a Grafana and Graphite server up and running.
  • PowerShell 4.0 or higher. You can check the PowerShell version by running the following command:
    $PSVersionTable.PSVersion
  • VMware PowerCLI.
  • A server or workstation which is always available in which you will use to schedule the PowerShell script to run. This system must always be avaialble. Otherwise there will be time gaps in your metrics.
  • A service account which has read access to vCenter Statistics.

vCenter configuration

There’s a small change we’ll need to make to vCenter logging in order to make the most of the script.

  1. Login to the vCenter Web Client since the vSphere Infrastructure Client (VIClient) does not contain the appropriate settings for altering the statistic logging details.
  2. Select vCenter > vCenter Servers. Then select your vCenter Server name.
  3. Select Manage. Click Edit… beside vCenter Server Setting.
  4. Select Statistics.
  5. Set the 5 minute interval to “Level 2”.
  6. Click Ok.

Graphite configuration to keep more data

This is optional and is not required to collect metrics. By default, Graphite will keep 1 minute of data for 1 day. After 24 hours the data is purged. This may be sufficient for some environments but for others it may defeat the purpose of collecting the metrics. This is easily solved by editing Graphite’s storage-schemas.conf file.

The following steps assume you’re running Ubuntu 14.04 and you had followed the post Installing and configuring Grafana on Ubuntu to install Graphite and Grafana.

  1. Login or SSH to the host running Graphite.
  2. Edit the storage-schemas.conf using nano.
    sudo nano /etc/carbon/storage-schemas.conf
  3. Add the following to the storage-schemas.conf file. You must add the following section above [default_1m_for_1day] section as the rules are matched in order.
    [vmperf]
    pattern = ^vmperf\.
    retentions = 60s:1d,5m:7d,15m:35d
  4. Save the file.

Note: The above changes to the storage-schemas.conf file must be completed before collecting statistics using the VMPerf-To-Graphite.ps1 script. Otherwise the metrics will continue to use the [default_1min_for_1day] retention period. The easiest way to resolve this issue is to purge the existing statistics.

sudo rm -rf /var/lib/graphite/whisper/vmperf

Obtaining and configuring the script

The script is available on GutHub. If you don’t already have an account, create one and then follow the project. The VMPerf-To-Graphite script is something you will definitely want to follow once you see just how powerful vCenter Metrics in Grafana can be to your organisation.

  1. Download the latest version of the VMPerf-To-Graphite.ps1 script from GitHub. The easiest way to obtain the script is to visit the GitHib site by clicking here, then select Clone or download followed by Download ZIP. This will download the latest branch of the project in a ZIP file.
  2. Extract the ZIP to C:\VMPerf-To-Graphite on the server or workstation which will be used to schedule the script.
  3. Update the permissions of the directory, C:\VMPerf-To-Graphite and its contents, to include whichever service account you’ve chosen. Alternatively the service account could be a local administrator.
  4. Edit the file VMPerf-To-Graphite.ps1 and configure your vCenter Server, credentials, and Graphite variables. It’s worth noting that setting these values is not required as you can specify them in the script’s arguments.
  5. Try running the script using the default variables defined in the script.
    .\VMPerf-To-Graphite.ps1 -Verbose
  6. If all goes well the script should run without any errors.

Schedule the script to run automatically

Schedule the script to run automatically using the Windows Task Scheduler. There are many ways to schedule the script as outlined on the GitHub project page but I prefer the following method.

  1. Create the file C:\VMPerf\VMPerf.bat and copy the example contents below. Make sure to set the vCenter server, account credentials, and URL of the Graphite server. This command will gather statistics for all Virtual Machines on the vCenter Server and send the metrics to Graphite. It will also note the poll time so the next time the script runs it will log only from the last time stamp.
    @echo off
    REM =============================================================================================================
    REM https://github.com/mothe-at/VMPerf-To-Graphite-PowerShell-Script
    REM =============================================================================================================
    cd /D C:\VMPerf
    powershell.exe .\VMPerf-To-Graphite.ps1 -server vcenter.domain.com -user domain\user -password S0m3P@$$w0rd -Graphiteserver graphite.domain.com:2003 -Iterations 1 -Group Default -EventLogLevel Warning -FromLastPoll VMPerf_Default.xml -Verbose >> VMPerf_Default.log 2>&1
    
  2. Launch the Windows Task Scheduler.
  3. Select Create a new task…
  4. Set the task to run as an account that has read access to vCenter (and access to the C:\VMPerf directory and content). Select the option to Run the task if the user is logged in or not. Select the option to Run with highest privileges.
  5. From the Triggers tab, add a new Daily trigger. Select to Repeat task every 5 minutes indefinitely. You may also want to implement some error recovery steps by selecting to Stop the task if it runs longer than 30 minutes.
  6. From the Actions tab, add a new action to Start a program. Browse for the C:\VMPerf\VMPerf.bat file.
  7. Click Ok and then Start the task. It should run forever.

Build a dashboard

Matthias included a JSON file within the GitHub project files. You can use the JSON file to import a dashboard into Grafana. I wasn’t successful with importing the dashboard. What I ended up doing was creating a new dashboard and then I recreated each table and graph by referencing the JSON file. If you want to import the already created dashboard, I suggest making sure that the Graphite data source in Grafana is set to default.

Note: If you attempt importing the JSON file, make sure you’ve set the default data source in Grafana to Graphite. Otherwise you will be forced to edit the data source for each table and graph individually.

Conclusion

A huge thank you to Matthias for building the VMPerf-To-Graphite.ps1 PowerShell script. The script is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

View Comments