数据中台赋能云成本管理(1):创建账单项目

背景

成本管家中,SLS提供了云产品账单的数据,通过SLS强大的分析和可视化能力,帮助客户分析云产品账单,优化成本。在发布后在客户中间引起强烈的反馈,不少客户要求在账单中增加实例名,Tag,资源用量等信息,通过更多维度数据做分账。客户对分账有比较强烈的需求,通过把账单分配到不同的部门,让不同部门各自完成自己的成本优化工作。为了满足客户的分账需求,我们在成本管家二期中引入了更加丰富的账单数据,本篇文档,讲介绍如何通过代码一步创建账单数据。

授权SLS成本管家获取账单数据

点击授权链接,同意SLS成本管家获取账单数据。

通过Java SDK创建账单数据

1. 引入Pom依赖

         <dependency>
            <groupId>com.aliyun.openservices</groupId>
            <artifactId>aliyun-log</artifactId>
            <version>0.6.45</version>
        </dependency>

2. 复制程序

创建类文件bill_ingestion_creator

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.AliyunBSSSource;
import com.aliyun.openservices.log.common.Dashboard;
import com.aliyun.openservices.log.common.Ingestion;
import com.aliyun.openservices.log.common.IngestionConfiguration;
import com.aliyun.openservices.log.common.JobSchedule;
import com.aliyun.openservices.log.common.JobScheduleType;
import com.aliyun.openservices.log.common.JobState;
import com.aliyun.openservices.log.common.LogStore;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.CreateDashboardRequest;
import com.aliyun.openservices.log.request.CreateIngestionRequest;
import com.aliyun.openservices.log.request.ListIngestionRequest;
import com.aliyun.openservices.log.response.ListIngestionResponse;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;

public class bill_ingestion_creator {

    static String endpoint = "cn-chengdu.log.aliyuncs.com";
    static String accessId = ""; // 修改成你的AccessId
    static String accessKey = ""; // 修改成你的accessKey
    static String aliuid = ""; // 修改成你的aliuid

    static String getIngestionName() {
        return "ingestion-" + (int) (new Date().getTime() / 1000);
    }

    private static Ingestion createIngestion() {
        Ingestion ingestion = new Ingestion();
        String jobName = getIngestionName();
        ingestion.setName(jobName);
        ingestion.setState(JobState.ENABLED);
        ingestion.setDisplayName("BSS-test");
        IngestionConfiguration configuration = new IngestionConfiguration();
        configuration.setLogstore("aliyun_bill");
        AliyunBSSSource source = new AliyunBSSSource();
        source.setHistoryMonth(7);
        source.setRoleARN("acs:ram::" + aliuid + ":role/AliyunLogAccessingBSSRole");
        configuration.setSource(source);
        ingestion.setConfiguration(configuration);
        JobSchedule schedule = new JobSchedule();
        schedule.setInterval("10m");
        schedule.setType(JobScheduleType.FIXED_RATE);
        schedule.setRunImmediately(true);
        ingestion.setSchedule(schedule);
        return ingestion;
    }

