186 lines
4.6 KiB
C
186 lines
4.6 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 "list.h"
|
|
|
|
#include "plugins.h"
|
|
#include "configfile.h"
|
|
#include "logging.h"
|
|
#include "network.h"
|
|
#include "rrdtool.h"
|
|
|
|
#define BUFSIZE 512
|
|
|
|
static LIST_HEAD(plugin_list);
|
|
|
|
static int plugin_flags;
|
|
|
|
static void plugin_load(char *filename)
|
|
{
|
|
struct sammler_plugin *plugin = NULL;
|
|
static char *plugin_dir;
|
|
|
|
char *buffer;
|
|
void *tmp;
|
|
int len;
|
|
|
|
if (plugin_dir == NULL)
|
|
plugin_dir = config_get_string("global", "plugin_dir", ".");
|
|
|
|
buffer = malloc(BUFSIZE);
|
|
if (buffer == NULL) {
|
|
log_print(LOG_ERROR, "plugin_load: out of memory");
|
|
return;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
dlerror();
|
|
tmp = dlopen(buffer, RTLD_NOW);
|
|
if (tmp == NULL) {
|
|
log_print(LOG_ERROR, "plugin_load: dlopen: %s", dlerror());
|
|
free(buffer);
|
|
return;
|
|
}
|
|
|
|
plugin = dlsym(tmp, "plugin");
|
|
if (plugin == NULL) {
|
|
log_print(LOG_ERROR, "plugin_load: failed to load '%s'", filename);
|
|
dlclose(tmp);
|
|
free(buffer);
|
|
return;
|
|
}
|
|
|
|
log_print(LOG_INFO, "Plugin '%s' loaded", plugin->name);
|
|
plugin->lastprobe = 0;
|
|
|
|
list_add_tail(&plugin->list, &plugin_list);
|
|
|
|
free(buffer);
|
|
return;
|
|
}
|
|
|
|
void plugin_init(int flags)
|
|
{
|
|
struct conf_section *section;
|
|
struct conf_tupel *tupel;
|
|
|
|
section = config_get_section("global");
|
|
if (section == NULL)
|
|
return;
|
|
|
|
list_for_each_entry(tupel, §ion->tupel, list)
|
|
if (!strcmp(tupel->option, "plugin"))
|
|
plugin_load(tupel->parameter);
|
|
|
|
plugin_flags = flags;
|
|
}
|
|
|
|
void plugins_probe(void)
|
|
{
|
|
struct sammler_plugin *plugin;
|
|
unsigned long now;
|
|
|
|
now = time(NULL);
|
|
|
|
list_for_each_entry(plugin, &plugin_list, list) {
|
|
if (plugin->lastprobe + plugin->interval <= now) {
|
|
plugin->probe();
|
|
plugin->lastprobe = now;
|
|
}
|
|
}
|
|
}
|
|
|
|
struct sammler_plugin * plugin_lookup(char *name)
|
|
{
|
|
struct sammler_plugin *plugin;
|
|
list_for_each_entry(plugin, &plugin_list, list) {
|
|
if (!strcmp(plugin->name, name))
|
|
return plugin;
|
|
}
|
|
|
|
return NULL;
|
|
}
|
|
|
|
void probe_submit(struct sammler_plugin *plugin, char *filename, int ds_id, const char *fmt, ... )
|
|
{
|
|
static char *hostname = NULL;
|
|
|
|
va_list az;
|
|
char *buffer;
|
|
int len;
|
|
|
|
if (hostname == NULL)
|
|
hostname = config_get_string("global", "hostname", "localhost");
|
|
|
|
buffer = malloc(BUFSIZE);
|
|
if (buffer == NULL) {
|
|
log_print(LOG_ERROR, "probe_submit: out of memory");
|
|
return;
|
|
}
|
|
|
|
va_start(az, fmt);
|
|
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;
|
|
}
|
|
|
|
if (plugin_flags & PLUGIN_RRD)
|
|
rrd_submit(hostname, plugin->name, filename, ds_id, buffer);
|
|
|
|
if (plugin_flags & PLUGIN_NET)
|
|
net_submit(hostname, plugin->name, filename, ds_id, buffer);
|
|
|
|
free(buffer);
|
|
}
|
|
|
|
|
|
|
|
int strsplit(char *string, char **fields, size_t size)
|
|
{
|
|
size_t i = 0;
|
|
char *ptr = string;
|
|
|
|
while ((fields[i] = strtok(ptr, " \n\t")) != NULL) {
|
|
ptr = NULL;
|
|
i++;
|
|
|
|
if (i >= size)
|
|
break;
|
|
}
|
|
return i;
|
|
}
|