Skip to content

Instantly share code, notes, and snippets.

@adamzwakk
Created February 10, 2022 15:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamzwakk/81c296670906e49cb56cf80b8bd64bd6 to your computer and use it in GitHub Desktop.
Save adamzwakk/81c296670906e49cb56cf80b8bd64bd6 to your computer and use it in GitHub Desktop.
Get active Jupyter Notebook sessions, combine with ps to get ram/cpu usage and prep for InfluxDB
import requests
import json
s = requests.Session()
resp = s.get('{notebook URL}/login', verify = False)
xsrf_cookie = resp.cookies['_xsrf']
params = {'_xsrf':xsrf_cookie,'password': '{notebook password}'}
l = s.post('{notebook URL}/login?next=/api/sessions', data=params, verify = False, allow_redirects=True).json()
output = ""
for s in l:
id = s['kernel']['id']
exece = s['kernel']['execution_state']
name = s['path'].split('/')[-1]
name = name.split('.')[0]
output+=id+'|'+name+'|'+exece+"\n"
print(output.rstrip())
#!/bin/bash
SESSIONS=`python /opt/nb_get.py 2> /dev/null`
PROCESSES=`ps -Ao pcpu,pmem,cmd | grep "jupyter"`
THISHOST=`hostname`
if [ -z "$SESSIONS" ]
then
exit 1
else
while IFS= read -r line; do
id=`echo "$line" | awk 'BEGIN {FS=OFS="|"} {print $1}'`
name=`echo "$line" | awk 'BEGIN {FS=OFS="|"} {print $2}'`
name=`printf %q "$name"`
pro=`echo "$PROCESSES" | grep "$id"`
if [ -z "$pro" ]
then
continue
fi
cpu=`echo "$pro" | awk -F" " '{print $1}'`
mem=`echo "$pro" | awk -F" " '{print $2}'`
echo "jupyter,host=$THISHOST,name=\"$name\" cpu=$cpu,mem=$mem"
done <<< "$SESSIONS"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment