示例#1
0
    def controlT():
        #global rrdFilesPath
        ntopSpoolPath = ntop.getSpoolPath()
        nameFileConfig = 'rrdAlarmConfig.txt'
        tempFileName = 'rrdAlarmStart.tmp'

        configuration = None
        TWODECIMAL = decimal.Decimal(10)**-2
        timeStart = time.time()

        alarmsFired = 0
        checkedFiles = 0
        fmt = '%a, %d %b %Y %H:%M:%S'  #format of the time showed
        form = cgi.FieldStorage()
        #get the parameter passed via the url
        noHTML = bool(form.getvalue('noHTML'))
        configFile = form.getvalue('configFile')
        if configFile and len(configFile) > 0:
            nameFileConfig = str(configFile)
        try:
            tempFile = open(os.path.join(ntopSpoolPath, tempFileName), 'r')

            configuration = pickle.load(tempFile)
            tempFile.close()

            if configuration and (
                    timeStart < float(configuration['timeStart']) + float(60)):
                ntop.sendHTTPHeader(1)
                ntop.printHTMLHeader('RRD Alarm Called too early!', 1, 0)
                ntop.sendString(
                    "Wait at least a minute. Last Time started: %s" %
                    time.strftime(fmt,
                                  time.localtime(configuration['timeStart'])))
                ntop.printHTMLFooter()
                return 0  #exit because the script was started less than one minute ago

            else:
                configuration['timeStart'] = timeStart

        except IOError:  #the tempFile does not exist or some other problem
            print >> sys.stderr, 'RRDAlarm: IOError while accessing tempfile ' + tempFileName
            configuration = createNewConfiguration(
                rrdFilesPath, os.path.join(ntopSpoolPath, nameFileConfig),
                timeStart)

        except pickle.PickleError, pickle.UnpicklingError:
            print >> sys.stderr, "RRDAlarm: Problems during the UnPickling load, tempFile Delete..."
            os.remove(os.path.join(ntopSpoolPath, tempFileName))
            return -1
示例#2
0
    def controlT():
        # global rrdFilesPath
        ntopSpoolPath = ntop.getSpoolPath()
        nameFileConfig = "rrdAlarmConfig.txt"
        tempFileName = "rrdAlarmStart.tmp"

        configuration = None
        TWODECIMAL = decimal.Decimal(10) ** -2
        timeStart = time.time()

        alarmsFired = 0
        checkedFiles = 0
        fmt = "%a, %d %b %Y %H:%M:%S"  # format of the time showed
        form = cgi.FieldStorage()
        # get the parameter passed via the url
        noHTML = bool(form.getvalue("noHTML"))
        configFile = form.getvalue("configFile")
        if configFile and len(configFile) > 0:
            nameFileConfig = str(configFile)
        try:
            tempFile = open(os.path.join(ntopSpoolPath, tempFileName), "r")

            configuration = pickle.load(tempFile)
            tempFile.close()

            if configuration and (timeStart < float(configuration["timeStart"]) + float(60)):
                ntop.sendHTTPHeader(1)
                ntop.printHTMLHeader("RRD Alarm Called too early!", 1, 0)
                ntop.sendString(
                    "Wait at least a minute. Last Time started: %s"
                    % time.strftime(fmt, time.localtime(configuration["timeStart"]))
                )
                ntop.printHTMLFooter()
                return 0  # exit because the script was started less than one minute ago

            else:
                configuration["timeStart"] = timeStart

        except IOError:  # the tempFile does not exist or some other problem
            print >>sys.stderr, "RRDAlarm: IOError while accessing tempfile " + tempFileName
            configuration = createNewConfiguration(rrdFilesPath, os.path.join(ntopSpoolPath, nameFileConfig), timeStart)

        except pickle.PickleError, pickle.UnpicklingError:
            print >>sys.stderr, "RRDAlarm: Problems during the UnPickling load, tempFile Delete..."
            os.remove(os.path.join(ntopSpoolPath, tempFileName))
            return -1
示例#3
0
def begin():

    templateFilename = 'rrdAlarmConfigurator.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")

    rows = []
    pathRRDFiles = os.path.join(ntop.getDBPath(), 'rrd/')
    nameFileConfig = 'rrdAlarmConfig.txt'  #default nameFileConfig
    pathTempFile = ntop.getSpoolPath() + os.sep

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

    help = form.getvalue('help')
    documentRoot = os.getenv('DOCUMENT_ROOT', '.')

    if jsonPathRRD:
        #a request from the autocomplete script, return a json string with the matching files names
        ntop.sendHTTPHeader(1)
        ntop.sendString(jsonListFileInPath(jsonPathRRD, pathRRDFiles))
        return
    elif help == 'true':
        #show help page
        templateFilename = 'rrdAlarmConfiguratorHelp.tmpl'
        ntop.printHTMLHeader('RRD Alarm Configurator Help', 1, 0)
    else:
        #normal operation
        requestFileConfig = form.getvalue('configFile')
        if requestFileConfig is not None:
            nameFileConfig = requestFileConfig

        #get all the scripts in the scripts directory
        listScripts = readScriptsDir(
            os.path.join(documentRoot, 'python/rrdalarm/scripts/'))

        ntop.printHTMLHeader('RRD Alarm Configurator', 1, 0)

        file_name = os.path.join(pathTempFile, nameFileConfig)

        try:
            configFile = open(file_name, 'rt')

            for line in configFile:
                line = line.rstrip()  #drop the \n at the end
                if len(line) > 0 and line[0] != '#':
                    rows.append(line.split('\t'))

            configFile.close()

        except IOError:
            try:
                open(file_name, 'w').close()  # Create an empty file if missing
            except:
                pass
            print >> sys.stderr, "RRDAlarm: empty configFile created " + file_name
        except:
            print >> sys.stderr, "RRDAlarm: Error reading configFile " + os.path.join(
                pathTempFile, nameFileConfig)
            raise
        #the elaboration will continue but no data will be displayed.

        #if requestFileConfig is not None:                 #if the nameFileConfig was specified by user show error
        #try:
        #    open(os.path.join(pathTempFile,nameFileConfig), 'w')
        #except:
        #    raise
        #else:
        #nameFileConfig='rrdAlarmConfig.txt'
        #ntop.sendString(exceptions.html_error_template().render())

    try:
        #pprint.pprint(listAllDirs(pathRRDFiles+'rrd/'), sys.stderr)
        basedir = os.path.join(documentRoot, 'python/templates')
        mylookup = TemplateLookup(directories=[basedir])
        myTemplate = mylookup.get_template(templateFilename)
        buf = StringIO()
        ctx = None
        if (help == 'true'):  #show help page
            ctx = Context(buf)
        else:
            ctx = Context(buf,
                          configRows=rows,
                          tempFilePath=pathTempFile,
                          nameFileConfig=nameFileConfig,
                          listScripts=listScripts,
                          pathRRDFiles=pathRRDFiles
                          )  #, rrdDirs=listAllDirs(pathRRDFiles+'rrd/')

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

    ntop.printHTMLFooter()
示例#4
0
def begin():
    
    templateFilename='rrdAlarmConfigurator.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")
    
    
    rows=[]
    pathRRDFiles=os.path.join(ntop.getDBPath(),'rrd/')
    nameFileConfig='rrdAlarmConfig.txt'              #default nameFileConfig
    pathTempFile=ntop.getSpoolPath()+os.sep
    
    
    form = cgi.FieldStorage();                      #get  from the url the parameter configfile that contains the 
                                                    #path+filename of the configfile to read
    jsonPathRRD=form.getvalue('pathRRDS')
    
    
    help=form.getvalue('help')
    documentRoot=os.getenv('DOCUMENT_ROOT', '.')
    
    if jsonPathRRD:
        #a request from the autocomplete script, return a json string with the matching files names
        ntop.sendHTTPHeader(1)
        ntop.sendString(jsonListFileInPath(jsonPathRRD, pathRRDFiles))
        return
    elif help == 'true':
        #show help page
        templateFilename='rrdAlarmConfiguratorHelp.tmpl'
        ntop.printHTMLHeader('RRD Alarm Configurator Help', 1, 0)
    else:
        #normal operation
        requestFileConfig=form.getvalue('configFile')
        if requestFileConfig is not None:
            nameFileConfig=requestFileConfig
        
        #get all the scripts in the scripts directory
        listScripts=readScriptsDir(os.path.join(documentRoot,'python/rrdalarm/scripts/'))
        
        ntop.printHTMLHeader('RRD Alarm Configurator', 1, 0)
        
        file_name = os.path.join(pathTempFile,nameFileConfig)
        
        try:    
            configFile= open(file_name, 'rt')
            
            for line in configFile:
                line=line.rstrip()                      #drop the \n at the end
                if len(line) >0 and line[0] != '#':
                    rows.append(line.split('\t'))
            
            configFile.close()
            
        except IOError:
            try:
                open(file_name, 'w').close()  # Create an empty file if missing
            except:
                pass
            print>>sys.stderr, "RRDAlarm: empty configFile created "+file_name
        except:
            print>>sys.stderr, "RRDAlarm: Error reading configFile "+os.path.join(pathTempFile,nameFileConfig)
            raise
        #the elaboration will continue but no data will be displayed.
        
            #if requestFileConfig is not None:                 #if the nameFileConfig was specified by user show error
            #try:
            #    open(os.path.join(pathTempFile,nameFileConfig), 'w')
            #except:
            #    raise
            #else:
            #nameFileConfig='rrdAlarmConfig.txt'
            #ntop.sendString(exceptions.html_error_template().render())
    
    try:
        #pprint.pprint(listAllDirs(pathRRDFiles+'rrd/'), sys.stderr)
        basedir =  os.path.join(documentRoot,'python/templates')
        mylookup = TemplateLookup(directories=[basedir])
        myTemplate = mylookup.get_template(templateFilename)
        buf = StringIO()
        ctx=None
        if(help =='true'):          #show help page
            ctx = Context(buf)
        else:
            ctx = Context(buf, configRows=rows,tempFilePath=pathTempFile, nameFileConfig=nameFileConfig,listScripts=listScripts,  pathRRDFiles=pathRRDFiles)  #, rrdDirs=listAllDirs(pathRRDFiles+'rrd/')
        
        myTemplate.render_context(ctx)
        ntop.sendString(buf.getvalue())
    except:
        ntop.sendString(exceptions.html_error_template().render())
    
    ntop.printHTMLFooter()
示例#5
0
    returnValue=''
    for x in listVal:
        returnValue=returnValue+str(x)+separator
    
    return returnValue[0:-1]+'\n'

'''HERE STARTS THE SCRIPT'''
#import pprint
#if os.getenv('REQUEST_METHOD', 'GET') == 'POST':                          #the get method is discarded. only POST
    
cgitb.enable()

form = cgi.FieldStorage()

form.type='application/jsonrequest'
pathConfigFile=ntop.getSpoolPath()+os.sep
nameFileConfig='rrdAlarmConfig.txt'              #default nameFileConfig
#print>>sys.stderr , nameFileConfig
jsonData=form.getvalue('jsonString', '{"rows":None}')                 #get the data from the body of the post request
userConfigFile=form.getfirst('configFile', None)
#pprint.pprint(form.getvalue('configFile', None), sys.stderr)
#print>>sys.stderr, userConfigFile
if userConfigFile is not None:
    
    nameFileConfig=checkFileName(str(userConfigFile))

#pprint.pprint(nameFileConfig, sys.stderr)
#call ntop method to get post data. parse the json variable and store
configData=json.loads(jsonData,'latin1')

rows=configData['rows']
示例#6
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']
示例#7
0
    for x in listVal:
        returnValue = returnValue + str(x) + separator

    return returnValue[0:-1] + '\n'


'''HERE STARTS THE SCRIPT'''
#import pprint
#if os.getenv('REQUEST_METHOD', 'GET') == 'POST':                          #the get method is discarded. only POST

cgitb.enable()

form = cgi.FieldStorage()

form.type = 'application/jsonrequest'
pathConfigFile = ntop.getSpoolPath() + os.sep
nameFileConfig = 'rrdAlarmConfig.txt'  #default nameFileConfig
#print>>sys.stderr , nameFileConfig
jsonData = form.getvalue(
    'jsonString',
    '{"rows":None}')  #get the data from the body of the post request
userConfigFile = form.getfirst('configFile', None)
#pprint.pprint(form.getvalue('configFile', None), sys.stderr)
#print>>sys.stderr, userConfigFile
if userConfigFile is not None:

    nameFileConfig = checkFileName(str(userConfigFile))

#pprint.pprint(nameFileConfig, sys.stderr)
#call ntop method to get post data. parse the json variable and store
configData = json.loads(jsonData, 'latin1')