17 Star 61 Fork 17

zhexiao / mnet

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

mnet

一个基于Softflowd,Kafka,Spark Streaming,Elk,Django开发的网络数据流监控分析后台, 支持Netflow V9与Netflow V5。可以对进入和流出的流量进行异常分析并执行自动化漏洞修复。

架构

image

项目部署

整套服务项目部署流程,整套操作都在deploy目录下进行。

docker跨主机通信

现有机器vm1(192.168.71.148)、vm2(192.168.71.152)和vm3(192.168.71.153)。设vm1为管理主节点,其余为工作节点。

跨主机

# 在 148 上创建manager
$ docker swarm init --advertise-addr 192.168.71.148

# 在其余的工作机器加入节点
$ docker swarm join --token SWMTKN-1-5hcor53t93skr6k8sacb54n8sipo1za7oqa4wgywid8ugjhkjd-6giddshlk9dsa5213ay80um8s 192.168.71.148:2377

# 如果找不到了加入命令,则可以在管理节点输入
$ docker swarm join-token worker

# 查看节点
$ docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
pm5q90eo4vsah02dv9mm74ba8 *   ubuntu              Ready               Active              Leader              18.09.6
vorniuw6o9cjpf03iedg9nfuy     ubuntu              Ready               Active                                  18.09.6
xnkk8zkn0z6lbow6bw589lskv     ubuntu              Ready               Active                                  18.09.6

网络

# 创建网络,--attachable  是为了swarm集群外的容器能够加入该网络
$ docker network create -d overlay --attachable zxnet

# 148机器(manager)测试
$ docker run -d -it --name alpine1 --restart always --network zxnet alpine 
# 152机器worker测试
$ docker run -d -it --name alpine2 --restart always --network zxnet alpine
# 153机器worker测试
$ docker run -d -it --name alpine3 --restart always --network zxnet alpine

# 互相启动ping命令
$ docker exec -it alpine1 ping alpine2
$ docker exec -it alpine1 ping alpine3

注:一定要确保几台机器互相能够ping通,否则下面的启动会出现错误。

Zookeeper

$ docker run \
    --name zoo1 \
    --restart always \
    --network zxnet \
    --detach \
    zookeeper:3.5.5 

Kafka

# 版本修改需要对应修改 Dockerfile-kafka
$ wget -P ./pkg http://mirrors.tuna.tsinghua.edu.cn/apache/kafka/2.3.0/kafka_2.11-2.3.0.tgz

$ docker build -t java-base -f Dockerfile-java-base .
$ docker build -t kafka -f Dockerfile-kafka .

$ docker run \
    --name kf1 \
    --publish 9092:9092 \
    --link=zoo1:zoo1 \
    --env KAFKA_BROKER_ID=1 \
    --env KAFKA_LISTENERS=PLAINTEXT://:9092 \
    --env KAFKA_ZOOKEEPER_CONNECT=zoo1:2181 \
    --restart always \
    --network zxnet \
    --detach \
    kafka 
    
$ docker run \
    --name kf2 \
    --publish 9093:9092 \
    --link=zoo1:zoo1 \
    --env KAFKA_BROKER_ID=2 \
    --env KAFKA_LISTENERS=PLAINTEXT://:9092 \
    --env KAFKA_ZOOKEEPER_CONNECT=zoo1:2181 \
    --restart always \
    --network zxnet \
    --detach \
    kafka 

测试

$ docker exec -it kf1 /kafka/kafka_2.11-2.3.0/bin/kafka-topics.sh --create --zookeeper zoo1:2181 --replication-factor 2 --partitions 1 --topic mnet
$ docker exec -it kf1 /kafka/kafka_2.11-2.3.0/bin/kafka-topics.sh --create --zookeeper zoo1:2181 --replication-factor 2 --partitions 1 --topic mnet_agg

