/** * Start the instance of the monitor, returning a handle on the monitor. * * This function creates a thread to execute the actual monitoring. * * @return A handle to use when interacting with the monitor */ static void * startMonitor(void *arg, void* opt) { MONITOR* mon = arg; GALERA_MONITOR *handle = mon->handle; CONFIG_PARAMETER* params = (CONFIG_PARAMETER*) opt; bool have_events = false, script_error = false; if (handle != NULL) { handle->shutdown = 0; } else { if ((handle = (GALERA_MONITOR *) malloc(sizeof(GALERA_MONITOR))) == NULL) { return NULL; } handle->shutdown = 0; handle->id = MONITOR_DEFAULT_ID; handle->disableMasterFailback = 0; handle->availableWhenDonor = 0; handle->disableMasterRoleSetting = 0; handle->master = NULL; handle->script = NULL; handle->use_priority = false; memset(handle->events, false, sizeof(handle->events)); spinlock_init(&handle->lock); } while (params) { if (!strcmp(params->name, "disable_master_failback")) { handle->disableMasterFailback = config_truth_value(params->value); } else if (!strcmp(params->name, "available_when_donor")) { handle->availableWhenDonor = config_truth_value(params->value); } else if (!strcmp(params->name, "disable_master_role_setting")) { handle->disableMasterRoleSetting = config_truth_value(params->value); } else if (!strcmp(params->name, "use_priority")) { handle->use_priority = config_truth_value(params->value); } else if (!strcmp(params->name, "script")) { if (externcmd_can_execute(params->value)) { free(handle->script); handle->script = strdup(params->value); } else { script_error = true; } } else if (!strcmp(params->name, "events")) { if (mon_parse_event_string((bool*) & handle->events, sizeof(handle->events), params->value) != 0) { script_error = true; } else { have_events = true; } } params = params->next; } if (script_error) { MXS_ERROR("Errors were found in the script configuration parameters " "for the monitor '%s'. The script will not be used.", mon->name); free(handle->script); handle->script = NULL; } /** If no specific events are given, enable them all */ if (!have_events) { memset(handle->events, true, sizeof(handle->events)); } handle->tid = (THREAD) thread_start(monitorMain, mon); return handle; }
/** * Create an instance of the filter for a particular service * within MaxScale. * * @param options The options for this filter * * @return The instance data for this new instance */ static FILTER * createInstance(char **options, FILTER_PARAMETER **params) { MQ_INSTANCE *my_instance; int paramcount = 0, parammax = 64, i = 0, x = 0, arrsize = 0; FILTER_PARAMETER** paramlist; char** arr; char taskname[512]; if ((my_instance = calloc(1, sizeof(MQ_INSTANCE)))) { spinlock_init(&my_instance->rconn_lock); spinlock_init(&my_instance->msg_lock); uid_gen = 0; paramlist = malloc(sizeof(FILTER_PARAMETER*)*64); if((my_instance->conn = amqp_new_connection()) == NULL){ return NULL; } my_instance->channel = 1; my_instance->last_rconn = time(NULL); my_instance->conn_stat = AMQP_STATUS_OK; my_instance->rconn_intv = 1; my_instance->port = 5672; my_instance->trgtype = TRG_ALL; my_instance->log_all = false; my_instance->strict_logging = true; for(i = 0;params[i];i++){ if(!strcmp(params[i]->name,"hostname")){ my_instance->hostname = strdup(params[i]->value); }else if(!strcmp(params[i]->name,"username")){ my_instance->username = strdup(params[i]->value); }else if(!strcmp(params[i]->name,"password")){ my_instance->password = strdup(params[i]->value); }else if(!strcmp(params[i]->name,"vhost")){ my_instance->vhost = strdup(params[i]->value); }else if(!strcmp(params[i]->name,"port")){ my_instance->port = atoi(params[i]->value); }else if(!strcmp(params[i]->name,"exchange")){ my_instance->exchange = strdup(params[i]->value); }else if(!strcmp(params[i]->name,"key")){ my_instance->key = strdup(params[i]->value); }else if(!strcmp(params[i]->name,"queue")){ my_instance->queue = strdup(params[i]->value); } else if(!strcmp(params[i]->name,"ssl_client_certificate")){ my_instance->ssl_client_cert = strdup(params[i]->value); }else if(!strcmp(params[i]->name,"ssl_client_key")){ my_instance->ssl_client_key = strdup(params[i]->value); }else if(!strcmp(params[i]->name,"ssl_CA_cert")){ my_instance->ssl_CA_cert = strdup(params[i]->value); }else if(!strcmp(params[i]->name,"exchange_type")){ my_instance->exchange_type = strdup(params[i]->value); }else if(!strcmp(params[i]->name,"logging_trigger")){ arr = parse_optstr(params[i]->value,",",&arrsize); for(x = 0;x<arrsize;x++){ if(!strcmp(arr[x],"source")){ my_instance->trgtype |= TRG_SOURCE; }else if(!strcmp(arr[x],"schema")){ my_instance->trgtype |= TRG_SCHEMA; }else if(!strcmp(arr[x],"object")){ my_instance->trgtype |= TRG_OBJECT; }else if(!strcmp(arr[x],"all")){ my_instance->trgtype = TRG_ALL; }else{ skygw_log_write(LOGFILE_ERROR,"Error: Unknown option for 'logging_trigger':%s.",arr[x]); } } if(arrsize > 0){ free(arr); } arrsize = 0; }else if(strstr(params[i]->name,"logging_")){ if(paramcount < parammax){ paramlist[paramcount] = malloc(sizeof(FILTER_PARAMETER)); paramlist[paramcount]->name = strdup(params[i]->name); paramlist[paramcount]->value = strdup(params[i]->value); paramcount++; } } } if(my_instance->trgtype & TRG_SOURCE){ my_instance->src_trg = (SRC_TRIG*)malloc(sizeof(SRC_TRIG)); my_instance->src_trg->user = NULL; my_instance->src_trg->host = NULL; my_instance->src_trg->usize = 0; my_instance->src_trg->hsize = 0; } if(my_instance->trgtype & TRG_SCHEMA){ my_instance->shm_trg = (SHM_TRIG*)malloc(sizeof(SHM_TRIG)); my_instance->shm_trg->objects = NULL; my_instance->shm_trg->size = 0; } if(my_instance->trgtype & TRG_OBJECT){ my_instance->obj_trg = (OBJ_TRIG*)malloc(sizeof(OBJ_TRIG)); my_instance->obj_trg->objects = NULL; my_instance->obj_trg->size = 0; } for(i = 0;i<paramcount;i++){ if(!strcmp(paramlist[i]->name,"logging_source_user")){ if(my_instance->src_trg){ my_instance->src_trg->user = parse_optstr(paramlist[i]->value,",",&arrsize); my_instance->src_trg->usize = arrsize; arrsize = 0; } }else if(!strcmp(paramlist[i]->name,"logging_source_host")){ if(my_instance->src_trg){ my_instance->src_trg->host = parse_optstr(paramlist[i]->value,",",&arrsize); my_instance->src_trg->hsize = arrsize; arrsize = 0; } }else if(!strcmp(paramlist[i]->name,"logging_schema")){ if(my_instance->shm_trg){ my_instance->shm_trg->objects = parse_optstr(paramlist[i]->value,",",&arrsize); my_instance->shm_trg->size = arrsize; arrsize = 0; } }else if(!strcmp(paramlist[i]->name,"logging_object")){ if(my_instance->obj_trg){ my_instance->obj_trg->objects = parse_optstr(paramlist[i]->value,",",&arrsize); my_instance->obj_trg->size = arrsize; arrsize = 0; } }else if(!strcmp(paramlist[i]->name,"logging_log_all")){ if(config_truth_value(paramlist[i]->value)){ my_instance->log_all = true; } }else if(!strcmp(paramlist[i]->name,"logging_strict")){ if(!config_truth_value(paramlist[i]->value)){ my_instance->strict_logging = false; } } free(paramlist[i]->name); free(paramlist[i]->value); free(paramlist[i]); } free(paramlist); if(my_instance->hostname == NULL){ my_instance->hostname = strdup("localhost"); } if(my_instance->username == NULL){ my_instance->username = strdup("guest"); } if(my_instance->password == NULL){ my_instance->password = strdup("guest"); } if(my_instance->vhost == NULL){ my_instance->vhost = strdup("/"); } if(my_instance->exchange == NULL){ my_instance->exchange = strdup("default_exchange"); } if(my_instance->key == NULL){ my_instance->key = strdup("key"); } if(my_instance->exchange_type == NULL){ my_instance->exchange_type = strdup("direct"); } if(my_instance->ssl_client_cert != NULL && my_instance->ssl_client_key != NULL && my_instance->ssl_CA_cert != NULL){ my_instance->use_ssl = true; }else{ my_instance->use_ssl = false; } if(my_instance->use_ssl){ amqp_set_initialize_ssl_library(0);/**Assume the underlying SSL library is already initialized*/ } /**Connect to the server*/ init_conn(my_instance); snprintf(taskname,511,"mqtask%d",atomic_add(&hktask_id,1)); hktask_add(taskname,sendMessage,(void*)my_instance,5); } return (FILTER *)my_instance; }
/** * Start the instance of the monitor, returning a handle on the monitor. * * This function creates a thread to execute the actual monitoring. * * @param arg The current handle - NULL if first start * @return A handle to use when interacting with the monitor */ static void * startMonitor(void *arg,void* opt) { MONITOR* mon = (MONITOR*)arg; MM_MONITOR *handle = mon->handle; CONFIG_PARAMETER* params = (CONFIG_PARAMETER*)opt; bool have_events = false,script_error = false; if (handle) { handle->shutdown = 0; } else { if ((handle = (MM_MONITOR *)malloc(sizeof(MM_MONITOR))) == NULL) return NULL; handle->shutdown = 0; handle->id = MONITOR_DEFAULT_ID; handle->master = NULL; handle->script = NULL; memset(handle->events,false,sizeof(handle->events)); spinlock_init(&handle->lock); } while(params) { if(!strcmp(params->name,"detect_stale_master")) { handle->detectStaleMaster = config_truth_value(params->value); } else if(!strcmp(params->name,"script")) { if(handle->script) { free(handle->script); } if(access(params->value,X_OK) == 0) { handle->script = strdup(params->value); } else { script_error = true; if(access(params->value,F_OK) == 0) { skygw_log_write(LE, "Error: The file cannot be executed: %s", params->value); } else { skygw_log_write(LE, "Error: The file cannot be found: %s", params->value); } handle->script = NULL; } } else if(!strcmp(params->name,"events")) { if(mon_parse_event_string((bool*)&handle->events,sizeof(handle->events),params->value) != 0) script_error = true; else have_events = true; } params = params->next; } if(script_error) { skygw_log_write(LE,"Error: Errors were found in the script configuration parameters " "for the monitor '%s'. The script will not be used.",mon->name); free(handle->script); handle->script = NULL; } /** If no specific events are given, enable them all */ if(!have_events) { memset(handle->events,true,sizeof(handle->events)); } handle->tid = (THREAD)thread_start(monitorMain, mon); return handle; }