How to display LeaseWeb data traffic using command-line

LeaseWeb provides an API that can be used to request information about servers and perform some maintanance tasks like update reverse DNS entries, null route IP addresses, reboot specific server or launch rescue/reinstallation process. I will show you how to request and display identifiable information about servers in each location including data traffic details.

Requirements

I will use Python3 and nothing more than basic http.client, json and datetime modules.

LeaseWeb API is disabled by default, enable it using Customer Portal then add your IP address to the IP Whitelist for security reasons.

Python script

Use the following script to display and print information gathered using LeaseWeb API.

Use api_keys variable to define each location and assigned API key.

#!/usr/bin/python3
# use api keys to iterate over servers in each location
# and display identifiable information and data traffic details
# https://blog.sleeplessbeastie.eu/2017/11/20/how-to-display-leaseweb-data-traffic-using-command-line/

# import http.client
import http.client

# import json
import json

# import datetime
import datetime


# current datetime
now = datetime.datetime.now()

# define api keys
api_keys={"NL":"8517f379-aa60-5c1e-53ef-55203cab8c81d", "DE":"c406d92f-3603-f477-6086-7e3a1953ba2dc", "US":"16986a34-4148-7141-142f-b9c998f8ca878", "SG": "a12da181-029e-7a5f-cf8e-c3c70ea717db1"}

for key in api_keys:
  try:
    # get servers in location
    https_connection = http.client.HTTPSConnection("api.leaseweb.com")
    https_headers = { 'x-lsw-auth': api_keys[key] }
    https_connection.request("GET", "/v1/bareMetals", headers=https_headers)
    https_response = https_connection.getresponse()
    https_response_data = https_response.read()
    json_servers = json.loads(https_response_data.decode("utf-8"))
    if not 'bareMetals' in json_servers:
      print('LeaseWeb', key, 'returned no data')
      continue
  except:
    # skip location if there is a problem
    continue

  print('LeaseWeb', key)
  for bareMetal in json_servers['bareMetals']:
    try:
      # get datatraffic for server
      # notice that you need to count datatraffic from the last day of previous month to the current day (to get the same results as found in customer portal)
      https_connection.request('GET', '/v1/bareMetals/' + bareMetal['bareMetal']['bareMetalId'] + '/networkUsage/datatraffic?dateFrom=00-' + str(now.month) + '-' + str(now.year) + '&dateTo=' + str(now.day) + '-' + str(now.month) + '-' + str(now.year), headers=https_headers)
      https_response = https_connection.getresponse()
      https_response_data = https_response.read()
      json_datatraffic = json.loads(https_response_data.decode("utf-8"))
    except:
      # skip server if there is a problem
      continue

    try:
      # get IP addresses for server
      https_connection.request('GET', '/v1/bareMetals/' + bareMetal['bareMetal']['bareMetalId'] + '/ips', headers=https_headers)
      https_response = https_connection.getresponse()
      https_response_data = https_response.read()
      json_ips= json.loads(https_response_data.decode("utf-8"))
      if not 'ips' in json_ips:
        # skip datapack
        continue
    except:
      continue

    # print results
    print(' *', bareMetal['bareMetal']['serverName'])
    print('   IP addresses: ', end='')
    for index, json_ip in enumerate(json_ips["ips"]):
      if index > 0: print(', ', end='')
      print(json_ip['ip']['ip'], end='')
    print()
    print('   Interval from', json_datatraffic['dataTraffic']['interval']['from'], 'to', json_datatraffic['dataTraffic']['interval']['to'], end='')
    if json_datatraffic['dataTraffic']['monthlyThreshold'] != "0 GB":
      print(' with monthly threshold ', json_datatraffic['dataTraffic']['monthlyThreshold'], end='')
    if json_datatraffic['dataTraffic']['overusage'] != "0 B":
      print(' [', json_datatraffic['dataTraffic']['overusage'], '] ', end='')
    print()
    print('   Used ', json_datatraffic['dataTraffic']['measurement']['total'])
    interface_index=1
    for json_interface_datatraffic in json_datatraffic['dataTraffic']['measurement']:
      try:
        print('    - Interface ', str(interface_index), ': total', json_datatraffic['dataTraffic']['measurement'][json_interface_datatraffic]['total'], end=' = ')
        print(json_datatraffic['dataTraffic']['measurement'][json_interface_datatraffic]['in'], 'in', end=' + ')
        print(json_datatraffic['dataTraffic']['measurement'][json_interface_datatraffic]['out'], 'out')
        if "node" in json_interface_datatraffic:
          interface_index=interface_index+1
      except:
        continue
  https_connection.close()

Sample usage

$ lw_check_datatraffic.py
LeaseWeb NL
 * POGC001
   IP addresses: 203.0.113.101, 203.0.113.102
   Interval from 30-09-2017 to 07-10-2017 with monthly threshold  20000 GB
   Used  1.9 TB
    - Interface  1 : total 1.9 TB = 174.58 GB in + 1.73 TB out
 * POGC002
   IP addresses: 203.0.113.199, 203.0.113.231, 203.0.113.244
   Interval from 30-09-2017 to 07-10-2017 with monthly threshold  20000 GB
   Used  1.92 TB
    - Interface  1 : total 1.92 TB = 193.24 GB in + 1.73 TB out
LeaseWeb DE
 * JQZA001
   IP addresses: 203.0.113.173, 203.0.113.175
   Interval from 30-09-2017 to 07-10-2017
   Used  59.81 GB
    - Interface  1 : total 59.72 GB = 10.27 GB in + 49.44 GB out
    - Interface  2 : total 98.16 MB = 86.27 MB in + 11.9 MB out
 * JQZA002
   IP addresses: 203.0.113.158, 203.0.113.169
   Interval from 30-09-2017 to 07-10-2017
   Used  44.99 GB
    - Interface  1 : total 44.89 GB = 19.1 GB in + 25.79 GB out
    - Interface  2 : total 97.03 MB = 85.59 MB in + 11.45 MB out
 * JQZA003
   IP addresses: 203.0.113.155, 203.0.113.170
   Interval from 30-09-2017 to 07-10-2017
   Used  0 B
    - Interface  1 : total 0 B = 0 B in + 0 B out
    - Interface  2 : total 0 B = 0 B in + 0 B out
LeaseWeb US
 * NXZL001
   IP addresses: 203.0.113.143
   Interval from 30-09-2017 to 07-10-2017 with monthly threshold  80000 GB
   Used  280.29 GB
    - Interface  1 : total 280.29 GB = 46.27 GB in + 234.02 GB out
 * NXZL002
   IP addresses: 203.0.113.137, 203.0.113.142
   Interval from 30-09-2017 to 07-10-2017
   Used  62.06 GB
    - Interface  1 : total 61.85 GB = 21.96 GB in + 39.89 GB out
    - Interface  2 : total 208.85 MB = 208.85 MB in + 0 B out
 * NXZL005
   IP addresses: 203.0.113.134, 203.0.113.146
   Interval from 30-09-2017 to 07-10-2017
   Used  60.06 GB
    - Interface  1 : total 59.85 GB = 19.67 GB in + 40.18 GB out
    - Interface  2 : total 209.73 MB = 209.63 MB in + 103.84 KB out
LeaseWeb SG
 * MCDU001
   IP addresses: 203.0.113.103, 203.0.113.105, 203.0.113.109
   Interval from 30-09-2017 to 07-10-2017
   Used  64.46 GB
    - Interface  1 : total 64.25 GB = 21.56 GB in + 42.69 GB out
    - Interface  2 : total 212.18 MB = 212.07 MB in + 104.3 KB out
 * MCDU003
   IP addresses: 203.0.113.112
   Interval from 30-09-2017 to 07-10-2017 with monthly threshold  200000 GB
   Used  1.04 TB
    - Interface  1 : total 1.04 TB = 65.79 GB in + 975.59 GB out
 * MCDU004
   IP addresses: 203.0.113.120
   Interval from 30-09-2017 to 07-10-2017
   Used  695.25 GB
    - Interface  1 : total 695.25 GB = 133.71 GB in + 561.54 GB out

You can display more data, but it already shows enough information to be useful near the end of the month.

You are not tied to the current datatraffic usage, as you can define date period in the past to view historical data. You can even provide date period in the future, but this is just a curiosity and I will not pursue this topic further at this moment.

Additional information

Milosz Galazka's Picture

About Milosz Galazka

Milosz is a Linux Foundation Certified Engineer working for a successful Polish company as a system administrator and a long time supporter of Free Software Foundation and Debian operating system. He is also open for new opportunities and challenges.

Gdansk, Poland https://sleeplessbeastie.eu