2006-06-13 21:34:36 +02:00
|
|
|
/***************************************************************************
|
|
|
|
* 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>
|
2006-09-30 17:10:32 +02:00
|
|
|
#include <time.h>
|
2007-03-31 22:31:07 +02:00
|
|
|
#include <limits.h>
|
2006-06-13 21:34:36 +02:00
|
|
|
|
|
|
|
#include "list.h"
|
|
|
|
|
|
|
|
#include "plugins.h"
|
2006-08-03 19:49:07 +02:00
|
|
|
#include "configfile.h"
|
2007-04-01 00:17:50 +02:00
|
|
|
#include "event.h"
|
2006-06-13 21:34:36 +02:00
|
|
|
#include "logging.h"
|
2006-08-03 19:49:07 +02:00
|
|
|
#include "network.h"
|
2006-06-13 21:34:36 +02:00
|
|
|
#include "rrdtool.h"
|
|
|
|
|
2006-09-30 20:27:56 +02:00
|
|
|
#define BUFSIZE 512
|
2006-06-13 21:34:36 +02:00
|
|
|
|
2007-04-01 00:17:50 +02:00
|
|
|
#define SUBMIT_NET_ONLY 0x01
|
|
|
|
|
2006-06-13 21:34:36 +02:00
|
|
|
static LIST_HEAD(plugin_list);
|
|
|
|
|
2007-04-01 00:17:50 +02:00
|
|
|
static int submit_flags;
|
2006-09-30 20:27:56 +02:00
|
|
|
|
2007-03-31 23:21:31 +02:00
|
|
|
static char *scratchpad;
|
|
|
|
|
2007-03-31 22:15:00 +02:00
|
|
|
static int plugin_init_cb(const char *filename, void *privdata)
|
2006-06-13 21:34:36 +02:00
|
|
|
{
|
2007-03-31 23:21:31 +02:00
|
|
|
int *bufsize = (int *)privdata;
|
2006-06-13 21:34:36 +02:00
|
|
|
|
2007-04-01 00:17:50 +02:00
|
|
|
static const char *plugin_dir;
|
2006-06-13 21:34:36 +02:00
|
|
|
if (plugin_dir == NULL)
|
|
|
|
plugin_dir = config_get_string("global", "plugin_dir", ".");
|
|
|
|
|
2007-04-01 00:17:50 +02:00
|
|
|
char *buffer = malloc(PATH_MAX);
|
2006-06-13 21:34:36 +02:00
|
|
|
if (buffer == NULL) {
|
|
|
|
log_print(LOG_ERROR, "plugin_load: out of memory");
|
2006-10-07 20:37:30 +02:00
|
|
|
return -1;
|
2006-06-13 21:34:36 +02:00
|
|
|
}
|
|
|
|
|
2007-04-01 00:17:50 +02:00
|
|
|
int len = snprintf(buffer, PATH_MAX, "%s/%s", plugin_dir, filename);
|
2007-03-31 22:31:07 +02:00
|
|
|
if (len < 0 || len >= PATH_MAX) {
|
2006-06-13 21:34:36 +02:00
|
|
|
log_print(LOG_ERROR, "plugin_load: file name too long: %s/%s", plugin_dir, filename);
|
|
|
|
free(buffer);
|
2006-10-07 20:37:30 +02:00
|
|
|
return -1;
|
2006-06-13 21:34:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
dlerror();
|
2007-04-01 00:17:50 +02:00
|
|
|
void *tmp = dlopen(buffer, RTLD_NOW);
|
2006-06-13 21:34:36 +02:00
|
|
|
if (tmp == NULL) {
|
|
|
|
log_print(LOG_ERROR, "plugin_load: dlopen: %s", dlerror());
|
|
|
|
free(buffer);
|
2006-10-07 20:37:30 +02:00
|
|
|
return -1;
|
2006-06-13 21:34:36 +02:00
|
|
|
}
|
|
|
|
|
2006-10-07 20:37:30 +02:00
|
|
|
free(buffer);
|
|
|
|
|
2007-04-01 00:17:50 +02:00
|
|
|
struct sammler_plugin *plugin = dlsym(tmp, "plugin");
|
2006-06-13 21:34:36 +02:00
|
|
|
if (plugin == NULL) {
|
|
|
|
log_print(LOG_ERROR, "plugin_load: failed to load '%s'", filename);
|
|
|
|
dlclose(tmp);
|
2006-10-07 20:37:30 +02:00
|
|
|
return -1;
|
2006-06-13 21:34:36 +02:00
|
|
|
}
|
|
|
|
|
2006-08-26 13:43:03 +02:00
|
|
|
log_print(LOG_INFO, "Plugin '%s' loaded", plugin->name);
|
2006-09-30 17:10:32 +02:00
|
|
|
plugin->lastprobe = 0;
|
2006-06-13 21:34:36 +02:00
|
|
|
|
2006-10-07 20:37:30 +02:00
|
|
|
if (plugin->init != NULL && (plugin->init() != 0)) {
|
|
|
|
log_print(LOG_ERROR, "Plugin '%s': init failed", plugin->name);
|
|
|
|
return -1;
|
|
|
|
}
|
2006-06-13 21:34:36 +02:00
|
|
|
|
2007-03-31 23:21:31 +02:00
|
|
|
if (plugin->bufsize > *bufsize)
|
|
|
|
*bufsize = plugin->bufsize;
|
|
|
|
|
2006-10-07 20:37:30 +02:00
|
|
|
list_add_tail(&plugin->list, &plugin_list);
|
|
|
|
return 0;
|
2006-06-13 21:34:36 +02:00
|
|
|
}
|
|
|
|
|
2007-04-01 00:17:50 +02:00
|
|
|
static int plugins_probe(void *privdata)
|
2006-06-13 21:34:36 +02:00
|
|
|
{
|
2007-04-01 00:17:50 +02:00
|
|
|
time_t now;
|
|
|
|
time(&now);
|
|
|
|
|
2007-03-31 23:21:31 +02:00
|
|
|
struct sammler_plugin *plugin;
|
2007-04-01 00:17:50 +02:00
|
|
|
list_for_each_entry(plugin, &plugin_list, list) {
|
|
|
|
if (plugin->lastprobe + plugin->interval <= now) {
|
|
|
|
plugin->probe();
|
|
|
|
plugin->lastprobe = now;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
net_submit_flush();
|
|
|
|
return 0;
|
|
|
|
}
|
2007-03-31 23:21:31 +02:00
|
|
|
|
2007-04-01 00:17:50 +02:00
|
|
|
int plugin_init(void)
|
|
|
|
{
|
|
|
|
int bufsize = 0;
|
2007-03-31 23:21:31 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2007-04-01 00:17:50 +02:00
|
|
|
struct sammler_plugin *plugin;
|
2007-03-31 23:21:31 +02:00
|
|
|
list_for_each_entry(plugin, &plugin_list, list)
|
|
|
|
plugin->buffer = scratchpad;
|
|
|
|
|
2007-04-01 00:17:50 +02:00
|
|
|
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;
|
2006-06-13 21:34:36 +02:00
|
|
|
|
2007-04-01 00:17:50 +02:00
|
|
|
struct timeval tv;
|
|
|
|
tv.tv_sec = 1;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
event_add_timeout(&tv, plugins_probe, NULL);
|
2006-08-03 19:49:07 +02:00
|
|
|
|
2007-04-01 00:17:50 +02:00
|
|
|
return 0;
|
2006-09-30 20:27:56 +02:00
|
|
|
}
|
2006-09-30 17:10:32 +02:00
|
|
|
|
2007-03-31 22:31:07 +02:00
|
|
|
struct sammler_plugin * plugin_lookup(const char *name)
|
2006-09-30 20:27:56 +02:00
|
|
|
{
|
|
|
|
struct sammler_plugin *plugin;
|
|
|
|
list_for_each_entry(plugin, &plugin_list, list) {
|
|
|
|
if (!strcmp(plugin->name, name))
|
|
|
|
return plugin;
|
|
|
|
}
|
|
|
|
return NULL;
|
2006-06-13 21:34:36 +02:00
|
|
|
}
|
|
|
|
|
2007-03-31 22:31:07 +02:00
|
|
|
int probe_submit(struct sammler_plugin *plugin, const char *filename, int ds_id, const char *fmt, ... )
|
2006-06-13 21:34:36 +02:00
|
|
|
{
|
2007-03-31 22:15:00 +02:00
|
|
|
static const char *hostname = NULL;
|
2006-08-26 14:01:10 +02:00
|
|
|
if (hostname == NULL)
|
|
|
|
hostname = config_get_string("global", "hostname", "localhost");
|
|
|
|
|
2007-04-01 00:17:50 +02:00
|
|
|
char *buffer = malloc(BUFSIZE);
|
2006-06-13 21:34:36 +02:00
|
|
|
if (buffer == NULL) {
|
|
|
|
log_print(LOG_ERROR, "probe_submit: out of memory");
|
2006-10-07 20:48:20 +02:00
|
|
|
return -1;
|
2006-06-13 21:34:36 +02:00
|
|
|
}
|
|
|
|
|
2007-04-01 00:17:50 +02:00
|
|
|
va_list az;
|
2006-06-13 21:34:36 +02:00
|
|
|
va_start(az, fmt);
|
2007-04-01 00:17:50 +02:00
|
|
|
int len = vsnprintf(buffer, BUFSIZE, fmt, az);
|
2006-06-13 21:34:36 +02:00
|
|
|
va_end(az);
|
|
|
|
|
2006-06-22 20:33:30 +02:00
|
|
|
if (len < 0 || len >= BUFSIZE) {
|
2006-06-13 21:34:36 +02:00
|
|
|
log_print(LOG_ERROR, "probe_submit: %s arguments too long", plugin->name);
|
|
|
|
free(buffer);
|
2006-10-07 20:48:20 +02:00
|
|
|
return -1;
|
2006-06-13 21:34:36 +02:00
|
|
|
}
|
|
|
|
|
2007-04-01 13:21:51 +02:00
|
|
|
net_submit(hostname, plugin->name, filename, ds_id, buffer);
|
2006-09-30 20:27:56 +02:00
|
|
|
|
2007-04-01 00:17:50 +02:00
|
|
|
if (!(submit_flags & SUBMIT_NET_ONLY))
|
2007-04-01 13:21:51 +02:00
|
|
|
rrd_submit(hostname, plugin->name, filename, ds_id, buffer);
|
2006-06-13 21:34:36 +02:00
|
|
|
|
|
|
|
free(buffer);
|
2006-10-07 20:48:20 +02:00
|
|
|
return 0;
|
2006-06-13 21:34:36 +02:00
|
|
|
}
|