-
Notifications
You must be signed in to change notification settings - Fork 0
/
nbcheck.py
executable file
·64 lines (54 loc) · 2.11 KB
/
nbcheck.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#! /usr/bin/env python3
import requests
import json
import subprocess
import pandas as pd
import argparse
def nb_check():
parser = argparse.ArgumentParser(description='List active ipython notebook kernels and corresponding process information')
parser.add_argument('-s', '--server', type=str, default='localhost', help='Jupyter server address, default: localhost')
parser.add_argument('-p', '--port', type=int, default=8888, help='Jupyter server port, default: 8888')
parser.add_argument('-t', '--token', type=str, default=None, help='Token is required')
args = parser.parse_args()
server = args.server
port = args.port
token = args.token
url = 'http://{server}:{port}/api/sessions?token={token}'.format(server=server, port=port, token=token)
if token==None:
print('Abort: Token is missing')
exit()
# api
response = requests.get(url)
sessions = json.loads(response.text)
nb_session = []
for s in sessions:
row = {'notebook':s['path'].split('/')[-1],
'exec':s['kernel']['execution_state'],
'KID':s['kernel']['id']}
nb_session.append(row)
nb_session = pd.DataFrame(nb_session)
# prc
grep_list = subprocess.getoutput('ps aux| grep ipy').splitlines()
nb_procs = []
for proc in grep_list:
proc_info = proc.split()
if not 'ipykernel_launcher' in proc_info:
continue
row = {'PID': proc_info[1],
'%CPU': proc_info[2],
'%MEM': proc_info[3],
'VSZ': str(round(int(proc_info[4])/1024))+'M',
'RSS': str(round(int(proc_info[5])/1024))+'M',
# 'STAT': proc_info[7],
'STARTED': proc_info[8],
'KID': proc_info[-1][-41:-5]}
nb_procs.append(row)
nb_procs = pd.DataFrame(nb_procs)
# merge
try:
nb = pd.merge(nb_session,nb_procs,how='outer')[['notebook', 'exec','VSZ','RSS', 'PID', '%CPU', '%MEM', 'STARTED', 'KID']]
print(nb)
except:
print('Error: Failed to merge nb_sessions and nb_process...')
if __name__ == "__main__":
nb_check()