]> granicus.if.org Git - icinga2/blob - doc/21-development.md
Docs: Formatting for dev requirements
[icinga2] / doc / 21-development.md
1 # Development <a id="development"></a>
2
3 This chapter provides hints on Icinga 2 debugging,
4 development, package builds and tests.
5
6 * [Debug Icinga 2](21-development.md#development-debug)
7   * [GDB Backtrace](21-development.md#development-debug-gdb-backtrace)
8   * [Core Dump](21-development.md#development-debug-core-dump)
9 * [Develop Icinga 2](21-development.md#development-develop)
10   * [Linux Dev Environment](21-development.md#development-linux-dev-env)
11   * [macOS Dev Environment](21-development.md#development-macos-dev-env)
12   * [Windows Dev Environment](21-development.md#development-windows-dev-env)
13 * [Package Builds](21-development.md#development-package-builds)
14   * [RPM](21-development.md#development-package-builds-rpms)
15   * [DEB](21-development.md#development-package-builds-deb)
16   * [Windows](21-development.md#development-package-builds-windows)
17 * [Advanced Tips](21-development.md#development-advanced)
18 * [Tests](21-development.md#development-tests)
19
20
21 ## Debug Icinga 2 <a id="development-debug"></a>
22
23 This chapter targets all users who have been asked by developers to provide
24 a stack trace or coredump if the application crashed. It is also useful
25 for developers working with different debuggers.
26
27 > **Note:**
28 >
29 > This is intentionally mentioned before any development insights
30 > as debugging is a more frequent and commonly asked question.
31
32 ### Debug Requirements <a id="debug-requirements"></a>
33
34 Make sure that the debug symbols are available for Icinga 2.
35 The Icinga 2 packages provide a debug package which must be
36 installed separately for all involved binaries, like `icinga2-bin`
37 or `icinga2-ido-mysql`.
38
39 Distribution       | Command
40 -------------------|------------------------------------------
41 Debian/Ubuntu      | `apt-get install icinga2-dbg`
42 RHEL/CentOS        | `yum install icinga2-debuginfo`
43 Fedora             | `dnf install icinga2-debuginfo icinga2-bin-debuginfo icinga2-ido-mysql-debuginfo`
44 SLES/openSUSE      | `zypper install icinga2-bin-debuginfo icinga2-ido-mysql-debuginfo`
45
46 Furthermore, you may also have to install debug symbols for Boost and your C++ library.
47
48 If you're building your own binaries, you should use the `-DCMAKE_BUILD_TYPE=Debug` cmake
49 build flag for debug builds.
50
51
52 ### GDB as Debugger <a id="development-debug-gdb"></a>
53
54 Install GDB in your development environment.
55
56 Distribution       | Command
57 -------------------|------------------------------------------
58 Debian/Ubuntu      | `apt-get install gdb`
59 RHEL/CentOS        | `yum install gdb`
60 Fedora             | `dnf install gdb`
61 SLES/openSUSE      | `zypper install gdb`
62
63 #### GDB Run <a id="development-debug-gdb-run"></a>
64
65 Call GDB with the binary (`/usr/sbin/icinga2` is a wrapper script calling
66 `/usr/lib64/icinga2/sbin/icinga2` since 2.4) and all arguments and run it in foreground.
67
68 ```
69 gdb --args /usr/lib64/icinga2/sbin/icinga2 daemon -x debug
70 ```
71
72 The exact path to the Icinga 2 binary differs on each distribution. On Ubuntu
73 it is installed into `/usr/lib/x86_64-linux-gnu/icinga2/sbin/icinga2` on 64-bit systems
74 for example.
75
76 > **Note**
77 >
78 > If gdb tells you it's missing debug symbols, quit gdb and install
79 > them: `Missing separate debuginfos, use: debuginfo-install ...`
80
81 Run/restart the application.
82
83 ```
84 (gdb) r
85 ```
86
87 Kill the running application.
88
89 ```
90 (gdb) k
91 ```
92
93 Continue after breakpoint.
94
95 ```
96 (gdb) c
97 ```
98
99 #### GDB Core Dump <a id="development-debug-gdb-coredump"></a>
100
101 Either attach to the running process using `gdb -p PID` or start
102 a new gdb run.
103
104 ```
105 (gdb) r
106 (gdb) generate-core-file
107 ```
108
109 #### GDB Backtrace <a id="development-debug-gdb-backtrace"></a>
110
111 If Icinga 2 aborted its operation abnormally, generate a backtrace.
112
113 > **Note**
114 >
115 > Please install the [required debug symbols](21-development.md#debug-requirements)
116 > prior to generating a backtrace.
117
118 `thread apply all` is important here since this includes all running threads.
119 We need this information when e.g. debugging dead locks and hanging features.
120
121 ```
122 (gdb) bt
123 (gdb) thread apply all bt full
124 ```
125
126 If gdb stops at a SIGPIPE signal please disable the signal before
127 running Icinga 2. This isn't an error, but we need to workaround it.
128
129 ```
130 (gdb) handle SIGPIPE nostop noprint pass
131 (gdb) r
132 ```
133
134 If you create a [new issue](https://github.com/Icinga/icinga2/issues),
135 make sure to attach as much detail as possible.
136
137 #### GDB Backtrace from Running Process <a id="development-debug-gdb-backtrace-running"></a>
138
139 If Icinga 2 is still running, generate a full backtrace from the running
140 process and store it into a new file (e.g. for debugging dead locks).
141
142 > **Note**
143 >
144 > Please install the [required debug symbols](21-development.md#debug-requirements)
145 > prior to generating a backtrace.
146
147 Icinga 2 runs with 2 processes: main and command executor, therefore generate two backtrace logs
148 and add them to the GitHub issue.
149
150 ```
151 for pid in $(pidof icinga2); do gdb -p $pid -batch -ex "thread apply all bt full" -ex "detach" -ex "q" > gdb_bt_${pid}_`date +%s`.log; done
152 ```
153
154 #### GDB Thread List from Running Process <a id="development-debug-gdb-thread-list-running"></a>
155
156 Instead of a full backtrace, you sometimes just need a list of running threads.
157
158 ```
159 for pid in $(pidof icinga2); do gdb -p $pid -batch -ex "info threads" -ex "detach" -ex "q" > gdb_threads_${pid}_`date +%s`.log; done
160 ```
161
162 #### GDB Backtrace Stepping <a id="development-debug-gdb-backtrace-stepping"></a>
163
164 Identifying the problem may require stepping into the backtrace, analysing
165 the current scope, attributes, and possible unmet requirements. `p` prints
166 the value of the selected variable or function call result.
167
168 ```
169 (gdb) up
170 (gdb) down
171 (gdb) p checkable
172 (gdb) p checkable.px->m_Name
173 ```
174
175 #### GDB Breakpoints <a id="development-debug-gdb-breakpoint"></a>
176
177 To set a breakpoint to a specific function call, or file specific line.
178
179 ```
180 (gdb) b checkable.cpp:125
181 (gdb) b icinga::Checkable::SetEnablePerfdata
182 ```
183
184 GDB will ask about loading the required symbols later, select `yes` instead
185 of `no`.
186
187 Then run Icinga 2 until it reaches the first breakpoint. Continue with `c`
188 afterwards.
189
190 ```
191 (gdb) run
192 (gdb) c
193 ```
194
195 In case you want to step into the next line of code, use `n`. If there is a
196 function call where you want to step into, use `s`.
197
198 ```
199 (gdb) n
200
201 (gdb) s
202 ```
203
204 If you want to delete all breakpoints, use `d` and select `yes`.
205
206 ```
207 (gdb) d
208 ```
209
210 > **Tip**
211 >
212 > When debugging exceptions, set your breakpoint like this: `b __cxa_throw`.
213
214 Breakpoint Example:
215
216     (gdb) b __cxa_throw
217     (gdb) r
218     (gdb) up
219     ....
220     (gdb) up
221     #11 0x00007ffff7cbf9ff in icinga::Utility::GlobRecursive(icinga::String const&, icinga::String const&, boost::function<void (icinga::String const&)> const&, int) (path=..., pattern=..., callback=..., type=1)
222         at /home/michi/coding/icinga/icinga2/lib/base/utility.cpp:609
223     609                 callback(cpath);
224     (gdb) l
225     604
226     605 #endif /* _WIN32 */
227     606
228     607         std::sort(files.begin(), files.end());
229     608         BOOST_FOREACH(const String& cpath, files) {
230     609                 callback(cpath);
231     610         }
232     611
233     612         std::sort(dirs.begin(), dirs.end());
234     613         BOOST_FOREACH(const String& cpath, dirs) {
235     (gdb) p files
236     $3 = std::vector of length 11, capacity 16 = {{static NPos = 18446744073709551615, m_Data = "/etc/icinga2/conf.d/agent.conf"}, {static NPos = 18446744073709551615,
237         m_Data = "/etc/icinga2/conf.d/commands.conf"}, {static NPos = 18446744073709551615, m_Data = "/etc/icinga2/conf.d/downtimes.conf"}, {static NPos = 18446744073709551615,
238         m_Data = "/etc/icinga2/conf.d/groups.conf"}, {static NPos = 18446744073709551615, m_Data = "/etc/icinga2/conf.d/notifications.conf"}, {static NPos = 18446744073709551615,
239         m_Data = "/etc/icinga2/conf.d/satellite.conf"}, {static NPos = 18446744073709551615, m_Data = "/etc/icinga2/conf.d/services.conf"}, {static NPos = 18446744073709551615,
240         m_Data = "/etc/icinga2/conf.d/templates.conf"}, {static NPos = 18446744073709551615, m_Data = "/etc/icinga2/conf.d/test.conf"}, {static NPos = 18446744073709551615,
241         m_Data = "/etc/icinga2/conf.d/timeperiods.conf"}, {static NPos = 18446744073709551615, m_Data = "/etc/icinga2/conf.d/users.conf"}}
242
243
244 ### Core Dump <a id="development-debug-core-dump"></a>
245
246 When the Icinga 2 daemon crashes with a `SIGSEGV` signal
247 a core dump file should be written. This will help
248 developers to analyze and fix the problem.
249
250 #### Core Dump File Size Limit <a id="development-debug-core-dump-limit"></a>
251
252 This requires setting the core dump file size to `unlimited`.
253
254
255 ##### Systemd
256
257 ```
258 systemctl edit icinga2.service
259
260 [Service]
261 ...
262 LimitCORE=infinity
263
264 systemctl daemon-reload
265
266 systemctl restart icinga2
267 ```
268
269 ##### Init Script
270
271 ```
272 vim /etc/init.d/icinga2
273 ...
274 ulimit -c unlimited
275
276 service icinga2 restart
277 ```
278
279 ##### Verify
280
281 Verify that the Icinga 2 process core file size limit is set to `unlimited`.
282
283 ```
284 for pid in $(pidof icinga2); do cat /proc/$pid/limits; done
285
286 ...
287 Max core file size        unlimited            unlimited            bytes
288 ```
289
290
291 #### Core Dump Kernel Format <a id="development-debug-core-dump-format"></a>
292
293 The Icinga 2 daemon runs with the SUID bit set. Therefore you need
294 to explicitly enable core dumps for SUID on Linux.
295
296 ```
297 sysctl -w fs.suid_dumpable=2
298 ```
299
300 Adjust the coredump kernel format and file location on Linux:
301
302 ```
303 sysctl -w kernel.core_pattern=/var/lib/cores/core.%e.%p
304
305 install -m 1777 -d /var/lib/cores
306 ```
307
308 MacOS:
309
310 ```
311 sysctl -w kern.corefile=/cores/core.%P
312
313 chmod 777 /cores
314 ```
315
316 #### Core Dump Analysis <a id="development-debug-core-dump-analysis"></a>
317
318 Once Icinga 2 crashes again a new coredump file will be written. Please
319 attach this file to your bug report in addition to the general details.
320
321 Simple test case for a `SIGSEGV` simulation with `sleep`:
322
323 ```
324 ulimit -c unlimited
325 sleep 1800&
326 [1] <PID>
327 kill -SEGV <PID>
328 gdb `which sleep` /var/lib/cores/core.sleep.<PID>
329 (gdb) bt
330 rm /var/lib/cores/core.sleep.*
331 ```
332
333 Analyzing Icinga 2:
334
335 ```
336 gdb /usr/lib64/icinga2/sbin/icinga2 core.icinga2.<PID>
337 (gdb) bt
338 ```
339
340 ### LLDB as Debugger <a id="development-debug-lldb"></a>
341
342 LLDB is available on macOS with the Xcode command line tools.
343
344 ```
345 $ xcode-select --install
346 ```
347
348 In order to run Icinga 2 with LLDB you need to pass the binary as argument.
349
350 ```
351 lldb -- /usr/local/icinga2/lib/icinga2/sbin/icinga2 daemon
352 ```
353
354 Breakpoint:
355
356 ```
357 > b checkable.cpp:57
358 > b icinga::Checkable::ProcessCheckResult
359 ```
360
361 Full backtrace:
362
363 ```
364 > bt all
365 ```
366
367 Select thread:
368
369 ```
370 > thr sel 5
371 ```
372
373 Step into:
374
375 ```
376 > s
377 ```
378
379 Next step:
380
381 ```
382 > n
383 ```
384
385 Continue:
386
387 ```
388 > c
389 ```
390
391 Up/down in stacktrace:
392
393 ```
394 > up
395 > down
396 ```
397
398
399
400 ## Develop Icinga 2 <a id="development-develop"></a>
401
402 Icinga 2 can be built on many platforms such as Linux, Unix and Windows.
403 There are limitations in terms of support, e.g. Windows is only supported for agents,
404 not a full-featured master or satellite.
405
406 Before you start with actual development, there is a couple of pre-requisites.
407
408 ### Choose your Editor <a id="development-develop-choose-editor"></a>
409
410 Icinga 2 can be developed with your favorite editor. Icinga developers prefer
411 these tools:
412
413 - vim
414 - CLion (macOS, Linux)
415 - MS Visual Studio (Windows)
416 - Atom
417
418 Editors differ on the functionality. The more helpers you get for C++ development,
419 the faster your development workflow will be.
420
421
422 #### Whitespace Cleanup <a id="development-develop-choose-editor-whitespaces"></a>
423
424 Patches must be cleaned up and follow the indent style (tabs instead of spaces).
425 You should also remove any training whitespaces.
426
427 `git diff` allows to highlight such.
428
429 ```
430 vim $HOME/.gitconfig
431
432 [color "diff"]
433         whitespace = red reverse
434 [core]
435         whitespace=fix,-indent-with-non-tab,trailing-space,cr-at-eol
436 ```
437
438 `vim` also can match these and visually alert you to remove them.
439
440 ```
441 vim $HOME/.vimrc
442
443 highlight ExtraWhitespace ctermbg=red guibg=red
444 match ExtraWhitespace /\s\+$/
445 autocmd BufWinEnter * match ExtraWhitespace /\s\+$/
446 autocmd InsertEnter * match ExtraWhitespace /\s\+\%#\@<!$/
447 autocmd InsertLeave * match ExtraWhitespace /\s\+$/
448 autocmd BufWinLeave * call clearmatches()
449 ```
450
451 ### Get to know the architecture <a id="development-develop-get-to-know-the-architecture"></a>
452
453 Icinga 2 can run standalone or in distributed environments. It contains a whole lot
454 more than a simple check execution engine.
455
456 Read more about it in the [Technical Concepts](19-technical-concepts.md#technical-concepts) chapter.
457
458 ### Get to know the code <a id="development-develop-get-to-know-the-code"></a>
459
460 First off, you really need to know C++ and portions of C++11 and the boost libraries.
461 Best is to start with a book or online tutorial to get into the basics.
462 Icinga developers gained their knowledge through studies, training and self-teaching
463 code by trying it out and asking senior developers for guidance.
464
465 Here's a few books we can recommend:
466
467 * [Accelerated C++: Practical Programming by Example](https://www.amazon.com/Accelerated-C-Practical-Programming-Example/dp/020170353X) (Andrew Koenig, Barbara E. Moo)
468 * [Effective C++](https://www.amazon.com/Effective-Specific-Improve-Programs-Designs/dp/0321334876) (Scott Meyers)
469 * [Der C++ Programmierer](https://www.amazon.de/Programmierer-lernen-Professionell-anwenden-L%C3%B6sungen/dp/3446416447), German (Ulrich Breymann)
470 * [C++11 programmieren](https://www.amazon.de/gp/product/3836217325/), German (Torsten T. Will)
471
472 In addition, it is a good bet to also know SQL when diving into backend development.
473
474 * [SQL Performance Explained](https://www.amazon.de/gp/product/3950307826/) (Markus Winand)
475
476 Last but not least, if you are developing on Windows, get to know the internals about services and the Win32 API.
477
478
479 ### Design Patterns <a id="development-develop-design-patterns"></a>
480
481 Icinga 2 heavily relies on object-oriented programming and encapsulates common
482 functionality into classes and objects. It also uses modern programming techniques
483 to e.g. work with shared pointer memory management.
484
485 Icinga 2 consists of libraries bundled into the main binary. Therefore you'll
486 find many code parts in the `lib/` directory wheras the actual application is
487 built from `icinga-app/`. Accompanied with Icinga 2, there's the Windows plugins
488 which are standalone and compiled from `plugins/`.
489
490 Library        | Description
491 ---------------|------------------------------------
492 base           | Objects, values, types, streams, tockets, TLS, utilities, etc.
493 config         | Configuration compiler, expressions, etc.
494 cli            | CLI (sub) commands and helpers.
495 icinga         | Icinga specific objects and event handling.
496 remote         | Cluster and HTTP client/server and REST API related code.
497 checker        | Checker feature, check scheduler.
498 notification   | Notification feature, notification scheduler.
499 methods        | Command execution methods, plugins and built-in checks.
500 perfdata       | Performance data related, including Graphite, Elastic, etc.
501 db\_ido        | IDO database abstraction layer.
502 db\_ido\_mysql | IDO database driver for MySQL.
503 db\_ido\_pgsql | IDO database driver for PgSQL.
504 mysql\_shin    | Library stub for linking against the MySQL client libraries.
505 pgsql\_shim    | Library stub for linking against the PgSQL client libraries.
506
507 #### Class Compiler <a id="development-develop-design-patterns-class-compiler"></a>
508
509 Another thing you will recognize are the `.ti` files which are compiled
510 by our own class compiler into actual source code. The meta language allows
511 developers to easily add object attributes and specify their behaviour.
512
513 Some object attributes need to be stored over restarts in the state file
514 and therefore have the `state` attribute set. Others are treated as `config`
515 attribute and automatically get configuration validation functions created.
516 Hidden or read-only REST API attributes are marked with `no_user_view` and
517 `no_user_modify`.
518
519 The most beneficial thing are getters and setters being generated. The actual object
520 inherits from `ObjectImpl<TYPE>` and therefore gets them "for free".
521
522 Example:
523
524 ```
525 vim lib/perfdata/gelfwriter.ti
526
527   [config] enable_tls;
528
529 vim lib/perfdata/gelfwriter.cpp
530
531     if (GetEnableTls()) {
532 ```
533
534 The logic is hidden in `tools/mkclass/` in case you want to learn more about it.
535 The first steps during CMake & make also tell you about code generation.
536
537
538 ### Builds: CMake <a id="development-develop-builds-cmake"></a>
539
540 In its early development stages in 2012, Icinga 2 was built with autoconf/automake
541 and separate Windows project files. We've found this very fragile, and have changed
542 this into CMake as our build tool.
543
544 The most common benefits:
545
546 * Everything is described in CMakeLists.txt in each directory
547 * CMake only needs to know that a sub directory needs to be included.
548 * The global CMakeLists.txt acts as main entry point for requirement checks and library/header includes.
549 * Separate binary build directories, the actual source tree stays clean.
550 * CMake automatically generates a Visual Studio project file `icinga2.sln` on Windows.
551
552 ### Builds: Unity Builds <a id="development-develop-builds-unity-builds"></a>
553
554 Another thing you should be aware of: Unity builds on and off.
555
556 Typically, we already use caching mechanisms to reduce recompile time with ccache.
557 For release builds, there's always a new build needed as the difference is huge compared
558 to a previous (major) release.
559
560 Therefore we've invented the Unity builds, which basically concatenates all source files
561 into one big library source code file. The compiler then doesn't need to load the many small
562 files but compiles and links this huge one.
563
564 Unity builds require more memory which is why you should disable them for development
565 builds in small sized VMs (Linux, Windows) and also Docker containers.
566
567 There's a couple of header files which are included everywhere. If you touch/edit them,
568 the cache is invalidated and you need to recompile a lot more files then. `base/utility.hpp`
569 and `remote/zone.hpp` are good candidates for this.
570
571
572 ### Linux Dev Environment <a id="development-linux-dev-env"></a>
573
574 Based on CentOS 7, we have an early draft available inside the Icinga Vagrant boxes:
575 [centos7-dev](https://github.com/Icinga/icinga-vagrant/tree/master/centos7-dev).
576
577 If you're compiling Icinga 2 natively without any virtualization layer in between,
578 this usually is faster. This is also the reason why developers on macOS prefer native builds
579 over Linux or Windows VMs. Don't forget to test the actual code on Linux later! Socket specific
580 stuff like `epoll` is not available on Unix kernels.
581
582 Depending on your workstation and environment, you may either develop and run locally,
583 use a container deployment pipeline or put everything in a high end resource remote VM.
584
585 Fork https://github.com/Icinga/icinga2 into your own repository, e.g. `https://github.com/dnsmichi/icinga2`.
586
587 Create two build directories for different binary builds.
588
589 * `debug` contains the debug build binaries. They contain more debug information and run tremendously slower than release builds from packages. Don't use them for benchmarks.
590 * `release` contains the release build binaries, as you would install them on a live system. This helps comparing specific scenarios for race conditions and more.
591
592 ```
593 mkdir -p release debug
594 ```
595
596 Proceed with the specific distribution examples below.
597
598 * [CentOS 7](21-development.md#development-linux-dev-env-centos)
599 * [Debian 9](21-development.md#development-linux-dev-env-debian)
600
601
602 #### CentOS 7 <a id="development-linux-dev-env-centos"></a>
603
604 ```
605 yum -y install gdb git bash-completion htop rpmdevtools \
606  ccache cmake make gcc-c++ flex bison \
607  openssl-devel boost-devel systemd-devel mysql-devel \
608  postgresql-devel libedit-devel libstdc++-devel
609
610 groupadd icinga
611 groupadd icingacmd
612 useradd -c "icinga" -s /sbin/nologin -G icingacmd -g icinga icinga
613
614 ln -s /bin/ccache /usr/local/bin/gcc
615 ln -s /bin/ccache /usr/local/bin/g++
616
617 git clone https://github.com/icinga/icinga2.git && cd icinga2
618
619 mkdir debug release
620 cd debug
621 cmake .. -DCMAKE_BUILD_TYPE=Debug -DICINGA2_UNITY_BUILD=OFF -DCMAKE_INSTALL_PREFIX=/usr/local/icinga2 -DICINGA2_PLUGINDIR=/usr/local/sbin
622 cd ..
623 make -j2 install -C debug
624 ```
625
626
627 ```
628 chown -R icinga:icinga /usr/local/icinga2/var/
629
630 /usr/local/icinga2/lib/icinga2/prepare-dirs /usr/local/icinga2/etc/sysconfig/icinga2
631 /usr/local/icinga2/sbin/icinga2 api setup
632 vim /usr/local/icinga2/etc/icinga2/conf.d/api-users.conf
633
634 gdb --args /usr/local/icinga2/lib/icinga2/sbin/icinga2 daemon
635 ```
636
637 ##### Debian 9 <a id="development-linux-dev-env-debian"></a>
638
639 ```
640 apt-get -y install gdb vim git cmake make ccache build-essential libssl-dev libboost-all-dev bison flex default-libmysqlclient-dev libpq-dev libyajl-dev libedit-dev monitoring-plugins
641
642 ln -s /usr/bin/ccache /usr/local/bin/gcc
643 ln -s /usr/bin/ccache /usr/local/bin/g++
644
645 groupadd icinga
646 groupadd icingacmd
647 useradd -c "icinga" -s /sbin/nologin -G icingacmd -g icinga icinga
648
649 git clone https://github.com/icinga/icinga2.git && cd icinga2
650
651 mkdir debug release
652 cd debug
653 cmake .. -DCMAKE_BUILD_TYPE=Debug -DICINGA2_UNITY_BUILD=OFF -DCMAKE_INSTALL_PREFIX=/usr/local/icinga2 -DICINGA2_PLUGINDIR=/usr/local/sbin
654 cd ..
655 make -j2 install -C debug
656 ```
657
658
659 ```
660 chown -R icinga:icinga /usr/local/icinga2/var/
661
662 /usr/local/icinga2/lib/icinga2/prepare-dirs /usr/local/icinga2/etc/sysconfig/icinga2
663 /usr/local/icinga2/sbin/icinga2 api setup
664 vim /usr/local/icinga2/etc/icinga2/conf.d/api-users.conf
665
666 gdb --args /usr/local/icinga2/lib/icinga2/sbin/icinga2 daemon
667 ```
668
669
670
671 ### macOS Dev Environment <a id="development-macos-dev-env"></a>
672
673 It is advised to use Homebrew to install required build dependencies.
674 Macports have been reported to work as well, typically you'll get more help
675 with Homebrew from Icinga developers.
676
677 #### Users and Groups
678
679 First off, create the following from `Settings - Users & Groups`:
680
681 * Users: `icinga`
682 * Groups: `icinga` with `icinga` as member
683 * Groups: `icingaweb2`
684
685 Then disallow login for these users.
686
687 ```
688 dscl
689 list Local/Default/Users
690 read Local/Default/Users/icinga
691 change Local/Default/Users/icinga UserShell /bin/bash /usr/bin/false
692 sudo dscl . create /Users/icinga IsHidden 1
693 sudo dseditgroup -o edit -a _www -t user icingaweb2
694 ```
695
696 #### Requirements
697
698 OpenSSL 1.0.x doesn't build anymore, so we're explicitly using 1.1.x here.
699
700 ```
701 brew install ccache boost cmake bison flex yajl openssl@1.1 mysql-connector-c++ postgresql libpq
702 ```
703
704 ##### ccache
705
706 ```
707 sudo mkdir /opt/ccache
708
709 sudo ln -s `which ccache` /opt/ccache/clang
710 sudo ln -s `which ccache` /opt/ccache/clang++
711
712 vim $HOME/.bashrc
713
714 # ccache is managed with symlinks to avoid collision with cgo
715 export PATH="/opt/ccache:$PATH"
716
717 source $HOME/.bashrc
718 ```
719
720 #### Builds
721
722 We will build two different flavors on macOS.
723
724 ```
725 mkdir -p release debug
726
727 cd debug
728 cmake -DICINGA2_UNITY_BUILD=OFF -DICINGA2_WITH_STUDIO=ON -DCMAKE_INSTALL_PREFIX=/usr/local/icinga2 -DOPENSSL_INCLUDE_DIR=/usr/local/opt/openssl@1.1/include -DOPENSSL_SSL_LIBRARY=/usr/local/opt/openssl@1.1/lib/libssl.dylib -DOPENSSL_CRYPTO_LIBRARY=/usr/local/opt/openssl@1.1/lib/libcrypto.dylib ..
729 cd ..
730
731 make -j4 -C debug
732 sudo make -j4 install -C debug
733 ```
734
735 ##### Build Aliases
736
737 This is derived from dnsmichi's flavour and not generally best practice.
738
739 ```
740 vim $HOME/.bashrc
741
742 export PATH=/usr/local/icinga2/sbin/:$PATH
743 source /usr/local/icinga2/etc/bash_completion.d/icinga2
744
745 export I2_GENERIC="-DCMAKE_INSTALL_PREFIX=/usr/local/icinga2 -DOPENSSL_INCLUDE_DIR=/usr/local/opt/openssl@1.1/include -DOPENSSL_SSL_LIBRARY=/usr/local/opt/openssl@1.1/lib/libssl.dylib -DOPENSSL_CRYPTO_LIBRARY=/usr/local/opt/openssl@1.1/lib/libcrypto.dylib -DICINGA2_PLUGINDIR=/usr/local/sbin"
746
747 export I2_DEBUG="-DCMAKE_BUILD_TYPE=Debug -DICINGA2_UNITY_BUILD=OFF $I2_GENERIC"
748 export I2_RELEASE="-DCMAKE_BUILD_TYPE=RelWithDebInfo -DICINGA2_WITH_TESTS=ON -DICINGA2_UNITY_BUILD=ON $I2_GENERIC"
749
750 alias i2_debug="mkdir -p debug; cd debug; cmake $I2_DEBUG ..; make -j4; sudo make -j4 install; cd .."
751 alias i2_release="mkdir -p release; cd release; cmake $I2_RELEASE ..; make -j4; sudo make -j4 install; cd .."
752
753 source $HOME/.bashrc
754 ```
755
756 #### Run
757
758 ```
759 chown -R icinga:icinga /usr/local/icinga2
760 chown -R icinga:_www /usr/local/icinga2/var/run/icinga2/cmd
761
762 icinga2 daemon
763 ```
764
765 #### Plugins
766
767 ```
768 brew install nagios-plugins
769
770 sudo vim /usr/local/icinga2/etc/icinga2/constants.conf
771 const PluginDir = "/usr/local/sbin"
772 ```
773
774 #### Databases: MariaDB
775
776 ```
777 brew install mariadb
778 ln -sfv /usr/local/opt/mariadb/*.plist ~/Library/LaunchAgents
779 launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mariadb.plist
780 mysql_secure_installation
781 ```
782
783 ```
784 vim $HOME/.my.cnf
785
786 [client]
787 user = root
788 password = supersecurerootpassword
789
790 sudo -i
791 ln -s /Users/michi/.my.cnf $HOME/.my.cnf
792 exit
793 ```
794
795 ```
796 cd $HOME/coding/icinga/icinga2
797
798 sudo mysql
799
800 CREATE DATABASE icinga;
801 GRANT SELECT, INSERT, UPDATE, DELETE, DROP, CREATE VIEW, INDEX, EXECUTE ON icinga.* TO 'icinga'@'localhost' IDENTIFIED BY 'icinga';
802 quit
803 sudo mysql icinga < lib/db_ido_mysql/schema/mysql.sql
804 ```
805
806
807
808
809 ### Windows Dev Environment <a id="development-windows-dev-env"></a>
810
811 The following sections explain how to setup the required build tools
812 and how to run and debug the code.
813
814 #### Chocolatey
815
816 Open an administrative command prompt (Win key, type “cmd”, right-click and “run as administrator”) and paste the following instructions:
817
818 ```
819 @powershell -NoProfile -ExecutionPolicy Bypass -Command "iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))" && SET PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin
820 ```
821
822 #### Visual Studio
823
824 Thanks to Microsoft they’ll now provide their Professional Edition of Visual Studio 2017
825 as community version, free for use for open source projects such as Icinga.
826 The installation requires ~9GB disk space. [Download](https://www.visualstudio.com/downloads/)
827 the web installer and start the installation.
828
829 You need a free Microsoft account to download and also store your preferences.
830
831 Choose the following minimal set:
832
833 * .NET Framework 4.x SDK
834 * C# Compiler
835 * Visual Studio C++ core features
836 * VC++ toolset
837 * Windows 10 SDK (10.0.10240.0 - required)
838 * Just-in-time debugger
839 * Windows 8 SDK (includes mscoree.lib required by clrchecktask)
840 * C++/CLI support
841 * Windows Universal C Runtime
842 * Git for Windows
843 * .NET Framework 3.5 development tools
844 * Github extension for Visual Studio
845
846 After a while, Visual Studio will be ready.
847
848 #### Flex and Bison
849
850 Install it using [chocolatey](https://www.wireshark.org/docs/wsdg_html_chunked/ChSetupWin32.html):
851
852 ```
853 choco install -y winflexbison
854 ```
855
856 Chocolatey installs these tools into the hidden directory `C:\ProgramData\chocolatey\lib\winflexbison\tools`.
857
858 #### OpenSSL
859
860 Icinga 2 requires the OpenSSL library. [Download](http://slproweb.com/products/Win32OpenSSL.html)
861 and install it into the default path.
862
863 Once asked for `Copy OpenSSLs DLLs to` select `The Windows system directory`. That way CMake/Visual Studio
864 will automatically detect them for builds and packaging.
865
866 > **Note**
867 >
868 > We cannot use the chocolatey package as this one does not provide any development headers.
869 >
870 > Choose 1.0.2 LTS from manual downloads for best compatibility if unsure.
871
872 #### Boost
873
874 In order to use the boost development header and library files you need to [download](http://www.boost.org/users/download/)
875 Boost and then extract it to e.g. `C:\boost_1_65_1`.
876
877 > **Note**
878 >
879 > Just use `C:`, the zip file already contains the sub folder. Extraction takes a while,
880 > the archive contains more than 10k files.
881
882 For integrating Boost into Visual Studio 2017, open the `Developer Command Prompt` from the start menu,
883 and navigate to `C:\boost_1_65_1`.
884
885 Execute `bootstrap.bat` first.
886
887 ```
888 cd C:\boost_1_65_1
889 bootstrap.bat
890 ```
891
892 Once finished, specify the required `toolset` to compile boost against Visual Studio.
893 This takes quite some time in a Windows VM.
894
895 Visual Studio 2015:
896
897 ```
898 b2 --toolset=msvc-14.0
899 ```
900
901 Visual Studio 2017:
902
903 ```
904 b2 --toolset=msvc-14.1
905 ```
906
907 #### TortoiseGit
908
909 TortoiseGit provides a graphical integration into the Windows explorer. This makes it easier to checkout, commit
910 and whatnot.
911
912 [Download](https://tortoisegit.org/download/) TortoiseGit on your system.
913
914 In order to clone via Git SSH you also need to create a new directory called `.ssh`
915 inside your user's home directory.
916 Therefore open a command prompt (win key, type `cmd`, enter) and run `mkdir .ssh`.
917 Add your `id_rsa` private key and `id_rsa.pub` public key files into that directory.
918
919 Start the setup routine and choose `OpenSSH` as default secure transport when asked.
920
921 Open a Windows Explorer window and navigate into
922
923 Version             | Project Location
924 --------------------|------------------------------
925 Visual Studio 2015  | `C:\Users\michi\Documents\Visual Studio 2015\Projects`
926 Visual Studio 2017+ | `C:\Users\michi\source\repos`
927
928 Right click and select `Git Clone` from the context menu.
929
930 Use `ssh://git@github.com/icinga/icinga2.git` for SSH clones, `https://github.com/icinga/icinga2.git` otherwise.
931
932 #### CMake
933
934 Icinga 2 uses CMake to manage the build environment. You can generate the Visual Studio project files
935 using CMake. [Download](https://cmake.org/download/) and install CMake. Select to add it to PATH for all users
936 when asked.
937
938 Once setup is completed, open a command prompt and navigate to
939
940 Visual Studio 2015
941
942 ```
943 cd C:\Users\<username>\Documents\Visual Studio 2015\Projects\icinga2
944 ```
945
946 Visual Studio 2017
947
948 ```
949 cd C:\Users\michi\source\repos
950 ```
951
952 Run CMake with the following command. This generates a new Visual Studio project file called `icinga2.sln`.
953
954 You need to specify the previously installed component paths:
955
956 Variable              | Value                                                                | Description
957 ----------------------|----------------------------------------------------------------------|-------------------------------------------------------
958 `BOOST_ROOT`          | `C:\boost_1_65_1`                                                    | Root path where you've extracted and compiled Boost.
959 `BISON_EXECUTABLE`    | `C:\ProgramData\chocolatey\lib\winflexbison\tools\win_bison.exe`     | Path to the Bison executable.
960 `FLEX_EXECUTABLE`     | `C:\ProgramData\chocolatey\lib\winflexbison\tools\win_flex.exe`      | Path to the Flex executable.
961 `ICINGA2_WITH_MYSQL`  | OFF                                                                  | Requires extra setup for MySQL if set to `ON`. Not supported for client setups.
962 `ICINGA2_WITH_PGSQL`  | OFF                                                                  | Requires extra setup for PgSQL if set to `ON`. Not supported for client setups.
963 `ICINGA2_UNITY_BUILD` | OFF                                                                  | Disable unity builds for development environments.
964
965 Tip: If you have previously opened a terminal, run `refreshenv` to re-read updated PATH variables.
966
967 ```
968 cmake . -DBOOST_ROOT=C:\boost_1_65_1 -DBISON_EXECUTABLE=C:\ProgramData\chocolatey\lib\winflexbison\tools\win_bison.exe -DFLEX_EXECUTABLE=C:\ProgramData\chocolatey\lib\winflexbison\tools\win_flex.exe -DICINGA2_WITH_MYSQL=OFF -DICINGA2_WITH_PGSQL=OFF -DICINGA2_UNITY_BUILD=OFF
969 ```
970
971 Best is write a small batch/Powershell script which just executes these lines.
972
973
974 #### Icinga 2 in Visual Studio
975
976 Navigate to
977
978 Version             | Project location
979 --------------------|-------------------------
980 Visual Studio 2015  | `C:\Users\michi\Documents\Visual Studio 2015\Projects\icinga2`
981 Visual Studio 2017+ | `C:\Users\michi\source\repos\icinga2`
982
983 Open `icinga2.sln`. Log into Visual Studio when asked.
984
985 On the right panel, select to build the `Bin/icinga-app` solution.
986
987 The executable binaries are located in `Bin\Release\Debug` in your `icinga2`
988 project directory.
989
990 Navigate there and run `icinga2.exe --version`.
991
992 Example for Visual Studio 2017:
993
994 ```
995 cd C:\Users\michi\source\repos\icinga2\Bin\Release\Debug
996 icinga2.exe --version
997 ```
998
999
1000 #### Release Package
1001
1002 CMake uses CPack and NSIS to create the setup executable including all binaries and libraries
1003 in addition to setup dialogues and configuration. Therefore we’ll need to install [NSIS](http://nsis.sourceforge.net/Download)
1004 first.
1005
1006 We also need to install the Windows Installer XML (WIX) toolset.
1007
1008 ```
1009 choco install -y wixtoolset
1010 ```
1011
1012 Once completed open an administrative shell and navigate to your Visual Studio project.
1013 Let CMake to build a release package.
1014
1015 ```
1016 cd "c:\Users\michi\Documents\Visual Studio 2015\Projects\icinga2"
1017 cmake --build . --target PACKAGE --config Release
1018 ```
1019
1020 Note: This will still use the debug builds. A yet more clean approach
1021 is to run CMake with changed release parameters beforehand and then
1022 re-run the release package builder.
1023
1024 ```
1025 C:\Users\michi\Documents\Visual Studio 2015\Projects\icinga2>
1026 cmake . -DCPACK_GENERATOR=WIX -DCMAKE_BUILD_TYPE=Release -DBOOST_ROOT=C:\boost_1_65_1 -DBISON_EXECUTABLE=C:\ProgramData\chocolatey\lib\winflexbison\tools\win_bison.exe -DFLEX_EXECUTABLE=C:\ProgramData\chocolatey\lib\winflexbison\tools\win_flex.exe -DICINGA2_WITH_MYSQL=OFF -DICINGA2_WITH_PGSQL=OFF -DICINGA2_UNITY_BUILD=OFF
1027
1028 cmake --build . --target PACKAGE --config Release
1029 ```
1030
1031 Again, put these lines into a batch/Powershell script and execute that.
1032
1033
1034
1035 ### Embedded Dev Env: Pi <a id="development-embedded-dev-env"></a>
1036
1037 > **Note**
1038 >
1039 > This isn't officially supported yet, just a few hints how you can do it yourself.
1040
1041 The following examples source from armhf on Raspberry Pi.
1042
1043 #### ccache
1044
1045 ```
1046 apt install -y ccache
1047
1048 /usr/sbin/update-ccache-symlinks
1049
1050 echo 'export PATH="/usr/lib/ccache:$PATH"' | tee -a ~/.bashrc
1051
1052 source ~/.bashrc && echo $PATH
1053 ```
1054
1055 #### Build
1056
1057 Copy the icinga2 source code into `$HOME/icinga2`. Clone the `deb-icinga2` repository into `debian/`.
1058
1059 ```
1060 git clone https://github.com/Icinga/icinga2 $HOME/icinga2
1061 git clone https://github.com/Icinga/deb-icinga2 $HOME/icinga2/debian
1062 ```
1063
1064 Then build a Debian package and install it like normal.
1065 ```
1066 dpkg-buildpackage -uc -us
1067 ```
1068
1069 ## Package Builds <a id="development-package-builds"></a>
1070
1071 This documentation is explicitly meant for packagers and the Icinga
1072 build infrastructure.
1073
1074 The following requirements need to be fulfilled in order to build the
1075 Icinga application using a dist tarball (including notes for distributions):
1076
1077 * cmake >= 2.6
1078 * GNU make (make) or ninja-build
1079 * C++ compiler which supports C++11
1080   - RHEL/Fedora/SUSE: gcc-c++ >= 4.7 (extra Developer Tools on RHEL5/6 see below)
1081   - Debian/Ubuntu: build-essential
1082   - Alpine: build-base
1083   - you can also use clang++
1084 * pkg-config
1085 * OpenSSL library and header files >= 1.0.1
1086   - RHEL/Fedora: openssl-devel
1087   - SUSE: libopenssl-devel (for SLES 11: libopenssl1-devel)
1088   - Debian/Ubuntu: libssl-dev
1089   - Alpine: libressl-dev
1090 * Boost library and header files >= 1.48.0
1091   - RHEL/Fedora: boost148-devel
1092   - Debian/Ubuntu: libboost-all-dev
1093   - Alpine: boost-dev
1094 * GNU bison (bison)
1095 * GNU flex (flex) >= 2.5.35
1096 * Systemd headers
1097   - Only required when using Systemd
1098   - Debian/Ubuntu: libsystemd-dev
1099   - RHEL/Fedora: systemd-devel
1100
1101 ### Optional features <a id="development-package-builds-optional-features"></a>
1102
1103 * MySQL (disable with CMake variable `ICINGA2_WITH_MYSQL` to `OFF`)
1104   - RHEL/Fedora: mysql-devel
1105   - SUSE: libmysqlclient-devel
1106   - Debian/Ubuntu: default-libmysqlclient-dev | libmysqlclient-dev
1107   - Alpine: mariadb-dev
1108 * PostgreSQL (disable with CMake variable `ICINGA2_WITH_PGSQL` to `OFF`)
1109   - RHEL/Fedora: postgresql-devel
1110   - Debian/Ubuntu: libpq-dev
1111   - postgresql-dev on Alpine
1112 * YAJL (Faster JSON library)
1113   - RHEL/Fedora: yajl-devel
1114   - Debian: libyajl-dev
1115   - Alpine: yajl-dev
1116 * libedit (CLI console)
1117   - RHEL/Fedora: libedit-devel on CentOS (RHEL requires rhel-7-server-optional-rpms)
1118   - Debian/Ubuntu/Alpine: libedit-dev
1119 * Termcap (only required if libedit doesn't already link against termcap/ncurses)
1120   - RHEL/Fedora: libtermcap-devel
1121   - Debian/Ubuntu: (not necessary)
1122
1123 ### Special requirements <a id="development-package-builds-special-requirements"></a>
1124
1125 **FreeBSD**: libexecinfo (automatically used when Icinga 2 is installed via port or package)
1126
1127 **RHEL6**: Requires a newer boost version which is available on packages.icinga.com
1128 with a version suffixed name.
1129
1130 ### Runtime user environment <a id="development-package-builds-runtime-user-env"></a>
1131
1132 By default Icinga will run as user `icinga` and group `icinga`. Additionally the
1133 external command pipe and livestatus features require a dedicated command group
1134 `icingacmd`. You can choose your own user/group names and pass them to CMake
1135 using the `ICINGA2_USER`, `ICINGA2_GROUP` and `ICINGA2_COMMAND_GROUP` variables.
1136
1137 ```
1138 # groupadd icinga
1139 # groupadd icingacmd
1140 # useradd -c "icinga" -s /sbin/nologin -G icingacmd -g icinga icinga
1141 ```
1142
1143 On Alpine (which uses ash busybox) you can run:
1144
1145 ```
1146 # addgroup -S icinga
1147 # addgroup -S icingacmd
1148 # adduser -S -D -H -h /var/spool/icinga2 -s /sbin/nologin -G icinga -g icinga icinga
1149 # adduser icinga icingacmd
1150 ```
1151
1152 Add the web server user to the icingacmd group in order to grant it write
1153 permissions to the external command pipe and livestatus socket:
1154
1155 ```
1156 # usermod -a -G icingacmd www-data
1157 ```
1158
1159 Make sure to replace "www-data" with the name of the user your web server
1160 is running as.
1161
1162 ### Building Icinga 2: Example <a id="development-package-builds-example"></a>
1163
1164 Once you have installed all the necessary build requirements you can build
1165 Icinga 2 using the following commands:
1166
1167 ```
1168 $ mkdir release && cd release
1169 $ cmake ..
1170 $ cd ..
1171 $ make -C release
1172 $ make install -C release
1173 ```
1174
1175 You can specify an alternative installation prefix using `-DCMAKE_INSTALL_PREFIX`:
1176
1177 ```
1178 $ cmake .. -DCMAKE_INSTALL_PREFIX=/tmp/icinga2
1179 ```
1180
1181 ### CMake Variables <a id="development-package-builds-cmake-variables"></a>
1182
1183 In addition to `CMAKE_INSTALL_PREFIX` here are most of the supported Icinga-specific cmake variables.
1184
1185 For all variables regarding defaults paths on in CMake, see
1186 [GNUInstallDirs](https://cmake.org/cmake/help/latest/module/GNUInstallDirs.html).
1187
1188 Also see `CMakeLists.txt` for details.
1189
1190 **System Environment**
1191 - `CMAKE_INSTALL_SYSCONFDIR`: The configuration directory; defaults to `CMAKE_INSTALL_PREFIX/etc`
1192 - `CMAKE_INSTALL_LOCALSTATEDIR`: The state directory; defaults to `CMAKE_INSTALL_PREFIX/var`
1193 - `ICINGA2_CONFIGDIR`: Main config directory; defaults to `CMAKE_INSTALL_SYSCONFDIR/icinga2` usually `/etc/icinga2`
1194 - `ICINGA2_CACHEDIR`: Directory for cache files; defaults to `CMAKE_INSTALL_LOCALSTATEDIR/cache/icinga2` usually `/var/cache/icinga2`
1195 - `ICINGA2_DATADIR`: Data directory  for the daemon; defaults to `CMAKE_INSTALL_LOCALSTATEDIR/lib/icinga2` usually `/var/lib/icinga2`
1196 - `ICINGA2_LOGDIR`: Logfiles of the daemon; defaults to `CMAKE_INSTALL_LOCALSTATEDIR/log/icinga2 usually `/var/log/icinga2`
1197 - `ICINGA2_SPOOLDIR`: Spooling directory ; defaults to `CMAKE_INSTALL_LOCALSTATEDIR/spool/icinga2` usually `/var/spool/icinga2`
1198 - `ICINGA2_INITRUNDIR`: Runtime data for the init system; defaults to `CMAKE_INSTALL_LOCALSTATEDIR/run/icinga2` usually `/run/icinga2`
1199 - `ICINGA2_GIT_VERSION_INFO`: Whether to use Git to determine the version number; defaults to `ON`
1200 - `ICINGA2_USER`: The user Icinga 2 should run as; defaults to `icinga`
1201 - `ICINGA2_GROUP`: The group Icinga 2 should run as; defaults to `icinga`
1202 - `ICINGA2_COMMAND_GROUP`: The command group Icinga 2 should use; defaults to `icingacmd`
1203 - `ICINGA2_SYSCONFIGFILE`: Where to put the config file the initscript/systemd pulls it's dirs from;
1204   defaults to `CMAKE_INSTALL_PREFIX/etc/sysconfig/icinga2`
1205 - `ICINGA2_PLUGINDIR`: The path for the Monitoring Plugins project binaries; defaults to `/usr/lib/nagios/plugins`
1206
1207 **Build Optimization**
1208 - `ICINGA2_UNITY_BUILD`: Whether to perform a unity build; defaults to `ON`. Note: This requires additional memory and is not advised for building VMs, Docker for Mac and embedded hardware.
1209 - `ICINGA2_LTO_BUILD`: Whether to use link time optimization (LTO); defaults to `OFF`
1210
1211 **Init System**
1212 - `USE_SYSTEMD=ON|OFF`: Use systemd or a classic SysV initscript; defaults to `OFF`
1213 - `INSTALL_SYSTEMD_SERVICE_AND_INITSCRIPT=ON|OFF` Force install both the systemd service definition file
1214   and the SysV initscript in parallel, regardless of how `USE_SYSTEMD` is set.
1215   Only use this for special packaging purposes and if you know what you are doing.
1216   Defaults to `OFF`.
1217
1218 **Features:**
1219 - `ICINGA2_WITH_CHECKER`: Determines whether the checker module is built; defaults to `ON`
1220 - `ICINGA2_WITH_COMPAT`: Determines whether the compat module is built; defaults to `ON`
1221 - `ICINGA2_WITH_DEMO`: Determines whether the demo module is built; defaults to `OFF`
1222 - `ICINGA2_WITH_HELLO`: Determines whether the hello module is built; defaults to `OFF`
1223 - `ICINGA2_WITH_LIVESTATUS`: Determines whether the Livestatus module is built; defaults to `ON`
1224 - `ICINGA2_WITH_NOTIFICATION`: Determines whether the notification module is built; defaults to `ON`
1225 - `ICINGA2_WITH_PERFDATA`: Determines whether the perfdata module is built; defaults to `ON`
1226 - `ICINGA2_WITH_TESTS`: Determines whether the unit tests are built; defaults to `ON`
1227
1228 **MySQL or MariaDB:**
1229
1230 The following settings can be tuned for the MySQL / MariaDB IDO feature.
1231
1232 - `ICINGA2_WITH_MYSQL`: Determines whether the MySQL IDO module is built; defaults to `ON`
1233 - `MYSQL_CLIENT_LIBS`: Client implementation used (mysqlclient / mariadbclient); defaults searches for `mysqlclient` and `mariadbclient`
1234 - `MYSQL_INCLUDE_DIR`: Directory containing include files for the mysqlclient; default empty -
1235   checking multiple paths like `/usr/include/mysql`
1236
1237 See [FindMySQL.cmake](third-party/cmake/FindMySQL.cmake) for the implementation.
1238
1239 **PostgreSQL:**
1240
1241 The following settings can be tuned for the PostgreSQL IDO feature.
1242
1243 - `ICINGA2_WITH_PGSQL`: Determines whether the PostgreSQL IDO module is built; defaults to `ON`
1244 - `PostgreSQL_INCLUDE_DIR`: Top-level directory containing the PostgreSQL include directories
1245 - `PostgreSQL_LIBRARY`: File path to PostgreSQL library : libpq.so (or libpq.so.[ver] file)
1246
1247 See [FindMySQL.cmake](third-party/cmake/FindPostgreSQL.cmake) for the implementation.
1248
1249 **Version detection:**
1250
1251 CMake determines the Icinga 2 version number using `git describe` if the
1252 source directory is contained in a Git repository. Otherwise the version number
1253 is extracted from the [VERSION](VERSION) file. This behavior can be
1254 overridden by creating a file called `icinga-version.h.force` in the source
1255 directory. Alternatively the `-DICINGA2_GIT_VERSION_INFO=OFF` option for CMake
1256 can be used to disable the usage of `git describe`.
1257
1258
1259 ### Building RPMs <a id="development-package-builds-rpms"></a>
1260
1261 #### Build Environment on RHEL, CentOS, Fedora, Amazon Linux
1262
1263 Setup your build environment:
1264
1265 ```
1266 yum -y install rpmdevtools
1267 ```
1268
1269 #### Build Environment on SuSE/SLES
1270
1271 SLES:
1272
1273 ```
1274 zypper addrepo http://download.opensuse.org/repositories/devel:tools/SLE_12_SP4/devel:tools.repo
1275 zypper refresh
1276 zypper install rpmdevtools spectool
1277 ```
1278
1279 OpenSuSE:
1280
1281 ```
1282 zypper addrepo http://download.opensuse.org/repositories/devel:tools/openSUSE_Leap_15.0/devel:tools.repo
1283 zypper refresh
1284 zypper install rpmdevtools spectool
1285 ```
1286
1287 #### Package Builds <a id="development-package-builds-rpms-package-builds"></a>
1288
1289 Prepare the rpmbuild directory tree:
1290
1291 ```
1292 cd $HOME
1293 rpmdev-setuptree
1294 ```
1295
1296 Snapshot builds:
1297
1298 ```
1299 curl https://raw.githubusercontent.com/Icinga/rpm-icinga2/master/icinga2.spec -o $HOME/rpmbuild/SPECS/icinga2.spec
1300 ```
1301
1302 > **Note**
1303 >
1304 > The above command builds snapshot packages. Change to the `release` branch
1305 > for release package builds.
1306
1307 Copy the tarball to `rpmbuild/SOURCES` e.g. by using the `spectool` binary
1308 provided with `rpmdevtools`:
1309
1310 ```
1311 cd $HOME/rpmbuild/SOURCES
1312 spectool -g ../SPECS/icinga2.spec
1313
1314 cd $HOME/rpmbuild
1315 ```
1316
1317 Install the build dependencies. Example for CentOS 7:
1318
1319 ```
1320 yum -y install libedit-devel ncurses-devel gcc-c++ libstdc++-devel openssl-devel \
1321 cmake flex bison boost-devel systemd mysql-devel postgresql-devel httpd \
1322 selinux-policy-devel checkpolicy selinux-policy selinux-policy-doc
1323 ```
1324
1325 Note: If you are using Amazon Linux, systemd is not required.
1326
1327 A shorter way is available using the `yum-builddep` command on RHEL based systems:
1328
1329 ```
1330 yum-builddep SPECS/icinga2.spec
1331 ```
1332
1333 Build the RPM:
1334
1335 ```
1336 rpmbuild -ba SPECS/icinga2.spec
1337 ```
1338
1339 #### Additional Hints <a id="development-package-builds-rpms-additional-hints"></a>
1340
1341 ##### SELinux policy module
1342
1343 The following packages are required to build the SELinux policy module:
1344
1345 * checkpolicy
1346 * selinux-policy (selinux-policy on CentOS 6, selinux-policy-devel on CentOS 7)
1347 * selinux-policy-doc
1348
1349 ##### RHEL/CentOS 6
1350
1351 The RedHat Developer Toolset is required for building Icinga 2 beforehand.
1352 This contains a modern version of flex and a C++ compiler which supports
1353 C++11 features.
1354 ```
1355 cat >/etc/yum.repos.d/devtools-2.repo <<REPO
1356 [testing-devtools-2-centos-\$releasever]
1357 name=testing 2 devtools for CentOS $releasever
1358 baseurl=https://people.centos.org/tru/devtools-2/\$releasever/\$basearch/RPMS
1359 gpgcheck=0
1360 REPO
1361 ```
1362
1363 Dependencies to devtools-2 are used in the RPM SPEC, so the correct tools
1364 should be used for building.
1365
1366 As an alternative, you can use newer Boost packages provided on
1367 [packages.icinga.com](https://packages.icinga.com/epel).
1368 ```
1369 cat >$HOME/.rpmmacros <<MACROS
1370 %build_icinga_org 1
1371 MACROS
1372 ```
1373
1374 ##### Amazon Linux
1375
1376 If you prefer to build packages offline, a suitable Vagrant box is located
1377 [here](https://atlas.hashicorp.com/mvbcoding/boxes/awslinux/).
1378
1379 ##### SLES 11
1380
1381 The Icinga repository provides the required boost package version and must be
1382 added before building.
1383
1384 ### Build Debian/Ubuntu packages <a id="development-package-builds-deb"></a>
1385
1386 Setup your build environment on Debian/Ubuntu, copy the 'debian' directory from
1387 the Debian packaging Git repository (https://github.com/Icinga/deb-icinga2)
1388 into your source tree and run the following command:
1389
1390 ```
1391 dpkg-buildpackage -uc -us
1392 ```
1393
1394 ### Build Alpine Linux packages <a id="development-package-builds-alpine"></a>
1395
1396 A simple way to setup a build environment is installing Alpine in a chroot.
1397 In this way, you can set up an Alpine build environment in a chroot under a
1398 different Linux distro.
1399 There is a script that simplifies these steps with just two commands, and
1400 can be found [here](https://github.com/alpinelinux/alpine-chroot-install).
1401
1402 Once the build environment is installed, you can setup the system to build
1403 the packages by following [this document](https://wiki.alpinelinux.org/wiki/Creating_an_Alpine_package).
1404
1405 ### Build Post Install Tasks <a id="development-package-builds-post-install-tasks"></a>
1406
1407 After building Icinga 2 yourself, your package build system should at least run the following post
1408 install requirements:
1409
1410 * enable the `checker`, `notification` and `mainlog` feature by default
1411 * run 'icinga2 api setup' in order to enable the `api` feature and generate SSL certificates for the node
1412
1413 ### Run Icinga 2 <a id="development-package-builds-run-icinga"></a>
1414
1415 Icinga 2 comes with a binary that takes care of loading all the relevant
1416 components (e.g. for check execution, notifications, etc.):
1417
1418 ```
1419 icinga2 daemon
1420
1421 [2016-12-08 16:44:24 +0100] information/cli: Icinga application loader (version: v2.5.4-231-gb10a6b7; debug)
1422 [2016-12-08 16:44:24 +0100] information/cli: Loading configuration file(s).
1423 [2016-12-08 16:44:25 +0100] information/ConfigItem: Committing config item(s).
1424 ...
1425 ```
1426
1427 #### Init Script <a id="development-package-builds-init-script"></a>
1428
1429 Icinga 2 can be started as a daemon using the provided init script:
1430
1431 ```
1432 # /etc/init.d/icinga2
1433 Usage: /etc/init.d/icinga2 {start|stop|restart|reload|checkconfig|status}
1434 ```
1435
1436 ### Systemd <a id="development-package-builds-systemd"></a>
1437
1438 If your distribution uses Systemd:
1439
1440 ```
1441 # systemctl {start|stop|reload|status|enable|disable} icinga2
1442 ```
1443
1444 In case the distribution is running Systemd >227, you'll also
1445 need to package and install the `etc/initsystem/icinga2.service.limits.conf`
1446 file into `/etc/systemd/system/icinga2.service.d`.
1447
1448 ### openrc <a id="development-package-builds-openrc"></a>
1449
1450 Or if your distribution uses openrc (like Alpine):
1451
1452 ```
1453 # rc-service icinga2
1454 Usage: /etc/init.d/icinga2 {start|stop|restart|reload|checkconfig|status}
1455   ```
1456
1457 Note: the openrc's init.d is not shipped by default.
1458 A working init.d with openrc can be found here: (https://git.alpinelinux.org/cgit/aports/plain/community/icinga2/icinga2.initd). If you have customized some path, edit the file and adjust it according with your setup.
1459 Those few steps can be followed:
1460
1461 ```
1462 # wget https://git.alpinelinux.org/cgit/aports/plain/community/icinga2/icinga2.initd
1463 # mv icinga2.initd /etc/init.d/icinga2
1464 # chmod +x /etc/init.d/icinga2
1465 ```
1466
1467 Icinga 2 reads a single configuration file which is used to specify all
1468 configuration settings (global settings, hosts, services, etc.). The
1469 configuration format is explained in detail in the [doc/](doc/) directory.
1470
1471 By default `make install` installs example configuration files in
1472 `/usr/local/etc/icinga2` unless you have specified a different prefix or
1473 sysconfdir.
1474
1475
1476 ### Windows Builds <a id="development-package-builds-windows"></a>
1477
1478 The Windows MSI packages are located at https://packages.icinga.com/windows/
1479
1480 #### Requirements <a id="development-package-builds-windows-requirements"></a>
1481
1482 * 32 or 64-bit system
1483 * Visual Studio >= 14 2015
1484 * CMake >= 2.6
1485 * OpenSSL >= 1.0.1
1486 * Flex and Bison
1487
1488 ##### Visual Studio
1489
1490 Download the community edition from [visualstudio.com](https://www.visualstudio.com/en/downloads/)
1491
1492 Workloads to install:
1493 * C++ Desktop
1494 * .NET Desktop
1495
1496 ##### OpenSSL for Icinga
1497
1498 Download custom OpenSSL builds from [openssl-windows GitHub project](https://github.com/Icinga/openssl-windows/releases).
1499
1500 You need to install a binary dist version to 'C:\\Program Files\\OpenSSL'.
1501
1502 The Powershell script `.\tools\win32\download-openssl.ps1` can be used for automated downloads.
1503
1504 ##### Chocolatey
1505
1506 A simple package manager for Windows, please see [install instructions](https://chocolatey.org/install).
1507
1508 ##### Git
1509
1510 Use Chocolatey, see [package details](https://chocolatey.org/packages/git).
1511
1512 ```
1513 choco install git
1514 ```
1515
1516 ##### Flex / Bison
1517
1518 Use Chocolatey, see [package details](https://chocolatey.org/packages/winflexbison3).
1519
1520 ```
1521 choco install winflexbison3
1522 ```
1523
1524 ##### CMake
1525
1526 Use Chocolatey, see [package details](https://chocolatey.org/packages/cmake)
1527 or download from: [cmake.org](https://cmake.org/download/)
1528
1529 ```
1530 choco install cmake
1531 ```
1532
1533 ##### WIX
1534
1535 Use Chocolatey, see [package details](https://chocolatey.org/packages/wixtoolset).
1536
1537 ```
1538 choco install wixtoolset
1539 ```
1540
1541 ##### Boost
1542
1543 Download third party Windows binaries from: [boost.org](http://www.boost.org/users/download/)
1544
1545 For example: `https://dl.bintray.com/boostorg/release/1.65.1/binaries/boost_1_65_1-msvc-14.1-64.exe`
1546
1547 *Warning:*
1548 * Must match your Visual Studio version!
1549 * CMake might not support the latest Boost version (we used CMake 3.10 and Boost 1_65_1)
1550
1551 Run the installer exe.
1552
1553
1554 #### Build Icinga 2
1555
1556 Run with VC Native x64 Command Prompt:
1557
1558 ```
1559 powershell .\tools\win32\configure.ps1
1560 powershell .\tools\win32\build.ps1
1561 powershell .\tools\win32\test.ps1
1562 ```
1563
1564 See these scripts for details.
1565
1566 #### CI: AppVeyor
1567
1568 We are building [Icinga 2 with AppVeyor](https://ci.appveyor.com/project/icinga/icinga2) for testing and CI integration.
1569
1570 Please check `appveyor.yml` for instructions.
1571
1572
1573
1574 ## Advanced Development Tips <a id="development-advanced"></a>
1575
1576 ### GDB Pretty Printers <a id="development-advanced-gdb-pretty-printer"></a>
1577
1578 Install the `boost`, `python` and `icinga2` pretty printers. Absolute paths are required,
1579 so please make sure to update the installation paths accordingly (`pwd`).
1580
1581     $ mkdir -p ~/.gdb_printers && cd ~/.gdb_printers
1582
1583 Boost Pretty Printers compatible with Python 3:
1584
1585     $ git clone https://github.com/mateidavid/Boost-Pretty-Printer.git && cd Boost-Pretty-Printer
1586     $ git checkout python-3
1587     $ pwd
1588     /home/michi/.gdb_printers/Boost-Pretty-Printer
1589
1590 Python Pretty Printers:
1591
1592     $ cd ~/.gdb_printers
1593     $ svn co svn://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python
1594
1595 Icinga 2 Pretty Printers:
1596
1597     $ mkdir -p ~/.gdb_printers/icinga2 && cd ~/.gdb_printers/icinga2
1598     $ wget https://raw.githubusercontent.com/Icinga/icinga2/master/tools/debug/gdb/icingadbg.py
1599
1600 Now you'll need to modify/setup your `~/.gdbinit` configuration file.
1601 You can download the one from Icinga 2 and modify all paths.
1602
1603 Example on Fedora 22:
1604
1605     $ wget https://raw.githubusercontent.com/Icinga/icinga2/master/tools/debug/gdb/gdbinit -O ~/.gdbinit
1606     $ vim ~/.gdbinit
1607
1608     set print pretty on
1609     
1610     python
1611     import sys
1612     sys.path.insert(0, '/home/michi/.gdb_printers/icinga2')
1613     from icingadbg import register_icinga_printers
1614     register_icinga_printers()
1615     end
1616     
1617     python
1618     import sys
1619     sys.path.insert(0, '/home/michi/.gdb_printers/python')
1620     from libstdcxx.v6.printers import register_libstdcxx_printers
1621     try:
1622         register_libstdcxx_printers(None)
1623     except:
1624         pass
1625     end
1626     
1627     python
1628     import sys
1629     sys.path.insert(0, '/home/michi/.gdb_printers/Boost-Pretty-Printer')
1630     import boost_print
1631     boost_print.register_printers()
1632     end
1633
1634
1635 If you are getting the following error when running gdb, the `libstdcxx`
1636 printers are already preloaded in your environment and you can remove
1637 the duplicate import in your `~/.gdbinit` file.
1638
1639     RuntimeError: pretty-printer already registered: libstdc++-v6
1640
1641
1642 ## Development Tests <a id="development-tests"></a>
1643
1644 Build the binaries and run the tests.
1645
1646
1647 ```
1648 make -j4 -C debug
1649 make test -C debug
1650 ```
1651
1652 Run a specific boost test:
1653
1654 ```
1655 debug/Bin/Debug/boosttest-test-base --run_test=remote_url
1656 ```
1657