From 4e60087e0693ea6920d711f10ba3f6ad08c26887 Mon Sep 17 00:00:00 2001 From: Olaf Rempel Date: Sun, 8 Oct 2006 16:33:07 +0200 Subject: [PATCH] cleanup & fixes --- Makefile | 2 +- configfile.c | 25 +++++++++- configfile.h | 3 ++ helper.c | 37 +++++++++++++++ helper.h | 8 ++++ network.c | 121 ++++++++++------------------------------------- p_ctstat.c | 3 +- p_load.c | 3 +- p_mysql.c | 57 ++++++---------------- p_mysql_helper.c | 8 ++++ p_mysql_helper.h | 1 + p_netdev.c | 3 +- p_random.c | 3 +- p_rtstat.c | 3 +- p_stat.c | 3 +- p_uptime.c | 3 +- plugins.c | 17 ------- plugins.h | 2 - rrdtool.c | 8 ++-- 19 files changed, 139 insertions(+), 171 deletions(-) create mode 100644 helper.c create mode 100644 helper.h diff --git a/Makefile b/Makefile index 19130de..9b4db8f 100644 --- a/Makefile +++ b/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 diff --git a/configfile.c b/configfile.c index 6246619..07cf7f7 100644 --- a/configfile.c +++ b/configfile.c @@ -22,9 +22,14 @@ #include #include -#include "list.h" +#include +#include +#include +#include #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; +} diff --git a/configfile.h b/configfile.h index 1f0131f..593523b 100644 --- a/configfile.h +++ b/configfile.h @@ -1,6 +1,8 @@ #ifndef _CONFIG_H_ #define _CONFIG_H_ +#include + #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_ */ diff --git a/helper.c b/helper.c new file mode 100644 index 0000000..e64dec8 --- /dev/null +++ b/helper.c @@ -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 +#include +#include + +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; +} diff --git a/helper.h b/helper.h new file mode 100644 index 0000000..d20eb86 --- /dev/null +++ b/helper.h @@ -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_ */ diff --git a/network.c b/network.c index 61dd9ec..d751e38 100644 --- a/network.c +++ b/network.c @@ -8,6 +8,7 @@ #include #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; } diff --git a/p_ctstat.c b/p_ctstat.c index 3f5e3ea..09a14e3 100644 --- a/p_ctstat.c +++ b/p_ctstat.c @@ -21,6 +21,7 @@ #include #include +#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++) diff --git a/p_load.c b/p_load.c index 43ffe8d..baac5ba 100644 --- a/p_load.c +++ b/p_load.c @@ -20,6 +20,7 @@ #include #include +#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]); diff --git a/p_mysql.c b/p_mysql.c index 13a5054..1ebc6f0 100644 --- a/p_mysql.c +++ b/p_mysql.c @@ -21,9 +21,10 @@ #include #include -#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; } diff --git a/p_mysql_helper.c b/p_mysql_helper.c index 377c8f1..97f6173 100644 --- a/p_mysql_helper.c +++ b/p_mysql_helper.c @@ -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; diff --git a/p_mysql_helper.h b/p_mysql_helper.h index 731e8a6..5b5b3b9 100644 --- a/p_mysql_helper.h +++ b/p_mysql_helper.h @@ -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); diff --git a/p_netdev.c b/p_netdev.c index 48e5f61..9a5c92e 100644 --- a/p_netdev.c +++ b/p_netdev.c @@ -21,6 +21,7 @@ #include #include +#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); diff --git a/p_random.c b/p_random.c index b418639..0b37831 100644 --- a/p_random.c +++ b/p_random.c @@ -20,6 +20,7 @@ #include #include +#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]); diff --git a/p_rtstat.c b/p_rtstat.c index 565093f..eb0ad1e 100644 --- a/p_rtstat.c +++ b/p_rtstat.c @@ -21,6 +21,7 @@ #include #include +#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++) diff --git a/p_stat.c b/p_stat.c index 1ba22c1..cabe8e6 100644 --- a/p_stat.c +++ b/p_stat.c @@ -21,6 +21,7 @@ #include #include +#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; diff --git a/p_uptime.c b/p_uptime.c index 87d029d..86a13a7 100644 --- a/p_uptime.c +++ b/p_uptime.c @@ -20,6 +20,7 @@ #include #include +#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]); diff --git a/plugins.c b/plugins.c index 9be4a69..76cef51 100644 --- a/plugins.c +++ b/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; -} diff --git a/plugins.h b/plugins.h index ead3c77..0193fa7 100644 --- a/plugins.h +++ b/plugins.h @@ -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_ */ diff --git a/rrdtool.c b/rrdtool.c index c378889..50a8e58 100644 --- a/rrdtool.c +++ b/rrdtool.c @@ -26,10 +26,10 @@ #include -#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;