Icinga2 Hosts and Services 101

Config Location

In a default installation the configuration files for hosts and services can be found in /etc/icinga2/conf.d/. For every hostgroup I create a single config file.

Reload and Check

#Check if the configuration is parsable
user@localhost:~$ sudo service icinga2 checkconfig
#Reload the changed config
user@localhost:~$ sudo service icinga2 reload

Hostgroup; Host; Service

The Icinga2 logic of objects:

  • A Service can be monitored and parametrized.
  • A Service is assigned to a Host which has an address etc.
  • A Host can be a member of a HostGroup

Defining a HostGroup

object HostGroup "RaspberryPI" {
  display_name = "Raspberry PI Cluster"
}

​​
Defining a Host and templating
You can define templates in Icinga2 which allows you to set default values for all hosts. In the following example every host will inherit the hostgroup RaspberryPI and the values from the template generic-host:

template Host "RaspberryPI-hosts" {
 import "generic-host"
 groups += [ "RaspberryPI" ]
}
​​

The hosts import the RaspberryPI-hosts template and adds the address using an IP:

object Host "TTRSS" {
  import "RaspberryPI-hosts"
  address = "192.168.3.44"
}

object Host "Mail" {
  import "RaspberryPI-hosts"
  address = "192.168.3.64"
}
​​​​

Service
To define a service you need the host and a check_command (Documentation). Additionally the generic-service template is imported. In the following example an http service is defined.

object Service "Frontpage" {
import "generic-service"
host_name = "TTRSS"
check_command = "http"

vars.sla = "24x7"                             //define sla 
vars.http_uri = "https://192.168.3.44/ttrss"  //check login page
vars.http_ignore_body = 1                     //just getting header.
vars.http_ssl = 1                             //using https
vars.http_warn_time = 1000ms                  //Warning if loading takes more than 1s
vars.http_critical_time = 3000ms              //Critical if loading takes more than 3s
}
​​​​

Advanced HTTP Service Configuration

Escaping special character
If you want to search in the http body for em>H”allo you would have to define something like this:

vars.string = "H"allo"
​​​​

Because this isn’t a valid configuration you have to escape the special character . This is done with \. Thus the correct config is:

vars.string = "H\"allo"
​​​​

Check result size of a http call

vars.http_pagesize = "40000:70000" /between 40kB and 70kB
​​​​

Complex string matching
To search for complex strings which span over more than one line you have set the switch http_linespan:

vars.http_linespan = 1
​​​​

The expected string:

"Status: OK

Message: XtraServer is OK"
​​​​

To search for this string you use a regex:​​

vars.http_expect_body_regex = "Status:.OK.*Message:.XtraServer.is.OK" 
​​​​

In the regex a dot is the equivalent for a single arbitrary character. “is.OK” expresses the need to have one character between “is” and “ok”.
“.*” is the equivalent for a multiple (>=1) arbitrary characters. Because i do not know how many character are between “OK” and “Message” (and is also not important here) “OK.*Message” is the right choise.

Find a specific string somewhere in the body

vars.http_linespan = 1
vars.string = "OK"
​​​​

Allow different possible http results
Example: The body can either contain “{“servicestatus”:3}” or “{“servicestatus”:2}”:
​​

vars.http_linespan = 1
vars.http_expect_body_regex = ".*servicestatus.*[23]"
​​​​

The amount of characters between servicestatus and the status code is not important, but it is expected that a 2 or 3 follows. To express you use the brackets []. If you would expect 6, 7 or 8 you have to write [678].

Advertisements

3 thoughts on “Icinga2 Hosts and Services 101

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s