static int varnish_init (void) /* {{{ */ { user_config_t *conf; user_data_t ud; if (have_instance) return (0); conf = malloc (sizeof (*conf)); if (conf == NULL) return (ENOMEM); memset (conf, 0, sizeof (*conf)); /* Default settings: */ conf->instance = NULL; varnish_config_apply_default (conf); ud.data = conf; ud.free_func = varnish_config_free; plugin_register_complex_read (/* group = */ "varnish", /* name = */ "varnish/localhost", /* callback = */ varnish_read, /* interval = */ 0, /* user data = */ &ud); return (0); } /* }}} int varnish_init */
void module_register(void) { plugin_register_init(DPDK_STATS_PLUGIN, dpdk_stats_init); plugin_register_complex_config(DPDK_STATS_PLUGIN, dpdk_stats_config); plugin_register_complex_read(NULL, DPDK_STATS_PLUGIN, dpdk_stats_read, 0, NULL); plugin_register_shutdown(DPDK_STATS_PLUGIN, dpdk_stats_shutdown); }
static int lua_cb_register_read(lua_State *L) /* {{{ */ { int nargs = lua_gettop(L); if (nargs != 1) return luaL_error(L, "Invalid number of arguments (%d != 1)", nargs); luaL_checktype(L, 1, LUA_TFUNCTION); char function_name[DATA_MAX_NAME_LEN]; ssnprintf(function_name, sizeof(function_name), "lua/%s", lua_tostring(L, 1)); int callback_id = clua_store_callback(L, 1); if (callback_id < 0) return luaL_error(L, "%s", "Storing callback function failed"); lua_State *thread = lua_newthread(L); if (thread == NULL) return luaL_error(L, "%s", "lua_newthread failed"); clua_store_thread(L, -1); lua_pop(L, 1); clua_callback_data_t *cb = calloc(1, sizeof(*cb)); if (cb == NULL) return luaL_error(L, "%s", "calloc failed"); cb->lua_state = thread; cb->callback_id = callback_id; cb->lua_function_name = strdup(function_name); pthread_mutex_init(&cb->lock, NULL); user_data_t ud = { .data = cb }; int status = plugin_register_complex_read(/* group = */ "lua", /* name = */ function_name, /* callback = */ clua_read, /* interval = */ 0, /* user_data = */ &ud); if (status != 0) return luaL_error(L, "%s", "plugin_register_complex_read failed"); return 0; } /* }}} int lua_cb_register_read */
static int memcached_add_read_callback (memcached_t *st) { user_data_t ud; char callback_name[3*DATA_MAX_NAME_LEN]; int status; memset (&ud, 0, sizeof (ud)); ud.data = st; ud.free_func = (void *) memcached_free; assert (st->name != NULL); ssnprintf (callback_name, sizeof (callback_name), "memcached/%s", st->name); status = plugin_register_complex_read (/* group = */ "memcached", /* name = */ callback_name, /* callback = */ memcached_read, /* interval = */ 0, /* user_data = */ &ud); return (status); } /* int memcached_add_read_callback */
static int ctail_init (void) { char str[255]; user_data_t ud = { 0 }; size_t i; if (tail_match_list_num == 0) { WARNING ("tail plugin: File list is empty. Returning an error."); return (-1); } for (i = 0; i < tail_match_list_num; i++) { ud.data = (void *)tail_match_list[i]; ssnprintf(str, sizeof(str), "tail-%zu", i); plugin_register_complex_read (NULL, str, ctail_read, tail_match_list_intervals[i], &ud); } return (0); } /* int ctail_init */
static int cj_config_add_url (oconfig_item_t *ci) /* {{{ */ { cj_t *db; int status = 0; int i; if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) { WARNING ("curl_json plugin: The `URL' block " "needs exactly one string argument."); return (-1); } db = (cj_t *) malloc (sizeof (*db)); if (db == NULL) { ERROR ("curl_json plugin: malloc failed."); return (-1); } memset (db, 0, sizeof (*db)); if (strcasecmp ("URL", ci->key) == 0) status = cf_util_get_string (ci, &db->url); else if (strcasecmp ("Sock", ci->key) == 0) status = cf_util_get_string (ci, &db->sock); else { ERROR ("curl_json plugin: cj_config: " "Invalid key: %s", ci->key); return (-1); } if (status != 0) { sfree (db); return (status); } /* Fill the `cj_t' structure.. */ for (i = 0; i < ci->children_num; i++) { oconfig_item_t *child = ci->children + i; if (strcasecmp ("Instance", child->key) == 0) status = cf_util_get_string (child, &db->instance); else if (strcasecmp ("Host", child->key) == 0) status = cf_util_get_string (child, &db->host); else if (db->url && strcasecmp ("User", child->key) == 0) status = cf_util_get_string (child, &db->user); else if (db->url && strcasecmp ("Password", child->key) == 0) status = cf_util_get_string (child, &db->pass); else if (strcasecmp ("Digest", child->key) == 0) status = cf_util_get_boolean (child, &db->digest); else if (db->url && strcasecmp ("VerifyPeer", child->key) == 0) status = cf_util_get_boolean (child, &db->verify_peer); else if (db->url && strcasecmp ("VerifyHost", child->key) == 0) status = cf_util_get_boolean (child, &db->verify_host); else if (db->url && strcasecmp ("CACert", child->key) == 0) status = cf_util_get_string (child, &db->cacert); else if (db->url && strcasecmp ("Header", child->key) == 0) status = cj_config_append_string ("Header", &db->headers, child); else if (db->url && strcasecmp ("Post", child->key) == 0) status = cf_util_get_string (child, &db->post_body); else if (strcasecmp ("Key", child->key) == 0) status = cj_config_add_key (db, child); else if (strcasecmp ("Interval", child->key) == 0) status = cf_util_get_cdtime(child, &db->interval); else { WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key); status = -1; } if (status != 0) break; } if (status == 0) { if (db->tree == NULL) { WARNING ("curl_json plugin: No (valid) `Key' block within `%s' \"`%s'\".", db->url ? "URL" : "Sock", db->url ? db->url : db->sock); status = -1; } if (status == 0 && db->url) status = cj_init_curl (db); } /* If all went well, register this database for reading */ if (status == 0) { user_data_t ud; char *cb_name; struct timespec interval = { 0, 0 }; CDTIME_T_TO_TIMESPEC (db->interval, &interval); if (db->instance == NULL) db->instance = strdup("default"); DEBUG ("curl_json plugin: Registering new read callback: %s", db->instance); memset (&ud, 0, sizeof (ud)); ud.data = (void *) db; ud.free_func = cj_free; cb_name = ssnprintf_alloc ("curl_json-%s-%s", db->instance, db->url ? db->url : db->sock); plugin_register_complex_read (/* group = */ NULL, cb_name, cj_read, /* interval = */ (db->interval > 0) ? &interval : NULL, &ud); sfree (cb_name); } else { cj_free (db); return (-1); } return (0); }
/* Configuration handling functions {{{ * * <Plugin mysql> * <Database "plugin_instance1"> * Host "localhost" * Port 22000 * ... * </Database> * </Plugin> */ static int mysql_config_database (oconfig_item_t *ci) /* {{{ */ { mysql_database_t *db; int status = 0; int i; if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) { WARNING ("mysql plugin: The `Database' block " "needs exactly one string argument."); return (-1); } db = (mysql_database_t *) malloc (sizeof (*db)); if (db == NULL) { ERROR ("mysql plugin: malloc failed."); return (-1); } memset (db, 0, sizeof (*db)); /* initialize all the pointers */ db->host = NULL; db->user = NULL; db->pass = NULL; db->database = NULL; db->socket = NULL; db->con = NULL; /* trigger a notification, if it's not running */ db->slave_io_running = 1; db->slave_sql_running = 1; status = cf_util_get_string (ci, &db->instance); if (status != 0) { sfree (db); return (status); } assert (db->instance != NULL); /* Fill the `mysql_database_t' structure.. */ for (i = 0; i < ci->children_num; i++) { oconfig_item_t *child = ci->children + i; if (strcasecmp ("Host", child->key) == 0) status = cf_util_get_string (child, &db->host); else if (strcasecmp ("User", child->key) == 0) status = cf_util_get_string (child, &db->user); else if (strcasecmp ("Password", child->key) == 0) status = cf_util_get_string (child, &db->pass); else if (strcasecmp ("Port", child->key) == 0) { status = cf_util_get_port_number (child); if (status > 0) { db->port = status; status = 0; } } else if (strcasecmp ("Socket", child->key) == 0) status = cf_util_get_string (child, &db->socket); else if (strcasecmp ("Database", child->key) == 0) status = cf_util_get_string (child, &db->database); else if (strcasecmp ("MasterStats", child->key) == 0) status = cf_util_get_boolean (child, &db->master_stats); else if (strcasecmp ("SlaveStats", child->key) == 0) status = cf_util_get_boolean (child, &db->slave_stats); else if (strcasecmp ("SlaveNotifications", child->key) == 0) status = cf_util_get_boolean (child, &db->slave_notif); else { WARNING ("mysql plugin: Option `%s' not allowed here.", child->key); status = -1; } if (status != 0) break; } /* If all went well, register this database for reading */ if (status == 0) { user_data_t ud; char cb_name[DATA_MAX_NAME_LEN]; DEBUG ("mysql plugin: Registering new read callback: %s", (db->database != NULL) ? db->database : "<default>"); memset (&ud, 0, sizeof (ud)); ud.data = (void *) db; ud.free_func = mysql_database_free; if (db->instance != NULL) ssnprintf (cb_name, sizeof (cb_name), "mysql-%s", db->instance); else sstrncpy (cb_name, "mysql", sizeof (cb_name)); plugin_register_complex_read (/* group = */ NULL, cb_name, mysql_read, /* interval = */ NULL, &ud); } else { mysql_database_free (db); return (-1); } return (0); } /* }}} int mysql_config_database */
static int cdbi_config_add_database (oconfig_item_t *ci) /* {{{ */ { cdbi_database_t *db; int status; int i; if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) { WARNING ("dbi plugin: The `Database' block " "needs exactly one string argument."); return (-1); } db = calloc (1, sizeof (*db)); if (db == NULL) { ERROR ("dbi plugin: calloc failed."); return (-1); } status = cf_util_get_string (ci, &db->name); if (status != 0) { sfree (db); return (status); } /* Fill the `cdbi_database_t' structure.. */ for (i = 0; i < ci->children_num; i++) { oconfig_item_t *child = ci->children + i; if (strcasecmp ("Driver", child->key) == 0) status = cf_util_get_string (child, &db->driver); else if (strcasecmp ("DriverOption", child->key) == 0) status = cdbi_config_add_database_driver_option (db, child); else if (strcasecmp ("SelectDB", child->key) == 0) status = cf_util_get_string (child, &db->select_db); else if (strcasecmp ("Query", child->key) == 0) status = udb_query_pick_from_list (child, queries, queries_num, &db->queries, &db->queries_num); else if (strcasecmp ("Host", child->key) == 0) status = cf_util_get_string (child, &db->host); else if (strcasecmp ("Interval", child->key) == 0) status = cf_util_get_cdtime(child, &db->interval); else { WARNING ("dbi plugin: Option `%s' not allowed here.", child->key); status = -1; } if (status != 0) break; } /* Check that all necessary options have been given. */ while (status == 0) { if (db->driver == NULL) { WARNING ("dbi plugin: `Driver' not given for database `%s'", db->name); status = -1; } if (db->driver_options_num == 0) { WARNING ("dbi plugin: No `DriverOption' given for database `%s'. " "This will likely not work.", db->name); } break; } /* while (status == 0) */ while ((status == 0) && (db->queries_num > 0)) { size_t j; db->q_prep_areas = calloc (db->queries_num, sizeof (*db->q_prep_areas)); if (db->q_prep_areas == NULL) { WARNING ("dbi plugin: calloc failed"); status = -1; break; } for (j = 0; j < db->queries_num; ++j) { db->q_prep_areas[j] = udb_query_allocate_preparation_area (db->queries[j]); if (db->q_prep_areas[j] == NULL) { WARNING ("dbi plugin: udb_query_allocate_preparation_area failed"); status = -1; break; } } break; } /* If all went well, add this database to the global list of databases. */ if (status == 0) { cdbi_database_t **temp; temp = realloc (databases, sizeof (*databases) * (databases_num + 1)); if (temp == NULL) { ERROR ("dbi plugin: realloc failed"); status = -1; } else { user_data_t ud; char *name = NULL; databases = temp; databases[databases_num] = db; databases_num++; memset (&ud, 0, sizeof (ud)); ud.data = (void *) db; ud.free_func = NULL; name = ssnprintf_alloc("dbi:%s", db->name); plugin_register_complex_read (/* group = */ NULL, /* name = */ name ? name : db->name, /* callback = */ cdbi_read_database, /* interval = */ (db->interval > 0) ? db->interval : 0, /* user_data = */ &ud); free (name); } } if (status != 0) { cdbi_database_free (db); return (-1); } return (0); } /* }}} int cdbi_config_add_database */
static int config_add (oconfig_item_t *ci) { apache_t *st; int i; int status; if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) { WARNING ("apache plugin: The `%s' config option " "needs exactly one string argument.", ci->key); return (-1); } st = (apache_t *) malloc (sizeof (*st)); if (st == NULL) { ERROR ("apache plugin: malloc failed."); return (-1); } memset (st, 0, sizeof (*st)); status = config_set_string (&st->name, ci); if (status != 0) { sfree (st); return (status); } assert (st->name != NULL); for (i = 0; i < ci->children_num; i++) { oconfig_item_t *child = ci->children + i; if (strcasecmp ("URL", child->key) == 0) status = config_set_string (&st->url, child); else if (strcasecmp ("Host", child->key) == 0) status = config_set_string (&st->host, child); else if (strcasecmp ("User", child->key) == 0) status = config_set_string (&st->user, child); else if (strcasecmp ("Password", child->key) == 0) status = config_set_string (&st->pass, child); else if (strcasecmp ("VerifyPeer", child->key) == 0) status = config_set_boolean (&st->verify_peer, child); else if (strcasecmp ("VerifyHost", child->key) == 0) status = config_set_boolean (&st->verify_host, child); else if (strcasecmp ("CACert", child->key) == 0) status = config_set_string (&st->cacert, child); else if (strcasecmp ("Server", child->key) == 0) status = config_set_string (&st->server, child); else { WARNING ("apache plugin: Option `%s' not allowed here.", child->key); status = -1; } if (status != 0) break; } /* Check if struct is complete.. */ if ((status == 0) && (st->url == NULL)) { ERROR ("apache plugin: Instance `%s': " "No URL has been configured.", st->name); status = -1; } if (status == 0) { user_data_t ud; char callback_name[3*DATA_MAX_NAME_LEN]; memset (&ud, 0, sizeof (ud)); ud.data = st; ud.free_func = (void *) apache_free; memset (callback_name, 0, sizeof (callback_name)); ssnprintf (callback_name, sizeof (callback_name), "apache/%s/%s", (st->host != NULL) ? st->host : hostname_g, (st->name != NULL) ? st->name : "default"), status = plugin_register_complex_read (/* group = */ NULL, /* name = */ callback_name, /* callback = */ apache_read_host, /* interval = */ NULL, /* user_data = */ &ud); } if (status != 0) { apache_free(st); return (-1); } return (0); } /* int config_add */
static int cx_config_add_url (oconfig_item_t *ci) /* {{{ */ { cx_t *db; int status = 0; int i; if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) { WARNING ("curl_xml plugin: The `URL' block " "needs exactly one string argument."); return (-1); } db = (cx_t *) malloc (sizeof (*db)); if (db == NULL) { ERROR ("curl_xml plugin: malloc failed."); return (-1); } memset (db, 0, sizeof (*db)); db->timeout = -1; if (strcasecmp ("URL", ci->key) == 0) { status = cf_util_get_string (ci, &db->url); if (status != 0) { sfree (db); return (status); } } else { ERROR ("curl_xml plugin: cx_config: " "Invalid key: %s", ci->key); cx_free (db); return (-1); } /* Fill the `cx_t' structure.. */ for (i = 0; i < ci->children_num; i++) { oconfig_item_t *child = ci->children + i; if (strcasecmp ("Instance", child->key) == 0) status = cf_util_get_string (child, &db->instance); else if (strcasecmp ("Host", child->key) == 0) status = cf_util_get_string (child, &db->host); else if (strcasecmp ("User", child->key) == 0) status = cf_util_get_string (child, &db->user); else if (strcasecmp ("Password", child->key) == 0) status = cf_util_get_string (child, &db->pass); else if (strcasecmp ("Digest", child->key) == 0) status = cf_util_get_boolean (child, &db->digest); else if (strcasecmp ("VerifyPeer", child->key) == 0) status = cf_util_get_boolean (child, &db->verify_peer); else if (strcasecmp ("VerifyHost", child->key) == 0) status = cf_util_get_boolean (child, &db->verify_host); else if (strcasecmp ("CACert", child->key) == 0) status = cf_util_get_string (child, &db->cacert); else if (strcasecmp ("xpath", child->key) == 0) status = cx_config_add_xpath (db, child); else if (strcasecmp ("Header", child->key) == 0) status = cx_config_append_string ("Header", &db->headers, child); else if (strcasecmp ("Post", child->key) == 0) status = cf_util_get_string (child, &db->post_body); else if (strcasecmp ("Namespace", child->key) == 0) status = cx_config_add_namespace (db, child); else if (strcasecmp ("Timeout", child->key) == 0) status = cf_util_get_int (child, &db->timeout); else { WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key); status = -1; } if (status != 0) break; } if (status == 0) { if (db->list == NULL) { WARNING ("curl_xml plugin: No (valid) `Key' block " "within `URL' block `%s'.", db->url); status = -1; } if (status == 0) status = cx_init_curl (db); } /* If all went well, register this database for reading */ if (status == 0) { user_data_t ud; char *cb_name; if (db->instance == NULL) db->instance = strdup("default"); DEBUG ("curl_xml plugin: Registering new read callback: %s", db->instance); memset (&ud, 0, sizeof (ud)); ud.data = (void *) db; ud.free_func = cx_free; cb_name = ssnprintf_alloc ("curl_xml-%s-%s", db->instance, db->url); plugin_register_complex_read (/* group = */ "curl_xml", cb_name, cx_read, /* interval = */ 0, &ud); sfree (cb_name); } else { cx_free (db); return (-1); } return (0); } /* }}} int cx_config_add_url */
static int mysql_config (oconfig_item_t *ci) /* {{{ */ { mysql_database_t *db; int plugin_block; int status = 0; int i; if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) { WARNING ("mysql plugin: The `Database' block " "needs exactly one string argument."); return (-1); } db = (mysql_database_t *) malloc (sizeof (*db)); if (db == NULL) { ERROR ("mysql plugin: malloc failed."); return (-1); } memset (db, 0, sizeof (*db)); /* initialize all the pointers */ db->host = NULL; db->user = NULL; db->pass = NULL; db->database = NULL; db->socket = NULL; db->con = NULL; /* trigger a notification, if it's not running */ db->slave_io_running = 1; db->slave_sql_running = 1; plugin_block = 1; if (strcasecmp ("Plugin", ci->key) == 0) { db->instance = NULL; } else if (strcasecmp ("Database", ci->key) == 0) { plugin_block = 0; status = mysql_config_set_string (&db->instance, ci); if (status != 0) { sfree (db); return (status); } assert (db->instance != NULL); } else { ERROR ("mysql plugin: mysql_config: " "Invalid key: %s", ci->key); return (-1); } /* Fill the `mysql_database_t' structure.. */ for (i = 0; i < ci->children_num; i++) { oconfig_item_t *child = ci->children + i; if (strcasecmp ("Host", child->key) == 0) status = mysql_config_set_string (&db->host, child); else if (strcasecmp ("User", child->key) == 0) status = mysql_config_set_string (&db->user, child); else if (strcasecmp ("Password", child->key) == 0) status = mysql_config_set_string (&db->pass, child); else if (strcasecmp ("Port", child->key) == 0) status = mysql_config_set_int (&db->port, child); else if (strcasecmp ("Socket", child->key) == 0) status = mysql_config_set_string (&db->socket, child); /* Check if we're currently handling the `Plugin' block. If so, * handle `Database' _blocks_, too. */ else if ((plugin_block != 0) && (strcasecmp ("Database", child->key) == 0) && (child->children != NULL)) { /* If `plugin_block > 1', there has been at least one * `Database' block */ plugin_block++; status = mysql_config (child); } /* Now handle ordinary `Database' options (without children) */ else if ((strcasecmp ("Database", child->key) == 0) && (child->children == NULL)) status = mysql_config_set_string (&db->database, child); else if (strcasecmp ("MasterStats", child->key) == 0) status = mysql_config_set_boolean (&db->master_stats, child); else if (strcasecmp ("SlaveStats", child->key) == 0) status = mysql_config_set_boolean (&db->slave_stats, child); else if (strcasecmp ("SlaveNotifications", child->key) == 0) status = mysql_config_set_boolean (&db->slave_notif, child); else { WARNING ("mysql plugin: Option `%s' not allowed here.", child->key); status = -1; } if (status != 0) break; } /* Check if there were any `Database' blocks. */ if (plugin_block > 1) { /* There were connection blocks. Don't use any legacy stuff. */ if ((db->host != NULL) || (db->user != NULL) || (db->pass != NULL) || (db->database != NULL) || (db->socket != NULL) || (db->port != 0)) { WARNING ("mysql plugin: At least one <Database> " "block has been found. The legacy " "configuration will be ignored."); } mysql_database_free (db); return (0); } else if (plugin_block != 0) { WARNING ("mysql plugin: You're using the legacy " "configuration options. Please consider " "updating your configuration!"); } /* Check that all necessary options have been given. */ while (status == 0) { /* Zero is allowed and automatically handled by * `mysql_real_connect'. */ if ((db->port < 0) || (db->port > 65535)) { ERROR ("mysql plugin: Database %s: Port number out " "of range: %i", (db->instance != NULL) ? db->instance : "<legacy>", db->port); status = -1; } break; } /* while (status == 0) */ /* If all went well, register this database for reading */ if (status == 0) { user_data_t ud; char cb_name[DATA_MAX_NAME_LEN]; DEBUG ("mysql plugin: Registering new read callback: %s", (db->database != NULL) ? db->database : "<default>"); memset (&ud, 0, sizeof (ud)); ud.data = (void *) db; ud.free_func = mysql_database_free; if (db->database != NULL) ssnprintf (cb_name, sizeof (cb_name), "mysql-%s", db->database); else sstrncpy (cb_name, "mysql", sizeof (cb_name)); plugin_register_complex_read (cb_name, mysql_read, /* interval = */ NULL, &ud); } else { mysql_database_free (db); return (-1); } return (0); } /* }}} int mysql_config */
static int varnish_config_instance (const oconfig_item_t *ci) /* {{{ */ { user_config_t *conf; user_data_t ud; char callback_name[DATA_MAX_NAME_LEN]; int i; conf = malloc (sizeof (*conf)); if (conf == NULL) return (ENOMEM); memset (conf, 0, sizeof (*conf)); conf->instance = NULL; varnish_config_apply_default (conf); if (ci->values_num == 1) { int status; status = cf_util_get_string (ci, &conf->instance); if (status != 0) { sfree (conf); return (status); } assert (conf->instance != NULL); if (strcmp ("localhost", conf->instance) == 0) { sfree (conf->instance); conf->instance = NULL; } } else if (ci->values_num > 1) { WARNING ("Varnish plugin: \"Instance\" blocks accept only " "one argument."); return (EINVAL); } for (i = 0; i < ci->children_num; i++) { oconfig_item_t *child = ci->children + i; if (strcasecmp ("CollectCache", child->key) == 0) cf_util_get_boolean (child, &conf->collect_cache); else if (strcasecmp ("CollectConnections", child->key) == 0) cf_util_get_boolean (child, &conf->collect_connections); else if (strcasecmp ("CollectESI", child->key) == 0) cf_util_get_boolean (child, &conf->collect_esi); #ifdef HAVE_VARNISH_V3 else if (strcasecmp ("CollectDirectorDNS", child->key) == 0) cf_util_get_boolean (child, &conf->collect_dirdns); #endif else if (strcasecmp ("CollectBackend", child->key) == 0) cf_util_get_boolean (child, &conf->collect_backend); else if (strcasecmp ("CollectFetch", child->key) == 0) cf_util_get_boolean (child, &conf->collect_fetch); else if (strcasecmp ("CollectHCB", child->key) == 0) cf_util_get_boolean (child, &conf->collect_hcb); else if (strcasecmp ("CollectObjects", child->key) == 0) cf_util_get_boolean (child, &conf->collect_objects); #if HAVE_VARNISH_V2 else if (strcasecmp ("CollectPurge", child->key) == 0) cf_util_get_boolean (child, &conf->collect_purge); #else else if (strcasecmp ("CollectBan", child->key) == 0) cf_util_get_boolean (child, &conf->collect_ban); #endif else if (strcasecmp ("CollectSession", child->key) == 0) cf_util_get_boolean (child, &conf->collect_session); else if (strcasecmp ("CollectSHM", child->key) == 0) cf_util_get_boolean (child, &conf->collect_shm); else if (strcasecmp ("CollectSMS", child->key) == 0) cf_util_get_boolean (child, &conf->collect_sms); #if HAVE_VARNISH_V2 else if (strcasecmp ("CollectSMA", child->key) == 0) cf_util_get_boolean (child, &conf->collect_sma); else if (strcasecmp ("CollectSM", child->key) == 0) cf_util_get_boolean (child, &conf->collect_sm); #endif else if (strcasecmp ("CollectStruct", child->key) == 0) cf_util_get_boolean (child, &conf->collect_struct); else if (strcasecmp ("CollectTotals", child->key) == 0) cf_util_get_boolean (child, &conf->collect_totals); #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4 else if (strcasecmp ("CollectUptime", child->key) == 0) cf_util_get_boolean (child, &conf->collect_uptime); #endif else if (strcasecmp ("CollectVCL", child->key) == 0) cf_util_get_boolean (child, &conf->collect_vcl); else if (strcasecmp ("CollectWorkers", child->key) == 0) cf_util_get_boolean (child, &conf->collect_workers); #if HAVE_VARNISH_V4 else if (strcasecmp ("CollectVSM", child->key) == 0) cf_util_get_boolean (child, &conf->collect_vsm); #endif else { WARNING ("Varnish plugin: Ignoring unknown " "configuration option: \"%s\". Did " "you forget to add an <Instance /> " "block around the configuration?", child->key); } } if (!conf->collect_cache && !conf->collect_connections && !conf->collect_esi && !conf->collect_backend #ifdef HAVE_VARNISH_V3 && !conf->collect_dirdns #endif && !conf->collect_fetch && !conf->collect_hcb && !conf->collect_objects #if HAVE_VARNISH_V2 && !conf->collect_purge #else && !conf->collect_ban #endif && !conf->collect_session && !conf->collect_shm && !conf->collect_sms #if HAVE_VARNISH_V2 && !conf->collect_sma && !conf->collect_sm #endif && !conf->collect_struct && !conf->collect_totals #if HAVE_VARNISH_V3 || HAVE_VARNISH_V4 && !conf->collect_uptime #endif && !conf->collect_vcl && !conf->collect_workers #if HAVE_VARNISH_V4 && !conf->collect_vsm #endif ) { WARNING ("Varnish plugin: No metric has been configured for " "instance \"%s\". Disabling this instance.", (conf->instance == NULL) ? "localhost" : conf->instance); return (EINVAL); } ssnprintf (callback_name, sizeof (callback_name), "varnish/%s", (conf->instance == NULL) ? "localhost" : conf->instance); ud.data = conf; ud.free_func = varnish_config_free; plugin_register_complex_read (/* group = */ "varnish", /* name = */ callback_name, /* callback = */ varnish_read, /* interval = */ 0, /* user data = */ &ud); have_instance = 1; return (0); } /* }}} int varnish_config_instance */
static int mb_config_add_host (oconfig_item_t *ci) /* {{{ */ { mb_host_t *host; int status; int i; host = malloc (sizeof (*host)); if (host == NULL) return (ENOMEM); memset (host, 0, sizeof (*host)); host->slaves = NULL; status = cf_util_get_string_buffer (ci, host->host, sizeof (host->host)); if (status != 0) return (status); if (host->host[0] == 0) return (EINVAL); for (i = 0; i < ci->children_num; i++) { oconfig_item_t *child = ci->children + i; status = 0; if (strcasecmp ("Address", child->key) == 0) { char buffer[NI_MAXHOST]; status = cf_util_get_string_buffer (child, buffer, sizeof (buffer)); if (status == 0) status = mb_config_set_host_address (host, buffer); } else if (strcasecmp ("Port", child->key) == 0) { host->port = cf_util_get_port_number (child); if (host->port <= 0) status = -1; } else if (strcasecmp ("Interval", child->key) == 0) status = cf_util_get_cdtime (child, &host->interval); else if (strcasecmp ("Slave", child->key) == 0) /* Don't set status: Gracefully continue if a slave fails. */ mb_config_add_slave (host, child); else { ERROR ("Modbus plugin: Unknown configuration option: %s", child->key); status = -1; } if (status != 0) break; } /* for (i = 0; i < ci->children_num; i++) */ assert (host->host[0] != 0); if (host->host[0] == 0) { ERROR ("Modbus plugin: Data block \"%s\": No type has been specified.", host->host); status = -1; } if (status == 0) { user_data_t ud; char name[1024]; struct timespec interval = { 0, 0 }; ud.data = host; ud.free_func = host_free; ssnprintf (name, sizeof (name), "modbus-%s", host->host); CDTIME_T_TO_TIMESPEC (host->interval, &interval); plugin_register_complex_read (/* group = */ NULL, name, /* callback = */ mb_read, /* interval = */ (host->interval > 0) ? &interval : NULL, &ud); } else { host_free (host); } return (status); } /* }}} int mb_config_add_host */
static int csnmp_config_add_host (oconfig_item_t *ci) { host_definition_t *hd; int status = 0; int i; /* Registration stuff. */ char cb_name[DATA_MAX_NAME_LEN]; user_data_t cb_data; hd = (host_definition_t *) malloc (sizeof (host_definition_t)); if (hd == NULL) return (-1); memset (hd, '\0', sizeof (host_definition_t)); hd->version = 2; C_COMPLAIN_INIT (&hd->complaint); status = cf_util_get_string(ci, &hd->name); if (status != 0) { sfree (hd); return status; } hd->sess_handle = NULL; hd->interval = 0; for (i = 0; i < ci->children_num; i++) { oconfig_item_t *option = ci->children + i; status = 0; if (strcasecmp ("Address", option->key) == 0) status = cf_util_get_string(option, &hd->address); else if (strcasecmp ("Community", option->key) == 0) status = cf_util_get_string(option, &hd->community); else if (strcasecmp ("Version", option->key) == 0) status = csnmp_config_add_host_version (hd, option); else if (strcasecmp ("Collect", option->key) == 0) csnmp_config_add_host_collect (hd, option); else if (strcasecmp ("Interval", option->key) == 0) cf_util_get_cdtime (option, &hd->interval); else if (strcasecmp ("Username", option->key) == 0) status = cf_util_get_string(option, &hd->username); else if (strcasecmp ("AuthProtocol", option->key) == 0) status = csnmp_config_add_host_auth_protocol (hd, option); else if (strcasecmp ("PrivacyProtocol", option->key) == 0) status = csnmp_config_add_host_priv_protocol (hd, option); else if (strcasecmp ("AuthPassphrase", option->key) == 0) status = cf_util_get_string(option, &hd->auth_passphrase); else if (strcasecmp ("PrivacyPassphrase", option->key) == 0) status = cf_util_get_string(option, &hd->priv_passphrase); else if (strcasecmp ("SecurityLevel", option->key) == 0) status = csnmp_config_add_host_security_level (hd, option); else if (strcasecmp ("Context", option->key) == 0) status = cf_util_get_string(option, &hd->context); else { WARNING ("snmp plugin: csnmp_config_add_host: Option `%s' not allowed here.", option->key); status = -1; } if (status != 0) break; } /* for (ci->children) */ while (status == 0) { if (hd->address == NULL) { WARNING ("snmp plugin: `Address' not given for host `%s'", hd->name); status = -1; break; } if (hd->community == NULL && hd->version < 3) { WARNING ("snmp plugin: `Community' not given for host `%s'", hd->name); status = -1; break; } if (hd->version == 3) { if (hd->username == NULL) { WARNING ("snmp plugin: `Username' not given for host `%s'", hd->name); status = -1; break; } if (hd->security_level == 0) { WARNING ("snmp plugin: `SecurityLevel' not given for host `%s'", hd->name); status = -1; break; } if (hd->security_level == SNMP_SEC_LEVEL_AUTHNOPRIV || hd->security_level == SNMP_SEC_LEVEL_AUTHPRIV) { if (hd->auth_protocol == NULL) { WARNING ("snmp plugin: `AuthProtocol' not given for host `%s'", hd->name); status = -1; break; } if (hd->auth_passphrase == NULL) { WARNING ("snmp plugin: `AuthPassphrase' not given for host `%s'", hd->name); status = -1; break; } } if (hd->security_level == SNMP_SEC_LEVEL_AUTHPRIV) { if (hd->priv_protocol == NULL) { WARNING ("snmp plugin: `PrivacyProtocol' not given for host `%s'", hd->name); status = -1; break; } if (hd->priv_passphrase == NULL) { WARNING ("snmp plugin: `PrivacyPassphrase' not given for host `%s'", hd->name); status = -1; break; } } } break; } /* while (status == 0) */ if (status != 0) { csnmp_host_definition_destroy (hd); return (-1); } DEBUG ("snmp plugin: hd = { name = %s, address = %s, community = %s, version = %i }", hd->name, hd->address, hd->community, hd->version); ssnprintf (cb_name, sizeof (cb_name), "snmp-%s", hd->name); memset (&cb_data, 0, sizeof (cb_data)); cb_data.data = hd; cb_data.free_func = csnmp_host_definition_destroy; status = plugin_register_complex_read (/* group = */ NULL, cb_name, csnmp_read_host, hd->interval, /* user_data = */ &cb_data); if (status != 0) { ERROR ("snmp plugin: Registering complex read function failed."); csnmp_host_definition_destroy (hd); return (-1); } return (0); } /* int csnmp_config_add_host */
static int csnmp_config_add_host (oconfig_item_t *ci) { host_definition_t *hd; int status = 0; int i; /* Registration stuff. */ char cb_name[DATA_MAX_NAME_LEN]; user_data_t cb_data; struct timespec cb_interval; if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) { WARNING ("snmp plugin: `Host' needs exactly one string argument."); return (-1); } hd = (host_definition_t *) malloc (sizeof (host_definition_t)); if (hd == NULL) return (-1); memset (hd, '\0', sizeof (host_definition_t)); hd->version = 2; C_COMPLAIN_INIT (&hd->complaint); hd->name = strdup (ci->values[0].value.string); if (hd->name == NULL) { free (hd); return (-1); } hd->sess_handle = NULL; hd->interval = 0; for (i = 0; i < ci->children_num; i++) { oconfig_item_t *option = ci->children + i; status = 0; if (strcasecmp ("Address", option->key) == 0) status = csnmp_config_add_host_address (hd, option); else if (strcasecmp ("Community", option->key) == 0) status = csnmp_config_add_host_community (hd, option); else if (strcasecmp ("Version", option->key) == 0) status = csnmp_config_add_host_version (hd, option); else if (strcasecmp ("Collect", option->key) == 0) csnmp_config_add_host_collect (hd, option); else if (strcasecmp ("Interval", option->key) == 0) csnmp_config_add_host_interval (hd, option); else { WARNING ("snmp plugin: csnmp_config_add_host: Option `%s' not allowed here.", option->key); status = -1; } if (status != 0) break; } /* for (ci->children) */ while (status == 0) { if (hd->address == NULL) { WARNING ("snmp plugin: `Address' not given for host `%s'", hd->name); status = -1; break; } if (hd->community == NULL) { WARNING ("snmp plugin: `Community' not given for host `%s'", hd->name); status = -1; break; } break; } /* while (status == 0) */ if (status != 0) { csnmp_host_definition_destroy (hd); return (-1); } DEBUG ("snmp plugin: hd = { name = %s, address = %s, community = %s, version = %i }", hd->name, hd->address, hd->community, hd->version); ssnprintf (cb_name, sizeof (cb_name), "snmp-%s", hd->name); memset (&cb_data, 0, sizeof (cb_data)); cb_data.data = hd; cb_data.free_func = csnmp_host_definition_destroy; memset (&cb_interval, 0, sizeof (cb_interval)); if (hd->interval != 0) cb_interval.tv_sec = (time_t) hd->interval; status = plugin_register_complex_read (cb_name, csnmp_read_host, /* interval = */ &cb_interval, /* user_data = */ &cb_data); if (status != 0) { ERROR ("snmp plugin: Registering complex read function failed."); csnmp_host_definition_destroy (hd); return (-1); } return (0); } /* int csnmp_config_add_host */
static int c_psql_config_database (oconfig_item_t *ci) { c_psql_database_t *db; char cb_name[DATA_MAX_NAME_LEN]; struct timespec cb_interval = { 0, 0 }; user_data_t ud; int i; if ((1 != ci->values_num) || (OCONFIG_TYPE_STRING != ci->values[0].type)) { log_err ("<Database> expects a single string argument."); return 1; } memset (&ud, 0, sizeof (ud)); db = c_psql_database_new (ci->values[0].value.string); if (db == NULL) return -1; for (i = 0; i < ci->children_num; ++i) { oconfig_item_t *c = ci->children + i; if (0 == strcasecmp (c->key, "Host")) cf_util_get_string (c, &db->host); else if (0 == strcasecmp (c->key, "Port")) cf_util_get_service (c, &db->port); else if (0 == strcasecmp (c->key, "User")) cf_util_get_string (c, &db->user); else if (0 == strcasecmp (c->key, "Password")) cf_util_get_string (c, &db->password); else if (0 == strcasecmp (c->key, "Instance")) cf_util_get_string (c, &db->instance); else if (0 == strcasecmp (c->key, "SSLMode")) cf_util_get_string (c, &db->sslmode); else if (0 == strcasecmp (c->key, "KRBSrvName")) cf_util_get_string (c, &db->krbsrvname); else if (0 == strcasecmp (c->key, "Service")) cf_util_get_string (c, &db->service); else if (0 == strcasecmp (c->key, "Query")) udb_query_pick_from_list (c, queries, queries_num, &db->queries, &db->queries_num); else if (0 == strcasecmp (c->key, "Interval")) cf_util_get_cdtime (c, &db->interval); else log_warn ("Ignoring unknown config key \"%s\".", c->key); } /* If no `Query' options were given, add the default queries.. */ if (db->queries_num == 0) { for (i = 0; i < def_queries_num; i++) udb_query_pick_from_list_by_name (def_queries[i], queries, queries_num, &db->queries, &db->queries_num); } if (db->queries_num > 0) { db->q_prep_areas = (udb_query_preparation_area_t **) calloc ( db->queries_num, sizeof (*db->q_prep_areas)); if (db->q_prep_areas == NULL) { log_err ("Out of memory."); c_psql_database_delete (db); return -1; } } for (i = 0; (size_t)i < db->queries_num; ++i) { c_psql_user_data_t *data; data = udb_query_get_user_data (db->queries[i]); if ((data != NULL) && (data->params_num > db->max_params_num)) db->max_params_num = data->params_num; db->q_prep_areas[i] = udb_query_allocate_preparation_area (db->queries[i]); if (db->q_prep_areas[i] == NULL) { log_err ("Out of memory."); c_psql_database_delete (db); return -1; } } ud.data = db; ud.free_func = c_psql_database_delete; ssnprintf (cb_name, sizeof (cb_name), "postgresql-%s", db->instance); CDTIME_T_TO_TIMESPEC (db->interval, &cb_interval); plugin_register_complex_read ("postgresql", cb_name, c_psql_read, /* interval = */ (db->interval > 0) ? &cb_interval : NULL, &ud); return 0; } /* c_psql_config_database */
/* <File /> block */ static int tcsv_config_add_file(oconfig_item_t *ci) { instance_definition_t* id; int status = 0; int i; /* Registration variables */ char cb_name[DATA_MAX_NAME_LEN]; user_data_t cb_data; id = malloc(sizeof(*id)); if (id == NULL) return (-1); memset(id, 0, sizeof(*id)); id->instance = NULL; id->path = NULL; id->metric_list = NULL; id->time_from = -1; id->next = NULL; status = cf_util_get_string (ci, &id->path); if (status != 0) { sfree (id); return (status); } /* Use default interval. */ id->interval = plugin_get_interval(); for (i = 0; i < ci->children_num; ++i){ oconfig_item_t *option = ci->children + i; status = 0; if (strcasecmp("Instance", option->key) == 0) status = cf_util_get_string(option, &id->instance); else if (strcasecmp("Collect", option->key) == 0) status = tcsv_config_add_instance_collect(id, option); else if (strcasecmp("Interval", option->key) == 0) cf_util_get_cdtime(option, &id->interval); else if (strcasecmp("TimeFrom", option->key) == 0) status = tcsv_config_get_index (option, &id->time_from); else { WARNING("tail_csv plugin: Option `%s' not allowed here.", option->key); status = -1; } if (status != 0) break; } if (status != 0){ tcsv_instance_definition_destroy(id); return (-1); } /* Verify all necessary options have been set. */ if (id->path == NULL){ WARNING("tail_csv plugin: Option `Path' must be set."); status = -1; } else if (id->metric_list == NULL){ WARNING("tail_csv plugin: Option `Collect' must be set."); status = -1; } if (status != 0){ tcsv_instance_definition_destroy(id); return (-1); } ssnprintf (cb_name, sizeof (cb_name), "tail_csv/%s", id->path); memset(&cb_data, 0, sizeof(cb_data)); cb_data.data = id; cb_data.free_func = tcsv_instance_definition_destroy; status = plugin_register_complex_read(NULL, cb_name, tcsv_read, id->interval, &cb_data); if (status != 0){ ERROR("tail_csv plugin: Registering complex read function failed."); tcsv_instance_definition_destroy(id); return (-1); } return (0); }
/* Configuration handling functiions * <Plugin apache> * <Instance "instance_name"> * URL ... * </Instance> * URL ... * </Plugin> */ static int config_add (oconfig_item_t *ci) { apache_t *st; int i; int status; st = calloc (1, sizeof (*st)); if (st == NULL) { ERROR ("apache plugin: calloc failed."); return (-1); } st->timeout = -1; status = cf_util_get_string (ci, &st->name); if (status != 0) { sfree (st); return (status); } assert (st->name != NULL); for (i = 0; i < ci->children_num; i++) { oconfig_item_t *child = ci->children + i; if (strcasecmp ("URL", child->key) == 0) status = cf_util_get_string (child, &st->url); else if (strcasecmp ("Host", child->key) == 0) status = cf_util_get_string (child, &st->host); else if (strcasecmp ("User", child->key) == 0) status = cf_util_get_string (child, &st->user); else if (strcasecmp ("Password", child->key) == 0) status = cf_util_get_string (child, &st->pass); else if (strcasecmp ("VerifyPeer", child->key) == 0) status = cf_util_get_boolean (child, &st->verify_peer); else if (strcasecmp ("VerifyHost", child->key) == 0) status = cf_util_get_boolean (child, &st->verify_host); else if (strcasecmp ("CACert", child->key) == 0) status = cf_util_get_string (child, &st->cacert); else if (strcasecmp ("SSLCiphers", child->key) == 0) status = cf_util_get_string (child, &st->ssl_ciphers); else if (strcasecmp ("Server", child->key) == 0) status = cf_util_get_string (child, &st->server); else if (strcasecmp ("Timeout", child->key) == 0) status = cf_util_get_int (child, &st->timeout); else { WARNING ("apache plugin: Option `%s' not allowed here.", child->key); status = -1; } if (status != 0) break; } /* Check if struct is complete.. */ if ((status == 0) && (st->url == NULL)) { ERROR ("apache plugin: Instance `%s': " "No URL has been configured.", st->name); status = -1; } if (status == 0) { user_data_t ud; char callback_name[3*DATA_MAX_NAME_LEN]; memset (&ud, 0, sizeof (ud)); ud.data = st; ud.free_func = (void *) apache_free; memset (callback_name, 0, sizeof (callback_name)); ssnprintf (callback_name, sizeof (callback_name), "apache/%s/%s", (st->host != NULL) ? st->host : hostname_g, (st->name != NULL) ? st->name : "default"), status = plugin_register_complex_read (/* group = */ NULL, /* name = */ callback_name, /* callback = */ apache_read_host, /* interval = */ 0, /* user_data = */ &ud); } if (status != 0) { apache_free (st); return (-1); } return (0); } /* int config_add */