Advertisement
Guest User

sql.py

a guest
Apr 26th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.72 KB | None | 0 0
  1. import glob
  2. import gzip
  3. import os
  4. import pymysql
  5.  
  6. # path to gz directory
  7. GZ_DIR = '/Users/mani/Desktop/log file/csv/'
  8. # Database Infomation
  9. DB_HOST='localhost'
  10. DB_USER='diaguser'
  11. DB_PASS='Password'
  12. DB_NAME='PADIAGDB'
  13.  
  14. def csv_reader(file, header=False):
  15.     import csv
  16.     with open(file, "r") as f:
  17.         reader = csv.reader(f)
  18.  
  19.         if header:
  20.             next(reader)
  21.  
  22.         for row in reader:
  23.             yield row
  24.  
  25. # function to connect sql
  26. def import_sql(filename):
  27.     #Prepare SQL query to INSERT a record into the database.
  28.  
  29.     # Open database connection
  30.     db = pymysql.connect(host=DB_HOST,
  31.                          user=DB_USER,
  32.                          password=DB_PASS,
  33.                          db=DB_NAME,
  34.                          charset='utf8')
  35.  
  36.     for row in csv_reader(filename, False):
  37.         # prepare a cursor object using cursor() method
  38.         with db.cursor() as cursor:
  39.             if row[3] == "THREAT":
  40.                 sql = ( """INSERT INTO threat(
  41.                        Domain,Receive_Time,Serial,Type,Threat_Content_Type,
  42.                        Config_Version,Generate_Time,Source_address,Destination_address,
  43.                        NAT_Source_IP,NAT_Destination_IP,Rule,Source_User,
  44.                        Destination_User,Application,Virtual_System,Source_Zone,
  45.                        Destination_Zone,Inbound_Interface,Outbound_Interface,Log_Action,
  46.                        Time_Logged,Session_ID,Repeat_Count,Source_Port,Destination_Port,
  47.                        NAT_Source_Port,NAT_Destination_Port,Flags,IP_Protocol,Action,
  48.                        URL_Filename,Threat_Content_Name,Category,Severity,Direction,
  49.                        Sequence_Number,Action_Flags,Source_Country,Destination_Country,
  50.                        cpadding,contenttype,pcap_id,filedigest,cloud,url_idx,user_agent,
  51.                        filetype,xff,referer,sender,subject,recipient,reportid,
  52.                        dg_hier_level_1,dg_hier_level_2,dg_hier_level_3,dg_hier_level_4,
  53.                        Virtual_System_Name,Device_Name,file_url )
  54.                        
  55.                        VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,
  56.                        %s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,
  57.                        %s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s
  58.                        );""" )
  59.            
  60.                 try:
  61.                     cursor.execute('SET foreign_key_checks = 0')
  62.                     # Execute the SQL command
  63.                     cursor.execute(sql, row)
  64.  
  65.                     #Commit your changes in the database
  66.                     cursor.execute('SET foreign_key_checks = 1')
  67.                     db.commit()
  68.  
  69.                 except Exception as e:
  70.                     print(e)
  71.                     # Rollback in case there is any error
  72.                     db.rollback()
  73.  
  74.     # disconnect from server
  75.     db.close()
  76.  
  77. #####################################################################
  78. # Main
  79. #####################################################################
  80.  
  81. # List of all gz files
  82. gz_files = (gz for gz in glob.glob(os.path.join(GZ_DIR, '*.gz')))
  83.  
  84. # Loop through all gz files
  85. for gz_file in gz_files:
  86.     # Step 1
  87.     with gzip.open(gz_file, 'rb') as in_file:
  88.         s = in_file.read()
  89.  
  90.     # store the uncompressed data
  91.     sql_file = gz_file[:-3]
  92.  
  93.     #Convert to csv
  94.     sql_file= sql_file[:-4] + '.csv'
  95.     with open(sql_file, 'wb') as out_file:
  96.         out_file.write(s)
  97.  
  98.     # Step 2: import to sql
  99.     import_sql(sql_file)
  100.  
  101.     # Step 3: remove uncompresed file
  102.     os.remove(sql_file)
  103.  
  104.     # Step 4: in loop, back to step 1 automatically for another gz files
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement