Browse Source

nfacct json support: add timestamp to json output

neels/inmarsat
Harald Welte 7 years ago
parent
commit
d57ac4e3e6
  1. 34
      recipes-extra/nfacct/files/0001-Add-JSON-output-formatting-to-nfacct-utility.patch

34
recipes-extra/nfacct/files/0001-Add-JSON-output-formatting-to-nfacct-utility.patch

@ -1,4 +1,4 @@
From aefba0f03444345dd5384631203f61647637175d Mon Sep 17 00:00:00 2001
From 83745c87b294166ff4dbee4a781e7eb5d4f568b8 Mon Sep 17 00:00:00 2001
From: Harald Welte <laforge@gnumonks.org>
Date: Tue, 19 May 2015 22:12:06 +0200
Subject: [PATCH] Add JSON output formatting to nfacct utility
@ -6,8 +6,8 @@ Subject: [PATCH] Add JSON output formatting to nfacct utility
This is based on the JSON support patch of libnetfilter_acct.
---
nfacct.8 | 2 ++
src/nfacct.c | 87 ++++++++++++++++++++++++++++++++++++++++++++----------------
2 files changed, 66 insertions(+), 23 deletions(-)
src/nfacct.c | 89 ++++++++++++++++++++++++++++++++++++++++++++----------------
2 files changed, 68 insertions(+), 23 deletions(-)
diff --git a/nfacct.8 b/nfacct.8
index 0c3249c..bf563ae 100644
@ -23,7 +23,7 @@ index 0c3249c..bf563ae 100644
.SH SEE ALSO
.BR iptables (8)
diff --git a/src/nfacct.c b/src/nfacct.c
index 2546a6e..a55655a 100644
index 2546a6e..d46ee91 100644
--- a/src/nfacct.c
+++ b/src/nfacct.c
@@ -137,13 +137,13 @@ int main(int argc, char *argv[])
@ -42,7 +42,7 @@ index 2546a6e..a55655a 100644
nfacct = nfacct_alloc();
if (nfacct == NULL) {
@@ -156,17 +156,36 @@ static int nfacct_cb(const struct nlmsghdr *nlh, void *data)
@@ -156,17 +156,38 @@ static int nfacct_cb(const struct nlmsghdr *nlh, void *data)
goto err_free;
}
@ -60,7 +60,9 @@ index 2546a6e..a55655a 100644
+ break;
+ case NFACCT_SNPRINTF_T_JSON:
+ if (!fmt_header) {
+ printf("{ \"nfacct_counters\" : [\n");
+ printf("{ \"timestamp\" : %lld, \n"
+ " \"nfacct_counters\" : [\n",
+ (long long) time(NULL));
+ fmt_header = true;
+ } else
+ printf(",\n");
@ -87,7 +89,7 @@ index 2546a6e..a55655a 100644
err_free:
nfacct_free(nfacct);
@@ -174,9 +193,26 @@ err:
@@ -174,9 +195,26 @@ err:
return MNL_CB_OK;
}
@ -115,7 +117,7 @@ index 2546a6e..a55655a 100644
struct mnl_socket *nl;
char buf[MNL_SOCKET_BUFFER_SIZE];
struct nlmsghdr *nlh;
@@ -190,9 +226,13 @@ static int nfacct_cmd_list(int argc, char *argv[])
@@ -190,9 +228,13 @@ static int nfacct_cmd_list(int argc, char *argv[])
duparg(argv[i]);
zeroctr = true;
} else if (strncmp(argv[i], "xml", strlen(argv[i])) == 0) {
@ -131,7 +133,7 @@ index 2546a6e..a55655a 100644
} else if (strncmp(argv[i], "counters", strlen(argv[i])) == 0) {
if (mask || value)
duparg(argv[i]);
@@ -251,7 +291,7 @@ static int nfacct_cmd_list(int argc, char *argv[])
@@ -251,7 +293,7 @@ static int nfacct_cmd_list(int argc, char *argv[])
ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
while (ret > 0) {
@ -140,7 +142,7 @@ index 2546a6e..a55655a 100644
if (ret <= 0)
break;
ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
@@ -262,8 +302,7 @@ static int nfacct_cmd_list(int argc, char *argv[])
@@ -262,8 +304,7 @@ static int nfacct_cmd_list(int argc, char *argv[])
}
mnl_socket_close(nl);
@ -150,7 +152,7 @@ index 2546a6e..a55655a 100644
return 0;
}
@@ -444,7 +483,8 @@ static int nfacct_cmd_delete(int argc, char *argv[])
@@ -444,7 +485,8 @@ static int nfacct_cmd_delete(int argc, char *argv[])
static int nfacct_cmd_get(int argc, char *argv[])
{
@ -160,7 +162,7 @@ index 2546a6e..a55655a 100644
struct mnl_socket *nl;
char buf[MNL_SOCKET_BUFFER_SIZE];
struct nlmsghdr *nlh;
@@ -460,7 +500,9 @@ static int nfacct_cmd_get(int argc, char *argv[])
@@ -460,7 +502,9 @@ static int nfacct_cmd_get(int argc, char *argv[])
if (strncmp(argv[i], "reset", strlen(argv[i])) == 0) {
zeroctr = true;
} else if (strncmp(argv[i], "xml", strlen(argv[i])) == 0) {
@ -171,7 +173,7 @@ index 2546a6e..a55655a 100644
} else {
nfacct_perror("unknown argument");
return -1;
@@ -503,7 +545,7 @@ static int nfacct_cmd_get(int argc, char *argv[])
@@ -503,7 +547,7 @@ static int nfacct_cmd_get(int argc, char *argv[])
ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
while (ret > 0) {
@ -180,7 +182,7 @@ index 2546a6e..a55655a 100644
if (ret <= 0)
break;
ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
@@ -514,8 +556,7 @@ static int nfacct_cmd_get(int argc, char *argv[])
@@ -514,8 +558,7 @@ static int nfacct_cmd_get(int argc, char *argv[])
}
mnl_socket_close(nl);
@ -190,7 +192,7 @@ index 2546a6e..a55655a 100644
return 0;
}
@@ -660,7 +701,7 @@ static int nfacct_cmd_restore(int argc, char *argv[])
@@ -660,7 +703,7 @@ static int nfacct_cmd_restore(int argc, char *argv[])
static int nfacct_cmd_monitor(int argc, char *argv[])
{
struct mnl_socket *nl;
@ -199,7 +201,7 @@ index 2546a6e..a55655a 100644
char buf[MNL_SOCKET_BUFFER_SIZE];
int ret, option = NFNLGRP_ACCT_QUOTA;
@@ -685,7 +726,7 @@ static int nfacct_cmd_monitor(int argc, char *argv[])
@@ -685,7 +728,7 @@ static int nfacct_cmd_monitor(int argc, char *argv[])
break;
}

Loading…
Cancel
Save