Skip to content

Instantly share code, notes, and snippets.

@torumakabe
Created December 19, 2017 04:56
Show Gist options
  • Save torumakabe/7f0eb15c40cdf4f9773be50f01f062f0 to your computer and use it in GitHub Desktop.
Save torumakabe/7f0eb15c40cdf4f9773be50f01f062f0 to your computer and use it in GitHub Desktop.
Azure Log Analytics API call Sample
const request = require('request');
const tenant = '<Your Tenant ID>';
const subscriptionId = '<Yout Subscription ID>';
const clientId = '<Your Service Principal App ID>';
const clientSecret = '<Your Service Principal Password>';
const resourceGroup = '<Your Resource Group Name of Log Analytics Workspace>';
const workSpace = '<Your Workspace Name>';
request.post({
url: `https://login.microsoftonline.com/${tenant}/oauth2/token`,
form: {
grant_type: 'client_credentials',
client_id: clientId,
redirect_uri: 'dummy',
resource: 'https://management.azure.com/',
client_secret: clientSecret
}
}, function (err, httpResponse, body) {
const args = {
uri: `https://management.azure.com/subscriptions/${subscriptionId}/resourceGroups/${resourceGroup}/providers/Microsoft.OperationalInsights/workspaces/${workSpace}/api/query?api-version=2017-01-01-preview`,
headers: {
"Content-type": "application/json",
"Authorization": "Bearer " + JSON.parse(body).access_token
},
json: {
//Log Analytics Query
"query": "AzureActivity | summarize count() by Category"
}
};
request.post(args, function (error, response, body) {
//Handle body
});
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment