Add statistics reporting

ulab-next-nosound
Timothy Pearson 12 years ago committed by Timothy Pearson
parent a2d4f91ed2
commit c12cbea6b1

@ -23,6 +23,12 @@
#include "libraptorsmiface.h"
#define STATISTICS_SERVER_START_EVENT 0
#define STATISTICS_SERVER_STOP_EVENT 1
#define STATISTICS_NEW_CONNECTION_EVENT 2
#define STATISTICS_CONNECTION_STATUS_EVENT 3
#define STATISTICS_DISCONNECTION_EVENT 4
//char *server = "localhost";
char *server = "freyja.starlink.edu";
char *user = "remotelab";
@ -104,6 +110,7 @@ char raptor_sm_deallocate_session(char* username) {
return 1;
}
int display = raptor_sm_get_display_for_username(username);
char* hostname = raptor_sm_get_hostname_for_username(username, false);
pid_t serverpid = raptor_sm_get_pid_for_username(username, RAPTOR_SM_SERVER_PID_FIELD);
if (serverpid >= 0) {
@ -138,12 +145,28 @@ char raptor_sm_deallocate_session(char* username) {
free(safe_username);
if (mysql_query_internal(conn, query)) {
// Server error
free(hostname);
free(query);
mysql_close(conn);
return 2;
}
else {
free(query);
// Insert connection information into the statistics database
char* safe_servername = get_mysql_escaped_string(conn, hostname);
char* safe_username = get_mysql_escaped_string(conn, username);
long long timestamp = time(NULL);
asprintf(&query, "INSERT INTO statistics (timestamp, eventtypeid, servername, display, typeid, username) VALUES ('%lld', '%d', '%s', '%d', '%d', '%s')", timestamp, STATISTICS_DISCONNECTION_EVENT, safe_servername, display, -1, safe_username);
free(safe_servername);
free(safe_username);
free(hostname);
if (mysql_query_internal(conn, query)) {
// Server error
dprint("Unable to insert data into statistics database! [%s]\n\r", mysql_error(conn));
}
free(query);
mysql_close(conn);
return 0;
}
@ -265,6 +288,20 @@ char* raptor_sm_allocate_session(char* username) {
}
else {
free(query);
// Insert connection information into the statistics database
char* safe_servername = get_mysql_escaped_string(conn, bestserver);
char* safe_username = get_mysql_escaped_string(conn, username);
long long timestamp = time(NULL);
asprintf(&query, "INSERT INTO statistics (timestamp, eventtypeid, servername, display, typeid, username) VALUES ('%lld', '%d', '%s', '%d', '%d', '%s')", timestamp, STATISTICS_NEW_CONNECTION_EVENT, safe_servername, -1, SM_STATUS_ALLOCATED, safe_username);
free(safe_servername);
free(safe_username);
if (mysql_query_internal(conn, query)) {
// Server error
dprint("Unable to insert data into statistics database! [%s]\n\r", mysql_error(conn));
}
free(query);
mysql_close(conn);
return strdup(bestserver);
}
@ -653,6 +690,22 @@ char* raptor_sm_server_started(char* username, pid_t pid, int display, char* dbf
}
else {
free(query);
// Insert connection information into the statistics database
char* current_server = raptor_sm_get_hostname_for_display(display);
char* safe_servername = get_mysql_escaped_string(conn, current_server);
char* safe_username = get_mysql_escaped_string(conn, username);
long long timestamp = time(NULL);
asprintf(&query, "INSERT INTO statistics (timestamp, eventtypeid, servername, display, typeid, username) VALUES ('%lld', '%d', '%s', '%d', '%d', '%s')", timestamp, STATISTICS_CONNECTION_STATUS_EVENT, safe_servername, display, SM_STATUS_RUNNING, safe_username);
free(safe_servername);
free(safe_username);
free(current_server);
if (mysql_query_internal(conn, query)) {
// Server error
dprint("Unable to insert data into statistics database! [%s]\n\r", mysql_error(conn));
}
free(query);
mysql_close(conn);
return 0;
}
@ -727,6 +780,48 @@ int raptor_sm_get_display_for_username(char* username) {
}
}
char* raptor_sm_get_username_for_display_and_hostname(int display, char* hostname) {
MYSQL_RES *res;
MYSQL_ROW row;
char* query;
MYSQL *conn = connect_if_needed();
if (!conn) {
return -1;
}
char* safe_hostname = get_mysql_escaped_string(conn, hostname);
asprintf(&query, "SELECT username FROM sessions WHERE display='%d' AND servername='%s'", display, safe_hostname);
free(safe_hostname);
if (mysql_query_internal(conn, query)) {
// Server error
free(query);
mysql_close(conn);
return strdup("");
}
else {
free(query);
res = mysql_store_result(conn);
while ((row = mysql_fetch_row(res)) != NULL) {
if (row[0]) {
char* ret = strdup(row[0]);
mysql_free_result(res);
mysql_close(conn);
return ret;
}
else {
mysql_free_result(res);
mysql_close(conn);
return strdup("");
}
}
// Nothing in the DB
mysql_free_result(res);
mysql_close(conn);
return strdup("");
}
}
void raptor_sm_wait_for_pid_exit(char* username, pid_t pid) {
char* ipaddr = raptor_sm_get_ip_for_username(username, false);
@ -833,6 +928,24 @@ char raptor_sm_set_session_state(int display, int state) {
}
else {
free(query);
// Insert connection information into the statistics database
char* hostname = raptor_sm_get_hostname_for_display(display);
char* username = raptor_sm_get_username_for_display_and_hostname(display, hostname);
char* safe_servername = get_mysql_escaped_string(conn, hostname);
char* safe_username = get_mysql_escaped_string(conn, username);
long long timestamp = time(NULL);
asprintf(&query, "INSERT INTO statistics (timestamp, eventtypeid, servername, display, typeid, username) VALUES ('%lld', '%d', '%s', '%d', '%d', '%s')", timestamp, STATISTICS_CONNECTION_STATUS_EVENT, safe_servername, display, state, safe_username);
free(safe_servername);
free(safe_username);
free(hostname);
free(username);
if (mysql_query_internal(conn, query)) {
// Server error
dprint("Unable to insert data into statistics database! [%s]\n\r", mysql_error(conn));
}
free(query);
mysql_close(conn);
return 0;
}

@ -36,6 +36,7 @@ char* raptor_sm_server_started(char* username, pid_t pid, int display, char* dbf
char* raptor_sm_wm_started(char* username, pid_t pid, char* dbfield);
int raptor_sm_get_display_for_username(char* username);
void raptor_sm_wait_for_pid_exit(char* username, pid_t pid);
char* raptor_sm_get_username_for_display_and_hostname(int display, char* hostname);
void raptor_sm_session_terminated(char* username);
void raptor_sm_wm_terminated(char* username);
int raptor_sm_get_new_unique_display(int mindisplay, int maxdisplay);

@ -496,8 +496,10 @@ struct xrdp_bitmap
#define DEFAULT_COMBO_H 21
#define DEFAULT_EDIT_W 210
#define DEFAULT_EDIT_H 21
#define DEFAULT_WND_LOGIN_W 425
#define DEFAULT_WND_LOGIN_H 475
#define DEFAULT_WND_LOGIN_W 500
#define DEFAULT_WND_LOGIN_H 200
//#define DEFAULT_WND_LOGIN_W 500
//#define DEFAULT_WND_LOGIN_H 250
#define DEFAULT_WND_HELP_W 340
#define DEFAULT_WND_HELP_H 300
#define DEFAULT_WND_LOG_W 400

Loading…
Cancel
Save