服務器監控客戶端系統狀態5.0

終於改爲zmq通訊,可是一個循環兩對send,recv,開多個客戶端時會混亂。前端

serverpython

#! /usr/bin/env python
#coding=utf-8
'''
fileName: server.py
數據發送方式:zmq
'''

import zmq  
import time
import json
from Queue import Queue
import threading
import pymongo


class Producer(threading.Thread):
    def __init__(self, t_name, processdata):
        threading.Thread.__init__(self, name=t_name)
        self.data=processdata
    def run(self):
        recvMassage()

def recvMassage():
    context = zmq.Context()  
    socket = context.socket(zmq.REP)  
    socket.bind("tcp://*:5555")  
    while (True):
        message = socket.recv()  
        print 'The message from the client is : ',message
        global sign
        if message == '*':  # 前端要求更改週期
            socket.send("*")     # "@"表示更改週期
            sign = 1   # 設置標誌位表示要更改週期
            c = socket.recv() 
            global cycle
            cycle = c
        elif:
            if sign == 1:    # 更改週期
                socket.send("@")     # "@"表示更改週期
                socket.recv()        #沒用
                socket.send(cycle)
                sign = 0
            else :
                socket.send("#")     # "#"表示發送監控數據
                message = socket.recv()
                print "message from client:", message
                load = json.loads(message)  
                info = dict(load)
                print type(info)
                handleData(info)
                socket.send("#")

def handleData(info):
        conn = pymongo.Connection("localhost", 27017)
        db = conn['networkSecurity']
        systemInfo = db['systemInfo']
        # 構造dict數據
        message = {'IP':info[u'IP'],'CPUstate':info[u'CPUstate'],'Memorystate':info[u'Memorystate'],
            'PortState':info[u'PortState'],'ProcessName':info[u'ProcessName']}
        print 'Client said :\nIP:%s\nCPUstate:%s\nMemorystate:%s\nPortState:%s\nProcessName:%s'%(message['IP'],message['CPUstate'],message['Memorystate'],message['PortState'],message['ProcessName'])
        # 將數據放入隊列
        processdata.put(message)
        # 將數據存入數據庫
        systemInfo.insert(message)
        print 'success to store the data!'

#消費者
class Consumer(threading.Thread):
    def __init__(self, t_name,processdata):
        threading.Thread.__init__(self, name=t_name)
    def run(self):
        print "%s: %s is consuming in the queue!/n" %(time.ctime(), self.getName())
        message = processdata.get()
        print 'the message in the queue is :   ',message
        print type(message)
        monitorSystem(message)

# 黑白名單匹配,info爲字典
def monitorSystem(info):
    warning = 0
    whiteList = ['cmd.exe']
    blackList = ['sublime_text.exe']
    # for info in systemInfo.find():
    #     print info
    IP = info['IP']
    processName = info['ProcessName']
    for process in processName:
        if process in blackList:
            warning = 1
            print 'Process  %s in black list is running in IP %s ! '%(process,IP)
    for process in whiteList:
        if process not in processName:
            warning = 1
            print 'Process %s in white list is not running in IP %s ! '%(process,IP)
    if warning == 0:
        print 'Host %s is running legally ! '%IP

if __name__ == '__main__':
    # 處理隊列
    processdata=Queue()

    # 生產進程:接受數據
    producer = Producer('Pro.', processdata)

    # 消費進程:處理數據,黑白名單匹配
    consumer = Consumer('Con.', processdata)

    producer.start()
    consumer.start()

    producer.join()
    consumer.join()

 

client數據庫

#! /usr/bin/env python
#coding=utf-8
'''
fileName:client.py
監控windows信息:CPU佔有率,內存佔有率,端口開放狀況,當前進程名稱
數據格式:    {'IP':getIp(),'CPUstate':getCPUState(),'Memorystate':getMemoryState(),
            'PortState':getPortState(),'ProcessName':getProcessName()}
數據發送方式:zmq
'''

import zmq  
import psutil
import json
import socket
import thread
import time


def sendMessage(portState): 
    context = zmq.Context()  
    print "Connecting to server..."  
    socket = context.socket(zmq.REQ)  
    socket.connect ("tcp://192.168.111.135:5555") 
    ip = json.dumps(getIp())
    socket.send(ip)
    # print '#######################'
    message = socket.recv()  
    print "Received reply: ", message
    if message =='@':        # "@"表示更改週期
        socket.send('@')
        global cycle
        info = socket.recv()
        json.loads(info)
        cycle = int(info)
        print 'type(cycle)',type(cycle)
    elif message == '#':    # "#"表示發送監控數據
        mymessage = json.dumps(packMessage(portState))
        socket.send(mymessage)
        socket.recv()

# 構造數據包
def packMessage(portState):
    message =     {'IP':getIp(),'CPUstate':getCPUState(),'Memorystate':getMemoryState(),
                'PortState':portState,'ProcessName':getProcessName()}
    print 'My message is :\nIP:%s\nCPUstate:%s\nMemorystate:%s\nPortState:%s\nProcessName:%s'%(message['IP'],message['CPUstate'],message['Memorystate'],message['PortState'],message['ProcessName'])
    return message

# 獲取本機IP
def getIp():
    myname = socket.getfqdn(socket.gethostname())
    myaddr = socket.gethostbyname(myname)
    return myaddr

# 獲取CPU使用率 
def getCPUState(interval=1):
    return (str(psutil.cpu_percent(interval)) + "%")

# 獲取內存使用率  
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

# 輸入IP和端口號,掃描判斷端口是否開放
def socket_port(ip,port,portList):
    try:
        if port >= 65535:
            print u'端口掃描結束'
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        result = s.connect_ex((ip,port))
        if result == 0:
            lock.acquire()
            portList.append(port)
            lock.release()
        s.close()
    except:
        print u'端口掃描異常'

# 輸入IP,掃描IP的0-65534端口狀況 
def ip_scan(ip):
    portList = []
    socket.setdefaulttimeout(3)
    try:
        for i in range(0,65534):
            thread.start_new_thread(socket_port,(ip,int(i),portList))
        # 返回全部開放的端口號
        return portList
    except:
        print u'掃描ip出錯'

# 獲取正在運行的進程名稱
def getProcessName():
    ProcessNameList = []
    for pnum in psutil.pids():
        p = psutil.Process(pnum) 
        ProcessNameList.append(p.name())
    return ProcessNameList    



if __name__ == '__main__':
    global cycle
    cycle = 60
    while(True):
        myIP = getIp()
        lock = thread.allocate_lock()
        portState = ip_scan(myIP)
        print portState
        sendMessage(portState)
        print 'cycle is',cycle
        time.sleep(cycle)
    
相關文章
相關標籤/搜索