cleanup & fixes
This commit is contained in:
parent
bddb4b013a
commit
4e60087e06
2
Makefile
2
Makefile
@ -6,7 +6,7 @@ WITH_MYSQL = yes
|
||||
|
||||
# ############################
|
||||
|
||||
SAMMLER_SRC := sammler.c configfile.c logging.c network.c plugins.c
|
||||
SAMMLER_SRC := sammler.c configfile.c helper.c logging.c network.c plugins.c
|
||||
PLUGIN_SRC := p_ctstat.c p_load.c p_memory.c p_mount.c p_netdev.c p_random.c
|
||||
PLUGIN_SRC += p_rtstat.c p_stat.c p_uptime.c p_vmstat.c
|
||||
CFLAGS := -O2 -Wall
|
||||
|
25
configfile.c
25
configfile.c
@ -22,9 +22,14 @@
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "list.h"
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/ip.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include "configfile.h"
|
||||
#include "helper.h"
|
||||
#include "list.h"
|
||||
#include "logging.h"
|
||||
|
||||
static LIST_HEAD(config_list);
|
||||
@ -183,3 +188,21 @@ int config_get_int(char *section, char *option, int def)
|
||||
ret = config_get_string(section, option, NULL);
|
||||
return ret ? atoi(ret) : def;
|
||||
}
|
||||
|
||||
int config_get_saddr(char *section, char *option, struct sockaddr_in *sa)
|
||||
{
|
||||
char *ret, *part[2];
|
||||
|
||||
ret = config_get_string(section, option, NULL);
|
||||
if (ret == NULL)
|
||||
return -1;
|
||||
|
||||
if (strsplit(ret, ":", part, 2) != 2)
|
||||
return -1;
|
||||
|
||||
sa->sin_family = AF_INET;
|
||||
sa->sin_port = htons(atoi(part[1]));
|
||||
inet_aton(part[0], &sa->sin_addr);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -1,6 +1,8 @@
|
||||
#ifndef _CONFIG_H_
|
||||
#define _CONFIG_H_
|
||||
|
||||
#include <netinet/in.h>
|
||||
|
||||
#include "list.h"
|
||||
|
||||
struct conf_section {
|
||||
@ -20,5 +22,6 @@ struct conf_section * config_get_section(char *name);
|
||||
char * config_get_parameter(struct conf_section *section, char *option);
|
||||
char * config_get_string(char *section, char *option, char *def);
|
||||
int config_get_int(char *section, char *option, int def);
|
||||
int config_get_saddr(char *section, char *option, struct sockaddr_in *sa);
|
||||
|
||||
#endif /* _CONFIG_H_ */
|
||||
|
37
helper.c
Normal file
37
helper.c
Normal file
@ -0,0 +1,37 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) 10/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 <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
int strsplit(char *string, char *delim, char **fields, int size)
|
||||
{
|
||||
size_t i = 0;
|
||||
char *tmp, *ptr = string;
|
||||
|
||||
while ((fields[i] = strtok_r(ptr, delim, &tmp)) != NULL) {
|
||||
ptr = NULL;
|
||||
i++;
|
||||
|
||||
if (i >= size)
|
||||
break;
|
||||
}
|
||||
return i;
|
||||
}
|
8
helper.h
Normal file
8
helper.h
Normal file
@ -0,0 +1,8 @@
|
||||
#ifndef _HELPER_H_
|
||||
#define _HELPER_H_
|
||||
|
||||
#define DFLT_DELIM " \n\t"
|
||||
|
||||
int strsplit(char *string, char *delim, char **fields, int size);
|
||||
|
||||
#endif /* _HELPER_H_ */
|
121
network.c
121
network.c
@ -8,6 +8,7 @@
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include "configfile.h"
|
||||
#include "helper.h"
|
||||
#include "list.h"
|
||||
#include "logging.h"
|
||||
#include "network.h"
|
||||
@ -16,13 +17,8 @@
|
||||
|
||||
#define BUFSIZE 1024
|
||||
|
||||
static LIST_HEAD(fwd_list);
|
||||
|
||||
struct fwd_entry {
|
||||
struct list_head list;
|
||||
struct sockaddr_in sa;
|
||||
int sock;
|
||||
};
|
||||
static struct sockaddr_in fwd_sa;
|
||||
static int fwd_sock;
|
||||
|
||||
// todo: never freed..
|
||||
static char *tx_buf, *rx_buf;
|
||||
@ -35,128 +31,59 @@ int net_submit(char *hostname, char *plugin, char *filename, int ds_id, char *da
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct fwd_entry *entry;
|
||||
list_for_each_entry(entry, &fwd_list, list)
|
||||
sendto(entry->sock, tx_buf, size +1, 0, (struct sockaddr *)&entry->sa, sizeof(entry->sa));
|
||||
|
||||
sendto(fwd_sock, tx_buf, size +1, 0, (struct sockaddr *)&fwd_sa, sizeof(fwd_sa));
|
||||
return 0;
|
||||
}
|
||||
|
||||
int net_receive(int socket)
|
||||
{
|
||||
char *hostname, *plugin, *filename, *ds_id, *data;
|
||||
int size;
|
||||
recv(socket, rx_buf, BUFSIZE, 0);
|
||||
|
||||
size = recv(socket, rx_buf, BUFSIZE, 0);
|
||||
|
||||
hostname = rx_buf;
|
||||
plugin = strchr(rx_buf, ':');
|
||||
if (plugin == NULL)
|
||||
return 0;
|
||||
|
||||
*plugin++ = '\0';
|
||||
filename = strchr(plugin, ':');
|
||||
if (filename == NULL)
|
||||
return 0;
|
||||
|
||||
*filename++ = '\0';
|
||||
ds_id = strchr(filename, ':');
|
||||
if (ds_id == NULL)
|
||||
return 0;
|
||||
|
||||
*ds_id++ = '\0';
|
||||
|
||||
data = strchr(ds_id, ' ');
|
||||
if (data == NULL)
|
||||
return 0;
|
||||
|
||||
*data++ = '\0';
|
||||
|
||||
rrd_submit(hostname, plugin, filename, atoi(ds_id), data);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int parse_address(char *addr, struct sockaddr_in *sa)
|
||||
{
|
||||
if (addr == NULL)
|
||||
char *data[2], *part[4];
|
||||
int ret = strsplit(rx_buf, " ", data, 2);
|
||||
if (ret != 2)
|
||||
return -1;
|
||||
|
||||
char *port = strchr(addr, ':');
|
||||
if (port == NULL)
|
||||
ret = strsplit(data[0], ":", part, 4);
|
||||
if (ret != 4)
|
||||
return -1;
|
||||
|
||||
*port = '\0';
|
||||
|
||||
sa->sin_family = AF_INET;
|
||||
sa->sin_port = htons(atoi(port +1));
|
||||
inet_aton(addr, &sa->sin_addr);
|
||||
|
||||
*port = ':';
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int net_add_cli(char *addr)
|
||||
{
|
||||
struct fwd_entry *entry;
|
||||
|
||||
entry = malloc(sizeof(struct fwd_entry));
|
||||
if (entry == NULL) {
|
||||
log_print(LOG_ERROR, "net_add_cli(): out of memory");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (parse_address(addr, &entry->sa) == -1) {
|
||||
free(entry);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((entry->sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
|
||||
log_print(LOG_ERROR, "net_add_cli(): socket()");
|
||||
free(entry);
|
||||
return -1;
|
||||
}
|
||||
|
||||
log_print(LOG_INFO, "forwarding to %s:%d", inet_ntoa(entry->sa.sin_addr), ntohs(entry->sa.sin_port));
|
||||
list_add_tail(&entry->list, &fwd_list);
|
||||
rrd_submit(part[0], part[1], part[2], atoi(part[3]), data[1]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int net_init_cli()
|
||||
{
|
||||
int ret = config_get_saddr("global", "forward", &fwd_sa);
|
||||
if (ret < 0)
|
||||
return -1;
|
||||
|
||||
tx_buf = malloc(BUFSIZE);
|
||||
if (tx_buf == NULL) {
|
||||
log_print(LOG_ERROR, "net_init_cli(): out of memory");
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct conf_section *section;
|
||||
section = config_get_section("global");
|
||||
if (section == NULL)
|
||||
fwd_sock = socket(PF_INET, SOCK_DGRAM, 0);
|
||||
if (fwd_sock < 0) {
|
||||
log_print(LOG_ERROR, "net_init_cli(): socket()");
|
||||
return -1;
|
||||
|
||||
int retval = -1;
|
||||
struct conf_tupel *tupel;
|
||||
list_for_each_entry(tupel, §ion->tupel, list) {
|
||||
if (!strcmp(tupel->option, "forward"))
|
||||
if (net_add_cli(tupel->parameter) != -1)
|
||||
retval = 0;
|
||||
}
|
||||
|
||||
return retval;
|
||||
log_print(LOG_INFO, "forwarding to %s:%d", inet_ntoa(fwd_sa.sin_addr), ntohs(fwd_sa.sin_port));
|
||||
return 0;
|
||||
}
|
||||
|
||||
int net_init_srv()
|
||||
{
|
||||
struct sockaddr_in sa_srv;
|
||||
int srv_sock;
|
||||
|
||||
char *listen = config_get_string("global", "listen", NULL);
|
||||
if (parse_address(listen, &sa_srv) == -1)
|
||||
int ret = config_get_saddr("global", "listen", &sa_srv);
|
||||
if (ret < 0)
|
||||
return -1;
|
||||
|
||||
if ((srv_sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
|
||||
int srv_sock = socket(PF_INET, SOCK_DGRAM, 0);
|
||||
if (srv_sock < 0) {
|
||||
log_print(LOG_ERROR, "net_init_src(): socket()");
|
||||
return -1;
|
||||
}
|
||||
|
@ -21,6 +21,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "helper.h"
|
||||
#include "plugins.h"
|
||||
|
||||
#define BUFSIZE 1024
|
||||
@ -70,7 +71,7 @@ static int probe(void)
|
||||
if (!strncmp(buffer, "entries", 7))
|
||||
continue;
|
||||
|
||||
if (strsplit(buffer, val, 16) != 16)
|
||||
if (strsplit(buffer, " \t\n", val, 16) != 16)
|
||||
continue;
|
||||
|
||||
for (i = 0; i < 16; i++)
|
||||
|
3
p_load.c
3
p_load.c
@ -20,6 +20,7 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "helper.h"
|
||||
#include "plugins.h"
|
||||
|
||||
struct sammler_plugin plugin;
|
||||
@ -55,7 +56,7 @@ static int probe(void)
|
||||
|
||||
fclose(fp);
|
||||
|
||||
if (strsplit(buffer, val, 3) != 3)
|
||||
if (strsplit(buffer, " \t\n", val, 3) != 3)
|
||||
return -1;
|
||||
|
||||
probe_submit(&plugin, "load.rrd", 0, "%s:%s:%s", val[0], val[1], val[2]);
|
||||
|
57
p_mysql.c
57
p_mysql.c
@ -21,9 +21,10 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "plugins.h"
|
||||
#include "configfile.h"
|
||||
#include "helper.h"
|
||||
#include "list.h"
|
||||
#include "plugins.h"
|
||||
|
||||
#include "p_mysql_helper.h"
|
||||
|
||||
@ -162,56 +163,27 @@ static int init(void)
|
||||
if (strcmp(tupel->option, "server"))
|
||||
continue;
|
||||
|
||||
char *name = tupel->parameter;
|
||||
|
||||
char *host = strchr(name, ':');
|
||||
if (host == NULL) {
|
||||
log_print(LOG_ERROR, "p_mysql: parse error (1)");
|
||||
char *part[4];
|
||||
int ret = strsplit(tupel->parameter, ":", part, 4);
|
||||
if (ret != 4) {
|
||||
log_print(LOG_ERROR, "p_mysql: parse error");
|
||||
continue;
|
||||
}
|
||||
*host++ = '\0';
|
||||
|
||||
char *user = strchr(host, ':');
|
||||
if (user == NULL) {
|
||||
log_print(LOG_ERROR, "p_mysql: parse error (2)");
|
||||
*--host = ':';
|
||||
void *mysql = init_connection(part[1], part[2], part[3]);
|
||||
if (mysql == NULL)
|
||||
continue;
|
||||
}
|
||||
*user++ = '\0';
|
||||
|
||||
char *pass = strchr(user, ':');
|
||||
if (pass == NULL) {
|
||||
log_print(LOG_ERROR, "p_mysql: parse error (3)");
|
||||
*--host = ':';
|
||||
*--user = ':';
|
||||
continue;
|
||||
}
|
||||
*pass++ = '\0';
|
||||
|
||||
int len = strlen(name);
|
||||
int len = strlen(part[0]);
|
||||
struct server_entry *entry = malloc(sizeof(struct server_entry) + len +1);
|
||||
if (entry == NULL) {
|
||||
log_print(LOG_ERROR, "p_mysql: out of memory");
|
||||
*--host = ':';
|
||||
*--user = ':';
|
||||
*--pass = ':';
|
||||
close_connection(mysql);
|
||||
continue;
|
||||
}
|
||||
|
||||
strcpy(entry->name, name);
|
||||
|
||||
entry->mysql = init_connection(host, user, pass);
|
||||
if (entry->mysql < 0) {
|
||||
free(entry);
|
||||
*--host = ':';
|
||||
*--user = ':';
|
||||
*--pass = ':';
|
||||
continue;
|
||||
}
|
||||
|
||||
*--host = ':';
|
||||
*--user = ':';
|
||||
*--pass = ':';
|
||||
strcpy(entry->name, part[0]);
|
||||
entry->mysql = mysql;
|
||||
|
||||
log_print(LOG_DEBUG, "p_mysql: added server '%s'", entry->name);
|
||||
list_add_tail(&entry->list, &server_list);
|
||||
@ -225,9 +197,10 @@ static int init(void)
|
||||
static int fini(void)
|
||||
{
|
||||
struct server_entry *entry, *tmp;
|
||||
list_for_each_entry_safe(entry, tmp, &server_list, list)
|
||||
list_for_each_entry_safe(entry, tmp, &server_list, list) {
|
||||
close_connection(entry->mysql);
|
||||
free(entry);
|
||||
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -31,6 +31,14 @@ int ping_connection(void *mysql)
|
||||
return mysql_ping(con);
|
||||
}
|
||||
|
||||
int close_connection(void *mysql)
|
||||
{
|
||||
MYSQL *con = mysql;
|
||||
|
||||
mysql_close(con);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int get_stats(void *mysql, struct mysql_stats *stats)
|
||||
{
|
||||
MYSQL *con = mysql;
|
||||
|
@ -27,6 +27,7 @@ struct mysql_stats {
|
||||
|
||||
void * init_connection(char *host, char *user, char *pass);
|
||||
int ping_connection(void *mysql);
|
||||
int close_connection(void *mysql);
|
||||
|
||||
int get_stats(void *mysql, struct mysql_stats *stats);
|
||||
|
||||
|
@ -21,6 +21,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "helper.h"
|
||||
#include "plugins.h"
|
||||
|
||||
#define BUFSIZE 1024
|
||||
@ -68,7 +69,7 @@ static int probe(void)
|
||||
if (*device == '\0')
|
||||
continue;
|
||||
|
||||
if (strsplit(stats, val, 16) != 16)
|
||||
if (strsplit(stats, " \t\n", val, 16) != 16)
|
||||
continue;
|
||||
|
||||
len = snprintf(filename, sizeof(filename), "net-%s.rrd", device);
|
||||
|
@ -20,6 +20,7 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "helper.h"
|
||||
#include "plugins.h"
|
||||
|
||||
struct sammler_plugin plugin;
|
||||
@ -53,7 +54,7 @@ static int probe(void)
|
||||
|
||||
fclose(fp);
|
||||
|
||||
if (strsplit(buffer, val, 1) != 1)
|
||||
if (strsplit(buffer, " \t\n", val, 1) != 1)
|
||||
return -1;
|
||||
|
||||
probe_submit(&plugin, "random.rrd", 0, "%s", val[0]);
|
||||
|
@ -21,6 +21,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "helper.h"
|
||||
#include "plugins.h"
|
||||
|
||||
#define BUFSIZE 1024
|
||||
@ -86,7 +87,7 @@ static int probe(void)
|
||||
if (!strncmp(buffer, "entries", 7))
|
||||
continue;
|
||||
|
||||
if (strsplit(buffer, val, 17) != 17)
|
||||
if (strsplit(buffer, " \t\n", val, 17) != 17)
|
||||
continue;
|
||||
|
||||
for (i = 0; i < 17; i++)
|
||||
|
3
p_stat.c
3
p_stat.c
@ -21,6 +21,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "helper.h"
|
||||
#include "plugins.h"
|
||||
|
||||
#define BUFSIZE 1024
|
||||
@ -97,7 +98,7 @@ static int probe(void)
|
||||
strncpy(filename, "cpu.rrd", sizeof(filename));
|
||||
}
|
||||
|
||||
numfields = strsplit(buffer, val, 9);
|
||||
numfields = strsplit(buffer, " \t\n", val, 9);
|
||||
if (numfields < 5)
|
||||
continue;
|
||||
|
||||
|
@ -20,6 +20,7 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "helper.h"
|
||||
#include "plugins.h"
|
||||
|
||||
struct sammler_plugin plugin;
|
||||
@ -54,7 +55,7 @@ static int probe(void)
|
||||
|
||||
fclose(fp);
|
||||
|
||||
if (strsplit(buffer, val, 2) != 2)
|
||||
if (strsplit(buffer, " \t\n", val, 2) != 2)
|
||||
return -1;
|
||||
|
||||
probe_submit(&plugin, "uptime.rrd", 0, "%s:%s", val[0], val[1]);
|
||||
|
17
plugins.c
17
plugins.c
@ -170,20 +170,3 @@ int probe_submit(struct sammler_plugin *plugin, char *filename, int ds_id, const
|
||||
free(buffer);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
@ -26,6 +26,4 @@ struct sammler_plugin * plugin_lookup(char *name);
|
||||
|
||||
int probe_submit(struct sammler_plugin *plugin, char *filename, int ds_id, const char *fmt, ... );
|
||||
|
||||
int strsplit(char *string, char **fields, size_t size);
|
||||
|
||||
#endif /* _PLUGINS_H_ */
|
||||
|
@ -26,10 +26,10 @@
|
||||
|
||||
#include <rrd.h>
|
||||
|
||||
#include "list.h"
|
||||
|
||||
#include "logging.h"
|
||||
#include "configfile.h"
|
||||
#include "helper.h"
|
||||
#include "list.h"
|
||||
#include "logging.h"
|
||||
#include "plugins.h"
|
||||
|
||||
#define DEFAULT_STEP 10
|
||||
@ -76,7 +76,7 @@ static int do_rrd(int mode, char *cmd)
|
||||
int argc;
|
||||
char *argv[ARGCMAX];
|
||||
|
||||
argc = strsplit(cmd, argv, ARGCMAX -1);
|
||||
argc = strsplit(cmd, " \t\n", argv, ARGCMAX -1);
|
||||
argv[argc] = NULL;
|
||||
|
||||
optind = 0;
|
||||
|
Loading…
Reference in New Issue
Block a user