Page 2 of 3

Re: Nagios XI + Ansible

Posted: Fri Mar 02, 2018 4:45 pm
by cdienger
Ah I see. Try removing the force option from the body then and use it in the url:

"url": "https://nagios.saq.qc.ca/nagiosxi/api/v ... EY&force=1"

Re: Nagios XI + Ansible

Posted: Mon Mar 05, 2018 2:56 pm
by bennyboy
cdienger wrote:Ah I see. Try removing the force option from the body then and use it in the url:

"url": "https://nagios.saq.qc.ca/nagiosxi/api/v ... EY&force=1"
I try it but I have the same problem.

Code: Select all

ok: [sldwbm0361 -> 127.0.0.1] => {
    "access_control_allow_methods": "*",
    "access_control_allow_orgin": "*",
    "changed": false,
    "connection": "close",
    "content": "{\"error\":\"Missing required variables\",\"missing\":[\"host_name\",\"service_description\",\"max_check_attempts\",\"check_interval\",\"retry_interval\",\"check_period\",\"notification_interval\",\"notification_period\",\"contacts OR contact_groups\"]}\n",
    "content_length": "230",
    "content_type": "application/json",
    "cookies": {},
    "date": "Mon, 05 Mar 2018 19:53:34 GMT",
    "invocation": {
        "module_args": {
            "attributes": null,
            "backup": null,
            "body": {
                "host_name": "sldwbm0361",
                "service_description": "Service Status: Filebeat",
                "use": "SAQ_Unix_nonprod_filebeat_services"
            },
            "body_format": "json",
            "client_cert": null,
            "client_key": null,
            "content": null,
            "creates": null,
            "delimiter": null,
            "dest": null,
            "directory_mode": null,
            "follow": false,
            "follow_redirects": "safe",
            "force": false,
            "force_basic_auth": false,
            "group": null,
            "headers": {
                "Content-Type": "application/json"
            },
            "http_agent": "ansible-httpget",
            "method": "POST",
            "mode": null,
            "owner": null,
            "regexp": null,
            "remote_src": null,
            "removes": null,
            "return_content": true,
            "selevel": null,
            "serole": null,
            "setype": null,
            "seuser": null,
            "src": null,
            "status_code": [
                "200"
            ],
            "timeout": 120,
            "unsafe_writes": null,
            "url": "https://nagios.saq.qc.ca/nagiosxi/api/v1/config/service?apikey=APIKEY&force=1",
            "url_password": null,
            "url_username": null,
            "use_proxy": true,
            "validate_certs": false
        }
    },
    "json": {
        "error": "Missing required variables",
        "missing": [
            "host_name",
            "service_description",
            "max_check_attempts",
            "check_interval",
            "retry_interval",
            "check_period",
            "notification_interval",
            "notification_period",
            "contacts OR contact_groups"
        ]
    },
    "msg": "OK (230 bytes)",
    "redirected": false,
    "server": "Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/5.4.16",
    "status": 200,
    "url": "https://nagios.saq.qc.ca/nagiosxi/api/v1/config/service?apikey=APIKEY&force=1",
    "x_powered_by": "PHP/5.4.16"
}

Re: Nagios XI + Ansible

