Skip to content

Instantly share code, notes, and snippets.

@boarpig
Created December 24, 2013 23:45
Show Gist options
  • Save boarpig/8118854 to your computer and use it in GitHub Desktop.
Save boarpig/8118854 to your computer and use it in GitHub Desktop.
How to get plugin-container's cpu usage percent with python. This currently only handles one plugin container so your mileage may vary. You'll have to find your own cpu usage threshold if you actually want to use this to monitor if someone is actually watching a video or if it's just sitting still **further improvements:** You could use sh modul…
#!/usr/bin/python
#
# Based on answer from
# http://stackoverflow.com/questions/1420426/calculating-cpu-usage-of-a-process-in-linux
#
from time import sleep
import os
import os.path
import sys
name = sys.argv[0]
# get total cpu time
def get_cpu_jiffies():
"Get total cpu time at the given time."
cpu_time = 0
with open("/proc/stat") as f:
for line in f:
if line.startswith("cpu "):
cpu_jiffies = line.split()[1:]
cpu_time = sum([int(x) for x in cpu_jiffies])
return cpu_time
def get_jiffies(proc):
"Get process's current cpu time used over time"
stats = os.path.join("/proc", proc, "stat")
with open(stats) as statfile:
content = statfile.read()
content = content.split()
return int(content[13]) + int(content[14])
def find_process_by_name(name):
"Searches through /proc to find certain process by name"
# get all directories from /proc which name is a number
# (get all process dirs)
procs = [x for x in os.listdir('/proc') if os.path.isdir("/proc/" + x)
and x.isdigit()]
# go through all processes and find plugin containers
pid = ""
for proc in procs:
path = os.path.join("/proc", proc, "status")
with open(path) as fd:
pname = fd.readlines()[0]
pname = pname.split()[1]
if pname.startswith(name):
pid = proc
break
return pid
pid = find_process_by_name("plugin-conta")
if pid:
last_prc = get_jiffies(pid)
last_cpu = get_cpu_jiffies()
while 1:
sleep(1)
new_prc = get_jiffies(pid)
new_cpu = get_cpu_jiffies()
time = 100 * (new_prc - last_prc) / (new_cpu - last_cpu)
print(time)
last_prc = new_prc
last_cpu = new_cpu
else:
print("no such process")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment