global plugin scratchpad memory

This commit is contained in:
Olaf Rempel 2007-03-31 23:21:31 +02:00
parent 253a79584e
commit b94e64cd45
11 changed files with 90 additions and 187 deletions

View File

@ -58,4 +58,4 @@ p_mysql.so: p_mysql_sh.o p_mysql_helper_sh.o
clean: clean:
rm -rf *.d *.o *.so sammler rm -rf *.d *.o *.so sammler
#-include $(SAMMLER_SRC:.c=.d) $(PLUGIN_SRC:.c=.d) -include $(SAMMLER_SRC:.c=.d) $(PLUGIN_SRC:.c=.d)

View File

@ -31,6 +31,8 @@
#define BUFSIZE 8192 #define BUFSIZE 8192
struct sammler_plugin plugin;
struct server_entry { struct server_entry {
struct list_head list; struct list_head list;
CURL *handle; CURL *handle;
@ -53,8 +55,6 @@ struct stats {
uint64_t idle_workers; uint64_t idle_workers;
}; };
struct sammler_plugin plugin;
static char *rx_buf; static char *rx_buf;
static int rx_pos; static int rx_pos;

View File

@ -24,8 +24,6 @@
#include "helper.h" #include "helper.h"
#include "plugins.h" #include "plugins.h"
#define BUFSIZE 1024
struct sammler_plugin plugin; struct sammler_plugin plugin;
static const char *ds_def = { static const char *ds_def = {
@ -52,8 +50,6 @@ static const char * get_ds(int ds_id)
return ds_def; return ds_def;
} }
static char *buffer;
static int probe(void) static int probe(void)
{ {
FILE *fp; FILE *fp;
@ -67,11 +63,11 @@ static int probe(void)
return -1; return -1;
} }
while (fgets(buffer, BUFSIZE, fp) != NULL) { while (fgets(plugin.buffer, plugin.bufsize, fp) != NULL) {
if (!strncmp(buffer, "entries", 7)) if (!strncmp(plugin.buffer, "entries", 7))
continue; continue;
if (strsplit(buffer, " \t\n", val, 16) != 16) if (strsplit(plugin.buffer, " \t\n", val, 16) != 16)
continue; continue;
for (i = 0; i < 16; i++) for (i = 0; i < 16; i++)
@ -94,27 +90,10 @@ static int probe(void)
return 0; return 0;
} }
static int init(void)
{
buffer = malloc(BUFSIZE);
if (buffer == NULL) {
log_print(LOG_WARN, "plugin ctstat: out of memory");
return -1;
}
return 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,
.init = &init, .bufsize = 1024,
.fini = &fini,
.probe = &probe, .probe = &probe,
.get_ds = &get_ds, .get_ds = &get_ds,
}; };

View File

@ -23,8 +23,6 @@
#include "plugins.h" #include "plugins.h"
#define BUFSIZE 1024
#define DS_MEMORY 1 #define DS_MEMORY 1
#define DS_SWAP 2 #define DS_SWAP 2
@ -65,8 +63,6 @@ struct meminfo_ {
unsigned long long swapfree; unsigned long long swapfree;
}; };
static char *buffer;
static int probe(void) static int probe(void)
{ {
FILE *fp; FILE *fp;
@ -80,24 +76,24 @@ static int probe(void)
return -1; return -1;
} }
while (fgets(buffer, BUFSIZE, fp) != NULL) { while (fgets(plugin.buffer, plugin.bufsize, fp) != NULL) {
if (!strncmp(buffer, "MemTotal:", 9)) if (!strncmp(plugin.buffer, "MemTotal:", 9))
meminfo.memtotal = atoll(buffer + 10); meminfo.memtotal = atoll(plugin.buffer + 10);
else if (!strncmp(buffer, "MemFree:", 8)) else if (!strncmp(plugin.buffer, "MemFree:", 8))
meminfo.memfree = atoll(buffer + 9); meminfo.memfree = atoll(plugin.buffer + 9);
else if (!strncmp(buffer, "Buffers:", 8)) else if (!strncmp(plugin.buffer, "Buffers:", 8))
meminfo.buffers = atoll(buffer + 9); meminfo.buffers = atoll(plugin.buffer + 9);
else if (!strncmp(buffer, "Cached:", 7)) else if (!strncmp(plugin.buffer, "Cached:", 7))
meminfo.cached = atoll(buffer + 8); meminfo.cached = atoll(plugin.buffer + 8);
else if (!strncmp(buffer, "SwapTotal:", 10)) else if (!strncmp(plugin.buffer, "SwapTotal:", 10))
meminfo.swaptotal = atoll(buffer + 11); meminfo.swaptotal = atoll(plugin.buffer + 11);
else if (!strncmp(buffer, "SwapFree:", 9)) else if (!strncmp(plugin.buffer, "SwapFree:", 9))
meminfo.swapfree = atoll(buffer + 10); meminfo.swapfree = atoll(plugin.buffer + 10);
} }
probe_submit(&plugin, "memory.rrd", DS_MEMORY, "%llu:%llu:%llu:%llu", probe_submit(&plugin, "memory.rrd", DS_MEMORY, "%llu:%llu:%llu:%llu",
@ -111,27 +107,10 @@ static int probe(void)
return 0; return 0;
} }
static int init(void)
{
buffer = malloc(BUFSIZE);
if (buffer == NULL) {
log_print(LOG_WARN, "plugin memory: out of memory");
return -1;
}
return 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,
.init = &init, .bufsize = 1024,
.fini = &fini,
.probe = &probe, .probe = &probe,
.get_ds = &get_ds, .get_ds = &get_ds,
}; };

