# Python Script as API Connector (BMC Truesight Events)

**URL:** <https://community.dynatrace.com/t/python-script-as-api-connector-bmc-truesight-events/21502>\
**Category:** Dynatrace API\
**Tags:** integrations, dynatrace-api\
**Created:** [February 15, 2021, 3:38pm UTC](https://community.dynatrace.com/t/python-script-as-api-connector-bmc-truesight-events/21502 "2021-02-15T15:38:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![serhat\_balik](https://avatars.discourse-cdn.com/v4/letter/s/c57346/32.png) [@serhat\_balik](https://community.dynatrace.com/u/serhat_balik)\
**Post date:** [February 15, 2021, 3:38pm UTC](https://community.dynatrace.com/t/python-script-as-api-connector-bmc-truesight-events/21502/1 "2021-02-15T15:38:09Z")

</div>

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.

```auto
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)

```

---

<div class="post-metadata">

**Author:** ![pahofmann](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/pahofmann/32/17076_2.png) [@pahofmann](https://community.dynatrace.com/u/pahofmann)\
**Post date:** [March 16, 2021, 9:07am UTC](https://community.dynatrace.com/t/python-script-as-api-connector-bmc-truesight-events/21502/2 "2021-03-16T09:07:38Z")

</div>

To do it in python you could use something like [schedule](https://github.com/dbader/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&nbsp; 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.
