From: Harald Welte <laforge@gnumonks.org>
To: netfilter-devel@vger.kernel.org
Subject: [PATCH] add JSON output format
Date: Mon, 22 Feb 2016 10:12:17 +0100 [thread overview]
Message-ID: <1456132337-23426-1-git-send-email-laforge@gnumonks.org> (raw)
the original output format looks a bit like JSON, but isn't. The XML
output is useful if you deal with XML, but a lot of applications prefer
more lightweight formats like YAML/JSON.
This adds the JSON output format to libnetfilter_acct, which will
subsequently be used by a similar change in the nfacct utility.
---
include/libnetfilter_acct/libnetfilter_acct.h | 1 +
src/libnetfilter_acct.c | 46 +++++++++++++++++++++++++++
2 files changed, 47 insertions(+)
diff --git a/include/libnetfilter_acct/libnetfilter_acct.h b/include/libnetfilter_acct/libnetfilter_acct.h
index c6ed858..647490f 100644
--- a/include/libnetfilter_acct/libnetfilter_acct.h
+++ b/include/libnetfilter_acct/libnetfilter_acct.h
@@ -41,6 +41,7 @@ int nfacct_nlmsg_parse_payload(const struct nlmsghdr *nlh, struct nfacct *nfacct
#define NFACCT_SNPRINTF_T_PLAIN 0
#define NFACCT_SNPRINTF_T_XML 1
+#define NFACCT_SNPRINTF_T_JSON 2
int nfacct_snprintf(char *buf, size_t size, struct nfacct *nfacct, uint16_t type, uint16_t flags);
diff --git a/src/libnetfilter_acct.c b/src/libnetfilter_acct.c
index bb15c68..b0bcf67 100644
--- a/src/libnetfilter_acct.c
+++ b/src/libnetfilter_acct.c
@@ -297,6 +297,49 @@ nfacct_snprintf_plain(char *buf, size_t rem, struct nfacct *nfacct,
return len;
}
+static int
+nfacct_snprintf_json(char *buf, size_t rem, struct nfacct *nfacct,
+ uint16_t flags)
+{
+ int ret = 0, offset = 0, len = 0;
+
+ if (flags & NFACCT_SNPRINTF_F_FULL) {
+ ret = snprintf(buf, rem,
+ " { \"pkts\" : %"PRIu64", \"bytes\" : %"PRIu64"",
+ nfacct_attr_get_u64(nfacct, NFACCT_ATTR_PKTS),
+ nfacct_attr_get_u64(nfacct, NFACCT_ATTR_BYTES));
+ SNPRINTF_CHECK(ret, rem, offset, len);
+
+ if (nfacct->flags) {
+ uint32_t mode;
+ char *mode_name;
+
+ mode = nfacct_attr_get_u64(nfacct, NFACCT_ATTR_FLAGS);
+ if (mode & NFACCT_F_QUOTA_PKTS)
+ mode_name = "packet";
+ else if (mode & NFACCT_F_QUOTA_BYTES)
+ mode_name = "byte";
+ else
+ mode_name = "unknown";
+
+ ret = snprintf(buf + offset, rem,
+ ", \"quota\" : %"PRIu64", \"mode\" = \"%s\""\
+ ", \"overquota\" = %u",
+ nfacct_attr_get_u64(nfacct, NFACCT_ATTR_QUOTA),
+ mode_name,
+ mode & NFACCT_F_OVERQUOTA ? 1 : 0);
+ SNPRINTF_CHECK(ret, rem, offset, len);
+ }
+
+ ret = snprintf(buf + offset, rem, ", \"name\" : \"%s\" }",
+ nfacct_attr_get_str(nfacct, NFACCT_ATTR_NAME));
+ }
+ /* non-F_FULL doesn't seem to make sense in JSON */
+ SNPRINTF_CHECK(ret, rem, offset, len);
+
+ return len;
+}
+
#define BUFFER_SIZE(ret, size, rem, offset) \
size += ret; \
if (ret > rem) \
@@ -393,6 +436,9 @@ int nfacct_snprintf(char *buf, size_t size, struct nfacct *nfacct,
case NFACCT_SNPRINTF_T_XML:
ret = nfacct_snprintf_xml(buf, size, nfacct, flags);
break;
+ case NFACCT_SNPRINTF_T_JSON:
+ ret = nfacct_snprintf_json(buf, size, nfacct, flags);
+ break;
default:
ret = -1;
break;
--
2.7.0
next reply other threads:[~2016-02-22 9:38 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-02-22 9:12 Harald Welte [this message]
2016-02-29 11:43 ` [PATCH] add JSON output format Pablo Neira Ayuso
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1456132337-23426-1-git-send-email-laforge@gnumonks.org \
--to=laforge@gnumonks.org \
--cc=netfilter-devel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).