Skip to content
This repository has been archived by the owner on Dec 26, 2017. It is now read-only.

Commit

Permalink
Updated getopt args for new -E parameter. Updated test scripts for ne…
Browse files Browse the repository at this point in the history
…w max_errors_per_host param to get_clinserts()
  • Loading branch information
rhinst2 committed Mar 31, 2014
1 parent e77384e commit 2f3ce1b
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 19 deletions.
2 changes: 2 additions & 0 deletions src/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#define DEFAULT_INTERVAL 300 /* seconds */
/** Default number of poller threads. */
#define DEFAULT_THREADS 8
/**Default maximum number of errors before we stop polling a host **/
#define DEFAULT_MAX_ERRORS_PER_HOST 3

struct clbuf;

Expand Down
16 changes: 12 additions & 4 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

static void help(void);
static void run_threads(struct rtgtargets *targets, struct rtgconf *config);
static struct mt_threads *create_poller_threads(unsigned nthreads, struct rtgtargets *targets);
static struct mt_threads *create_poller_threads(unsigned nthreads, struct rtgtargets *targets, unsigned max_errors_per_host);
static struct mt_threads *create_database_threads(unsigned nthreads, struct rtgconf *config);
static struct mt_threads *create_monitor_thread(struct rtgtargets *targets, struct rtgconf *config, curms_t next_iteration);
static void free_threads_params(struct mt_threads *threads);
Expand All @@ -42,6 +42,7 @@ int main(int argc, char *const argv[])
int detach = 1;
const char *rtgconf_file = DEFAULT_RTGCONF_FILE;
const char *targets_file = DEFAULT_TARGETS_FILE;
unsigned max_errors_per_host = DEFAULT_MAX_ERRORS_PER_HOST;
int use_db = 1;
int use_rate_column = 1;
int use_currvalue_column = 0;
Expand All @@ -58,7 +59,7 @@ int main(int argc, char *const argv[])
exit(EXIT_FAILURE);
}

while ((c = getopt(argc, argv, "c:dt:vzDOOQ:W:")) != -1) {
while ((c = getopt(argc, argv, "c:dt:vzCDE:OOQ:W:")) != -1) {
switch (c) {
case 'c':
rtgconf_file = optarg;
Expand All @@ -81,6 +82,9 @@ int main(int argc, char *const argv[])
case 'D':
detach = 0;
break;
case 'E':
max_errors_per_host = (unsigned) strtol(optarg, NULL, 10);
break;
case 'O':
use_rate_column = 0;
break;
Expand Down Expand Up @@ -171,6 +175,7 @@ int main(int argc, char *const argv[])
config->allow_db_zero = allow_db_zero;
config->max_db_queue = max_db_queue;
config->num_dbthreads = num_dbthreads;
config->max_errors_per_host = max_errors_per_host;

/* Read targets.cfg */
targets = rtgtargets_parse(targets_file, config);
Expand Down Expand Up @@ -208,7 +213,9 @@ void help(void)
fprintf(stderr, " -z Database zero delta inserts\n");
fprintf(stderr, "\n");
fprintf(stderr, "Extended options:\n");
fprintf(stderr, " -C Use currvalue column to store current interface counter value\n");
fprintf(stderr, " -D Don't detach, run in foreground\n");
fprintf(stderr, " -E <num> Maximum errors before we stop polling a host [%d]\n", DEFAULT_MAX_ERRORS_PER_HOST);
fprintf(stderr, " -O Use old database schema, no `rate` column\n");
fprintf(stderr, " -Q <num> Maximum database queue length [%d]\n", DEFAULT_QUEUE_LENGTH);
fprintf(stderr, " -W <num> Number of database threads [%d]\n", DEFAULT_NUM_DBTHREADS);
Expand All @@ -230,7 +237,7 @@ void run_threads(struct rtgtargets *targets, struct rtgconf *config)

queries = clbuf_create(config->max_db_queue);

poller_threads = create_poller_threads(config->threads, targets);
poller_threads = create_poller_threads(config->threads, targets, config->max_errors_per_host);
database_threads = create_database_threads(config->num_dbthreads, config);
monitor_thread = create_monitor_thread(targets, config, next_iteration);

Expand All @@ -248,7 +255,7 @@ void run_threads(struct rtgtargets *targets, struct rtgconf *config)
queries = 0;
}

struct mt_threads *create_poller_threads(unsigned nthreads, struct rtgtargets *targets)
struct mt_threads *create_poller_threads(unsigned nthreads, struct rtgtargets *targets, unsigned max_errors_per_host)
{
struct mt_threads *poller_threads = mt_threads_create(nthreads);
unsigned i;
Expand All @@ -257,6 +264,7 @@ struct mt_threads *create_poller_threads(unsigned nthreads, struct rtgtargets *t
for (i = 0; i < nthreads; i++) {
struct poller_ctx *ctx = (struct poller_ctx *) xmalloc(sizeof(struct poller_ctx));
ctx->targets = targets;
ctx->max_errors_per_host = max_errors_per_host;
poller_threads->contexts[i].param = ctx;
}
mt_threads_start(poller_threads, poller_run);
Expand Down
14 changes: 6 additions & 8 deletions src/poller.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@
#include <string.h>
#include <time.h>

#define MAXERRORSPERHOST 3

static void thread_inactive(unsigned id, unsigned iterations);
static void thread_active(unsigned id);
static void process_host(unsigned id, struct queryhost *host);
static void process_host(unsigned id, struct queryhost *host, unsigned max_errors_per_host);
static void process_queries(unsigned id, struct clinsert **host_queries);

void *poller_run(void *ptr)
Expand All @@ -51,7 +49,7 @@ void *poller_run(void *ptr)

/* Loop over our share of the hosts. */
while (!thread_stop_requested && (host = rtgtargets_next(targets)))
process_host(id, host);
process_host(id, host, poller_context->max_errors_per_host);

/* Prepare for next iteration. */
iterations++;
Expand Down Expand Up @@ -87,14 +85,14 @@ void thread_active(unsigned id)
pthread_mutex_unlock(&global_lock);
}

void process_host(unsigned id, struct queryhost *host)
void process_host(unsigned id, struct queryhost *host, unsigned max_errors_per_host)
{
struct clinsert **host_queries;

cllog(2, "Thread %u picked host '%s'.", id, host->host);

/* Poll the host, get back the list of inserts, and queue them all. */
host_queries = get_clinserts(host);
host_queries = get_clinserts(host, max_errors_per_host);
process_queries(id, host_queries);
free(host_queries);
}
Expand Down Expand Up @@ -166,7 +164,7 @@ void calculate_rate(time_t prev_time, unsigned long long prev_counter, time_t cu
}
}

struct clinsert **get_clinserts(struct queryhost *host)
struct clinsert **get_clinserts(struct queryhost *host, unsigned max_errors_per_host)
{
unsigned snmp_fail = 0;
unsigned snmp_success = 0;
Expand Down Expand Up @@ -213,7 +211,7 @@ struct clinsert **get_clinserts(struct queryhost *host)
errors++;
snmp_fail++;
}
if (errors >= MAXERRORSPERHOST) {
if (errors >= max_errors_per_host) {
cllog(0, "Too many errors for host %s, aborted.", host->host);
break;
}
Expand Down
3 changes: 2 additions & 1 deletion src/poller.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ struct queryhost;
/** Thread context for poller thread. */
struct poller_ctx {
struct rtgtargets *targets; /**< The targets to poll. */
unsigned max_errors_per_host; /**< Maximum errors per host before we stop polling the host. */
};

/**
Expand All @@ -26,7 +27,7 @@ struct poller_ctx {
* @param host The host to query.
* @return A NULL-terminated list of clinserts.
*/
struct clinsert **get_clinserts(struct queryhost *host);
struct clinsert **get_clinserts(struct queryhost *host, unsigned max_errors_per_host);

/**
* Main loop for the poller thread.
Expand Down
1 change: 1 addition & 0 deletions src/rtgconf.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ struct rtgconf {
int allow_db_zero; /**< True if we should insert zero rates in the database. */
unsigned num_dbthreads; /**< The number of database threads. */
unsigned max_db_queue; /**< The maximum database queue depth. */
unsigned max_errors_per_host; /**< Maximum number of errors per host before we stop polling that host. */
};

/**
Expand Down
4 changes: 2 additions & 2 deletions test/integrationtests.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ void TestResultSetForOneHost(CuTest *tc)
struct rtgconf *conf = rtgconf_create("test/example-rtg.conf");
struct rtgtargets *hosts = rtgtargets_parse("test/example-targets.cfg", conf);

struct clinsert **inserts = get_clinserts(hosts->hosts[0]);
struct clinsert **inserts = get_clinserts(hosts->hosts[0], 3);
sleep(1);
inserts = get_clinserts(hosts->hosts[0]);
inserts = get_clinserts(hosts->hosts[0], 3);

CuAssertTrue(tc, NULL != inserts[0]); /* One table */
CuAssertTrue(tc, NULL == inserts[1]); /* Not two tables */
Expand Down
8 changes: 4 additions & 4 deletions test/longtests.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ void TestMeasureOneHostsAt10MbpsForTenSeconds(CuTest *tc)
struct rtgconf *conf = rtgconf_create("test/example-rtg.conf");
struct rtgtargets *hosts = rtgtargets_parse("test/example-targets.cfg", conf);

struct clinsert **queries = get_clinserts(hosts->hosts[0]);
struct clinsert **queries = get_clinserts(hosts->hosts[0], 3);
unsigned queries_size;
for (queries_size = 0; queries[queries_size]; queries_size++) ;

CuAssertIntEquals(tc, 0, queries_size); /* No inserts first iteration */

sleep(10);

queries = get_clinserts(hosts->hosts[0]);
queries = get_clinserts(hosts->hosts[0], 3);
for (queries_size = 0; queries[queries_size]; queries_size++) ;

CuAssertIntEquals(tc, 1, queries_size); /* One insert next iteration */
Expand All @@ -38,15 +38,15 @@ void TestMeasureOneHostAt100MbpsForOneInterval(CuTest *tc)
struct rtgconf *conf = rtgconf_create("test/example-rtg.conf");
struct rtgtargets *hosts = rtgtargets_parse("test/example-targets.cfg", conf);

struct clinsert **queries = get_clinserts(hosts->hosts[0]);
struct clinsert **queries = get_clinserts(hosts->hosts[0], 3);
unsigned queries_size;
for (queries_size = 0; queries[queries_size]; queries_size++) ;

CuAssertIntEquals(tc, 0, queries_size); /* No inserts first iteration */

sleep(conf->interval);

queries = get_clinserts(hosts->hosts[0]);
queries = get_clinserts(hosts->hosts[0], 3);
for (queries_size = 0; queries[queries_size]; queries_size++) ;
CuAssertIntEquals(tc, 0, queries_size); /* No inserts next iteration due to too high speed */
}
Expand Down

0 comments on commit 2f3ce1b

Please sign in to comment.