leed ga

IDCF每日流量报表

2018-3-28 网络

东二新区没加进去,两个文件放在同一目录,执行php billingReport.php即可

util.php

<?php
function httpGet($url, $headers = []) {
    $httpClient = curl_init();
    curl_setopt($httpClient, CURLOPT_URL, $url);
    curl_setopt($httpClient, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($httpClient, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($httpClient, CURLOPT_HTTPGET, true);
    curl_setopt($httpClient, CURLOPT_HEADER, 0);
    curl_setopt($httpClient, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($httpClient, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($httpClient, CURLOPT_SSL_VERIFYHOST, false);
    return curl_exec($httpClient);
}
function sendMail($to, $subject, $body) {
    $subject = str_replace("\r\n", ' ', $subject);
    $body = preg_replace('/(=?^|\r\n)\./', '..', $body);
    $s = fsockopen("SMTP服务器地址", "25");
    fgets($s);
    $data = array(
        'MIME-Version: 1.0',
        'Content-Type: text/html; charset=utf-8',
        'From: ' . "发件邮箱",
        'To: ' . $to,
        'Subject: ' . $subject,
        "\r\n$body",
        '.'
    );
    $username = base64_encode("发件邮箱");
    foreach (array(
                 'HELO WEBMASTER',
                 'AUTH LOGIN',
                 $username,
                 base64_encode("发件邮箱密码"),
                 'MAIL FROM: <' . "发件邮箱" . '>',
                 'RCPT TO: <' . $to . '>',
                 'DATA', implode("\r\n", $data)
             ) as $i) {
        fwrite($s, "$i\r\n");
        $m = fgets($s);
        if ($m[0] > 3) return $m;
    }
    fclose($s);
}


billingReport.php

<?php
include_once "util.php";
const ENDPOINT_BILLING = "https://your.idcfcloud.com";
const ENDPOINT_EAST1 = "https://compute.jp-east.idcfcloud.com/client/api";
const ENDPOINT_EAST2 = "https://compute.jp-east-2.idcfcloud.com/client/api";
const ENDPOINT_WEST = "https://compute.jp-west.idcfcloud.com/client/api";
$apiKey = "";
$apiSecret = "";
$expiration = time() + 10;
date_default_timezone_set("Asia/Hong_Kong");
$month = date("Y-m");
$uri = "/api/v1/billings/{$month}";
$query_string = "format=json";
$message = "GET\n{$uri}\n{$apiKey}\n{$expiration}\n{$query_string}";
$signature = trim(base64_encode(hash_hmac("sha256", $message, $apiSecret, true)));
$headers = [
    "X-IDCF-APIKEY: {$apiKey}",
    "X-IDCF-Expires: {$expiration}",
    "X-IDCF-Signature: {$signature}",
];
$result = json_decode(httpGet(ENDPOINT_BILLING . "{$uri}?" . $query_string, $headers),  true);
$totalConsumption = $result['meta']['total'];
$totalConsumptionRMB = $totalConsumption * 0.0630484909;
$regionStatistics = [
    "jp-east" => [
        "henry" => [
            "Billing" => 0,
            "Transfer" => 0,
        ],
        "pascal" => [
            "Billing" => 0,
            "Transfer" => 0,
        ],
        "joule" => [
            "Billing" => 0,
            "Transfer" => 0,
        ],
    ],
    "jp-east-2" => [
        "weber" => [
            "Billing" => 0,
            "Transfer" => 0,
        ],
        "lux" => [
            "Billing" => 0,
            "Transfer" => 0,
        ],
    ],
    "jp-west" => [
        "augusta" => [
            "Billing" => 0,
            "Transfer" => 0,
        ],
        "monstera" => [
            "Billing" => 0,
            "Transfer" => 0,
        ],
    ]
];
foreach ($result['data'] as $row) {
    switch ($row['Category']) {
        case "VirtualMachine":
        case "Volume":
        $regionStatistics[$row['Region']][$row['ZoneName']]['Billing'] += $row['Net'];
            break;
        case "Internet Data Transfer":
            if ($row['Menu'] == "Internet Data Transfer [out]") {
                $regionStatistics[$row['Region']][$row['ZoneName']]['Transfer'] += $row['Usage'];
            }
    }
}
$mailBody = "
<html>
<head>
<link href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet">
<script src="http://cdn.bootcss.com/jquery/2.1.1/jquery.min.js"></script>
<script src="http://cdn.bootcss.com/less.js/1.5.0/less.min.js"></script>
<script src="http://cdn.bootcss.com/jquery-cookie/1.4.1/jquery.cookie.js"></script>
</head>
<body>
<div class="container">
                <div class="page-header">
                        <br /><br />
                        <h2>
                                <label>{$month}  IDCF消费日报</label>
                        </h2>
                </div>
                总消费额:{$totalConsumption}日元(约{$totalConsumptionRMB}人民币)<br />
                以下为各区消费情况
                <div class="row" style="margin-bottom:30px;">
                        <div class="col-xs-12">
                                <table class="table table-striped">
                                        <thead><tr><th>大区</th><th>Zone</th><th>消费额</th><th>流出流量</th></tr></thead>
                                        <tbody>";
foreach ($regionStatistics as $regionName => $data) {
    foreach ($data as $zone => $detail) {
        $mailBody .= "<tr><td>{$regionName}</td><td>$zone<td>{$detail['Billing']}日元</td><td>{$detail['Transfer']}GB</td></tr>";
    }
}
$mailBody .= "</tbody>
                                </table>
                        </div>
        </div>
</div>
<script src="https://cdn.bootcss.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
</body>
</html>";
sendMail("收件邮箱", "【IDCF】消费日报", $mailBody);


发表评论:

  • 1
  • 5
  • 0
  • 4

© CNM.EE SiteMap