    public static void main(String args[]) throws LogException {
        Ingestion ingestion = createIngestion();
        String project = "bill-sls-" + aliuid;
        String logstore = "aliyun_bill";
        Client client = new Client(endpoint, accessId, accessKey);
        String index = downFile("index");
        try {
            client.CreateProject(project, "bill analysis");
        } catch (LogException e) {
            e.printStackTrace();
        }
        try {
            client.CreateLogStore(project, new LogStore("aliyun_bill", 3000, 5));
        } catch (LogException e) {
            e.printStackTrace();
        }
        try {
            client.CreateIndex(project, logstore, index);
        } catch (LogException e) {
            e.printStackTrace();
        }
        String dashboards[] = {"dashboard_sls", "dashboard_oss", "dashboard_ecs", "dashboard_all", "bill_summary_cn", "bill_detail_cn", "bill_optimize_cn"};
        for (String dashboardName : dashboards) {
            Dashboard dashboard = new Dashboard();
            String config = downFile(dashboardName);
            dashboard.FromJsonString(config);
            try {
                client.createDashboard(new CreateDashboardRequest(project, dashboard));
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
        ListIngestionResponse response1 = client.listIngestion(new ListIngestionRequest(project));
        if(response1.getResults().size()> 0) {
            System.out.println("ingestion is already created,don't recreate");
        }
        else {
            client.createIngestion(new CreateIngestionRequest(project, ingestion));
        }
        response1 = client.listIngestion(new ListIngestionRequest(project));
        for (Ingestion ingestion1 : response1.getResults()) {
            System.out.println("bill ingestion:" + ingestion1.getName() + ", project:" + project + ", logstore :" + ingestion1.getConfiguration().getLogstore());
        }    }

    public static String downFile(String fileName) {
        try {
            URL url = new URL("http://logdemo.oss-cn-beijing.aliyuncs.com/bill_logstore/" + fileName);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(3 * 1000);
            InputStream inputStream = conn.getInputStream();
            byte[] buf =  readInputStream(inputStream);
            return new String(buf);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
    public static byte[] readInputStream(InputStream inputStream) throws IOException {
        byte[] buffer = new byte[1024];
        int len = 0;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        while ((len = inputStream.read(buffer)) != -1) {
            bos.write(buffer, 0, len);
        }
        bos.close();
        return bos.toByteArray();
    }
}

3. 修改账号参数

在上述程序中,修改以下三个参数,分别是accessId, accessKey, aliuid。

     static String accessId = ""; // 修改成你的AccessId
    static String accessKey = ""; // 修改成你的accessKey
    static String aliuid = ""; // 修改成你的aliuid

查看账单数据

以上操作完成后,等待一分钟,即可进入sls控制台,选择bill-sls-开头的账单即可。

数据中台赋能云成本管理(1):创建账单项目

  1. 在logstore列表中选择aliyun_bill,即可查看账单原始数据。
    数据中台赋能云成本管理(1):创建账单项目
  2. 在dashboard中选择对应的报表,即可查看系统SLS提供的内置报表。

数据中台赋能云成本管理(1):创建账单项目

账单原始数据:

账单日期 BillingDate:  2020-02-05
计费项 BillingItem:  流出流量
计费类型 BillingType:  其它
消费单位 CostUnit:  未分配
货币 Currency:  CNY
现金券抵扣 DeductedByCashCoupons:  0.0
代金券抵扣 DeductedByCoupons:  0.0
预存卡抵扣 DeductedByPrepaidCard:  0.0
资源包抵扣 DeductedByResourcePackage:  0
实例配置 InstanceConfig:  iz:华东 1 可用区 F;实例规格名称:ecs.xn4.small;CPU:1;内存:1024;磁盘:1;带宽:512000;OS:UBUNTU;是否io优化:io_optimized_only
实例ID InstanceID:  i-bp14putxkqvmal310ian
实例描述 InstanceSpec:  ecs.xn4.small
公网IP InternetIP:  47.96.36.117
内网IP IntranetIP:  10.80.65.37
发票抵扣 InvoiceDiscount:  0.0
付费类型  Item:  PayAsYouGoBill
单价 ListPrice:  0.800000
单价单位 ListPriceUnit:  元/Mbps
昵称  NickName:  iZbp14putxkqvmal310ianZ
OutstandingAmount:  0.0
OwnerID:  1340796328858956
付费金额 PaymentAmount:  0.0
税前金额  PretaxAmount:  0.0
税前原始金额  PretaxGrossAmount:  0.009
产品ProductCode:  ecs
产品详情 ProductDetail:  云服务器ECS-按量付费
产品名称 ProductName:  云服务器 ECS
产品类型 ProductType:  
地域 Region:  华东1(杭州)
资源组 ResourceGroup:  默认资源组
服务周期 ServicePeriod:  86400
订阅类型 SubscriptionType:  PayAsYouGo
标签 Tag:  key:department value:mayunlei
使用量 Usage:  0.011000
使用量单位 UsageUnit:  Mbps
地域 Zone:  cn-hangzhou-f
__source__:  instance_bill

进行自定义分析

在实例账单分析中,各个企业的分账模式各不相同,有的采用TAG方式,有的在实例名称中定义责任人,有的在昵称中定义责任人,不同的方式如何进行分账管理,请参考本系列接下来的文章。

上一篇:JS获取客户端系统当前时区


下一篇:酷!在终端上使用命令行可视化分析日志