Browse Source

удалил лишнее

devel
bashmak 9 years ago
parent
commit
4c7b73422d
  1. 125
      agent/netflow/to_mysql.c
  2. 66
      agent/netflow/to_mysql.py

125
agent/netflow/to_mysql.c

@ -1,125 +0,0 @@
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include <time.h>
#define FLOW_COLS 8
#define uint unsigned int
uint32_t ip2int(const char* ip)
{
uint32_t res = 0;
inet_pton(AF_INET, ip, &res);
return htonl(res);
}
uint str_split(char* str, const char* delimiter, char** pInChunks)
{
char* dat = strtok(str, " ");
register uint n=0;
while(dat)
{
pInChunks[n++] = dat;
dat = strtok(NULL, " ");
}
return n;
}
void curtime(char* pInStrTime, const uint maxlen)
{
time_t rawtime;
time( &rawtime );
strftime(pInStrTime, maxlen, "flowstat_%d%m%Y", localtime( &rawtime ));
}
void convert(char* query, char* pInRes)
{
char* chunks[FLOW_COLS] = {NULL};
int chunk_count = str_split(query, " ", chunks);
if(chunk_count < 7)
{
printf("Too short input line\n");
exit(1);
}
uint32_t src_ip = ip2int(chunks[0]);
uint32_t dst_ip = ip2int(chunks[1]);
uint proto = atoi(chunks[2]);
uint16_t src_port = ip2int(chunks[3]);
uint16_t dst_port = ip2int(chunks[4]);
uint octets = atoi(chunks[5]);
uint packets = atoi(chunks[6]);
sprintf(pInRes, ",(%u,%u,%u,%u,%u,%u,%u)\0",
src_ip, dst_ip, proto, src_port, dst_port, octets, packets);
}
int main()
{
char buf_result_convert[0xff] = {0};
FILE* f = stdin;
char* input_line = malloc(0xff);
size_t input_line_len = 0;
ssize_t read_len = 0;
char table_name[19] = {0};
curtime(table_name, 19);
printf("CREATE TABLE IF NOT EXISTS %s (\n", table_name);
printf("`id` int(10) AUTO_INCREMENT NOT NULL,\n");
printf("`src_ip` INT(10) UNSIGNED NOT NULL,\n");
printf("`dst_ip` INT(10) UNSIGNED NOT NULL,\n");
printf("`proto` smallint(2) unsigned NOT NULL DEFAULT 0,\n");
printf("`src_port` smallint(5) unsigned NOT NULL DEFAULT 0,\n");
printf("`dst_port` smallint(5) unsigned NOT NULL DEFAULT 0,\n");
printf("`octets` INT unsigned NOT NULL DEFAULT 0,\n");
printf("`packets` INT unsigned NOT NULL DEFAULT 0,\n");
printf("PRIMARY KEY (`id`)\n");
printf(") ENGINE=MyISAM DEFAULT CHARSET=utf8;\n");
char ins_sql[0xff] = {0};
sprintf(ins_sql, "INSERT INTO %s(`src_ip`, `dst_ip`, `proto`, `src_port`, `dst_port`, `octets`, `packets`) VALUES", table_name);
// always none
read_len = getline(&input_line, &input_line_len, f);
while(true)
{
register uint n=0xfff;
read_len = getline(&input_line, &input_line_len, f);
if(read_len <= 0)
break;
convert(input_line, buf_result_convert);
printf("%s\n", ins_sql);
// without first comma
printf("%s\n", buf_result_convert+1);
while(n>0)
{
read_len = getline(&input_line, &input_line_len, f);
if(read_len <= 0)
break;
convert(input_line, buf_result_convert);
printf("%s\n", buf_result_convert);
n--;
}
putc(';', stdout);
}
free(input_line);
return 0;
}

66
agent/netflow/to_mysql.py

@ -1,66 +0,0 @@
#!/bin/env python3
import sys
import socket
import struct
from re import sub
from django.utils import timezone
def ip2int(strip):
return struct.unpack("!I", socket.inet_aton(strip))[0]
def convert(query):
dat = sub(r'\s+', ' ', query.strip('\n')).split(' ')
if len(dat) == 1:
return
src_ip = ip2int(dat[0])
dst_ip = ip2int(dat[1])
proto = int(dat[2])
src_port = int(dat[3])
dst_port = int(dat[4])
octets = int(dat[5])
packets = int(dat[6])
sql = ",(%d,%d,%d,%d,%d,%d,%d)" % (
src_ip, dst_ip, proto, src_port, dst_port, octets, packets
)
return sql
if __name__ == '__main__':
f = sys.stdin
table_name = "flowstat_%s" % timezone.now().strftime("%d%m%Y")
print(("CREATE TABLE IF NOT EXISTS %s (" % table_name))
print("`id` int(10) AUTO_INCREMENT NOT NULL,")
print("`src_ip` INT(10) UNSIGNED NOT NULL,")
print("`dst_ip` INT(10) UNSIGNED NOT NULL,")
print("`proto` smallint(2) unsigned NOT NULL DEFAULT 0,")
print("`src_port` smallint(5) unsigned NOT NULL DEFAULT 0,")
print("`dst_port` smallint(5) unsigned NOT NULL DEFAULT 0,")
print("`octets` INT unsigned NOT NULL DEFAULT 0,")
print("`packets` INT unsigned NOT NULL DEFAULT 0,")
print("PRIMARY KEY (`id`)")
print(") ENGINE=MyISAM DEFAULT CHARSET=utf8;")
ins_sql = r"INSERT INTO %s(`src_ip`, `dst_ip`, `proto`, `src_port`, `dst_port`, `octets`, `packets`) VALUES" % table_name
# always none
f.readline()
while True:
n = 0xfff
rs = convert(f.readline())
if not rs: exit()
# without first comma
print(ins_sql)
print((rs[1:]))
while n > 0:
rs = convert(f.readline())
if not rs: exit()
print(rs)
n -= 1
print(';')
f.close()
Loading…
Cancel
Save