pyzabbix ZabbixAPI creating http agent item - preprocessing params

38 Views Asked by At

I'm seeking help with the creation of a http agent item with pyzabbix on a existing template. This is the python function I came up with so far:

def create_http_agent_item_on_template(self, template_name, item_name, item_key, item_url):
        """
        Creates an HTTP agent item on a specified template.

        Args:
            template_name (str): Name of the template where the item will be created.
            item_name (str): Name of the item.
            item_key (str): Key of the item.
            item_url (str): URL to be monitored.
        """
        template_id = self._get_template_id_by_name(template_name)

        try:
            preprocessing_params = {
                "type": "5",  # Regular expression
                "params": "HTTP/.* (\\d{3}) .*\n\\1",  # Multiple parameters separated by newline
                "error_handler": "0",  # No error handler
                "error_handler_params": ""
            }

            item_id = self.zapi.item.create(
                name=item_name,
                key_=item_key,
                type=19,  # HTTP agent
                url=item_url,
                hostid=template_id,
                value_type=3,  # Numeric (unsigned)
                delay="30s",  # Check every 30 seconds
                history="7d",  # Store history for 7 days
                follow_redirects=0,
                retrieve_mode=0,
                request_method=0,  # Specify the request method
                preprocessing=preprocessing_params
            )['itemids'][0]

            return item_id
        except ZabbixAPIException as e:
            print(f"Failed to create HTTP agent item on template: {e}")
            return None

but testing this function I get the following error:

('Error -32602: Invalid params., Incorrect value for field "params": second parameter is expected.', -32602)

while in the docs, below "Item prototype preprocessing" it says for providing multiple preprocessing params:

Multiple parameters are separated by the newline (\n) character.

I did separate the two params for the preprocessing regex and despite receive this error message:

"params": "HTTP/.* (\\d{3}) .*\n\\1"

Does anyone had the same experience and knows a solution to this? Thank you in advance!

0

There are 0 best solutions below