59 lines
2.2 KiB
Python
Executable File
59 lines
2.2 KiB
Python
Executable File
#!/usr/bin/python
|
|
#echo server program
|
|
import socket
|
|
import subprocess
|
|
|
|
help_message="""Recognized commands:
|
|
HELO (default)
|
|
SHORT
|
|
|
|
"""
|
|
|
|
HOST = '' # Symbolic name meaning all available interfaces
|
|
PORT = 50774 # Arbitrary non-privileged port
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
s.bind((HOST, PORT))
|
|
while 1:
|
|
s.listen(1)
|
|
conn, addr = s.accept()
|
|
#print 'Connected by', addr
|
|
while 1:
|
|
data = conn.recv(1024)
|
|
if not data: break
|
|
message=""
|
|
if (data=="HELO"):
|
|
uptime=subprocess.Popen(['uptime'], stdout=subprocess.PIPE)
|
|
mem=subprocess.Popen(['free -tg | head -n 2'], stdout=subprocess.PIPE,shell=True)
|
|
users=subprocess.Popen(['who -q | head -n 1'], stdout=subprocess.PIPE,shell=True)
|
|
message='='*10+socket.gethostname()+'='*10+'\n'+uptime.stdout.read()+mem.stdout.read()+users.stdout.read()
|
|
uptime.kill()
|
|
mem.kill()
|
|
users.kill()
|
|
elif (data=="SHORT"):
|
|
load=open('/proc/loadavg','r').read()
|
|
load=load.split(" ")
|
|
cpu=open('/proc/cpuinfo','r').read()
|
|
cpucount=0
|
|
for row in cpu.split("\n"):
|
|
if row.startswith('processor'):
|
|
cpucount+=1
|
|
loadpercent=int(100*float(load[0])/float(cpucount))
|
|
|
|
mem=open('/proc/meminfo','r').read()
|
|
memory={'total':0, 'free':0, 'cache':0, 'percent':0}
|
|
for row in mem.split("\n"):
|
|
if row.startswith('MemTotal'):
|
|
memory['total']=float(''.join(c for c in row if c.isdigit()))
|
|
if row.startswith('MemFree'):
|
|
memory['free']=float(''.join(c for c in row if c.isdigit()))
|
|
if row.startswith('Cached'):
|
|
memory['cache']=float(''.join(c for c in row if c.isdigit()))
|
|
memory['percent']=int(100 - (100*(memory['free']+memory['cache'])/memory['total']))
|
|
|
|
message='|'+socket.gethostname()+' L/M:'+str(loadpercent).zfill(3)+'/'+str(memory['percent']).zfill(3)+'% '
|
|
else:
|
|
message=help_message
|
|
conn.send(message)
|
|
conn.close()
|
|
|