全球主机交流论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

CeraNetworks网络延迟测速工具IP归属甄别会员请立即修改密码
查看: 5563|回复: 26

开源个IDCF每日流量报表,同情下那个流量超了的代购哥们...

[复制链接]
发表于 2018-3-27 18:42:20 | 显示全部楼层 |阅读模式
本帖最后由 vForce 于 2018-3-28 08:32 编辑

自己写的,用了挺久了。有个东二的新区没做进去,有点代码基础的朋友自己加一下吧。
两个文件放在同一目录,执行php billingReport.php即可,基于PHP5.6开发,PHP7没试过。
util.php:
  1. <?php
  2. function httpGet($url, $headers = []) {
  3.     $httpClient = curl_init();
  4.     curl_setopt($httpClient, CURLOPT_URL, $url);
  5.     curl_setopt($httpClient, CURLOPT_RETURNTRANSFER, 1);
  6.     curl_setopt($httpClient, CURLOPT_FOLLOWLOCATION, true);
  7.     curl_setopt($httpClient, CURLOPT_HTTPGET, true);
  8.     curl_setopt($httpClient, CURLOPT_HEADER, 0);
  9.     curl_setopt($httpClient, CURLOPT_HTTPHEADER, $headers);
  10.     curl_setopt($httpClient, CURLOPT_SSL_VERIFYPEER, false);
  11.     curl_setopt($httpClient, CURLOPT_SSL_VERIFYHOST, false);
  12.     return curl_exec($httpClient);
  13. }
  14. function sendMail($to, $subject, $body) {
  15.     $subject = str_replace("\r\n", ' ', $subject);
  16.     $body = preg_replace('/(=?^|\r\n)\./', '..', $body);
  17.     $s = fsockopen("SMTP服务器地址", "25");
  18.     fgets($s);
  19.     $data = array(
  20.         'MIME-Version: 1.0',
  21.         'Content-Type: text/html; charset=utf-8',
  22.         'From: ' . "发件邮箱",
  23.         'To: ' . $to,
  24.         'Subject: ' . $subject,
  25.         "\r\n$body",
  26.         '.'
  27.     );
  28.     $username = base64_encode("发件邮箱");
  29.     foreach (array(
  30.                  'HELO WEBMASTER',
  31.                  'AUTH LOGIN',
  32.                  $username,
  33.                  base64_encode("发件邮箱密码"),
  34.                  'MAIL FROM: <' . "发件邮箱" . '>',
  35.                  'RCPT TO: <' . $to . '>',
  36.                  'DATA', implode("\r\n", $data)
  37.              ) as $i) {
  38.         fwrite($s, "$i\r\n");
  39.         $m = fgets($s);
  40.         if ($m[0] > 3) return $m;
  41.     }
  42.     fclose($s);
  43. }
复制代码

billingReport.php:
  1. <?php
  2. include_once "util.php";
  3. const ENDPOINT_BILLING = "https://your.idcfcloud.com";
  4. const ENDPOINT_EAST1 = "https://compute.jp-east.idcfcloud.com/client/api";
  5. const ENDPOINT_EAST2 = "https://compute.jp-east-2.idcfcloud.com/client/api";
  6. const ENDPOINT_WEST = "https://compute.jp-west.idcfcloud.com/client/api";
  7. $apiKey = "";
  8. $apiSecret = "";
  9. $expiration = time() + 10;
  10. date_default_timezone_set("Asia/Hong_Kong");
  11. $month = date("Y-m");
  12. $uri = "/api/v1/billings/{$month}";
  13. $query_string = "format=json";
  14. $message = "GET\n{$uri}\n{$apiKey}\n{$expiration}\n{$query_string}";
  15. $signature = trim(base64_encode(hash_hmac("sha256", $message, $apiSecret, true)));
  16. $headers = [
  17.     "X-IDCF-APIKEY: {$apiKey}",
  18.     "X-IDCF-Expires: {$expiration}",
  19.     "X-IDCF-Signature: {$signature}",
  20. ];
  21. $result = json_decode(httpGet(ENDPOINT_BILLING . "{$uri}?" . $query_string, $headers),  true);
  22. $totalConsumption = $result['meta']['total'];
  23. $totalConsumptionRMB = $totalConsumption * 0.0630484909;
  24. $regionStatistics = [
  25.     "jp-east" => [
  26.         "henry" => [
  27.             "Billing" => 0,
  28.             "Transfer" => 0,
  29.         ],
  30.         "pascal" => [
  31.             "Billing" => 0,
  32.             "Transfer" => 0,
  33.         ],
  34.         "joule" => [
  35.             "Billing" => 0,
  36.             "Transfer" => 0,
  37.         ],
  38.     ],
  39.     "jp-east-2" => [
  40.         "weber" => [
  41.             "Billing" => 0,
  42.             "Transfer" => 0,
  43.         ],
  44.         "lux" => [
  45.             "Billing" => 0,
  46.             "Transfer" => 0,
  47.         ],
  48.     ],
  49.     "jp-west" => [
  50.         "augusta" => [
  51.             "Billing" => 0,
  52.             "Transfer" => 0,
  53.         ],
  54.         "monstera" => [
  55.             "Billing" => 0,
  56.             "Transfer" => 0,
  57.         ],
  58.     ]
  59. ];
  60. foreach ($result['data'] as $row) {
  61.     switch ($row['Category']) {
  62.         case "VirtualMachine":
  63.         case "Volume":
  64.         $regionStatistics[$row['Region']][$row['ZoneName']]['Billing'] += $row['Net'];
  65.             break;
  66.         case "Internet Data Transfer":
  67.             if ($row['Menu'] == "Internet Data Transfer [out]") {
  68.                 $regionStatistics[$row['Region']][$row['ZoneName']]['Transfer'] += $row['Usage'];
  69.             }
  70.     }
  71. }
  72. $mailBody = "
  73. <html>
  74. <head>
  75. <link href=\"http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css\" rel=\"stylesheet\">
  76. <script src=\"http://cdn.bootcss.com/jquery/2.1.1/jquery.min.js\"></script>
  77. <script src=\"http://cdn.bootcss.com/less.js/1.5.0/less.min.js\"></script>
  78. <script src=\"http://cdn.bootcss.com/jquery-cookie/1.4.1/jquery.cookie.js\"></script>
  79. </head>
  80. <body>
  81. <div class=\"container\">
  82.                 <div class=\"page-header\">
  83.                         <br /><br />
  84.                         <h2>
  85.                                 <label>{$month}  IDCF消费日报</label>
  86.                         </h2>
  87.                 </div>
  88.                 总消费额:{$totalConsumption}日元(约{$totalConsumptionRMB}人民币)<br />
  89.                 以下为各区消费情况
  90.                 <div class=\"row\" style=\"margin-bottom:30px;\">
  91.                         <div class=\"col-xs-12\">
  92.                                 <table class=\"table table-striped\">
  93.                                         <thead><tr><th>大区</th><th>Zone</th><th>消费额</th><th>流出流量</th></tr></thead>
  94.                                         <tbody>";
  95. foreach ($regionStatistics as $regionName => $data) {
  96.     foreach ($data as $zone => $detail) {
  97.         $mailBody .= "<tr><td>{$regionName}</td><td>$zone<td>{$detail['Billing']}日元</td><td>{$detail['Transfer']}GB</td></tr>";
  98.     }
  99. }
  100. $mailBody .= "</tbody>
  101.                                 </table>
  102.                         </div>
  103.         </div>
  104. </div>
  105. <script src=\"https://cdn.bootcss.com/bootstrap/3.3.0/js/bootstrap.min.js\"></script>
  106. </body>
  107. </html>";
  108. sendMail("收件邮箱", "【IDCF】消费日报", $mailBody);
复制代码
 楼主| 发表于 2018-3-27 20:18:00 | 显示全部楼层
zddewe 发表于 2018-3-27 19:56
支持大佬。但是飛機場代碼好像可以限制流量了。按理來說 VPS 應該也能寫出類似的代碼,VPS 達到多少流量就 ...

确实很有道理,可以在这个基础上加上流量超过阈值直接API关机。好想法!
发表于 2018-3-27 19:02:11 | 显示全部楼层
支持一下大佬!
发表于 2018-3-27 19:31:33 来自手机 | 显示全部楼层
支持大佬!感谢!
发表于 2018-3-27 19:56:33 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
发表于 2018-3-27 20:05:46 | 显示全部楼层
zddewe 发表于 2018-3-27 19:56
支持大佬。但是飛機場代碼好像可以限制流量了。按理來說 VPS 應該也能寫出類似的代碼,VPS 達到多少流量就 ...

你说的怕是叫后门
发表于 2018-3-27 20:07:47 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
发表于 2018-3-27 20:17:03 来自手机 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
发表于 2018-3-27 20:35:38 | 显示全部楼层
这都有,厉害了
发表于 2018-3-27 21:17:30 | 显示全部楼层
支持php大佬, 感谢!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|全球主机交流论坛

GMT+8, 2024-4-19 21:04 , Processed in 0.061901 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表