Python script que recoge estadísticas de carga sobre un sistema linux 2.4 y 2.6, este script no es intrusivo. Y no requiere ser programado dentro de cron.
#!/usr/bin/python
import os, sys , commands, time , re
#Necesario en Python 1.5
import string
##################################
# Script: recolector.py #
# Escrito por: Edwin Plauchu #
# mailto: j4nusx@gmail.com #
# 28 / feb / 2007 #
##################################
DATE = str(time.localtime(time.time())[0:6][0]) + "-" + str(time.localtime(time.time())[0:6][1]) + "-" + str(time.localtime(time.time())[0:6][2])
RECOLECTOR_LOG_FILE_PATH = "/var/log/recolector-"+ DATE + ".log"
INTERVAL_SEG = 3
def LoadAverage():
file = open( '/proc/loadavg' )
lines = file.readlines()
for line in lines:
return string.split( line , " ")[0:3]
def UsedMemoryInMegaBytes():
file = open( '/proc/meminfo' )
lines = file.readlines()
mem_total = 0
mem_free = 0
expression_for_mem_total = re.compile( r"MemTotal:" )
expression_for_mem_free = re.compile( r"MemFree:" )
for line in lines:
if expression_for_mem_total.search( line ):
mem_total = int(string.split(string.split( line , ":")[1],"kB")[0])
if expression_for_mem_free.search( line ):
mem_free = int(string.split(string.split( line , ":")[1],"kB")[0])
return {'USED':( (mem_total - mem_free) / 1024 ) , 'FREE':(mem_free/1024) }
def CapturaEnLog( file_absolute_path = "/tmp/recolector.log" , string_to_add = None ):
now = time.localtime(time.time())[0:6]
fecha_formateda = str(now[3]) + ":" + str(now[4]) + ":" + str(now[5])
fichero_log = open(file_absolute_path, 'a' )
if string_to_add:
fichero_log.write( fecha_formateda + "," + string_to_add + "\n")
fichero_log.close()
def CpuStat():
file = open( '/proc/stat' )
lines = file.readlines()
expression_for_cpu_sigma = re.compile( r"^cpu " )
for line in lines:
if expression_for_cpu_sigma.search(line):
line = line[5:]
line = string.split(line," ")
return {'USER': int(line[0]), 'NICE': int(line[1]), 'SYSTEM':int(line[2]) , 'IDLE':int(line[3]), 'IOWAIT':int(line[4]), 'IRQ':int(line[5]), 'SOFTIRQ':int(line[6])}
def SigmaJiffies( diccionario = {}):
total_jiffies = 0
for key in diccionario.keys():
total_jiffies = total_jiffies + diccionario[key]
return total_jiffies
def DeltaTime(interval):
x = CpuStat()
time.sleep(interval)
y = CpuStat()
for key in x.keys():
y[key] = x[key] - y[key]
return y
def main():
while "Hasta el final de los tiempos":
dt = DeltaTime(INTERVAL_SEG)
sum_jiffies = SigmaJiffies(dt)
cpu_user_pct = (dt['USER'] * 100.00 / sum_jiffies)
nice_user_pct = (dt['NICE'] * 100.00 / sum_jiffies)
system_pct = (dt['SYSTEM'] * 100.00 / sum_jiffies)
idle_pct = (dt['IDLE'] * 100.00 / sum_jiffies)
iowait_pct = (dt['IOWAIT'] * 100.00 / sum_jiffies)
irq_pct = (dt['IRQ'] * 100.00 / sum_jiffies)
softirq_pct = (dt['SOFTIRQ'] * 100.00 / sum_jiffies)
data = str('%.4f' %cpu_user_pct) + "," +str('%.4f' %nice_user_pct) + "," +str('%.4f' %system_pct) + "," +str('%.4f' %idle_pct) + "," +str('%.4f' %iowait_pct) + "," +str('%.4f' %irq_pct) + "," +str('%.4f' %softirq_pct) + "," + str(UsedMemoryInMegaBytes()['USED'])
CapturaEnLog( RECOLECTOR_LOG_FILE_PATH , data )
if __name__ == "__main__":
sys.exit( main() )
Tipo de Salida Generada
La forma de interpretar los datos mostrados a continuacion es la siguiente:
HORA,USER,NICE,SYSTEM,IDLE,IOWAIT,IRQ,SOFTIRQ,USEDMEMORY(MB),LOADAVERAGE
10:10:37,0.8306,-0.0000,0.4983,98.5050,0.1661,-0.0000,-0.0000,734,0.19 10:10:40,6.0403,-0.0000,0.5034,93.4564,-0.0000,-0.0000,-0.0000,734,0.17 10:10:43,5.3422,-0.0000,0.3339,94.3239,-0.0000,-0.0000,-0.0000,736,0.16 10:10:46,1.6667,-0.0000,0.3333,97.8333,-0.0000,0.1667,-0.0000,736,0.16 10:10:49,2.3372,-0.0000,0.5008,96.9950,-0.0000,0.1669,-0.0000,736,0.15 10:10:52,2.3609,-0.0000,0.6745,96.9646,-0.0000,-0.0000,-0.0000,736,0.15 10:10:55,0.1656,-0.0000,-0.0000,99.6689,-0.0000,0.1656,-0.0000,736,0.21 10:10:58,0.5008,-0.0000,0.1669,99.3322,-0.0000,-0.0000,-0.0000,736,0.20 10:11:1,0.4992,-0.0000,0.1664,99.3344,-0.0000,-0.0000,-0.0000,736,0.20 10:11:4,-0.0000,-0.0000,0.1669,99.8331,-0.0000,-0.0000,-0.0000,736,0.18 10:11:7,1.3333,-0.0000,-0.0000,98.6667,-0.0000,-0.0000,-0.0000,736,0.18 10:11:10,1.0050,-0.0000,0.1675,98.8275,-0.0000,-0.0000,-0.0000,736,0.17
Se genera un fichero log por dia sobre la carpeta /var/log/
Tal vez no comprendas los datos arrojados por este script… asi que anexo la siguiente liga que lo explica…..
http://www.linuxhowtos.org/System/procstat.htm
