import sys, csv, os
import subprocess

def run_command(command):
    p = subprocess.Popen(command,
                        stdout=subprocess.PIPE,
                        stderr=subprocess.STDOUT, shell=True)
    return iter(p.stdout.readline, b'')


def post2Influx(statement):
    prefix = "curl -i -XPOST 'http://localhost:8086/write?db=defcon1' --data-binary '"
    os.system(prefix + statement + "'")

def write2Influx(row):
    #print '==>', row
    try:
        if float(row[0]) <= 0.0:
            return
    except:
        return
    epoch_time  = row[0]
    channel     = row[1]
    signal      = row[2]
    drate       = row[3]
    rt_freq     = row[4]
    rt_signalDBM= row[5]
    rt_datarate = row[6]
    ra          = row[7]
    da          = row[8]
    ta          = row[9]
    sa          = row[10]
    fctype      = row[11]
    fcsubtype   = row[12]
    fcretry     = row[13]
    frame_len   = row[14]
    frame_seq   = row[15]
    beacon_interval = row[16]
        

    tstamp = epoch_time[:10] + epoch_time[11:]
    series = 'metainfo'
    st = series
    
    # tags
    if ra:
        st += ',receiver_addr=' + ra
    if da:
        st += ',dest_addr=' + da
    if ta:
        st += ',sender_addr=' + ta
    if sa:
        st += ',source_addr=' + sa
    if fctype:
        st += ',fc_type=' + fctype
    if fcsubtype:
        st += ',fc_subtype=' + fcsubtype
    if fcretry:
        st += ',fc_retry=' + fcretry
    # value
    st += ' '
    if not frame_len:
        return
    st += 'length=' + frame_len
    if channel:
        st += ',ieee_channel=' + channel
    if signal:
        st += ',signal_percent=' + signal
    if drate:
        st += ',data_rate=' + drate
    if rt_freq:
        st += ',frequency=' + rt_freq
    if rt_signalDBM:
        st += ',signal_dbm=' + rt_signalDBM
    if rt_datarate:
        st += ',datarate_Mbps=' + rt_datarate
    if frame_seq:
        st += ',frame_seq=' + frame_seq
    if beacon_interval:
        st += ',beacon_interval=' + beacon_interval
    #time stamp
    st += ' '
    st += tstamp
    st += '\n'
    
    write2Influx.count += 1
    write2Influx.buffer += st
    if write2Influx.count > 99:
        print '---'
        #print write2Influx.buffer
        #sys.exit(0)
        post2Influx(write2Influx.buffer)
        write2Influx.buffer = ''
        write2Influx.count = 0
    else:
        print '\r\r'+ str(write2Influx.count),
    
# init the counter
write2Influx.count = 0
write2Influx.buffer = ''

def post2Influx(statement):
    prefix = "curl -i -XPOST 'http://localhost:8086/write?db=live' --data-binary '"
    os.system(prefix + statement + "'")

if len(sys.argv) != 2:
    print 'Usage:', sys.argv[0], 'interface'

tsharkPara = ['-e frame.time_epoch',
            '-e wlan.channel',
            '-e wlan.signal_strength',
            '-e wlan.data_rate',
            '-e radiotap.channel.freq',
            '-e radiotap.dbm_antsignal',
            '-e radiotap.datarate',
            '-e wlan.ra',
            '-e wlan.da',
            '-e wlan.ta',
            '-e wlan.sa',
            '-e wlan.fc.type',
            '-e wlan.fc.subtype',
            '-e wlan.fc.retry',
            '-e frame.len',
            '-e wlan.seq',
            '-e wlan_mgt.fixed.beacon']

tsharkCmd = '/usr/bin/tshark ' + ' '.join(tsharkPara)
tsharkCmd += ' -T fields -E separator=,'
tsharkCmd += ' -i ' + sys.argv[1]

print 'Running:', tsharkCmd
print '=========='
content = []
for aline in run_command(tsharkCmd):
    alist = aline.strip().split(',')
    if len(alist) != len(tsharkPara):
        print "error! # of fields =", len(alist), 'should be', len(tsharkPara)
        continue
    
    # print alist
    # Write to database
    write2Influx(alist)
print '=========='

print 'All done!'

@@ -30,16 +30,30 @@
 bprocess
+%0Aimport urllib
 %0A%0Adef ru
@@ -275,32 +275,46 @@
 Influx(statement
+, hostname, db
 ):%0A    prefix = 
@@ -341,39 +341,44 @@
 p://
-localhost:8086/write?db=defcon1
+%22 + hostname + %22/write?db=%22 + db + %22
 ' --
@@ -1002,16 +1002,42 @@
 row%5B16%5D%0A
+    ssid        = row%5B17%5D%0A
         
@@ -1487,16 +1487,71 @@
 fcretry%0A
+    if ssid:%0A        st += ',ssid='+urllib.quote(ssid)%0A
     # va
@@ -2331,16 +2331,42 @@
 x.buffer
+, sys.argv%5B1%5D, sys.argv%5B2%5D
 %0A       
@@ -2418,16 +2418,42 @@
 x.buffer
+, sys.argv%5B2%5D, sys.argv%5B3%5D
 )%0A      
@@ -2644,161 +2644,8 @@
 ''%0A%0A
-def post2Influx(statement):%0A    prefix = %22curl -i -XPOST 'http://localhost:8086/write?db=live' --data-binary '%22%0A    os.system(prefix + statement + %22'%22)%0A%0A
 if l
@@ -2660,17 +2660,17 @@
 rgv) != 
-2
+4
 :%0A    pr
@@ -2707,16 +2707,38 @@
 terface'
+, 'db_host', 'db_name'
 %0A%0Atshark
@@ -3282,16 +3282,48 @@
 .beacon'
+,%0A            '-e wlan_mgt.ssid'
 %5D%0A%0Atshar

Add hostname and db name support