This document describes how to setup Naemon in a development environment with attached Debugger using Eclipse on Ubuntu 18.04
If you are looking for an production setup please follow one of these guides:
I'm not a professional C developer. Want to improve this document? Please send a Pull Request to https://github.com/statusengine/docs
This tutorial contains a lot of screenshots. Hopefully this helps developers that are not familiar with eclipse (like myself) to follow this guide.
Please run all commands as your user. It is not recommended to use root
user.
Download the latest version of Eclipse IDE for C/C++ Developers using your favorite web browser.
Install OpenJDK
sudo apt-get update
sudo apt-get install default-jre
Open a terminal and navigate to your downloads folder. Extract Eclipse to your home directory:
tar xfv eclipse-cpp-2019-03-R-linux-gtk-x86_64.tar.gz -C ~/
Execute:
~/eclipse/eclipse
Use the default path as Workspace and continue.
If you see the welcome message just minimize Eclipse for now.
sudo apt-get update
sudo apt-get install build-essential automake gperf help2man libtool libglib2.0-dev gdb monitoring-plugins git valgrind
To run Naemon a few configuration files are required like naemon.cfg
and a host and service definition.
I'm used to use the default configuration examples that are shipped with Naemon.
I'm also pretty sure that there is an easier way to generate this configuration files, which I didn't discovered yet. ¯\_(ツ)_/¯
cd /tmp/
wget https://github.com/naemon/naemon-core/archive/v1.0.10.tar.gz
tar xfv v1.0.10.tar.gz
cd naemon-core-1.0.10/
mkdir ~/naemon-dev
./autogen.sh --prefix=$(echo ~/naemon-dev) --with-naemon-user=$(whoami) --with-naemon-group=users --with-pluginsdir=/usr/lib/nagios/plugins
make all
make install
mkdir -p ~/naemon-dev/var/
mkdir -p ~/naemon-dev/var/cache/naemon
mkdir -p ~/naemon-dev/var/spool/checkresults
mkdir ~/naemon-dev/etc/naemon/module-conf.d
rm -rf ~/naemon-dev/bin
rm -rf ~/naemon-dev/include
rm -rf ~/naemon-dev/lib
rm -rf ~/naemon-dev/share
mkdir -p ~/git
cd ~/git
git clone https://github.com/naemon/naemon-core.git
Switch back to Eclipse IDE.
First of all, close the welcome message.
Now import the Naemon code to Eclipse by navigate to File Import
Select C/C++
Existing code as Autotools project
and continue.
Set the path where the Naemon source code is located (~/git/naemon-core
) and continue.
Finish import.
Now its time to enable Eclipse to compile and run Naemon.
Go to Project Properties Autotools Configuration Settings
Set the --prefix
option to the path, where your naemon configuration is stored: /home/$USERNAME$/naemon-dev
.
Replace $USERNAME$ with your user name.
Make sure to enable the Debug option
And set the following additional command line options:
--with-naemon-user=$(echo ~/naemon-dev) --with-naemon-group=users --with-pluginsdir=/usr/lib/nagios/plugins
Make sure that all Builders are checked and click on Apply and Close
Now build Naemon Core through Project Build All
Check that the build was successful.
After Naemon was built successfully through the IDE you can set up the "Run environment" for Naemon.
This allows you to run Naemon Core inside the IDE.
Navigate to Project Properties Run/Debug Settings New C/C++ Application and click on Search Project and select the naemon binary.
Switch to the Arguments Tab and set /home/$USERNAME$/naemon-dev/etc/naemon/naemon.cfg
as argument.
Replace $USERNAME$ with your user name.
Switch to the Environment Tab and create a new environment variable:
Name: LD_LIBRARY_PATH
Value: /home/$USERNAME$/git/naemon-core/.libs:$LD_LIBRARY_PATH
Replace $USERNAME$ with your user name.
Press Apply and close the window.
Run Naemon through the IDE and check the Console for the following line:
Successfully launched command file worker with pid <PID>
Press Stop to kill the Naemon process.
Now you are able to configure, compile and run Naemon inside the IDE. In the last step you need to configure the IDE to be able to attach a Debugger.
Open the Debug Configurations
Select
C/C++ Application Naemon-Core Build
Debugger Tab Disable Stop on startup at: main
Apply and close the window.
Open a C-File, for Example src/naemon/checks_service.c
and add a Breakpoint by double clicking on a line number (line 127 in my case).
Than start Naemon with the Debugger.
If Eclipse ask you if you want to switch into the Debug perspective select switch.
As soon as the code reaches your Breakpoint you can debug as you are used to.
Valgrind is a powerful profiling tool which can also be integrated to Eclipse. It can be used to find memory leaks.
Navigate to Project Properties Run/Debug Settings New Profile With Valgrind and click on Search Project and select the naemon binary.
Switch to the Arguments Tab and set /home/$USERNAME$/naemon-dev/etc/naemon/naemon.cfg
as argument.
Replace $USERNAME$ with your user name.
Switch to the Valgrind Options Tab. Valgrind provides different profiling tools. I chosed Massif
in my case.
Switch to the Environment Tab and create a new environment variable:
Name: LD_LIBRARY_PATH
Value: /home/$USERNAME$/git/naemon-core/.libs:$LD_LIBRARY_PATH
Replace $USERNAME$ with your user name.
Switch you Launch Configuration to Naemon Valgrind
and click on Launch in Profile mode
Let Naemon run in profiling mode for a few minutes. As soon as you stop Naemon you will get a heap allocation chart.
Happy hacking!