Skip to content

Commit 0e60a37

Browse files
authored
Merge pull request #4 from extended-debug/get-os-info
Create get_os_info.py
2 parents 5707e6b + e2c2c2c commit 0e60a37

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

get_os_info.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import os
2+
import time
3+
4+
import psutil # pip install psutil
5+
import humanfriendly # pip install humanfriendly
6+
7+
8+
def get_os_info():
9+
""" Get some OS info with psutils and humanfriendly. """
10+
is_win = lambda: True if os.name == 'nt' else False
11+
pid = os.getgid() if not is_win() else None
12+
ppid = os.getppid()
13+
14+
now = time.time()
15+
16+
current_process = psutil.Process(pid=ppid)
17+
process_uptime = current_process.create_time()
18+
process_uptime_delta = now - process_uptime
19+
process_uptime_human = humanfriendly.format_timespan(process_uptime_delta)
20+
21+
system_uptime = psutil.boot_time()
22+
system_uptime_delta = now - system_uptime
23+
system_uptime_human = humanfriendly.format_timespan(system_uptime_delta)
24+
25+
free_memory = psutil.disk_usage('/').free
26+
total_memory = psutil.disk_usage('/').total
27+
percent_used_memory = psutil.disk_usage('/').percent
28+
used_memory = psutil.disk_usage('/').used
29+
free_memory_human = humanfriendly.format_size(free_memory)
30+
31+
return vars()
32+
33+
34+
if __name__ == '__main__':
35+
from pprint import pprint
36+
pprint(get_os_info())
37+
### Output example:
38+
# {'current_process': <psutil.Process(pid=10628, name='pycharm.exe') at 2074457600744>,
39+
# 'free_memory': 28299886592,
40+
# 'free_memory_human': '28.3 GB',
41+
# 'is_win': <function get_os_info.<locals>.<lambda> at 0x000001E2FFDED158>,
42+
# 'now': 1491982808.197413,
43+
# 'percent_used_memory': 88.2,
44+
# 'pid': 1001,
45+
# 'ppid': 10628,
46+
# 'process_uptime': 1491471889.0,
47+
# 'process_uptime_delta': 510919.1974129677,
48+
# 'process_uptime_human': '5 days, 21 hours and 55 minutes',
49+
# 'system_uptime': 1490198873.0,
50+
# 'system_uptime_delta': 1783935.1974129677,
51+
# 'system_uptime_human': '2 weeks, 6 days and 15 hours',
52+
# 'total_memory': 239530405888,
53+
# 'used_memory': 211230519296}

0 commit comments

Comments
 (0)