ITOM

SayaniB82563620
Tera Contributor

I want to store data in the event management (em_event) table from a third party monitoring tool.  With the REST message am able to fetch data in my instance but not able to store data in the particular table. Please help me out how I can achieve this?

2 REPLIES 2

Kieran Anson
Kilo Patron

Can you share your code? We can then see if there is an issue which will prevent you inserting a record into em_event

var DatagogAPIIntegration = Class.create();
DatagogAPIIntegration.prototype = {
    initialize: function() {},
fetchData:function(){
        var request = new sn_ws.RESTMessageV2();
        request.setEndpoint('my end point');
        request.setHttpMethod('GET');
        request.setRequestHeader('Content-Type', 'application/json');
        request.setRequestHeader('DD-API-KEY', 'xxxx');
        request.setRequestHeader('DD-APPLICATION-KEY', 'xxxxxxxx');
 
        var response = request.execute();
        var responseBody = response.getBody();
        var httpStatus = response.getStatusCode();
       
        if (httpStatus == 200) {
            var responseData = JSON.parse(responseBody);
            this.storeData(responseData);
        } else {
            gs.error('Error fetching data from Datadog: ' + httpStatus);
        }
    },
 
    storeData: function(data) {
        var pointlist = data.series[0].pointlist;
        for (var i = 0; i < pointlist.length; i++) {
            var timestamp = pointlist[i][0];
            var value = pointlist[i][1];
           
            
            var glideDateTime = new GlideDateTime();
            glideDateTime.setNumericValue(timestamp);
           
            var eventGR = new GlideRecord('em_event');
            eventGR.initialize();
            eventGR.setValue('source', 'Datadog');
            eventGR.setValue('node', 'VLPXYZSOL-93');
            eventGR.setValue('type', 'metric'); 
            eventGR.setValue('metric_name', 'system.cpu.system');
            eventGR.setValue('resource', 'CPU'); 
            eventGR.setValue('type_of_event', 'cpu_usage');
            eventGR.setValue('metric_value', value); 
            eventGR.setValue('time_of_event', glideDateTime);
            eventGR.insert();
        }
    },
 
    type: 'DatagogAPIIntegration'
};