summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGergely Nagy <algernon@balabit.hu>2012-03-21 12:57:19 +0100
committerGergely Nagy <algernon@balabit.hu>2012-03-21 12:57:19 +0100
commitdcea6d50ba8cfd7985c5628a20ce19f0b9ce9993 (patch)
treea7c0f6e25114bb42fd6fdc92850c692ab4cbcd92
parent70e16501eae0d5990df75d6a57f60cb827754d74 (diff)
Include the hostname in the JSON payload.
Include the hostname (as returned by gethostname()) in the JSON payload. Test case updated to verify this. Signed-off-by: Gergely Nagy <algernon@balabit.hu>
-rw-r--r--TODO.org6
-rw-r--r--lib/cee-syslog.c13
-rw-r--r--t/test_cee_format.c6
3 files changed, 24 insertions, 1 deletions
diff --git a/TODO.org b/TODO.org
index a39db5b..8af859a 100644
--- a/TODO.org
+++ b/TODO.org
@@ -16,7 +16,11 @@ a wrapper, so this also needs some configure-time discovery.
*** TODO Timestamp in the payload
Not enabled by default, can be toggled on with LOG_CEE_TIMESTAMP.
**** What precision do we want?
-*** MAYBE Hostname in the payload
+*** DONE Hostname in the payload
+CLOSED: [2012-03-21 Wed 12:53]
+- CLOSING NOTE [2012-03-21 Wed 12:53] \\
+ Done, we use gethostname(), as that's the easiest that yields somewhat
+ useful results.
Do we want it, at all? If so, cache gethostname() and be happy, or is
there any other, better way?
diff --git a/lib/cee-syslog.c b/lib/cee-syslog.c
index bac8435..6ad7abb 100644
--- a/lib/cee-syslog.c
+++ b/lib/cee-syslog.c
@@ -37,6 +37,7 @@
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
+#include <limits.h>
#include "cee-syslog.h"
@@ -56,6 +57,7 @@ static __thread struct
uid_t uid;
gid_t gid;
const char *ident;
+ char hostname[HOST_NAME_MAX + 1];
} cee_sys_settings;
static void
@@ -77,6 +79,8 @@ cee_openlog (const char *ident, int option, int facility)
cee_sys_settings.gid = getgid ();
cee_sys_settings.uid = getuid ();
cee_sys_settings.ident = ident;
+
+ gethostname (cee_sys_settings.hostname, HOST_NAME_MAX);
}
/** HELPERS **/
@@ -137,6 +141,14 @@ _get_gid (void)
return cee_sys_settings.gid;
}
+static inline const char *
+_get_hostname (void)
+{
+ if (cee_sys_settings.flags & LOG_CEE_NOCACHE)
+ gethostname (cee_sys_settings.hostname, HOST_NAME_MAX);
+ return cee_sys_settings.hostname;
+}
+
static struct json_object *
_cee_json_vappend (struct json_object *json, va_list ap)
{
@@ -179,6 +191,7 @@ _cee_discover (struct json_object *jo, int priority)
"program", "%s", cee_sys_settings.ident,
"uid", "%d", _get_uid (),
"gid", "%d", _get_gid (),
+ "host", "%s", _get_hostname (),
NULL);
}
diff --git a/t/test_cee_format.c b/t/test_cee_format.c
index 29a4ffd..145c810 100644
--- a/t/test_cee_format.c
+++ b/t/test_cee_format.c
@@ -8,6 +8,7 @@
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
+#include <limits.h>
static void
verify_value (struct json_object *jo, const char *key,
@@ -60,6 +61,7 @@ test_simple (void)
{
char *msg;
struct json_object *jo;
+ char host[HOST_NAME_MAX + 1];
openlog ("cee-syslog/test_simple", 0, LOG_LOCAL0);
@@ -67,6 +69,8 @@ test_simple (void)
jo = parse_msg (msg);
free (msg);
+ gethostname (host, HOST_NAME_MAX);
+
verify_value (jo, "msg", "hello, I'm test_simple!");
verify_value (jo, "facility", "local0");
verify_value (jo, "priority", "debug");
@@ -74,6 +78,7 @@ test_simple (void)
verify_value_exists (jo, "pid");
verify_value_exists (jo, "uid");
verify_value_exists (jo, "gid");
+ verify_value (jo, "host", host);
json_object_put (jo);
@@ -99,6 +104,7 @@ test_no_discover (void)
verify_value_missing (jo, "pid");
verify_value_missing (jo, "uid");
verify_value_missing (jo, "gid");
+ verify_value_missing (jo, "host");
json_object_put (jo);