$ docker exec -it kf1 /kafka/kafka_2.11-2.3.0/bin/kafka-topics.sh --describe --zookeeper zoo1:2181 --topic mnet
$ docker exec -it kf1 /kafka/kafka_2.11-2.3.0/bin/kafka-topics.sh --describe --zookeeper zoo1:2181 --topic mnet_agg

$ docker exec -it kf1 /kafka/kafka_2.11-2.3.0/bin/kafka-console-consumer.sh --bootstrap-server kf1:9092,kf2:9092 --topic mnet --from-beginning
$ docker exec -it kf1 /kafka/kafka_2.11-2.3.0/bin/kafka-console-producer.sh --broker-list kf1:9092,kf2:9092 --topic mnet

$ docker exec -it kf1 /kafka/kafka_2.11-2.3.0/bin/kafka-topics.sh --list  --zookeeper zoo1:2181
$ docker exec -it kf1 /kafka/kafka_2.11-2.3.0/bin/kafka-topics.sh --delete --zookeeper zoo1:2181 --topic mnet 

Elasticsearch

$ docker run \
    --name es1 \
    --publish 9200:9200 \
    --publish 9300:9300 \
    --env "discovery.type=single-node" \
    --restart always \
    --network zxnet \
    --detach \
    elasticsearch:6.4.3

Kibana

$ docker run \
    --name kb1 \
    --publish 5601:5601 \
    --link=es1:es1 \
    --env "ELASTICSEARCH_URL=http://es1:9200" \
    --restart always \
    --network zxnet \
    --detach \
    kibana:6.4.3 

Logstash

# default.conf定义数据流动,与下方测试紧密关联
$ cp logstash_default.conf.example logstash_default.conf
$ cp logstash.yml.example logstash.yml

$ docker build -t mylogstash -f Dockerfile-logstash .

$ docker run \
    --name lg1 \
    --publish 4739:4739/udp \
    --publish 21561:21561/udp \
    --publish 21562:21562/udp \
    --link=es1:es1 \
    --restart always \
    --network zxnet \
    --detach \
    mylogstash 

Spark

# 安装包,包名对应Dockerfile-spark-base里面的ENV,如果版本有变化,需要对应修改
$ wget -P ./pkg http://mirrors.tuna.tsinghua.edu.cn/apache/spark/spark-2.4.4/spark-2.4.4-bin-hadoop2.7.tgz
$ wget -P ./pkg http://mirrors.tuna.tsinghua.edu.cn/apache/hadoop/common/hadoop-2.7.7/hadoop-2.7.7.tar.gz

# 镜像
$ docker build -t spark-base -f Dockerfile-spark-base .
$ docker build -t spark-master -f Dockerfile-spark-master .
$ docker build -t spark-worker -f Dockerfile-spark-worker .

# 运行master,如果报错UnresolvedAddressException,检查--name是否与Dockerfile里面的SPARK_MASTER_HOST一致
$ docker run \
    --name spark-master \
    --publish 8080:8080 \
    --publish 7077:7077 \
    --restart always \
    --network zxnet \
    --detach \
    spark-master
    
# 运行worker,启动多个worker需要运行多次,保证--name和--publish唯一即可
$ docker run \
    --name spark-worker1 \
    --publish 8081:8080 \
    --restart always \
    --link=spark-master:spark-master \
    --network zxnet \
    --detach \
    spark-worker
    
$ docker run \
    --name spark-worker2 \
    --publish 8082:8080 \
    --restart always \
    --link=spark-master:spark-master \
    --network zxnet \
    --detach \
    spark-worker

测试1

$ docker exec -it spark-master python3

# 把下面的代码复制进去测试:
from pyspark import SparkContext , SparkConf

conf = SparkConf()
conf.set("spark.master", "spark://spark-master:7077")

# 设置任务使用的核数
conf.set("spark.cores.max", 1)

sc = SparkContext(conf=conf)
ts = sc.parallelize([3, 1, 2, 5])

print(ts.count())
print(ts.collect())

测试2

