remove plugin scratchpad memory

This commit is contained in:
Olaf Rempel 2007-04-01 15:23:09 +02:00
parent b71a7cfe04
commit 4ff80b5e24
8 changed files with 177 additions and 88 deletions

View File

@ -8,15 +8,15 @@
struct sammler_plugin { struct sammler_plugin {
struct list_head list; struct list_head list;
void *dlhandle;
const char *name; const char *name;
/* timing */ /* timing */
unsigned int interval; unsigned int interval;
time_t lastprobe; time_t lastprobe;
/* scratchpad memory */ /* flags */
int bufsize; int flags;
char *buffer;
int (*init) (void); int (*init) (void);
int (*fini) (void); int (*fini) (void);

View File

@ -31,58 +31,56 @@
#include "network.h" #include "network.h"
#include "plugins.h" #include "plugins.h"
static LIST_HEAD(plugin_list); #define FLAGS_ACTIVE 0x01
static char *scratchpad; static LIST_HEAD(plugin_list);
static int plugin_init_cb(const char *filename, void *privdata) static int plugin_init_cb(const char *filename, void *privdata)
{ {
int *bufsize = (int *)privdata;
static const char *plugin_dir; static const char *plugin_dir;
if (plugin_dir == NULL) if (plugin_dir == NULL)
plugin_dir = config_get_string("global", "plugin_dir", "."); plugin_dir = config_get_string("global", "plugin_dir", ".");
char *buffer = malloc(PATH_MAX); char *fullname = malloc(PATH_MAX);
if (buffer == NULL) { if (fullname == NULL) {
log_print(LOG_ERROR, "plugin_load: out of memory"); log_print(LOG_ERROR, "plugin_load: out of memory");
return -1; return -1;
} }
int len = snprintf(buffer, PATH_MAX, "%s/%s", plugin_dir, filename); int len = snprintf(fullname, PATH_MAX, "%s/%s", plugin_dir, filename);
if (len < 0 || len >= PATH_MAX) { if (len < 0 || len >= PATH_MAX) {
log_print(LOG_ERROR, "plugin_load: file name too long: %s/%s", plugin_dir, filename); log_print(LOG_ERROR, "plugin_load: file name too long: %s/%s", plugin_dir, filename);
free(buffer); free(fullname);
return -1; return -1;
} }
dlerror(); dlerror();
void *tmp = dlopen(buffer, RTLD_NOW); void *dlhandle = dlopen(fullname, RTLD_NOW);
if (tmp == NULL) { if (dlhandle == NULL) {
log_print(LOG_ERROR, "plugin_load: dlopen: %s", dlerror()); log_print(LOG_ERROR, "plugin_load: dlopen: %s", dlerror());
free(buffer); free(fullname);
return -1; return -1;
} }
free(buffer); free(fullname);
struct sammler_plugin *plugin = dlsym(tmp, "plugin"); struct sammler_plugin *plugin = dlsym(dlhandle, "plugin");
if (plugin == NULL) { if (plugin == NULL) {
log_print(LOG_ERROR, "plugin_load: failed to load '%s'", filename); log_print(LOG_ERROR, "plugin_load: failed to load '%s'", filename);
dlclose(tmp); dlclose(dlhandle);
return -1;
}
if (plugin->init != NULL && (plugin->init() != 0)) {
log_print(LOG_ERROR, "Plugin '%s': init failed", plugin->name);
dlclose(dlhandle);
return -1; return -1;
} }
log_print(LOG_INFO, "Plugin '%s' loaded", plugin->name); log_print(LOG_INFO, "Plugin '%s' loaded", plugin->name);
plugin->lastprobe = 0; plugin->lastprobe = 0;
plugin->flags = FLAGS_ACTIVE;
if (plugin->init != NULL && (plugin->init() != 0)) { plugin->dlhandle = dlhandle;
log_print(LOG_ERROR, "Plugin '%s': init failed", plugin->name);
return -1;
}
if (plugin->bufsize > *bufsize)
*bufsize = plugin->bufsize;
list_add_tail(&plugin->list, &plugin_list); list_add_tail(&plugin->list, &plugin_list);
return 0; return 0;
@ -95,8 +93,14 @@ static int plugins_probe(void *privdata)
struct sammler_plugin *plugin; struct sammler_plugin *plugin;
list_for_each_entry(plugin, &plugin_list, list) { list_for_each_entry(plugin, &plugin_list, list) {
if (!(plugin->flags & FLAGS_ACTIVE))
continue;
if (plugin->lastprobe + plugin->interval <= now) { if (plugin->lastprobe + plugin->interval <= now) {
plugin->probe(); if (plugin->probe() != 0) {
log_print(LOG_ERROR, "plugin_probe(): plugin %s disabled", plugin->name);
plugin->flags &= ~FLAGS_ACTIVE;
}
plugin->lastprobe = now; plugin->lastprobe = now;
} }
} }
@ -107,23 +111,12 @@ static int plugins_probe(void *privdata)
int plugin_init(void) int plugin_init(void)
{ {
int bufsize = 0; int cnt = config_get_strings("global", "plugin", plugin_init_cb, NULL);
int cnt = config_get_strings("global", "plugin", plugin_init_cb, &bufsize);
if (cnt == 0) { if (cnt == 0) {
log_print(LOG_ERROR, "plugin_init(): no working plugins"); log_print(LOG_ERROR, "plugin_init(): no working plugins");
return -1; return -1;
} }
scratchpad = malloc(bufsize);
if (scratchpad == NULL) {
log_print(LOG_ERROR, "plugin_init(): out of memory");
return -1;
}
struct sammler_plugin *plugin;
list_for_each_entry(plugin, &plugin_list, list)
plugin->buffer = scratchpad;
struct timeval tv; struct timeval tv;
tv.tv_sec = 1; tv.tv_sec = 1;
tv.tv_usec = 0; tv.tv_usec = 0;

View File

@ -25,7 +25,10 @@
#include "plugins.h" #include "plugins.h"
#include "probe.h" #include "probe.h"
#define BUFSIZE 1024
struct sammler_plugin plugin; struct sammler_plugin plugin;
static char *buffer;
static const char *ds_def = { static const char *ds_def = {
"DS:entries:GAUGE:15:0:U " "DS:entries:GAUGE:15:0:U "
@ -64,11 +67,11 @@ static int probe(void)
return -1; return -1;
} }
while (fgets(plugin.buffer, plugin.bufsize, fp) != NULL) { while (fgets(buffer, BUFSIZE, fp) != NULL) {
if (!strncmp(plugin.buffer, "entries", 7)) if (!strncmp(buffer, "entries", 7))
continue; continue;
if (strsplit(plugin.buffer, " \t\n", val, 16) != 16) if (strsplit(buffer, " \t\n", val, 16) != 16)
continue; continue;
for (i = 0; i < 16; i++) for (i = 0; i < 16; i++)
@ -91,10 +94,23 @@ static int probe(void)
return 0; return 0;
} }
static int init(void)
{
buffer = malloc(BUFSIZE);
return (buffer == NULL) ? -1 : 0;
}
static int fini(void)
{
free(buffer);
return 0;
}
struct sammler_plugin plugin = { struct sammler_plugin plugin = {
.name = "ctstat", .name = "ctstat",
.interval = 10, .interval = 10,
.bufsize = 1024, .init = &init,
.fini = &fini,
.probe = &probe, .probe = &probe,
.get_ds = &get_ds, .get_ds = &get_ds,
}; };

View File

@ -27,7 +27,10 @@
#define DS_MEMORY 1 #define DS_MEMORY 1
#define DS_SWAP 2 #define DS_SWAP 2
#define BUFSIZE 1024
struct sammler_plugin plugin; struct sammler_plugin plugin;
static char *buffer;
static const char *mem_ds_def = { static const char *mem_ds_def = {
"DS:total:GAUGE:15:0:U " "DS:total:GAUGE:15:0:U "
@ -77,24 +80,24 @@ static int probe(void)
return -1; return -1;
} }
while (fgets(plugin.buffer, plugin.bufsize, fp) != NULL) { while (fgets(buffer, BUFSIZE, fp) != NULL) {
if (!strncmp(plugin.buffer, "MemTotal:", 9)) if (!strncmp(buffer, "MemTotal:", 9))
meminfo.memtotal = atoll(plugin.buffer + 10); meminfo.memtotal = atoll(buffer + 10);
else if (!strncmp(plugin.buffer, "MemFree:", 8)) else if (!strncmp(buffer, "MemFree:", 8))
meminfo.memfree = atoll(plugin.buffer + 9); meminfo.memfree = atoll(buffer + 9);
else if (!strncmp(plugin.buffer, "Buffers:", 8)) else if (!strncmp(buffer, "Buffers:", 8))
meminfo.buffers = atoll(plugin.buffer + 9); meminfo.buffers = atoll(buffer + 9);
else if (!strncmp(plugin.buffer, "Cached:", 7)) else if (!strncmp(buffer, "Cached:", 7))
meminfo.cached = atoll(plugin.buffer + 8); meminfo.cached = atoll(buffer + 8);
else if (!strncmp(plugin.buffer, "SwapTotal:", 10)) else if (!strncmp(buffer, "SwapTotal:", 10))
meminfo.swaptotal = atoll(plugin.buffer + 11); meminfo.swaptotal = atoll(buffer + 11);
else if (!strncmp(plugin.buffer, "SwapFree:", 9)) else if (!strncmp(buffer, "SwapFree:", 9))
meminfo.swapfree = atoll(plugin.buffer + 10); meminfo.swapfree = atoll(buffer + 10);
} }
probe_submit(&plugin, "memory.rrd", DS_MEMORY, "%llu:%llu:%llu:%llu", probe_submit(&plugin, "memory.rrd", DS_MEMORY, "%llu:%llu:%llu:%llu",
@ -108,10 +111,23 @@ static int probe(void)
return 0; return 0;
} }
static int init(void)
{
buffer = malloc(BUFSIZE);
return (buffer == NULL) ? -1 : 0;
}
static int fini(void)
{
free(buffer);
return 0;
}
struct sammler_plugin plugin = { struct sammler_plugin plugin = {
.name = "memory", .name = "memory",
.interval = 10, .interval = 10,
.bufsize = 1024, .init = &init,
.fini = &fini,
.probe = &probe, .probe = &probe,
.get_ds = &get_ds, .get_ds = &get_ds,
}; };

View File

@ -25,7 +25,10 @@
#include "plugins.h" #include "plugins.h"
#include "probe.h" #include "probe.h"
#define BUFSIZE 1024
struct sammler_plugin plugin; struct sammler_plugin plugin;
static char *buffer;
static const char *ds_def = { static const char *ds_def = {
"DS:byte_in:COUNTER:15:0:U " "DS:byte_in:COUNTER:15:0:U "
@ -52,14 +55,14 @@ static int probe(void)
return -1; return -1;
} }
while (fgets(plugin.buffer, plugin.bufsize, fp) != NULL) { while (fgets(buffer, BUFSIZE, fp) != NULL) {
if (!(stats = strchr(plugin.buffer, ':'))) if (!(stats = strchr(buffer, ':')))
continue; continue;
*stats++ = '\0'; *stats++ = '\0';
device = plugin.buffer; device = buffer;
while (*device == ' ') while (*device == ' ')
device++; device++;
@ -80,10 +83,23 @@ static int probe(void)
return 0; return 0;
} }
static int init(void)
{
buffer = malloc(BUFSIZE);
return (buffer == NULL) ? -1 : 0;
}
static int fini(void)
{
free(buffer);
return 0;
}
struct sammler_plugin plugin = { struct sammler_plugin plugin = {
.name = "netdev", .name = "netdev",
.interval = 10, .interval = 10,
.bufsize = 1024, .init = &init,
.fini = &fini,
.probe = &probe, .probe = &probe,
.get_ds = &get_ds, .get_ds = &get_ds,
}; };

View File

@ -28,7 +28,10 @@
#define DS_STAT 1 #define DS_STAT 1
#define DS_GC 2 #define DS_GC 2
#define BUFSIZE 1024
struct sammler_plugin plugin; struct sammler_plugin plugin;
static char *buffer;
static const char *ds_def_stat = { static const char *ds_def_stat = {
"DS:in_hit:DERIVE:15:0:U " "DS:in_hit:DERIVE:15:0:U "
@ -80,11 +83,11 @@ static int probe(void)
return -1; return -1;
} }
while (fgets(plugin.buffer, plugin.bufsize, fp) != NULL) { while (fgets(buffer, BUFSIZE, fp) != NULL) {
if (!strncmp(plugin.buffer, "entries", 7)) if (!strncmp(buffer, "entries", 7))
continue; continue;
if (strsplit(plugin.buffer, " \t\n", val, 17) != 17) if (strsplit(buffer, " \t\n", val, 17) != 17)
continue; continue;
for (i = 0; i < 17; i++) for (i = 0; i < 17; i++)
@ -114,10 +117,23 @@ static int probe(void)
return 0; return 0;
} }
static int init(void)
{
buffer = malloc(BUFSIZE);
return (buffer == NULL) ? -1 : 0;
}
static int fini(void)
{
free(buffer);
return 0;
}
struct sammler_plugin plugin = { struct sammler_plugin plugin = {
.name = "rtstat", .name = "rtstat",
.interval = 10, .interval = 10,
.bufsize = 1024, .init = &init,
.fini = &fini,
.probe = &probe, .probe = &probe,
.get_ds = &get_ds, .get_ds = &get_ds,
}; };

View File

@ -28,7 +28,10 @@
#define DS_CPU 1 #define DS_CPU 1
#define DS_PROC 2 #define DS_PROC 2
#define BUFSIZE 1024
struct sammler_plugin plugin; struct sammler_plugin plugin;
static char *buffer;
static const char *cpu_ds_def = { static const char *cpu_ds_def = {
"DS:user:COUNTER:15:0:U " "DS:user:COUNTER:15:0:U "
@ -80,13 +83,13 @@ static int probe(void)
return -1; return -1;
} }
while (fgets(plugin.buffer, plugin.bufsize, fp) != NULL) { while (fgets(buffer, BUFSIZE, fp) != NULL) {
if (!strncmp(plugin.buffer, "cpu", 3)) { if (!strncmp(buffer, "cpu", 3)) {
char *val[9], filename[16]; char *val[9], filename[16];
int numfields, len, cpu; int numfields, len, cpu;
if ((plugin.buffer[3] >= '0') && (plugin.buffer[3] <= '9')) { if ((buffer[3] >= '0') && (buffer[3] <= '9')) {
cpu = atoi(plugin.buffer +3); cpu = atoi(buffer +3);
len = snprintf(filename, sizeof(filename), "cpu-%d.rrd", cpu); len = snprintf(filename, sizeof(filename), "cpu-%d.rrd", cpu);
if (len < 0 || len >= sizeof(filename)) if (len < 0 || len >= sizeof(filename))
continue; continue;
@ -95,7 +98,7 @@ static int probe(void)
strncpy(filename, "cpu.rrd", sizeof(filename)); strncpy(filename, "cpu.rrd", sizeof(filename));
} }
numfields = strsplit(plugin.buffer, " \t\n", val, 9); numfields = strsplit(buffer, " \t\n", val, 9);
if (numfields < 5) if (numfields < 5)
continue; continue;
@ -110,14 +113,14 @@ static int probe(void)
val[1], val[2], val[3], val[4], val[1], val[2], val[3], val[4],
val[5], val[6], val[7], val[8]); val[5], val[6], val[7], val[8]);
} else if (!strncmp(plugin.buffer, "intr", 4)) { } else if (!strncmp(buffer, "intr", 4)) {
proc.intr = atoll(plugin.buffer + 5); proc.intr = atoll(buffer + 5);
} else if (!strncmp(plugin.buffer, "ctxt", 4)) { } else if (!strncmp(buffer, "ctxt", 4)) {
proc.ctxt = atoll(plugin.buffer + 5); proc.ctxt = atoll(buffer + 5);
} else if (!strncmp(plugin.buffer, "processes", 9)) { } else if (!strncmp(buffer, "processes", 9)) {
proc.fork = atoll(plugin.buffer + 10); proc.fork = atoll(buffer + 10);
} }
} }
@ -128,10 +131,23 @@ static int probe(void)
return 0; return 0;
} }
static int init(void)
{
buffer = malloc(BUFSIZE);
return (buffer == NULL) ? -1 : 0;
}
static int fini(void)
{
free(buffer);
return 0;
}
struct sammler_plugin plugin = { struct sammler_plugin plugin = {
.name = "stat", .name = "stat",
.interval = 10, .interval = 10,
.bufsize = 1024, .init = &init,
.fini = &fini,
.probe = &probe, .probe = &probe,
.get_ds = &get_ds, .get_ds = &get_ds,
}; };

View File

@ -24,7 +24,10 @@
#include "plugins.h" #include "plugins.h"
#include "probe.h" #include "probe.h"
#define BUFSIZE 1024
struct sammler_plugin plugin; struct sammler_plugin plugin;
static char *buffer;
static const char *ds_def = { static const char *ds_def = {
"DS:pgalloc_high:DERIVE:15:0:U " "DS:pgalloc_high:DERIVE:15:0:U "
@ -60,21 +63,21 @@ static int probe(void)
return -1; return -1;
} }
while (fgets(plugin.buffer, plugin.bufsize, fp) != NULL) { while (fgets(buffer, BUFSIZE, fp) != NULL) {
if (!strncmp(plugin.buffer, "pgalloc_high", 12)) if (!strncmp(buffer, "pgalloc_high", 12))
vmstat.pgalloc_high = atoll(plugin.buffer + 13); vmstat.pgalloc_high = atoll(buffer + 13);
else if (!strncmp(plugin.buffer, "pgalloc_normal", 14)) else if (!strncmp(buffer, "pgalloc_normal", 14))
vmstat.pgalloc_normal = atoll(plugin.buffer + 15); vmstat.pgalloc_normal = atoll(buffer + 15);
else if (!strncmp(plugin.buffer, "pgalloc_dma", 11)) else if (!strncmp(buffer, "pgalloc_dma", 11))
vmstat.pgalloc_dma = atoll(plugin.buffer + 12); vmstat.pgalloc_dma = atoll(buffer + 12);
else if (!strncmp(plugin.buffer, "pgfree", 6)) else if (!strncmp(buffer, "pgfree", 6))
vmstat.pgfree = atoll(plugin.buffer + 7); vmstat.pgfree = atoll(buffer + 7);
else if (!strncmp(plugin.buffer, "pgfault", 7)) else if (!strncmp(buffer, "pgfault", 7))
vmstat.pgfault = atoll(plugin.buffer + 8); vmstat.pgfault = atoll(buffer + 8);
} }
probe_submit(&plugin, "vmstat.rrd", 0, "%llu:%llu:%llu:%llu:%llu", probe_submit(&plugin, "vmstat.rrd", 0, "%llu:%llu:%llu:%llu:%llu",
@ -85,10 +88,23 @@ static int probe(void)
return 0; return 0;
} }
static int init(void)
{
buffer = malloc(BUFSIZE);
return (buffer == NULL) ? -1 : 0;
}
static int fini(void)
{
free(buffer);
return 0;
}
struct sammler_plugin plugin = { struct sammler_plugin plugin = {
.name = "vmstat", .name = "vmstat",
.interval = 10, .interval = 10,
.bufsize = 1024, .init = &init,
.fini = &fini,
.probe = &probe, .probe = &probe,
.get_ds = &get_ds, .get_ds = &get_ds,
}; };