static switch_status_t load_config(switch_bool_t reload) { switch_status_t status = SWITCH_STATUS_SUCCESS; switch_xml_t cfg, xml = NULL, settings, param, x_profiles, x_profile; switch_cache_db_handle_t *dbh = NULL; if (!(xml = switch_xml_open_cfg(global_cf, &cfg, NULL))) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Open of %s failed\n", global_cf); return SWITCH_STATUS_TERM; } switch_mutex_lock(globals.mutex); if ((settings = switch_xml_child(cfg, "settings"))) { for (param = switch_xml_child(settings, "param"); param; param = param->next) { char *var = (char *) switch_xml_attr_soft(param, "name"); char *val = (char *) switch_xml_attr_soft(param, "value"); if (!strcasecmp(var, "odbc-dsn") && !zstr(val)) { if (switch_odbc_available()) { switch_set_string(globals.odbc_dsn, val); } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "ODBC IS NOT AVAILABLE!\n"); } } else if (!strcasecmp(var, "dbname") && !zstr(val)) { globals.dbname = switch_core_strdup(globals.pool, val); } if (!strcasecmp(var, "debug")) { globals.debug = atoi(val); } } } if ((x_profiles = switch_xml_child(cfg, "profiles"))) { for (x_profile = switch_xml_child(x_profiles, "profile"); x_profile; x_profile = x_profile->next) { load_profile(switch_xml_attr_soft(x_profile, "name")); } } if (zstr(globals.odbc_dsn) && zstr(globals.dbname)) { globals.dbname = switch_core_sprintf(globals.pool, "directory"); } dbh = directory_get_db_handle(); if (dbh) { if (!reload) { switch_cache_db_test_reactive(dbh, "delete from directory_search where uuid != '' and name_visible != '' ", "drop table directory_search", dir_sql); } switch_cache_db_release_db_handle(&dbh); } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Cannot open DB!2\n"); status = SWITCH_STATUS_TERM; goto end; } end: switch_mutex_unlock(globals.mutex); switch_xml_free(xml); return status; }
static switch_status_t directory_execute_sql(char *sql, switch_mutex_t *mutex) { switch_cache_db_handle_t *dbh = NULL; switch_status_t status = SWITCH_STATUS_FALSE; if (mutex) { switch_mutex_lock(mutex); } if (!(dbh = directory_get_db_handle())) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Opening DB\n"); goto end; } if (globals.debug > 1) switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "sql: %s\n", sql); status = switch_cache_db_execute_sql(dbh, sql, NULL); end: switch_cache_db_release_db_handle(&dbh); if (mutex) { switch_mutex_unlock(mutex); } return status; }
static switch_bool_t directory_execute_sql_callback(switch_mutex_t *mutex, char *sql, switch_core_db_callback_func_t callback, void *pdata) { switch_bool_t ret = SWITCH_FALSE; switch_cache_db_handle_t *dbh = NULL; char *errmsg = NULL; if (mutex) { switch_mutex_lock(mutex); } if (!(dbh = directory_get_db_handle())) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Opening DB\n"); goto end; } switch_cache_db_execute_sql_callback(dbh, sql, callback, pdata, &errmsg); if (errmsg) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQL ERR: [%s] %s\n", sql, errmsg); free(errmsg); } end: switch_cache_db_release_db_handle(&dbh); if (mutex) { switch_mutex_unlock(mutex); } return ret; }
static switch_bool_t nibblebill_execute_sql_callback(char *sql, switch_core_db_callback_func_t callback, void *pdata) { switch_bool_t retval = SWITCH_FALSE; switch_cache_db_handle_t *dbh = NULL; if (globals.odbc_dsn && (dbh = nibblebill_get_db_handle())) { if (switch_cache_db_execute_sql_callback(dbh, sql, callback, pdata, NULL) != SWITCH_STATUS_SUCCESS) { retval = SWITCH_FALSE; } else { retval = SWITCH_TRUE; } } switch_cache_db_release_db_handle(&dbh); return retval; }
static switch_bool_t nibblebill_execute_sql(char *sql) { switch_bool_t retval = SWITCH_FALSE; switch_cache_db_handle_t *dbh = NULL; if (globals.odbc_dsn && (dbh = nibblebill_get_db_handle())) { if ( switch_cache_db_execute_sql(dbh, sql, NULL ) != SWITCH_STATUS_SUCCESS ) { retval = SWITCH_FALSE; } else { retval = SWITCH_TRUE; } } switch_cache_db_release_db_handle(&dbh); return retval; }
static switch_bool_t cidlookup_execute_sql_callback(char *sql, switch_core_db_callback_func_t callback, callback_t *cbt, char **err) { switch_bool_t retval = SWITCH_FALSE; switch_cache_db_handle_t *dbh = NULL; if (!zstr(globals.odbc_dsn) && (dbh = cidlookup_get_db_handle())) { if (switch_cache_db_execute_sql_callback(dbh, sql, callback, (void *) cbt, err) != SWITCH_STATUS_SUCCESS) { retval = SWITCH_FALSE; } else { retval = SWITCH_TRUE; } } else { *err = switch_core_sprintf(cbt->pool, "Unable to get ODBC handle. dsn: %s, dbh is %s\n", globals.odbc_dsn, dbh ? "not null" : "null"); } switch_cache_db_release_db_handle(&dbh); return retval; }
static switch_status_t limit_execute_sql(char *sql) { switch_cache_db_handle_t *dbh = NULL; switch_status_t status = SWITCH_STATUS_FALSE; if (!(dbh = limit_get_db_handle())) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Opening DB\n"); goto end; } status = switch_cache_db_execute_sql(dbh, sql, NULL); end: switch_cache_db_release_db_handle(&dbh); return status; }
static switch_status_t write_cdr(char *sql) { switch_cache_db_handle_t *dbh = NULL; switch_status_t status = SWITCH_STATUS_FALSE; if (!(dbh = cdr_get_db_handle())) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Opening DB\n"); goto end; } switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Writing SQL to DB: %s\n", sql); status = switch_cache_db_execute_sql(dbh, sql, NULL); end: switch_cache_db_release_db_handle(&dbh); return status; }
int channelList_load(netsnmp_cache *cache, void *vmagic) { switch_cache_db_handle_t *dbh; char sql[1024] = ""; channelList_free(cache, NULL); if (switch_core_db_handle(&dbh) != SWITCH_STATUS_SUCCESS) { return 0; } idx = 1; sprintf(sql, "SELECT * FROM channels WHERE hostname='%s' ORDER BY created_epoch", switch_core_get_switchname()); switch_cache_db_execute_sql_callback(dbh, sql, channelList_callback, NULL, NULL); switch_cache_db_release_db_handle(&dbh); return 0; }
static switch_status_t config_callback_dsn(switch_xml_config_item_t *data, const char *newvalue, switch_config_callback_type_t callback_type, switch_bool_t changed) { switch_status_t status = SWITCH_STATUS_SUCCESS; switch_cache_db_handle_t *dbh = NULL; if (!switch_odbc_available()) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "ODBC is not compiled in. Do not configure odbc-dsn parameter!\n"); return SWITCH_STATUS_FALSE; } if ((callback_type == CONFIG_LOAD || callback_type == CONFIG_RELOAD) && changed) { if (zstr(newvalue)) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "No local database defined.\n"); } else { switch_safe_free(globals.odbc_dsn); globals.odbc_dsn = strdup(newvalue); if ((globals.odbc_user = strchr(globals.odbc_dsn, ':'))) { *globals.odbc_user++ = '\0'; if ((globals.odbc_pass = strchr(globals.odbc_user, ':'))) { *globals.odbc_pass++ = '\0'; } } switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Connecting to dsn: %s\n", globals.odbc_dsn); if (!(dbh = cidlookup_get_db_handle())) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Cannot Open ODBC Database!\n"); switch_goto_status(SWITCH_STATUS_FALSE, done); } } } switch_goto_status(SWITCH_STATUS_SUCCESS, done); done: switch_cache_db_release_db_handle(&dbh); return status; }
int utils_count_profile_channels(char *profile_name) { char *sql; char *errmsg; switch_cache_db_handle_t *db; int count = 0; switch_status_t status; char hostname[256] = ""; gethostname(hostname, sizeof(hostname)); if (switch_core_db_handle(&db) != SWITCH_STATUS_SUCCESS) { return -1; } /*non mi ricordo per quale motivo non usai una count(*)*/ sql = switch_mprintf("select * from channels where hostname='%s' AND (name LIKE 'sofia/%s/%')", hostname, profile_name); if (!sql) return -1; status = switch_cache_db_execute_sql_callback(db, sql, show_callback, &count, &errmsg); switch_safe_free(sql); if (errmsg) { free(errmsg); errmsg = NULL; } if (db) { switch_cache_db_release_db_handle(&db); } if (status == SWITCH_STATUS_FALSE) return -1; return count; }
static switch_status_t load_config(switch_memory_pool_t *pool) { char *cf = "cdr_sqlite.conf"; switch_xml_t cfg, xml, settings, param; switch_cache_db_handle_t *dbh = NULL; char *select_sql = NULL, *create_sql = NULL; switch_status_t status = SWITCH_STATUS_SUCCESS; memset(&globals, 0, sizeof(globals)); switch_core_hash_init(&globals.template_hash, pool); globals.pool = pool; switch_core_hash_insert(globals.template_hash, "default", default_template); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Adding default template.\n"); globals.legs = CDR_LEG_A; if ((xml = switch_xml_open_cfg(cf, &cfg, NULL))) { if ((settings = switch_xml_child(cfg, "settings"))) { for (param = switch_xml_child(settings, "param"); param; param = param->next) { char *var = (char *) switch_xml_attr_soft(param, "name"); char *val = (char *) switch_xml_attr_soft(param, "value"); if (!strcasecmp(var, "debug")) { globals.debug = switch_true(val); } else if (!strcasecmp(var, "db-name")) { globals.db_name = switch_core_strdup(pool, val); } else if (!strcasecmp(var, "db-table")) { globals.db_table = switch_core_strdup(pool, val); } else if (!strcasecmp(var, "legs")) { globals.legs = 0; if (strchr(val, 'a')) { globals.legs |= CDR_LEG_A; } if (strchr(val, 'b')) { globals.legs |= CDR_LEG_B; } } else if (!strcasecmp(var, "default-template")) { globals.default_template = switch_core_strdup(pool, val); } } } if ((settings = switch_xml_child(cfg, "templates"))) { for (param = switch_xml_child(settings, "template"); param; param = param->next) { char *var = (char *) switch_xml_attr(param, "name"); if (var) { char *tpl; tpl = switch_core_strdup(pool, param->txt); switch_core_hash_insert(globals.template_hash, var, tpl); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Adding template %s.\n", var); } } } switch_xml_free(xml); } if (zstr(globals.db_name)) { globals.db_name = switch_core_strdup(pool, "cdr"); } if (zstr(globals.db_table)) { globals.db_table = switch_core_strdup(pool, "cdr"); } if (zstr(globals.default_template)) { globals.default_template = switch_core_strdup(pool, "default"); } dbh = cdr_get_db_handle(); if (dbh) { select_sql = switch_mprintf("SELECT * FROM %s LIMIT 1", globals.db_table); assert(select_sql); create_sql = switch_mprintf(default_create_sql, globals.db_table); assert(create_sql); /* Check if table exists (try SELECT FROM ...) and create table if query fails */ switch_cache_db_test_reactive(dbh, select_sql, NULL, create_sql); switch_safe_free(select_sql); switch_safe_free(create_sql); switch_cache_db_release_db_handle(&dbh); } return status; }
SWITCH_DECLARE(char *) switch_console_expand_alias(char *cmd, char *arg) { char *errmsg = NULL; char *r = NULL; char *sql = NULL; char *exp = NULL; switch_cache_db_handle_t *db = NULL; switch_core_flag_t cflags = switch_core_flags(); int full = 0; if (!(cflags & SCF_USE_SQL)) { return NULL; } if (switch_core_db_handle(&db) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Database Error\n"); return NULL; } if (switch_cache_db_get_type(db) == SCDB_TYPE_CORE_DB) { sql = switch_mprintf("select command from aliases where alias='%q'", cmd); } else { sql = switch_mprintf("select command from aliases where alias='%w'", cmd); } switch_cache_db_execute_sql_callback(db, sql, alias_callback, &r, &errmsg); if (errmsg) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "error [%s][%s]\n", sql, errmsg); free(errmsg); } switch_safe_free(sql); if (!r) { if (switch_cache_db_get_type(db) == SCDB_TYPE_CORE_DB) { sql = switch_mprintf("select command from aliases where alias='%q %q'", cmd, arg); } else { sql = switch_mprintf("select command from aliases where alias='%w %w'", cmd, arg); } switch_cache_db_execute_sql_callback(db, sql, alias_callback, &r, &errmsg); if (errmsg) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "error [%s][%s]\n", sql, errmsg); free(errmsg); } if (r) { full++; } } switch_safe_free(sql); if (r) { if (arg && !full) { exp = switch_mprintf("%s %s", r, arg); free(r); } else { exp = r; } } else { exp = cmd; } switch_cache_db_release_db_handle(&db); return exp; }
int handle_systemStats(netsnmp_mib_handler *handler, netsnmp_handler_registration *reginfo, netsnmp_agent_request_info *reqinfo, netsnmp_request_info *requests) { netsnmp_request_info *request = NULL; oid subid; switch_time_t uptime; uint32_t int_val = 0; switch(reqinfo->mode) { case MODE_GET: subid = requests->requestvb->name[reginfo->rootoid_len - 2]; snmp_log(LOG_DEBUG, "systemStats OID-suffix requested (%d)\n", (int) subid); switch (subid) { case SS_UPTIME: uptime = switch_core_uptime() / 10000; snmp_set_var_typed_value(requests->requestvb, ASN_TIMETICKS, (u_char *) &uptime, sizeof(uptime)); break; case SS_SESSIONS_SINCE_STARTUP: int_val = switch_core_session_id() - 1; snmp_set_var_typed_integer(requests->requestvb, ASN_COUNTER, int_val); break; case SS_CURRENT_SESSIONS: int_val = switch_core_session_count(); snmp_set_var_typed_integer(requests->requestvb, ASN_GAUGE, int_val); break; case SS_MAX_SESSIONS: switch_core_session_ctl(SCSC_MAX_SESSIONS, &int_val); snmp_set_var_typed_integer(requests->requestvb, ASN_GAUGE, int_val); break; case SS_CURRENT_CALLS: { switch_cache_db_handle_t *dbh; char sql[1024] = ""; if (switch_core_db_handle(&dbh) != SWITCH_STATUS_SUCCESS) { return SNMP_ERR_GENERR; } sprintf(sql, "SELECT COUNT(*) FROM calls WHERE hostname='%s'", switch_core_get_switchname()); switch_cache_db_execute_sql_callback(dbh, sql, sql_count_callback, &int_val, NULL); snmp_set_var_typed_integer(requests->requestvb, ASN_GAUGE, int_val); switch_cache_db_release_db_handle(&dbh); } break; case SS_SESSIONS_PER_SECOND: switch_core_session_ctl(SCSC_LAST_SPS, &int_val); snmp_set_var_typed_integer(requests->requestvb, ASN_GAUGE, int_val); break; case SS_MAX_SESSIONS_PER_SECOND: switch_core_session_ctl(SCSC_SPS, &int_val); snmp_set_var_typed_integer(requests->requestvb, ASN_GAUGE, int_val); break; case SS_PEAK_SESSIONS_PER_SECOND: switch_core_session_ctl(SCSC_SPS_PEAK, &int_val); snmp_set_var_typed_integer(requests->requestvb, ASN_GAUGE, int_val); break; case SS_PEAK_SESSIONS_PER_FIVEMIN: switch_core_session_ctl(SCSC_SPS_PEAK_FIVEMIN, &int_val); snmp_set_var_typed_integer(requests->requestvb, ASN_GAUGE, int_val); break; case SS_PEAK_SESSIONS: switch_core_session_ctl(SCSC_SESSIONS_PEAK, &int_val); snmp_set_var_typed_integer(requests->requestvb, ASN_GAUGE, int_val); break; case SS_PEAK_SESSIONS_FIVEMIN: switch_core_session_ctl(SCSC_SESSIONS_PEAK_FIVEMIN, &int_val); snmp_set_var_typed_integer(requests->requestvb, ASN_GAUGE, int_val); break; default: snmp_log(LOG_WARNING, "Unregistered OID-suffix requested (%d)\n", (int) subid); netsnmp_set_request_error(reqinfo, request, SNMP_NOSUCHOBJECT); } break; default: /* we should never get here, so this is a really bad error */ snmp_log(LOG_ERR, "Unknown mode (%d) in handle_systemStats\n", reqinfo->mode); return SNMP_ERR_GENERR; } return SNMP_ERR_NOERROR; }
static switch_status_t do_config() { switch_cache_db_handle_t *dbh = NULL; switch_status_t status = SWITCH_STATUS_SUCCESS; char *sql = NULL; limit_config_dsn.pool = globals.pool; if (switch_xml_config_parse_module_settings("db.conf", SWITCH_FALSE, config_settings) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "No config file found, defaulting to sqlite\n"); } if (globals.odbc_dsn) { if ((globals.odbc_user = strchr(globals.odbc_dsn, ':'))) { *globals.odbc_user++ = '\0'; if ((globals.odbc_pass = strchr(globals.odbc_user, ':'))) { *globals.odbc_pass++ = '\0'; } } if (!(dbh = limit_get_db_handle())) { globals.odbc_dsn = globals.odbc_user = globals.odbc_pass; } } if (zstr(globals.odbc_dsn)) { globals.dbname = "call_limit"; dbh = limit_get_db_handle(); } if (dbh) { int x = 0; char *indexes[] = { "create index ld_hostname on limit_data (hostname)", "create index ld_uuid on limit_data (uuid)", "create index ld_realm on limit_data (realm)", "create index ld_id on limit_data (id)", "create index dd_realm on db_data (realm)", "create index dd_data_key on db_data (data_key)", "create index gd_groupname on group_data (groupname)", "create index gd_url on group_data (url)", NULL }; switch_cache_db_test_reactive(dbh, "select * from limit_data", NULL, limit_sql); switch_cache_db_test_reactive(dbh, "select * from db_data", NULL, db_sql); switch_cache_db_test_reactive(dbh, "select * from group_data", NULL, group_sql); for (x = 0; indexes[x]; x++) { switch_cache_db_execute_sql(dbh, indexes[x], NULL); } switch_cache_db_release_db_handle(&dbh); sql = switch_mprintf("delete from limit_data where hostname='%q';", globals.hostname); limit_execute_sql(sql); switch_safe_free(sql); } return status; }