cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Python Script as API Connector (BMC Truesight Events)

serhat_balik
Helper

Our client uses BMC Truesight to get the event feeds for their environment. Currently they are using AppDynamics and we are pushing for conversion to Dynatrace. The problem with Dynatrace in this situation is, we cannot use logical operators inside the Custom Integration > Payload.

For example; They don't want to use AVAILABILITY, ERROR, etc.. as their severity levels, so they wrote custom if/else blocks inside AppDynamics payload to change these to another names such as CRITICAL, MEDIUM, and push it to BMC Truesight with these names.

I tried writing a simple Python script to get events feed via API and push it to another API end point. I am considering to run this script in some server, possibly inside their BMC or Dynatrace node. But I am not sure how to make this script run indefinitely and only get unique eventIds that are not previously posted to BMC API.

I am new to Python scripting and the code below is probably not ideal, but I just wanted to write something that works, and it actually works. But again I don't know how to run it indefinitely and only post unique eventIds. I am open to all suggestions as well as about the structure of the code.


import requests
import json

getDynatraceEvents = requests.get("dynatrace-tenant-api-and-token")
payload = getDynatraceEvents.json()

for events in payload['events']:
  eventId = events['eventId']
  startTime = events['startTime']
  endTime = events['endTime']
  entityName = events['entityName']
  impactLevel = events['impactLevel']
  eventType = events['eventType']
  if 'severityLevel' in events:
   severityLevel = events['severityLevel']
  data = {
    "eventId": eventId,
    "startTime": startTime,
    "endTime": endTime,
    "entityName": entityName,
    "impactLevel": impactLevel,
    "eventType": eventType,
    "severityLevel": severityLevel
  }

  postPayload = json.dumps(data)
  postHeader = {'Accept' : 'application/json', 'Content-Type' : 'application/json'}
  truesightPost = requests.post('bmc-truesight-api-endpoint', headers = postHeader, data = postPayload)


1 REPLY 1

pahofmann
Leader

To do it in python you could use something like schedule. Or you could just setup a cronjob that calls you script in a certain intervall.

 

Another way would be to start a small web server  in your python script (e.g. with http.server) and call that from the dynatrace webhook. Then you can do the transformation and call Truesight everytime a webhook comes in.