Last active
July 10, 2019 00:16
-
-
Save fuji246/ee512556f4a83ce1b23f2e50419a9451 to your computer and use it in GitHub Desktop.
plot cpu and memory usage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.pyplot as plt | |
import sys | |
''' | |
#!/bin/bash | |
while sleep 1; do | |
sudo netstat -anp | grep "80 " | wc -l | |
top -n 1 -b | grep -E "Cpu|Mem" | |
done | |
''' | |
''' | |
#!/bin/bash | |
echo 'CPU MEM CMD' | |
# while sleep 1; do ps --no-headers -C mfap -C media_ctrl -o %cpu,%mem,cmd; done | |
for i in {1..240} | |
do | |
#ps --no-headers -C mfap -C media_ctrl -o %cpu,%mem,cmd | |
top -n 1 -b | grep "myprogram" | awk '{print $9 " " $10 " myprogram"}' | |
sleep 1 | |
done | |
''' | |
''' | |
Output: | |
2029 | |
Cpu(s): 1.0%us, 0.5%sy, 0.0%ni, 98.5%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st | |
Mem: 15463384k total, 1847176k used, 13616208k free, 492140k buffers | |
''' | |
rec_lst = [] | |
memKB = None | |
cpu = None | |
halfopen = None | |
with open(sys.argv[1], 'r') as f: | |
for item in f.readlines(): | |
if 'Mem' in item: | |
memKB = int(item.split()[3][:-1]) | |
elif 'Cpu' in item: | |
cpuItems = item.split() | |
us = float(cpuItems[1][:-4]) | |
sy = float(cpuItems[2][:-4]) | |
ni = float(cpuItems[3][:-4]) | |
hi = float(cpuItems[6][:-4]) | |
si = float(cpuItems[7][:-4]) | |
#print(us, sy, ni, hi, si) | |
cpu = us + sy + ni + hi + si | |
else: | |
halfopen = int(item.split()[0]) | |
if memKB is not None and halfopen is not None: | |
rec_lst.append([memKB, cpu, halfopen]) | |
memKB = None | |
halfopen = None | |
cpu = None | |
#print(rec_lst) | |
fig, (ax0, ax1, ax2) = plt.subplots(3, 1) | |
ax0.plot(range(len(rec_lst)), [rec[0] for rec in rec_lst]) | |
ax0.set_ylabel('mem usage (KB)'); | |
#ax0.set_ylim([13000000, 14000000]) | |
ax0.get_yaxis().get_major_formatter().set_scientific(False) | |
ax1.plot(range(len(rec_lst)), [rec[1] for rec in rec_lst]) | |
ax1.set_ylabel('cpu usage (%)'); | |
ax2.plot(range(len(rec_lst)), [rec[2] for rec in rec_lst]) | |
ax2.set_ylabel('half open count (#)'); | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment