Skip to content

Instantly share code, notes, and snippets.

@elpatron68
Forked from djtecha/notify_cachet.py
Created April 12, 2019 14:14
Show Gist options
  • Save elpatron68/b04302866971c72c25f79b4cc39f15a7 to your computer and use it in GitHub Desktop.
Save elpatron68/b04302866971c72c25f79b4cc39f15a7 to your computer and use it in GitHub Desktop.
#!/platform/python/bin/python
import datetime
from dateutil.parser import parse as parse_date
import sys
import cachetclient.cachet as cachet
import json
ENDPOINT = 'http://status/api/v1'
API_TOKEN = '<%= @cachethq_key %>'
#Get Conponents and incidents
components = cachet.Components(endpoint=ENDPOINT, api_token=API_TOKEN)
incidents = cachet.Incidents(endpoint=ENDPOINT, api_token=API_TOKEN)
#EX INPUT
# SCRIPT 'master-commerce-1' 'My nagios service' CRITICAL HARD 'The service is CRITZZ'
SERVER = sys.argv[1]
SERVICE = sys.argv[2]
STATE = sys.argv[3]
STATE_LOCK = sys.argv[4]
DESCRIPTION = sys.argv[5]
if STATE == 'OK':
C_STATUS = 1
I_STATUS = 4
elif STATE == 'WARNING':
C_STATUS = 2
I_STATUS = 3
elif STATE == 'CRITICAL':
C_STATUS = 4
I_STATUS = 1
elif STATE == 'UNKNOWN':
C_STATUS = 2
I_STATUS = 1
try:
device = json.loads(components.get(name=SERVER))
id = device['data'][0]['id']
except Exception, e:
print "%s is not a valid component" % SERVER
sys.exit(1)
INCIDENT_NAME=SERVER + ' ' + SERVICE
#Clear Description if moving from OK state
if device['data'][0]['status'] and C_STATUS < 4:
UPDATED_DESCRIPTION = DESCRIPTION
else:
UPDATED_DESCRIPTION = device['data'][0]['description'] + '\n' + DESCRIPTION
#Only perform an action on HARD events
if STATE == 'CRITICAL' and STATE_LOCK == 'HARD':
components.put(id=id, status=C_STATUS, description=UPDATED_DESCRIPTION)
#Create the incident
new_incident = json.loads(incidents.post(name=INCIDENT_NAME,
message=DESCRIPTION,
status=I_STATUS))
elif STATE == 'OK' and STATE_LOCK == 'HARD':
components.put(id=id, status=C_STATUS, description=DESCRIPTION)
#Create the incident
all_incidents = json.loads(incidents.get())
for i in all_incidents['data']:
if i['name'] == INCIDENT_NAME:
incidents.put(id=i['id'],
message=DESCRIPTION,
status=I_STATUS)
elif STATE == 'WARNING' and STATE_LOCK == 'HARD':
components.put(id=id, status=C_STATUS, description=UPDATED_DESCRIPTION)
elif STATE == 'UNKNOWN' and STATE_LOCK == 'HARD':
components.put(id=id, status=C_STATUS, description=UPDATED_DESCRIPTION)
#Get incidents that are ok and remove them
all_incidents = json.loads(incidents.get())
for i in all_incidents['data']:
updated = parse_date(i['updated_at'])
if i['status'] == '4' and updated < datetime.datetime.now() - datetime.timedelta(minutes=30):
incidents.delete(id=i['id'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment