commands: External command handling system overhaul

HUGE PATCH WARNING: This is a significant changeset in a single commit.
I'm deeply sorry for this, but the (previous) state of this TU makes it
non-trivial to make incremental changes to achieve big changes. Coupled
with the fact that the branch was in a state of decay (I haven't had the
time to touch it for a few months), it was quite the challenge to merge
with Naemon's new indentation rules and other changes. Again, apologies.

This patch-set is the first step towards getting a sane command
handling system. It is not finished, but it should in its current form
be a significant improvement to the one it replaces.

Patch overview:

* Proper single-point parsing, and verification of external commands,
 including arguments and their respective types:
    a) Proper error reporting on violation of the above
    b) Automatic resolution of referenced objects
* Wireframe for supporting different parse modes
* On-the-fly creation of custom external commands
    a) Default parameters
    b) In-core command descriptions
* Better error messages
* Relevant ABI/API backwards compatibility preserved
* Future changes

Detailed information:

** Proper single-point parsing, and verification of external commands,
 including arguments and their respective types

Parsing of external commands was previously very much ad-hoc and deeply
in violation of DRY, leading to various errors that should be trivial to
avoid. Each command handling routine reinventing the parsing lead to
minor variations in how arguments were parsed and resolved as well as
errors stemming from simply parsing the wrong argument.

This patch instead introduces a generic (but not too generic),
batteries-included parsing system, which verifies the type of arguments
based on typing information specified at the time of their creation
(more on this later).
This leads to a consistent approach to error handling/reporting and, in
general, a more pleasant way of working with external commands.

Also included is automatic resolution of referenced objects, such as
hosts, services, timeperiods, etc. This is something that was previously
also very ad-hoc, leading to inconsistencies in error reporting, should
you reference an object that doesn't exist or in some other way, was
"bad".

Of course, this patch introduces a number of new error codes/messages to
reflect the state of execution.

** Wireframe for supporting different parse modes
We have, historically, been tremendously locked-in to the syntax of
external commands. While this may not seem like a huge problem to most
people (especially those comfortable with the Nagios-style syntax of
semicolon separators and what-not), it is hardly optimal for a number of
reasons. For example, by using a position-dependant syntax, we require
users to _always_ specify _every_ argument, even if that means simply
specifying whatever seems more "default". A better approach would likely
be to adopt a key:value-like syntax, and allow commands to have default
values for unspecified parameters (which we now also have, see more
below). This would also ease integration with external systems/syntaxes
and commonly used datastructures such as JSON/maps/dictionaries.

This patch, however does not include such a parse mode, since it should,
in my opinion, be discussed further among the developers what such a
mode should entail.

** On-the-fly creation of custom external commands
Naturally, to be able to support type checking of parameters, we need to
be able to specify their types. This patch includes an API that allows
developers to create and register their commands with the external
command subsystem. Created commands need information such as a name, a
description and a list of types/arguments as well as a
developer-specified callback. It is also possible to specify default
values for arguments. In addition, for backwards compatibility
reasons, the system supports specifying a "command code",
which is simply a unique integer macro as used in many of the
pre-existing command handlers.

To increase ease-of-use, I've also includes a number of helper macros
and a trivial syntax for declaring parameters at creation-time.

** Better error messages
This work was already started a while back, and some of it has already
been merged. It is still not perfect, and still needs more effort, but
at least it's something.

** Relevant ABI/API backwards compatibility preserved
Nagios has historically not cared much about which functions are
exported (non-static) outside of TUs. Because of this, it is nigh
impossible to make significant changes without breaking _some_ sort of
API. I have, however, tried very hard (and of course, after getting some
input on the matter from AE) to only change/remove things that
sane people would actually have used. It is truly my belief that this
patch will not break existing NEBs API-wise.

** Future changes
In the future, I hope to, among other things:

