要求:
1、客户端收集系统CPU及内存占用率,正在运行的进程名称,周期上传至服务器
2、服务器接受信息,存储至数据库
客户端程序:
环境:win7,python,python的psutil库
程序:
#! /usr/bin/env python#coding=utf-8'''version:2.0监控windows信息:CPU占有率,内存占有率,当前进程名称数据格式:{'name':p.name(),'memeory':p.memory_percent()},{},...]'''import socketimport timeimport psutilimport json#function of Run Server def runServer(s,HOST,PORT): s.connect((HOST, PORT)) print 'connect success!'#function of Close Serverdef closeServer(s): time.sleep(5) s.close()#function of Get CPU State def getCPUstate(interval=1): return (str(psutil.cpu_percent(interval)) + "%")#function of Get Memory def getMemorystate(): mem_rate = 0 for pnum in psutil.pids(): p = psutil.Process(pnum) mem_rate = mem_rate + p.memory_percent() return "%.2f%%"%mem_rate#function of get process's namedef getProcessName(): ProcessNameList = [] for pnum in psutil.pids(): p = psutil.Process(pnum) ProcessNameList.append(p.name()) return ProcessNameList#function of Package Massagedef packMassage(): massage = { 'CPUstate':getCPUstate(),'Memorystate':getMemorystate(),'ProcessName':getProcessName()} print massage return massage #function of Send Massagedef sendMassage(s): s.send(json.dumps(packMassage())) time.sleep(5) s.sendall('\n')#function of Receive Massagedef recvMassage(s): data = s.recv(1024) print 'Received', repr(data)if __name__ == "__main__" : HOST = '172.29.153.22' # The server host PORT = 8000 # The same port as used by the server cycle = 60 # The cycle of sending massage while True: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) runServer(s,HOST,PORT) sendMassage(s) closeServer(s) time.sleep(cycle)
服务器程序:
环境:ubuntu14.04,python,tornado,mongodb
程序:
#! /usr/bin/env python#coding=utf-8 from tornado.tcpserver import TCPServer from tornado.ioloop import IOLoop import pymongo import json class Connection(object): clients = set() def __init__(self, stream, address): Connection.clients.add(self) self._stream = stream self._address = address self._stream.set_close_callback(self.on_close) self.read_message() def read_message(self): self._stream.read_until('\n', self.broadcast_messages) #Read massage until '\n' def broadcast_messages(self, data, massageDict): massageDict=eval(data[:-1]) #str to dict if 'CPUstate' in massageDict.keys() : print '\n','CPUstate:',massageDict['CPUstate'],'\n' if 'Memorystate' in massageDict.keys() : print 'Memorystate:',massageDict['Memorystate'],'\n' if 'ProcessName' in massageDict.keys() : print 'ProcessName:',massageDict['ProcessName'],'\n' self.storeMassage() for conn in Connection.clients: conn.send_message(data) self.read_message() def storeMassage(self,massageDict ): conn = pymongo.Connection("localhost", 27017) db = conn['networkSecurity'] processInfo=db['processInfo'] processInfo.insert(massageDict) print 'success to store the massage!' def send_message(self, data): self._stream.write(data) def on_close(self): print "A user has left the chat room.", self._address Connection.clients.remove(self) class ChatServer(TCPServer): def handle_stream(self, stream, address): print "New connection :", address Connection(stream, address) # print "connection num is:", len(Connection.clients) if __name__ == '__main__': print "Server start ......" server = ChatServer() server.listen(8000) IOLoop.instance().start()