0

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 )

  • 1
    Does this help: https://stackoverflow.com/questions/45570779/python-handling-newlines-in-json-load-vs-json-loads ? – D Malan Jan 09 '23 at 23:57
  • you know Zabbix can run scripts when a Problem triggers, right? there's no need to call Rundeck https://www.zabbix.com/documentation/current/en/manual/config/notifications/action/operation/remote_command – Iron Bishop Jan 10 '23 at 12:53
  • 1
    you do not need python to send webhooks from Zabbix, see https://www.zabbix.com/documentation/guidelines/en/webhooks – Iron Bishop Jan 10 '23 at 12:56
  • payload is a string, while data needs "A dictionary, list of tuples, bytes or a file object to send to the specified url" https://www.w3schools.com/python/ref_requests_post.asp – Iron Bishop Jan 10 '23 at 12:59

1 Answers1

0

I ended encoding the webhook function of Zabbix instead of using a script. Thank you guys for your help.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 03 '23 at 09:58