|
| 1 | + |
| 2 | +from builtins import str |
| 3 | +#!/usr/bin/env python |
| 4 | +# |
| 5 | +# Copyright (c) 2015-2018 Intel, Inc. All rights reserved. |
| 6 | +# $COPYRIGHT$ |
| 7 | +# |
| 8 | +# Additional copyrights may follow |
| 9 | +# |
| 10 | +# $HEADER$ |
| 11 | +# |
| 12 | + |
| 13 | +import sys |
| 14 | +import datetime |
| 15 | +from BaseMTTUtility import * |
| 16 | +import json |
| 17 | +import os |
| 18 | +import pwd |
| 19 | +import grp |
| 20 | + |
| 21 | +## @addtogroup Utilities |
| 22 | +# @{ |
| 23 | +# @section ElkLogger |
| 24 | +# Log results to *.elog files with JSON entries to be later consumed by Elastic Stack (ELK) |
| 25 | +# Uses pymtt.py options: elk_head, elk_id, elk_testcycle, elk_testcase, elk_debug, elk_chown, elk_maxsize, elk_nostdout, elk_nostderr |
| 26 | +# @} |
| 27 | +class ElkLogger(BaseMTTUtility): |
| 28 | + def __init__(self): |
| 29 | + BaseMTTUtility.__init__(self) |
| 30 | + self.options = {} |
| 31 | + self.elk_log = None |
| 32 | + self.execmds_stash = [] |
| 33 | + |
| 34 | + def print_name(self): |
| 35 | + return "ElkLogger" |
| 36 | + |
| 37 | + def print_options(self, testDef, prefix): |
| 38 | + lines = testDef.printOptions(self.options) |
| 39 | + for line in lines: |
| 40 | + print(prefix + line) |
| 41 | + sys.stdout.flush() |
| 42 | + return |
| 43 | + |
| 44 | + def log_to_elk(self, result, logtype, testDef): |
| 45 | + result = result.copy() |
| 46 | + if testDef.options['elk_head'] is None or testDef.options['elk_id'] is None: |
| 47 | + testDef.logger.verbose_print('Error: entered log_to_elk() function without elk_id and elk_head specified') |
| 48 | + return |
| 49 | + |
| 50 | + result = {k:(str(v) if isinstance(v, datetime.datetime) |
| 51 | + or isinstance(v, datetime.date) else v) |
| 52 | + for k,v in list(result.items())} |
| 53 | + result['execid'] = testDef.options['elk_id'] |
| 54 | + result['cycleid'] = testDef.options['elk_testcycle'] |
| 55 | + result['caseid'] = testDef.options['elk_testcase'] |
| 56 | + result['logtype'] = logtype |
| 57 | + |
| 58 | + # drop the stderr and stdout, will use the copies that are part of the commands issues |
| 59 | + if 'stderr' in result: |
| 60 | + del result['stderr'] |
| 61 | + if 'stdout' in result: |
| 62 | + del result['stdout'] |
| 63 | + |
| 64 | + # convert ini_files, lists of lists into a comma separated string |
| 65 | + if 'options' in result and 'ini_files' in result['options']: |
| 66 | + result['options']['ini_files'] = ','.join(t[0] for t in result['options']['ini_files']) |
| 67 | + |
| 68 | + if logtype == 'mtt-sec': |
| 69 | + # convert comamands, list of dicts into a dictionary |
| 70 | + if self.execmds_stash: |
| 71 | + result['commands'] = {"command{}".format(i):c for i,c in enumerate(self.execmds_stash)} |
| 72 | + self.execmds_stash = [] |
| 73 | + |
| 74 | + # convert parameters, list of lists into a dictionary |
| 75 | + if 'parameters' in result: |
| 76 | + result['parameters'] = { p[0]:p[1] for p in result['parameters'] } |
| 77 | + |
| 78 | + # convert profile, list of lists into a dictionary |
| 79 | + if 'profile' in result: |
| 80 | + result['profile'] = { k:' '.join(v) for k,v in list(result['profile'].items()) } |
| 81 | + |
| 82 | + if testDef.options['elk_debug']: |
| 83 | + testDef.logger.verbose_print('Logging to elk_head={}/{}.elog: {}'.format(testDef.options['elk_head'], testDef.options['elk_id'], result)) |
| 84 | + |
| 85 | + if self.elk_log is None: |
| 86 | + allpath = '/' |
| 87 | + for d in os.path.normpath(testDef.options['elk_head']).split(os.path.sep): |
| 88 | + allpath = os.path.join(allpath, d) |
| 89 | + if not os.path.exists(allpath): |
| 90 | + os.mkdir(allpath) |
| 91 | + uid = None |
| 92 | + gid = None |
| 93 | + if testDef.options['elk_chown'] is not None and ':' in testDef.options['elk_chown']: |
| 94 | + user = testDef.options['elk_chown'].split(':')[0] |
| 95 | + group = testDef.options['elk_chown'].split(':')[1] |
| 96 | + uid = pwd.getpwnam(user).pw_uid |
| 97 | + gid = grp.getgrnam(group).gr_gid |
| 98 | + os.chown(allpath, uid, gid) |
| 99 | + self.elk_log = open(os.path.join(testDef.options['elk_head'], '{}-{}.elog'.format(testDef.options['elk_testcase'], testDef.options['elk_id'])), 'a+') |
| 100 | + self.elk_log.write(json.dumps(result) + '\n') |
| 101 | + return |
| 102 | + |
| 103 | + def log_execmd_elk(self, cmdargs, status, stdout, stderr, timedout, starttime, endtime, elapsed_secs, slurm_job_ids, testDef): |
| 104 | + if testDef.options['elk_id'] is not None: |
| 105 | + if testDef.options['elk_maxsize'] is not None: |
| 106 | + try: |
| 107 | + maxsize = int(testDef.options['elk_maxsize']) |
| 108 | + except: |
| 109 | + maxsize = None |
| 110 | + if maxsize is not None and len(stdout) > maxsize: |
| 111 | + if maxsize > 0: |
| 112 | + stdout = ['<truncated>'] + stdout[-maxsize:] |
| 113 | + else: |
| 114 | + stdout = ['<truncated>'] |
| 115 | + if maxsize is not None and len(stderr) > maxsize: |
| 116 | + if maxsize > 0: |
| 117 | + stderr = ['<truncated>'] + stderr[-maxsize:] |
| 118 | + else: |
| 119 | + stderr = ['<truncated>'] |
| 120 | + self.execmds_stash.append({'cmdargs': ' '.join(cmdargs), |
| 121 | + 'status': status, |
| 122 | + 'stdout': stdout if testDef.options['elk_nostdout'] is not None else ['<ignored>'], |
| 123 | + 'stderr': stderr if testDef.options['elk_nostderr'] is not None else ['<ignored>'], |
| 124 | + 'timedout': timedout, |
| 125 | + 'starttime': str(starttime), |
| 126 | + 'endtime': str(endtime), |
| 127 | + 'elapsed': elapsed_secs, |
| 128 | + 'slurm_job_ids': ','.join([str(j) for j in slurm_job_ids]) |
| 129 | + }) |
0 commit comments