View File

@ -33,6 +33,8 @@
#define DS_QCACHE 3 #define DS_QCACHE 3
#define DS_THREADS 4 #define DS_THREADS 4
struct sammler_plugin plugin;
struct server_entry { struct server_entry {
struct list_head list; struct list_head list;
void *mysql; void *mysql;
@ -93,8 +95,6 @@ static const char * get_ds(int ds_id)
} }
} }
struct sammler_plugin plugin;
static int probe(void) static int probe(void)
{ {
struct mysql_stats stats; struct mysql_stats stats;

View File

@ -24,8 +24,6 @@
#include "helper.h" #include "helper.h"
#include "plugins.h" #include "plugins.h"
#define BUFSIZE 1024
struct sammler_plugin plugin; struct sammler_plugin plugin;
static const char *ds_def = { static const char *ds_def = {
@ -40,8 +38,6 @@ static const char * get_ds(int ds_id)
return ds_def; return ds_def;
} }
static char *buffer;
static int probe(void) static int probe(void)
{ {
FILE *fp; FILE *fp;
@ -55,14 +51,14 @@ static int probe(void)
return -1; return -1;
} }
while (fgets(buffer, BUFSIZE, fp) != NULL) { while (fgets(plugin.buffer, plugin.bufsize, fp) != NULL) {
if (!(stats = strchr(buffer, ':'))) if (!(stats = strchr(plugin.buffer, ':')))
continue; continue;
*stats++ = '\0'; *stats++ = '\0';
device = buffer; device = plugin.buffer;
while (*device == ' ') while (*device == ' ')
device++; device++;
@ -83,27 +79,10 @@ static int probe(void)
return 0; return 0;
} }
static int init(void)
{
buffer = malloc(BUFSIZE);
if (buffer == NULL) {
log_print(LOG_WARN, "plugin netdev: out of memory");
return -1;
}
return 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,
.init = &init, .bufsize = 1024,
.fini = &fini,
.probe = &probe, .probe = &probe,
.get_ds = &get_ds, .get_ds = &get_ds,
}; };

View File

@ -24,8 +24,6 @@
#include "helper.h" #include "helper.h"
#include "plugins.h" #include "plugins.h"
#define BUFSIZE 1024
#define DS_STAT 1 #define DS_STAT 1
#define DS_GC 2 #define DS_GC 2
@ -68,8 +66,6 @@ static const char * get_ds(int ds_id)
} }
} }
static char *buffer;
static int probe(void) static int probe(void)
{ {
FILE *fp; FILE *fp;
@ -83,11 +79,11 @@ static int probe(void)
return -1; return -1;
} }
while (fgets(buffer, BUFSIZE, fp) != NULL) { while (fgets(plugin.buffer, plugin.bufsize, fp) != NULL) {
if (!strncmp(buffer, "entries", 7)) if (!strncmp(plugin.buffer, "entries", 7))
continue; continue;
if (strsplit(buffer, " \t\n", val, 17) != 17) if (strsplit(plugin.buffer, " \t\n", val, 17) != 17)
continue; continue;
for (i = 0; i < 17; i++) for (i = 0; i < 17; i++)
@ -117,27 +113,10 @@ static int probe(void)
return 0; return 0;
} }
static int init(void)
{
buffer = malloc(BUFSIZE);
if (buffer == NULL) {
log_print(LOG_WARN, "plugin rtstat: out of memory");
return -1;
}
return 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,
.init = &init, .bufsize = 1024,
.fini = &fini,
.probe = &probe, .probe = &probe,
.get_ds = &get_ds, .get_ds = &get_ds,
}; };

View File

@ -24,8 +24,6 @@
#include "helper.h" #include "helper.h"
#include "plugins.h" #include "plugins.h"
#define BUFSIZE 1024
#define DS_CPU 1 #define DS_CPU 1
#define DS_PROC 2 #define DS_PROC 2
@ -68,8 +66,6 @@ struct proc_ {
unsigned long long fork; unsigned long long fork;
}; };
static char *buffer;
static int probe(void) static int probe(void)
{ {
FILE *fp; FILE *fp;
@ -83,13 +79,13 @@ static int probe(void)
return -1; return -1;
} }
while (fgets(buffer, BUFSIZE, fp) != NULL) { while (fgets(plugin.buffer, plugin.bufsize, fp) != NULL) {
if (!strncmp(buffer, "cpu", 3)) { if (!strncmp(plugin.buffer, "cpu", 3)) {
char *val[9], filename[16]; char *val[9], filename[16];
int numfields, len, cpu; int numfields, len, cpu;
if ((buffer[3] >= '0') && (buffer[3] <= '9')) { if ((plugin.buffer[3] >= '0') && (plugin.buffer[3] <= '9')) {
cpu = atoi(buffer +3); cpu = atoi(plugin.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;
@ -98,7 +94,7 @@ static int probe(void)
strncpy(filename, "cpu.rrd", sizeof(filename)); strncpy(filename, "cpu.rrd", sizeof(filename));
} }
numfields = strsplit(buffer, " \t\n", val, 9); numfields = strsplit(plugin.buffer, " \t\n", val, 9);
if (numfields < 5) if (numfields < 5)
continue; continue;
@ -113,14 +109,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(buffer, "intr", 4)) { } else if (!strncmp(plugin.buffer, "intr", 4)) {
proc.intr = atoll(buffer + 5); proc.intr = atoll(plugin.buffer + 5);
} else if (!strncmp(buffer, "ctxt", 4)) { } else if (!strncmp(plugin.buffer, "ctxt", 4)) {
proc.ctxt = atoll(buffer + 5); proc.ctxt = atoll(plugin.buffer + 5);
} else if (!strncmp(buffer, "processes", 9)) { } else if (!strncmp(plugin.buffer, "processes", 9)) {
proc.fork = atoll(buffer + 10); proc.fork = atoll(plugin.buffer + 10);
} }
} }
@ -131,27 +127,10 @@ static int probe(void)
return 0; return 0;
} }
static int init(void)
{
buffer = malloc(BUFSIZE);
if (buffer == NULL) {
log_print(LOG_WARN, "plugin stat: out of memory");
return -1;
}
return 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,
.init = &init, .bufsize = 1024,
.fini = &fini,
.probe = &probe, .probe = &probe,
.get_ds = &get_ds, .get_ds = &get_ds,
}; };

View File