Posted: Tue Mar 06, 2018 12:29 pm
by jomann
It looks like you are using a JSON body to pass the data. You should actually be passing the data as form variables into the endpoint. Typically in CURL (and with the URL you've given) we would do curl -XPOST <url> -d 'var1=thing&var2=thing2' which is what we want to mimic in ansible.

If you look at the examples on the ansible page, this is similar to what you'd want to copy:

Code: Select all

# Login to a form based webpage, then use the returned cookie to
# access the app in later tasks

- uri:
    url: https://your.form.based.auth.example.com/index.php
    method: POST
    body: "name=your_username&password=your_password&enter=Sign%20in"
    status_code: 302
    headers:
      Content-Type: "application/x-www-form-urlencoded"
  register: login
Basically you'd want to pass the body the same way it's passing it above. You should pass it like so (you may not need the header section):

Code: Select all

body: "host_name=host&service_description=service&use=template&force=1"
headers:
    Content-Type: "application/x-www-form-urlencoded"
I'd also make sure that you don't send the body as JSON too. So remove the body_format: json section also.

Re: Nagios XI + Ansible

Posted: Tue Mar 06, 2018 1:04 pm
by DoubleDoubleA
bennyboy wrote:Hello,

I would like to use Ansible to automate the addition of monitoring in Nagios XI.
Have you ever experienced this? If yes, do you have any examples?

For example, I wonder whether to use the uri module to interact with the NAgios XI API. If so how could I check if the monitoring already exists before adding it.

Thank you in advance for your advice.
Hi benny boy. I am also working on Ansible + Nagios XI API calls. I was going a little different direction, using a bash script rather than the uri module. I'm interested in your direction, though, and I'm starting to work on using uri.

You're a little bit ahead of me in this regard. I'm curious to see if the body_format change does the trick.

Re: Nagios XI + Ansible

Posted: Tue Mar 06, 2018 1:17 pm
by WillemDH
Hello all,

Just an FYI, Nagios Xi integration with Ansible is also on my to do... So +1 for all provided and future info on this.

Grtz

Willem

Re: Nagios XI + Ansible

Posted: Tue Mar 06, 2018 3:00 pm
by bennyboy
This is exactly what I use in my playbook. It's not working. I think Nagios XI don't see the force = 1 or uri module ignore force = 1.

Can you create nagiosxi-ansible or ansible-nagiosxi project under Nagios github?

Code: Select all

- name: Monitoring Filebeat Service
  uri:
    url: 'https://{{ nagios_server }}/nagiosxi/api/v1/config/service?apikey={{ nagios_api_key }}'
    body: 
      host_name: '{{ ansible_nodename }}'
      service_description: 'Service Status: Filebeat'
      use: '{{ nagios_filebeat_template }}'
      force: 1
    body_format: json
    headers:
      Content-Type: "application/json"
    status_code: 200
    method: POST
    return_content: yes
    validate_certs: no
    timeout: 120
  delegate_to: 127.0.0.1
- name: Apply Config on Nagios
  uri:
    url: 'https://{{ nagios_server }}/nagiosxi/api/v1/system/applyconfig?apikey={{ nagios_api_key }}'
    status_code: 200
    method: POST
    return_content: yes
    validate_certs: no
    headers:
      Content-Type: "application/json"
    timeout: 120
  delegate_to: 127.0.0.1
  run_once: true

Re: Nagios XI + Ansible

Posted: Tue Mar 06, 2018 4:16 pm
by DoubleDoubleA
bennyboy wrote:This is exactly what I use in my playbook. It's not working. I think Nagios XI don't see the force = 1 or uri module ignore force = 1.

Can you create nagiosxi-ansible or ansible-nagiosxi project under Nagios github?

Code: Select all

- name: Monitoring Filebeat Service
  uri:
    url: 'https://{{ nagios_server }}/nagiosxi/api/v1/config/service?apikey={{ nagios_api_key }}'
    body: 
      host_name: '{{ ansible_nodename }}'
      service_description: 'Service Status: Filebeat'
      use: '{{ nagios_filebeat_template }}'
      force: 1
    body_format: json
    headers:
      Content-Type: "application/json"
    status_code: 200
    method: POST
    return_content: yes
    validate_certs: no
    timeout: 120
  delegate_to: 127.0.0.1
- name: Apply Config on Nagios
  uri:
    url: 'https://{{ nagios_server }}/nagiosxi/api/v1/system/applyconfig?apikey={{ nagios_api_key }}'
    status_code: 200
    method: POST
    return_content: yes
    validate_certs: no
    headers:
      Content-Type: "application/json"
    timeout: 120
  delegate_to: 127.0.0.1
  run_once: true
I think what jomann was saying was that

Code: Select all

body_format
should NOT be json. What happens when you remove that line?

In theory what should happen is that then the body_format would be the default raw, and not json. What I notice about the example URLs on the Help page in the XI interface is that the curl has the -d option after the address. When I look at the man page for curl for the -d option, it is almost exactly like sending raw data.

Re: Nagios XI + Ansible

Posted: Tue Mar 06, 2018 4:23 pm
by jomann
DoubleDoubleA wrote:I think what jomann was saying was that

Code: Select all

body_format
should NOT be json. What happens when you remove that line?

In theory what should happen is that then the body_format would be the default raw, and not json. What I notice about the example URLs on the Help page in the XI interface is that the curl has the -d option after the address. When I look at the man page for curl for the -d option, it is almost exactly like sending raw data.
Yes you're correct, the raw data should not be sent as json, since it is raw input data, not json. The XI API can't use json input.

This should work:

Code: Select all

- name: Monitoring Filebeat Service
  uri:
    url: 'https://{{ nagios_server }}/nagiosxi/api/v1/config/service?apikey={{ nagios_api_key }}'
    body: 'host_name={{ ansible_nodename }}&service_description=Service Status: Filebeat&use={{ nagios_filebeat_template }}&force=1'
    status_code: 200
    method: POST
    return_content: yes
    validate_certs: no
    timeout: 120
  delegate_to: 127.0.0.1

Re: Nagios XI + Ansible

Posted: Tue Mar 06, 2018 4:56 pm
by bennyboy
What about a github repo to work together on that ?

Now I want to find a way to validate if the monitoring already existe before add. I will explorer couple option and update that thread. (Github is a better tool) :mrgreen:

This is working !!! Thank you!!!

Code: Select all

- name: Monitoring Filebeat Service
  uri:
    url: 'https://{{ nagios_server }}/nagiosxi/api/v1/config/service?apikey={{ nagios_api_key }}'
    body: 'host_name={{ ansible_nodename }}&service_description=Service Status: Filebeat&use={{ nagios_filebeat_template }}&force=1'
    status_code: 200
    method: POST
    return_content: yes
    validate_certs: no
    timeout: 120
  delegate_to: 127.0.0.1
- name: Apply Config on Nagios
  uri:
    url: 'https://{{ nagios_server }}/nagiosxi/api/v1/system/applyconfig?apikey={{ nagios_api_key }}'
    status_code: 200
    method: POST
    return_content: yes
    validate_certs: no
    headers:
      Content-Type: "application/json"
    timeout: 120
  delegate_to: 127.0.0.1
  run_once: true

Re: Nagios XI + Ansible

Posted: Tue Mar 06, 2018 5:58 pm
by DoubleDoubleA
Nice! Glad it is working.

What do you think of the run_once benny boy?

My challenge is for use on, for example, a playbook that might bulk register hosts and services, is how the run_once will actually function. What I want is to apply configs once all the hosts and services have hit the CCM, right? I don't want to restart Core for every host, or (as I read how run_once works), restart Core on the first run through, but then not again. But maybe I don't understand it correctly.

Right now I the best idea I have to is run a standard API call from the ansible control machine's command line to apply configs after running the playbook.

Any other thoughts on that?