Monitoring virtual machine cost via the MyCloudSpace API
One of our clever partners wanted to monitor costs of virtual machines for a mutual client. They use LogicMonitor for this client already so why not bring cost data into this same solution and have a single place to monitor and alert on their environment.
We have a few API endpoints for cost monitoring but we decided to make this heaps easier for them and create something for them to consume and do a lot of the heavy lifting on our side as well as adding some functions I think may be useful to other clients wanting to do the same thing.
Here is what I decided to include;
Hourly burn rate
a change in this would indicate that something has changed to the specification, you could alert on any change or a % difference
Total if terminated now
In the portal you can always see how much an asset is costing you but this pricing is often based on the average monthly cost or it will show you previous months billing once that period has been finalised. Take a look at the below for example;
This shows the total for the machine if nothing changed on it at it was around for the entire month. In reality this could have been provisioned half way through the month in which case the bill for this machine would be roughly 50% of what is in the interface.
So then this figure will look at adds/moves/changes in the current period and run our billing calculations. This will increase each hour throughout the month so you can see an accumulation of cost across the month.
Detail
Some health, spec and backup information on the selected virtual machine which you could monitor and alert on specific items.
- Health state - this will move from green to yellow/red depending on cpu and memory usage
- Power state - is the machine on or off
- Has Snapshot - does the VM have an active snapshot on it
- Last backup date - either a date will be specified or it will be null (for no backup)
- Guest information - hostname, primary ip address etc
- Hosting site - Auckland or Christchurch
This looks like the following as an example;
"vmDetail": {
"id": 15569,
"name": "Test03",
"healthState": "green",
"powerState": "poweredOn",
"hasSnapshot": false,
"healthFetched": "2021-08-23T12:03:02.95+12:00",
"firstSeen": "2019-12-17T09:00:00+13:00",
"lastVirtualDisks": 1,
"lastCPU": 4,
"lastMemory": 6,
"hostingLocation": "Auckland",
"guestHostname": "WIN-R4D9OIPH2OA",
"guestNetworkAddress": "192.168.99.100",
"guestOS": "Microsoft Windows Server 2016 or later (64-bit)",
"lastBackup": "2021-08-20T17:04:44+12:00",
"acType": null,
"blType": null,
"sellCostDaily": 0.0,
"sellCostMonthly": 0.0,
"sellCostHourly": 0.0,
"margin": 0.0,
"annotation": ""
}
Billing Line Items
A detailed item level view of items that are/will be billed in the current period. This will show all items even ones which have been "terminated" because the specification has changed, for example where a disk has been extended or cpu cores changed.
For example here is a representation of two line items showing the change in cpu cores from 1 through to 4.
{
"id": 676397,
"reference": "vProcessor",
"description": "1 ea Virtual CPU Cores",
"quantity": 1.0000,
"unitsConsumed": 256.0,
"endedOn": "2021-08-11T16:00:00+12:00",
"itemCode": "vProcessor-1011",
"total": 10.2144000000,
"currentSpecMonthlyCost": 0.0
},
{
"id": 678976,
"reference": "vProcessor",
"description": "4 ea Virtual CPU Cores",
"quantity": 4.0000,
"unitsConsumed": 285.0,
"endedOn": null,
"itemCode": "vProcessor-1011",
"total": 45.4860000000,
"currentSpecMonthlyCost": 118.7424000000
}
You can see from the units consumed that the specification changed 256 hours into the month and this first item has a total value specified which would be the 256 hours worth of 1 virtual cpu core. It gives you the termination as a date - where is is set in the data it means it is no longer part of the current specification (it is historic)
Looking at the 4 virtual cpu core item you can see that the ended on is null meaning this item is current and part of the specification and also there is a currentSpecMonthlyCost item which shows you the cost of 4 cores if it was going to be around for the entire month. The other total field here represents what you would be billed for if the machine was terminated right now.
Hours in current month
Useful if you want to do anything with the hourly burn rate or see what percentage you are based on the billing data.
Bringing it all together
With the above in mind you can now hit the endpoint and do what you need it for. By the way if you need an API Key get in touch with servicedesk@vbridge.co.nz and we will sort you out.
I have put together VMCost.ps1 PowerShell script located in our MyCloudSpace API Examples repository.
This is used to showcase most of the data returned, you execute it like so;
.\VMCost.ps1 -vmName "Test03"
And you will get the costing output first
Following by the breakdown of the items, I have added some logic to these to show in red if the item has been terminated/is a historic record.
For those who just want to hack away at it without the script, the new endpoint is as follows
https://api.mycloudspace.co.nz/api/virtualresource/VMID/billingmonitor
VMID is the id of the virtual machine in MyCloudSpace. Here is an example of the full output;
{
"billingLineItems": [
{
"id": 674846,
"reference": "vCoreServices",
"description": "1 ea Core Services",
"quantity": 1.0000,
"unitsConsumed": 542.0,
"endedOn": null,
"itemCode": "vCoreServices-1011",
"total": 0.0000000000,
"currentSpecMonthlyCost": 0.0000000000
},
{
"id": 674847,
"reference": "vProcessor",
"description": "1 ea Virtual CPU Cores",
"quantity": 1.0000,
"unitsConsumed": 542.0,
"endedOn": null,
"itemCode": "vProcessor-1011",
"total": 21.6258000000,
"currentSpecMonthlyCost": 29.6856000000
},
{
"id": 674848,
"reference": "vMemoryReplica",
"description": "1 GB Memory (Powered off)",
"quantity": 1.0000,
"unitsConsumed": 542.0,
"endedOn": null,
"itemCode": "vMemoryReplica-1011",
"total": 4.5593040000,
"currentSpecMonthlyCost": 6.2585280000
},
{
"id": 674849,
"reference": "licWinSrvStdCorp",
"description": "1 proc Windows Server Standard",
"quantity": 1.0000,
"unitsConsumed": 542.0,
"endedOn": null,
"itemCode": "licWinSrvStdCorp-1011",
"total": 23.7997620000,
"currentSpecMonthlyCost": 32.6697840000
},
{
"id": 674850,
"reference": "6000C296-54b7-f670-13bf-e68788ce9956",
"description": "45 GB Tier 1 Storage",
"quantity": 45.0000,
"unitsConsumed": 542.0,
"endedOn": null,
"itemCode": "vStorageT1-1011",
"total": 6.6584700000,
"currentSpecMonthlyCost": 9.1400400000
},
{
"id": 674851,
"reference": "6000C29a-844a-e5b9-511d-7cb9a96269a3",
"description": "2 GB Tier 1 Storage",
"quantity": 2.0000,
"unitsConsumed": 542.0,
"endedOn": null,
"itemCode": "vStorageT1-1011",
"total": 0.2959320000,
"currentSpecMonthlyCost": 0.4062240000
},
{
"id": 674852,
"reference": "6000C297-f9cd-0f74-4826-0042a7082a01",
"description": "1 GB Tier 3 Storage",
"quantity": 1.0000,
"unitsConsumed": 542.0,
"endedOn": null,
"itemCode": "vStorageT3-1011",
"total": 0.0856360000,
"currentSpecMonthlyCost": 0.1175520000
},
{
"id": 674853,
"reference": "6000C292-e166-2739-9e69-8a953d9718ca",
"description": "100 GB Tier 1 Storage",
"quantity": 100.0000,
"unitsConsumed": 542.0,
"endedOn": null,
"itemCode": "vStorageT1-1011",
"total": 14.7966000000,
"currentSpecMonthlyCost": 20.3112000000
},
{
"id": 674854,
"reference": "6000C29a-56de-5690-b9bf-883c1e60b8de",
"description": "1 GB Tier 2 Storage",
"quantity": 1.0000,
"unitsConsumed": 542.0,
"endedOn": null,
"itemCode": "vStorageT2-1011",
"total": 0.1479660000,
"currentSpecMonthlyCost": 0.2031120000
},
{
"id": 674855,
"reference": "vBackupDisk",
"description": "149 GB Disk based backup (10 Rollbacks)",
"quantity": 149.0000,
"unitsConsumed": 542.0,
"endedOn": null,
"itemCode": "vBackupDisk-1011",
"total": 30.7562880000,
"currentSpecMonthlyCost": 35.4515760000
}
],
"totalIfTerminatedNow": 102.7257580000,
"currentSpecMonthlyCost": 134.2436160000,
"burnRateHourly": 0.1804349677419354838709677419,
"periodEnd": "2021-08-23T14:00:00+12:00",
"hoursInMonth": 744.0,
"vmDetail": {
"id": 0000000,
"name": "MyServer01",
"healthState": "green",
"powerState": "poweredOff",
"hasSnapshot": false,
"healthFetched": "2021-08-23T13:17:04.79+12:00",
"firstSeen": "2018-09-12T08:49:41.757+12:00",
"lastVirtualDisks": 5,
"lastCPU": 1,
"lastMemory": 1,
"hostingLocation": "Christchurch",
"guestHostname": "WIN-2A4JPC0ITEJ",
"guestNetworkAddress": "",
"guestOS": "Microsoft Windows Server 2016 or later (64-bit)",
"lastBackup": "2021-08-20T17:04:44+12:00",
"acType": null,
"blType": null,
"sellCostDaily": 0.0,
"sellCostMonthly": 0.0,
"sellCostHourly": 0.0,
"margin": 0.0,
"annotation": "Adipisci. Veritatis explicabo or aliqua consectetur yet anim. Sit beatae dicta or laboris. Officia consequat so consequat si dolores."
}
}
With that in mind, go forth and create! I would love to hear what you do with this data.