From 48a5b65a3e02431d17856ead120a2dda52146e94 Mon Sep 17 00:00:00 2001 From: Mike Blume Date: Thu, 10 Oct 2013 12:30:58 -0700 Subject: [PATCH 1/6] move version compatibility check later make sure we have oppurtunity to log user details before we abort --- configure-syslog.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/configure-syslog.py b/configure-syslog.py index 7c63cfe..02ff800 100755 --- a/configure-syslog.py +++ b/configure-syslog.py @@ -1468,12 +1468,6 @@ def main(): options = parse_options() global LOGGLY_QA LOGGLY_QA = os.environ.get('LOGGLY_QA', '').split() - version_compatibility_check(MINIMUM_SUPPORTED_PYTHON_VERSION) - - if options.action == 'loggly_help': - loggly_help() - sys.exit() - log({"status":"start", "args": vars(options)}) current_environment = get_environment_details() current_environment['options'] = options @@ -1481,7 +1475,13 @@ def main(): "operating_system": current_environment['operating_system'], "syslog_versions": [ {"daemon": d, "version": v} for d,v in current_environment['syslog_versions'] ] }) + version_compatibility_check(MINIMUM_SUPPORTED_PYTHON_VERSION) assert_os() + + if options.action == 'loggly_help': + loggly_help() + sys.exit() + call_module(options.action, current_environment) printMessage("Finished") log({"status":"finish", "args": vars(options)}) From 9715ecb7ca02ef80fec4c2a3a55163af3865cb65 Mon Sep 17 00:00:00 2001 From: Mike Blume Date: Thu, 10 Oct 2013 12:39:34 -0700 Subject: [PATCH 2/6] don't use bytes literals -- python2.5 doesn't like them --- configure-syslog.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/configure-syslog.py b/configure-syslog.py index 02ff800..fdae43c 100755 --- a/configure-syslog.py +++ b/configure-syslog.py @@ -536,18 +536,14 @@ def find_syslog_process(): if results: #For python version 3 and above, reading binary data, not str, #so we need to decode the output first: - reslines = results.split(b'\n') + reslines = results.split('\n') if len(reslines) == 1: ps_out_fields = reslines[0].split() pid = int(ps_out_fields[1]) progname = ps_out_fields[7] - if b'/' in progname: - progname = progname.split(b'/')[-1] - try: - return (progname.decode('UTF-8'), pid) - except ValueError: - # if progname won't decode, it's not a progname we know. - pass + if '/' in progname: + progname = progname.split('/')[-1] + return progname, pid return None, 0 def check_syslog_service_status(syslog_type): From fed37cc47aa8050b6fba032cc270b090ddc9ab40 Mon Sep 17 00:00:00 2001 From: Mike Blume Date: Thu, 10 Oct 2013 12:48:37 -0700 Subject: [PATCH 3/6] catch exceptions using python 2.5 syntax =( --- configure-syslog.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/configure-syslog.py b/configure-syslog.py index fdae43c..bff2298 100755 --- a/configure-syslog.py +++ b/configure-syslog.py @@ -797,17 +797,17 @@ def get_json_data(url, user, password): req.add_header("Authorization", "Basic " + str(user_passwd.rstrip().decode("utf-8"))) return json.loads(urllib_request.urlopen(req).read().decode("utf-8")) - except urllib_request.HTTPError as e: + except urllib_request.HTTPError, e: if e.code == 401: msg = STR_AUTHENTICATION_FAIL_MESSAGE % USER else: msg = str(e) printLog("%s" % msg) sys_exit(reason = "%s" % msg) - except urllib_request.URLError as e: + except urllib_request.URLError, e: printLog("%s" % e) sys_exit(reason = "%s" % e) - except Exception as e: + except Exception, e: printLog("Exception %s" % e) sys_exit(reason = "%s" % e) @@ -838,7 +838,7 @@ def get_auth_token(loggly_user, loggly_password, loggly_subdomain): printLog("Loggly credentials could not be verified.") sys_exit(reason = "Loggly credentials could not be verified.") - except Exception as e: + except Exception, e: printLog("Exception %s" % e) sys_exit(reason = "%s" % e) @@ -951,7 +951,7 @@ def create_loggly_config_file(syslog_id, syslog_configuration_details, return - except IOError as e: + except IOError, e: printLog("IOError %s" % e) def modify_syslog_config_file(syslog_id, syslog_configuration_details, @@ -1175,7 +1175,7 @@ def write_env_details(current_environment): printLog("Created environment details file at %s, " "please visit http://loggly.com/docs/sending-logs-unixlinux-system-setup/ for more information." % file_path) printEnvironment(current_environment) - except Exception as e: + except Exception, e: printLog("Error %s" % e) sys_exit(reason = "Error %s" % e) @@ -1203,7 +1203,7 @@ def log(d, prio = 'info', facility = 'local0'): global _LOG_SOCKET try: pri = LOG_PRIORITIES[prio] + LOG_FACILITIES[facility] - except KeyError as errmsg: + except KeyError, errmsg: pass msg_dict = {"version": OUR_VERSION} @@ -1485,7 +1485,7 @@ def main(): #Python3 and above throw error print("\nAborting...") log({"status":"aborted", "args": vars(options), "msg":"KeyboardInterrupt" }) - except Exception as e: + except Exception, e: try: trace = traceback.format_exc() printLog(trace) From 6794eb78a7d0d241ee60538d7323bc873c598b42 Mon Sep 17 00:00:00 2001 From: Mike Blume Date: Thu, 10 Oct 2013 13:26:44 -0700 Subject: [PATCH 4/6] separate out function to log string --- configure-syslog.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/configure-syslog.py b/configure-syslog.py index bff2298..fb447b2 100755 --- a/configure-syslog.py +++ b/configure-syslog.py @@ -1199,16 +1199,16 @@ def log(d, prio = 'info', facility = 'local0'): Send a log message to Loggly; send a UDP datagram to Loggly rather than risk blocking. """ + msg_dict = {"version": OUR_VERSION} + msg_dict.update(d) + log_msg(json.dumps(msg_dict)) +def log_msg(msg, prio='info', facility='local0'): global _LOG_SOCKET try: pri = LOG_PRIORITIES[prio] + LOG_FACILITIES[facility] except KeyError, errmsg: pass - - msg_dict = {"version": OUR_VERSION} - msg_dict.update(d) - vals = { 'pri': pri, 'version': 1, @@ -1219,7 +1219,7 @@ def log(d, prio = 'info', facility = 'local0'): 'msgid': '-', 'loggly-auth-token': LOGGLY_AUTH_TOKEN, 'loggly-pen': int(DISTRIBUTION_ID), - 'msg': json.dumps(msg_dict) + 'msg': msg } fullmsg = ("<%(pri)s>%(version)s %(timestamp)s %(hostname)s " From 6fbb315b7a57b9b0cde92760133c2c6ea7e75dfe Mon Sep 17 00:00:00 2001 From: Mike Blume Date: Thu, 10 Oct 2013 13:36:25 -0700 Subject: [PATCH 5/6] pull out function for py version --- configure-syslog.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/configure-syslog.py b/configure-syslog.py index fb447b2..4a6d6fe 100755 --- a/configure-syslog.py +++ b/configure-syslog.py @@ -316,6 +316,9 @@ def sendEnvironment(data): printLog("Sending environment details to Loggly Server.") log(data) +def get_python_version_string(): + return ".".join(map(str, sys.version_info)) + def sys_exit(reason = None): """ If script fails, send environment details with reason for failure to loggly @@ -324,7 +327,7 @@ def sys_exit(reason = None): data = { "operating_system": current_environment['operating_system'], "syslog_versions": current_environment['syslog_versions'], - "python_version": ".".join(map(str, sys.version_info)), + "python_version": get_python_version_string(), "reason":reason, "username":USER, "subdomain": SUBDOMAIN From aeca45ef57acdb39793a95a79e3fce0c5b8c9a5d Mon Sep 17 00:00:00 2001 From: Mike Blume Date: Thu, 10 Oct 2013 13:36:43 -0700 Subject: [PATCH 6/6] handle lack of json --- configure-syslog.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/configure-syslog.py b/configure-syslog.py index 4a6d6fe..82215e9 100755 --- a/configure-syslog.py +++ b/configure-syslog.py @@ -29,7 +29,10 @@ import urllib.request as urllib_request except ImportError: import urllib2 as urllib_request -import json +try: + import json +except ImportError: + json = None import uuid import base64 import socket @@ -1465,6 +1468,13 @@ def main(): try: printMessage("Starting") options = parse_options() + if json is None: + version = get_python_version_string() + log_msg('''{"python_version": "%s", "subdomain": "%s"}''' % + (version, options.subdomain)) + printMessage(STR_PYTHON_FAIL_MESSAGE % + (version, MINIMUM_SUPPORTED_PYTHON_VERSION)) + sys.exit(-1) global LOGGLY_QA LOGGLY_QA = os.environ.get('LOGGLY_QA', '').split() log({"status":"start", "args": vars(options)})