Skip to content

Instantly share code, notes, and snippets.

@astj
Last active Sep 29, 2016
Embed
What would you like to do?
function main() {
var mackerelApiKey = "YOUR_MACKEREL_API_KEY";
var dropBoxAccessToken = "YOUR_DROPBOX_ACCESS_TOKEN"
var serviceName = "YOUR_SERVICE_NAME"
var dropBoxUsageRes = JSON.parse(getDropBoxUsage(dropBoxAccessToken).getContentText())
var usedBytes = dropBoxUsageRes.used;
var allocatedBytes = dropBoxUsageRes.allocation.allocated;
var epoch = Date.now() / 1000;
var payload = [
{"name" : "dropbox.used", "time" : epoch, "value" : usedBytes},
{"name" : "dropbox.allocated", "time" : epoch, "value" : allocatedBytes}
];
Logger.log(payload)
var mackerelPostRes = postMackerelServiceMetric(mackerelApiKey, serviceName, payload);
Logger.log(mackerelPostRes)
}
function postMackerelServiceMetric(apiKey, serviceName, payload) {
return UrlFetchApp.fetch(
"https://mackerel.io/api/v0/services/" + serviceName + "/tsdb",
{
"contentType" : "application/json",
"method" : "post",
"headers" : {
"X-Api-Key" : apiKey
},
"payload" : JSON.stringify(payload),
"muteHttpExceptions" : true
}
);
}
function getDropBoxUsage(apiKey) {
return UrlFetchApp.fetch(
"https://api.dropboxapi.com/2/users/get_space_usage",
{
"method" : "post",
"contentType" : "application/json",
"payload" : "null",
"headers" : {
"Authorization" : "Bearer " + apiKey
},
"muteHttpExceptions" : true // for debug
}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment