Ahoj, neřešil někdo jak napojit Azure AI nebo Logs do Zabbixu, a nemohl by mně navést jak to udělat?
Díky.
Díky.
const tenantId = "fill";
const clientId = "fill";
const clientSecret = "fill";
const resourceUrl = "fill";
// URL pro získání přístupového tokenu
const tokenUrl = "https://login.microsoftonline.com/" + tenantId + "/oauth2/token";
// Data pro požadavek na token
const tokenData = "grant_type=client_credentials&client_id=" + encodeURIComponent(clientId) + "&client_secret=" + encodeURIComponent(clientSecret) + "&resource=" + encodeURIComponent(resourceUrl);
// Odešleme požadavek na získání tokenu
const tokenRequest = new HttpRequest();
tokenRequest.addHeader("Content-Type", "application/x-www-form-urlencoded");
var resp = tokenRequest.post(tokenUrl, tokenData);
if (tokenRequest.getStatus() === 200) {
const tokenResponse = JSON.parse(resp);
const accessToken = tokenResponse.access_token;
// accessToken je nyní dostupný pro použití v dalších API voláních
return "Access Token: " + accessToken; // Vrátíme access token
} else {
return 'Chyba při získávání přístupového tokenu: ' + tokenRequest.getStatus(); // Vrátíme chybovou zprávu
}
const params = JSON.parse(value);
var Azure = {
params: {},
token: '',
// Function to set the parameters for Azure
setParams: function (params) {
// Check if all required parameters are provided
['client_id', 'secret', 'scope', 'tenant_id', 'url'].forEach(function (field) {
if (typeof params !== 'object' || typeof params[field] === 'undefined' || params[field] === '') {
throw 'Required param is not set: ' + field + '.';
}
});
Azure.params = params;
},
// Function to perform login and retrieve access token
login: function () {
var response, login = new HttpRequest();
login.addHeader('Content-Type: application/x-www-form-urlencoded');
response = login.post(
'https://login.microsoftonline.com/' + encodeURIComponent(Azure.params.tenant_id) + '/oauth2/v2.0/token',
'grant_type=client_credentials&client_id=' + encodeURIComponent(Azure.params.client_id) + '&client_secret=' + encodeURIComponent(Azure.params.secret) + '&scope=' + encodeURIComponent(Azure.params.scope)
);
// Check if login was successful and access token is received
if (login.getStatus() !== 200) {
throw 'Login failed with status code ' + login.getStatus() + ': ' + response;
}
try {
response = JSON.parse(response);
if (!response.hasOwnProperty('access_token')) {
throw null;
}
}
catch (error) {
throw 'Authentication response does not contain access token.';
}
Azure.token = response['access_token'];
},
// Function to make a request to the specified URL with the access token
request: function (url) {
var response, request = new HttpRequest();
// Check if access token is available
if (!Azure.token) {
throw 'Request does not contain access token.';
}
request.addHeader('Accept: application/json');
request.addHeader('Authorization: Bearer ' + Azure.token);
response = request.get(url);
// Check if the request was successful and parse the response
if (request.getStatus() !== 200) {
throw 'Request failed with status code ' + request.getStatus() + ': ' + response;
}
try {
return JSON.parse(response);
}
catch (error) {
throw 'Failed to parse response received from API.';
}
},
// Function to retrieve the content of a URL using Azure request
getUrlContent: function (url) {
var response = Azure.request(url);
return response.value;
}
};
try {
// Set the parameters, perform login, and retrieve URL content
Azure.setParams(JSON.parse(value));
Azure.login();
return JSON.stringify(Azure.getUrlContent(Azure.params.url));
}
catch (error) {
error += (String(error).endsWith('.')) ? '' : '.';
Zabbix.log(3, '[ Azure ] ERROR: ' + error);
return JSON.stringify({'error': error});
}
Comment