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.
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:
- 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.
There’s a small change we’ll need to make to vCenter logging in order to make the most of the script.
- Login to the vCenter Web Client since the vSphere Infrastructure Client (VIClient) does not contain the appropriate settings for altering the statistic logging details.
- Select vCenter > vCenter Servers. Then select your vCenter Server name.
- Select Manage. Click Edit… beside vCenter Server Setting.
- Select Statistics.
- Set the 5 minute interval to “Level 2”.
- 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.
- Login or SSH to the host running Graphite.
- Edit the storage-schemas.conf using nano.
sudo nano /etc/carbon/storage-schemas.conf
- 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
- 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.
- 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.
- Extract the ZIP to C:\VMPerf-To-Graphite on the server or workstation which will be used to schedule the script.
- 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.
- 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.
- Try running the script using the default variables defined in the script.
- 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.
- 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
- Launch the Windows Task Scheduler.
- Select Create a new task…
- 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.
- 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.
- From the Actions tab, add a new action to Start a program. Browse for the C:\VMPerf\VMPerf.bat file.
- 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.
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.