Skip to content

Examples

Crapworks edited this page Dec 14, 2012 · 8 revisions

Here are some example api calls via Curl. Of course the response depends on your configuration.

Find Objects

Find all host objects

$ curl -H "content-type: application/json" 'http://admin:password@localhost:5000/host'
[
  {
    "check_command": "check-host-alive", 
    "name": "generic-host", 
    "notification_interval": "0", 
    "retain_nonstatus_information": "1", 
    "failure_prediction_enabled": "1", 
    "notifications_enabled": "1", 
    "contact_groups": "admins", 
    "retain_status_information": "1", 
    "notification_period": "24x7", 
    "event_handler_enabled": "1", 
    "process_perf_data": "1", 
    "max_check_attempts": "10", 
    "flap_detection_enabled": "1", 
    "notification_options": "d,u,r", 
    "register": "0"
  }, 
  {
    "alias": "localhost", 
    "use": "generic-host", 
    "host_name": "localhost", 
    "address": "127.0.0.1"
  }, 
  [...]
]

Get all hosts with testhost in the host_name field

$ curl -H "content-type: application/json" 'http://admin:password@localhost:5000/host?host_name=testhost'
[
  {
    "alias": "testhost2", 
    "use": "generic-host", 
    "host_name": "testhost2", 
    "address": "127.0.0.3"
  }, 
  {
    "alias": "testhost1", 
    "use": "generic-host", 
    "host_name": "testhost1", 
    "address": "127.0.0.2"
  }, 
  {
    "alias": "testhost3", 
    "use": "generic-host", 
    "host_name": "testhost3", 
    "address": "127.0.0.4"
  }
]

Create Objects

$ cat /tmp/hosts
[
  {
    "alias": "newhost1", 
    "use": "generic-host", 
    "host_name": "newhost1", 
    "address": "127.0.0.101"
  }, 
  {
    "alias": "newhost2", 
    "use": "generic-host", 
    "host_name": "newhost2", 
    "address": "127.0.0.102"
  }, 
  {
    "alias": "newhost3", 
    "use": "generic-host", 
    "host_name": "newhost3", 
    "address": "127.0.0.103"
  }
]
$ curl -X POST -d @/tmp/hosts -H "content-type: application/json" 'http://admin:password@localhost:5000/host'
{
  "results": [
    {
      "200": "successfully stored host object: newhost1"
    },
    {
      "200": "successfully stored host object: newhost2"
    },
    {
      "200": "successfully stored host object: newhost3"
    }
  ],
  "summary": {
    "failed": 0,
    "total": 3,
    "succeeded": 3
  }
}
$ curl -H "content-type: application/json" 'http://admin:password@localhost:5000/host?host_name=new'
[
  {
    "alias": "newhost3", 
    "use": "generic-host", 
    "host_name": "newhost3", 
    "address": "127.0.0.103"
  }, 
  {
    "alias": "newhost1", 
    "use": "generic-host", 
    "host_name": "newhost1", 
    "address": "127.0.0.101"
  }, 
  {
    "alias": "newhost2", 
    "use": "generic-host", 
    "host_name": "newhost2", 
    "address": "127.0.0.102"
  }
]

Update Objects

$ cat /tmp/host-update
[
  {
    "alias": "newhost1", 
    "use": "generic-host", 
    "host_name": "newhost1", 
    "_MAC": "11:11:11:11:11:11",
    "address": "127.0.0.101"
  }, 
  {
    "alias": "newhost2", 
    "use": "generic-host", 
    "host_name": "newhost2", 
    "_MAC": "22:22:22:22:22:22",
    "address": "127.0.0.102"
  }, 
  {
    "alias": "newhost3", 
    "use": "generic-host", 
    "host_name": "newhost3", 
    "_MAC": "33:33:33:33:33:33",
    "address": "127.0.0.103"
  }
]
$ curl -X POST -d @/tmp/hosts-update -H "content-type: application/json" 'http://admin:password@localhost:5000/host'
{
  "results": [
    {
      "200": "successfully stored host object: newhost1"
    },
    {
      "200": "successfully stored host object: newhost2"
    },
    {
      "200": "successfully stored host object: newhost3"
    }
  ],
  "summary": {
    "failed": 0,
    "total": 3,
    "succeeded": 3
  }
}
$ curl -H "content-type: application/json" 'http://admin:password@localhost:5000/host?host_name=new'
[
  {
    "alias": "newhost3", 
    "use": "generic-host", 
    "_MAC": "33:33:33:33:33:33", 
    "host_name": "newhost3", 
    "address": "127.0.0.103"
  }, 
  {
    "alias": "newhost1", 
    "use": "generic-host", 
    "_MAC": "11:11:11:11:11:11", 
    "host_name": "newhost1", 
    "address": "127.0.0.101"
  }, 
  {
    "alias": "newhost2", 
    "use": "generic-host", 
    "_MAC": "22:22:22:22:22:22", 
    "host_name": "newhost2", 
    "address": "127.0.0.102"
  }
]

Delete Objetcs

Delete all host objects with testhost in the host_name field

$ curl -X DELETE -H "content-type: application/json" 'http://admin:password@localhost:5000/host?host_name=testhost'
{
  "results": [
    {
      "200": "successfully deleted host object: testhost2"
    },
    {
      "200": "successfully deleted host object: testhost1"
    },
    {
      "200": "successfully deleted host object: testhost3"
    }
  ],
  "summary": {
    "failed": 0,
    "total": 3,
    "succeeded": 3
  }
}

Reload Monitoring Core

$ curl -X POST -H "content-type: application/json" 'http://admin:password@localhost:5000/control?restart'
{
      "result": "successfully sent command to command file"
}

Verify Configuration

Just warnings

curl -X POST -H "content-type: application/json" 'http://admin:password@localhost:5000/control?verify'
{
  "output": {
    "Total Warnings": "3",
    "Total Errors": "0",
    "Warning": [
      "Host 'newhost1' has no services associated with it!",
      "Host 'newhost2' has no services associated with it!",
      "Host 'newhost3' has no services associated with it!"
    ]
  },
  "returncode": 0

Fatal Errors

curl -X POST -H "content-type: application/json" 'http://admin:password@localhost:5000/control?verify'
{
  "output": {
    "Error": [
      "Could not find any host matching 'localhost' (config file '/etc/icinga/objects/hostgroups_icinga.cfg', starting on line 25)",
      "Could not expand members specified in hostgroup (config file '/etc/icinga/objects/hostgroups_icinga.cfg', starting on line 25)"
    ]
  },
  "returncode": 254
Clone this wiki locally