network & mount plugin

This commit is contained in:
Olaf Rempel 2007-04-01 16:51:39 +02:00
parent 207649b7b6
commit eb17f95748
4 changed files with 86 additions and 56 deletions

View File

@ -17,7 +17,7 @@
#include "plugins.h" #include "plugins.h"
#include "rrdtool.h" #include "rrdtool.h"
#define BUFSIZE 1024 #define PKTSIZE 1400
struct net_entry { struct net_entry {
struct list_head list; struct list_head list;
@ -50,17 +50,17 @@ int net_submit(const char *hostname, const char *pluginname, const char *filenam
return 0; return 0;
if (fwd_buf == NULL) { if (fwd_buf == NULL) {
fwd_buf = malloc(BUFSIZE); fwd_buf = malloc(PKTSIZE);
if (fwd_buf == NULL) { if (fwd_buf == NULL) {
log_print(LOG_ERROR, "net_submit(): out of memory"); log_print(LOG_ERROR, "net_submit(): out of memory");
return -1; return -1;
} }
} }
int size = snprintf(fwd_buf + fwd_buf_len, BUFSIZE - fwd_buf_len, "%s:%s:%s:%d %s\n", int size = snprintf(fwd_buf + fwd_buf_len, PKTSIZE - fwd_buf_len, "%s:%s:%s:%d %s\n",
hostname, pluginname, filename, ds_id, data); hostname, pluginname, filename, ds_id, data);
if (size < 0 || size >= BUFSIZE - fwd_buf_len) { if (size < 0 || size >= PKTSIZE - fwd_buf_len) {
/* the complete buffer is already full */ /* the complete buffer is already full */
if (fwd_buf_len == 0) { if (fwd_buf_len == 0) {
log_print(LOG_ERROR, "net_submit(): arguments too long"); log_print(LOG_ERROR, "net_submit(): arguments too long");
@ -97,31 +97,29 @@ static int net_receive(int socket, void *privdata)
return 0; return 0;
} }
char *delim = memchr(buf, '\n', size); char *delim;
if (delim == NULL) { int pos = 0;
log_print(LOG_WARN, "net_receive(): invalid data"); while ((delim = memchr(buf + pos, '\n', size - pos)) != NULL) {
free(buf); *delim = '\0';
return 0;
}
*delim = '\0';
char *data[2]; char *data[2];
int ret = strsplit(buf, " ", data, 2); int ret = strsplit(buf + pos, " ", data, 2);
if (ret != 2) { pos = (delim - buf) +1;
log_print(LOG_ERROR, "net_receive(): abort data-split"); if (ret != 2) {
free(buf); log_print(LOG_ERROR, "net_receive(): abort data-split");
return 0; continue;
}
char *part[4];
ret = strsplit(data[0], ":", part, 4);
if (ret != 4) {
log_print(LOG_ERROR, "net_receive(): abort header-split");
continue;
}
rrd_submit(part[0], part[1], part[2], atoi(part[3]), data[1]);
} }
char *part[4];
ret = strsplit(data[0], ":", part, 4);
if (ret != 4) {
log_print(LOG_ERROR, "net_receive(): abort header-split");
free(buf);
return 0;
}
rrd_submit(part[0], part[1], part[2], atoi(part[3]), data[1]);
free(buf); free(buf);
return 0; return 0;
} }

View File

@ -28,6 +28,8 @@
#include "plugins.h" #include "plugins.h"
#include "probe.h" #include "probe.h"
#define MAXFSNAME 16
struct sammler_plugin plugin; struct sammler_plugin plugin;
static const char *ds_def = { static const char *ds_def = {
@ -40,42 +42,69 @@ static const char * get_ds(int ds_id)
return ds_def; return ds_def;
} }
static char * get_valid_fs(int *xcnt)
{
FILE *fp = fopen("/proc/filesystems", "r");
if (fp == NULL)
return NULL;
int cnt = 0;
char buffer[64];
while (fgets(buffer, sizeof(buffer), fp) != NULL) {
if (!strncmp(buffer, "nodev", 5))
continue;
cnt++;
}
char *valid_arr = malloc(cnt * MAXFSNAME);
if (valid_arr == NULL)
return NULL;
rewind(fp);
int i = 0;
while (fgets(buffer, sizeof(buffer), fp) != NULL) {
if (!strncmp(buffer, "nodev", 5))
continue;
char *end = memccpy(valid_arr + (i++ * MAXFSNAME), buffer +1, '\n', MAXFSNAME);
*(end -1) = '\0';
}
fclose(fp);
*xcnt = cnt;
return valid_arr;
}
static int probe(void) static int probe(void)
{ {
FILE *fp; FILE *fp = setmntent("/etc/mtab", "r");
struct mntent *mnt;
struct statfs fs;
char *slash, filename[64];
int len;
fp = setmntent("/etc/mtab", "r");
if (fp == NULL) { if (fp == NULL) {
log_print(LOG_WARN, "plugin mount"); log_print(LOG_WARN, "plugin mount");
return -1; return -1;
} }
int cnt;
char *valid_arr = get_valid_fs(&cnt);
if (valid_arr == NULL) {
log_print(LOG_WARN, "plugin mount");
endmntent(fp);
return -1;
}
struct mntent *mnt;
while ((mnt = getmntent(fp)) != NULL) { while ((mnt = getmntent(fp)) != NULL) {
if (!strcmp(mnt->mnt_fsname, "none"))
continue; int i, valid = 0;
for (i = 0; i < cnt; i++)
if (!strcmp(mnt->mnt_fsname, "proc")) if (strncmp(mnt->mnt_type, valid_arr + (i * MAXFSNAME), MAXFSNAME) == 0)
continue; valid = 1;
if (!strcmp(mnt->mnt_fsname, "sysfs")) if (valid == 0)
continue;
if (!strcmp(mnt->mnt_fsname, "udev"))
continue;
if (!strcmp(mnt->mnt_fsname, "devpts"))
continue;
if (!strcmp(mnt->mnt_type, "nfs"))
continue;
if (!strcmp(mnt->mnt_type, "tmpfs"))
continue; continue;
struct statfs fs;
if (statfs(mnt->mnt_dir, &fs) == -1) { if (statfs(mnt->mnt_dir, &fs) == -1) {
log_print(LOG_WARN, "plugin mount: statfs(%s)", mnt->mnt_dir); log_print(LOG_WARN, "plugin mount: statfs(%s)", mnt->mnt_dir);
continue; continue;
@ -84,13 +113,14 @@ static int probe(void)
if (fs.f_blocks == 0) if (fs.f_blocks == 0)
continue; continue;
slash = mnt->mnt_fsname; char *slash = mnt->mnt_fsname;
while (slash && (slash = strchr(slash, '/'))) { while (slash && (slash = strchr(slash, '/'))) {
slash = strchr(slash, '/'); slash = strchr(slash, '/');
*slash++ = '_'; *slash++ = '_';
} }
len = snprintf(filename, sizeof(filename), "mount%s.rrd", mnt->mnt_fsname); char filename[64];
int len = snprintf(filename, sizeof(filename), "mount%s.rrd", mnt->mnt_fsname);
if (len < 0 || len >= sizeof(filename)) { if (len < 0 || len >= sizeof(filename)) {
log_print(LOG_WARN, "plugin mount: file name too long", mnt->mnt_fsname); log_print(LOG_WARN, "plugin mount: file name too long", mnt->mnt_fsname);
continue; continue;
@ -100,6 +130,8 @@ static int probe(void)
fs.f_blocks * (fs.f_bsize /1024), fs.f_blocks * (fs.f_bsize /1024),
fs.f_bfree * (fs.f_bsize /1024)); fs.f_bfree * (fs.f_bsize /1024));
} }
free(valid_arr);
endmntent(fp); endmntent(fp);
return 0; return 0;
} }

View File

@ -17,7 +17,7 @@ static int submit_flags;
int probe_init(void) int probe_init(void)
{ {
const char *fwd_only = config_get_string("global", "forward_only", "false"); const char *fwd_only = config_get_string("global", "forward_only", "false");
if (fwd_only == NULL || strncmp(fwd_only, "true", 4)) if (!strncmp(fwd_only, "true", 4))
submit_flags |= SUBMIT_NET_ONLY; submit_flags |= SUBMIT_NET_ONLY;
return 0; return 0;

View File

@ -132,7 +132,7 @@ static int rrd_update_file(const char *filename, const char *values)
return -1; return -1;
} }
int pos = snprintf(buffer, ARGVSIZE, "update %s %lu:%s", filename, time(NULL), values); int pos = snprintf(buffer, ARGVSIZE, "update %s N:%s", filename, values);
if (pos < 0 || pos >= ARGVSIZE) { if (pos < 0 || pos >= ARGVSIZE) {
log_print(LOG_ERROR, "rrd_update_file: arguments too long"); log_print(LOG_ERROR, "rrd_update_file: arguments too long");
free(buffer); free(buffer);