美文网首页
python GUI + socket监控服务器流量

python GUI + socket监控服务器流量

作者: 梦想做小猿 | 来源:发表于2018-05-10 15:34 被阅读0次

前言

最近入职了新公司,有一部分windows机器,这些机器没做监控,而且机器上跑的业务并不重要。虽然说不重要,但是出了问题,运维没有及时发现也是运维的锅。也懒得把这些机器加入现有的监控系统。和服务器使用需求方确认后,只要知道网卡有流量在跑,也不需要报警什么的,只要能看到流量就行了。然后一个简单的监控就这样形成了。

构思

  1. window服务器上装python环境,放一个脚本取流量信息,调用一个api接口将流量信息入库,然后一个web端结合echarts做图型展示。

2 . 通过ansible 运行python脚本,获取每台机器的流量

  1. 使用socket + GUI动态展示流量信息

第一种方案代码成本太高,还不如加入监控系统, pass掉。
第二种方案无法给到机器使用方使用(毕竟他们不是运维),而且也没有直观展示,太low, pass掉
所以使用第三种方案

监控方案

被监控端为服务端, 运行一个python脚本(为了避免在每台机器上装python环境,将python打包成exe),脚本计算3秒内的流量信息,主动发送到客户端
客户端通过配置文件去连接配置文件中的所有被监控端,然后在GUI中展示。这样在他们自己上的电脑上就能看到机器流量状态

(GUI和socket现学现卖,代码不好轻喷)

脚本

monitor_server.py

#!/usr/bin/python
import psutil
import socket
import os
import time

host_net_file =  os.path.join(os.path.expanduser('~'), '.host_net.txt')

def get_host_info():
    bytes_sent = psutil.net_io_counters().bytes_sent
    bytes_recv = psutil.net_io_counters().bytes_recv
    net_info = dict(bytes_sent=bytes_sent, bytes_recv=bytes_recv)
    return net_info

def get_old_data():
    if os.path.exists(host_net_file):
        with open(host_net_file, 'r') as f:
            line = f.readline().strip()
        return line

def sock_send_data():
    new_data = get_host_info()

    new_bytes_sent = new_data.get('bytes_sent', 0)
    new_bytes_recv = new_data.get('bytes_recv', 0)

    old_data = get_old_data()
    if old_data:
        old_bytes_sent = eval(old_data).get('bytes_sent', 0)
        old_bytes_recv = eval(old_data).get('bytes_recv', 0)
    else:
        old_bytes_sent, old_bytes_recv = 0, 0

    sent_data = round(float(new_bytes_sent - old_bytes_sent) / 1024, 2)
    recv_data = round(float(new_bytes_recv - old_bytes_recv) / 1024, 2)

    with open(host_net_file, 'w+') as f:
        f.write(str(get_host_info()))

    return '{"sent_data": %s, "recv_data": %s}'%(sent_data, recv_data)

if __name__ == '__main__':
    HOST = '0.0.0.0'
    PORT = 50000
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((HOST, PORT))
    s.listen(5)
    while True:
            conn, addr = s.accept()
            print "Connected by", addr
            while True:
                try:
                    data = sock_send_data()
                    conn.sendall(data)
                    time.sleep(2)
                except:
                    break
    conn.close()
    s.close()        

monitor_client.py

#coding=utf8
from Tkinter import *
import os
import time
import json
import threading
import socket

conf_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'conf', 'config.conf')

def get_conf_info(config):
    with open(config) as f:
        return json.loads(f.read())

def telnet(host, port):
    sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sk.settimeout(1)
    try:
        sk.connect((host, port))
        return True
    except Exception:
        return False
    finally:
        sk.close()

def monitor_sock(ip, port, num):
    data = json.loads(s.recv(1024))
    nat_sent = StringVar()
    nat_recv = StringVar()
    nat_sent.set(data.get('sent_data'))
    nat_recv.set(data.get('recv_data'))
    content_host = Label(main, text=ip, relief=RIDGE, width=15).grid(row=num, column=0)
    content_state = Label(main, text='在线', relief=RIDGE, width=10, fg='green').grid(row=num, column=1)
    content_sent = Label(main, textvariable=nat_sent, relief=RIDGE, width=10).grid(row=num, column=2)
    content_recv = Label(main, textvariable=nat_recv, relief=RIDGE, width=10).grid(row=num, column=3)
    global timer
    timer = threading.Timer(2, monitor_sock, (ip, port, num))
    timer.start()

if __name__ == '__main__':
    main = Tk()
    main.title("系统监控")
    main.geometry('350x300')

    title_host = Label(main, text="主机IP", relief=RIDGE, width=15).grid(row=0, column=0)
    title_state = Label(main, text="状态", relief=RIDGE, width=10).grid(row=0, column=1)
    title_nat_out = Label(main, text="Net_Sent", relief=RIDGE, width=10).grid(row=0, column=2)
    title_nat_in = Label(main, text="Net_Recv", relief=RIDGE, width=10).grid(row=0, column=3)

    num = 1
    for ip, port in get_conf_info(conf_file).items():
        if telnet(ip, port):
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.connect((ip, port))
            timer = threading.Timer(1, monitor_sock, (ip, port, num,))
            timer.start()
        else:
            content_host = Label(main, text=ip, relief=RIDGE, width=15).grid(row=num, column=0)
            content_state = Label(main, text='不在线', relief=RIDGE, width=10, fg='red').grid(row=num, column=1)
            content_sent = Label(main, text=0, relief=RIDGE, width=10).grid(row=num, column=2)
            content_recv = Label(main, text=0, relief=RIDGE, width=10).grid(row=num, column=3)
        num += 1

    main.mainloop()

config.conf

{
    "192.168.2.200": 50000,
    "192.168.2.100": 50000,
    "192.168.2.21": 50000
}

打包

安装pyinstall

pip install pyinstall

打包

pyinstall -F -w monitor_server.py
pyinstall -F -w monitor_client.py

会生成一个dist目录,打包好的exe文件就在该目录下

运行效果图

monitor_client.png

相关文章

网友评论

      本文标题:python GUI + socket监控服务器流量

      本文链接:https://www.haomeiwen.com/subject/uonzrftx.html