Exemplo n.º 1
0
def begin():
    historyLimit = 10
    templateFilename = 'fastbit.tmpl'
    # Imports for mako
    try:
        from mako.template import Template
        from mako.runtime import Context
        from mako.lookup import TemplateLookup
        from mako import exceptions
    except:
        ntop.printHTMLHeader('ntop Python Configuration Error', 1, 0)
        ntop.sendString(
            "<b><center><font color=red>Please install <A HREF=http://www.makotemplates.org/>Mako</A> template engine</font> (sudo easy_install Mako)</center></b>"
        )
        ntop.printHTMLFooter()
        return

    # Fix encoding
    #reload(sys)
    #sys.setdefaultencoding("latin1")
    ntopSpoolPath = ntop.getSpoolPath()
    tempQueryHistory = "fbQueryHistory"

    rows = []
    cols = None

    databasePath = ntop.getDBPath()
    '''TODO CHANGE THIS!!! fastbit database location'''
    fb_DB = '/tmp/'  #ntop.getPreference ("fastbitDB");    #default location of the fastbit DB

    if fb_DB is not None:
        databasePath = fb_DB
    pathFastBit = os.path.join(databasePath, 'fastbit' + os.path.sep)

    form = cgi.FieldStorage()
    #get  from the url the parameter configfile that contains the
    #path+filename of the configfile to read
    '''Parameters for calling the autocomplete function'''
    selectAuto = form.getvalue('selectAuto')
    fromAuto = form.getvalue('fromAuto')

    documentRoot = os.getenv('DOCUMENT_ROOT', '.')

    if selectAuto:
        print >> sys.stderr, "PARAMETRO SELECT PASSATO " + str(selectAuto)
        #a request from the autocomplete script, return a json string with the matching files names
        ntop.sendHTTPHeader(1)
        ntop.sendString(expandSelect(selectAuto, pathFastBit, fromAuto))
        return
    elif fromAuto:
        ntop.sendHTTPHeader(1)
        ntop.sendString(expandFrom(fromAuto, pathFastBit))
        return
    else:

        history = {
            'history': []
        }  #object containing the last 10 queries successfully executed

        try:
            tempFile = open(os.path.join(ntopSpoolPath, tempQueryHistory), 'r')
            history = pickle.load(tempFile)

        except IOError:  #the tempFile does not exist or some other problem
            print >> sys.stderr, 'Fastbit query: IOError while accessing queries history ' + tempQueryHistory
            history = {
                'history': []
            }  #object containing the last 10 queries successfully executed
        except pickle.PickleError, pickle.UnpicklingError:
            print >> sys.stderr, "Error while loading the queries history removing file..." + os.path.join(
                ntopSpoolPath, tempQueryHistory)
            try:
                os.remove(os.path.join(ntopSpoolPath, tempQueryHistory))
            except:
                pass
            raise

        selectArg = form.getvalue('select')
        fromArg = form.getvalue('from')
        whereArg = form.getvalue('where')
        limit = int(form.getvalue('limit', 100))

        carPosition = int(
            form.getvalue('carPosition', 0)
        )  #to be used to expand in the middle of a string knowing the cursor position...

        queryPar = ["", "", "", limit]  #show limit 100 as default

        ntop.printHTMLHeader('Fastbit Query', 1, 0)

        if selectArg and fromArg:
            queryPar = [selectArg, fromArg, whereArg, limit]
            try:
                #pipe = subprocess.Popen (['ntop.getPreference ("fastbit.fbquery")', "-c", selectArg, "-d", fromArg, "-q", whereArg, "-P", "-L", limit],
                print >> sys.stderr, "Query passed: SELECT %s FROM %s WHERE %s LIMIT %i" % (
                    selectArg, os.path.join(pathFastBit,
                                            fromArg), whereArg, limit)
                res = fastbit.query(os.path.join(pathFastBit, fromArg),
                                    selectArg, whereArg, limit)
                print >> sys.stderr, 'Number of records: %i' % len(
                    res['values'])
            except:
                print >> sys.stderr, 'ERROR Executing query: ' + (
                    "SELECT %s FROM %s WHERE %s LIMIT %i" %
                    (selectArg, os.path.join(pathFastBit,
                                             fromArg), whereArg, limit))
                res = {}
            if res is not None and 'columns' in res:
                cols = res['columns']
                #control if the history list has reach the limit
                if len(history['history']) >= historyLimit:
                    history['history'] = history['history'][0:historyLimit - 1]
                #insert  the newly executed query at the beginning of the list
                history['history'] = [
                    "SELECT %s FROM %s WHERE %s" %
                    (selectArg.upper(), fromArg.upper(), whereArg.upper())
                ] + history['history']
                saveTempFile(history,
                             os.path.join(ntopSpoolPath, tempQueryHistory))
            else:
                cols = []
            if res is not None and 'values' in res:

                toFormat = getAddrCols(
                    selectArg)  #get a list of addr column numbers

                for x in res['values']:
                    for j in toFormat:
                        #for every number in the list format as an IP ADDR
                        try:
                            #just ipv4
                            x[j] = socket.inet_ntop(socket.AF_INET,
                                                    struct.pack('>L', x[j]))
                        except:
                            #could be an ipv6
                            try:
                                x[j] = socket.inet_ntop(
                                    socket.AF_INET6, struct.pack('>L', x[j]))
                            except:
                                #failed ipv6 adn ipv4 conversion
                                print >> sys.stderr, "fastbit.py: IMPOSSIBLE TO FORMAT value: " + str(
                                    x[j]) + " TO IP ADDR"

                    #x[1]=socket.inet_ntop(socket.AF_INET,struct.pack('>L',x[1]))
                rows = res['values']
Exemplo n.º 2
0
def begin():
    # Imports for mako
    try:
        from mako.template import Template
        from mako.runtime import Context
        from mako.lookup import TemplateLookup
        from mako import exceptions
    except:
        ntop.printHTMLHeader('ntop Python Configuration Error', 1, 1)
        ntop.sendString("<b><center><font color=red>Please install <A HREF=http://www.makotemplates.org/>Mako</A> template engine</font><p></b><br>(1) 'sudo yum install python-setuptools' (on RedHat-like systems)<br>(2) 'sudo easy_install Mako'</font></center>")
        ntop.printHTMLFooter()
        return
    # Fix encoding
    #reload(sys)
    #sys.setdefaultencoding("latin1")
    
    templateFilename='ipPortQuery.tmpl'
    
    #fb_DB = '/tmp/'               #ntop.getPreference ("fastbitDBPath");    #default location of the fastbit DB
    databasePath = ntop.getPreference ("fastbit.DBPath");    #default location of the fastbit DB
        
    if databasePath is None or databasePath=='':
        ntop.printHTMLHeader('ntop Fastbit Configuration Error', 1, 1)
        ntop.sendString("<b><center><font color=red>Please set fastbit.DBPath ntop preferences from <i>Admin/Configure/Preferences</i> menu (es: fastbit.DBPath=/tmp/)</b></font></center>")
        ntop.printHTMLFooter()
        return
    
    #pathFastBit=os.path.join(databasePath,'fastbit'+os.path.sep)
        
    
    form = cgi.FieldStorage();                      #get from the url the parameter configfile that contains the 
                                                    #path+filename of the configfile to read
    fromAuto=form.getvalue('fromAuto')
    
    if fromAuto:
        #print>>sys.stderr, "AJAX REQUEST FOR PARTITION IN  "+databasePath+" "+fromAuto
        jsonString=expandFrom(fromAuto, os.path.join(databasePath, "") )
        ntop.sendHTTPHeader(12)
        ntop.sendString(jsonString)
        return
    
    
    documentRoot=os.getenv('DOCUMENT_ROOT', '.')
    
    selectArg='PROTOCOL,IPV4_SRC_ADDR,L4_SRC_PORT,IPV4_DST_ADDR,L4_DST_PORT,IN_BYTES,IN_PKTS'
    fromArg=form.getvalue('partition')
    
    ipSrc=form.getvalue('ipSrc')
    ipDst=form.getvalue('ipDst')
        
    
    portSrc=form.getvalue('portSrc')
    portDst=form.getvalue('portDst')
    
    limit = int(form.getvalue('limit', 100))
    
    if limit<0: 
        limit=0
    res=None                #variable to store the results of the query   
    ntop.printHTMLHeader('IP-Port Query', 1, 0)
    #regex to check passed parameters
    ipV4Type=re.compile(r'(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)')
    portType=re.compile(r'\d{1,5}')
    
    #pprint.pprint(ipV4Type.match(str(ipSrc)), sys.stderr)
    formatErrorString=''
    #without the partition do nothing
    if fromArg :
        whereArg='1=1'      #to avoid leaving the where clause empty
        if ipSrc:
            #print>>sys.stderr, 'ECCO '+str(ipV4Type.match(ipSrc))
            if ipV4Type.match(ipSrc):
                whereArg=whereArg+' AND IPV4_SRC_ADDR='+str(ipToNumber(ipSrc))
            else:
                formatErrorString=formatErrorString+'Source ip format invalid! ipv4 format required. '
        if portSrc:
            if portType.match(portSrc):
                whereArg=whereArg+' AND L4_SRC_PORT='+str(portSrc)
            else:
                formatErrorString=formatErrorString+'Source Port format invalid! Number required. '
        if ipDst:
            if ipV4Type.match(ipDst):
                whereArg=whereArg+' AND IPV4_DST_ADDR='+str(ipToNumber(ipDst))
            else:
                formatErrorString=formatErrorString+'Destination ip format invalid! ipv4 format required. '
        if portDst:
            if portType.match(portDst):
                whereArg=whereArg+' AND L4_DST_PORT='+str(portDst)
            else:
                formatErrorString=formatErrorString+'Destination Port format invalid! Number required. '
        try:
            #pipe = subprocess.Popen (['ntop.getPreference ("fastbit.fbquery")', "-c", selectArg, "-d", fromArg, "-q", whereArg, "-P", "-L", limit],
            #print>>sys.stderr, "Query passed: SELECT %s FROM %s WHERE %s LIMIT %i" %(selectArg,os.path.join(databasePath, fromArg),  whereArg, limit)
            if formatErrorString=='':
                res = fastbit.query(os.path.join(databasePath, fromArg), selectArg, whereArg, limit)
            else:
                print>>sys.stderr, 'ipPortQuery: ERROR '+formatErrorString
                ntop.sendString('<center><font color=red>%s</font></center>'%formatErrorString)
            #print>>sys.stderr, 'Number of records: %i' % len(res['values'])
        except:
            print>>sys.stderr, 'ERROR Executing query: '+("SELECT %s FROM %s WHERE %s LIMIT %i" %(selectArg,os.path.join(databasePath, fromArg),  whereArg, limit))
            res = {}
        if res is not None and 'columns' in res and 'values' in res:
            
            toFormat=getAddrCols(selectArg)         #get a list of addr column numbers
            
            for x in res['values']:
                x[0]=getNameByProto(x[0])           #format protocol number to text
                for j in toFormat:
                    #for every number in the list format as an IP ADDR
                    ipStr=numberToIp(x[j])
                    x[j]='<a href="/%s.html" class="tooltip">%s</a>' % (ipStr,ipStr)   #format ip number to string ip and create a link to ntop host page 
    '''else:
        print>>sys.stderr, 'ipPortQuery: ERROR partition required'
        ntop.sendString('<center><font color=red>Partition field required!</font></center>')'''
    #pprint.pprint(res, sys.stderr)
    #if res is not None:
    #    res['columns']=['Protocol', 'IP Source Addr', 'IP Dest. Addr', 'Source Port', 'Dest. Port', 'Bytes Rcvd', 'Packets Rcvd']
    
    try:
        basedir =  os.path.join(documentRoot,'python/templates')
        mylookup = TemplateLookup(directories=[basedir])
        myTemplate = mylookup.get_template(templateFilename)
        buf = StringIO()
        
        ctx = Context(buf, results=res)
        
        myTemplate.render_context(ctx)
        ntop.sendString(buf.getvalue())
    except:
        ntop.sendString(exceptions.html_error_template().render())
    
    ntop.printHTMLFooter()
Exemplo n.º 3
0
def begin():
    # Imports for mako
    try:
        from mako.template import Template
        from mako.runtime import Context
        from mako.lookup import TemplateLookup
        from mako import exceptions
    except:
        ntop.printHTMLHeader('ntop Python Configuration Error', 1, 1)
        ntop.sendString(
            "<b><center><font color=red>Please install <A HREF=http://www.makotemplates.org/>Mako</A> template engine</font><p></b><br>(1) 'sudo yum install python-setuptools' (on RedHat-like systems)<br>(2) 'sudo easy_install Mako'</font></center>"
        )
        ntop.printHTMLFooter()
        return
    # Fix encoding
    #reload(sys)
    #sys.setdefaultencoding("latin1")

    templateFilename = 'ipPortQuery.tmpl'

    #fb_DB = '/tmp/'               #ntop.getPreference ("fastbitDBPath");    #default location of the fastbit DB
    databasePath = ntop.getPreference("fastbit.DBPath")
    #default location of the fastbit DB

    if databasePath is None or databasePath == '':
        ntop.printHTMLHeader('ntop Fastbit Configuration Error', 1, 1)
        ntop.sendString(
            "<b><center><font color=red>Please set fastbit.DBPath ntop preferences from <i>Admin/Configure/Preferences</i> menu (es: fastbit.DBPath=/tmp/)</b></font></center>"
        )
        ntop.printHTMLFooter()
        return

    #pathFastBit=os.path.join(databasePath,'fastbit'+os.path.sep)

    form = cgi.FieldStorage()
    #get from the url the parameter configfile that contains the
    #path+filename of the configfile to read
    fromAuto = form.getvalue('fromAuto')

    if fromAuto:
        #print>>sys.stderr, "AJAX REQUEST FOR PARTITION IN  "+databasePath+" "+fromAuto
        jsonString = expandFrom(fromAuto, os.path.join(databasePath, ""))
        ntop.sendHTTPHeader(12)
        ntop.sendString(jsonString)
        return

    documentRoot = os.getenv('DOCUMENT_ROOT', '.')

    selectArg = 'PROTOCOL,IPV4_SRC_ADDR,L4_SRC_PORT,IPV4_DST_ADDR,L4_DST_PORT,IN_BYTES,IN_PKTS'
    fromArg = form.getvalue('partition')

    ipSrc = form.getvalue('ipSrc')
    ipDst = form.getvalue('ipDst')

    portSrc = form.getvalue('portSrc')
    portDst = form.getvalue('portDst')

    limit = int(form.getvalue('limit', 100))

    if limit < 0:
        limit = 0
    res = None  #variable to store the results of the query
    ntop.printHTMLHeader('IP-Port Query', 1, 0)
    #regex to check passed parameters
    ipV4Type = re.compile(
        r'(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)'
    )
    portType = re.compile(r'\d{1,5}')

    #pprint.pprint(ipV4Type.match(str(ipSrc)), sys.stderr)
    formatErrorString = ''
    #without the partition do nothing
    if fromArg:
        whereArg = '1=1'  #to avoid leaving the where clause empty
        if ipSrc:
            #print>>sys.stderr, 'ECCO '+str(ipV4Type.match(ipSrc))
            if ipV4Type.match(ipSrc):
                whereArg = whereArg + ' AND IPV4_SRC_ADDR=' + str(
                    ipToNumber(ipSrc))
            else:
                formatErrorString = formatErrorString + 'Source ip format invalid! ipv4 format required. '
        if portSrc:
            if portType.match(portSrc):
                whereArg = whereArg + ' AND L4_SRC_PORT=' + str(portSrc)
            else:
                formatErrorString = formatErrorString + 'Source Port format invalid! Number required. '
        if ipDst:
            if ipV4Type.match(ipDst):
                whereArg = whereArg + ' AND IPV4_DST_ADDR=' + str(
                    ipToNumber(ipDst))
            else:
                formatErrorString = formatErrorString + 'Destination ip format invalid! ipv4 format required. '
        if portDst:
            if portType.match(portDst):
                whereArg = whereArg + ' AND L4_DST_PORT=' + str(portDst)
            else:
                formatErrorString = formatErrorString + 'Destination Port format invalid! Number required. '
        try:
            #pipe = subprocess.Popen (['ntop.getPreference ("fastbit.fbquery")', "-c", selectArg, "-d", fromArg, "-q", whereArg, "-P", "-L", limit],
            #print>>sys.stderr, "Query passed: SELECT %s FROM %s WHERE %s LIMIT %i" %(selectArg,os.path.join(databasePath, fromArg),  whereArg, limit)
            if formatErrorString == '':
                res = fastbit.query(os.path.join(databasePath, fromArg),
                                    selectArg, whereArg, limit)
            else:
                print >> sys.stderr, 'ipPortQuery: ERROR ' + formatErrorString
                ntop.sendString('<center><font color=red>%s</font></center>' %
                                formatErrorString)
            #print>>sys.stderr, 'Number of records: %i' % len(res['values'])
        except:
            print >> sys.stderr, 'ERROR Executing query: ' + (
                "SELECT %s FROM %s WHERE %s LIMIT %i" %
                (selectArg, os.path.join(databasePath,
                                         fromArg), whereArg, limit))
            res = {}
        if res is not None and 'columns' in res and 'values' in res:

            toFormat = getAddrCols(
                selectArg)  #get a list of addr column numbers

            for x in res['values']:
                x[0] = getNameByProto(x[0])  #format protocol number to text
                for j in toFormat:
                    #for every number in the list format as an IP ADDR
                    ipStr = numberToIp(x[j])
                    x[j] = '<a href="/%s.html" class="tooltip">%s</a>' % (
                        ipStr, ipStr
                    )  #format ip number to string ip and create a link to ntop host page
    '''else:
        print>>sys.stderr, 'ipPortQuery: ERROR partition required'
        ntop.sendString('<center><font color=red>Partition field required!</font></center>')'''
    #pprint.pprint(res, sys.stderr)
    #if res is not None:
    #    res['columns']=['Protocol', 'IP Source Addr', 'IP Dest. Addr', 'Source Port', 'Dest. Port', 'Bytes Rcvd', 'Packets Rcvd']

    try:
        basedir = os.path.join(documentRoot, 'python/templates')
        mylookup = TemplateLookup(directories=[basedir])
        myTemplate = mylookup.get_template(templateFilename)
        buf = StringIO()

        ctx = Context(buf, results=res)

        myTemplate.render_context(ctx)
        ntop.sendString(buf.getvalue())
    except:
        ntop.sendString(exceptions.html_error_template().render())

    ntop.printHTMLFooter()