api-v2 icon indicating copy to clipboard operation
api-v2 copied to clipboard

Droplet bandwidth usage feature

Open sajidali118 opened this issue 5 years ago • 10 comments

It would be nice if i can access the Droplet Bandwidth usage through api. currently this feature is not available on api to get bandwidth usage.

sajidali118 avatar Oct 24 '19 11:10 sajidali118

+1

Berndinox avatar Mar 01 '21 20:03 Berndinox

is this happening?

asad-awadia avatar Mar 10 '21 18:03 asad-awadia

+1

vleeuwenmenno avatar Mar 15 '21 15:03 vleeuwenmenno

+1

ph0b14 avatar Apr 25 '21 19:04 ph0b14

+1 👍

I see it's available through the dashboard UI (v1 API) at: https://cloud.digitalocean.com/api/v1/billing/user_settings

{
   "alert_dollars_amount" : 1000,
   "alert_enabled" : false,
   "balances" : { "current_balance" : "123.45", "current_usage" : "123.45", "ledger_balance" : "0.0" },
   "bandwidth_usage" : {
      "current" : {
         "as_of" : "2021-08-06T00:00:00.000Z",
         "billing_segment_id" : 158,
         "projected_quota_gb" : "1234.56",
         "quota_gb" : "123.45",
         "usage_gb" : "12.34"
      },
      "previous" : {
         "as_of" : "2021-07-31T23:59:59.000Z",
         "billing_segment_id" : 157,
         "quota_gb" : "1234.56",
         "usage_gb" : "123.45"
      }
   },
   "can_add_payment_methods" : true,
   "can_pay_now" : true,
   "current_invoice_id" : 123456789,
   "current_invoice_total" : 123.45,
   "max_paypal_amount" : 1000,
   "overdue_cc" : false,
   "overdue_paypal" : false,
   "promo_amount" : null,
   "promo_code" : null
}

Is there a way to programmatically access that? (OAuth perhaps, vs. $DO_API_TOKEN?)

Cheers! :-D

DabeDotCom avatar Aug 06 '21 14:08 DabeDotCom

I'd like to monitor the current bandwidth usage of our droplets, to automate alerts if we're going to go over for the month and let us spin up more instances early enough in the month to make a difference.

I tried using the Droplet bandwidth metrics to calculate the bandwidth used, but my result is always at least 10% off from what the Billing panel says. I'm not confident I'm even measuring the same thing the Billing panel is measuring, and we might find some future month is way more than 10% off.

Some notes from this:

  1. If I request 13 hours or less in the API call, each bandwidth sample covers 120 seconds
  2. If I request 14 hours in the API call, each bandwidth sample covers 126 seconds
  3. etc, etc
  4. If I request a full month in the API call, each bandwidth sample covers 613 seconds

I take the bits-per-second reported in the sample multiplied by the time period covered by the sample, and add them all up over the month. I made it loop over API calls for 13 hours each, trying to get the most accurate estimate.


I'd love to use https://cloud.digitalocean.com/api/v1/billing/user_settings, as it reports exactly the thing I want to monitor: are we on a trajectory to have a substantial overage charge? I can't stomach the idea of putting user credentials on the monitoring machine to run an OAuth flow, though. I really want to use a read-only personal access token or API key, but https://cloud.digitalocean.com/api/v1/billing/user_settings doesn't appear to accept Bearer authorizations.

DentonGentry avatar Jul 05 '22 02:07 DentonGentry

Fwiw, both vultr and linode provide apis for this

asad-awadia avatar Jul 05 '22 03:07 asad-awadia

+1 we need this

mehrangta avatar Nov 18 '22 15:11 mehrangta

I think I've got this. The droplet bandwidth metrics API gives speed samples for each timestamp in units of Mbits/sec.

To convert this to a gigabyte per period (e.g. a month; as specified by period_secs=end_timestamp - start_timestamp which are supplied to the API), first you have to calculate the average bandwidth usage over all the samples that the API provides (usually using a loop; See the last paragraph).

Having calculated the average bandwidth usage (over the specified period) we then have to use a formula similar to: period_bandwidth_usage_avg * period_secs / 1024 / 8 * to get outbound (or inbound; whatever was supplied to the API) data usage over the specified period.

Also the bandwidth usage samples array (which is an array of bandwidth usage samples with a timestamp as its first field and the actual recorded value as the second field) is buried deep inside the returned response from the API; Something like bandwidth_usage_data_points = response.json()["data"]["result"][0]["values"] This is the array which is to be iterated over and averaged.

OnceUponATimeInAmerica avatar May 11 '23 11:05 OnceUponATimeInAmerica