125 lines
2.7 KiB
Python
Executable File
125 lines
2.7 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
|
|
from os import getpid
|
|
|
|
# find mem_total
|
|
# find positions of SwapFree and SwapTotal in /proc/meminfo
|
|
|
|
with open('/proc/meminfo') as f:
|
|
mem_list = f.readlines()
|
|
|
|
mem_list_names = []
|
|
for s in mem_list:
|
|
mem_list_names.append(s.split(':')[0])
|
|
|
|
if mem_list_names[2] != 'MemAvailable':
|
|
errprint('WARNING: Your Linux kernel is too old, Linux 3.14+ requied')
|
|
# exit(1)
|
|
|
|
swap_total_index = mem_list_names.index('SwapTotal')
|
|
swap_free_index = swap_total_index + 1
|
|
|
|
mem_total = int(mem_list[0].split(':')[1][:-4])
|
|
|
|
# Get names from /proc/*/status to be able to get VmRSS and VmSwap values
|
|
|
|
with open('/proc/self/status') as file:
|
|
status_list = file.readlines()
|
|
|
|
status_names = []
|
|
for s in status_list:
|
|
status_names.append(s.split(':')[0])
|
|
|
|
ppid_index = status_names.index('PPid')
|
|
vm_size_index = status_names.index('VmSize')
|
|
vm_rss_index = status_names.index('VmRSS')
|
|
vm_swap_index = status_names.index('VmSwap')
|
|
uid_index = status_names.index('Uid')
|
|
state_index = status_names.index('State')
|
|
|
|
|
|
try:
|
|
anon_index = status_names.index('RssAnon')
|
|
file_index = status_names.index('RssFile')
|
|
shmem_index = status_names.index('RssShmem')
|
|
detailed_rss = True
|
|
# print(detailed_rss, 'detailed_rss')
|
|
except ValueError:
|
|
detailed_rss = False
|
|
# print('It is not Linux 4.5+')
|
|
|
|
|
|
|
|
self_pid = str(getpid())
|
|
|
|
|
|
def self_rss():
|
|
r = pid_to_status(self_pid)[5]
|
|
print(r)
|
|
|
|
|
|
|
|
def pid_to_status(pid):
|
|
"""
|
|
"""
|
|
|
|
try:
|
|
|
|
with open('/proc/' + pid + '/status') as f:
|
|
|
|
for n, line in enumerate(f):
|
|
|
|
if n is 0:
|
|
name = line.split('\t')[1][:-1]
|
|
|
|
if n is state_index:
|
|
state = line.split('\t')[1][0]
|
|
continue
|
|
|
|
if n is ppid_index:
|
|
ppid = line.split('\t')[1][:-1]
|
|
continue
|
|
|
|
if n is uid_index:
|
|
uid = line.split('\t')[2]
|
|
continue
|
|
|
|
if n is vm_size_index:
|
|
vm_size = int(line.split('\t')[1][:-4])
|
|
continue
|
|
|
|
if n is vm_rss_index:
|
|
vm_rss = int(line.split('\t')[1][:-4])
|
|
continue
|
|
|
|
if n is vm_swap_index:
|
|
vm_swap = int(line.split('\t')[1][:-4])
|
|
break
|
|
|
|
return name, state, ppid, uid, vm_size, vm_rss, vm_swap
|
|
|
|
except UnicodeDecodeError:
|
|
return pid_to_status_unicode(pid)
|
|
|
|
except FileNotFoundError:
|
|
return None
|
|
|
|
except ProcessLookupError:
|
|
return None
|
|
|
|
except ValueError:
|
|
return None
|
|
|
|
|
|
self_rss()
|
|
|
|
|
|
import logging
|
|
import subprocess
|
|
import argparse
|
|
|
|
|
|
self_rss()
|
|
|