Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/ShExBy/smshex_r.../opt/sharedra.../oldrads
File: check_dstates.py
#!/opt/imh-python/bin/python3
[0] Fix | Delete
"""Collates and displays useful information on processes in D state"""
[1] Fix | Delete
[2] Fix | Delete
import os
[3] Fix | Delete
[4] Fix | Delete
[5] Fix | Delete
def get_uid(pid):
[6] Fix | Delete
"""Get uid of process"""
[7] Fix | Delete
try:
[8] Fix | Delete
with open(f'/proc/{pid}/status', encoding='ascii') as file:
[9] Fix | Delete
for line in file:
[10] Fix | Delete
if line.startswith('Uid:'):
[11] Fix | Delete
return line.split()[1]
[12] Fix | Delete
except OSError:
[13] Fix | Delete
pass
[14] Fix | Delete
return '-'
[15] Fix | Delete
[16] Fix | Delete
[17] Fix | Delete
def get_ctid(pid):
[18] Fix | Delete
"""Get container associated with PID"""
[19] Fix | Delete
try:
[20] Fix | Delete
with open(f'/proc/{pid}/stat', encoding='ascii') as file:
[21] Fix | Delete
return file.read().split()[-1]
[22] Fix | Delete
except OSError:
[23] Fix | Delete
return '-'
[24] Fix | Delete
[25] Fix | Delete
[26] Fix | Delete
def main():
[27] Fix | Delete
"""Grabs the D, jostles it around, and displays it for all to see"""
[28] Fix | Delete
[29] Fix | Delete
iznode = os.path.isdir('/etc/vz/')
[30] Fix | Delete
[31] Fix | Delete
badpids = {}
[32] Fix | Delete
for pid in [i for i in os.listdir('/proc') if i.isdigit()]:
[33] Fix | Delete
try:
[34] Fix | Delete
with open(f'/proc/{pid}/stat', encoding='ascii') as f:
[35] Fix | Delete
statinfo = f.read().split()[2]
[36] Fix | Delete
except OSError:
[37] Fix | Delete
continue
[38] Fix | Delete
[39] Fix | Delete
if 'D' in statinfo:
[40] Fix | Delete
badpids[pid] = {'pid': pid}
[41] Fix | Delete
[42] Fix | Delete
try:
[43] Fix | Delete
badpids[pid]['exe'] = os.readlink('/proc/%s/exe' % pid)
[44] Fix | Delete
except OSError:
[45] Fix | Delete
badpids[pid]['exe'] = ''
[46] Fix | Delete
[47] Fix | Delete
badpids[pid]['uid'] = get_uid(pid)
[48] Fix | Delete
[49] Fix | Delete
isnode_output = "%(pid)6s%(uid)6s %(ctid).4s %(exe)-60s"
[50] Fix | Delete
notnode_output = "%(pid)6s%(uid)6s %(exe)-60s"
[51] Fix | Delete
output_formatter = isnode_output if iznode else notnode_output
[52] Fix | Delete
[53] Fix | Delete
if iznode:
[54] Fix | Delete
badpids[pid]['ctid'] = get_ctid(pid)
[55] Fix | Delete
[56] Fix | Delete
print("\n :: Check Processes in D State ::")
[57] Fix | Delete
[58] Fix | Delete
if iznode:
[59] Fix | Delete
print("\n\n%6s%6s %.4s %s\n" % ('PID', 'UID', 'CTID', 'Executable'))
[60] Fix | Delete
else:
[61] Fix | Delete
print("\n\n%6s%6s %s\n" % ('PID', 'UID', 'Executable'))
[62] Fix | Delete
[63] Fix | Delete
for pid in sorted(badpids, key=lambda x: int(x)):
[64] Fix | Delete
print(output_formatter % badpids[pid])
[65] Fix | Delete
print('\nTotal: %d\n' % len(badpids))
[66] Fix | Delete
[67] Fix | Delete
[68] Fix | Delete
main()
[69] Fix | Delete
[70] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function