1

我有多个客户端到单个服务器双向 iperf 设置以进行网络监控。iperf 服务器运行良好,并根据客户端编写的 cron 作业以 CSV 格式显示输出。

我希望编写一个 python 脚本来自动化将这些 CSV 输出映射到 MySQL 数据库的过程;反过来,它会定期更新和保存,无需人工干预。

我使用 Ubuntu 13.10 机器作为 iperf 服务器。以下是我得到的示例 CSV 输出。这不会存储到文件中,只是显示在屏幕上。

s1:~$ iperf -s -y C  
20140422105054,172.16.10.76,41065,172.16.10.65,5001,6,0.0-20.0,73138176,29215083
20140422105054,172.16.10.76,5001,172.16.10.65,56254,4,0.0-20.0,46350336,18502933
20140422105100,172.16.10.76,54550,172.16.10.50,5001,8,0.0-20.0,67895296,27129408
20140422105100,172.16.10.76,5001,172.16.10.50,58447,5,0.0-20.1,50937856,20292796
20140422105553,172.16.10.76,5001,172.16.10.65,47382,7,0.0-20.1,51118080,20358083
20140422105553,172.16.10.76,41067,172.16.10.65,5001,5,0.0-20.1,76677120,30524007
20140422105600,172.16.10.76,5001,172.16.10.50,40734,4,0.0-20.0,57606144,23001066
20140422105600,172.16.10.76,54552,172.16.10.50,5001,8,0.0-20.0,70123520,28019115
20140422110053,172.16.10.76,41070,172.16.10.65,5001,5,0.0-20.1,63438848,25284066
20140422110053,172.16.10.76,5001,172.16.10.65,46462,6,0.0-20.1,11321344,4497094

我想将它们映射到的字段是:timestamp、server_ip、server_port、client_ip、client_port、tag_id、interval、transferred、bandwidth

我想定期将此 CSV 输出映射到 MySQL 数据库,为此我知道我必须编写一个 Python 脚本(在 cron 作业中)查询并存储在 MySQL 数据库中。我是 Python 脚本和数据库查询的初学者。

我在 [ https://serverfault.com/questions/566737/iperf-csv-output-format]进行了关于服务器故障的另一次讨论;并想基于此构建我的查询。

4

3 回答 3

0

生成 SQL 脚本,然后运行它

如果您不想使用复杂的解决方案,例如sqlalchemy,以下方法是可能的。

  1. 拥有您的 csv 数据,将它们转换为 SQL 脚本
  2. 使用 mysql 命令行工具运行此脚本

在你第一次做之前,确保你在数据库中创建了需要的数据库结构(这个我留给你)。

我的以下示例使用(只是为了方便) package docopt,因此您需要安装它:

$ pip install docopt

CSV 到 SQL 脚本转换实用程序

csv2sql.py

"""
Usage:
    csv2sql.py [--table <tablename>] <csvfile>

Options:
    --table <tablename>  Name of table in database to import into [default: mytable]

Convert csv file with iperf data into sql script for importing
those data into MySQL database.
"""
from csv import DictReader
from docopt import docopt

if __name__ == "__main__":
    args = docopt(__doc__)
    fname = args["<csvfile>"]
    tablename = args["--table"]
    headers = ["timestamp",
               "server_ip",
               "server_port",
               "client_ip",
               "client_port",
               "tag_id",
               "interval",
               "transferred",
               "bandwidth"
              ]
    sql = """insert into {tablename}
    values ({timestamp},"{server_ip}",{server_port},"{client_ip}",{client_port},{tag_id},"{interval}",{transferred},{bandwidth});"""
    with open(fname) as f:
        reader = DictReader(f, headers, delimiter=",")
        for rec in reader:
            print(sql.format(tablename=tablename, **rec)) # python <= 2.6 will fail here

将 CSV 转换为 SQL 脚本

首先让转换实用程序介绍:

$ python csv2sql.py -h
Usage:
    csv2sql.py [--table <tablename>] <csvfile>

Options:
    --table <tablename>  Name of table in database to import into [default: mytable]

Convert csv file with iperf data into sql script for importing
those data into MySQL database.

将您的数据保存在文件中data.csv

$ python csv2sql.py data.csv 
insert into mytable
    values (20140422105054,"172.16.10.76",41065,"172.16.10.65",5001,6,"0.0-20.0",73138176,29215083);
insert into mytable
    values (20140422105054,"172.16.10.76",5001,"172.16.10.65",56254,4,"0.0-20.0",46350336,18502933);
insert into mytable
    values (20140422105100,"172.16.10.76",54550,"172.16.10.50",5001,8,"0.0-20.0",67895296,27129408);
insert into mytable
    values (20140422105100,"172.16.10.76",5001,"172.16.10.50",58447,5,"0.0-20.1",50937856,20292796);
insert into mytable
    values (20140422105553,"172.16.10.76",5001,"172.16.10.65",47382,7,"0.0-20.1",51118080,20358083);
insert into mytable
    values (20140422105553,"172.16.10.76",41067,"172.16.10.65",5001,5,"0.0-20.1",76677120,30524007);
insert into mytable
    values (20140422105600,"172.16.10.76",5001,"172.16.10.50",40734,4,"0.0-20.0",57606144,23001066);
insert into mytable
    values (20140422105600,"172.16.10.76",54552,"172.16.10.50",5001,8,"0.0-20.0",70123520,28019115);
insert into mytable
    values (20140422110053,"172.16.10.76",41070,"172.16.10.65",5001,5,"0.0-20.1",63438848,25284066);
insert into mytable
    values (20140422110053,"172.16.10.76",5001,"172.16.10.65",46462,6,"0.0-20.1",11321344,4497094);

将其全部放入文件中data.sql

$ python csv2sql.py data.csv > data.sql

应用于data.sql您的 MySQL 数据库

最后使用mysql命令(由 MySQL 提供)导入数据库:

$ myslq --user username --password password db_name < data.sql 
于 2014-04-25T19:45:39.437 回答
0

如果您打算使用 Python,那么我建议您使用sqlalchemy

一般做法是:

  • 定义类,它具有您要存储的所有属性
  • 将类的所有属性映射到数据库列和类型
  • 从 csv 读取您的数据(使用例如csv模块),为每一行创建相应的对象作为之前准备的类,并让它被存储。

sqlalchemy 将为您提供更多详细信息和说明,您的要求似乎很简单。

另一种选择是找出现有的 csv 导入工具,其中一些已经在 MySQL 中可用,还有很多其他的。

于 2014-04-25T18:55:07.910 回答
0

这可能不是你要找的那种答案,但是如果你通过在线做一个基础教程学习了一点 sqlite3(一个原生 Python 模块 - “import sqlite3”) ,你就会意识到你的问题一点也不难解决解决。然后只需使用标准计时器,例如 time.sleep() 来重复该过程。

于 2014-04-25T18:57:31.353 回答