Skip to content

Instantly share code, notes, and snippets.

@gmastrokostas
Created November 25, 2015 09:30
Show Gist options
  • Select an option

  • Save gmastrokostas/29d5728c7a740d7cfe5e to your computer and use it in GitHub Desktop.

Select an option

Save gmastrokostas/29d5728c7a740d7cfe5e to your computer and use it in GitHub Desktop.
Python – psutil module – Gather system info with dynamically generated histograms
#!/usr/bin/python
import psutil
def Virtual_memory_usage():
#VIRTUAL MEMORY INFORMATION
virtu_full_num = psutil.virtual_memory().percent
virtnum_int = int(float(psutil.virtual_memory().percent))
print "VRT MEM USAGE: ", virtu_full_num
#print "0----------------100%" ##from initial version of script
for loop_hist in range (1):
virtnum_int = virtnum_int / 10
whitespace = " " * (20 - 4*virtnum_int)
print ">%s%s%s" % ("||"*virtnum_int, virtu_full_num, whitespace), "MAX"
#Displays just the vertical lines.
#print ">%s%s" % ("||"*virtnum_int, whitespace), "MAX"
print ""
def Swap_memory_usage():
#SWAP SIZE USAGE
swap_mem_full_num = psutil.swap_memory().percent
swapnum_int = int(float(psutil.swap_memory().percent))
print "SWP MEM USAGE: ", swap_mem_full_num
for loop_hist in range (1):
swapnum_int = swapnum_int / 10
whitespace = " "*(20 - 4*swapnum_int)
print ">%s%s%s" % ("||"*swapnum_int, swap_mem_full_num, whitespace), "MAX"
print ""
def CPU_general_usage():
#CPU INFORMATION.
cpu_prec_usage = psutil.cpu_percent(interval=1)
cpunum_int = int (float(psutil.cpu_percent(interval=1)))
print "CPU PRC USAGE: ", cpu_prec_usage
for loop_hist in range (1):
cpunum_int = cpunum_int / 10
whitespace = " " * (20 - 4*cpunum_int)
print ">%s%s%s" % ("||"*cpunum_int, cpu_prec_usage, whitespace), "MAX"
#Displays just the vertical lines.
#print "0%s%s" % ("||"*virtnum_int, whitespace), "100"
print ""
def Partition_root_space():
#PARTITION SPACE INFORMATION
disk_prec_usage = psutil.disk_usage('/').percent
disknum_int = int(float(psutil.disk_usage('/').percent))
print "ROOT PRC USAGE: ", disk_prec_usage
for loop_hist in range (1):
disknum_int = disknum_int / 10
whitespace = " " * (20 - 4*disknum_int)
print ">%s%s%s" % ("||"*disknum_int,disk_prec_usage,whitespace), "MAX"
print ""
def Banned_users_monitor():
#BANNED USERS MONITORING.
users = psutil.users()
#list of banned users
banned_users = ['root','gmastrokostas', 'test', 'test1', 'test3', 'root']
#empty list to insert the logged users.
logged_users = []
#Will used to insert the results between the banned_users and logged_users.
#This will distinguish users who are in the banned list Vs those who are not.
banned_logged_users = []
#The loop goe through logged in users and inserts elements in the empty logged_users
for loop in users:
logged_users.append(loop.name)
#This checks for common elements between the two lists logged_users and banned_users
#Look for info about set here https://docs.python.org/2/library/stdtypes.html#set.intersection
report_users = set(logged_users).intersection(banned_users)
if report_users:
print "UNAUTHORIZED LOGGED IN USERS: "
print (", ".join(report_users))
else:
pass
print ""
def running_services():
http_process = []
pids = [int(pid) for pid in os.listdir('/proc') if pid.isdigit()]
for elements in pids:
p = psutil.Process(elements)
p.name()
if p.name() == 'apache2' or 'httpd' or 'nginx':
http_status = True
if http_status == True:
print "This is a web server"
Virtual_memory_usage()
Swap_memory_usage()
CPU_general_usage()
Partition_root_space()
Banned_users_monitor()
running_services()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment