2929 import urllib .request as urllib_request
3030except ImportError :
3131 import urllib2 as urllib_request
32- import json
32+ try :
33+ import json
34+ except ImportError :
35+ json = None
3336import uuid
3437import base64
3538import socket
@@ -316,6 +319,9 @@ def sendEnvironment(data):
316319 printLog ("Sending environment details to Loggly Server." )
317320 log (data )
318321
322+ def get_python_version_string ():
323+ return "." .join (map (str , sys .version_info ))
324+
319325def sys_exit (reason = None ):
320326 """
321327 If script fails, send environment details with reason for failure to loggly
@@ -324,7 +330,7 @@ def sys_exit(reason = None):
324330 data = {
325331 "operating_system" : current_environment ['operating_system' ],
326332 "syslog_versions" : current_environment ['syslog_versions' ],
327- "python_version" : "." . join ( map ( str , sys . version_info ) ),
333+ "python_version" : get_python_version_string ( ),
328334 "reason" :reason ,
329335 "username" :USER ,
330336 "subdomain" : SUBDOMAIN
@@ -536,18 +542,14 @@ def find_syslog_process():
536542 if results :
537543 #For python version 3 and above, reading binary data, not str,
538544 #so we need to decode the output first:
539- reslines = results .split (b '\n ' )
545+ reslines = results .split ('\n ' )
540546 if len (reslines ) == 1 :
541547 ps_out_fields = reslines [0 ].split ()
542548 pid = int (ps_out_fields [1 ])
543549 progname = ps_out_fields [7 ]
544- if b'/' in progname :
545- progname = progname .split (b'/' )[- 1 ]
546- try :
547- return (progname .decode ('UTF-8' ), pid )
548- except ValueError :
549- # if progname won't decode, it's not a progname we know.
550- pass
550+ if '/' in progname :
551+ progname = progname .split ('/' )[- 1 ]
552+ return progname , pid
551553 return None , 0
552554
553555def check_syslog_service_status (syslog_type ):
@@ -801,17 +803,17 @@ def get_json_data(url, user, password):
801803 req .add_header ("Authorization" ,
802804 "Basic " + str (user_passwd .rstrip ().decode ("utf-8" )))
803805 return json .loads (urllib_request .urlopen (req ).read ().decode ("utf-8" ))
804- except urllib_request .HTTPError as e :
806+ except urllib_request .HTTPError , e :
805807 if e .code == 401 :
806808 msg = STR_AUTHENTICATION_FAIL_MESSAGE % USER
807809 else :
808810 msg = str (e )
809811 printLog ("%s" % msg )
810812 sys_exit (reason = "%s" % msg )
811- except urllib_request .URLError as e :
813+ except urllib_request .URLError , e :
812814 printLog ("%s" % e )
813815 sys_exit (reason = "%s" % e )
814- except Exception as e :
816+ except Exception , e :
815817 printLog ("Exception %s" % e )
816818 sys_exit (reason = "%s" % e )
817819
@@ -842,7 +844,7 @@ def get_auth_token(loggly_user, loggly_password, loggly_subdomain):
842844 printLog ("Loggly credentials could not be verified." )
843845 sys_exit (reason = "Loggly credentials could not be verified." )
844846
845- except Exception as e :
847+ except Exception , e :
846848 printLog ("Exception %s" % e )
847849 sys_exit (reason = "%s" % e )
848850
@@ -955,7 +957,7 @@ def create_loggly_config_file(syslog_id, syslog_configuration_details,
955957 return
956958
957959
958- except IOError as e :
960+ except IOError , e :
959961 printLog ("IOError %s" % e )
960962
961963def modify_syslog_config_file (syslog_id , syslog_configuration_details ,
@@ -1179,7 +1181,7 @@ def write_env_details(current_environment):
11791181 printLog ("Created environment details file at %s, "
11801182 "please visit http://loggly.com/docs/sending-logs-unixlinux-system-setup/ for more information." % file_path )
11811183 printEnvironment (current_environment )
1182- except Exception as e :
1184+ except Exception , e :
11831185 printLog ("Error %s" % e )
11841186 sys_exit (reason = "Error %s" % e )
11851187
@@ -1203,16 +1205,16 @@ def log(d, prio = 'info', facility = 'local0'):
12031205 Send a log message to Loggly;
12041206 send a UDP datagram to Loggly rather than risk blocking.
12051207 """
1208+ msg_dict = {"version" : OUR_VERSION }
1209+ msg_dict .update (d )
1210+ log_msg (json .dumps (msg_dict ))
12061211
1212+ def log_msg (msg , prio = 'info' , facility = 'local0' ):
12071213 global _LOG_SOCKET
12081214 try :
12091215 pri = LOG_PRIORITIES [prio ] + LOG_FACILITIES [facility ]
1210- except KeyError as errmsg :
1216+ except KeyError , errmsg :
12111217 pass
1212-
1213- msg_dict = {"version" : OUR_VERSION }
1214- msg_dict .update (d )
1215-
12161218 vals = {
12171219 'pri' : pri ,
12181220 'version' : 1 ,
@@ -1223,7 +1225,7 @@ def log(d, prio = 'info', facility = 'local0'):
12231225 'msgid' : '-' ,
12241226 'loggly-auth-token' : LOGGLY_AUTH_TOKEN ,
12251227 'loggly-pen' : int (DISTRIBUTION_ID ),
1226- 'msg' : json . dumps ( msg_dict )
1228+ 'msg' : msg
12271229 }
12281230
12291231 fullmsg = ("<%(pri)s>%(version)s %(timestamp)s %(hostname)s "
@@ -1466,30 +1468,37 @@ def main():
14661468 try :
14671469 printMessage ("Starting" )
14681470 options = parse_options ()
1471+ if json is None :
1472+ version = get_python_version_string ()
1473+ log_msg ('''{"python_version": "%s", "subdomain": "%s"}''' %
1474+ (version , options .subdomain ))
1475+ printMessage (STR_PYTHON_FAIL_MESSAGE %
1476+ (version , MINIMUM_SUPPORTED_PYTHON_VERSION ))
1477+ sys .exit (- 1 )
14691478 global LOGGLY_QA
14701479 LOGGLY_QA = os .environ .get ('LOGGLY_QA' , '' ).split ()
1471- version_compatibility_check (MINIMUM_SUPPORTED_PYTHON_VERSION )
1472-
1473- if options .action == 'loggly_help' :
1474- loggly_help ()
1475- sys .exit ()
1476-
14771480 log ({"status" :"start" , "args" : vars (options )})
14781481 current_environment = get_environment_details ()
14791482 current_environment ['options' ] = options
14801483 log ({
14811484 "operating_system" : current_environment ['operating_system' ],
14821485 "syslog_versions" : [ {"daemon" : d , "version" : v } for d ,v in current_environment ['syslog_versions' ] ]
14831486 })
1487+ version_compatibility_check (MINIMUM_SUPPORTED_PYTHON_VERSION )
14841488 assert_os ()
1489+
1490+ if options .action == 'loggly_help' :
1491+ loggly_help ()
1492+ sys .exit ()
1493+
14851494 call_module (options .action , current_environment )
14861495 printMessage ("Finished" )
14871496 log ({"status" :"finish" , "args" : vars (options )})
14881497 except KeyboardInterrupt :
14891498 #Python3 and above throw error
14901499 print ("\n Aborting..." )
14911500 log ({"status" :"aborted" , "args" : vars (options ), "msg" :"KeyboardInterrupt" })
1492- except Exception as e :
1501+ except Exception , e :
14931502 try :
14941503 trace = traceback .format_exc ()
14951504 printLog (trace )
0 commit comments