Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Introduction

...

Please add "Name" and "Access". Then click on "Save Integration"

Image RemovedImage Added

Python Scripts

...

Copy the following script and replace "YOUR_API_KEY" with your actual Opsgenie API key.

Code Block
#!/usr/bin/python3

import requests, json, sys


# Set your Opsgenie API key
API_KEY = "YOUR_API_KEY"

# Create a new alert in Opsgenie with the specified alias
def create_alert():
    url = "https://api.opsgenie.com/v2/alerts"
    headers = {"Content-Type": "application/json", "Authorization": "GenieKey " + API_KEY}
    data = {
        "alias": sys.argv[1],
        "message": sys.argv[2] +" : " + sys.argv[4],
        "priority": "P3",
        "tags": ["FirstWave", "Opmantek"],
        "description": sys.argv[2] + sys.argv[4] + ": This alert will write to the Opmantek event log.",
        "name": "FirstWave",
        "type": "team"
    }
    response = requests.post(url, headers=headers, data=json.dumps(data))
    if response.status_code == 202:
        print("Alert created successfully.")
    else:
        print("Failed to create alert.")

create_alert()

...

Copy the following script and replace:


  • "YOUR_API_KEY" with your actual Opsgenie API key.
  • "YOUR_IP_ADDRESS" with your opEvents IP Address. 
  • "YOUR_OPEVENTS_USER" with your opEvents User.
  • "YOUR_OPEVENTS_PASSWORD" with your opEvents Password.


Code Block
#!/usr/bin/python3

import sys, requests, json

API_KEY = "YOUR_API_KEY"


eventid = sys.argv[1]

host = 'http://YOUR_IP_ADDRESS/en/omk/opEvents'
loginurl = host + '/login'
eventurl = host + '/events/' + eventid + '.json'

s = requests.Session()

# Set proper headers
headers = {"Content-Type":"application/json","Accept":"application/json"}

user = 'YOUR_OPEVENTS_USER'
pwd = 'YOUR_OPEVENTS_PASSWORD'

auth = {'username': user, 'password': pwd}
loginresponse = s.post(loginurl, data = auth)
#print loginresponse
if loginresponse.status_code != 200:
    print('Login failed. Status:', loginresponse.status_code)
    sys.exit(1)

# Do the HTTP request
response = s.get(eventurl, headers=headers)

# Check for HTTP codes other than 200
if response.status_code != 200:
   print('Status:', response.status_code, 'Headers:', response.headers, 'Error Response:',response.json())
   exit(1)

# Decode the JSON response into a dictionary and use the data
result = response.json()
stateful_eventids = result.get('stateful_eventids', [])
statefulID = stateful_eventids[0]['$oid'] if stateful_eventids else None

print('Stateful ID:', statefulID)

# Close an existing alert in Opsgenie with the specified alias
def close_alert():
    url = 'https://api.opsgenie.com/v2/alerts/' + statefulID + '/close?identifierType=alias'
    headers = {"Content-Type": "application/json", "Authorization": "GenieKey " + API_KEY}
    data = {"user": "Opmantek@example.com", "note": sys.argv[2] + " : " + sys.argv[4] + ": Alert has been resolved."}
    response = requests.post(url, headers=headers, data=json.dumps(data))
    if response.status_code == 202:
        print("Alert closed successfully.")
    else:
        print("Failed to close alert.")

# Example usage
close_alert()

...

Note: In this case we are using the "alias" in order to CLOSE or UPDATE the alert on Opsgenie. Values are idtiny and alias. Default value is id.

Change the owner and permissions. 

Code Block
# chown nmis:nmis openOpsgenie.py
# chmod 775 closeOpsgenie.py

...