Project

General

Profile

Add New Checks to Icinga2 » History » Version 3

Jon Goldberg, 06/09/2017 05:01 AM

1 3 Jon Goldberg
{{last_updated_at}} by {{last_updated_by}}
2 1 Jon Goldberg
To configure a new check in Icinga, you need 4 components:
3
4 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.:
5 1 Jon Goldberg
6
        root@rh5:/usr/lib/nagios/plugins# /usr/lib/nagios/plugins/check_dns -H jmaconsulting.biz -a 72.249.190.114
7
        DNS OK: 0.018 seconds response time. jmaconsulting.biz returns 72.249.190.114|time=0.018268s;;;0.000000
8
        root@rh5:/usr/lib/nagios/plugins# /usr/lib/nagios/plugins/check_dns -H jmaconsulting.biz -a 72.249.190.222
9
        DNS CRITICAL - expected '72.249.190.222' but got '72.249.190.114'
10
11 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:
12 1 Jon Goldberg
13
{{collapse(Show example,Hide example)
14
15
        object CheckCommand "dns" {
16
                import "ipv4-or-ipv6"
17
18
                command = [ PluginDir + "/check_dns" ]
19
20
                arguments = {
21
                        "-H" = {
22
                                value = "$dns_lookup$"
23
                                description = "The name or address you want to query."
24
                        }
25
                        "-s" = {
26
                                value = "$dns_server$"
27
                                description = "Optional DNS server you want to use for the lookup."
28
                        }
29
                        "-a" = {
30
                                value = "$dns_expected_answers$"
31
                                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)."
32
                        }
33
                        "-a_old" = {
34
                                key = "-a"
35
                                value ="$dns_expected_answer$"
36
                        }
37
                        "-A" = {
38
                                set_if = "$dns_authoritative$"
39
                        }
40
                        "-w" = {
41
                                value = "$dns_wtime$"
42
                                description = "Return warning if elapsed time exceeds value."
43
                        }
44
                        "-c" = {
45
                                value = "$dns_ctime$"
46
                                description = "Return critical if elapsed time exceeds value."
47
                        }
48
                        "-t" = {
49
                                value = "$dns_timeout$"
50
                                description = "Seconds before connection times out. Defaults to 10."
51
                        }
52
                }
53
54
                vars.dns_lookup = "$host.name$"
55
                vars.dns_expected_answer = "$check_address$"
56
                vars.dns_timeout = 10
57
        }
58
}}
59
60 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:
61 1 Jon Goldberg
62
        apply Service "civicrm" {
63
          import "generic-service"
64
65
          check_command = "civicrm"
66
67
          assign where host.vars.cms && host.vars.crm_site_key && host.vars.crm_api_key
68
        }
69
70 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:
71 1 Jon Goldberg
72
        apply Service for (domain => config in host.vars.domains) {
73
          import "generic-service"
74
75
          check_command = "dns"
76
77
          vars += config
78
        }
79
80 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.