1 # Development <a id="development"></a>
3 This chapter provides hints on Icinga 2 debugging,
4 development, package builds and tests.
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 * [Test Icinga 2](21-development.md#development-tests)
10 * [Snapshot Packages (Nightly Builds)](21-development.md#development-tests-snapshot-packages)
11 * [Develop Icinga 2](21-development.md#development-develop)
12 * [Preparations](21-development.md#development-develop-prepare)
13 * [Design Patterns](21-development.md#development-develop-design-patterns)
14 * [Build Tools](21-development.md#development-develop-builds-tools)
15 * [Unit Tests](21-development.md#development-develop-tests)
16 * [Style Guide](21-development.md#development-develop-styleguide)
17 * [Development Environment](21-development.md#development-environment)
18 * [Linux Dev Environment](21-development.md#development-linux-dev-env)
19 * [macOS Dev Environment](21-development.md#development-macos-dev-env)
20 * [Windows Dev Environment](21-development.md#development-windows-dev-env)
21 * [Package Builds](21-development.md#development-package-builds)
22 * [RPM](21-development.md#development-package-builds-rpms)
23 * [DEB](21-development.md#development-package-builds-deb)
24 * [Windows](21-development.md#development-package-builds-windows)
25 * [Advanced Tips](21-development.md#development-advanced)
27 <!-- mkdocs requires 4 spaces indent for nested lists: https://github.com/Python-Markdown/markdown/issues/3 -->
29 ## Debug Icinga 2 <a id="development-debug"></a>
31 This chapter targets all users who have been asked by developers to provide
32 a stack trace or coredump if the application crashed. It is also useful
33 for developers working with different debuggers.
37 > This is intentionally mentioned before any development insights
38 > as debugging is a more frequent and commonly asked question.
40 ### Debug Requirements <a id="debug-requirements"></a>
42 Make sure that the debug symbols are available for Icinga 2.
43 The Icinga 2 packages provide a debug package which must be
44 installed separately for all involved binaries, like `icinga2-bin`
45 or `icinga2-ido-mysql`.
47 Distribution | Command
48 -------------------|------------------------------------------
49 Debian/Ubuntu | `apt-get install icinga2-dbg`
50 RHEL/CentOS | `yum install icinga2-debuginfo`
51 Fedora | `dnf install icinga2-debuginfo icinga2-bin-debuginfo icinga2-ido-mysql-debuginfo`
52 SLES/openSUSE | `zypper install icinga2-bin-debuginfo icinga2-ido-mysql-debuginfo`
54 Furthermore, you may also have to install debug symbols for Boost and your C++ library.
56 If you're building your own binaries, you should use the `-DCMAKE_BUILD_TYPE=Debug` cmake
57 build flag for debug builds.
60 ### GDB as Debugger <a id="development-debug-gdb"></a>
62 Install GDB in your development environment.
64 Distribution | Command
65 -------------------|------------------------------------------
66 Debian/Ubuntu | `apt-get install gdb`
67 RHEL/CentOS | `yum install gdb`
68 Fedora | `dnf install gdb`
69 SLES/openSUSE | `zypper install gdb`
71 #### GDB Run <a id="development-debug-gdb-run"></a>
73 Call GDB with the binary (`/usr/sbin/icinga2` is a wrapper script calling
74 `/usr/lib64/icinga2/sbin/icinga2` since 2.4) and all arguments and run it in foreground.
77 gdb --args /usr/lib64/icinga2/sbin/icinga2 daemon -x debug
80 The exact path to the Icinga 2 binary differs on each distribution. On Ubuntu
81 it is installed into `/usr/lib/x86_64-linux-gnu/icinga2/sbin/icinga2` on 64-bit systems
86 > If gdb tells you it's missing debug symbols, quit gdb and install
87 > them: `Missing separate debuginfos, use: debuginfo-install ...`
89 Run/restart the application.
95 Kill the running application.
101 Continue after breakpoint.
107 #### GDB Core Dump <a id="development-debug-gdb-coredump"></a>
109 Either attach to the running process using `gdb -p PID` or start
114 (gdb) generate-core-file
117 #### GDB Backtrace <a id="development-debug-gdb-backtrace"></a>
119 If Icinga 2 aborted its operation abnormally, generate a backtrace.
123 > Please install the [required debug symbols](21-development.md#debug-requirements)
124 > prior to generating a backtrace.
126 `thread apply all` is important here since this includes all running threads.
127 We need this information when e.g. debugging dead locks and hanging features.
131 (gdb) thread apply all bt full
134 If gdb stops at a SIGPIPE signal please disable the signal before
135 running Icinga 2. This isn't an error, but we need to workaround it.
138 (gdb) handle SIGPIPE nostop noprint pass
142 If you create a [new issue](https://github.com/Icinga/icinga2/issues),
143 make sure to attach as much detail as possible.
145 #### GDB Backtrace from Running Process <a id="development-debug-gdb-backtrace-running"></a>
147 If Icinga 2 is still running, generate a full backtrace from the running
148 process and store it into a new file (e.g. for debugging dead locks).
152 > Please install the [required debug symbols](21-development.md#debug-requirements)
153 > prior to generating a backtrace.
155 Icinga 2 runs with 2 processes: main and command executor, therefore generate two backtrace logs
156 and add them to the GitHub issue.
159 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
162 #### GDB Thread List from Running Process <a id="development-debug-gdb-thread-list-running"></a>
164 Instead of a full backtrace, you sometimes just need a list of running threads.
167 for pid in $(pidof icinga2); do gdb -p $pid -batch -ex "info threads" -ex "detach" -ex "q" > gdb_threads_${pid}_`date +%s`.log; done
170 #### GDB Backtrace Stepping <a id="development-debug-gdb-backtrace-stepping"></a>
172 Identifying the problem may require stepping into the backtrace, analysing
173 the current scope, attributes, and possible unmet requirements. `p` prints
174 the value of the selected variable or function call result.
180 (gdb) p checkable.px->m_Name
183 #### GDB Breakpoints <a id="development-debug-gdb-breakpoint"></a>
185 To set a breakpoint to a specific function call, or file specific line.
188 (gdb) b checkable.cpp:125
189 (gdb) b icinga::Checkable::SetEnablePerfdata
192 GDB will ask about loading the required symbols later, select `yes` instead
195 Then run Icinga 2 until it reaches the first breakpoint. Continue with `c`
203 In case you want to step into the next line of code, use `n`. If there is a
204 function call where you want to step into, use `s`.
212 If you want to delete all breakpoints, use `d` and select `yes`.
220 > When debugging exceptions, set your breakpoint like this: `b __cxa_throw`.
230 #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)
231 at /home/michi/coding/icinga/icinga2/lib/base/utility.cpp:609
235 605 #endif /* _WIN32 */
237 607 std::sort(files.begin(), files.end());
238 608 BOOST_FOREACH(const String& cpath, files) {
242 612 std::sort(dirs.begin(), dirs.end());
243 613 BOOST_FOREACH(const String& cpath, dirs) {
245 $3 = std::vector of length 11, capacity 16 = {{static NPos = 18446744073709551615, m_Data = "/etc/icinga2/conf.d/agent.conf"}, {static NPos = 18446744073709551615,
246 m_Data = "/etc/icinga2/conf.d/commands.conf"}, {static NPos = 18446744073709551615, m_Data = "/etc/icinga2/conf.d/downtimes.conf"}, {static NPos = 18446744073709551615,
247 m_Data = "/etc/icinga2/conf.d/groups.conf"}, {static NPos = 18446744073709551615, m_Data = "/etc/icinga2/conf.d/notifications.conf"}, {static NPos = 18446744073709551615,
248 m_Data = "/etc/icinga2/conf.d/satellite.conf"}, {static NPos = 18446744073709551615, m_Data = "/etc/icinga2/conf.d/services.conf"}, {static NPos = 18446744073709551615,
249 m_Data = "/etc/icinga2/conf.d/templates.conf"}, {static NPos = 18446744073709551615, m_Data = "/etc/icinga2/conf.d/test.conf"}, {static NPos = 18446744073709551615,
250 m_Data = "/etc/icinga2/conf.d/timeperiods.conf"}, {static NPos = 18446744073709551615, m_Data = "/etc/icinga2/conf.d/users.conf"}}
254 ### Core Dump <a id="development-debug-core-dump"></a>
256 When the Icinga 2 daemon crashes with a `SIGSEGV` signal
257 a core dump file should be written. This will help
258 developers to analyze and fix the problem.
260 #### Core Dump File Size Limit <a id="development-debug-core-dump-limit"></a>
262 This requires setting the core dump file size to `unlimited`.
268 systemctl edit icinga2.service
274 systemctl daemon-reload
276 systemctl restart icinga2
282 vim /etc/init.d/icinga2
286 service icinga2 restart
291 Verify that the Icinga 2 process core file size limit is set to `unlimited`.
294 for pid in $(pidof icinga2); do cat /proc/$pid/limits; done
297 Max core file size unlimited unlimited bytes
301 #### Core Dump Kernel Format <a id="development-debug-core-dump-format"></a>
303 The Icinga 2 daemon runs with the SUID bit set. Therefore you need
304 to explicitly enable core dumps for SUID on Linux.
307 sysctl -w fs.suid_dumpable=2
310 Adjust the coredump kernel format and file location on Linux:
313 sysctl -w kernel.core_pattern=/var/lib/cores/core.%e.%p
315 install -m 1777 -d /var/lib/cores
321 sysctl -w kern.corefile=/cores/core.%P
326 #### Core Dump Analysis <a id="development-debug-core-dump-analysis"></a>
328 Once Icinga 2 crashes again a new coredump file will be written. Please
329 attach this file to your bug report in addition to the general details.
331 Simple test case for a `SIGSEGV` simulation with `sleep`:
338 gdb `which sleep` /var/lib/cores/core.sleep.<PID>
340 rm /var/lib/cores/core.sleep.*
346 gdb /usr/lib64/icinga2/sbin/icinga2 core.icinga2.<PID>
350 ### LLDB as Debugger <a id="development-debug-lldb"></a>
352 LLDB is available on macOS with the Xcode command line tools.
355 $ xcode-select --install
358 In order to run Icinga 2 with LLDB you need to pass the binary as argument.
361 lldb -- /usr/local/icinga2/lib/icinga2/sbin/icinga2 daemon
368 > b icinga::Checkable::ProcessCheckResult
401 Up/down in stacktrace:
409 ### Debug on Windows <a id="development-debug-windows"></a>
412 Whenever the application crashes, the Windows error reporting (WER) can be [configured](https://docs.microsoft.com/en-gb/windows/win32/wer/collecting-user-mode-dumps)
413 to create user-mode dumps.
416 Tail the log file with Powershell:
419 Get-Content .\icinga2.log -tail 10 -wait
423 #### Debug on Windows: Dependencies <a id="development-debug-windows-dependencies"></a>
425 Similar to `ldd` or `nm` on Linux/Unix.
427 Extract the dependent DLLs from a binary with Visual Studio's `dumpbin` tool
431 C:> &'C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.22.27905\bin\Hostx64\x64\dumpbin.exe' /dependents .\debug\Bin\Debug\Debug\boosttest-test-base.exe
432 DEBUG: 1+ >>>> &'C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.22.27905\bin\Hostx64\x64\dumpbin.exe' /dependents .\debug\Bin\Debug\Debug\boosttest-test-base.exe
433 Microsoft (R) COFF/PE Dumper Version 14.22.27905.0
434 Copyright (C) Microsoft Corporation. All rights reserved.
437 Dump of file .\debug\Bin\Debug\Debug\boosttest-test-base.exe
439 File Type: EXECUTABLE IMAGE
441 Image has the following dependencies:
443 boost_coroutine-vc142-mt-gd-x64-1_71.dll
444 boost_date_time-vc142-mt-gd-x64-1_71.dll
445 boost_filesystem-vc142-mt-gd-x64-1_71.dll
446 boost_thread-vc142-mt-gd-x64-1_71.dll
447 boost_regex-vc142-mt-gd-x64-1_71.dll
449 libcrypto-1_1-x64.dll
454 boost_unit_test_framework-vc142-mt-gd-x64-1_71.dll
478 ## Test Icinga 2 <a id="development-tests"></a>
480 ### Snapshot Packages (Nightly Builds) <a id="development-tests-snapshot-packages"></a>
482 Icinga provides snapshot packages as nightly builds from [Git master](https://github.com/icinga/icinga2).
484 These packages contain development code which should be considered "work in progress".
485 While developers ensure that tests are running fine with CI actions on PRs,
486 things might break, or changes are not yet documented in the changelog.
488 You can help the developers and test the snapshot packages, e.g. when larger
489 changes or rewrites are taking place for a new major version. Your feedback
490 is very much appreciated.
492 Snapshot packages are available for all supported platforms including
493 Linux and Windows and can be obtained from [https://packages.icinga.com](https://packages.icinga.com).
495 The [Vagrant boxes](https://github.com/Icinga/icinga-vagrant) also use
496 the Icinga snapshot packages to allow easier integration tests. It is also
497 possible to use Docker with base OS images and installing the snapshot
500 If you encounter a problem, please [open a new issue](https://github.com/Icinga/icinga2/issues/new/choose)
501 on GitHub and mention that you're testing the snapshot packages.
503 #### RHEL/CentOS <a id="development-tests-snapshot-packages-rhel"></a>
505 2.11+ requires the [EPEL repository](02-installation.md#package-repositories-rhel-epel) for Boost 1.66+.
507 In addition to that, the `icinga-rpm-release` package already provides the `icinga-snapshot-builds`
508 repository but it is disabled by default.
511 yum -y install https://packages.icinga.com/epel/icinga-rpm-release-7-latest.noarch.rpm
512 yum -y install epel-release
515 yum install --enablerepo=icinga-snapshot-builds icinga2
518 #### Debian <a id="development-tests-snapshot-packages-debian"></a>
520 2.11+ requires Boost 1.66+ which either is provided by the OS, backports or Icinga stable repositories.
521 It is advised to configure both Icinga repositories, stable and snapshot and selectively
522 choose the repository with the `-t` flag on `apt-get install`.
526 apt-get -y install apt-transport-https wget gnupg
528 wget -O - https://packages.icinga.com/icinga.key | apt-key add -
530 DIST=$(awk -F"[)(]+" '/VERSION=/ {print $2}' /etc/os-release); \
531 echo "deb https://packages.icinga.com/debian icinga-${DIST} main" > \
532 /etc/apt/sources.list.d/${DIST}-icinga.list
533 echo "deb-src https://packages.icinga.com/debian icinga-${DIST} main" >> \
534 /etc/apt/sources.list.d/${DIST}-icinga.list
536 DIST=$(awk -F"[)(]+" '/VERSION=/ {print $2}' /etc/os-release); \
537 echo "deb http://packages.icinga.com/debian icinga-${DIST}-snapshots main" > \
538 /etc/apt/sources.list.d/${DIST}-icinga-snapshots.list
539 echo "deb-src http://packages.icinga.com/debian icinga-${DIST}-snapshots main" >> \
540 /etc/apt/sources.list.d/${DIST}-icinga-snapshots.list
545 On Debian Stretch, you'll also need to add Debian Backports.
548 DIST=$(awk -F"[)(]+" '/VERSION=/ {print $2}' /etc/os-release); \
549 echo "deb https://deb.debian.org/debian ${DIST}-backports main" > \
550 /etc/apt/sources.list.d/${DIST}-backports.list
555 Then install the snapshot packages.
558 DIST=$(awk -F"[)(]+" '/VERSION=/ {print $2}' /etc/os-release); \
559 apt-get install -t icinga-${DIST}-snapshots icinga2
562 #### Ubuntu <a id="development-tests-snapshot-packages-ubuntu"></a>
566 apt-get -y install apt-transport-https wget gnupg
568 wget -O - https://packages.icinga.com/icinga.key | apt-key add -
570 . /etc/os-release; if [ ! -z ${UBUNTU_CODENAME+x} ]; then DIST="${UBUNTU_CODENAME}"; else DIST="$(lsb_release -c| awk '{print $2}')"; fi; \
571 echo "deb https://packages.icinga.com/ubuntu icinga-${DIST} main" > \
572 /etc/apt/sources.list.d/${DIST}-icinga.list
573 echo "deb-src https://packages.icinga.com/ubuntu icinga-${DIST} main" >> \
574 /etc/apt/sources.list.d/${DIST}-icinga.list
576 . /etc/os-release; if [ ! -z ${UBUNTU_CODENAME+x} ]; then DIST="${UBUNTU_CODENAME}"; else DIST="$(lsb_release -c| awk '{print $2}')"; fi; \
577 echo "deb https://packages.icinga.com/ubuntu icinga-${DIST}-snapshots main" > \
578 /etc/apt/sources.list.d/${DIST}-icinga-snapshots.list
579 echo "deb-src https://packages.icinga.com/ubuntu icinga-${DIST}-snapshots main" >> \
580 /etc/apt/sources.list.d/${DIST}-icinga-snapshots.list
585 Then install the snapshot packages.
588 . /etc/os-release; if [ ! -z ${UBUNTU_CODENAME+x} ]; then DIST="${UBUNTU_CODENAME}"; else DIST="$(lsb_release -c| awk '{print $2}')"; fi; \
589 apt-get install -t icinga-${DIST}-snapshots icinga2
592 #### SLES <a id="development-tests-snapshot-packages-sles"></a>
594 The required Boost packages are provided with the stable release repository.
597 rpm --import https://packages.icinga.com/icinga.key
599 zypper ar https://packages.icinga.com/SUSE/ICINGA-release.repo
602 zypper ar https://packages.icinga.com/SUSE/ICINGA-snapshot.repo
606 Selectively install the snapshot packages using the `-r` parameter.
609 zypper in -r icinga-snapshot-builds icinga2
613 ### Unit Tests <a id="development-tests-unit"></a>
615 Build the binaries and run the tests.
623 Run a specific boost test:
626 debug/Bin/Debug/boosttest-test-base --run_test=remote_url
631 ## Develop Icinga 2 <a id="development-develop"></a>
633 Icinga 2 can be built on many platforms such as Linux, Unix and Windows.
634 There are limitations in terms of support, e.g. Windows is only supported for agents,
635 not a full-featured master or satellite.
637 Before you start with actual development, there is a couple of pre-requisites.
639 ### Preparations <a id="development-develop-prepare"></a>
641 #### Choose your Editor <a id="development-develop-choose-editor"></a>
643 Icinga 2 can be developed with your favorite editor. Icinga developers prefer
647 - CLion (macOS, Linux)
648 - MS Visual Studio (Windows)
651 Editors differ on the functionality. The more helpers you get for C++ development,
652 the faster your development workflow will be.
654 #### Get to know the architecture <a id="development-develop-get-to-know-the-architecture"></a>
656 Icinga 2 can run standalone or in distributed environments. It contains a whole lot
657 more than a simple check execution engine.
659 Read more about it in the [Technical Concepts](19-technical-concepts.md#technical-concepts) chapter.
661 #### Get to know the code <a id="development-develop-get-to-know-the-code"></a>
663 First off, you really need to know C++ and portions of C++11 and the boost libraries.
664 Best is to start with a book or online tutorial to get into the basics.
665 Icinga developers gained their knowledge through studies, training and self-teaching
666 code by trying it out and asking senior developers for guidance.
668 Here's a few books we can recommend:
670 * [Accelerated C++: Practical Programming by Example](https://www.amazon.com/Accelerated-C-Practical-Programming-Example/dp/020170353X) (Andrew Koenig, Barbara E. Moo)
671 * [Effective C++](https://www.amazon.com/Effective-Specific-Improve-Programs-Designs/dp/0321334876) (Scott Meyers)
672 * [Boost C++ Application Development Cookbook - Second Edition: Recipes to simplify your application development](https://www.amazon.com/dp/1787282244/ref=cm_sw_em_r_mt_dp_U_dN1OCbERS00EQ) (Antony Polukhin)
673 * [Der C++ Programmierer](https://www.amazon.de/Programmierer-lernen-Professionell-anwenden-L%C3%B6sungen/dp/3446416447), German (Ulrich Breymann)
674 * [C++11 programmieren](https://www.amazon.de/gp/product/3836217325/), German (Torsten T. Will)
676 In addition, it is a good bet to also know SQL when diving into backend development.
678 * [SQL Performance Explained](https://www.amazon.de/gp/product/3950307826/) (Markus Winand)
680 Last but not least, if you are developing on Windows, get to know the internals about services and the Win32 API.
682 ### Design Patterns <a id="development-develop-design-patterns"></a>
684 Icinga 2 heavily relies on object-oriented programming and encapsulates common
685 functionality into classes and objects. It also uses modern programming techniques
686 to e.g. work with shared pointer memory management.
688 Icinga 2 consists of libraries bundled into the main binary. Therefore you'll
689 find many code parts in the `lib/` directory wheras the actual application is
690 built from `icinga-app/`. Accompanied with Icinga 2, there's the Windows plugins
691 which are standalone and compiled from `plugins/`.
693 Library | Description
694 ---------------|------------------------------------
695 base | Objects, values, types, streams, tockets, TLS, utilities, etc.
696 config | Configuration compiler, expressions, etc.
697 cli | CLI (sub) commands and helpers.
698 icinga | Icinga specific objects and event handling.
699 remote | Cluster and HTTP client/server and REST API related code.
700 checker | Checker feature, check scheduler.
701 notification | Notification feature, notification scheduler.
702 methods | Command execution methods, plugins and built-in checks.
703 perfdata | Performance data related, including Graphite, Elastic, etc.
704 db\_ido | IDO database abstraction layer.
705 db\_ido\_mysql | IDO database driver for MySQL.
706 db\_ido\_pgsql | IDO database driver for PgSQL.
707 mysql\_shin | Library stub for linking against the MySQL client libraries.
708 pgsql\_shim | Library stub for linking against the PgSQL client libraries.
710 #### Class Compiler <a id="development-develop-design-patterns-class-compiler"></a>
712 Another thing you will recognize are the `.ti` files which are compiled
713 by our own class compiler into actual source code. The meta language allows
714 developers to easily add object attributes and specify their behaviour.
716 Some object attributes need to be stored over restarts in the state file
717 and therefore have the `state` attribute set. Others are treated as `config`
718 attribute and automatically get configuration validation functions created.
719 Hidden or read-only REST API attributes are marked with `no_user_view` and
722 The most beneficial thing are getters and setters being generated. The actual object
723 inherits from `ObjectImpl<TYPE>` and therefore gets them "for free".
728 vim lib/perfdata/gelfwriter.ti
732 vim lib/perfdata/gelfwriter.cpp
734 if (GetEnableTls()) {
737 The logic is hidden in `tools/mkclass/` in case you want to learn more about it.
738 The first steps during CMake & make also tell you about code generation.
740 ### Build Tools <a id="development-develop-builds-tools"></a>
742 #### CMake <a id="development-develop-builds-cmake"></a>
744 In its early development stages in 2012, Icinga 2 was built with autoconf/automake
745 and separate Windows project files. We've found this very fragile, and have changed
746 this into CMake as our build tool.
748 The most common benefits:
750 * Everything is described in CMakeLists.txt in each directory
751 * CMake only needs to know that a sub directory needs to be included.
752 * The global CMakeLists.txt acts as main entry point for requirement checks and library/header includes.
753 * Separate binary build directories, the actual source tree stays clean.
754 * CMake automatically generates a Visual Studio project file `icinga2.sln` on Windows.
756 #### Unity Builds <a id="development-develop-builds-unity-builds"></a>
758 Another thing you should be aware of: Unity builds on and off.
760 Typically, we already use caching mechanisms to reduce recompile time with ccache.
761 For release builds, there's always a new build needed as the difference is huge compared
762 to a previous (major) release.
764 Therefore we've invented the Unity builds, which basically concatenates all source files
765 into one big library source code file. The compiler then doesn't need to load the many small
766 files but compiles and links this huge one.
768 Unity builds require more memory which is why you should disable them for development
769 builds in small sized VMs (Linux, Windows) and also Docker containers.
771 There's a couple of header files which are included everywhere. If you touch/edit them,
772 the cache is invalidated and you need to recompile a lot more files then. `base/utility.hpp`
773 and `remote/zone.hpp` are good candidates for this.
775 ### Unit Tests <a id="development-develop-tests"></a>
777 New functions and classes must implement new unit tests. Whenever
778 you decide to add new functions, ensure that you don't need a complex
779 mock or runtime attributes in order to test them. Better isolate
780 code into function interfaces which can be invoked in the Boost tests
783 Look into the existing tests in the [test/](https://github.com/Icinga/icinga2/tree/master/test) directory
784 and adopt new test cases.
786 Specific tests require special time windows, they are only
787 enabled in debug builds for developers. This is the case e.g.
788 for testing the flapping algorithm with expected state change
789 detection at a specific point from now.
792 ### Style Guide <a id="development-develop-styleguide"></a>
794 Overview of project files:
796 File Type | File Name/Extension | Description
797 ---------------|---------------------|-----------------------------
798 Header | .hpp | Classes, enums, typedefs inside the icinga Namespace.
799 Source | .cpp | Method implementation for class functions, static/global variables.
800 CMake | CMakeLists.txt | Build configuration, source and header file references.
801 CMake Source | .cmake | Source/Header files generated from CMake placeholders.
802 ITL/conf.d | .conf | Template library and example files as configuration
803 Class Compiler | .ti | Object classes in our own language, generates source code as `<filename>-ti.{c,h}pp`.
804 Lexer/Parser | .ll, .yy | Flex/Bison code generated into source code from CMake builds.
805 Docs | .md | Markdown docs and READMEs.
807 Anything else are additional tools and scripts for developers and build systems.
809 All files must include the copyright header. We don't use the
810 current year as this implies yearly updates we don't want.
812 Depending on the file type, this must be a comment.
815 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
817 # Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+
820 #### Code Formatting <a id="development-develop-code-formatting"></a>
822 **Tabs instead of spaces.** Inside Visual Studio, choose to keep tabs instead of
823 spaces. Tabs should use 4 spaces indent by default, depending on your likings.
825 We follow the clang format, with some exceptions.
827 - Curly braces for functions and classes always start at a new line.
830 String ConfigObjectUtility::EscapeName(const String& name)
835 String ConfigObjectUtility::CreateObjectConfig(const Type::Ptr& type, const String& fullName,
836 bool ignoreOnError, const Array::Ptr& templates, const Dictionary::Ptr& attrs)
842 - Too long lines break at a parameter, the new line needs a tab indent.
845 static String CreateObjectConfig(const Type::Ptr& type, const String& fullName,
846 bool ignoreOnError, const Array::Ptr& templates, const Dictionary::Ptr& attrs);
849 - Conditions require curly braces if it is not a single if with just one line.
863 - There's a space between `if` and the opening brace `(`. Also after the closing brace `)` and opening curly brace `{`.
864 - Negation with `!` doesn't need an extra space.
865 - Else branches always start in the same line after the closing curly brace.
868 #### Code Comments <a id="development-develop-code-comments"></a>
870 Add comments wherever you think that another developer will have a hard
871 time to understand the complex algorithm. Or you might have forgotten
872 it in a year and struggle again. Also use comments to highlight specific
873 stages in a function. Generally speaking, make things easier for the
874 team and external contributors.
876 Comments can also be used to mark additional references and TODOs.
877 If there is a specific GitHub issue or discussion going on,
878 use that information as a summary and link over to it on purpose.
880 - Single line comments may use `//` or `/* ... */`
881 - Multi line comments must use this format:
884 /* Ensure to check for XY
885 * This relies on the fact that ABC has been set before.
889 #### Function Docs <a id="development-develop-function-docs"></a>
891 Function header documentation must be added. The current code basis
892 needs rework, future functions must provide this.
894 Editors like CLion or Visual Studio allow you to type `/**` followed
895 by Enter and generate the skeleton from the implemented function.
897 Add a short summary in the first line about the function's purpose.
898 Edit the param section with short description on their intention.
899 The `return` value should describe the value type and additional details.
905 * Reads a message from the connected peer.
907 * @param stream ASIO TLS Stream
908 * @param yc Yield Context for ASIO
909 * @param maxMessageLength maximum size of bytes read.
911 * @return A JSON string
913 String JsonRpc::ReadMessage(const std::shared_ptr<AsioTlsStream>& stream, boost::asio::yield_context yc, ssize_t maxMessageLength)
916 While we can generate code docs from it, the main idea behind it is
917 to provide on-point docs to fully understand all parameters and the
918 function's purpose in the same spot.
921 #### Header <a id="development-develop-styleguide-header"></a>
923 Only include other headers which are mandatory for the header definitions.
924 If the source file requires additional headers, add them there to avoid
927 The included header order is important.
929 - First, include the library header `i2-<libraryname>.hpp`, e.g. `i2-base.hpp`.
930 - Second, include all headers from Icinga itself, e.g. `remote/apilistener.hpp`. `base` before `icinga` before `remote`, etc.
931 - Third, include third-party and external library headers, e.g. openssl and boost.
932 - Fourth, include STL headers.
934 #### Source <a id="development-develop-styleguide-source"></a>
936 The included header order is important.
938 - First, include the header whose methods are implemented.
939 - Second, include all headers from Icinga itself, e.g. `remote/apilistener.hpp`. `base` before `icinga` before `remote`, etc.
940 - Third, include third-party and external library headers, e.g. openssl and boost.
941 - Fourth, include STL headers.
943 Always use an empty line after the header include parts.
945 #### Namespace <a id="development-develop-styleguide-namespace"></a>
947 The icinga namespace is used globally, as otherwise we would need to write `icinga::Utility::FormatDateTime()`.
950 using namespace icinga;
953 Other namespaces must be declared in the scope they are used. Typically
954 this is inside the function where `boost::asio` and variants would
958 namespace ssl = boost::asio::ssl;
960 auto context (std::make_shared<ssl::context>(ssl::context::sslv23));
963 #### Functions <a id="development-develop-styleguide-functions"></a>
965 Ensure to pass values and pointers as const reference. By default, all
966 values will be copied into the function scope, and we want to avoid this
970 std::vector<EventQueue::Ptr> EventQueue::GetQueuesForType(const String& type)
973 C++ only allows to return a single value. This can be abstracted with
974 returning a specific class object, or with using a map/set. Array and
975 Dictionary objects increase the memory footprint, use them only where needed.
977 A common use case for Icinga value types is where a function can return
978 different values - an object, an array, a boolean, etc. This happens in the
979 inner parts of the config compiler expressions, or config validation.
981 The function caller is responsible to determine the correct value type
982 and handle possible errors.
984 Specific algorithms may require to populate a list, which can be passed
985 by reference to the function. The inner function can then append values.
986 Do not use a global shared resource here, unless this is locked by the caller.
989 #### Conditions and Cases <a id="development-develop-styleguide-conditions"></a>
991 Prefer if-else-if-else branches. When integers are involved,
992 switch-case statements increase readability. Don't forget about `break` though!
994 Avoid using ternary operators where possible. Putting a condition
995 after an assignment complicates reading the source. The compiler
996 optimizes this anyways.
1001 int res = s == "OK" ? 0 : s == "WARNING" ? 1;
1013 } else if (s == "WARNING") {
1018 Even better: Create a lookup map instead of if branches. The complexity
1019 is reduced to O(log(n)).
1022 std::map<String, unsigned int> stateMap = {
1027 auto it = stateMap.find(s);
1029 if (it == stateMap.end()) {
1036 The code is not as short as with a ternary operator, but one can re-use
1037 this design pattern for other generic definitions with e.g. moving the
1038 lookup into a utility class.
1040 Once a unit test is written, everything works as expected in the future.
1042 #### Locks and Guards <a id="development-develop-locks-guards"></a>
1044 Lock access to resources where multiple threads can read and write.
1045 Icinga objects can be locked with the `ObjectLock` class.
1047 Object locks and guards must be limited to the scope where they are needed. Otherwise we could create dead locks.
1051 ObjectLock olock(frame.Locals);
1052 for (const Dictionary::Pair& kv : frame.Locals) {
1053 AddSuggestion(matches, word, kv.first);
1058 #### Objects and Pointers <a id="development-develop-objects-pointers"></a>
1060 Use shared pointers for objects. Icinga objects implement the `Ptr`
1061 typedef returning an `intrusive_ptr` for the class object (object.hpp).
1062 This also ensures reference counting for the object's lifetime.
1064 Use raw pointers with care!
1066 Some methods and classes require specific shared pointers, especially
1067 when interacting with the Boost library.
1069 #### Value Types <a id="development-develop-styleguide-value-types"></a>
1071 Icinga has its own value types. These provide methods to allow
1072 generic serialization into JSON for example, and other type methods
1073 which are made available in the DSL too.
1075 - Always use `String` instead of `std::string`. If you need a C-string, use the `CStr()` method.
1076 - Avoid casts and rather use the `Convert` class methods.
1079 double s = static_cast<double>(v); //Wrong
1081 double s = Convert::ToDouble(v); //Correct, ToDouble also provides overloads with different value types
1084 - Prefer STL containers for internal non-user interfaces. Icinga value types add a small overhead which may decrease performance if e.g. the function is called 100k times.
1085 - `Array::FromVector` and variants implement conversions, use them.
1087 #### Utilities <a id="development-develop-styleguide-utilities"></a>
1089 Don't re-invent the wheel. The `Utility` class provides
1090 many helper functions which allow you e.g. to format unix timestamps,
1091 search in filesystem paths.
1093 Also inspect the Icinga objects, they also provide helper functions
1094 for formatting, splitting strings, joining arrays into strings, etc.
1096 #### Libraries <a id="development-develop-styleguide-libraries"></a>
1098 2.11 depends on [Boost 1.66](https://www.boost.org/doc/libs/1_66_0/).
1099 Use the existing libraries and header-only includes
1100 for this specific version.
1102 Note: Prefer C++11 features where possible, e.g. std::atomic and lambda functions.
1106 - [exception](https://www.boost.org/doc/libs/1_66_0/libs/exception/doc/boost-exception.html) (header only)
1107 - [algorithm](https://www.boost.org/doc/libs/1_66_0/libs/algorithm/doc/html/index.html) (header only)
1108 - [lexical_cast](https://www.boost.org/doc/libs/1_66_0/doc/html/boost_lexical_cast.html) (header only)
1109 - [regex](https://www.boost.org/doc/libs/1_66_0/libs/regex/doc/html/index.html)
1110 - [uuid](https://www.boost.org/doc/libs/1_66_0/libs/uuid/doc/uuid.html) (header only)
1111 - [range](https://www.boost.org/doc/libs/1_66_0/libs/range/doc/html/index.html) (header only)
1112 - [variant](https://www.boost.org/doc/libs/1_66_0/doc/html/variant.html) (header only)
1113 - [multi_index](https://www.boost.org/doc/libs/1_66_0/libs/multi_index/doc/index.html) (header only)
1114 - [function_types](https://www.boost.org/doc/libs/1_66_0/libs/function_types/doc/html/index.html) (header only)
1115 - [circular_buffer](https://www.boost.org/doc/libs/1_66_0/doc/html/circular_buffer.html) (header only)
1116 - [math](https://www.boost.org/doc/libs/1_66_0/libs/math/doc/html/index.html) (header only)
1120 - [system](https://www.boost.org/doc/libs/1_66_0/libs/system/doc/index.html)
1121 - [thread](https://www.boost.org/doc/libs/1_66_0/doc/html/thread.html)
1122 - [signals2](https://www.boost.org/doc/libs/1_66_0/doc/html/signals2.html) (header only)
1123 - [program_options](https://www.boost.org/doc/libs/1_66_0/doc/html/program_options.html)
1124 - [date_time](https://www.boost.org/doc/libs/1_66_0/doc/html/date_time.html)
1125 - [filesystem](https://www.boost.org/doc/libs/1_66_0/libs/filesystem/doc/index.htm)
1129 - [asio](https://www.boost.org/doc/libs/1_66_0/doc/html/boost_asio.html) (header only)
1130 - [beast](https://www.boost.org/doc/libs/1_66_0/libs/beast/doc/html/index.html) (header only)
1131 - [coroutine](https://www.boost.org/doc/libs/1_66_0/libs/coroutine/doc/html/index.html)
1132 - [context](https://www.boost.org/doc/libs/1_66_0/libs/context/doc/html/index.html)
1134 Consider abstracting their usage into `*utility.{c,h}pp` files with
1135 wrapping existing Icinga types. That also allows later changes without
1136 rewriting large code parts.
1140 > A new Boost library should be explained in a PR and discussed with the team.
1142 > This requires package dependency changes.
1144 If you consider an external library or code to be included with Icinga, the following
1145 requirements must be fulfilled:
1147 - License is compatible with GPLv2+. Boost license, MIT works, Apache is not.
1148 - C++11 is supported, C++14 or later doesn't work
1149 - Header only implementations are preferred, external libraries require packages on every distribution.
1150 - No additional frameworks, Boost is the only allowed.
1151 - The code is proven to be robust and the GitHub repository is alive, or has 1k+ stars. Good libraries also provide a user list, if e.g. Ceph is using it, this is a good candidate.
1154 #### Log <a id="development-develop-styleguide-log"></a>
1156 Icinga allows the user to configure logging backends, e.g. syslog or file.
1158 Any log message inside the code must use the `Log()` function.
1160 - The first parameter is the severity level, use them with care.
1161 - The second parameter defines the location/scope where the log
1162 happened. Typically we use the class name here, to better analyse
1163 the logs the user provide in GitHub issues and on the community
1165 - The third parameter takes a log message string
1167 If the message string needs to be computed from existing values,
1168 everything must be converted to the String type beforehand.
1169 This conversion for every value is very expensive which is why
1172 Instead, use Log() with the shift operator where everything is written
1173 on the stream and conversions are explicitly done with templates
1176 The trick here is that the Log object is destroyed immediately
1177 after being constructed once. The destructor actually
1178 evaluates the values and sends it to registers loggers.
1180 Since flushing the stream every time a log entry occurs is
1181 very expensive, a timer takes care of flushing the stream
1186 > If logging stopped, the flush timer thread may be dead.
1187 > Inspect that with gdb/lldb.
1189 Avoid log messages which could irritate the user. During
1190 implementation, developers can change log levels to better
1191 see what's going one, but remember to change this back to `debug`
1192 or remove it entirely.
1195 #### Goto <a id="development-develop-styleguide-goto"></a>
1197 Avoid using `goto` statements. There are rare occasions where
1200 - The code would become overly complicated within nested loops and conditions.
1201 - Event processing and C interfaces.
1202 - Question/Answer loops within interactive CLI commands.
1204 #### Typedef and Auto Keywords <a id="development-develop-styleguide-typedef-auto"></a>
1206 Typedefs allow developers to use shorter names for specific types,
1207 classes and structs.
1210 typedef std::map<String, std::shared_ptr<NamespaceValue> >::iterator Iterator;
1213 These typedefs should be part of the Class definition in the header,
1214 or may be defined in the source scope where they are needed.
1216 Avoid declaring global typedefs, unless necessary.
1218 Using the `auto` keyword allows to ignore a specific value type.
1219 This comes in handy with maps/sets where no specific access
1222 The following example iterates over a map returned from `GetTypes()`.
1225 for (const auto& kv : GetTypes()) {
1226 result.insert(kv.second);
1230 The long example would require us to define a map iterator, and a slightly
1231 different algorithm.
1234 typedef std::map<String, DbType::Ptr> TypeMap;
1235 typedef std::map<String, DbType::Ptr>::const_iterator TypeMapIterator;
1237 TypeMap types = GetTypes();
1239 for (TypeMapIterator it = types.begin(); it != types.end(); it++) {
1240 result.insert(it.second);
1244 We could also use a pair here, but requiring to know
1245 the specific types of the map keys and values.
1248 typedef std::pair<String, DbType::Ptr> kv_pair;
1250 for (const kv_pair& kv : GetTypes()) {
1251 result.insert(kv.second);
1255 After all, `auto` shortens the code and one does not always need to know
1256 about the specific types. Function documentation for `GetTypes()` is
1261 #### Whitespace Cleanup <a id="development-develop-choose-editor-whitespaces"></a>
1263 Patches must be cleaned up and follow the indent style (tabs instead of spaces).
1264 You should also remove any trailing whitespaces.
1266 `git diff` allows to highlight such.
1269 vim $HOME/.gitconfig
1272 whitespace = red reverse
1274 whitespace=fix,-indent-with-non-tab,trailing-space,cr-at-eol
1277 `vim` also can match these and visually alert you to remove them.
1282 highlight ExtraWhitespace ctermbg=red guibg=red
1283 match ExtraWhitespace /\s\+$/
1284 autocmd BufWinEnter * match ExtraWhitespace /\s\+$/
1285 autocmd InsertEnter * match ExtraWhitespace /\s\+\%#\@<!$/
1286 autocmd InsertLeave * match ExtraWhitespace /\s\+$/
1287 autocmd BufWinLeave * call clearmatches()
1291 ## Development Environment <a id="development-environment"></a>
1293 ### Linux Dev Environment <a id="development-linux-dev-env"></a>
1295 Based on CentOS 7, we have an early draft available inside the Icinga Vagrant boxes:
1296 [centos7-dev](https://github.com/Icinga/icinga-vagrant/tree/master/centos7-dev).
1298 If you're compiling Icinga 2 natively without any virtualization layer in between,
1299 this usually is faster. This is also the reason why developers on macOS prefer native builds
1300 over Linux or Windows VMs. Don't forget to test the actual code on Linux later! Socket specific
1301 stuff like `epoll` is not available on Unix kernels.
1303 Depending on your workstation and environment, you may either develop and run locally,
1304 use a container deployment pipeline or put everything in a high end resource remote VM.
1306 Fork https://github.com/Icinga/icinga2 into your own repository, e.g. `https://github.com/dnsmichi/icinga2`.
1308 Create two build directories for different binary builds.
1310 * `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.
1311 * `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.
1314 mkdir -p release debug
1317 Proceed with the specific distribution examples below. Keep in mind that these instructions
1318 are best effort and sometimes out-of-date. Git Master may contain updates.
1320 * [CentOS 7](21-development.md#development-linux-dev-env-centos)
1321 * [Debian 10 Buster](21-development.md#development-linux-dev-env-debian)
1322 * [Ubuntu 18 Bionic](21-development.md#development-linux-dev-env-ubuntu)
1325 #### CentOS 7 <a id="development-linux-dev-env-centos"></a>
1328 yum -y install gdb vim git bash-completion htop
1330 yum -y install rpmdevtools ccache \
1331 cmake make gcc-c++ flex bison \
1332 openssl-devel boost169-devel systemd-devel \
1333 mysql-devel postgresql-devel libedit-devel \
1338 useradd -c "icinga" -s /sbin/nologin -G icingacmd -g icinga icinga
1340 ln -s /bin/ccache /usr/local/bin/gcc
1341 ln -s /bin/ccache /usr/local/bin/g++
1343 git clone https://github.com/icinga/icinga2.git && cd icinga2
1346 The debug build binaries contain specific code which runs
1347 slower but allows for better debugging insights.
1349 For benchmarks, change `CMAKE_BUILD_TYPE` to `RelWithDebInfo` and
1350 build inside the `release` directory.
1352 First, off export some generics for Boost.
1355 export I2_BOOST="-DBoost_NO_BOOST_CMAKE=TRUE -DBoost_NO_SYSTEM_PATHS=TRUE -DBOOST_LIBRARYDIR=/usr/lib64/boost169 -DBOOST_INCLUDEDIR=/usr/include/boost169 -DBoost_ADDITIONAL_VERSIONS='1.69;1.69.0'"
1358 Second, add the prefix path to it.
1361 export I2_GENERIC="$I2_BOOST -DCMAKE_INSTALL_PREFIX=/usr/local/icinga2"
1364 Third, define the two build types with their specific CMake variables.
1367 export I2_DEBUG="-DCMAKE_BUILD_TYPE=Debug -DICINGA2_UNITY_BUILD=OFF $I2_GENERIC"
1368 export I2_RELEASE="-DCMAKE_BUILD_TYPE=RelWithDebInfo -DICINGA2_WITH_TESTS=ON -DICINGA2_UNITY_BUILD=ON $I2_GENERIC"
1371 Fourth, depending on your likings, you may add a bash alias for building,
1372 or invoke the commands inside:
1375 alias i2_debug="cd /root/icinga2; mkdir -p debug; cd debug; cmake $I2_DEBUG ..; make -j2; sudo make -j2 install; cd .."
1376 alias i2_release="cd /root/icinga2; mkdir -p release; cd release; cmake $I2_RELEASE ..; make -j2; sudo make -j2 install; cd .."
1379 This is taken from the [centos7-dev](https://github.com/Icinga/icinga-vagrant/tree/master/centos7-dev) Vagrant box.
1382 The source installation doesn't set proper permissions, this is
1383 handled in the package builds which are officially supported.
1386 chown -R icinga:icinga /usr/local/icinga2/var/
1388 /usr/local/icinga2/lib/icinga2/prepare-dirs /usr/local/icinga2/etc/sysconfig/icinga2
1389 /usr/local/icinga2/sbin/icinga2 api setup
1390 vim /usr/local/icinga2/etc/icinga2/conf.d/api-users.conf
1392 gdb --args /usr/local/icinga2/lib/icinga2/sbin/icinga2 daemon
1395 #### Debian 10 <a id="development-linux-dev-env-debian"></a>
1397 Debian Buster doesn't need updated Boost packages from packages.icinga.com,
1398 the distribution already provides 1.66+. For older versions such as Stretch,
1399 include the release repository for packages.icinga.com as shown in the [setup instructions](02-installation.md#package-repositories-debian-ubuntu-raspbian).
1402 $ docker run -ti ubuntu:bionic bash
1405 apt-get -y install apt-transport-https wget gnupg
1407 apt-get -y install gdb vim git cmake make ccache build-essential libssl-dev bison flex default-libmysqlclient-dev libpq-dev libedit-dev monitoring-plugins
1408 apt-get -y install libboost-all-dev
1412 ln -s /usr/bin/ccache /usr/local/bin/gcc
1413 ln -s /usr/bin/ccache /usr/local/bin/g++
1417 useradd -c "icinga" -s /sbin/nologin -G icingacmd -g icinga icinga
1419 git clone https://github.com/icinga/icinga2.git && cd icinga2
1423 export I2_DEB="-DBoost_NO_BOOST_CMAKE=TRUE -DBoost_NO_SYSTEM_PATHS=TRUE -DBOOST_LIBRARYDIR=/usr/lib/x86_64-linux-gnu -DBOOST_INCLUDEDIR=/usr/include -DCMAKE_INSTALL_RPATH=/usr/lib/x86_64-linux-gnu"
1424 export I2_GENERIC="-DCMAKE_INSTALL_PREFIX=/usr/local/icinga2 -DICINGA2_PLUGINDIR=/usr/local/sbin"
1425 export I2_DEBUG="$I2_DEB $I2_GENERIC -DCMAKE_BUILD_TYPE=Debug -DICINGA2_UNITY_BUILD=OFF"
1431 make -j2 install -C debug
1435 The source installation doesn't set proper permissions, this is
1436 handled in the package builds which are officially supported.
1439 chown -R icinga:icinga /usr/local/icinga2/var/
1441 /usr/local/icinga2/lib/icinga2/prepare-dirs /usr/local/icinga2/etc/sysconfig/icinga2
1442 /usr/local/icinga2/sbin/icinga2 api setup
1443 vim /usr/local/icinga2/etc/icinga2/conf.d/api-users.conf
1445 gdb --args /usr/local/icinga2/lib/icinga2/sbin/icinga2 daemon
1449 #### Ubuntu 18 Bionic <a id="development-linux-dev-env-ubuntu"></a>
1451 Requires Boost packages from packages.icinga.com.
1454 $ docker run -ti ubuntu:bionic bash
1457 apt-get -y install apt-transport-https wget gnupg
1459 wget -O - https://packages.icinga.com/icinga.key | apt-key add -
1461 . /etc/os-release; if [ ! -z ${UBUNTU_CODENAME+x} ]; then DIST="${UBUNTU_CODENAME}"; else DIST="$(lsb_release -c| awk '{print $2}')"; fi; \
1462 echo "deb https://packages.icinga.com/ubuntu icinga-${DIST} main" > \
1463 /etc/apt/sources.list.d/${DIST}-icinga.list
1464 echo "deb-src https://packages.icinga.com/ubuntu icinga-${DIST} main" >> \
1465 /etc/apt/sources.list.d/${DIST}-icinga.list
1471 apt-get -y install gdb vim git cmake make ccache build-essential libssl-dev bison flex default-libmysqlclient-dev libpq-dev libedit-dev monitoring-plugins
1473 apt-get install -y libboost1.67-icinga-all-dev
1475 ln -s /usr/bin/ccache /usr/local/bin/gcc
1476 ln -s /usr/bin/ccache /usr/local/bin/g++
1480 useradd -c "icinga" -s /sbin/nologin -G icingacmd -g icinga icinga
1482 git clone https://github.com/icinga/icinga2.git && cd icinga2
1486 export I2_DEB="-DBoost_NO_BOOST_CMAKE=TRUE -DBoost_NO_SYSTEM_PATHS=TRUE -DBOOST_LIBRARYDIR=/usr/lib/x86_64-linux-gnu/icinga-boost -DBOOST_INCLUDEDIR=/usr/include/icinga-boost -DCMAKE_INSTALL_RPATH=/usr/lib/x86_64-linux-gnu/icinga-boost"
1487 export I2_GENERIC="-DCMAKE_INSTALL_PREFIX=/usr/local/icinga2 -DICINGA2_PLUGINDIR=/usr/local/sbin"
1488 export I2_DEBUG="$I2_DEB $I2_GENERIC -DCMAKE_BUILD_TYPE=Debug -DICINGA2_UNITY_BUILD=OFF"
1496 make -j2 install -C debug
1499 The source installation doesn't set proper permissions, this is
1500 handled in the package builds which are officially supported.
1503 chown -R icinga:icinga /usr/local/icinga2/var/
1505 /usr/local/icinga2/lib/icinga2/prepare-dirs /usr/local/icinga2/etc/sysconfig/icinga2
1506 /usr/local/icinga2/sbin/icinga2 api setup
1507 vim /usr/local/icinga2/etc/icinga2/conf.d/api-users.conf
1509 gdb --args /usr/local/icinga2/lib/icinga2/sbin/icinga2 daemon
1512 ### macOS Dev Environment <a id="development-macos-dev-env"></a>
1514 It is advised to use Homebrew to install required build dependencies.
1515 Macports have been reported to work as well, typically you'll get more help
1516 with Homebrew from Icinga developers.
1518 The idea is to run Icinga with the current user, avoiding root permissions.
1519 This requires at least v2.11.
1523 > This is a pure development setup for Icinga developers reducing the compile
1524 > time in contrast to VMs. There are no packages, startup scripts or dependency management involved.
1526 > **macOS agents are not officially supported.**
1528 > macOS uses its own TLS implementation, Icinga relies on extra OpenSSL packages
1529 > requiring updates apart from vendor security updates.
1533 Explicitly use OpenSSL 1.1.x, older versions are out of support.
1536 brew install ccache boost cmake bison flex openssl@1.1 mysql-connector-c++ postgresql libpq
1542 sudo mkdir /opt/ccache
1544 sudo ln -s `which ccache` /opt/ccache/clang
1545 sudo ln -s `which ccache` /opt/ccache/clang++
1547 vim $HOME/.bash_profile
1549 # ccache is managed with symlinks to avoid collision with cgo
1550 export PATH="/opt/ccache:$PATH"
1552 source $HOME/.bash_profile
1557 Icinga is built as release (optimized build for packages) and debug (more symbols and details for debugging). Debug builds
1558 typically run slower than release builds and must not be used for performance benchmarks.
1560 The preferred installation prefix is `/usr/local/icinga/icinga2`. This allows to put e.g. Icinga Web 2 into the `/usr/local/icinga` directory as well.
1563 mkdir -p release debug
1565 export I2_USER=$(id -u -n)
1566 export I2_GROUP=$(id -g -n)
1567 export I2_GENERIC="-DCMAKE_INSTALL_PREFIX=/usr/local/icinga/icinga2 -DICINGA2_USER=$I2_USER -DICINGA2_GROUP=$I2_GROUP -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 -DICINGA2_WITH_PGSQL=OFF -DCMAKE_EXPORT_COMPILE_COMMANDS=ON"
1568 export I2_DEBUG="-DCMAKE_BUILD_TYPE=Debug -DICINGA2_UNITY_BUILD=OFF $I2_GENERIC"
1569 export I2_RELEASE="-DCMAKE_BUILD_TYPE=RelWithDebInfo -DICINGA2_WITH_TESTS=ON -DICINGA2_UNITY_BUILD=ON $I2_GENERIC"
1576 make -j4 install -C debug
1579 In order to run Icinga without any path prefix, and also use Bash completion it is advised to source additional
1580 things into the local dev environment.
1583 export PATH=/usr/local/icinga/icinga2/sbin/:$PATH
1585 test -f /usr/local/icinga/icinga2/etc/bash_completion.d/icinga2 && source /usr/local/icinga/icinga2/etc/bash_completion.d/icinga2
1590 This is derived from [dnsmichi's flavour](https://github.com/dnsmichi/dotfiles) and not generally best practice.
1593 vim $HOME/.bash_profile
1595 export I2_USER=$(id -u -n)
1596 export I2_GROUP=$(id -g -n)
1597 export I2_GENERIC="-DCMAKE_INSTALL_PREFIX=/usr/local/icinga/icinga2 -DICINGA2_USER=$I2_USER -DICINGA2_GROUP=$I2_GROUP -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 -DICINGA2_WITH_PGSQL=OFF -DCMAKE_EXPORT_COMPILE_COMMANDS=ON"
1599 export I2_DEBUG="-DCMAKE_BUILD_TYPE=Debug -DICINGA2_UNITY_BUILD=OFF $I2_GENERIC"
1600 export I2_RELEASE="-DCMAKE_BUILD_TYPE=RelWithDebInfo -DICINGA2_WITH_TESTS=ON -DICINGA2_UNITY_BUILD=ON $I2_GENERIC"
1602 alias i2_debug="mkdir -p debug; cd debug; cmake $I2_DEBUG ..; make -j4; make -j4 install; cd .."
1603 alias i2_release="mkdir -p release; cd release; cmake $I2_RELEASE ..; make -j4; make -j4 install; cd .."
1605 export PATH=/usr/local/icinga/icinga2/sbin/:$PATH
1606 test -f /usr/local/icinga/icinga2/etc/bash_completion.d/icinga2 && source /usr/local/icinga/icinga2/etc/bash_completion.d/icinga2
1609 source $HOME/.bash_profile
1614 `make install` doesn't set all required permissions, override this.
1617 chown -R $I2_USER:$I2_GROUP /usr/local/icinga/icinga2
1622 Start Icinga in foreground.
1628 Reloads triggered with HUP or cluster syncs just put the process into background.
1633 brew install monitoring-plugins
1635 sudo vim /usr/local/icinga/icinga2/etc/icinga2/constants.conf
1636 const PluginDir = "/usr/local/sbin"
1639 #### Backends: Redis
1643 brew services start redis
1646 #### Databases: MariaDB
1649 brew install mariadb
1650 mkdir -p /usr/local/etc/my.cnf.d
1651 brew services start mariadb
1653 mysql_secure_installation
1661 password = supersecurerootpassword
1664 ln -s /Users/michi/.my.cnf $HOME/.my.cnf
1669 mysql -e 'create database icinga;'
1670 mysql -e "grant all on icinga.* to 'icinga'@'localhost' identified by 'icinga';"
1671 mysql icinga < $HOME/dev/icinga/icinga2/lib/db_ido_mysql/schema/mysql.sql
1678 cd /usr/local/icinga/icinga2/var/lib/icinga2/certs
1679 HOST_NAME=mbpmif.int.netways.de
1680 icinga2 pki new-cert --cn ${HOST_NAME} --csr ${HOST_NAME}.csr --key ${HOST_NAME}.key
1681 icinga2 pki sign-csr --csr ${HOST_NAME}.csr --cert ${HOST_NAME}.crt
1682 echo "const NodeName = \"${HOST_NAME}\"" >> /usr/local/icinga/icinga2/etc/icinga2/constants.conf
1687 While it is recommended to use Docker or the Icinga Web 2 development VM pointing to the shared IDO database resource/REST API, you can also install it locally on macOS.
1689 The required steps are described in [this script](https://github.com/dnsmichi/dotfiles/blob/master/icingaweb2.sh).
1693 ### Windows Dev Environment <a id="development-windows-dev-env"></a>
1695 The following sections explain how to setup the required build tools
1696 and how to run and debug the code.
1700 Open an administrative command prompt (Win key, type “cmd”, right-click and “run as administrator”) and paste the following instructions:
1703 @powershell -NoProfile -ExecutionPolicy Bypass -Command "iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))" && SET PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin
1706 #### Git, Posh and Vim
1708 In case you are used to `vim`, start a new administrative Powershell:
1711 choco install -y vim
1714 The same applies for Git integration in Powershell:
1717 choco install -y poshgit
1720 ![Powershell Posh Git](images/development/windows_powershell_posh_git.png)
1722 In order to fix the colors for commands like `git status` or `git diff`,
1723 edit `$HOME/.gitconfig` in your Powershell and add the following lines:
1726 vim $HOME/.gitconfig
1730 untracked = yellow bold
1742 current = yellow reverse
1750 Thanks to Microsoft they’ll now provide their Professional Edition of Visual Studio
1751 as community version, free for use for open source projects such as Icinga.
1752 The installation requires ~9GB disk space. [Download](https://www.visualstudio.com/downloads/)
1753 the web installer and start the installation.
1755 Note: Both Visual Studio 2017 and 2019 are covered here. Older versions
1758 You need a free Microsoft account to download and also store your preferences.
1760 Install the following Workloads:
1762 * C++ Desktop Development (icinga2.exe)
1763 * .NET Desktop Development (Agent Setup Wizard in C#)
1765 In addition also choose these individual components on Visual Studio:
1768 * .NET Framework 4.6 targeting pack
1769 * .NET Framework 4.6.1 SDK
1770 * .NET Framework 4.6.1 targeting pack
1773 * Static analysis tools
1774 * GitHub Extension for Visual Studio
1775 * Compilers, build tools and runtimes
1776 * C# and Visual Basic Roslyn compilers
1778 * VC++ 2017 v141 toolset (x86_64)
1779 * Debugging and testing
1780 * C++ profiling tools
1781 * Just-in-Time debugger
1782 * Development activities
1783 * Visual Studio C++ core features
1784 * Games and Graphics
1785 * Graphics debugger and GPU profiler for DirectX (required by C++ profiling tools)
1786 * SDKs, libraries and frameworks
1787 * Graphics Tools Windows 8.1 SDK (required by C++ profiling tools)
1790 * Windows Universal C Runtime
1792 After a while, Visual Studio will be ready.
1794 ##### Style Guide for Visual Studio
1796 Navigate into `Tools > Options > Text Editor` and repeat the following for
1801 Navigate into `Tabs` and set:
1803 - Indenting: Smart (default)
1806 - Keep tabs (instead of spaces)
1808 ![Visual Studio Tabs](images/development/windows_visual_studio_tabs_c++.png)
1813 Install it using [chocolatey](https://www.wireshark.org/docs/wsdg_html_chunked/ChSetupWin32.html):
1816 choco install -y winflexbison
1819 Chocolatey installs these tools into the hidden directory `C:\ProgramData\chocolatey\lib\winflexbison\tools`.
1823 Icinga 2 requires the OpenSSL library. [Download](http://slproweb.com/products/Win32OpenSSL.html) the Win64 package
1824 and install it into `c:\local\OpenSSL-Win64`.
1826 Once asked for `Copy OpenSSLs DLLs to` select `The Windows system directory`. That way CMake/Visual Studio
1827 will automatically detect them for builds and packaging.
1831 > We cannot use the chocolatey package as this one does not provide any development headers.
1833 > Choose 1.1.1 LTS from manual downloads for best compatibility.
1837 Icinga needs the development header and library files from the Boost library.
1839 Visual Studio translates into the following compiler versions:
1841 - `msvc-14.1` = Visual Studio 2017
1842 - `msvc-14.2` = Visual Studio 2019
1844 ##### Pre-built Binaries
1846 Prefer the pre-built package over self-compiling, if the newest version already exists.
1848 Download the [boost-binaries](https://sourceforge.net/projects/boost/files/boost-binaries/) for
1850 - msvc-14.2 is Visual Studio 2019
1851 - 64 for 64 bit builds
1854 https://sourceforge.net/projects/boost/files/boost-binaries/1.71.0/boost_1_71_0-msvc-14.2-64.exe/download
1857 Run the installer and leave the default installation path in `C:\local\boost_1_71_0`.
1860 ##### Source & Compile
1862 In order to use the boost development header and library files you need to [download](http://www.boost.org/users/download/)
1863 Boost and then extract it to e.g. `C:\local\boost_1_71_0`.
1867 > Just use `C:\local`, the zip file already contains the sub folder. Extraction takes a while,
1868 > the archive contains more than 70k files.
1870 In order to integrate Boost into Visual Studio 2017, open the `Developer Command Prompt` from the start menu,
1871 and navigate to `C:\local\boost_1_71_0`.
1873 Execute `bootstrap.bat` first.
1876 cd C:\local\boost_1_71_0
1880 Once finished, specify the required `toolset` to compile boost against Visual Studio.
1881 This takes quite some time in a Windows VM. Boost Context uses Assembler code,
1882 which isn't treated as exception safe by the VS compiler. Therefore set the
1883 additional compilation flag according to [this entry](https://lists.boost.org/Archives/boost/2015/08/224570.php).
1886 b2 --toolset=msvc-14.2 link=static threading=multi runtime-link=static address-model=64 asmflags=\safeseh
1889 ![Windows Boost Build in VS2017 Development Console](images/development/windows_boost_build_dev_cmd.png)
1893 TortoiseGit provides a graphical integration into the Windows explorer. This makes it easier to checkout, commit
1896 [Download](https://tortoisegit.org/download/) TortoiseGit on your system.
1898 In order to clone via Git SSH you also need to create a new directory called `.ssh`
1899 inside your user's home directory.
1900 Therefore open a command prompt (win key, type `cmd`, enter) and run `mkdir .ssh`.
1901 Add your `id_rsa` private key and `id_rsa.pub` public key files into that directory.
1903 Start the setup routine and choose `OpenSSH` as default secure transport when asked.
1905 Open a Windows Explorer window and navigate into
1908 cd %HOMEPATH%\source\repos
1911 Right click and select `Git Clone` from the context menu.
1913 Use `ssh://git@github.com/icinga/icinga2.git` for SSH clones, `https://github.com/icinga/icinga2.git` otherwise.
1917 CMake uses CPack and NSIS to create the setup executable including all binaries and libraries
1918 in addition to setup dialogues and configuration. Therefore we’ll need to install [NSIS](http://nsis.sourceforge.net/Download)
1921 We also need to install the Windows Installer XML (WIX) toolset.
1924 choco install -y wixtoolset
1929 Icinga 2 uses CMake to manage the build environment. You can generate the Visual Studio project files
1930 using CMake. [Download](https://cmake.org/download/) and install CMake. Select to add it to PATH for all users
1935 > In order to properly detect the Boost libraries and VS 2019, install CMake 3.15.2+.
1939 > Cheatsheet: http://www.brianlheim.com/2018/04/09/cmake-cheat-sheet.html
1941 Once setup is completed, open a command prompt and navigate to
1944 cd %HOMEPATH%\source\repos
1947 Build Icinga with specific CMake variables. This generates a new Visual Studio project file called `icinga2.sln`.
1949 Visual Studio translates into the following:
1951 - `msvc-14.1` = Visual Studio 2017
1952 - `msvc-14.2` = Visual Studio 2019
1954 You need to specify the previously installed component paths.
1956 Variable | Value | Description
1957 ----------------------|----------------------------------------------------------------------|-------------------------------------------------------
1958 `BOOST_ROOT` | `C:\local\boost_1_71_0` | Root path where you've extracted and compiled Boost.
1959 `BOOST_LIBRARYDIR` | Binary: `C:\local\boost_1_71_0\lib64-msvc-14.1`, Source: `C:\local\boost_1_71_0\stage` | Path to the static compiled Boost libraries, directory must contain `lib`.
1960 `BISON_EXECUTABLE` | `C:\ProgramData\chocolatey\lib\winflexbison\tools\win_bison.exe` | Path to the Bison executable.
1961 `FLEX_EXECUTABLE` | `C:\ProgramData\chocolatey\lib\winflexbison\tools\win_flex.exe` | Path to the Flex executable.
1962 `ICINGA2_WITH_MYSQL` | OFF | Requires extra setup for MySQL if set to `ON`. Not supported for client setups.
1963 `ICINGA2_WITH_PGSQL` | OFF | Requires extra setup for PgSQL if set to `ON`. Not supported for client setups.
1964 `ICINGA2_UNITY_BUILD` | OFF | Disable unity builds for development environments.
1966 Tip: If you have previously opened a terminal, run `refreshenv` to re-read updated PATH variables.
1970 Icinga provides the build scripts inside the Git repository.
1972 Open a new Powershell and navigate into the cloned Git repository. Set
1973 specific environment variables and run the build scripts.
1976 cd %HOMEPATH%\source\repos\icinga2
1978 .\tools\win32\configure-dev.ps1
1979 .\tools\win32\build.ps1
1980 .\tools\win32\test.ps1
1983 If you did not follow the above steps with Boost binaries
1984 and OpenSSL paths, or using VS 2017, you can still modify
1985 the environment variables.
1988 $env:CMAKE_GENERATOR='Visual Studio 16 2019'
1989 $env:CMAKE_GENERATOR_PLATFORM='x64'
1991 $env:ICINGA2_INSTALLPATH = 'C:\Program Files\Icinga2-debug'
1992 $env:ICINGA2_BUILDPATH='debug'
1993 $env:CMAKE_BUILD_TYPE='Debug'
1994 $env:OPENSSL_ROOT_DIR='C:\OpenSSL-Win64'
1995 $env:BOOST_ROOT='C:\local\boost_1_71_0'
1996 $env:BOOST_LIBRARYDIR='C:\local\boost_1_71_0\lib64-msvc-14.2'
1999 #### Icinga 2 in Visual Studio
2004 cd %HOMEPATH%\source\repos\icinga2
2007 Open `icinga2.sln`. Log into Visual Studio when asked.
2009 On the right panel, select to build the `Bin/icinga-app` solution.
2011 The executable binaries are located in `Bin\Release\Debug` in your `icinga2`
2014 Navigate there and run `icinga2.exe --version`.
2017 cd %HOMEPATH%\source\repos\icinga2\Bin\Release\Debug
2018 icinga2.exe --version
2022 #### Release Package
2024 This is part of the build process script already.
2025 You need to override the build type and pick a different
2029 cd %HOMEPATH%\source\repos\icinga2
2031 $env:ICINGA2_BUILDPATH='release'
2032 $env:CMAKE_BUILD_TYPE='RelWithDebInfo'
2034 .\tools\win32\configure-dev.ps1
2035 .\tools\win32\build.ps1
2036 .\tools\win32\test.ps1
2041 ### Embedded Dev Env: Pi <a id="development-embedded-dev-env"></a>
2045 > This isn't officially supported yet, just a few hints how you can do it yourself.
2047 The following examples source from armhf on Raspberry Pi.
2052 apt install -y ccache
2054 /usr/sbin/update-ccache-symlinks
2056 echo 'export PATH="/usr/lib/ccache:$PATH"' | tee -a ~/.bashrc
2058 source ~/.bashrc && echo $PATH
2063 Copy the icinga2 source code into `$HOME/icinga2`. Clone the `deb-icinga2` repository into `debian/`.
2066 git clone https://github.com/Icinga/icinga2 $HOME/icinga2
2067 git clone https://github.com/Icinga/deb-icinga2 $HOME/icinga2/debian
2070 Then build a Debian package and install it like normal.
2072 dpkg-buildpackage -uc -us
2075 ## Package Builds <a id="development-package-builds"></a>
2077 This documentation is explicitly meant for packagers and the Icinga
2078 build infrastructure.
2080 The following requirements need to be fulfilled in order to build the
2081 Icinga application using a dist tarball (including notes for distributions):
2084 * GNU make (make) or ninja-build
2085 * C++ compiler which supports C++11
2086 * RHEL/Fedora/SUSE: gcc-c++ >= 4.7 (extra Developer Tools on RHEL5/6 see below)
2087 * Debian/Ubuntu: build-essential
2088 * Alpine: build-base
2089 * you can also use clang++
2091 * OpenSSL library and header files >= 1.0.1
2092 * RHEL/Fedora: openssl-devel
2093 * SUSE: libopenssl-devel
2094 * Debian/Ubuntu: libssl-dev
2095 * Alpine: libressl-dev
2096 * Boost library and header files >= 1.66.0
2097 * RHEL/Fedora: boost166-devel
2098 * Debian/Ubuntu: libboost-all-dev
2101 * GNU flex (flex) >= 2.5.35
2103 * Only required when using systemd
2104 * Debian/Ubuntu: libsystemd-dev
2105 * RHEL/Fedora: systemd-devel
2107 ### Optional features <a id="development-package-builds-optional-features"></a>
2109 * MySQL (disable with CMake variable `ICINGA2_WITH_MYSQL` to `OFF`)
2110 * RHEL/Fedora: mysql-devel
2111 * SUSE: libmysqlclient-devel
2112 * Debian/Ubuntu: default-libmysqlclient-dev | libmysqlclient-dev
2113 * Alpine: mariadb-dev
2114 * PostgreSQL (disable with CMake variable `ICINGA2_WITH_PGSQL` to `OFF`)
2115 * RHEL/Fedora: postgresql-devel
2116 * Debian/Ubuntu: libpq-dev
2117 * postgresql-dev on Alpine
2118 * libedit (CLI console)
2119 * RHEL/Fedora: libedit-devel on CentOS (RHEL requires rhel-7-server-optional-rpms)
2120 * Debian/Ubuntu/Alpine: libedit-dev
2121 * Termcap (only required if libedit doesn't already link against termcap/ncurses)
2122 * RHEL/Fedora: libtermcap-devel
2123 * Debian/Ubuntu: (not necessary)
2125 ### Special requirements <a id="development-package-builds-special-requirements"></a>
2127 **FreeBSD**: libexecinfo (automatically used when Icinga 2 is installed via port or package)
2129 **RHEL6**: Requires a newer boost version which is available on packages.icinga.com
2130 with a version suffixed name.
2132 ### Runtime user environment <a id="development-package-builds-runtime-user-env"></a>
2134 By default Icinga will run as user `icinga` and group `icinga`. Additionally the
2135 external command pipe and livestatus features require a dedicated command group
2136 `icingacmd`. You can choose your own user/group names and pass them to CMake
2137 using the `ICINGA2_USER`, `ICINGA2_GROUP` and `ICINGA2_COMMAND_GROUP` variables.
2141 # groupadd icingacmd
2142 # useradd -c "icinga" -s /sbin/nologin -G icingacmd -g icinga icinga
2145 On Alpine (which uses ash busybox) you can run:
2148 # addgroup -S icinga
2149 # addgroup -S icingacmd
2150 # adduser -S -D -H -h /var/spool/icinga2 -s /sbin/nologin -G icinga -g icinga icinga
2151 # adduser icinga icingacmd
2154 Add the web server user to the icingacmd group in order to grant it write
2155 permissions to the external command pipe and livestatus socket:
2158 # usermod -a -G icingacmd www-data
2161 Make sure to replace "www-data" with the name of the user your web server
2164 ### Building Icinga 2: Example <a id="development-package-builds-example"></a>
2166 Once you have installed all the necessary build requirements you can build
2167 Icinga 2 using the following commands:
2170 $ mkdir release && cd release
2174 $ make install -C release
2177 You can specify an alternative installation prefix using `-DCMAKE_INSTALL_PREFIX`:
2180 $ cmake .. -DCMAKE_INSTALL_PREFIX=/tmp/icinga2
2183 ### CMake Variables <a id="development-package-builds-cmake-variables"></a>
2185 In addition to `CMAKE_INSTALL_PREFIX` here are most of the supported Icinga-specific cmake variables.
2187 For all variables regarding defaults paths on in CMake, see
2188 [GNUInstallDirs](https://cmake.org/cmake/help/latest/module/GNUInstallDirs.html).
2190 Also see `CMakeLists.txt` for details.
2192 #### System Environment
2194 * `CMAKE_INSTALL_SYSCONFDIR`: The configuration directory; defaults to `CMAKE_INSTALL_PREFIX/etc`
2195 * `CMAKE_INSTALL_LOCALSTATEDIR`: The state directory; defaults to `CMAKE_INSTALL_PREFIX/var`
2196 * `ICINGA2_CONFIGDIR`: Main config directory; defaults to `CMAKE_INSTALL_SYSCONFDIR/icinga2` usually `/etc/icinga2`
2197 * `ICINGA2_CACHEDIR`: Directory for cache files; defaults to `CMAKE_INSTALL_LOCALSTATEDIR/cache/icinga2` usually `/var/cache/icinga2`
2198 * `ICINGA2_DATADIR`: Data directory for the daemon; defaults to `CMAKE_INSTALL_LOCALSTATEDIR/lib/icinga2` usually `/var/lib/icinga2`
2199 * `ICINGA2_LOGDIR`: Logfiles of the daemon; defaults to `CMAKE_INSTALL_LOCALSTATEDIR/log/icinga2 usually `/var/log/icinga2`
2200 * `ICINGA2_SPOOLDIR`: Spooling directory ; defaults to `CMAKE_INSTALL_LOCALSTATEDIR/spool/icinga2` usually `/var/spool/icinga2`
2201 * `ICINGA2_INITRUNDIR`: Runtime data for the init system; defaults to `CMAKE_INSTALL_LOCALSTATEDIR/run/icinga2` usually `/run/icinga2`
2202 * `ICINGA2_GIT_VERSION_INFO`: Whether to use Git to determine the version number; defaults to `ON`
2203 * `ICINGA2_USER`: The user Icinga 2 should run as; defaults to `icinga`
2204 * `ICINGA2_GROUP`: The group Icinga 2 should run as; defaults to `icinga`
2205 * `ICINGA2_COMMAND_GROUP`: The command group Icinga 2 should use; defaults to `icingacmd`
2206 * `ICINGA2_SYSCONFIGFILE`: Where to put the config file the initscript/systemd pulls it's dirs from;
2207 * defaults to `CMAKE_INSTALL_PREFIX/etc/sysconfig/icinga2`
2208 * `ICINGA2_PLUGINDIR`: The path for the Monitoring Plugins project binaries; defaults to `/usr/lib/nagios/plugins`
2210 #### Build Optimization
2212 * `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.
2213 * `ICINGA2_LTO_BUILD`: Whether to use link time optimization (LTO); defaults to `OFF`
2217 * `USE_SYSTEMD=ON|OFF`: Use systemd or a classic SysV initscript; defaults to `OFF`
2218 * `INSTALL_SYSTEMD_SERVICE_AND_INITSCRIPT=ON|OFF` Force install both the systemd service definition file
2219 and the SysV initscript in parallel, regardless of how `USE_SYSTEMD` is set.
2220 Only use this for special packaging purposes and if you know what you are doing.
2225 * `ICINGA2_WITH_CHECKER`: Determines whether the checker module is built; defaults to `ON`
2226 * `ICINGA2_WITH_COMPAT`: Determines whether the compat module is built; defaults to `ON`
2227 * `ICINGA2_WITH_LIVESTATUS`: Determines whether the Livestatus module is built; defaults to `ON`
2228 * `ICINGA2_WITH_NOTIFICATION`: Determines whether the notification module is built; defaults to `ON`
2229 * `ICINGA2_WITH_PERFDATA`: Determines whether the perfdata module is built; defaults to `ON`
2230 * `ICINGA2_WITH_TESTS`: Determines whether the unit tests are built; defaults to `ON`
2232 #### MySQL or MariaDB
2234 The following settings can be tuned for the MySQL / MariaDB IDO feature.
2236 * `ICINGA2_WITH_MYSQL`: Determines whether the MySQL IDO module is built; defaults to `ON`
2237 * `MYSQL_CLIENT_LIBS`: Client implementation used (mysqlclient / mariadbclient); defaults searches for `mysqlclient` and `mariadbclient`
2238 * `MYSQL_INCLUDE_DIR`: Directory containing include files for the mysqlclient; default empty -
2239 checking multiple paths like `/usr/include/mysql`
2241 See [FindMySQL.cmake](https://github.com/Icinga/icinga2/blob/master/third-party/cmake/FindMySQL.cmake)
2242 for implementation details.
2246 The following settings can be tuned for the PostgreSQL IDO feature.
2248 * `ICINGA2_WITH_PGSQL`: Determines whether the PostgreSQL IDO module is built; defaults to `ON`
2249 * `PostgreSQL_INCLUDE_DIR`: Top-level directory containing the PostgreSQL include directories
2250 * `PostgreSQL_LIBRARY`: File path to PostgreSQL library : libpq.so (or libpq.so.[ver] file)
2252 See [FindPostgreSQL.cmake](https://github.com/Icinga/icinga2/blob/master/third-party/cmake/FindPostgreSQL.cmake)
2253 for implementation details.
2255 #### Version detection
2257 CMake determines the Icinga 2 version number using `git describe` if the
2258 source directory is contained in a Git repository. Otherwise the version number
2259 is extracted from the [VERSION](VERSION) file. This behavior can be
2260 overridden by creating a file called `icinga-version.h.force` in the source
2261 directory. Alternatively the `-DICINGA2_GIT_VERSION_INFO=OFF` option for CMake
2262 can be used to disable the usage of `git describe`.
2265 ### Building RPMs <a id="development-package-builds-rpms"></a>
2267 #### Build Environment on RHEL, CentOS, Fedora, Amazon Linux
2269 Setup your build environment:
2272 yum -y install rpmdevtools
2275 #### Build Environment on SuSE/SLES
2280 zypper addrepo http://download.opensuse.org/repositories/devel:tools/SLE_12_SP4/devel:tools.repo
2282 zypper install rpmdevtools spectool
2288 zypper addrepo http://download.opensuse.org/repositories/devel:tools/openSUSE_Leap_15.0/devel:tools.repo
2290 zypper install rpmdevtools spectool
2293 #### Package Builds <a id="development-package-builds-rpms-package-builds"></a>
2295 Prepare the rpmbuild directory tree:
2305 curl https://raw.githubusercontent.com/Icinga/rpm-icinga2/master/icinga2.spec -o $HOME/rpmbuild/SPECS/icinga2.spec
2310 > The above command builds snapshot packages. Change to the `release` branch
2311 > for release package builds.
2313 Copy the tarball to `rpmbuild/SOURCES` e.g. by using the `spectool` binary
2314 provided with `rpmdevtools`:
2317 cd $HOME/rpmbuild/SOURCES
2318 spectool -g ../SPECS/icinga2.spec
2323 Install the build dependencies. Example for CentOS 7:
2326 yum -y install libedit-devel ncurses-devel gcc-c++ libstdc++-devel openssl-devel \
2327 cmake flex bison boost-devel systemd mysql-devel postgresql-devel httpd \
2328 selinux-policy-devel checkpolicy selinux-policy selinux-policy-doc
2331 Note: If you are using Amazon Linux, systemd is not required.
2333 A shorter way is available using the `yum-builddep` command on RHEL based systems:
2336 yum-builddep SPECS/icinga2.spec
2342 rpmbuild -ba SPECS/icinga2.spec
2345 #### Additional Hints <a id="development-package-builds-rpms-additional-hints"></a>
2347 ##### SELinux policy module
2349 The following packages are required to build the SELinux policy module:
2352 * selinux-policy (selinux-policy on CentOS 6, selinux-policy-devel on CentOS 7)
2353 * selinux-policy-doc
2357 The RedHat Developer Toolset is required for building Icinga 2 beforehand.
2358 This contains a modern version of flex and a C++ compiler which supports
2361 cat >/etc/yum.repos.d/devtools-2.repo <<REPO
2362 [testing-devtools-2-centos-\$releasever]
2363 name=testing 2 devtools for CentOS $releasever
2364 baseurl=https://people.centos.org/tru/devtools-2/\$releasever/\$basearch/RPMS
2369 Dependencies to devtools-2 are used in the RPM SPEC, so the correct tools
2370 should be used for building.
2372 As an alternative, you can use newer Boost packages provided on
2373 [packages.icinga.com](https://packages.icinga.com/epel).
2375 cat >$HOME/.rpmmacros <<MACROS
2382 If you prefer to build packages offline, a suitable Vagrant box is located
2383 [here](https://atlas.hashicorp.com/mvbcoding/boxes/awslinux/).
2385 ### Build Debian/Ubuntu packages <a id="development-package-builds-deb"></a>
2387 Setup your build environment on Debian/Ubuntu, copy the 'debian' directory from
2388 the Debian packaging Git repository (https://github.com/Icinga/deb-icinga2)
2389 into your source tree and run the following command:
2392 dpkg-buildpackage -uc -us
2395 ### Build Alpine Linux packages <a id="development-package-builds-alpine"></a>
2397 A simple way to setup a build environment is installing Alpine in a chroot.
2398 In this way, you can set up an Alpine build environment in a chroot under a
2399 different Linux distro.
2400 There is a script that simplifies these steps with just two commands, and
2401 can be found [here](https://github.com/alpinelinux/alpine-chroot-install).
2403 Once the build environment is installed, you can setup the system to build
2404 the packages by following [this document](https://wiki.alpinelinux.org/wiki/Creating_an_Alpine_package).
2406 ### Build Post Install Tasks <a id="development-package-builds-post-install-tasks"></a>
2408 After building Icinga 2 yourself, your package build system should at least run the following post
2409 install requirements:
2411 * enable the `checker`, `notification` and `mainlog` feature by default
2412 * run 'icinga2 api setup' in order to enable the `api` feature and generate TLS certificates for the node
2414 ### Run Icinga 2 <a id="development-package-builds-run-icinga"></a>
2416 Icinga 2 comes with a binary that takes care of loading all the relevant
2417 components (e.g. for check execution, notifications, etc.):
2422 [2016-12-08 16:44:24 +0100] information/cli: Icinga application loader (version: v2.5.4-231-gb10a6b7; debug)
2423 [2016-12-08 16:44:24 +0100] information/cli: Loading configuration file(s).
2424 [2016-12-08 16:44:25 +0100] information/ConfigItem: Committing config item(s).
2428 #### Init Script <a id="development-package-builds-init-script"></a>
2430 Icinga 2 can be started as a daemon using the provided init script:
2434 Usage: /etc/init.d/icinga2 {start|stop|restart|reload|checkconfig|status}
2437 #### Systemd <a id="development-package-builds-systemd"></a>
2439 If your distribution uses systemd:
2442 systemctl {start|stop|reload|status|enable|disable} icinga2
2445 In case the distribution is running systemd >227, you'll also
2446 need to package and install the `etc/initsystem/icinga2.service.limits.conf`
2447 file into `/etc/systemd/system/icinga2.service.d`.
2449 #### openrc <a id="development-package-builds-openrc"></a>
2451 Or if your distribution uses openrc (like Alpine):
2455 Usage: /etc/init.d/icinga2 {start|stop|restart|reload|checkconfig|status}
2458 Note: the openrc's init.d is not shipped by default.
2459 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.
2460 Those few steps can be followed:
2463 wget https://git.alpinelinux.org/cgit/aports/plain/community/icinga2/icinga2.initd
2464 mv icinga2.initd /etc/init.d/icinga2
2465 chmod +x /etc/init.d/icinga2
2468 Icinga 2 reads a single configuration file which is used to specify all
2469 configuration settings (global settings, hosts, services, etc.). The
2470 configuration format is explained in detail in the [doc/](doc/) directory.
2472 By default `make install` installs example configuration files in
2473 `/usr/local/etc/icinga2` unless you have specified a different prefix or
2477 ### Windows Builds <a id="development-package-builds-windows"></a>
2479 The Windows MSI packages are located at https://packages.icinga.com/windows/
2481 #### Requirements <a id="development-package-builds-windows-requirements"></a>
2483 * 32 or 64-bit system
2484 * Visual Studio >= 14.1 2017
2491 Download the community edition from [visualstudio.com](https://www.visualstudio.com/en/downloads/)
2493 Workloads to install:
2498 ##### OpenSSL for Icinga
2500 Download custom OpenSSL builds from [openssl-windows GitHub project](https://github.com/Icinga/openssl-windows/releases).
2502 You need to install a binary dist version to 'C:\\Program Files\\OpenSSL'.
2504 The Powershell script `.\tools\win32\download-openssl.ps1` can be used for automated downloads.
2508 A simple package manager for Windows, please see [install instructions](https://chocolatey.org/install).
2512 Use Chocolatey, see [package details](https://chocolatey.org/packages/git).
2520 Use Chocolatey, see [package details](https://chocolatey.org/packages/winflexbison3).
2523 choco install winflexbison3
2528 Use Chocolatey, see [package details](https://chocolatey.org/packages/cmake)
2529 or download from: [cmake.org](https://cmake.org/download/)
2537 Use Chocolatey, see [package details](https://chocolatey.org/packages/wixtoolset).
2540 choco install wixtoolset
2545 Download third party Windows binaries from: [boost.org](http://www.boost.org/users/download/)
2547 For example: `https://dl.bintray.com/boostorg/release/1.65.1/binaries/boost_1_65_1-msvc-14.1-64.exe`
2550 * Must match your Visual Studio version!
2551 * CMake might not support the latest Boost version (we used CMake 3.10 and Boost 1_65_1)
2553 Run the installer exe.
2558 Run with VC Native x64 Command Prompt:
2561 powershell .\tools\win32\configure.ps1
2562 powershell .\tools\win32\build.ps1
2563 powershell .\tools\win32\test.ps1
2566 See these scripts for details.
2570 We are building [Icinga 2 with AppVeyor](https://ci.appveyor.com/project/icinga/icinga2) for testing and CI integration.
2572 Please check `appveyor.yml` for instructions.
2576 ## Advanced Development Tips <a id="development-advanced"></a>
2578 ### GDB Pretty Printers <a id="development-advanced-gdb-pretty-printer"></a>
2580 Install the `boost`, `python` and `icinga2` pretty printers. Absolute paths are required,
2581 so please make sure to update the installation paths accordingly (`pwd`).
2584 $ mkdir -p ~/.gdb_printers && cd ~/.gdb_printers
2587 Boost Pretty Printers compatible with Python 3:
2590 $ git clone https://github.com/mateidavid/Boost-Pretty-Printer.git && cd Boost-Pretty-Printer
2591 $ git checkout python-3
2593 /home/michi/.gdb_printers/Boost-Pretty-Printer
2596 Python Pretty Printers:
2599 $ cd ~/.gdb_printers
2600 $ svn co svn://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python
2603 Icinga 2 Pretty Printers:
2606 $ mkdir -p ~/.gdb_printers/icinga2 && cd ~/.gdb_printers/icinga2
2607 $ wget https://raw.githubusercontent.com/Icinga/icinga2/master/tools/debug/gdb/icingadbg.py
2610 Now you'll need to modify/setup your `~/.gdbinit` configuration file.
2611 You can download the one from Icinga 2 and modify all paths.
2613 Example on Fedora 22:
2616 $ wget https://raw.githubusercontent.com/Icinga/icinga2/master/tools/debug/gdb/gdbinit -O ~/.gdbinit
2623 sys.path.insert(0, '/home/michi/.gdb_printers/icinga2')
2624 from icingadbg import register_icinga_printers
2625 register_icinga_printers()
2630 sys.path.insert(0, '/home/michi/.gdb_printers/python')
2631 from libstdcxx.v6.printers import register_libstdcxx_printers
2633 register_libstdcxx_printers(None)
2640 sys.path.insert(0, '/home/michi/.gdb_printers/Boost-Pretty-Printer')
2642 boost_print.register_printers()
2646 If you are getting the following error when running gdb, the `libstdcxx`
2647 printers are already preloaded in your environment and you can remove
2648 the duplicate import in your `~/.gdbinit` file.
2651 RuntimeError: pretty-printer already registered: libstdc++-v6