sammler/network.c

54 lines
895 B
C

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "configfile.h"
#include "logging.h"
#define MAX_SIZE 8192
static char *pktbuf = NULL;
static int pos = 0;
static char *hostname = NULL;
void net_submit(char *plugin, int version, char *filename, int ds_id, char *data)
{
int len = 0;
if (pktbuf == NULL) {
pktbuf = malloc(MAX_SIZE);
pos = 0;
}
if (hostname == NULL)
hostname = config_get_string("global", "hostname", "localhost");
if (pos == 0) {
len = snprintf(pktbuf, MAX_SIZE - pos, "%s\n", hostname);
if (len < 0 || len >= MAX_SIZE - pos)
return;
pos += len;
}
len = snprintf(pktbuf + pos, MAX_SIZE - pos, "%s:%d:%s:%d %s\n",
plugin, version, filename, ds_id, data);
if (len < 0 || len >= MAX_SIZE - pos)
return;
pos += len;
return;
}
void net_commit()
{
if (pos == 0)
return;
log_print(LOG_ERROR, "%s", pktbuf);
pos = 0;
}