Project

General

Profile

Add New Checks to Icinga2 » History » Version 2

Jon Goldberg, 06/09/2017 04:20 AM

1 1 Jon Goldberg
To configure a new check in Icinga, you need 4 components:
2
3 2 Jon Goldberg
-   A **plugin** file.** ** This is a command-line executable (script or binary) that can be run independently of Icinga.  It should return an errorlevel of 0, 1, 2, or 3, corresponding to "OK", "Warning", "Critical", and "Unknown".  These reside in the plugin folder, which is `/usr/lib/nagios/plugins`.  These should always be testable on the command line.  E.g.:
4 1 Jon Goldberg
5
        root@rh5:/usr/lib/nagios/plugins# /usr/lib/nagios/plugins/check_dns -H jmaconsulting.biz -a 72.249.190.114
6
        DNS OK: 0.018 seconds response time. jmaconsulting.biz returns 72.249.190.114|time=0.018268s;;;0.000000
7
        root@rh5:/usr/lib/nagios/plugins# /usr/lib/nagios/plugins/check_dns -H jmaconsulting.biz -a 72.249.190.222
8
        DNS CRITICAL - expected '72.249.190.222' but got '72.249.190.114'
9
10 2 Jon Goldberg
-   A **CheckCommand** object.  This maps variables defined in Icinga to the command-line arguments the plugin requires.  CheckCommands can be anywhere in `/etc/icinga2/conf.d` (localhost only) or `/etc/icinga2/zones.d/global-templates` (distributed to all satellites).  Please use `icinga::/etc/icinga2/zones.d/global-templates/CheckCommands` for CheckCommands.   Place Icinga ships with many CheckCommands which are in `/usr/share/icinga2/include/command-plugins.conf`. CheckCommands can also define default values for arguments.  For instance, the check_dns plugin has a CheckCommand object as follows:
11 1 Jon Goldberg
12
{{collapse(Show example,Hide example)
13
14
        object CheckCommand "dns" {
15
                import "ipv4-or-ipv6"
16
17
                command = [ PluginDir + "/check_dns" ]
18
19
                arguments = {
20
                        "-H" = {
21
                                value = "$dns_lookup$"
22
                                description = "The name or address you want to query."
23
                        }
24
                        "-s" = {
25
                                value = "$dns_server$"
26
                                description = "Optional DNS server you want to use for the lookup."
27
                        }
28
                        "-a" = {
29
                                value = "$dns_expected_answers$"
30
                                description = "Optional ip address or host you expect the DNS server to return. Host must end with a dot (.). This option can be repeated multiple times (Returns OK if any value match). If multiple addresses are returned at once, you have to match the whole string of addresses separated with commas (sorted alphabetically)."
31
                        }
32
                        "-a_old" = {
33
                                key = "-a"
34
                                value ="$dns_expected_answer$"
35
                        }
36
                        "-A" = {
37
                                set_if = "$dns_authoritative$"
38
                        }
39
                        "-w" = {
40
                                value = "$dns_wtime$"
41
                                description = "Return warning if elapsed time exceeds value."
42
                        }
43
                        "-c" = {
44
                                value = "$dns_ctime$"
45
                                description = "Return critical if elapsed time exceeds value."
46
                        }
47
                        "-t" = {
48
                                value = "$dns_timeout$"
49
                                description = "Seconds before connection times out. Defaults to 10."
50
                        }
51
                }
52
53
                vars.dns_lookup = "$host.name$"
54
                vars.dns_expected_answer = "$check_address$"
55
                vars.dns_timeout = 10
56
        }
57
}}
58
59 2 Jon Goldberg
-   A **Service** object.  This defines which hosts have this check applied, and can optionally define variables.  Services can be anywhere in `/etc/icinga2/conf.d` but should be in the `services` subfolder. The civicrm check is a simple one - only one CiviCRM check can be defined per host, and is only added if the three relevant variables are defined.  It looks like this:
60 1 Jon Goldberg
61
        apply Service "civicrm" {
62
          import "generic-service"
63
64
          check_command = "civicrm"
65
66
          assign where host.vars.cms && host.vars.crm_site_key && host.vars.crm_api_key
67
        }
68
69 2 Jon Goldberg
    The DNS check iterates over an array of one or more domains.  It doesn't need an `assign where` statement as a consequence (if the array is empty, no checks are added) but the syntax for iterating is different:
70 1 Jon Goldberg
71
        apply Service for (domain => config in host.vars.domains) {
72
          import "generic-service"
73
74
          check_command = "dns"
75
76
          vars += config
77
        }
78
79 2 Jon Goldberg
-   A **Host** object.  Each server has a Host object - to run a service against 10 hosts, you would define 10 host objects.  Hosts contain all the host-specific variables.  `Hosts` can be anywhere in `/etc/icinga2/conf.d` but should be in the `hosts` subfolder.