# 把测试文件夹映射到容器内部
$ docker run \
    --name spark-master \
    --publish 8080:8080 \
    --publish 7077:7077 \
    --restart always \
    --network zxnet \
    -v /vagrant/mnet/deploy/test:/test \
    --detach \
    spark-master
    
# 运行
$ docker exec -it spark-master python3 /test/test1.py

网络收集

# 使用softflowd读取网络数据
$ sudo apt-get install git autoconf bison build-essential libtool libpcap-dev
$ git clone https://gitee.com/zhexiao/softflowd.git
$ cd softflowd
$ autoreconf -i | ./configure
$ make
$ sudo make install

# 抓取数据发送到vm1的端口
$ sudo softflowd -D -v 9 -i ens160 -n 192.168.71.148:4739 -T full

# 发送模拟数据
$ sudo apt install apache2-utils
$ ab -c 100 -n 1000 -t 30 http://sina.com.cn/

数据查看

# 进入192.168.71.148服务器
$ docker logs --tail 100 lg1

正常会输出
{
    "tags" => [
        [0] "netflow_input"
    ],
    "@version" => "1",
    "@timestamp" => 2019-08-30T03:55:00.000Z,
        "host" => "192.168.71.182",
        "netflow" => {
        "first_switched" => "2019-08-30T03:54:50.999Z",
        "last_switched" => "2019-08-30T03:54:51.999Z",
        "input_snmp" => 0,
        "l4_src_port" => 80,
        "in_bytes" => 52,
        "l4_dst_port" => 55474,
        "protocol" => 6,
        "src_tos" => 0,
        "in_src_mac" => "00:50:56:b6:00:ed",
        "output_snmp" => 0,
        "out_dst_mac" => "0c:da:41:ae:dd:b3",
        "version" => 9,
        "flowset_id" => 1024,
        "ipv4_src_addr" => "13.107.21.200",
        "ip_protocol_version" => 4,
        "flow_seq_num" => 3796,
        "in_pkts" => 1,
        "tcp_flags" => 18,
        "ipv4_dst_addr" => "192.168.71.182"
    }
}

spark-client

$ docker build -t spark-client -f Dockerfile-spark-client .

$ docker run \
    --name spark-client1 \
    --restart always \
    --link=spark-master:spark-master \
    --network zxnet \
    --detach \
    spark-client

后端

安装memcached

> sudo apt-get install memcached

安装Mysql mysqlclient必备的依赖

> sudo apt-get install libmysqlclient-dev

安装package

> pip install -r requirements.txt

pre-commit 安装

> pre-commit install
> pre-commit autoupdate

运行服务器

> ./run.sh server
> ./run.sh migration_all
> ./run.sh migrate_all

# 单独app操作
> ./run.sh migration netflow
> ./run.sh migrate netflow

前端

安装SASS

1. install ruby:
    https://github.com/oneclick/rubyinstaller2/releases/download/2.4.1-2/rubyinstaller-2.4.1-2-x64.exe

2. open terminal and install sass:
    gem install sass
    sass -v

安装项目

1. clone 项目:
    git clone https://github.com/zhexiao/mnet.git

2. run sass:
    cd web
    sass --watch sass:css --no-cache --style compressed

编程工具和运行

1. 安装pycharm专业版
2. 右键web/app/index.html -> open in browser

挂载到nginx

> sudo apt-get install nginx
> sudo vim /etc/nginx/sites-enabled/default
------
# if the static file encode has problem, set it
sendfile off;
root /vagrant/mnet/web;
------

> sudo service nginx restart
Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "{}" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright {yyyy} {name of copyright owner} Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

一个基于Softflowd,Kafka,Spark Streaming,Elk,Django开发的网络数据流监控分析后台, 支持Netflow V9与Netflow V5。可以对进入和流出的流量进行异常分析并执行自动化漏洞修复。 展开 收起
Python 等 5 种语言
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Python
1
https://gitee.com/zhexiao/mnet.git
git@gitee.com:zhexiao/mnet.git
zhexiao
mnet
mnet
dev

搜索帮助