static int
nfacct_snprintf_plain(char *buf, size_t rem, struct nfacct *nfacct,
		      uint16_t flags)
{
	int ret;

	if (flags & NFACCT_SNPRINTF_F_FULL) {
		ret = snprintf(buf, rem,
			"{ pkts = %.20llu, bytes = %.20llu } = %s;",
			(unsigned long long)
			nfacct_attr_get_u64(nfacct, NFACCT_ATTR_PKTS),
			(unsigned long long)
			nfacct_attr_get_u64(nfacct, NFACCT_ATTR_BYTES),
			nfacct_attr_get_str(nfacct, NFACCT_ATTR_NAME));
	} else {
		ret = snprintf(buf, rem, "%s\n",
			nfacct_attr_get_str(nfacct, NFACCT_ATTR_NAME));
	}

	return ret;
}
예제 #2
0
static int nfacct_callback(const struct nlmsghdr *nlh, void *data) {
	if(data) {};

	if(!nfacct_list || nfacct_list->len == nfacct_list->size) {
		int size = (nfacct_list) ? nfacct_list->size : 0;
		int len = (nfacct_list) ? nfacct_list->len : 0;
		size++;

		info("nfacct.plugin: increasing nfacct_list to size %d", size);

		nfacct_list = realloc(nfacct_list, sizeof(struct nfacct_list) + (sizeof(struct mynfacct) * size));
		if(!nfacct_list) {
			error("nfacct.plugin: cannot allocate nfacct_list.");
			return MNL_CB_OK;
		}

		nfacct_list->data[len].nfacct = nfacct_alloc();
		if(!nfacct_list->data[size - 1].nfacct) {
			error("nfacct.plugin: nfacct_alloc() failed.");
			free(nfacct_list);
			nfacct_list = NULL;
			return MNL_CB_OK;
		}

		nfacct_list->size = size;
		nfacct_list->len = len;
	}

	if(nfacct_nlmsg_parse_payload(nlh, nfacct_list->data[nfacct_list->len].nfacct) < 0) {
		error("nfacct.plugin: nfacct_nlmsg_parse_payload() failed.");
		return MNL_CB_OK;
	}

	nfacct_list->data[nfacct_list->len].name  = nfacct_attr_get_str(nfacct_list->data[nfacct_list->len].nfacct, NFACCT_ATTR_NAME);
	nfacct_list->data[nfacct_list->len].pkts  = nfacct_attr_get_u64(nfacct_list->data[nfacct_list->len].nfacct, NFACCT_ATTR_PKTS);
	nfacct_list->data[nfacct_list->len].bytes = nfacct_attr_get_u64(nfacct_list->data[nfacct_list->len].nfacct, NFACCT_ATTR_BYTES);

	nfacct_list->len++;
	return MNL_CB_OK;
}
static int
nfacct_snprintf_xml(char *buf, size_t rem, struct nfacct *nfacct,
		    uint16_t flags)
{
	int ret = 0;
	unsigned int size = 0, offset = 0;

	ret = snprintf(buf, rem,
			"<obj><name>%s</name>"
			"<pkts>%.20llu</pkts>"
			"<bytes>%.20llu</bytes>",
			nfacct_attr_get_str(nfacct, NFACCT_ATTR_NAME),
			(unsigned long long)
			nfacct_attr_get_u64(nfacct, NFACCT_ATTR_BYTES),
			(unsigned long long)
			nfacct_attr_get_u64(nfacct, NFACCT_ATTR_PKTS));
	BUFFER_SIZE(ret, size, rem, offset);

	if (flags & NFACCT_SNPRINTF_F_TIME) {
		time_t t;
		struct tm tm;

		t = time(NULL);
		if (localtime_r(&t, &tm) == NULL)
			goto err;

		ret = nfacct_snprintf_xml_localtime(buf+offset, rem, &tm);
	        BUFFER_SIZE(ret, size, rem, offset);
	}

	ret = snprintf(buf+offset, rem, "</obj>");
	BUFFER_SIZE(ret, size, rem, offset);

err:
	return ret;
}