Project

General

Profile

Add New Checks to Icinga2 » History » Version 1

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

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