Skip to content

Instantly share code, notes, and snippets.

@manuzhang
Created March 5, 2019 07:03
Show Gist options
  • Save manuzhang/d1bfd477f140956096df134edd5df5e8 to your computer and use it in GitHub Desktop.
Save manuzhang/d1bfd477f140956096df134edd5df5e8 to your computer and use it in GitHub Desktop.
Monitor CPU and memory usage from Spark master UI
# coding: utf-8
from bs4 import BeautifulSoup
import requests
page = requests.get("http://spark-master.com").content
soup = BeautifulSoup(page, 'html.parser')
cores_text = soup.find('strong', string='Cores in use:').next_sibling
cores_parts = cores_text.strip().split('\n')
total_cores = float(cores_parts[0].strip().split(' ')[0])
used_cores = float(cores_parts[1].strip().split(' ')[0])
cpu_usage = used_cores / total_cores
print(cpu_usage)
memory_text = soup.find('strong', string='Memory in use:').next_sibling
memory_parts = memory_text.strip().split('\n')
def get_memory(text):
parts = text.split(' ')
if (parts[1] == 'TB'):
return float(parts[0]) * 1024
else:
return float(parts[0])
memory_total = get_memory(memory_parts[0].strip())
memory_used = get_memory(memory_parts[1].strip())
memory_usage = memory_used / memory_total
print(memory_usage)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment