Set up a Naemon development environment with Eclipse

Set up a Naemon development environment with Eclipse

This guide sets up Naemon Core for development, with a debugger and Valgrind attached, using Eclipse. For a production install, follow Install Naemon Core on Ubuntu instead.

ℹ️
Written against Ubuntu 18.04 and Eclipse 2019-03, and the screenshots come from that. The steps still hold, but Eclipse has moved some menus around since — treat the pictures as a guide to roughly where things live rather than an exact match.

Run everything as your own user, not as root.

Install Eclipse

Download Eclipse IDE for C/C++ Developers , then install a JRE:

sudo apt-get update
sudo apt-get install default-jre

Extract Eclipse into your home directory — substitute the filename you actually downloaded:

tar xfv eclipse-cpp-2019-03-R-linux-gtk-x86_64.tar.gz -C ~/

Start it:

~/eclipse/eclipse

Accept the default workspace path.

Set the Eclipse workspace

Minimise the welcome screen for now.

The Eclipse welcome screen

Install the Naemon build dependencies

sudo apt-get update
sudo apt-get install build-essential automake gperf help2man libtool libglib2.0-dev gdb monitoring-plugins git valgrind

Build a basic Naemon configuration

Naemon needs a naemon.cfg plus a host and service definition before it will start. The quickest way to get them is to install a release once, into a throwaway prefix, and keep only the configuration it generates:

cd /tmp/
wget https://github.com/naemon/naemon-core/archive/v1.5.2.tar.gz
tar xfv v1.5.2.tar.gz
cd naemon-core-1.5.2/

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

The four rm -rf lines are the point of the exercise: the binaries go away again, and only ~/naemon-dev/etc and ~/naemon-dev/var are kept. Eclipse builds the binary from now on.

Clone the Naemon source

mkdir -p ~/git
cd ~/git

git clone https://github.com/naemon/naemon-core.git

Configure Eclipse

Close the welcome screen, then import the code with File → Import.

Import code into Eclipse

Choose C/C++ → Existing code as Autotools project.

Import as an Autotools project

Point it at ~/git/naemon-core.

Select the source path

Finish the import.

Finish the import

Set up Autotools

Go to Project → Properties → Autotools → Configuration Settings.

Project properties

Set --prefix to the configuration you built above, /home/<your user>/naemon-dev.

Set the Autotools prefix

Enable the debug option.

Enable debug

Add these additional command line options:

--with-naemon-user=$(echo ~/naemon-dev) --with-naemon-group=users --with-pluginsdir=/usr/lib/nagios/plugins

Additional command line options

Check that all Builders are enabled, then Apply and Close.

Check the builders

Build with Project → Build All.

Build Naemon

Confirm the build succeeded.

A successful build

Set up the run configuration

Go to Project → Properties → Run/Debug Settings → New → C/C++ Application, click Search Project and pick the naemon binary.

Set up the run configuration

On the Arguments tab, pass /home/<your user>/naemon-dev/etc/naemon/naemon.cfg.

Run arguments

On the Environment tab, add LD_LIBRARY_PATH with the value /home/<your user>/git/naemon-core/.libs:$LD_LIBRARY_PATH. Without it the binary picks up an installed Naemon library instead of the one you just built.

Run environment

Apply and close.

Run Naemon inside the IDE

Run it, and watch the console for:

Successfully launched command file worker with pid <PID>

Running Naemon

Naemon started successfully

Press Stop to kill the process.

Attach the debugger

Open the Debug Configurations.

Open the debug configuration

Under C/C++ Application → Naemon-Core Build → Debugger, turn off Stop on startup at: main, then apply and close.

Set the debug configuration

Run with the debugger attached

Open a source file — src/naemon/checks_service.c is a good one — and set a breakpoint by double-clicking a line number. Then start Naemon under the debugger.

Set a breakpoint

Switch to the debug perspective when Eclipse offers.

Switch to the debug perspective

Once execution reaches the breakpoint you can step through as usual.

Stopped at a breakpoint

Set up Valgrind

Valgrind finds memory leaks and can run from inside Eclipse. Go to Project → Properties → Run/Debug Settings → New → Profile With Valgrind, click Search Project and pick the naemon binary.

Create a Valgrind profile

On the Arguments tab, pass /home/<your user>/naemon-dev/etc/naemon/naemon.cfg again.

Valgrind profile arguments

On the Valgrind Options tab, pick a tool — Valgrind offers several ; Massif is the one used here.

Valgrind options

Set the same LD_LIBRARY_PATH on the Environment tab.

Valgrind environment variable

Run Naemon through Valgrind

Switch the launch configuration to Naemon Valgrind and launch in profile mode.

Launch configuration

Launch in profile mode

Let it run for a few minutes. When you stop Naemon you get a heap allocation chart.

Heap allocation chart

Happy hacking!