import os
import sys
import psutil
import string
import re
import time
import datetime
import csv
def get_pid(name):
process_list = psutil.get_process_list()
regex = "pid=(\d+),\sname=\'" + name + "\'"
pid = 0
for line in process_list:
process_info = str(line)
ini_regex = re.compile(regex)
result = ini_regex.search(process_info)
if result is not None:
pid = string.atoi(result.group(1))
return pid
pid = get_pid('slam_karto')
filename = 'cpu_supervise.csv'
fileHeader = ["time","timestamp", "Total","SlamKarto"]
csvFile = open(filename, "a")
writer = csv.writer(csvFile)
writer.writerow(fileHeader)
info = []
while 1:
info[:] = []
info.append(datetime.datetime.now())
info.append(int(time.time()))
Total_cpu_percent = psutil.cpu_percent(interval=1,percpu=False)
info.append(Total_cpu_percent)
p1=psutil.Process(pid)
p1_cpu_percent = p1.get_cpu_percent(interval=1)
info.append(p1_cpu_percent)
print info
writer.writerow(info)
csvFile.close()