void do_index(switch_stream_handle_t *stream)
{
	switch_cache_db_handle_t *db;
	const char *sql = "select * from channels";
	struct holder holder;
	char *errmsg;

	if (switch_core_db_handle(&db) != SWITCH_STATUS_SUCCESS) {
		return;
	}

	holder.host = switch_event_get_header(stream->param_event, "http-host");
	holder.port = switch_event_get_header(stream->param_event, "http-port");
	holder.uri = switch_event_get_header(stream->param_event, "http-uri");
	holder.stream = stream;

	stream->write_function(stream, "Content-type: text/html\r\n\r\n");
	stream->write_function(stream,
						   "<table align=center border=1 cellpadding=6 cellspacing=0>"
						   "<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>\n",
						   "Created", "CID Name", "CID Num", "Ext", "App", "Data", "Codec", "Rate", "Listen");

	switch_cache_db_execute_sql_callback(db, sql, web_callback, &holder, &errmsg);

	stream->write_function(stream, "</table>");

	if (errmsg) {
		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error [%s]\n", errmsg);
		switch_safe_free(errmsg);
	}
}
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;
}
Exemple #3
0
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 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;
}
Exemple #5
0
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;
}
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;
}
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;
}
Exemple #8
0
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;
}