Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save geoffreyanderson/4508084 to your computer and use it in GitHub Desktop.
Save geoffreyanderson/4508084 to your computer and use it in GitHub Desktop.
This patch can be applied to the mysql client in Percona-Server-5.5.28-rel29.3 to add a new option "--identifiable-comment" (defaulted to on) that appends a comment in the form of "user=os_username&host=source_host&pid=pid_of_mysql_client" to every query sent from the mysql client.
--- client/mysql.cc 2013-01-06 22:29:49.000000000 -0800
+++ client/mysql.cc 2013-01-10 20:31:12.000000000 -0800
@@ -42,6 +42,7 @@
#include <violite.h>
#ifndef __WIN__
#include "syslog.h"
+#include <pwd.h>
#endif
#define MAX_SYSLOG_MESSAGE 900
@@ -140,8 +141,8 @@
static MYSQL mysql; /* The connection */
static my_bool ignore_errors=0,wait_flag=0,quick=0,
connected=0,opt_raw_data=0,unbuffered=0,output_tables=0,
- opt_rehash=1,skip_updates=0,safe_updates=0,one_database=0,
- opt_compress=0, using_opt_local_infile=0,
+ opt_rehash=1,skip_updates=0,safe_updates=0,identifiable_comment=1,
+ one_database=0,opt_compress=0, using_opt_local_infile=0,
vertical=0, line_numbers=1, column_names=1,opt_html=0,
opt_xml=0,opt_nopager=1, opt_outfile=0, named_cmds= 0,
tty_password= 0, opt_nobeep=0, opt_reconnect=1,
@@ -1548,6 +1549,11 @@
{"i-am-a-dummy", 'U', "Synonym for option --safe-updates, -U.",
&safe_updates, &safe_updates, 0, GET_BOOL, NO_ARG, 0, 0,
0, 0, 0, 0},
+#ifndef __WIN__
+ {"identifiable-comment", 'O', "Append an identifiable comment to queries",
+ &identifiable_comment, &identifiable_comment, 0, GET_BOOL, NO_ARG, 1, 0,
+ 0, 0, 0, 0},
+#endif
{"verbose", 'v', "Write more. (-v -v -v gives the table output format).", 0,
0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
{"version", 'V', "Output version information and exit.", 0, 0, 0,
@@ -3005,6 +3011,52 @@
return 0;
}
+#ifndef __WIN__
+/*
+ Creates an "identifiable comment" to append to a query. This comment will be in the form of:
+ user=os_username&host=source_host&pid=pid_of_mysql_client
+*/
+static String build_identifiable_comment()
+{
+ String comment;
+ const char *prefix = " /** ";
+ const char *suffix = " */";
+
+ comment.append(prefix);
+ comment.append("user=");
+ struct passwd *pw;
+ pw = getpwuid(geteuid());
+ if (pw)
+ {
+ comment.append(pw->pw_name);
+ }
+ else
+ {
+ comment.append("");
+ }
+
+ char hostname[1024];
+ hostname[1023] = '\0';
+ int gethostname_success = gethostname(hostname, 1023);
+ if(gethostname_success == 0)
+ {
+ comment.append("&host=");
+ comment.append(hostname);
+ }
+
+ unsigned int pid_length = sizeof(pid_t);
+ char pid_str[pid_length];
+
+ sprintf(pid_str, "%d", (int)getpid());
+ comment.append("&pid=");
+ comment.append(pid_str);
+
+ comment.append(suffix);
+
+ return comment;
+}
+#endif
+
/*
Execute command
Returns: 0 if ok
@@ -3058,6 +3110,14 @@
return 0;
}
+#ifndef __WIN__
+ if (identifiable_comment)
+ {
+ /* Append an identifiable comment to the end of the query */
+ buffer->append(build_identifiable_comment());
+ }
+#endif
+
timer=start_timer();
executing_query= 1;
error= mysql_real_query_for_lazy(buffer->ptr(),buffer->length());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment