You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
smart-top/smart/top/__init__.py

125 lines
4.3 KiB
Python

"""
This file contains class and functions that extract data from running processes like top and stores them into a data store of the calling codes choice
dependencies:
- top (on the os)
@TODO:
Test this thing on windows to see if it works
"""
import pandas as pd
import numpy as np
import subprocess
import os
import datetime
# from transport import factory
import sys
import hashlib
class Util:
def app(self,stream):
"""
Formatting application name, sometimes the name has parameters os separators ...
"""
index = 1 if os.path.exists(" ".join(stream[:1])) else len(stream)-1
cmd = " ".join(stream[:index]) if index > 0 else " ".join(stream)
if ' ' in cmd.split(os.sep)[len(cmd.split(os.sep))-1] :
p = cmd.split(os.sep)[len(cmd.split(os.sep))-1].split(' ')
name = p[0]
args = " ".join(p[1:])
else:
name = cmd.split('/')[len(cmd.split(os.sep))-1]
args = " ".join(stream[index:]) if index > 0 else ""
return [name,cmd,args]
def parse(self,rows,xchar=';'):
"""
This function parses the document returned by the execution of the command returns a document that will have to be parsed and formatted
"""
m = []
TIME_INDEX = 5
ARGS_INDEX = 6
for item in rows :
if rows.index(item) != 0 :
parts = item.split(xchar)
row = parts[:TIME_INDEX]
row.append(' '.join(parts[TIME_INDEX:ARGS_INDEX]))
row += self.app(parts[ARGS_INDEX:])
else:
row = item.split(xchar)
row = (xchar.join(row)).strip()
if len(row.replace(";","")) > 0 :
m.append(row)
return m
names = args['name'].split(',')