Python – Find RAM usage for highest HTTP process and calculate MAXClients setting.

The script examines the /proc directory and imports the numerical directories in a list and distinguishes which process is for HTTP. It will then calculate the amount of RAM it uses. This script actually can pickup other process as well, if told so and report the amount of RAM it consumes. The script will then calculate how much RAM it uses

#!/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
Share Button

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload the CAPTCHA.