Changeset 7167 for lliurex-statistics


Ignore:
Timestamp:
Apr 24, 2018, 5:59:33 PM (18 months ago)
Author:
mabarracus
Message:

Fix bugs , switch to threaded code

Location:
lliurex-statistics/trunk/fuentes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • lliurex-statistics/trunk/fuentes/debian/changelog

    r7164 r7167  
     1lliurex-statistics (0.5.4) xenial; urgency=medium
     2
     3  * Fix bugs , switch to threaded code
     4
     5 -- M.Angel Juan <m.angel.juan@gmail.com>  Tue, 24 Apr 2018 17:54:54 +0200
     6
    17lliurex-statistics (0.5.3.3) xenial; urgency=medium
    28
  • lliurex-statistics/trunk/fuentes/lliurex-statistics.install/usr/sbin/analytics

    r7164 r7167  
    88import requests
    99import json
    10 import configparser
     10try:
     11    import configparser
     12except:
     13    import ConfigParser as configparser
    1114import daemon
    12 from xmlrpc import client
     15try:
     16    from xmlrpc import client
     17except:
     18    import xmlrpclib as client
    1319import lockfile
    1420import logging.handlers
     
    1925
    2026oldsignals = {}
    21 for sig in signal.Signals:
    22     try:
    23         oldsignals.setdefault(sig.name, signal.getsignal(sig))
    24         signal.signal(sig.value, signal.SIG_IGN)
    25     except:
    26         continue
     27try:
     28    for sig in signal.Signals:
     29        try:
     30            oldsignals.setdefault(sig.name, signal.getsignal(sig))
     31            signal.signal(sig.value, signal.SIG_IGN)
     32        except:
     33            continue
     34except:
     35    allsignals = [s for s in dir(signal) if s[0:3] == 'SIG']
     36    for sig in allsignals:
     37        try:
     38            oldsignals.setdefault(sig,getattr(signal,sig))
     39            signal.signal(getattr(signal,sig), signal.SIG_IGN)
     40        except:
     41            continue
    2742
    2843#
     
    4055# OVERRIDE_SEND_PERMISSION   = 1
    4156
    42 # MIN_LOG_LEVEL = 'info'
     57# MIN_LOG_LEVEL = 'debug'
    4358
    4459# DAEMON_MODE = 1
     
    166181    global log
    167182
    168     for sect in config:
    169         for key in config[sect]:
    170             log.debug("Config Loaded: '{}' '{}' '{}'".format(sect, key, config[sect][key]))
     183    try:
     184        for sect in config:
     185            for key in config[sect]:
     186                log.debug("Config Loaded: '{}' '{}' '{}'".format(sect, key, config[sect][key]))
     187    except:
     188        for sect in config.sections():
     189            for key in config.options(sect):
     190                log.debug("Config Loaded: '{}' '{}' '{}'".format(sect, key, config.get(sect,key)))
    171191
    172192
     
    208228def get_llx_version():
    209229    global log
    210 
    211     output = bin_to_ascii(subprocess.check_output(['lliurex-version','-n']))
     230    if sys.version_info[0] > 2:
     231        output = bin_to_ascii(subprocess.check_output(['lliurex-version','-n']))
     232    else:
     233        output = bin_to_ascii(subprocess.check_output(['bash','lliurex-version','-n']))
    212234    release = output[0:2].strip()
    213235    if release == '15':
     
    215237        output = bin_to_ascii(subprocess.check_output(use))
    216238    else:
    217         use = ['lliurex-version','-f']
     239        if sys.version_info[0] > 2:
     240            use = ['lliurex-version','-f']
     241        else:
     242            use = ['bash','lliurex-version','-f']
    218243        output = bin_to_ascii(subprocess.check_output(use))
    219244    flavour = output.strip()
     
    334359            glob['TERMINATE'] = True
    335360
    336         fp = subprocess.Popen(['tail', '-F', logfilename], stdout=subprocess.PIPE, stderr=open(os.devnull, 'w'))
     361        fp = subprocess.Popen(['tail', '-n', '0', '-F', logfilename], stdout=subprocess.PIPE, stderr=open(os.devnull, 'w'))
    337362        glob['monitor_pid'] = fp.pid
    338363    except Exception as e:
     
    358383
    359384    except Exception as e:
    360         if isinstance(e, ConnectionResetError):
    361             log.info('Connection reset exitting monitor thread')
    362             glob['TERMINATE'] = True
    363             return
    364         else:
    365             log.error('Error reading file {}, {}'.format(logfilename, e))
    366             glob['TERMINATE'] = True
    367             return
     385        try:
     386            if isinstance(e, ConnectionResetError):
     387                log.info('Connection reset exitting monitor thread')
     388                glob['TERMINATE'] = True
     389                return
     390            else:
     391                log.error('Error reading file {}, {}'.format(logfilename, e))
     392                glob['TERMINATE'] = True
     393                return
     394        except:
     395            if e.errno == 32:
     396                log.info('Connection reset exitting monitor thread')
     397                glob['TERMINATE'] = True
     398                return
     399            else:
     400                log.error('Error reading file {}, {}'.format(logfilename, e))
     401                glob['TERMINATE'] = True
     402                return
    368403
    369404    log.info('Exitting monitor thread')
     
    374409
    375410    log.info('Start update list')
    376     try:
    377         list_path = get_var_value('list_path', glob['config'], mode='string', section='Server')
    378         server = get_var_value('server', glob['config'], mode='string', section='Server')
    379         url = 'http://' + server + '/' + list_path
    380         agent = glob['user_agent']
    381         headers = {'user-agent': agent}
    382     except Exception as e:
    383         log.warning('Error gettting update list settings {}'.format(e))
     411    retry = 3
     412    done = False
     413    while retry > 0:
     414        try:
     415            list_path = get_var_value('list_path', glob['config'], mode='string', section='Server')
     416            server = get_var_value('server', glob['config'], mode='string', section='Server')
     417            url = 'http://' + server + '/' + list_path
     418            agent = glob['user_agent']
     419            headers = {'user-agent': agent}
     420            retry = 0
     421            done = True
     422        except Exception as e:
     423            retry -= 1
     424            done = e
     425
     426    if done != True :
     427        log.error('Error gettting update list settings {}'.format(done))
    384428
    385429    log.debug('List path {}'.format(url))
     
    450494def timed_send():
    451495    global glob, log
    452 
    453496    log.debug('Start timed_send ')
    454497    try:
     
    476519
    477520    except Exception as e:
    478         if isinstance(e, ConnectionResetError):
    479             log.info('Connection reset exitting timer thread')
    480         else:
    481             log.error('Error with timed send, {}'.format(e))
     521        try:
     522            if isinstance(e, ConnectionResetError):
     523                log.info('Connection reset exitting timer thread')
     524            else:
     525                log.error('Error with timed send, {}'.format(e))
     526        except:
     527            if e.errno == 32:
     528                log.info('Connection reset exitting timer thread')
     529            else:
     530                log.error('Error with timed send, {}'.format(e))
    482531
    483532    log.info('Exitting timer thread')
     
    485534
    486535
    487 def start(*args, daemon_mode=False,release='Unknown',flavour='Unknown',proxy=False, **kwargs):
    488     global THREADS, oldsignals, log, CONFIG, glob
     536def start(daemon_mode=False,release='Unknown',flavour='Unknown',proxy=False, **kwargs):
     537    global THREADS, oldsignals, log, CONFIG, glob, mgr
    489538
    490539    log.info("Starting analytics")
     
    526575        log.warning('Error getting user-agent, {}'.format(e))
    527576
    528     # write pid
     577        # write pid
    529578    try:
    530579        with open(pidfile, 'w') as fp:
     
    538587
    539588    glob['platform_data'] = get_platform_data()
    540 
    541     THREADS = dict()
    542     THREADS['monitor'] = Process(target=monitor, name='monitor')
    543     THREADS['monitor'].daemon = glob['DAEMON_MODE']
    544 
    545     THREADS['timed_send'] = Process(target=timed_send, name='timed_send')
    546     THREADS['timed_send'].daemon = glob['DAEMON_MODE']
    547 
    548     THREADS['update_list'] = Process(target=update_list, name='update_list')
    549     THREADS['update_list'].daemon = glob['DAEMON_MODE']
    550 
    551     THREADS['monitor'].start()
    552     THREADS['timed_send'].start()
    553     THREADS['update_list'].start()
    554589
    555590    signals = {'SIGTERM': interrupt, 'SIGINT': interrupt, 'SIGUSR1': clean_and_send, 'SIGUSR2': show_captured}
     
    562597            except:
    563598                continue
     599
     600    THREADS = dict()
     601    THREADS['monitor'] = Process(target=monitor, name='monitor')
     602    THREADS['monitor'].daemon = glob['DAEMON_MODE']
     603
     604    THREADS['timed_send'] = Process(target=timed_send, name='timed_send')
     605    THREADS['timed_send'].daemon = glob['DAEMON_MODE']
     606
     607    THREADS['update_list'] = Process(target=update_list, name='update_list')
     608    THREADS['update_list'].daemon = glob['DAEMON_MODE']
     609
     610    THREADS['monitor'].start()
     611    THREADS['timed_send'].start()
     612    THREADS['update_list'].start()
    564613
    565614
     
    679728    subtype = { 'LTSP': None , 'MODE': None}
    680729    try:
    681         outtype = bin_to_ascii(subprocess.check_output(['lliurex-version -a -e'],shell=True)).strip()
     730        rel,fla = get_llx_version()
     731        if rel == '15':
     732            outtype = bin_to_ascii(subprocess.check_output(['lliurex-detect -a -e'],shell=True)).strip()
     733        else:
     734            outtype = bin_to_ascii(subprocess.check_output(['lliurex-version -a -e'],shell=True)).strip()
    682735        outtype = outtype.split('\n')
    683736        ltsp = None
     
    788841
    789842    log.info('Interrupting analytics')
    790    
    791843    try:
    792844        clean_and_send()
     
    798850            sys.exit(1)
    799851        for x in THREADS:
    800             THREADS[x].join()
    801             log.info('Interrupt: Joined {} {}'.format(x))
     852            THREADS[x].join(1)
     853            log.info('Interrupt: Joined {}'.format(x))
    802854
    803855    except Exception as e:
     
    915967        a=False
    916968        b=False
    917         for argument in proc.cmdline():
    918             #print('{} {} {}'.format(proc.cmdline(),keyword,argument[-len(keyword):]))
     969        cmd=None
     970        try:
     971            cmd = proc.cmdline()
     972        except:
     973            cmd = proc.cmdline
     974        for argument in cmd:
     975            #print('{} {} {}'.format(cmd,keyword,argument[-len(keyword):]))
    919976            if interpreter in argument[-len(interpreter):]:
    920977                a = True
     
    9651022
    9661023    log.debug('End main')
    967     for t in THREADS:
    968         THREADS[t].join()
    969         log.info('Joined {}'.format(t))
     1024    ended = False
     1025    while not ended:
     1026        ended = True
     1027        for t in THREADS:
     1028            THREADS[t].join(1)
     1029            if THREADS[t].exitcode == None:
     1030                ended = False
     1031            else:
     1032                log.info('Joined {}'.format(t))
    9701033    try:
    9711034        mgr.shutdown()
Note: See TracChangeset for help on using the changeset viewer.