LoginSignup
2
0

More than 1 year has passed since last update.

さくらのクラウド ウェブアクセラレータ(CDN) の使用量などのレポートをSlackに通知する

Last updated at Posted at 2022-02-22

前提

ウェブアクセラレータのAPI

ウェブアクセラレータのAPIキーの発行の仕方

1.【さくらのクラウド】コントロールパネルにログインする

2.【ホーム】→【APIキー】を選ぶ
APIキー.png

3.対象のクラウドアカウントを選ぶ(通知したいウェブアクセラレータのサイト登録をしているクラウドアカウント)
APIキー_2.png
4. 【+追加】を選ぶ
5. 「リソース操作APIキー」の種類で、「他サービスへのアクセス権」に【ウェブアクセラレータ】を選択したキーを【作成】スクリーンショット 2022-02-01 161600.png

ウェブアクセラレータの月別使用量取得APIの手動実行

  • 月別使用量取得API」(クラウドアカウントに登録されている全サイトの月別使用量を取得)

  • 実行例 (1日前の日付の「年月」分のmonthlyusage取得)

# target_month=$(date -d '1 days ago' '+%Y%m')
# webaccel_api_baseurl="https://secure.sakura.ad.jp/cloud/zone/is1a/api/webaccel/1.0/"
# sakura_cloud_api_secret="*****"
# sakura_cloud_api_token="*****"
# curl -s --user "${sakura_cloud_api_token}":"${sakura_cloud_api_secret}" ${webaccel_api_baseurl}monthlyusage?target=${target_month} | jq .

{                                                                                                                                                                                                                  "Year": 2022,                                                                                                                                                                                                    "Month": 2,                                                                                                                                                                                                      "MonthlyUsages": [                                                                                                                                                                                                 
    {                                                                                                                                                                                                                  
      "SiteID": "***********",                                                                                                                                                                                        
      "Domain": "****.example.com",                                                                                                                                                                           
      "ASCIIDomain": "****.example.com",                                                                                                                                                                      
      "SubDomain": "********.user.webaccel.jp",
      "AccessCount": "1010204050",                                                                                                                                                                                                         
      "BytesSent": "106658204050",                                                                                                                                                                                                   
      "CacheMissBytesSent": "10824020707",                                                                                                                                                                                     
      "CacheHitRatio": 85.148514851485,                                                                                                                                                                                
      "BytesCacheHitRatio": 99.89851675449,                                                                                                                                                                            
      "Price": 495                                                                                                                                                                                       
    },
    ...(略)....
    }
  ],
  "Success": true,
  "is_ok": true
}             

ウェブアクセラレータの月別使用量取得APIを実行し、Slackに通知するスクリプト

  • スクリプト例 (1日前の日付の「年月」分のmonthlyusage取得し、Slackに通知)
#!/bin/bash


webhookurl="https://hooks.slack.com/services/*************************"
channel_name="#*****"
username="webaccel-report"


sakura_cloud_api_secret="*****"
sakura_cloud_api_token="*****"


webaccel_api_baseurl="https://secure.sakura.ad.jp/cloud/zone/is1a/api/webaccel/1.0/"


target_month=$(date -d '1 days ago' '+%Y%m')
text_target_month=$(date -d '1 days ago' '+%Y年%m月')


which jq >/dev/null
rc=$?
if [ $rc -ne 0 ] ; then
	echo "jqをインストールしてください"
	exit
fi


jq_all=""
jq_all=$(curl -s --user "${sakura_cloud_api_token}":"${sakura_cloud_api_secret}" ${webaccel_api_baseurl}monthlyusage?target=${target_month} | jq '.MonthlyUsages[]')
rc=$?


if [ $rc -ne 0 ] ; then
	report="取得に失敗しました"
else
  report=$(echo "ドメイン"  "転送量"  "金額")"\n====================\n"
  for domain in $(echo $jq_all | jq '.Domain')
  do
    jq_domain=$(echo $jq_all | jq ". | select(.Domain == "$domain")")


    bytes_sent=$(echo $jq_domain | jq -c .BytesSent )
    bytes_sent=$(echo $bytes_sent | sed "s/\"//g" )
    gib_bytes_sent=$(bc <<< "scale=2; ${bytes_sent}/1024/1024/1024")


    price=$(echo $jq_domain | jq -c .Price )
    price=$(echo $price  | sed "s/\"//g" )


    domain=$(echo $domain | sed -e 's/"//g')
    report=${report}$(echo $domain :  $gib_bytes_sent GiB , $price "円" )"\n"
  done
fi


massage="ウェブアクセラレータ配信量レポート ${text_target_month}\n"
massage=$massage'```'$report'```'
curl -s -S -X POST --data-urlencode "payload={\"channel\": \"${channel_name}\", \"username\": \"${username}\", \"text\": \"${massage}\", \"icon_emoji\": \":ghost:\"}" $webhookurl  >/dev/null


exit
  • Slack通知例
ウェブアクセラレータ配信量レポート 2022年02月

ドメイン 転送量 金額
====================
*****.example.com : 99.3 GiB , 495 円
*****.example.com : 11.2 GiB , 55 円
*****.user.webaccel.jp : 0.5 GiB , 0 円

  • 月次、週次、日次など好みのタイミングでcronに登録。
    • 設定例:月初9時に前月分を取得の場合
9 0 1 * * /usr/local/bin/webaccel-usage-slack.sh

資料

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0