~ubuntu-branches/ubuntu/natty/ntop/natty

« back to all changes in this revision

Viewing changes to python/rrdalarm/config.py

  • Committer: Bazaar Package Importer
  • Author(s): Ludovico Cavedon, Jordan Metzmeier, Ludovico Cavedon
  • Date: 2010-12-15 20:06:19 UTC
  • mfrom: (5.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20101215200619-0ojz3iak95ihibun
Tags: 3:4.0.3+dfsg1-1
[ Jordan Metzmeier ]
* New upstream release (Closes: #522042)
* Move data files to /usr/share/ntop (Closes: #595450).
* Package architecture independent data in a separate ntop-data package.
* Use debhelper 7.
* Update Standards-Version to 3.9.1.
* Depend on python-mako.
* Do not include ntop.txt in binary packages as it is a copy of the man
  page.
* Do not include NEWS, as it is outdated.
* Switch to package source version 3.0 (quilt).
* Add password creation to debconf
* Changed init script to fix localization problems (thanks to Alejandro
  Varas <alej0varas@gmail.com>, LP: #257466)
* Remove manual update-rc.d calls from postrm and postinst. debhelper adds
  this for us.
* Add pre-depends on adduser for postinst script.
* Fix errors in the manpages: fix-manpage-errors.patch.
* Added fixes for matching active interfaces.
* Added a watch file.

[ Ludovico Cavedon ]
* Remove direct changes to upstream tree, and move them into specific patch
  files:
  - fix-manpage-errors.patch: fix typos in ntop.8.
  - dot-path.patch: fix path of /usr/bin/dot executable
* Add patches:
  - reduce-autogen-purged-files.patch: prevent agutogen.sh from reamoving
  too many files during cleanup.
  - Add build-without-ntop-darwin.patch, to fix compilation without
  ntop_darwin.c.
* No longer add faq.html, as it is not distributed in the upstream tarball.
* Use ${source:Version} in control file. Have ntop-data recommend
  ntop.
* Rename dirs to ntop.dirs and keep only empty directories that need
  to be created.
* Remove var/lib from ntop.install file, as it is empty (keeping it in
  ntop.dirs).
* Update po files.
* Breaks and Replaces instead of Conflitcs for ntop-data.
* Use a longer package description.
* Remove useless configure options from debian/rules.
* Move private shared libraries libraries in /usr/lib/ntop.
* Add change-plugin-dir.patch for adjusting plugin directory.
* Remove development files.
* Use system library for MochiKit.js.
* Rewrite DEP5 copyright file.
* Repackage upstream tarball in order to remove non-DFSG-compliant code. Add
  get-orig-source.sh script and get-orig-source target in debian/rules.
* Add explanation to README.Debian why geolocation is no longer working.
* Add avoid-copy-maxmind-db.patch to prevent copying of Geo*.dat
  files.
* Remove old unused patches.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
'''
 
2
Created on 16/gen/2010
 
3
 
 
4
@author: Gianluca Medici
 
5
'''
 
6
import ntop
 
7
import host
 
8
import os, os.path
 
9
import sys
 
10
#import pprint
 
11
import glob
 
12
 
 
13
exceptions_so_far = 0
 
14
 
 
15
try:
 
16
    import json
 
17
except:
 
18
    ntop.printHTMLHeader('ntop Python Configuration Error', 1, 1)
 
19
    ntop.sendString("<b><center><font color=red>Please install JSON support in python</font><p></b><br>E.g. 'sudo apt-get install python-json' (on Debian-like systems)</font></center>")
 
20
    ntop.printHTMLFooter()    
 
21
    exceptions_so_far=1
 
22
 
 
23
# Import modules for CGI handling
 
24
import cgi, cgitb
 
25
 
 
26
from StringIO import StringIO
 
27
#return a json string of filenames from the path expanded with a *, if the result is just one and is a directory it continues the search inside it 
 
28
def jsonListFileInPath(path, ntopSuffix):
 
29
    jsonList={'results':[]}
 
30
    
 
31
    listFile = glob.glob(os.path.join(ntopSuffix,path)+'*')   #expand the * and ? into a list of files
 
32
    i=1
 
33
    if len(listFile)==1 and os.path.isdir(listFile[0]):
 
34
        listFile=listFile+glob.glob(os.path.join(listFile[0],'*'))
 
35
        #pprint.pprint(listFile, sys.stderr)
 
36
    
 
37
    for file in listFile:
 
38
        if os.path.isdir(file):
 
39
           file=file+os.sep
 
40
        
 
41
        #print>>sys.stderr, file
 
42
        file=file[len(ntopSuffix):]
 
43
        jsonList['results'].append({'id': i , 'value':file})
 
44
    return json.dumps(jsonList)
 
45
        
 
46
def listAllDirs(dirPath):
 
47
    arr=[]
 
48
    #pprint.pprint(os.listdir(dirPath),sys.stderr) 
 
49
    for file in os.listdir(dirPath):
 
50
        newPath=os.path.join(dirPath,file)
 
51
        if os.path.isdir(newPath) : 
 
52
            #return [newPath]+listAllDirs(newPath)
 
53
            #print>>sys.stderr, file
 
54
            arr.append(newPath+os.sep);
 
55
            arr=arr+listAllDirs(newPath+os.sep)
 
56
    return arr
 
57
    
 
58
''' Function that provide a list of names of the scripts .py present in the scripts directory
 
59
    the list returned does not contain the name of this module as well as the name of the __init__
 
60
    module. The names provided in the returned list will be lacking of the trailing .py
 
61
'''
 
62
def readScriptsDir(pathScriptsDir):
 
63
    import sys
 
64
    import glob
 
65
    directoryList=[]
 
66
    try:
 
67
        directoryList = glob.glob1(pathScriptsDir, '*')
 
68
    except:
 
69
        raise
 
70
        return ['None']
 
71
    
 
72
    nameScriptList=['None']
 
73
    for scriptName in directoryList:
 
74
        if os.access(os.path.join(pathScriptsDir,scriptName), os.X_OK) or scriptName[-3:]=='.py':
 
75
            justName=scriptName.partition('.')[0]
 
76
            if justName and len(justName)>0:
 
77
                nameScriptList.append(justName)             #remove the last 3 characters .py from the name of the script
 
78
    
 
79
    return nameScriptList
 
80
 
 
81
def begin():
 
82
    
 
83
    templateFilename='rrdAlarmConfigurator.tmpl'
 
84
    # Imports for mako
 
85
    try:
 
86
        from mako.template import Template
 
87
        from mako.runtime import Context
 
88
        from mako.lookup import TemplateLookup
 
89
        from mako import exceptions
 
90
    except:
 
91
        ntop.printHTMLHeader('ntop Python Configuration Error',1,0)
 
92
        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>")
 
93
        ntop.printHTMLFooter()    
 
94
        return
 
95
    
 
96
    # Fix encoding
 
97
    #reload(sys)
 
98
    #sys.setdefaultencoding("latin1")
 
99
    
 
100
    
 
101
    rows=[]
 
102
    pathRRDFiles=os.path.join(ntop.getDBPath(),'rrd/')
 
103
    nameFileConfig='rrdAlarmConfig.txt'              #default nameFileConfig
 
104
    pathTempFile=ntop.getSpoolPath()+os.sep
 
105
    
 
106
    
 
107
    form = cgi.FieldStorage();                      #get  from the url the parameter configfile that contains the 
 
108
                                                    #path+filename of the configfile to read
 
109
    jsonPathRRD=form.getvalue('pathRRDS')
 
110
    
 
111
    
 
112
    help=form.getvalue('help')
 
113
    documentRoot=os.getenv('DOCUMENT_ROOT', '.')
 
114
    
 
115
    if jsonPathRRD:
 
116
        #a request from the autocomplete script, return a json string with the matching files names
 
117
        ntop.sendHTTPHeader(1)
 
118
        ntop.sendString(jsonListFileInPath(jsonPathRRD, pathRRDFiles))
 
119
        return
 
120
    elif help == 'true':
 
121
        #show help page
 
122
        templateFilename='rrdAlarmConfiguratorHelp.tmpl'
 
123
        ntop.printHTMLHeader('RRD Alarm Configurator Help', 1, 0)
 
124
    else:
 
125
        #normal operation
 
126
        requestFileConfig=form.getvalue('configFile')
 
127
        if requestFileConfig is not None:
 
128
            nameFileConfig=requestFileConfig
 
129
        
 
130
        #get all the scripts in the scripts directory
 
131
        listScripts=readScriptsDir(os.path.join(documentRoot,'python/rrdalarm/scripts/'))
 
132
        
 
133
        ntop.printHTMLHeader('RRD Alarm Configurator', 1, 0)
 
134
        
 
135
        file_name = os.path.join(pathTempFile,nameFileConfig)
 
136
        
 
137
        try:    
 
138
            configFile= open(file_name, 'rt')
 
139
            
 
140
            for line in configFile:
 
141
                line=line.rstrip()                      #drop the \n at the end
 
142
                if len(line) >0 and line[0] != '#':
 
143
                    rows.append(line.split('\t'))
 
144
            
 
145
            configFile.close()
 
146
            
 
147
        except IOError:
 
148
            try:
 
149
                open(file_name, 'w').close()  # Create an empty file if missing
 
150
            except:
 
151
                pass
 
152
            print>>sys.stderr, "RRDAlarm: empty configFile created "+file_name
 
153
        except:
 
154
            print>>sys.stderr, "RRDAlarm: Error reading configFile "+os.path.join(pathTempFile,nameFileConfig)
 
155
            raise
 
156
        #the elaboration will continue but no data will be displayed.
 
157
        
 
158
            #if requestFileConfig is not None:                 #if the nameFileConfig was specified by user show error
 
159
            #try:
 
160
            #    open(os.path.join(pathTempFile,nameFileConfig), 'w')
 
161
            #except:
 
162
            #    raise
 
163
            #else:
 
164
            #nameFileConfig='rrdAlarmConfig.txt'
 
165
            #ntop.sendString(exceptions.html_error_template().render())
 
166
    
 
167
    try:
 
168
        #pprint.pprint(listAllDirs(pathRRDFiles+'rrd/'), sys.stderr)
 
169
        basedir =  os.path.join(documentRoot,'python/templates')
 
170
        mylookup = TemplateLookup(directories=[basedir])
 
171
        myTemplate = mylookup.get_template(templateFilename)
 
172
        buf = StringIO()
 
173
        ctx=None
 
174
        if(help =='true'):          #show help page
 
175
            ctx = Context(buf)
 
176
        else:
 
177
            ctx = Context(buf, configRows=rows,tempFilePath=pathTempFile, nameFileConfig=nameFileConfig,listScripts=listScripts,  pathRRDFiles=pathRRDFiles)  #, rrdDirs=listAllDirs(pathRRDFiles+'rrd/')
 
178
        
 
179
        myTemplate.render_context(ctx)
 
180
        ntop.sendString(buf.getvalue())
 
181
    except:
 
182
        ntop.sendString(exceptions.html_error_template().render())
 
183
    
 
184
    ntop.printHTMLFooter()
 
185
 
 
186
 
 
187
'''Here starts the script'''
 
188
 
 
189
if exceptions_so_far == 0:
 
190
    begin()