Mute and Unmute Checks in Morpheus with Python

If you are using Morpheus for monitoring and you want to mute check in a Jenkins task or similar, here's a quick Python script to mute a task by name:

from pymorpheus import MorpheusClient
import os

# env vars from jenkins secrets
morpheusUrl = os.environ['morpheusUrl']
morpheusToken = os.environ['morpheusToken']
morpheusCheckName = os.environ['morpheusCheckName']

morpheus = MorpheusClient(morpheusUrl, token=morpheusToken)

checkResult = morpheus.call("get", path="/monitoring/checks", options=[("name", morpheusCheckName)])
if checkResult['meta']['total'] == 1:
    checkId = checkResult['checks'][0]['id']
elif checkResult['meta']['total'] == 0:
    print("Cannot find check ID by name")
    quit(1)
else:
    print("Ambigious search term, multiple checks found")
    quit(1)

disableCheckResult = morpheus.call("put", path="/monitoring/checks/%s/mute" % checkId, options=[("muted", "true")])
print(disableCheckResult)
quit()

# to unmute:
# enableCheckResult = morpheus.call("put", path="/monitoring/checks/%s/mute" % checkId, options=[("muted", "false")])
# print(disableCheckResult)
# quit()

You'll need to import the pymorpheus module which is available on PyPI.