sammler/plugins.c

186 lines
5.0 KiB
C

/***************************************************************************
* Copyright (C) 06/2006 by Olaf Rempel *
* razzor@kopf-tisch.de *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <stdio.h>
#include <string.h>
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <time.h>
#include <limits.h>
#include "list.h"
#include "plugins.h"
#include "configfile.h"
#include "event.h"
#include "logging.h"
#include "network.h"
#include "rrdtool.h"
#define BUFSIZE 512
#define SUBMIT_NET_ONLY 0x01
static LIST_HEAD(plugin_list);
static int submit_flags;
static char *scratchpad;
static int plugin_init_cb(const char *filename, void *privdata)
{
int *bufsize = (int *)privdata;
static const char *plugin_dir;
if (plugin_dir == NULL)
plugin_dir = config_get_string("global", "plugin_dir", ".");
char *buffer = malloc(PATH_MAX);
if (buffer == NULL) {
log_print(LOG_ERROR, "plugin_load: out of memory");
return -1;
}
int len = snprintf(buffer, PATH_MAX, "%s/%s", plugin_dir, filename);
if (len < 0 || len >= PATH_MAX) {
log_print(LOG_ERROR, "plugin_load: file name too long: %s/%s", plugin_dir, filename);
free(buffer);
return -1;
}
dlerror();
void *tmp = dlopen(buffer, RTLD_NOW);
if (tmp == NULL) {
log_print(LOG_ERROR, "plugin_load: dlopen: %s", dlerror());
free(buffer);
return -1;
}
free(buffer);
struct sammler_plugin *plugin = dlsym(tmp, "plugin");
if (plugin == NULL) {
log_print(LOG_ERROR, "plugin_load: failed to load '%s'", filename);
dlclose(tmp);
return -1;
}
log_print(LOG_INFO, "Plugin '%s' loaded", plugin->name);
plugin->lastprobe = 0;
if (plugin->init != NULL && (plugin->init() != 0)) {
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);
return 0;
}
static int plugins_probe(void *privdata)
{
time_t now;
time(&now);
struct sammler_plugin *plugin;
list_for_each_entry(plugin, &plugin_list, list) {
if (plugin->lastprobe + plugin->interval <= now) {
plugin->probe();
plugin->lastprobe = now;
}
}
net_submit_flush();
return 0;
}
int plugin_init(void)
{
int bufsize = 0;
config_get_strings("global", "plugin", plugin_init_cb, &bufsize);
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;
const char *fwd_only = config_get_string("global", "forward_only", NULL);
if (fwd_only == NULL || strncmp(fwd_only, "true", 4))
submit_flags |= SUBMIT_NET_ONLY;
struct timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 0;
event_add_timeout(&tv, plugins_probe, NULL);
return 0;
}
struct sammler_plugin * plugin_lookup(const char *name)
{
struct sammler_plugin *plugin;
list_for_each_entry(plugin, &plugin_list, list) {
if (!strcmp(plugin->name, name))
return plugin;
}
return NULL;
}
int probe_submit(struct sammler_plugin *plugin, const char *filename, int ds_id, const char *fmt, ... )
{
static const char *hostname = NULL;
if (hostname == NULL)
hostname = config_get_string("global", "hostname", "localhost");
char *buffer = malloc(BUFSIZE);
if (buffer == NULL) {
log_print(LOG_ERROR, "probe_submit: out of memory");
return -1;
}
va_list az;
va_start(az, fmt);
int len = vsnprintf(buffer, BUFSIZE, fmt, az);
va_end(az);
if (len < 0 || len >= BUFSIZE) {
log_print(LOG_ERROR, "probe_submit: %s arguments too long", plugin->name);
free(buffer);
return -1;
}
net_submit(hostname, plugin, filename, ds_id, buffer);
if (!(submit_flags & SUBMIT_NET_ONLY))
rrd_submit(hostname, plugin, filename, ds_id, buffer);
free(buffer);
return 0;
}