Created
August 28, 2020 02:48
-
-
Save gbraccialli/f5c07ad0703b093d46e85032d42cd46a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#option 2, obtain token by username/password | |
username = 'jupyter' | |
password = 'jupyter' | |
# step 1: login with username + password | |
r = requests.post(login_url, data={'username': username, 'password': password}, allow_redirects=False) | |
r.raise_for_status() | |
cookies = r.cookies | |
# 2. request token | |
r = requests.post(token_url, | |
headers={'Referer': login_url}, | |
cookies=cookies, | |
) | |
r.raise_for_status() | |
token = r.json()['token'] | |
auth_headers = {'Authorization': 'token %s' % token} |
This file contains hidden or 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 sys | |
import re | |
import pandas as pd | |
import psutil | |
import requests | |
from notebook.notebookapp import list_running_servers | |
kernel_regex = re.compile(r".+kernel-(.+)\.json") | |
notebook_regex = re.compile(r"(https?://([^:/]+):?(\d+)?)/?(\?token=([a-z0-9]+))?") | |
def get_proc_info(): | |
pids = psutil.pids() | |
# memory info from psutil.Process | |
df_mem = [] | |
for pid in pids: | |
try: | |
proc = psutil.Process(pid) | |
cmd = " ".join(proc.cmdline()) | |
except psutil.NoSuchProcess: | |
continue | |
if len(cmd) > 0 and ("jupyter" in cmd or "ipython" in cmd) and "kernel" in cmd: | |
# kernel | |
kernel_ID = re.sub(kernel_regex, r"\1", cmd) | |
# memory | |
mem = proc.memory_info()[0] / float(1e9) | |
uname = proc.username() | |
# user, pid, memory, kernel_ID | |
df_mem.append([uname, pid, mem, kernel_ID]) | |
df_mem = pd.DataFrame(df_mem, columns=["user", "pid", "memory_GB", "kernel_id"]) | |
return df_mem | |
df_mem = get_proc_info() | |
token = "" | |
auth_headers = {"Authorization": "token %s" % token} | |
base_url = "https://127.0.0.1:8443/" | |
r = requests.get( | |
base_url + "api/sessions", headers=auth_headers, allow_redirects=False, verify=False | |
) | |
r.raise_for_status() | |
# print(r.json()) | |
if r.status_code == 200 and r.json(): | |
df_notebooks = pd.DataFrame.from_dict(r.json()) | |
df_notebooks["kernel_id"] = df_notebooks["kernel"].apply(lambda x: x.get("id")) | |
df_notebooks = df_notebooks[["path", "kernel_id"]] | |
else: | |
df_notebooks = pd.DataFrame(columns=["path", "kernel_id"]) | |
pd.set_option("display.max_colwidth", 1000) | |
pd.set_option("display.max_columns", None) | |
pd.set_option("display.max_rows", None) | |
pd.set_option("display.width", None) | |
df_final = ( | |
df_mem.merge(df_notebooks, on="kernel_id") | |
.groupby(["path", "kernel_id"]) | |
.agg( | |
pids=("pid", lambda col: str(list(col))), | |
count=("memory_GB", "count"), | |
memory_GB=("memory_GB", "sum"), | |
) | |
.sort_values("memory_GB") | |
) | |
df_final |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment