[mw_shl_code=python,true]# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
import psutil
import sys,os
import socket
from time import time as now
import time
import thread
import datetime
timestr = str(datetime.datetime.now())[:-7]
cmd_java_home = r"d:/puppet_flume/java_home/bin/java.exe"
cmd_java_opt =r"""-Xms300m -Xmx300m -Dlog4j.configuration=file:///d:/puppet_flume/tier1/tier14j.properties -Dflume.monitoring.type=http -Dflume.monitoring.port=34545 -cp "d:/puppet_flume/flume_home/lib/*" org.apache.flume.node.Application"""
cmd_config = r"-f d:/puppet_flume/tier1/tier1.conf -n tier1"
cmd = r"start /b " + cmd_java_home +" " + cmd_java_opt + " " + cmd_config
try:
f = open('services_pids.txt')
except IOError,mes:
print "%s IOError: %s"%(timestr , mes)
services_pids = {}
else:
services_pids = eval(f.read())
f.close()
#monitor flume
def flume_run_test(moniter_service_names):
monitor ={}
for i in moniter_service_names:
try:
pidi = int(services_pids)
except KeyError,mes:
print "%s KeyError : %s"%(timestr, mes)
monitors = {'service_name':i,'flume_pid':None,"flume_mem":None,
"flume_cpu":{"user":None,"sys":None}}
else:
ps = psutil.Process(pidi)
try:
ps_cmd = ps.exe()
except psutil.AccessDenied,mes:
print "%s AccessDenied to get the pid exe :%s"%(timestr, mes)
else:
monitors ={'service_name':i,'flume_pid':pidi,
"flume_mem":ps.memory_percent(),"flume_cpu":{"user":ps.cpu_times().user,
"sys":ps.cpu_times().system}}
return monitor
#start flume with a service_name
def start_flume(start_service_names):
for i in start_service_names:
p = psutil.Popen(cmd,shell=True)
p_pid = str(p.pid)
services_pids = p_pid
f = open('services_pids.txt','w')
print services_pids
str_f = str(services_pids)
print str_f
f.write(str_f)
f.close()
#stop flume and remove servicename from services_pids
def stop_flume(stop_service_names):
if not stop_service_names:
print "%s no service to stop "%timestr
return False
for i in stop_service_names:
try:
pid = services_pids
except KeyError,mes:
print "%s KeyError: %s"%(timestr, mes)
else:
cmd = r"taskkill /pid " + str(pid) + r" /f "
os.system(cmd)
del services_pids
ft = open('services_pids.txt','w')
print services_pids
str_f = str(services_pids)
print str_f
ft.write(str_f)
ft.close()
service_names = ["test1"]
start_flume(service_names)
#stop_flume(service_names)[/mw_shl_code]
执行的时候报错如下:
[mw_shl_code=python,true]runfile('C:/Users/shuangyu/untitled0.py', wdir='C:/Users/shuangyu')
File "<string>", line unknown
^
SyntaxError: unexpected EOF while parsing[/mw_shl_code]
盯着看了半天还是没找到哪里处错了,烦请大家帮帮忙,看看是哪里出错了。谢谢!
|