[IDCF] A story about tinkering with CloudStack API when building a jmeter execution environment for load testing

I'm Teraoka, an infrastructure engineer.
For a certain project, I needed to build a JMeter execution environment on IDCF, and I
'd like to share some of the clever ways I used the CloudStack API during the setup process.

■What did you want to do in the first place?

when JMeter is launched.
I wanted to configure the server acting as the JMeter controller to automatically recognize the IP address of the server acting as the corresponding node

Yes, that's basically it.
JMeterremote testing, andhas a mechanism called
in the configuration file of the controller server,
by registering the servers (nodes) where remote JMeter is running
it becomes possible to generate a load greater than the number of registered servers.

However, the number of servers to register changes depending on the load generated, and
it's troublesome to change the configuration file each time, so
by identifying only the nodes that are currently running
when JMeter is launched on the controller side,
.

■ What specifically did you do?

To register a remote server in JMeter,
add the setting to the "remote_hosts" section of the "jmeter.properties" file.
Here's an example:

remote_hosts=10.31.X.XX,10.31.X.XX,10.31.X.XX

As described above, we will be adding IP addresses separated by commas, so
first we need to obtain the IP addresses of the remote servers all at once.
We used the CloudStack API to obtain these addresses.

The CloudStack API is written in Python, so it can be installed relatively easily using pip.
Installation instructions are available on the official IDCF website, so please refer to that.

Getting Started | IDCF Documentation

The API reference is also summarized on the official page, so let's take a look at it

API References | IDCF Documentation

Okay, let's get started.
Here's the command to get all the IP addresses of remote servers at once:

cloudstack-api listVirtualMachines --name "jmeter-node" --state "Running" | jq -r '.listvirtualmachinesresponse.virtualmachine[].nic[].ipaddress' | tr "\n" "," | sed -e "s/,$//g"

Here's a breakdown of what this command does:

①Get a list of virtual server information using CloudStack API

② Use the jq command to format the json data

③ Use the tr command to format the formatted JSON data into a "," separated format

①Get a list of virtual server information using CloudStack API

cloudstack-api listVirtualMachines --name "jmeter-node" --state "Running"

This part

This time, I used the "listVirtualMachines" method from among the many methods available in the "CloudStack API".
As the name suggests, this method retrieves a list of virtual server information.
The result of executing this method is returned in the following JSON format.

$ cloudstack-api listVirtualMachines { "listvirtualmachinesresponse": { "count": 1, "virtualmachine": [ { "nic": [ { "ipaddress": "XXX.XXX.XXX.XXX" } ] } ] } }

*Although many other items besides "ipaddress" are returned in the response,
 only the parts necessary for this article have been extracted.

So, all we need to do is extract the value of the "ipaddress" field from this JSON data!
When actually executing the command, we'll use "--name" to narrow it down to only the servers that are nodes,
and then use "--state" to further narrow it down to only those virtual servers whose status is Running.
This means we'll "exclude stopped ones and extract only the information of running nodes."

② Use the jq command to format the json data

jq -r '.listvirtualmachinesresponse.virtualmachine[].nic[].ipaddress'

This part

The CloudStack API response is returned as JSON data, so
you need to format the JSON data so that you can extract only the data you want.
In this case, it's the "ipaddress" field.
You tell the jq command which data to extract from the JSON in a hierarchical structure.

③ Use the tr command to format the formatted JSON data into a "," separated format

tr "\n" "," | sed -e "s/,$//g"

This part

When you format the json data in ②, it will be in the following format

10.31.X.XX 10.31.X.XX 10.31.X.XX

...It even adds line breaks, which is inconvenient.
This means we can't embed it in the Jmeter configuration file as is, so
we need to convert the line breaks to commas.
Also, this conversion adds an extra comma at the end, so we
'll use the sed command to remove that.

And then..

10.31.X.XX,10.31.X.XX,10.31.X.XX

Excellent! Now we can embed this directly into the configuration file.
All that's left is to edit the configuration file before starting JMeter.
Let's quickly create a startup script.

$ vi start-controller.sh #!/bin/sh NODE=$(cloudstack-api listVirtualMachines --name "jmeter-node" --state "Running" | jq -r '.listvirtualmachinesresponse.virtualmachine[].nic[].ipaddress' | tr "\n" "," | sed -e "s/,$//g") sed -i -e "s/^remote_hosts=.*/remote_hosts=${NODE}/" /usr/local/jmeter/bin/jmeter.properties /usr/local/jmeter/bin/jmeter & $ ./start-controller.sh

Yes, now JMeter will automatically recognize the node when you start it.
That's all, thank you.

If you found this article helpful,please give it a "Like"!
0
Loading...
0 votes, average: 0.00 / 10
990
X Facebook Hatena Bookmark pocket

The person who wrote this article

About the author

Yuki Teraoka

Joined Beyond in 2016, I am currently
in my sixth year as an infrastructure engineer and MSP. I handle troubleshooting during incidents and
also design and build infrastructure using public clouds such as AWS. Recently, I have been working
container infrastructure such as Docker and Kubernetes, and
with HashiCorp tools such as Terraform and Packer as part of building and automating
I also take on the role of an evangelist, speaking at external study groups and seminars.

・GitHub
https://github.com/nezumisannn

• Speaking Engagements
: https://github.com/nezumisannn/my-profile

• Presentation materials (SpeakerDeck)
https://speakerdeck.com/nezumisannn

・Certification:
AWS Certified Solutions Architect - Associate
Google Cloud Professional Cloud Architect