|
|
|
@ -7,6 +7,7 @@ import sys
|
|
|
|
|
import subprocess
|
|
|
|
|
import copy
|
|
|
|
|
import importlib
|
|
|
|
|
import psutil
|
|
|
|
|
#Input arg
|
|
|
|
|
# [
|
|
|
|
|
# {
|
|
|
|
@ -38,6 +39,13 @@ import importlib
|
|
|
|
|
#
|
|
|
|
|
# },
|
|
|
|
|
# {
|
|
|
|
|
# "Type":"ProcessStartIfTurnedOff",
|
|
|
|
|
# "CheckTaskName":"", #Check if current task name is not active (then start process),
|
|
|
|
|
# "Path":"",
|
|
|
|
|
# "ArgList":[]
|
|
|
|
|
#
|
|
|
|
|
# },
|
|
|
|
|
# {
|
|
|
|
|
# "Type":"ProcessStop",
|
|
|
|
|
# "Name":"",
|
|
|
|
|
# "FlagForce":True,
|
|
|
|
@ -71,6 +79,7 @@ def Activity(inActivity):
|
|
|
|
|
inActivity["DateTimeUTCStringStart"] = datetime.datetime.strftime(datetime.datetime.now(),"%Y-%m-%dT%H:%M:%S.%f")
|
|
|
|
|
#Alias (compatibility)
|
|
|
|
|
lItem = inActivity
|
|
|
|
|
lCurrentDateTime = datetime.datetime.now()
|
|
|
|
|
###########################################################
|
|
|
|
|
#Обработка запроса на отправку команды на удаленную машину
|
|
|
|
|
###########################################################
|
|
|
|
@ -138,10 +147,32 @@ def Activity(inActivity):
|
|
|
|
|
#Вид активности - запуск процесса
|
|
|
|
|
#Запись в массив отработанных активностей
|
|
|
|
|
#Лог
|
|
|
|
|
mGlobalDict["Processor"]["LogList"].append({"activityType":lItem["activityType"], "activityDateTime":str(lActivityDateTime), "processPath":lItem["processPath"], "activityStartDateTime":str(lCurrentDateTime)})
|
|
|
|
|
mGlobalDict["Processor"]["LogList"].append({"activityType":lItem["Type"], "activityDateTime":str(lActivityDateTime), "processPath":lItem["Path"], "activityStartDateTime":str(lCurrentDateTime)})
|
|
|
|
|
#Запустить процесс
|
|
|
|
|
lItemArgs=[lItem["Path"]]
|
|
|
|
|
lItemArgs.extend(lItem["ArgList"])
|
|
|
|
|
subprocess.Popen(lItemArgs,shell=True)
|
|
|
|
|
#####################################
|
|
|
|
|
#ProcessStartIfTurnedOff
|
|
|
|
|
#####################################
|
|
|
|
|
if lItem["Type"]=="ProcessStartIfTurnedOff":
|
|
|
|
|
#Check if process running
|
|
|
|
|
#remove .exe from Taskname if exists
|
|
|
|
|
lCheckTaskName = lItem["CheckTaskName"]
|
|
|
|
|
if len(lCheckTaskName)>4:
|
|
|
|
|
if lCheckTaskName[-4:].upper() != ".EXE":
|
|
|
|
|
lCheckTaskName = lCheckTaskName+".exe"
|
|
|
|
|
else:
|
|
|
|
|
lCheckTaskName = lCheckTaskName+".exe"
|
|
|
|
|
#Check if process exist
|
|
|
|
|
if not CheckIfProcessRunning(lCheckTaskName):
|
|
|
|
|
#Вид активности - запуск процесса
|
|
|
|
|
#Запись в массив отработанных активностей
|
|
|
|
|
#Лог
|
|
|
|
|
mGlobalDict["Processor"]["LogList"].append({"activityType":lItem["Type"], "activityDateTime":str(lActivityDateTime), "processPath":lItem["Path"], "activityStartDateTime":str(lCurrentDateTime)})
|
|
|
|
|
#Запустить процесс
|
|
|
|
|
lItemArgs=[lItem["processPath"]]
|
|
|
|
|
lItemArgs.extend(lItem["processArgs"])
|
|
|
|
|
lItemArgs=[lItem["Path"]]
|
|
|
|
|
lItemArgs.extend(lItem["ArgList"])
|
|
|
|
|
subprocess.Popen(lItemArgs,shell=True)
|
|
|
|
|
#################################
|
|
|
|
|
#ProcessStop
|
|
|
|
@ -150,7 +181,7 @@ def Activity(inActivity):
|
|
|
|
|
#Вид активности - остановка процесса
|
|
|
|
|
#часовой пояс пока не учитываем
|
|
|
|
|
#Сформировать команду на завершение
|
|
|
|
|
lActivityCloseCommand='taskkill /im '+lItem["processName"]
|
|
|
|
|
lActivityCloseCommand='taskkill /im '+lItem["Name"]
|
|
|
|
|
#TODO Сделать безопасную обработку,если параметра нет в конфигурации
|
|
|
|
|
if lItem.get('FlagForce',False):
|
|
|
|
|
lActivityCloseCommand+=" /F"
|
|
|
|
@ -158,7 +189,7 @@ def Activity(inActivity):
|
|
|
|
|
if lItem.get('User',"")!="":
|
|
|
|
|
lActivityCloseCommand+=f' /fi "username eq {lItem["User"]}"'
|
|
|
|
|
#Лог
|
|
|
|
|
mGlobalDict["Processor"]["LogList"].append({"activityType":lItem["activityType"], "activityDateTime":str(lActivityDateTime), "processPath":lItem["processName"], "activityStartDateTime":str(lCurrentDateTime)})
|
|
|
|
|
mGlobalDict["Processor"]["LogList"].append({"activityType":lItem["Type"], "activityDateTime":str(lActivityDateTime), "processPath":lItem["Name"], "activityStartDateTime":str(lCurrentDateTime)})
|
|
|
|
|
#Завершить процесс
|
|
|
|
|
os.system(lActivityCloseCommand)
|
|
|
|
|
#################################
|
|
|
|
@ -214,3 +245,17 @@ def ActivityListOrDict(inActivityListOrDict):
|
|
|
|
|
if type(inActivityListOrDict)==dict:
|
|
|
|
|
#Dict activity
|
|
|
|
|
return Activity(inActivityListOrDict)
|
|
|
|
|
|
|
|
|
|
def CheckIfProcessRunning(processName):
|
|
|
|
|
'''
|
|
|
|
|
Check if there is any running process that contains the given name processName.
|
|
|
|
|
'''
|
|
|
|
|
#Iterate over the all the running process
|
|
|
|
|
for proc in psutil.process_iter():
|
|
|
|
|
try:
|
|
|
|
|
# Check if process name contains the given name string.
|
|
|
|
|
if processName.lower() in proc.name().lower():
|
|
|
|
|
return True
|
|
|
|
|
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
|
|
|
|
|
pass
|
|
|
|
|
return False;
|