落絮飞雁的个人网站
Use API to monitor IDCF data transfer usage
Use API to monitor IDCF data transfer usage

IDCF is a large enterprise which provides VPS service at an affordable price in Japan. It provides 3TB data transfer for free each month, Extra transfer fee should be paid after exceed the free plan. So it’s a wise choice to balance the data transfer from the different zone.

Firstly, let’s clarify this free plan: When you set up a VPS, there are three different regions available for selection(east JP1, east JP2, and west JP), each region has several zones which named by physical units, like Pascal, Henry, or Joule (all of them in the east JP1 region), all zone list here. After you choose the region you want. Each zone has 3,240GB (almost 3TB) data transfer (transfer in AND transfer out) per month. So we need to calculate all VPS from the same region, add inbound transfer and outbound transfer altogether.

You can enable a new zone in zone management panel. You can select zone by yourself when you launch a new Virtual Machine. One IDCF account can launch up to 20 VMs.

https://www.luoxufeiyan.com/wp-content/uploads/2018/05/IDCF-zone.png

Fortunately, IDCF provides enormous API with a detailed doc. Although it is written in Japanese but contains a large number of English words, So docs can be read without translation. There’s two API about the Internet Data Transfer: listUsageRecords and list_billing_detail. I used the later one in my project.

Firstly, you need to get your own API in the user panel, refer the official example to make a request.

https://www.luoxufeiyan.com/wp-content/uploads/2018/05/180512-apikey.png

My code(written in Python3):

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018.05.12 09:50
# @Author  : LXFY
# @Site    : http://www.luoxufeiyan.com/
# @File    : idcf.py
# @Software: VS code
# @Descriptiion: get IDCF data transfer usage

import base64
import hashlib
import hmac
import json
import time

import requests

def getTransferUsage():
    RequestMethod = "GET"
    Endpoint = "https://your.idcfcloud.com"
    RequestPath = "/api/v1/billings/" + time.strftime("%Y-%m", time.localtime()) #current month
    apiKey = ""
    apiSecret = ""
    QueryString = "format=json"

    # Expire time, default: 60 seconds
    Expiration = str(int(time.time()) + 60)

    message = RequestMethod + "\n" + RequestPath + "\n" + \
        apiKey + "\n" + Expiration + "\n" + QueryString
    signature = hmac.new(bytes(apiSecret, 'ascii'), bytes(
        message, 'ascii'), hashlib.sha256).digest()
    sign = base64.b64encode(signature)

    reqhead = {'X-IDCF-APIKEY': apiKey,
            'X-IDCF-Expires': Expiration,
            'X-IDCF-Signature': str(sign, 'utf-8')
            }
    uri = Endpoint + RequestPath + "?" + QueryString
    res = requests.get(uri, headers=reqhead)
    resultdata = res.json()
    # print(json.dumps(resultdata,sort_keys=True,indent=4,separators=(',', ': ')))
    return resultdata

Collect data from json result, and use it in your own project. In addition, IDCF also provide CLI and cloudstack-api. You also can get data via those tools.

 

REF:

原文标题:Use API to monitor IDCF data transfer usage|落絮飞雁的个人网站
授权协议:创作共用 署名-非商业性使用 2.5 中国大陆
除注明外,本站文章均为原创;转载时请保留上述链接。

发表评论

textsms
account_circle
email

落絮飞雁的个人网站

Use API to monitor IDCF data transfer usage
IDCF is a large enterprise which provides VPS service at an affordable price in Japan. It provides 3TB data transfer for free each month, Extra transfer fee should be paid af…
扫描二维码继续阅读
2018-05-12