diff --git a/py/ps_fields.py b/py/ps_fields.py new file mode 100644 index 0000000..e23ea6c --- /dev/null +++ b/py/ps_fields.py @@ -0,0 +1,76 @@ +field_defs = { + 'PID': { + 'desc': 'PID', + 'dtype': str, + }, + 'PPID': { + 'desc': 'Parent PID', + 'dtype': str, + }, + 'UID': { + 'desc': 'User ID', + 'dtype': str, + }, + 'LXC': { + 'desc': 'LXC ID', + 'dtype': str, + }, + '%CUU': { + 'desc': 'CPU Use', + 'dtype': float, + }, + '%MEM': { + 'desc': 'Memory Use', + 'dtype': float, + }, + 'VSZ': { + 'desc': 'Virtual Memory', + 'dtype': int, + 'mult': 1024, + }, + 'RSS': { + 'desc': 'Resident Memory', + 'dtype': int, + 'mult': 1024, + }, + 'PSS': { + 'desc': 'Proportional Shared Memory', + 'dtype': int, + 'mult': 1024, + }, + 'USS': { + 'desc': 'Unique Memory', + 'dtype': int, + 'mult': 1024, + }, + 'SZ': { + 'desc': 'Core Size', + 'dtype': int, + 'mult': 4096, + }, + 'SIZE': { + 'desc': 'Approximate Memory', + 'dtype': int, + 'mult': 1024, + }, + 'THCNT': { + 'desc': 'Thread Count', + 'dtype': int, + }, + 'TIME': { + 'desc': 'Execution Time', + 'dtype': int, + }, + 'ELAPSED': { + 'desc': 'Elapsed Time', + 'dtype': int, + }, + 'STAT': { + 'desc': 'Status', + 'dtype': str, + }, + 'COMMAND': { + 'desc': 'Command', + 'dtype': str, + } +} diff --git a/py/ps_parse.py b/py/ps_parse.py index 5f1359b..265ca0d 100644 --- a/py/ps_parse.py +++ b/py/ps_parse.py @@ -1,86 +1,8 @@ -field_defs = { - 'PID': { - 'desc': 'PID', - 'dtype': str, - }, - 'PPID': { - 'desc': 'Parent PID', - 'dtype': str, - }, - 'UID': { - 'desc': 'User ID', - 'dtype': str, - }, - 'LXC': { - 'desc': 'LXC ID', - 'dtype': str, - }, - '%CUU': { - 'desc': 'CPU Use', - 'dtype': float, - }, - '%MEM': { - 'desc': 'Memory Use', - 'dtype': float, - }, - 'VSZ': { - 'desc': 'Virtual Memory', - 'dtype': int, - 'mult': 1024, - }, - 'RSS': { - 'desc': 'Resident Memory', - 'dtype': int, - 'mult': 1024, - }, - 'PSS': { - 'desc': 'Proportional Shared Memory', - 'dtype': int, - 'mult': 1024, - }, - 'USS': { - 'desc': 'Unique Memory', - 'dtype': int, - 'mult': 1024, - }, - 'SZ': { - 'desc': 'Core Size', - 'dtype': int, - 'mult': 4096, - }, - 'SIZE': { - 'desc': 'Approximate Memory', - 'dtype': int, - 'mult': 1024, - }, - 'THCNT': { - 'desc': 'Thread Count', - 'dtype': int, - }, - 'TIME': { - 'desc': 'Execution Time', - 'dtype': int, - }, - 'ELAPSED': { - 'desc': 'Elapsed Time', - 'dtype': int, - }, - 'STAT': { - 'desc': 'Status', - 'dtype': str, - }, - 'COMMAND': { - 'desc': 'Command', - 'dtype': str, - } -} +from ps_fields import * +import mongodb -def parseFile(fname): - f = open(fname, 'r') - data = f.read() - f.close() - - data = data.split('\n') +def parsePsOutput(output): + data = output.split('\n') data = list(filter(bool, [line.split() for line in data])) header = data[0] procs = [] @@ -91,11 +13,21 @@ def parseFile(fname): field = field_defs[header[i]] value = line[i] if i == len(header): + # combine command arguments together to one string value = ' '.join(line[i:]) + value = field['dtype'](value) if 'mult' in field: + # mult if applicable value *= field['mult'] + proc[header[i]] = value + procs.append(proc) return procs - + +def parseFile(fname): + f = open(fname, 'r') + data = f.read() + f.close() + return parsePsOutput(data)