@ -23,8 +23,6 @@
#include "plugins.h" #include "plugins.h"
#define BUFSIZE 1024
struct sammler_plugin plugin; struct sammler_plugin plugin;
static const char *ds_def = { static const char *ds_def = {
@ -48,8 +46,6 @@ struct vmstat_ {
unsigned long long pgfault; unsigned long long pgfault;
}; };
static char *buffer;
static int probe(void) static int probe(void)
{ {
FILE *fp; FILE *fp;
@ -63,21 +59,21 @@ static int probe(void)
return -1; return -1;
} }
while (fgets(buffer, BUFSIZE, fp) != NULL) { while (fgets(plugin.buffer, plugin.bufsize, fp) != NULL) {
if (!strncmp(buffer, "pgalloc_high", 12)) if (!strncmp(plugin.buffer, "pgalloc_high", 12))
vmstat.pgalloc_high = atoll(buffer + 13); vmstat.pgalloc_high = atoll(plugin.buffer + 13);
else if (!strncmp(buffer, "pgalloc_normal", 14)) else if (!strncmp(plugin.buffer, "pgalloc_normal", 14))
vmstat.pgalloc_normal = atoll(buffer + 15); vmstat.pgalloc_normal = atoll(plugin.buffer + 15);
else if (!strncmp(buffer, "pgalloc_dma", 11)) else if (!strncmp(plugin.buffer, "pgalloc_dma", 11))
vmstat.pgalloc_dma = atoll(buffer + 12); vmstat.pgalloc_dma = atoll(plugin.buffer + 12);
else if (!strncmp(buffer, "pgfree", 6)) else if (!strncmp(plugin.buffer, "pgfree", 6))
vmstat.pgfree = atoll(buffer + 7); vmstat.pgfree = atoll(plugin.buffer + 7);
else if (!strncmp(buffer, "pgfault", 7)) else if (!strncmp(plugin.buffer, "pgfault", 7))
vmstat.pgfault = atoll(buffer + 8); vmstat.pgfault = atoll(plugin.buffer + 8);
} }
probe_submit(&plugin, "vmstat.rrd", 0, "%llu:%llu:%llu:%llu:%llu", probe_submit(&plugin, "vmstat.rrd", 0, "%llu:%llu:%llu:%llu:%llu",
@ -88,27 +84,10 @@ static int probe(void)
return 0; return 0;
} }
static int init(void)
{
buffer = malloc(BUFSIZE);
if (buffer == NULL) {
log_print(LOG_WARN, "plugin vmstat: out of memory");
return -1;
}
return 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,
.init = &init, .bufsize = 1024,
.fini = &fini,
.probe = &probe, .probe = &probe,
.get_ds = &get_ds, .get_ds = &get_ds,
}; };

View File

@ -40,8 +40,11 @@ static LIST_HEAD(plugin_list);
static int plugin_flags; static int plugin_flags;
static char *scratchpad;
static int plugin_init_cb(const char *filename, void *privdata) static int plugin_init_cb(const char *filename, void *privdata)
{ {
int *bufsize = (int *)privdata;
struct sammler_plugin *plugin = NULL; struct sammler_plugin *plugin = NULL;
static const char *plugin_dir; static const char *plugin_dir;
@ -90,22 +93,39 @@ static int plugin_init_cb(const char *filename, void *privdata)
return -1; 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;
} }
void plugin_init(int flags) int plugin_init(int flags)
{ {
config_get_strings("global", "plugin", plugin_init_cb, NULL); struct sammler_plugin *plugin;
int bufsize = 0;
config_get_strings("global", "plugin", plugin_init_cb, &bufsize);
plugin_flags = flags; plugin_flags = flags;
scratchpad = malloc(bufsize);
if (scratchpad == NULL) {
log_print(LOG_ERROR, "plugin_init: out of memory");
return -1;
}
list_for_each_entry(plugin, &plugin_list, list)
plugin->buffer = scratchpad;
return 0;
} }
void plugins_probe(void) void plugins_probe(void)
{ {
struct sammler_plugin *plugin; struct sammler_plugin *plugin;
unsigned long now; time_t now;
now = time(NULL); time(&now);
list_for_each_entry(plugin, &plugin_list, list) { list_for_each_entry(plugin, &plugin_list, list) {
if (plugin->lastprobe + plugin->interval <= now) { if (plugin->lastprobe + plugin->interval <= now) {

View File

@ -1,6 +1,8 @@
#ifndef _PLUGINS_H_ #ifndef _PLUGINS_H_
#define _PLUGINS_H_ #define _PLUGINS_H_
#include <time.h>
#include "list.h" #include "list.h"
#include "logging.h" #include "logging.h"
@ -10,15 +12,22 @@
struct sammler_plugin { struct sammler_plugin {
struct list_head list; struct list_head list;
const char *name; const char *name;
/* timing */
unsigned int interval; unsigned int interval;
unsigned long lastprobe; time_t lastprobe;
/* scratchpad memory */
int bufsize;
char *buffer;
int (*init) (void); int (*init) (void);
int (*fini) (void); int (*fini) (void);
int (*probe) (void); int (*probe) (void);
const char * (*get_ds) (int ds_id); const char * (*get_ds) (int ds_id);
}; };
void plugin_init(int flags); int plugin_init(int flags);
void plugins_probe(void); void plugins_probe(void);