netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] add JSON output format
@ 2016-02-22  9:12 Harald Welte
  2016-02-29 11:43 ` Pablo Neira Ayuso
  0 siblings, 1 reply; 2+ messages in thread
From: Harald Welte @ 2016-02-22  9:12 UTC (permalink / raw)
  To: netfilter-devel

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


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2016-02-29 11:43 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-02-22  9:12 [PATCH] add JSON output format Harald Welte
2016-02-29 11:43 ` Pablo Neira Ayuso

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).