OpsGenie Python API

The OpsGenie SDK for Python provides a set of Python API for OpsGenie services, making it easier for Python developers to build applications that integrate with OpsGenie. Developers can build Python applications on top of APIs that take the complexity out of coding directly against a web service interface. The library provides APIs that hide much of the lower-level plumbing, including authentication, request retries, and error handling.

Before you begin, you must sign up for OpsGenie service, create an API Integration and get your API key. In order to use the OpsGenie Python SDK, you will need the API key from the API Integration you created. The API key is used to authenticate requests to the service and identify yourself as the sender of a request.

Python SDK Installation

Opsgenie Python SDK requires Python SDK to be installed. You can install from Python website. Following that the API source can be downloaded via command pip install opsgenie-sdk. The command will automatically download and install necessary package files and dependencies.

OpsGenie Python Sdk source code is available at GitHub OpsGenie Python Sdk repository.

Client Initialization

Opsgenie client is the basic data structure on which all operations and actions are taken. A client variable can be created as following.

          #import statements
          from opsgenie import OpsGenie
          from opsgenie.config import Configuration

          config = Configuration(apikey="YOUR_API_KEY")
          client = OpsGenie(config)
          

It is required to set the API key parameter to execute Opsgenie Web Api calls using client. Otherwise the authentication will fail and the API is going to raise an exception.

Alert API

Create Alert

            #import statements
            from opsgenie.alert.requests import CreateAlertRequest
            from opsgenie.errors import OpsGenieError

            try:
                request = CreateAlertRequest(
                                    message="appserver1 down",
                                    description="cpu usage is over 60%",
                                    source="nagios",
                                    entity="appserver1",
                                    actions=["ping", "restart"],
                                    tags=["network", "operations"],
                                    recipients=["john.smith@acme.com", "admin@acme.com"])
                response = client.alert.create_alert(request)

                print "message: ", (response.message)
                print "alert id: ", (response.alert_id)
                print "status: ", (response.status)
                print "code: ", (response.code)
            except OpsGenieError as err:
                print "[ERROR]", err.message
          

Close Alert

            #import statements
            from opsgenie.alert.requests import CloseAlertRequest
            from opsgenie.errors import OpsGenieError

            try:
                request = CloseAlertRequest(id="alert id here")
                close_alert_response = client.alert.close_alert(request)

                print "status: ", (close_alert_response.status)
                print "code: ", (close_alert_response.code)
            except OpsGenieError as err:
                print "[ERROR]", err.message
          

Delete Alert

            #import statements
            from opsgenie.alert.requests import DeleteAlertRequest
            from opsgenie.errors import OpsGenieError

            try:
                request = DeleteAlertRequest(id="alert id here")
                delete_alert_response = client.alert.delete_alert(request)

                print "status: ", (delete_alert_response.status)
                print "code: ", (delete_alert_response.code)
            except OpsGenieError as err:
                print "[ERROR]", err.message
          

Get Alert

            #import statements
            from opsgenie.alert.requests import GetAlertRequest
            from opsgenie.utility import list_to_str
            from opsgenie.errors import OpsGenieError

            try:
                request = GetAlertRequest(id="alert id here")
                get_alert_response = client.alert.get_alert(request)

                print "tags: ", (list_to_str(get_alert_response.tags))
                print "count: ", (get_alert_response.count)
                print "teams: ", (list_to_str(get_alert_response.teams))
                print "recipients: ", (list_to_str(get_alert_response.recipients))
                print "tiny id: ", (get_alert_response.tiny_id)
                print "alias: ", (get_alert_response.alias)
                print "entity: ", (get_alert_response.entity)
                print "id: ", (get_alert_response.id)
                print "updated at: ", (get_alert_response.updated_at)
                print "message: ", (get_alert_response.message)
                print "details: ", (get_alert_response.details)
                print "source: ", (get_alert_response.source)
                print "description: ", (get_alert_response.description)
                print "created at: ", (get_alert_response.created_at)
                print "is seen: ", (get_alert_response.is_seen)
                print "acknowledged: ", (get_alert_response.acknowledged)
                print "owner: ", (get_alert_response.owner)
                print "system data: ", (get_alert_response.system_data)
                print "actions: ", (list_to_str(get_alert_response.actions))
            except OpsGenieError as err:
                print "[ERROR]", err.message
          

List Alerts

            #import statements
            from opsgenie.alert.requests import ListAlertsRequest
            from opsgenie.errors import OpsGenieError

            try:
                list_alerts_response = client.alert.list_alerts(ListAlertsRequest())

                for alert in list_alerts_response.alerts:
                    print "Id: ", alert.id
                    print "Alias: ", alert.alias
                    print "Message: ", alert.message
                    print "Status: ", alert.status
                    print "IsSeen: ", alert.is_seen
                    print "Acknowledged: ", alert.acknowledged
                    print "Created at: ", alert.created_at
                    print "Updated at: ", alert.updated_at
                    print "Tiny id: ", alert.tiny_id
                    print "Owner: ", alert.owner
                    print "------------------"
            except OpsGenieError as err:
                print "[ERROR]", err.message
          

Count Alerts

            #import statements
            from opsgenie.alert.requests import CountAlertsRequest
            from opsgenie.errors import OpsGenieError

            try:
                request = CountAlertsRequest()
                count_alert_response = client.alert.count_alerts(request)

                print "status: ", (count_alert_response.status)
                print "code: ", (count_alert_response.code)
            except OpsGenieError as err:
                print "[ERROR]", err.message
          

List Alert Logs

            #import statements
            from opsgenie.alert.requests import ListAlertLogsRequest
            from opsgenie.errors import OpsGenieError

            try:
                request = ListAlertLogsRequest("alert id here")
                list_logs_response = client.alert.list_alert_logs(request)

                print "Logs:"
                for log in list_logs_response.logs:
                    print "Owner: ", (log.owner)
                    print "Log: ", (log.log)
                    print "Log Type: ", (log.log_type)
                    print "Created At: ", (log.created_at)
                    print "------------------"
            except OpsGenieError as err:
                print "[ERROR]", err.message
          

List Alert Notes

            #import statements
            from opsgenie.alert.requests import ListAlertNotesRequest
            from opsgenie.errors import OpsGenieError

            try:
                request = ListAlertNotesRequest("alert id here")
                list_notes_response = client.alert.list_alert_notes(request)

                print "Last Key: ", (list_notes_response.last_key)
                print "Notes:"
                for note in list_notes_response.notes:
                    print "Note: ", (note.note)
                    print "Owner: ", (note.owner)
                    print "Created At: ", (note.created_at)
                    print "------------------"
            except OpsGenieError as err:
                print "[ERROR]", err.message
          

List Alert Recipients

            #import statements
            from opsgenie.alert.requests import ListAlertRecipientsRequest
            from opsgenie.utility import list_to_str
            from opsgenie.errors import OpsGenieError

            try:
                request = ListAlertRecipientsRequest("alert id here")
                list_recipients_response = client.alert.list_alert_recipients(request)

                print "Users: ", (list_to_str(map(lambda u: u.username, list_recipients_response.users)))
                print "Groups: ", (list_to_str(map(lambda u: u.username, list_recipients_response.groups)))
            except OpsGenieError as err:
                print "[ERROR]", err.message
          

Acknowledge

            #import statements
            from opsgenie.alert.requests import AcknowledgeAlertRequest
            from opsgenie.errors import OpsGenieError

            try:
                request = AcknowledgeAlertRequest(id="alert id here")
                ack_response = client.alert.acknowledge_alert(request)

                print "status: ", (ack_response.status)
                print "code: ", (ack_response.code)
            except OpsGenieError as err:
                print "[ERROR]", err.message
          

UnAcknowledge

                #import statements
                from opsgenie.alert.requests import UnAcknowledgeAlertRequest
                from opsgenie.errors import OpsGenieError

                try:
                    request = UnAcknowledgeAlertRequest(id="alert id here")
                    unAck_response = client.alert.unAcknowledge_alert(request)

                    print "status: ", (unAck_response.status)
                    print "code: ", (unAck_response.code)
                except OpsGenieError as err:
                    print "[ERROR]", err.message
          

Snooze

            #import statements
            from opsgenie.alert.requests import SnoozeAlertRequest
            from opsgenie.errors import OpsGenieError

            try:
                request = SnoozeAlertRequest(id="alert id here")
                snooze_response = client.alert.snooze_alert(request)

                print "status: ", (snooze_response.status)
                print "code: ", (snooze_response.code)
            except OpsGenieError as err:
                print "[ERROR]", err.message
          

Renotify

            #import statements
            from opsgenie.alert.requests import RenotifyAlertRequest
            from opsgenie.errors import OpsGenieError

            try:
                request = RenotifyAlertRequest(id="alert id here")
                renotify_response = client.alert.renotify_alert(request)

                print "status: ", (renotify_response.status)
                print "code: ", (renotify_response.code)
            except OpsGenieError as err:
                print "[ERROR]", err.message
          

Take Ownership

            #import statements
            from opsgenie.alert.requests import TakeOwnershipOfAlertRequest
            from opsgenie.errors import OpsGenieError

            try:
                request = TakeOwnershipOfAlertRequest(id="alert id here")
                renotify_response = client.alert.take_ownership_of_alert(request)

                print "status: ", (renotify_response.status)
                print "code: ", (renotify_response.code)
            except OpsGenieError as err:
                print "[ERROR]", err.message
          

Assign Owner

            #import statements
            from opsgenie.alert.requests import AssignOwnerToAlertRequest
            from opsgenie.errors import OpsGenieError

            try:
                request = AssignOwnerToAlertRequest(id="alert id here", owner="owner here")
                assign_owner_response = client.alert.assign_owner_to_alert(request)

                print "status: ", (assign_owner_response.status)
                print "code: ", (assign_owner_response.code)
            except OpsGenieError as err:
                print "[ERROR]", err.message
          

Add Team

            #import statements
            from opsgenie.alert.requests import AddTeamToAlertRequest
            from opsgenie.errors import OpsGenieError

            try:
                request = AddTeamToAlertRequest(id="alert id here", team="team name here")
                add_team_response = client.alert.add_team_to_alert(request)

                print "status: ", (add_team_response.status)
                print "code: ", (add_team_response.code)
            except OpsGenieError as err:
                print "[ERROR]", err.message
          

Add Recipient

            #import statements
            from opsgenie.alert.requests import AddRecipientToAlertRequest
            from opsgenie.errors import OpsGenieError

            try:
                request = AddRecipientToAlertRequest(id="alert id here", recipient="recipient here")
                add_recipient_response = client.alert.add_recipient_to_alert(request)

                print "status: ", (add_recipient_response.status)
                print "code: ", (add_recipient_response.code)
            except OpsGenieError as err:
                print "[ERROR]", err.message
          

Add Note

            #import statements
            from opsgenie.alert.requests import AddNoteToAlertRequest
            from opsgenie.errors import OpsGenieError

            try:
                request = AddNoteToAlertRequest("alert id here", note="note here")
                add_note_response = client.alert.add_note_to_alert(request)

                print "[Add note] ", (add_note_response.status), (add_note_response.code)
            except OpsGenieError as err:
                print "[ERROR]", err.message
          

Add Tags

            #import statements
            from opsgenie.alert.requests import AddTagsToAlertRequest
            from opsgenie.errors import OpsGenieError

            try:
                request = AddTagsToAlertRequest(id="alert id here", tags=["tag1 here", "tag2 here"])
                add_tags_response = client.alert.add_tags_to_alert(request)

                print "status: ", (add_tags_response.status)
                print "code: ", (add_tags_response.code)
            except OpsGenieError as err:
                print "[ERROR]", err.message
          

Remove Tags

            #import statements
            from opsgenie.alert.requests import RemoveTagsFromAlertRequest
            from opsgenie.errors import OpsGenieError

            try:
                request = RemoveTagsFromAlertRequest(id="alert id here", tags=["tag1 here", "tag2 here"])
                remove_tags_response = client.alert.remove_tags_from_alert(request)

                print "status: ", (remove_tags_response.status)
                print "code: ", (remove_tags_response.code)
            except OpsGenieError as err:
                print "[ERROR]", err.message
          

Add Details

            #import statements
            from opsgenie.alert.requests import AddDetailsToAlertRequest
            from opsgenie.errors import OpsGenieError

            try:
                request = AddDetailsToAlertRequest(id="alert id here", details={'key1': 'value1', 'key2': 'value2'})
                add_details_response = client.alert.add_details_to_alert(request)

                print "status: ", (add_details_response.status)
                print "code: ", (add_details_response.code)
            except OpsGenieError as err:
                print "[ERROR]", err.message
          

Remove Details

            #import statements
            from opsgenie.alert.requests import RemoveDetailsFromAlertRequest
            from opsgenie.errors import OpsGenieError

            try:
                request = RemoveDetailsFromAlertRequest(id="alert id here", keys=["key1", "key2"])
                remove_details_response = client.alert.remove_details_to_alert(request)

                print "status: ", (remove_details_response.status)
                print "code: ", (remove_details_response.code)
            except OpsGenieError as err:
                print "[ERROR]", err.message
          

Execute Action

            #import statements
            from opsgenie.alert.requests import ExecuteActionOfAlertRequest
            from opsgenie.errors import OpsGenieError

            try:
                request = ExecuteActionOfAlertRequest(id=r"alert id here", action="action to execute")
                execute_action_response = client.alert.execute_action_of_alert(request)

                print "status: ", (execute_action_response.status)
                print "code: ", (execute_action_response.code)
            except OpsGenieError as err:
                print "[ERROR]", err.message
          

Attach File

            #import statements
            from opsgenie.alert.requests import AttachFileToAlertRequest
            from opsgenie.errors import OpsGenieError

            try:
                request = AttachFileToAlertRequest(id="alert id here", attachment="file path here")
                attach_file_response = client.alert.attach_file_to_alert(request)

                print "status: ", (attach_file_response.status)
                print "code: ", (attach_file_response.code)
            except OpsGenieError as err:
                print "[ERROR]", err.message
          

Escalate To Next Request

                #import statements
                from opsgenie.alert.requests import EscalateToNextAlertRequest
                from opsgenie.errors import OpsGenieError

                try:
                    request = EscalateToNextAlertRequest(id="alert id here", escalation_id="escalation id here")
                    escalate_to_next_response = client.alert.attach.escalate_to_next(request)

                    print "status: ", (escalate_to_next_response.status)
                    print "code: ", (escalate_to_next_response.code)
                except OpsGenieError as err:
                    print "[ERROR]", err.message
          

Heartbeat API

(Will be avaliable soon)

Policy API

(Will be avaliable soon)

Integration API

(Will be avaliable soon)

Connection Configuration

Proxy configuration

Users can set the enlisted parameters if they would like to connect through a proxy server.
  • Host
  • Port
  • Username
  • Password
  • Protocol (http or https)
The configuration is achieved via setting a proxy data structure with the above given parameters and client. Here is an example of how to configure the OpsGenie client.
            # initialize client with proxy config / you can give ProxyConfiguration object or dict to configuration param
            config = Configuration(apikey="api key here",
                                   proxy_config={'host': '10.10.10.10', 'port': 8080, 'protocol': 'http'})
            client = OpsGenie(config)
          

Http transport layer configuration

It is also possible to modify a few Http transport layer settings. They include:
  • Connect timeout
  • Read timeout
  • Maximum number of retries when the requests fallback
            # initialize client with http config / you can give HttpConfiguration object or dict to configuration param
            config = Configuration(apikey="api key here",
                                   http_config={"connect_timeout": 30, 'read_timeout': 20, 'max_retry': 3})
            client = OpsGenie(config)