[collectd] patch: monitor serial port traffic
David Bacher
drbacher at gmail.com
Sat Oct 29 01:53:42 CEST 2005
Skipped content of type multipart/alternative-------------- next part --------------
#ifndef SERIAL_H
#define SERIAL_H
#include "collectd.h"
#include "common.h"
#ifndef COLLECT_SERIAL
#if defined(KERNEL_LINUX) || defined(HAVE_LIBKSTAT) || defined(HAVE_LIBSTATGRAB)
#define COLLECT_SERIAL 1
#else
#define COLLECT_SERIAL 0
#endif
#endif /* !defined(COLLECT_SERIAL) */
#endif /* SERIAL_H */
-------------- next part --------------
#include "serial.h"
#if COLLECT_SERIAL
#define MODULE_NAME "serial"
#include "plugin.h"
#include "common.h"
static char *serial_filename_template = "serial-%s.rrd";
#ifdef HAVE_LIBKSTAT
/* TODO: fill in appropriate LIBKSTAT declaration */
#endif /* HAVE_LIBKSTAT */
static char *ds_def[] =
{
"DS:incoming:COUNTER:25:0:U",
"DS:outgoing:COUNTER:25:0:U",
NULL
};
static int ds_num = 2;
void serial_init (void)
{
#ifdef HAVE_LIBKSTAT
/* TODO: fill in appropriate LIBKSTAT initialization functions */
#endif /* HAVE_LIBKSTAT */
}
void serial_write (char *host, char *inst, char *val)
{
char file[512];
int status;
status = snprintf (file, 512, serial_filename_template, inst);
if (status < 1)
return;
else if (status >= 512)
return;
#ifdef HAVE_RRD
rrd_update_file (host, file, val, ds_def, ds_num);
#endif /* HAVE_RRD */
}
#define BUFSIZE 512
void serial_submit (char *device,
unsigned long long incoming,
unsigned long long outgoing)
{
char buf[BUFSIZE];
time_t curtime = time(NULL);
if (snprintf (buf, BUFSIZE, "%u:%lld:%lld", (unsigned int) curtime, incoming, outgoing) >= BUFSIZE)
return;
plugin_submit (MODULE_NAME, device, buf);
}
#undef BUFSIZE
void serial_read (void)
{
#ifdef KERNEL_LINUX
FILE *fh;
char buffer[1024];
unsigned long long incoming, outgoing;
char device[16];
char *dummy;
char *fields[16];
int i, numfields;
/* there are a variety of names for the serial device */
if ((fh = fopen ("/proc/tty/driver/ttyS", "r")) == NULL &&
(fh = fopen ("/proc/tty/driver/serial", "r")) == NULL)
{
syslog (LOG_WARNING, "serial: fopen: %s", strerror (errno));
return;
}
while (fgets (buffer, 1024, fh) != NULL)
{
/* check whether this is a numbered serial device */
if (buffer[1] != ':' || buffer[0] < '0' || buffer[0] > '9')
continue;
snprintf(device, sizeof(device)-1, "ttyS%c", buffer[0]);
/* TODO: needs more error checking */
numfields = strsplit (buffer, fields, 16);
if (numfields < 1)
continue;
for (i = 1; i < numfields; ++i)
{
char* field = fields[i];
if (strncmp(field, "tx:", 3) == 0)
{
outgoing = atoll(&field[3]);
}
else if (strncmp(field, "rx:", 3) == 0)
{
incoming = atoll(&field[3]);
}
}
serial_submit (device, incoming, outgoing);
}
fclose (fh);
/* #endif KERNEL_LINUX */
#elif defined(HAVE_LIBKSTAT)
/* TODO: fill in appropriate LIBKSTAT functions */
/* #endif HAVE_LIBKSTAT */
#elif defined(HAVE_LIBSTATGRAB)
/* TODO: fill in appropriate LIBSTATGRAB functions */
#endif /* HAVE_LIBSTATGRAB */
}
void module_register (void)
{
plugin_register (MODULE_NAME, serial_init, serial_read, serial_write);
}
#undef MODULE_NAME
#endif /* COLLECT_SERIAL */
More information about the Collectd
mailing list