费用

视图预览

1639623196405-93662279-89b2-4309-a346-098931434c63.png

安装部署

  • 说明:
    示例 Linux 版本为:CentOS Linux release 7.8.2003 (Core)
    通过一台服务器采集所有阿里云账单费用数据

前置条件

RAM 访问控制

  1. 登录 RAM 控制台 https://ram.console.aliyun.com/users
  2. 新建用户:人员管理 - 用户 - 创建用户
    1639623496834-3692d450-cc89-4364-aa6f-c41399a2dfa1.png
  3. 保存或下载 AccessKeyIDAccessKey Secret 的 CSV 文件 (配置文件会用到)
  4. 用户授权(账单权限)

1627893756593-a6f5114d-309e-419a-96b2-e3637f0028c7.png

配置实施

  1. 登录 Func,地址 http://ip:8088(默认 admin/admin)

1639115383741-ad518ea3-5206-4e62-a6a2-d14fdf1b8f4e.png

  1. 输入标题/描述信息

1639635589395-838955e1-b71d-4552-9713-d166c52430a0.png

  1. 编辑脚本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import sys, datetime
import time
import json
import urllib
import hmac
from hashlib import sha1
import base64
import random
import requests


# 请求中所需要的公共参数(就是调用 API 都需要用到的参数)
D = {
'BillingCycle':str(time.strftime("%Y-%m", time.gmtime())),
'Action':'QuerySettleBill',
# 'PageNum':'5',
'Format':'JSON',
'Version':'2017-12-14',
'AccessKeyId':'<AccessKeyId>',
'SignatureMethod':'HMAC-SHA1',
'MaxResults' : '300',
# 'NextToken':"", #?
'Timestamp':str(time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())),
'SignatureVersion':'1.0'
# 'SignatureNonce':str_seed
}
#当前时间
now_time = str(time.strftime("%Y-%m-%d", time.gmtime()))

#链接本地 Datakit
datakit = DFF.SRC('datakit')

# 使用 python 中的 urllib 库来进行编码
def percentEncode(str):
res = urllib.parse.quote(str.encode('utf8'), '')
res = res.replace('+', '%20')
res = res.replace('*', '%2A')
res = res.replace('%7E', '~')
return res


#获取账单
@DFF.API('getBill')
def getBill():
# 账单当前记录位置
next_token = ""
# 循环获取账单写入 DataKit
for i in range(10000):
random.seed()
# 唯一随机数,用于防止网络重放攻击。用户在不同请求间要使用不同的随机数值。
D["SignatureNonce"] = str(random.random())
D["NextToken"] = next_token
# 由于签名要求唯一性,包括顺序,所以需要按照参数名称排序
sortedD = sorted(D.items(),key=lambda x: x[0])
canstring = ''
for k,v in sortedD:
canstring += '&' + percentEncode(k) + '=' + percentEncode(v)
# 生成标准化请求字符串
stringToSign = 'GET&%2F&' + percentEncode(canstring[1:])
# access_key_secret
access_key_secret = '<access_key_secret>'
# 计算 HMAC 值
h = hmac.new((access_key_secret + "&").encode('utf8'), stringToSign.encode('utf8'), sha1)
# 计算签名值生成 signature 签名
signature = base64.encodestring(h.digest()).strip()
# 添加签名
D['Signature'] = signature
# 最终调用 API
url = 'http://business.aliyuncs.com/?' + urllib.parse.urlencode(D)
# 请求阿里云账单费用
response = requests.get(url)
billing_cycle = response.json()["Data"]["BillingCycle"]
account_id = response.json()["Data"]["AccountID"]
next_token = response.json()["Data"]["NextToken"]
if next_token is not None:
bill = response.json()["Data"]["Items"]["Item"]
print(bill)
# 写入当天账单到观测云
for i in bill:
print(i["UsageEndTime"])
time = i["UsageEndTime"].split(" ")[0]
print(time,now_time)
if time == now_time:
measurement = "aliyunSettleBill"
tags = {
"BillingCycle":billing_cycle,
"AccountID":account_id
}
fields = {
"ProductName":i["ProductName"],
"SubOrderId":i["SubOrderId"],
"BillAccountID":i["BillAccountID"],
"DeductedByCashCoupons":i["DeductedByCashCoupons"],
"PaymentTime":i["PaymentTime"],
"PaymentAmount":i["PaymentAmount"],
"DeductedByPrepaidCard":i["DeductedByPrepaidCard"],
"InvoiceDiscount":i["InvoiceDiscount"],
"UsageEndTime":i["UsageEndTime"],
"Item":i["Item"],
"SubscriptionType":i["SubscriptionType"],
"PretaxGrossAmount":i["PretaxGrossAmount"],
"Currency":i["Currency"],
"CommodityCode":i["CommodityCode"],
"UsageStartTime":i["UsageStartTime"],
"AdjustAmount":i["AdjustAmount"],
"Status":i["Status"],
"DeductedByCoupons":i["DeductedByCoupons"],
"RoundDownDiscount":i["RoundDownDiscount"],
"ProductDetail":i["ProductDetail"],
"ProductCode":i["ProductCode"],
"ProductType":i["ProductType"],
"OutstandingAmount":i["OutstandingAmount"],
"BizType":i["BizType"],
"PipCode":i["PipCode"],
"PretaxAmount":i["PretaxAmount"],
"OwnerID":i["OwnerID"],
"BillAccountName":i["BillAccountName"],
"RecordID":i["RecordID"],
"CashAmount":i["CashAmount"],
}
try:
status_code, result = datakit.write_logging(measurement=measurement, tags=tags, fields=fields)
print(status_code,result)
except:
print("插入失败!")
else:
break
else:
continue
break
else:
break


  1. **保存 **配置并 发布

1639636297439-6c843462-a534-44b5-915b-9f36fd420c65.png

  1. 添加自动触发任务,管理 - 自动触发配置 - 新建任务

由于账单为每日账单,所以采集频率设置每天一次就可以了

1639637052342-179346ea-f8a3-490d-9d5d-0aedecf0ed52.png

1639637089249-d3e6d331-4deb-433b-8bce-237932db4a4b.png

  1. 日志预览

1639637510658-f08ddaf1-1383-4870-9e68-626faf0d086d.png

链路分析

暂无

场景视图

暂无

查看器

1639637569053-b37c71fc-9f5a-48fc-97e1-28825a57cf54.png

异常检测

暂无

最佳实践

暂无

故障排查

  1. Func 日志路径:/usr/local/dataflux-func/data/logs/dataflux-func.log
  2. 代码调试,选择主函数,直接运行 (可以看到脚本输出)

1639637678125-2c3f8472-083b-43d2-a3a8-3bb0d96c5c80.png