I'm trying to make my zabbix send a payload with trigger information to a rundeck server, so it runs a script based into the information to restart services and solve low level problems.
I'm facing a problem trying to parse the json to send it to rundeck, here's the code on my script @ zabbix server:
#!/usr/bin/env python3
import sys
import requests
import json
import logging
# set logging
#logging.basicConfig(filename='/tmp/script.log', level=logging.INFO)
# Webhook URL ( localhost is a example )
api_url = 'http://localhost:4440/api/41/webhook/rLT583Yb0lOkObrFvm6iUz3YjLKWqhal#webhook-listener'
# setting up zabbix payload
#payload = sys.stdin.read() ( couldnt make this work )
payload = '{"alertid": "666", "clock": "1615696071", "message": "Trigger: Server memory usage is too high\nTrigger status: PROBLEM\nTrigger severity: Warning\nTrigger URL: http://zabbix.example.com/tr_events.php?triggerid=11839&eventid=23945\n\nItem values:\n\n1. Memory usage is 85.61% (sign: greater than) (threshold: 70.00%)", "sendto": "admin@example.com", "subject": "Zabbix server: {TRIGGER.NAME}", "eventid": "12345", "hostname": "Testing Variables"}'
# Loading the JSON ( already tried the methods below )
#data = dict(payload)
#data = json.loads(payload)
# Validando o payload
if 'hostname' not in data or not isinstance(data['hostname'], str):
logging.error('Payload com valor hostname inválido')
sys.exit(1)
if 'alertid' not in data or not isinstance(data['event_id'], str):
logging.error('Payload com valor event_id invalido')
sys.exit(1)
# Sorting payload fields
hostname = data['hostname']
event_id = data['alertid']
# setting trigger data as data dictionary ( rundeck data )
trigger_data = {
'hostname': hostname,
'event_id': event_id,
}
# Saving payload as txt to validate
#with open('/tmp/payload.txt', 'w') as f:
#f.write(payload)
# Set up headers
headers = {'Content-Type': 'application/json'}
# Manda o Post
response = requests.post(api_url, data=payload, headers=headers)
# Logando a resposta
logging.info(response.text)
# Print
print(response.text)
This script should bring trigger data through a payload into rundeck, so rundeck understands the payload and read the argument, so i can base my job on the 'hostname' variable ( that zabbix sends ).
I'm getting json parsing error when i run it
json.decoder.JSONDecodeError: Invalid control character at: line 1 column 95 (char 94)
I'm loosing hair, i already tried other ways to parse this json ( ast and dict ), but not seems to work.
already tried other ways to parse the json already sent the data via the payload directly as text ( json validated )