* Implement a key-value command syntax, as discussed above.
* Provide a reasonably opaque data structure in which to expose parse
 errors ("syntax error in position <n>", "parameter <n> has the wrong
 type", "parameter <name> is a <type> that does not exist"). This would
 allow, for example, query handlers to expose errors in a way that makes
 sense to their use case.
* Consolidate and deprecate superfluous commands. Today, we have a
large number of commands, which differ only in parameter count and
minor implementation details. For example, SCHEDULE_SVC_CHECK and
SCHEDULE_FORCED_SVC_CHECK are two different commands. This example
could easily be implemented as a SCHEDULE_SVC_CHECK command with a
'force' flag, default off. Again, there are a lot of commands like
this, and this is one of the less horrendous ones. I believe that doing
so would much reduce the complexity of the system, with all the benefits
that usually brings.

Thanks-to: Andreas Ericsson <ae@op5.se>
Thanks-to: Carl Helmertz <chelmertz@op5.com>
Signed-off-by: Anton Lofgren <alofgren@op5.com>
naemon: Register and initialize external command register

This patch enables the use of the external commands register.

Signed-off-by: Anton Lofgren <alofgren@op5.com>
commands/test: Test external command register

This patch adds and integrates tests for parsing of, resolution of and
execution of external commands.

I was initially inclined to write a test for every external command that
we currently support. However, I finally realised that that would take
me way too much time (as in, I would never finish it). Besides, as I've
mentioned earlier, I think we should start deprecating and consolidating
many of the commands that we currently support. And hey, it's better
than the 0 runnable test cases we previously had.

Signed-off-by: Anton Lofgren <alofgren@op5.com>
Remove dependencies in wproc, use wproc_run_callback

Designing the system to keep as few dependencies between the modules
in the system as low as possible is generally a good thing. That
makes each module do it's own thing without knowledge of the other
parts of the system. That means, each functionality has its own
distinct border, and debugging is mostly easier.

One part in this cleanup is wproc:s. wproc handles job types as
integers, and each job type can only be called from one other module.
But the wproc needs to have knowledege of the other module, because
it should be able to call back to that module depending on job type,
by using a switch statement in the wproc result handler.

Better structure is to use function pointers, and design a generic
callback, because then, the wproc module doesn't know anything at all
about the caller, and the explicit dependencies to other modules can
be reamoved.

Using function callbacks makes it having fewer special cases, and
makes it easier to set up test cases for.

Signed-off-by: Max Sikstrom <msikstrom@op5.com>
Fix memory leak when running callback worker job

Destroying a callback job didn't free the callback storage, causing a
memory leak. Fix that!

This wasn't an issue earlier, when callbacks wasn't used much at all
(only some broker modules used that on some user interactions)

Signed-off-by: Max Sikstrom <msikstrom@op5.com>
Notifications: Use wproc_run_callback

By using wproc_run_callback instead of a notify-specific job type, we
can loose the dependency from workers.c to notifications.

Signed-off-by: Max Sikstrom <msikstrom@op5.com>
Convert OCSP and OCHP to wproc_run_callback

Continue to reduce dependencies from wproc to other parts of the system.

Signed-off-by: Max Sikstrom <msikstrom@op5.com>
Convert event handlers to wproc_run_callback

This reduces the knowledege of the surrounding environment in the wproc
module further. That makes it easier to write tests for later.

Signed-off-by: Max Sikstrom <msikstrom@op5.com>
Convert perfdata to wproc_run_callback

performance data doesn't need to handle the result, but convert it to
wproc_run_callback, so that's the only used interface for wproc.

Signed-off-by: Max Sikstrom <msikstrom@op5.com>
Cleanup wproc, when we don't use types anymore

Since the earlier cleanup, all worker calls results in callback jobs,
there's no need to have any job types anymore. Cleanup all type-related
code in wproc, and merge structs, so job contains callback and data
pointers directly

Signed-off-by: Max Sikstrom <msikstrom@op5.com>
Introduce code coverage testing for libnaemon

It's the foundation upon which we put all other building blocks, so
let's be anal when we test it and aim for close to 100% code coverage,
minus the _strerror() functions and a few others that simply don't
apply.

Signed-off-by: Andreas Ericsson <ae@op5.se>
Show some love to the .gitignore files

Namely, ignore pretty much all test*.trs and test*.log files, although
we do so on a directory basis, since we may want to incorporate test
data later and that test-data will almost certainly have .log files if
we do.

Also, remove the tests from the t-tap directory .gitignore that haven't
been built these past five years.

Signed-off-by: Andreas Ericsson <ae@op5.se>
Fix a bunch of compiler warnings and annoyances with tests

The indentation was pretty horrible, and the plethora of warnings
was just appalling. Naming variables the same as types is not only
frowned upon, but an actual error for many compilers, and mixing
declarations and statements is illegal in ISO C90.

The rest is mostly just getting rid of shadowed variables and
functions and getting included files in the right order.

Signed-off-by: Andreas Ericsson <ae@op5.se>
sretention: Remove some debugging output

Leftover printf()'s from the times when state retention was made
configuration sensitive.

Signed-off-by: Andreas Ericsson <ae@op5.se>
downtime: Log properly when and why we fail to add downtime

Previously that stuff ended up in the debug log, which isn't always
enabled. With this patch, it will end up in the normal logfile
where real errors belong (even though they may well be user errors).

Signed-off-by: Andreas Ericsson <ae@op5.se>
t-tap/smallconfig/retention.dat: Make retained host downtime sensible

It doesn't make sense to have zero-second downtimes, and the code we
have today rightly denies such crap as nonsense. However, the tests
in test_config.c (well, soon-to-be test_config.c) attempts to locate
a host downtime with exactly those parameters, stashed in our
retention data testfile.

This patch fixes it by nudging the end_time by 10 seconds, thus
allowing the downtime a small piece of space in the fourth dimension
in where it can, if not thrive, then at least exist.

Signed-off-by: Andreas Ericsson <ae@op5.se>
Reintroduce config and retention data testing

The old test_nagios_config.c has been revived as test_config.c and
has been given some love so that it actually works and passes just
fine now.

Signed-off-by: Andreas Ericsson <ae@op5.se>
t-tap/smallconfig/naemon.cfg: Enable logging, but to stdout

All the automated testrunners we have grab stdout and send us in
nice emails and stuff. With this patch we'll be spending a little
less time bughunting and a little more time on the fun stuff, like
eating icecream, drinking beer and going bungyjumping.

Signed-off-by: Andreas Ericsson <ae@op5.se>
t-tap/test_timeperiods: Fix 'em

Faster than the speed of Robin. Shuzam!

Signed-off-by: Andreas Ericsson <ae@op5.se>
macros: Escape note macros as well

It seems that randomly blacklisting macros that a user manages to put a
doublequote into, instead of realizing that our whole approach is FUBAR
and that pretty much any macro can be used to break notification
commands, is the way to go. Jay :(

This resolves bug op5#8266.

Change-Id: I746b140947ca397059907518ea4d87c21ce138fe
Signed-off-by: Robin Sonefors <robin.sonefors@op5.com>