ansible-collections / community.zabbix

Zabbix Ansible modules
http://galaxy.ansible.com/community/zabbix
Other
322 stars 284 forks source link

Update community.zabbix.zabbix_mediatype Documentation #1349

Closed djdejawu closed 3 months ago

djdejawu commented 3 months ago
SUMMARY

Create a new E-mail notification

ISSUE TYPE
COMPONENT NAME

community.zabbix.zabbix_mediatype

community.zabbix                         3.0.4
ANSIBLE VERSION
ansible [core 2.16.6]
  config file = /home/dejawu/git/ansible_zabbix/ansible.cfg
  configured module search path = ['/home/dejawu/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/dejawu/git/ansible_zabbix/venv/lib64/python3.12/site-packages/ansible
  ansible collection location = /home/dejawu/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/dejawu/git/ansible_zabbix/venv/bin/ansible
  python version = 3.12.4 (main, Jun  7 2024, 00:00:00) [GCC 13.3.1 20240522 (Red Hat 13.3.1-1)] (/home/dejawu/git/ansible_zabbix/venv/bin/python3)
  jinja version = 3.1.3
  libyaml = True
CONFIGURATION
CONFIG_FILE() = /home/dejawu/git/ansible_zabbix/ansible.cfg
EDITOR(env: EDITOR) = /usr/bin/nano
HOST_KEY_CHECKING(/home/dejawu/git/ansible_zabbix/ansible.cfg) = False
OS / ENVIRONMENT / Zabbix Version

Debian 11, Zabbix 6.0.30

STEPS TO REPRODUCE
    - name: Add E-mail notification
      community.zabbix.zabbix_mediatype:
        state: present
        name: E-mail
        type: email
        smtp_server: mx.email.com
        smtp_server_port: 587
        smtp_security: STARTTLS
        smtp_authentication: true
        smtp_email: example@email.com
        username: example@email.com
        password: Password
        content_type: plaintext
        message_templates:
          - subject: "Problem: {EVENT.NAME}"
            body: |
              "Problem started at {EVENT.TIME} on {EVENT.DATE}
              Problem name: {EVENT.NAME}
              Host: {HOST.NAME}
              Severity: {EVENT.SEVERITY}
              Operational data: {EVENT.OPDATA}
              Original problem ID: {EVENT.ID}>
              {TRIGGER.URL}"
          - subject: "Resolved in {EVENT.DURATION}: {EVENT.NAME}"
            body: |
              "Problem has been resolved at {EVENT.RECOVERY.TIME} on {EVENT.RECOVERY.DATE}
              Problem name: {EVENT.NAME}
              Problem duration: {EVENT.DURATION}
              Host: {HOST.NAME}
              Severity: {EVENT.SEVERITY}
              Original problem ID: {EVENT.ID}
              {TRIGGER.URL}"
            recovery: recovery_operations
            eventsource: triggers
          - subject: "Updated problem in {EVENT.AGE}: {EVENT.NAME}"
            body: |
              "{USER.FULLNAME} {EVENT.UPDATE.ACTION} problem< at {EVENT.UPDATE.DATE} {EVENT.UPDATE.TIME}.
              {EVENT.UPDATE.MESSAGE}

              <Current problem status: {EVENT.STATUS}
              Age: {EVENT.AGE}
              Acknowledged: {EVENT.ACK.STATUS}."
            recovery: update_operations
            eventsource: triggers
EXPECTED RESULTS

Message templates in media type

ACTUAL RESULTS

type error

TASK [Add E-mail notification] *************************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "msg": "connection error occurred: REST API returned {'code': -32602, 'message': 'Invalid params.', 'data': 'Invalid parameter \"/1/message_templates/1/eventsource\": an integer is expected.'} when sending {\"jsonrpc\": \"2.0\", \"method\": \"mediatype.update\", \"id\": \"e5f9731c-a04b-4f60-94c1-9fd69d9cebda\", \"params\": {\"mediatypeid\": \"39\", \"message_templates\": [{\"eventsource\": null, \"recovery\": null, \"subject\": \"Problem: {EVENT.NAME}\", \"message\": \"\\\"Problem started at {EVENT.TIME} on {EVENT.DATE}\\nProblem name: {EVENT.NAME}\\nHost: {HOST.NAME}\\nSeverity: {EVENT.SEVERITY}\\nOperational data: {EVENT.OPDATA}\\nOriginal problem ID: {EVENT.ID}>\\n{TRIGGER.URL}\\\"\\n\"}, {\"eventsource\": \"0\", \"recovery\": \"1\", \"subject\": \"Resolved in {EVENT.DURATION}: {EVENT.NAME}\", \"message\": \"\\\"Problem has been resolved at {EVENT.RECOVERY.TIME} on {EVENT.RECOVERY.DATE}\\nProblem name: {EVENT.NAME}\\nProblem duration: {EVENT.DURATION}\\nHost: {HOST.NAME}\\nSeverity: {EVENT.SEVERITY}\\nOriginal problem ID: {EVENT.ID}\\n{TRIGGER.URL}\\\"\\n\"}, {\"eventsource\": \"0\", \"recovery\": \"2\", \"subject\": \"Updated problem in {EVENT.AGE}: {EVENT.NAME}\", \"message\": \"\\\"{USER.FULLNAME} {EVENT.UPDATE.ACTION} problem< at {EVENT.UPDATE.DATE} {EVENT.UPDATE.TIME}.\\n{EVENT.UPDATE.MESSAGE}\\n\\n<Current problem status: {EVENT.STATUS}\\nAge: {EVENT.AGE}\\nAcknowledged: {EVENT.ACK.STATUS}.\\\"\\n\"}], \"content_type\": \"0\"}, \"auth\": \"619067eea891ed8d4894950d10fe49d8\"}"}
pyrodie18 commented 3 months ago

Yep looks like you're right. The code doesn't handle not having an eventsource set for some reason even though the documentation says its not required.

        if self._module.params["message_templates"]:
            msg_templates = []
            for template in self._module.params["message_templates"]:
                msg_templates.append(dict(
                    eventsource={
                        "triggers": "0",
                        "discovery": "1",
                        "autoregistration": "2",
                        "internal": "3"}.get(template["eventsource"]),
                    recovery={
                        "operations": "0",
                        "recovery_operations": "1",
                        "update_operations": "2"}.get(template["recovery"]),
                    subject=template["subject"],
                    message=template["body"]
                ))
            parameters.update(dict(message_templates=msg_templates))

Kinda surprised they never got caught before.

pyrodie18 commented 3 months ago

OK I stand corrected. We have a problem with documentation, not the code. Looking at the actual API documentation for Zabbix (https://www.zabbix.com/documentation/current/en/manual/api/reference/mediatype/object#message-template), all message templates need to have an eventsource and a recovery parameter defined.

djdejawu commented 3 months ago

So, in fact:

eventsource: triggers
recovery: operations

this two attributes is required.

You need to update documents, because this is misunderstanding.