plugin init/fini functions

This commit is contained in:
Olaf Rempel 2006-10-07 20:37:30 +02:00
parent 9add656662
commit 273218612e
12 changed files with 169 additions and 78 deletions

View File

@ -51,24 +51,20 @@ static char * get_ds(int ds_id)
return ds_def;
}
static void probe(void)
static char *buffer;
static int probe(void)
{
FILE *fp;
char *buffer, *val[16], filename[16];
char *val[16], filename[16];
unsigned long long arr[16];
int i, len, cpu = 0;
buffer = malloc(BUFSIZE);
if (buffer == NULL) {
log_print(LOG_WARN, "plugin ctstat: out of memory");
return;
}
fp = fopen("/proc/net/stat/ip_conntrack", "r");
if (fp == NULL) {
log_print(LOG_WARN, "plugin ctstat");
free(buffer);
return;
return -1;
}
while (fgets(buffer, BUFSIZE, fp) != NULL) {
@ -95,12 +91,30 @@ static void probe(void)
cpu++;
}
fclose(fp);
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 = {
.name = "ctstat",
.interval = 10,
.init = &init,
.fini = &fini,
.probe = &probe,
.get_ds = &get_ds,
};

View File

@ -35,7 +35,7 @@ static char * get_ds(int ds_id)
return ds_def;
}
static void probe(void)
static int probe(void)
{
FILE *fp;
char buffer[32];
@ -44,21 +44,22 @@ static void probe(void)
fp = fopen("/proc/loadavg", "r");
if (fp == NULL) {
log_print(LOG_WARN, "plugin load");
return;
return -1;
}
if (fgets(buffer, sizeof(buffer), fp) == NULL) {
log_print(LOG_WARN, "plugin load");
fclose(fp);
return;
return -1;
}
fclose(fp);
if (strsplit(buffer, val, 3) != 3)
return;
return -1;
probe_submit(&plugin, "load.rrd", 0, "%s:%s:%s", val[0], val[1], val[2]);
return 0;
}
struct sammler_plugin plugin = {

View File

@ -65,25 +65,20 @@ struct meminfo_ {
unsigned long long swapfree;
};
static void probe(void)
static char *buffer;
static int probe(void)
{
FILE *fp;
char *buffer;
struct meminfo_ meminfo;
memset(&meminfo, 0, sizeof(meminfo));
buffer = malloc(BUFSIZE);
if (buffer == NULL) {
log_print(LOG_WARN, "plugin memory: out of memory");
return;
}
fp = fopen("/proc/meminfo", "r");
if (fp == NULL) {
log_print(LOG_WARN, "plugin memory");
free(buffer);
return;
return -1;
}
while (fgets(buffer, BUFSIZE, fp) != NULL) {
@ -114,12 +109,30 @@ static void probe(void)
meminfo.swaptotal, meminfo.swapfree);
fclose(fp);
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 = {
.name = "memory",
.interval = 10,
.init = &init,
.fini = &fini,
.probe = &probe,
.get_ds = &get_ds,
};

View File

@ -37,7 +37,7 @@ static char * get_ds(int ds_id)
return ds_def;
}
static void probe(void)
static int probe(void)
{
FILE *fp;
struct mntent *mnt;
@ -48,7 +48,7 @@ static void probe(void)
fp = setmntent("/etc/mtab", "r");
if (fp == NULL) {
log_print(LOG_WARN, "plugin mount");
return;
return -1;
}
while ((mnt = getmntent(fp)) != NULL) {
@ -98,6 +98,7 @@ static void probe(void)
fs.f_bfree * (fs.f_bsize /1024));
}
endmntent(fp);
return 0;
}
struct sammler_plugin plugin = {

View File

@ -39,24 +39,20 @@ static char * get_ds(int ds_id)
return ds_def;
}
static void probe(void)
static char *buffer;
static int probe(void)
{
FILE *fp;
char *buffer, *stats, *device;
char *stats, *device;
char *val[16], filename[32];
int len;
buffer = malloc(BUFSIZE);
if (buffer == NULL) {
log_print(LOG_WARN, "plugin netdev: out of memory");
return;
}
fp = fopen("/proc/net/dev", "r");
if (fp == NULL) {
log_print(LOG_WARN, "plugin netdev");
free(buffer);
return;
return -1;
}
while (fgets(buffer, BUFSIZE, fp) != NULL) {
@ -84,12 +80,30 @@ static void probe(void)
val[0], val[8], val[1], val[9]);
}
fclose(fp);
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 = {
.name = "netdev",
.interval = 10,
.init = &init,
.fini = &fini,
.probe = &probe,
.get_ds = &get_ds,
};

View File

@ -33,7 +33,7 @@ static char * get_ds(int ds_id)
return ds_def;
}
static void probe(void)
static int probe(void)
{
FILE *fp;
char buffer[32];
@ -42,21 +42,22 @@ static void probe(void)
fp = fopen("/proc/sys/kernel/random/entropy_avail", "r");
if (fp == NULL) {
log_print(LOG_WARN, "plugin random");
return;
return -1;
}
if (fgets(buffer, sizeof(buffer), fp) == NULL) {
log_print(LOG_WARN, "plugin random");
fclose(fp);
return;
return -1;
}
fclose(fp);
if (strsplit(buffer, val, 1) != 1)
return;
return -1;
probe_submit(&plugin, "random.rrd", 0, "%s", val[0]);
return 0;
}
struct sammler_plugin plugin = {

View File

@ -67,24 +67,20 @@ static char * get_ds(int ds_id)
}
}
static void probe(void)
static char *buffer;
static int probe(void)
{
FILE *fp;
char *buffer, *val[17], filename[16];
char *val[17], filename[16];
unsigned long long arr[17];
int i, len, cpu = 0;
buffer = malloc(BUFSIZE);
if (buffer == NULL) {
log_print(LOG_WARN, "plugin rtstat: out of memory");
return;
}
fp = fopen("/proc/net/stat/rt_cache", "r");
if (fp == NULL) {
log_print(LOG_WARN, "plugin rtstat");
free(buffer);
return;
return -1;
}
while (fgets(buffer, BUFSIZE, fp) != NULL) {
@ -118,12 +114,30 @@ static void probe(void)
cpu++;
}
fclose(fp);
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 = {
.name = "rtstat",
.interval = 10,
.init = &init,
.fini = &fini,
.probe = &probe,
.get_ds = &get_ds,
};

View File

@ -67,25 +67,20 @@ struct proc_ {
unsigned long long fork;
};
static void probe(void)
static char *buffer;
static int probe(void)
{
FILE *fp;
char *buffer;
struct proc_ proc;
memset(&proc, 0, sizeof(proc));
buffer = malloc(BUFSIZE);
if (buffer == NULL) {
log_print(LOG_WARN, "plugin stat: out of memory");
return;
}
fp = fopen("/proc/stat", "r");
if (fp == NULL) {
log_print(LOG_WARN, "plugin stat");
free(buffer);
return;
return -1;
}
while (fgets(buffer, BUFSIZE, fp) != NULL) {
@ -134,12 +129,30 @@ static void probe(void)
proc.intr, proc.ctxt, proc.fork);
fclose(fp);
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 = {
.name = "stat",
.interval = 10,
.init = &init,
.fini = &fini,
.probe = &probe,
.get_ds = &get_ds,
};

View File

@ -34,7 +34,7 @@ static char * get_ds(int ds_id)
return ds_def;
}
static void probe(void)
static int probe(void)
{
FILE *fp;
char buffer[32];
@ -43,21 +43,22 @@ static void probe(void)
fp = fopen("/proc/uptime", "r");
if (fp == NULL) {
log_print(LOG_WARN, "plugin uptime");
return;
return -1;
}
if (fgets(buffer, sizeof(buffer), fp) == NULL) {
log_print(LOG_WARN, "plugin uptime");
fclose(fp);
return;
return -1;
}
fclose(fp);
if (strsplit(buffer, val, 2) != 2)
return;
return -1;
probe_submit(&plugin, "uptime.rrd", 0, "%s:%s", val[0], val[1]);
return 0;
}
struct sammler_plugin plugin = {

View File

@ -48,25 +48,20 @@ struct vmstat_ {
unsigned long long pgfault;
};
static void probe(void)
static char *buffer;
static int probe(void)
{
FILE *fp;
char *buffer;
struct vmstat_ vmstat;
memset(&vmstat, 0, sizeof(vmstat));
buffer = malloc(BUFSIZE);
if (buffer == NULL) {
log_print(LOG_WARN, "plugin vmstat: out of memory");
return;
}
fp = fopen("/proc/vmstat", "r");
if (fp == NULL) {
log_print(LOG_WARN, "plugin vmstat");
free(buffer);
return;
return -1;
}
while (fgets(buffer, BUFSIZE, fp) != NULL) {
@ -91,12 +86,30 @@ static void probe(void)
vmstat.pgalloc_dma, vmstat.pgfree, vmstat.pgfault);
fclose(fp);
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 = {
.name = "vmstat",
.interval = 10,
.init = &init,
.fini = &fini,
.probe = &probe,
.get_ds = &get_ds,
};

View File

@ -39,7 +39,7 @@ static LIST_HEAD(plugin_list);
static int plugin_flags;
static void plugin_load(char *filename)
static int plugin_load(char *filename)
{
struct sammler_plugin *plugin = NULL;
static char *plugin_dir;
@ -54,14 +54,14 @@ static void plugin_load(char *filename)
buffer = malloc(BUFSIZE);
if (buffer == NULL) {
log_print(LOG_ERROR, "plugin_load: out of memory");
return;
return -1;
}
len = snprintf(buffer, BUFSIZE, "%s/%s", plugin_dir, filename);
if (len < 0 || len >= BUFSIZE) {
log_print(LOG_ERROR, "plugin_load: file name too long: %s/%s", plugin_dir, filename);
free(buffer);
return;
return -1;
}
dlerror();
@ -69,24 +69,28 @@ static void plugin_load(char *filename)
if (tmp == NULL) {
log_print(LOG_ERROR, "plugin_load: dlopen: %s", dlerror());
free(buffer);
return;
return -1;
}
free(buffer);
plugin = dlsym(tmp, "plugin");
if (plugin == NULL) {
log_print(LOG_ERROR, "plugin_load: failed to load '%s'", filename);
dlclose(tmp);
free(buffer);
return;
return -1;
}
log_print(LOG_INFO, "Plugin '%s' loaded", plugin->name);
plugin->lastprobe = 0;
list_add_tail(&plugin->list, &plugin_list);
if (plugin->init != NULL && (plugin->init() != 0)) {
log_print(LOG_ERROR, "Plugin '%s': init failed", plugin->name);
return -1;
}
free(buffer);
return;
list_add_tail(&plugin->list, &plugin_list);
return 0;
}
void plugin_init(int flags)

View File

@ -12,7 +12,9 @@ struct sammler_plugin {
char *name;
unsigned int interval;
unsigned long lastprobe;
void (*probe) (void);
int (*init) (void);
int (*fini) (void);
int (*probe) (void);
char * (*get_ds) (int ds_id);
};