-
Notifications
You must be signed in to change notification settings - Fork 1
/
sysinfo.py
executable file
·242 lines (206 loc) · 7.64 KB
/
sysinfo.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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#!/usr/bin/env python3
# Copyright (c) 2016 John Adams <[email protected]> & Tom Carrio <[email protected]>
# Terrible coding is standard practice
# Let me know what I can do better by emailing me at <[email protected]> or
# by submitting a pull request at (https://github.com/jadams/sysinfo)
import os, sys, subprocess, datetime, json
def _get_kernel():
kernel = subprocess.check_output(['uname', '-r']).decode('utf-8').strip('\n')
kernel = kernel.split('-')
return kernel
def _get_fqdn():
fqdn = subprocess.check_output(['hostname', '-f']).decode('utf-8').strip('\n')
fqdn = fqdn.split('.')
return fqdn
def _get_uptime():
with open('/proc/uptime', 'r') as upfile:
uptime = datetime.timedelta(seconds = float(upfile.readline().split()[0]))
return {'days':uptime.days, 'hours':int(uptime.seconds/3600), 'mins':int(uptime.seconds%3600/60), 'secs':int(uptime.seconds%3600%60)}
def _get_date():
dt = str(datetime.datetime.now()).split()
dt[1] = dt[1].split('.')[0]
return dt
def _get_ip_addr():
addr = subprocess.check_output(['ip', 'addr']).decode('utf-8').strip('\n')
addr = addr.split('\n')
addrs = {}
for line in addr:
if line[0].isdigit():
iface = line.split()[1].strip(':')
addrs[iface] = {'ipv6':[], 'ipv4':[]}
if 'inet6' in line:
address = line.split()[1].split('/')
addrs[iface]['ipv6'].append(address)
elif 'inet' in line:
address = line.split()[1].split('/')
addrs[iface]['ipv4'].append(address)
return addrs
def _get_ip_route():
route = subprocess.check_output(['ip', 'route']).decode('utf-8').strip('\n')
route = route.split('\n')
routes = {}
try:
for line in route:
info = line.split()
name = info[0]
routes[name] = {}
for i in range(1,len(info)):
if info[i] == 'dev':
routes[name]['dev'] = info[i+1]
elif info[i] == 'via':
routes[name]['via'] = info[i+1]
elif info[i] == 'src':
routes[name]['src'] = info[i+1]
return routes
except:
return
def _get_users():
if not os.path.isfile('/etc/passwd'):
return
else:
users = {}
try:
with open('/etc/passwd', 'r') as passwd:
for line in passwd:
line = line.strip('\n').split(':')
if int(line[2]) >= 1000 and int(line[2]) < 65534:
users[line[0]] = {'uid':int(line[2]),
'gid':int(line[3]),
'home':line[5],
'shell':line[6]}
except:
return
return users
def _get_disks():
try:
ddisks = {}
df_keys=['filesystem','size','used','avail','use%','mount']
df = subprocess.check_output(['df', '-h']).decode('utf-8')[:-1].split('\n')
mounts = subprocess.check_output('mount').decode('utf-8')[:-1].split('\n')
# pull info from 'df -h'
for line in df:
if('/'==line[0]):
line=line.split()
ddisks.update({line[5]:dict()})
for i in range(0,5):
ddisks[line[5]][df_keys[i]]=line[i]
# pull info from mount using new df dict
for m in mounts:
for d in ddisks:
m_info = m.split()
if(m_info[2]==d):
ddisks[d]['type']=m_info[4]
ddisks[d]['permission']=m_info[5].split(',')[0][1:]
return ddisks
except:
return
def _detect_distro():
return
def _get_processes():
pdict = {'_total':len([c for c in subprocess.check_output(['ps','aux']) if('\n'==c)])-1}
pdict.update({'users':{}})
[pdict.update({'users':{u:len([c for c in subprocess.check_output(['ps','-u',u]) if('\n'==c)])-1}}) for u in _get_users()]
return pdict
def _get_hosts():
return
def _get_dns():
return
def _get_mounts():
return
def _get_cpuinfo():
if os.path.isfile('/proc/cpuinfo'):
with open('/proc/cpuinfo', 'r') as cpuinfo:
for line in cpuinfo:
if 'model name' in line:
return line.strip('\n').split(':')[1].split()
def _get_meminfo():
meminfo = subprocess.check_output(['free', '-h']).decode('utf-8').strip('\n').split()
mem = meminfo.index("Mem:")
swap = meminfo.index("Swap:")
return {'total':meminfo[int(mem)+1],
'used':meminfo[int(mem)+2],
'swap_total':meminfo[int(swap)+1],
'swap_used':meminfo[int(swap)+2]}
def _get_current_user():
return os.getenv('USER')
def _get_systemd_services():
return
def _get_initd_services():
return
def _get_usb_dev():
return
def _get_pci_dev():
return
def _get_listening_ports():
return
def full_print():
print('Kernel:\t{0}-{1}'.format(*_get_kernel()))
print('Host:\t{}'.format('.'.join(_get_fqdn())))
print('Uptime:\t{days} Days, {hours} Hours, {mins} Minutes'.format(**_get_uptime()))
print('Date:\t{0} {1}'.format(*_get_date()))
print('IP Addresses: ')
ipinfo = _get_ip_addr()
for iface in ipinfo:
print('{}:'.format(iface))
if not ipinfo[iface]['ipv4']:
continue
else:
print('\tIPv4:')
for addr in ipinfo[iface]['ipv4']:
print('\t\t{}'.format('/'.join(addr)))
if not ipinfo[iface]['ipv6']:
continue
else:
print('\tIPv6:')
for addr in ipinfo[iface]['ipv6']:
print('\t\t{}'.format('/'.join(addr)))
print('Routes:')
iproute = _get_ip_route()
if iproute:
for route in iproute:
proute = '\t{}'.format(route)
for key in iproute[route]:
#proute = proute+' '+key+' '+iproute[route][key]
proute = '{0} {1} {2}'.format(proute, key, iproute[route][key])
print(proute)
print('Users:')
users = _get_users()
for user in users:
print('\t{0}: {1}'.format(user,
'uid={uid}, gid={gid}, home={home}, shell={shell}'.format(**users[user])))
print('CPU:\t{}'.format(' '.join(_get_cpuinfo())))
print('Memory:\t{used}/{total} Swap: {swap_used}/{swap_total}'.format(**_get_meminfo()))
print('Processes:')
processes=_get_processes()
if(processes):
for username in processes['users']:
print('\t{0}: {1}'.format(username,processes['users'][username]))
print('\t{0}: {1}'.format('Total',processes['_total']))
print('Disks:')
_disk_dict = _get_disks()
for disk in _disk_dict:
disk_info='\t\tMount:{0}, Type:{type}, Permission:{permission},\n\t\tSize:{size}, Avail:{avail}, Used%:{use%}'.format(disk,**_disk_dict[disk])
print('\t{}:\n{}'.format(_disk_dict[disk]['filesystem'],disk_info))
def short_print():
print(' '.join(_get_date()))
print('{0}@{1}'.format(_get_current_user(), _get_fqdn()[0]))
print('-'.join(_get_kernel()))
print('Up: {days} Days, {hours} Hours, {mins} Minutes'.format(**_get_uptime()))
def get_json():
jdb = {}
jdb['kernel'] = _get_kernel()
jdb['hostname'] = _get_fqdn()
jdb['uptime'] = _get_uptime()
jdb['date'] = _get_date()
jdb['ipaddr'] = _get_ip_addr()
jdb['iproute'] = _get_ip_route()
jdb['users'] = _get_users()
jdb['cpu'] = _get_cpuinfo()
jdb['mem'] = _get_meminfo()
return jdb
if __name__ == '__main__':
full_print()
print('==============================')
short_print()
with open('host.json', 'w') as outfile:
json.dump(get_json(), outfile)