From d9d774c216167e4ce31e1ab02f45005f07a7033d Mon Sep 17 00:00:00 2001 From: Timothy Pearson Date: Thu, 20 Sep 2012 00:26:14 -0500 Subject: [PATCH] Add additional statistics reporting --- raptorsmiface/libraptorsmiface.c | 46 ++++++++++++++++++++++++++++++++ raptorsmiface/libraptorsmiface.h | 4 ++- xrdp/xrdp.c | 22 +++++++++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) diff --git a/raptorsmiface/libraptorsmiface.c b/raptorsmiface/libraptorsmiface.c index 6900a63c..6b0e0d0c 100644 --- a/raptorsmiface/libraptorsmiface.c +++ b/raptorsmiface/libraptorsmiface.c @@ -971,3 +971,49 @@ void raptor_sm_terminate_server(char* username) { free(command_string); } } + +void raptor_sm_stats_report_server_start(char* hostname) { + MYSQL_RES *res; + MYSQL_ROW row; + char* query; + + MYSQL *conn = connect_if_needed(); + if (!conn) { + return -1; + } + + // Insert information into the statistics database + char* safe_servername = get_mysql_escaped_string(conn, hostname); + long long timestamp = time(NULL); + asprintf(&query, "INSERT INTO statistics (timestamp, eventtypeid, servername, display, typeid) VALUES ('%lld', '%d', '%s', '%d', '%d')", timestamp, STATISTICS_SERVER_START_EVENT, safe_servername, -1, -1); + free(safe_servername); + 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); +} + +void raptor_sm_stats_report_server_stop(char* hostname) { + MYSQL_RES *res; + MYSQL_ROW row; + char* query; + + MYSQL *conn = connect_if_needed(); + if (!conn) { + return -1; + } + + // Insert information into the statistics database + char* safe_servername = get_mysql_escaped_string(conn, hostname); + long long timestamp = time(NULL); + asprintf(&query, "INSERT INTO statistics (timestamp, eventtypeid, servername, display, typeid) VALUES ('%lld', '%d', '%s', '%d', '%d')", timestamp, STATISTICS_SERVER_STOP_EVENT, safe_servername, -1, -1); + free(safe_servername); + 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); +} \ No newline at end of file diff --git a/raptorsmiface/libraptorsmiface.h b/raptorsmiface/libraptorsmiface.h index b27110bc..5d03b3e1 100644 --- a/raptorsmiface/libraptorsmiface.h +++ b/raptorsmiface/libraptorsmiface.h @@ -44,4 +44,6 @@ bool raptor_sm_sesslimit_reached(char* username); char raptor_sm_set_session_state(int display, int state); void raptor_sm_run_remote_desktop(char* username, int display, char* executable); void raptor_sm_terminate_server(char* username); -char* raptor_sm_get_hostname_for_display(int display); \ No newline at end of file +char* raptor_sm_get_hostname_for_display(int display); +void raptor_sm_stats_report_server_start(char* hostname); +void raptor_sm_stats_report_server_stop(char* hostname); \ No newline at end of file diff --git a/xrdp/xrdp.c b/xrdp/xrdp.c index 88743cc6..3acbb7e2 100644 --- a/xrdp/xrdp.c +++ b/xrdp/xrdp.c @@ -21,6 +21,9 @@ #include "xrdp.h" #include "log.h" +#include +#include "libraptorsmiface.h" + #if !defined(PACKAGE_VERSION) #define PACKAGE_VERSION "???" #endif @@ -589,6 +592,17 @@ main(int argc, char **argv) /* end of daemonizing code */ } + // Update statistics + char hostname[512]; + gethostname(hostname, 512); + struct hostent* hostinfo; + if(hostinfo=gethostbyname(hostname)) { + raptor_sm_stats_report_server_start(hostinfo->h_name); + } + else { + raptor_sm_stats_report_server_start(hostname); + } + g_threadid = tc_get_threadid(); g_listen = xrdp_listen_create(); g_signal_user_interrupt(xrdp_shutdown); /* SIGINT */ @@ -631,5 +645,13 @@ main(int argc, char **argv) g_free(startup_params); g_deinit(); + + if(hostinfo) { + raptor_sm_stats_report_server_stop(hostinfo->h_name); + } + else { + raptor_sm_stats_report_server_stop(hostname); + } + return 0; }