int test_cb_service_check_processed() { assert(OK == neb_register_callback(NEBCALLBACK_SERVICE_CHECK_DATA, test_nebmodule->module_handle, 0, _test_cb)); event_broker_options = BROKER_EVERYTHING; check_result *cr = check_result_new(0, "Some output"); host *host = host_new("MyHost"); service *service = service_new(host, "MyService"); nebstruct_service_check_data *ds = NULL; assert(OK == handle_async_service_check_result(service, cr)); ds = (nebstruct_service_check_data *) received_callback_data[NEBCALLBACK_SERVICE_CHECK_DATA][NEBTYPE_SERVICECHECK_PROCESSED]; ok(ds != NULL, "SERVICE_CHECK_DATA callback invoked"); ok(ds->type == NEBTYPE_SERVICECHECK_PROCESSED, "nebstruct has expected type") || diag("Type was %d", ds->type); ok(!strcmp(ds->host_name, "MyHost"), "nebstruct has expected hostname"); ok(!strcmp(ds->service_description, "MyService"), "nebstruct has expected service description"); ok(ds->attr == NEBATTR_NONE, "nebstruct has no attributes set"); clear_callback_data(); /* test stalking */ /* a change in plugin output should result in the NEBATTR_CHECK_ALERT attribute being set * for a service, for which stalking is enabled * * This output change is emulated implicitly by our fixture where service->plugin_output * is "Initial state" and cr->output is "Some output" * */ service->stalking_options |= ~0; /*stalk all the states*/ assert(OK == handle_async_service_check_result(service, cr)); ds = (nebstruct_service_check_data *) received_callback_data[NEBCALLBACK_SERVICE_CHECK_DATA][NEBTYPE_SERVICECHECK_PROCESSED]; ok(ds->attr == NEBATTR_CHECK_ALERT, "nebstruct has NEBATTR_CHECK_ALERT attribute set"); clear_callback_data(); check_result_destroy(cr); service_destroy(service); host_destroy(host); return 0; }
int test_cb_host_check_processed() { assert(OK == neb_register_callback(NEBCALLBACK_HOST_CHECK_DATA, test_nebmodule->module_handle, 0, _test_cb)); event_broker_options = BROKER_EVERYTHING; check_result *cr = check_result_new(0, "Some output"); host *host = host_new("MyHost"); nebstruct_host_check_data *ds = NULL; assert(OK == handle_async_host_check_result(host, cr)); ds = (nebstruct_host_check_data *) received_callback_data[NEBCALLBACK_HOST_CHECK_DATA][NEBTYPE_HOSTCHECK_PROCESSED]; ok(ds != NULL, "HOST_CHECK_DATA callback invoked"); ok(ds->type == NEBTYPE_HOSTCHECK_PROCESSED, "nebstruct has expected type") || diag("Type was %d", ds->type); ok(!strcmp(ds->host_name, "MyHost"), "nebstruct has expected hostname"); ok(ds->attr == NEBATTR_NONE, "nebstruct has no attributes set"); clear_callback_data(); /* test stalking */ /* a change in plugin output should result in the NEBATTR_CHECK_ALERT attribute being set * for a host, for which stalking is enabled * */ host->plugin_output = strdup("Initial state"); host->stalking_options |= ~0; /*stalk all the states*/ assert(OK == handle_async_host_check_result(host, cr)); ds = (nebstruct_host_check_data *) received_callback_data[NEBCALLBACK_HOST_CHECK_DATA][NEBTYPE_HOSTCHECK_PROCESSED]; ok(ds->attr == NEBATTR_CHECK_ALERT, "nebstruct has NEBATTR_CHECK_ALERT attribute set"); clear_callback_data(); check_result_destroy(cr); host_destroy(host); return 0; }
/* * routine for adding elements in the existing hostlist * linked list. */ host* host_add(host *hst, char *name, char *user) { if (!hst) return(host_new(name, user)); hst->next = host_add(hst->next, name, user); return(hst->next); }
/* * routine for adding elements in the existing hostlist * linked list. */ static struct host* host_add(struct host *hst, char *user, char *host, uint16_t port) { if (hst == NULL) return(host_new(user, host, port)); hst->next = host_add(hst->next, user, host, port); return(hst->next); }
static int on_message_ready(GIOChannel *source, GIOCondition cond, void *user_data) { state_t *app = (state_t*) user_data; char buf[65536]; struct sockaddr_in from; socklen_t fromlen = sizeof(struct sockaddr); int sz = recvfrom(app->recvfd, buf, 65536, 0, (struct sockaddr*) &from, &fromlen); if (sz < 0) { perror("receiving message"); return TRUE; } int64_t recv_utime = timestamp_now(); time_t recv_t = recv_utime / 1000000; struct tm *recv_tm = localtime(&recv_t); char recv_tm_buf[200]; strftime(recv_tm_buf, sizeof(recv_tm_buf), "%b %d %H:%M:%S", recv_tm); int *key = (int*) &from.sin_addr.s_addr; host_t * host = g_hash_table_lookup(app->hosts, key); if(!host) { host = host_new(from.sin_addr); g_hash_table_insert(app->hosts, &host->addr.s_addr, host); printf("%s - new host detected! %s\n", recv_tm_buf, host->addr_str); } sender_t *sender = host_get_sender(host, ntohs(from.sin_port)); if(!sender) { sender = host_add_new_sender(host, ntohs(from.sin_port)); printf("%s - new sender detected! %s\n", recv_tm_buf, sender->id_str); } sender->last_recvtime = recv_utime; host->last_recvtime = recv_utime; return TRUE; }
void gui_host_connect(GtkWidget *w, gpointer dialog) { gchar *text; gpointer host; GtkEntry *user_entry, *host_entry; user_entry = dialog_child_get(dialog, "user"); host_entry = dialog_child_get(dialog, "host"); text = g_strdup_printf("%s@%s", gtk_entry_get_text(user_entry), gtk_entry_get_text(host_entry)); host = host_new(text); g_free(text); if (host_connect(host)) printf("host_connect(): success!\n"); else printf("host_connect(): failed!\n"); gui_host_update(host, dialog); }
/* * routine that reads the host from a file and puts them * in the hostlist linked list using the above two routines */ host* host_readlist(char *fname) { FILE *hstlist; host *head; host *hst; char line[MAXNAME]; char host_line[MAXNAME]; char user[MAXUSER]; int linelen; if (!fname) return(NULL); hstlist = fopen(fname, "r"); if (!hstlist) return(NULL); head = host_new("", NULL); hst = head; char tok[] = "@"; char *res = NULL; while (fgets(line, sizeof(line), hstlist)) { if (sscanf(line, "%[A-Za-z0-9-.@]", line) != 1) continue; if (strpbrk(line, "@") != NULL) { res = strtok(line, tok); int i = 0; while (res != NULL) { if (i == 0) { strncpy(user, res, sizeof(user)); } else { res = strtok(NULL, tok); if (res == NULL) continue; strncpy(host_line, res, sizeof(host_line)); } i++; } hst = host_add(hst, host_line, user); linelen = strlen(host_line); } else { hst = host_add(hst, line, NULL); linelen = strlen(line); } /* keep track of the longest line */ if (linelen > host_len_max) host_len_max = linelen; hostcount++; } fclose(hstlist); hst = head->next; free(head); if (maxchld > hostcount) maxchld = hostcount; return(hst); }
Host *db_read_host(MYSQL_ROW row, MYSQL_FIELD *fields, gint num_fields, Host *default_host) { Host *host; gchar **tokens, **tok; gint i; host = host_new(); if (default_host != NULL && BUS_IS_HOST(default_host)) { host_set_user(host, host_get_user(default_host)); host_set_email(host, host_get_email(default_host)); host_set_backupdir(host, host_get_backupdir(default_host)); host_set_archivedir(host, host_get_archivedir(default_host)); host_set_max_age(host, host_get_max_age(default_host)); host_set_max_age_incr(host, host_get_max_age_incr(default_host)); host_set_max_age_full(host, host_get_max_age_full(default_host)); host_set_max_incr(host, host_get_max_incr(default_host)); host_set_rsync_opts(host, host_get_rsync_opts(default_host)); host->excludes = g_list_clone(default_host->excludes); host->schedule = g_list_clone(default_host->schedule); host->srcdirs = g_list_clone(default_host->srcdirs); host->ips = g_list_clone(default_host->ips); } for (i = 0; i < num_fields; i++) { if (g_strcmp0(fields[i].name, "id") == 0) { host_set_mysql_id(host, atoi(row[i])); } if (g_strcmp0(fields[i].name, "name") == 0) { if (row[i] == 0 || strlen(row[i]) == 0) { syslog(LOG_ERR, "Invalid name"); g_object_unref(host); continue; } host_set_name(host, row[i]); } if (g_strcmp0(fields[i].name, "hostname") == 0) { if (row[i] == NULL || strlen(row[i]) == 0) { syslog(LOG_ERR, "Invalid hostname"); g_object_unref(host); continue; } host_set_hostname(host, row[i]); } if (g_strcmp0(fields[i].name, "user") == 0) { host_set_user(host, row[i] ? row[i] : DEFAULT_USER); } if (g_strcmp0(fields[i].name, "email") == 0) { if (row[i] != NULL) { host_set_email(host, row[i]); } } if (g_strcmp0(fields[i].name, "max_incr") == 0) { host_set_max_incr(host, row[i] ? atoi(row[i]) : DEFAULT_MAX_INCR); } if (g_strcmp0(fields[i].name, "max_age") == 0) { host_set_max_age(host, row[i] ? atof(row[i]) : DEFAULT_MAX_AGE); } if (g_strcmp0(fields[i].name, "max_age_incr") == 0) { host_set_max_age_incr(host, row[i] ? atof(row[i]) : DEFAULT_MAX_AGE_INCR); } if (g_strcmp0(fields[i].name, "max_age_full") == 0) { host_set_max_age_full(host, row[i] ? atof(row[i]) : DEFAULT_MAX_AGE_FULL); } if (g_strcmp0(fields[i].name, "rsync_opts") == 0) { host_set_rsync_opts(host, row[i] ? row[i] : DEFAULT_RSYNC_OPTS); } if (g_strcmp0(fields[i].name, "archivedir") == 0) { host_set_archivedir(host, row[i] ? row[i] : ""); } if (g_strcmp0(fields[i].name, "backupdir") == 0) { host_set_backupdir(host, row[i] ? row[i] : DEFAULT_BACKUPDIR); } if (g_strcmp0(fields[i].name, "schedule") == 0) { if (row[i] != NULL) { tokens = g_strsplit(row[i], "\n", 0); for (tok = tokens; *tok != NULL; tok++) { gchar *str = g_strstrip(*tok); if (str != NULL && strlen(str) > 0) { host_add_schedule(host, str); } } g_strfreev(tokens); } } if (g_strcmp0(fields[i].name, "excludes") == 0) { tokens = g_strsplit(row[i], "\n", 0); for (tok = tokens; *tok != NULL; tok++) { gchar *str = g_strstrip(*tok); if (str != NULL && strlen(str) > 0) { host_add_exclude(host, str); } } g_strfreev(tokens); } if (g_strcmp0(fields[i].name, "ips") == 0) { tokens = g_strsplit(row[i], "\n", 0); for (tok = tokens; *tok != NULL; tok++) { gchar *str; str = g_strstrip(*tok); if (str != NULL && strlen(str) > 0 && is_valid_ip(str)) { host_add_ip(host, str); } } g_strfreev(tokens); } if (g_strcmp0(fields[i].name, "srcdirs") == 0) { tokens = g_strsplit(row[i], "\n", 0); for (tok = tokens; *tok != NULL; tok++) { gchar *str = g_strstrip(*tok); if (str != NULL && strlen(str) > 0) { host_add_srcdir(host, str); } } g_strfreev(tokens); } } return (host); }
int main (int argc, char *argv[]) { Host host = host_new(); host_connect(&host, "localhost", 7101, 7102, "localhost:7103"); printf("Testing command..."); assert(command_test(&host)); printf("done\n"); // printf("Testing directory..."); // assert(directory_test(&host)); // printf("done\n"); // print("Testing file..."); // assert(file_test(&host)); // print("done\n"); // // print("Testing package..."); // assert(package_test(&host)); // print("done\n"); // // print("Testing service..."); // assert(service_test(&host)); // print("done\n"); // // print("Testing telemetry..."); // assert(telemetry_test(&host)); // print("done\n"); printf("ALL TESTS PASSED. HI-DIDDLY-HO NEIGHBOUR-EENO!\n\n"); printf(" .sS$$$$$$$$$$$$$$Ss." " .$$$$$$$$$$$$$$$$$$$$$$s." " $$$$$$$$$$$$$$$$$$$$$$$$S." " $$$$$$$$$$$$$$$$$$$$$$$$$$s." " S$$$$' `$$$$$$$$$$$$$" " `$$' `$$$$$$$$$$$." " : `$$$$$$$$$$$" " : `$$$$$$$$$$" ".====. ,=====. $$$$$$$$$$" ".' ~' \". s$$$$$$$$$$" ": : :=_ $$$$$$$$$$$" "`. () : () ' ~=$$$$$$$$$$$'" "~====~`. .' $$$$$$$$$$$" " .' ~====~ sS$$$$$$$$$'" " : . $$$$$' $$$$" ".sS$$$$$$$$Ss. `$$' $$$'" "$$$$$$$$$$$$$$$s s$$$$" "$SSSSSSSSSSSSSSS$ $$$$$" " : $$$$'" " `. $$$'" " `. :" " : :" " : .'`." " .'. .' :" " : .$s. .' .'" " :.S$$$S. .' .'" " : $$$$$$`.' .'" " $$$$ `. .'" " `"); return 0; }