Skip to content

Instantly share code, notes, and snippets.

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

  • Save gmastrokostas/83083e2e523ed5dd828e to your computer and use it in GitHub Desktop.

Select an option

Save gmastrokostas/83083e2e523ed5dd828e to your computer and use it in GitHub Desktop.
Python – Find RAM usage for highest HTTP process and calculate MAXClients setting.
#!/usr/bin/python
import os
import psutil
import subprocess
import time
'''
if os.exists("/etc/redhat-release"):
process = "httpd"
else:
process = "apache2"
'''
pids = [int(pid) for pid in os.listdir('/proc') if pid.isdigit()]
http_proccess = []
for elements in pids:
p = psutil.Process(elements)
p.name()
p.memory_info_ex()[0]
if p.name() == 'apache2':
#print p.name(), p.memory_info_ex()[0]
http_proccess.append(p.memory_info()[0])
process = p.name()
apache_status = True
elif p.name() == 'httpd':
#print p.name(), p.memory_info_ex()[0]
http_proccess.append(p.memory_info()[0])
process = p.name()
apache_status = True
else:
pass
print "--------------------------------------------------"
http_largest_hog = float((max(http_proccess) / 1000000.0))
print "Largest ", process, " process is ", round(http_largest_hog, 10), "MBs"
free_memory_proc = float(psutil.virtual_memory().free) / 1000000.0
print "Free memory with ", process, " running",round(free_memory_proc,10), "MBs"
if apache_status == True:
subprocess.call('service apache2 stop', shell=True )
time.sleep(10)
print "Processing...."
else:
pass
free_memory_Noproc = float(psutil.virtual_memory().free) / 1000000.0
print "Free memory with out ", process, " running",round(free_memory_Noproc,10), "MBs"
ram_gained = free_memory_proc - free_memory_Noproc
print "RAM Gained after ", process, "terminated: ",ram_gained, " MBs"
apache_maxClients = ram_gained / http_largest_hog
print "Adjust Apache MAXCLIENTS parameter at ", apache_maxClients
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment