-
Notifications
You must be signed in to change notification settings - Fork 0
/
40-ubuntu-motd-sysinfo
189 lines (143 loc) · 5.4 KB
/
40-ubuntu-motd-sysinfo
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/opt/scripts/venv/bin/python3
# -*- coding: utf-8 -*-
import os
import netifaces
import time
import platform
import distro
import psutil
import subprocess
def get_header():
return "\nSystem information as of {}\n".format(
time.asctime(time.localtime(time.time())))
def get_filecontent(path):
with open(path) as f:
return f.read()
def get_hardware():
hw_products = ('/sys/firmware/devicetree/base/model',
'/sys/devices/virtual/dmi/id/product_name')
for hw_product in hw_products:
if os.path.isfile(hw_product):
return get_filecontent(hw_product).rstrip('\x00')
def get_sec_to_str(secs):
intervals = (('day', 86400),
('hour', 3600),
('minute', 60))
result = []
remainder = secs
for name, count in intervals:
cur_value, remainder = divmod(remainder, count)
if cur_value:
result.append("{} {}{}".format(int(cur_value), name,
"s" if cur_value > 1 else ""))
return f"up {', '.join(result)}." if len(result) else "up <1 minute."
def get_seconds_elapsed():
return time.time() - psutil.boot_time()
def get_boottime():
return time.strftime("%Y/%m/%d %H:%M:%S %Z",
time.localtime(psutil.boot_time()))
def get_uptime():
return "{} ({})".format(get_sec_to_str(get_seconds_elapsed()),
get_boottime())
def get_distribution():
os_version = distro.os_release_info()
return "{} {} {} ({}/{})".format(platform.system(),
os_version.get('pretty_name'),
os_version.get('release_codename'),
platform.architecture()[0],
platform.machine())
def get_kernel():
return platform.release()
def get_temp():
return psutil.sensors_temperatures(
fahrenheit=False)['cpu_thermal'][0].current
def get_cpu():
cmd = "lscpu | grep 'Model name:' | head -1 | awk '{print $3}'"
model = subprocess.getoutput(cmd)
return "{} / {} Cores".format(model, psutil.cpu_count())
def get_nbusers():
return len(psutil.users())
def get_all_ips():
"""
Find all IPs for this machine.
"""
ips = {}
interfaces = netifaces.interfaces()
for interface in interfaces:
# get all ips defined
addresses = netifaces.ifaddresses(interface)
# test if up/down
interface_is_up = (netifaces.AF_INET in addresses or
netifaces.AF_INET6 in addresses)
if not interface_is_up or interface == "lo":
continue
# set array
ips[interface] = []
for address_family in (netifaces.AF_INET, netifaces.AF_INET6):
if addresses.get(address_family) is None:
continue
for address in addresses.get(address_family):
if '%' in address['addr']:
continue
ips[interface].append(address['addr'])
return ips
def get_process():
return len(psutil.pids())
def get_load():
return os.getloadavg()[0]
def get_diskusage():
partitions = psutil.disk_partitions()
result = []
for p in partitions:
if 'snap' in p.mountpoint:
continue
try:
usage = psutil.disk_usage(p.mountpoint)
except PermissionError:
continue
result.append({'mountpoint': p.mountpoint,
'percent': usage.percent,
'total': usage.total / 1024 ** 3})
return result
def get_memory():
virtual_memory = psutil.virtual_memory()
return {'percent': virtual_memory.percent,
'total': virtual_memory.total / 1024 ** 3}
def get_swap():
return psutil.swap_memory()
def get_sysinfo():
virtual_memory = get_memory()
result = [("Product:", get_hardware()),
("CPU:", get_cpu()),
("OS:", get_distribution()),
("Kernel:", get_kernel()),
("Uptime:", get_uptime()),
("System load:", "{0:.2f}".format(get_load()))
] + \
[("Usage of {}:".format(partition['mountpoint']),
"{0:.1f}% of {1:.1f}GB".format(partition['percent'],
partition['total'])
) for partition in get_diskusage()
] + \
[("Memory usage:",
"{0:.1f}% of {1:.1f}GB".format(
virtual_memory['percent'],
virtual_memory['total'])),
("Swap usage:", "{0:.1f}%".format(get_swap().percent)),
("Temperature:", "{0:.1f}°C".format(get_temp())),
("Processes:", "{}".format(get_process())),
("Users logged in:", "{}".format(get_nbusers()))
] + \
[("{} address for {}:".format('IPv4' if '.' in ip else 'IPv6',
interface), "{}".format(ip))
for interface, ips in get_all_ips().items() for ip in ips
]
return result
def print_sysinfo(header, sysinfo):
max_label_width = len(max(sysinfo, key=lambda s: len(s[0]))[0])
print(header)
for label, value in sysinfo:
decor = " {0:<" + str(max_label_width) + "s} {1}"
print(decor.format(label, value))
if __name__ == '__main__':
print_sysinfo(get_header(), get_sysinfo())