Created
February 8, 2022 23:18
-
-
Save jaychakra/fcf0d779ee1c7ae973e0ecfed19455bb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This script copies alerts from one project to another in MongoDB opsManager | |
# To install dependencies | |
# run pip install requests | |
# then python copyAlerts.py | |
import json | |
import requests | |
from requests.auth import HTTPDigestAuth | |
sourcePublicKey = "sourcePublicKey" | |
sourcePrivateKey = "sourcePrivateKey" | |
sourceProjectId = "sourceProjectId" | |
destinationPublicKey = "destinationPublicKey" | |
destinationPrivateKey = "destinationPrivateKey" | |
destinationProjectId = "destinationProjectId" | |
def get_alerts(): | |
url = "https://opsmgr.tools.struxurewarecloud.com:8443/api/public/v1.0/groups/" + sourceProjectId + "/alertConfigs" | |
response = requests.get(url, auth=HTTPDigestAuth(sourcePublicKey, sourcePrivateKey)) | |
alerts = [] | |
if response.status_code == 200: | |
json_response = json.loads(response.text)['results'] | |
for alert in json_response: | |
del alert['created'] | |
del alert['groupId'] | |
del alert['links'] | |
del alert['typeName'] | |
del alert['updated'] | |
if 'typeName' in alert.keys(): | |
del alert['typeName'] | |
# print(alert) | |
alerts.append(alert) | |
return alerts | |
else: | |
print("Error Occurred. Response Code: ", response.status_code) | |
return | |
def post_alert(alert): | |
url = "https://opsmgr.tools.struxurewarecloud.com:8443/api/public/v1.0/groups/" + destinationProjectId + "/alertConfigs" | |
headers = {'Content-type': 'application/json'} | |
response = requests.post(url, auth=HTTPDigestAuth(destinationPublicKey, destinationPrivateKey), data=json.dumps(alert), headers=headers) | |
if response.status_code == 201: | |
print("Alert ", alert['eventTypeName'], " created successfully") | |
else: | |
print("Error Occurred. Error Code: ", response.status_code) | |
alerts = get_alerts() | |
for alert in alerts: | |
post_alert(alert) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment