4
@author: Gianluca Medici
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()
23
# Import modules for CGI handling
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':[]}
31
listFile = glob.glob(os.path.join(ntopSuffix,path)+'*') #expand the * and ? into a list of files
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)
38
if os.path.isdir(file):
41
#print>>sys.stderr, file
42
file=file[len(ntopSuffix):]
43
jsonList['results'].append({'id': i , 'value':file})
44
return json.dumps(jsonList)
46
def listAllDirs(dirPath):
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)
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
62
def readScriptsDir(pathScriptsDir):
67
directoryList = glob.glob1(pathScriptsDir, '*')
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
83
templateFilename='rrdAlarmConfigurator.tmpl'
86
from mako.template import Template
87
from mako.runtime import Context
88
from mako.lookup import TemplateLookup
89
from mako import exceptions
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()
98
#sys.setdefaultencoding("latin1")
102
pathRRDFiles=os.path.join(ntop.getDBPath(),'rrd/')
103
nameFileConfig='rrdAlarmConfig.txt' #default nameFileConfig
104
pathTempFile=ntop.getSpoolPath()+os.sep
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')
112
help=form.getvalue('help')
113
documentRoot=os.getenv('DOCUMENT_ROOT', '.')
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))
122
templateFilename='rrdAlarmConfiguratorHelp.tmpl'
123
ntop.printHTMLHeader('RRD Alarm Configurator Help', 1, 0)
126
requestFileConfig=form.getvalue('configFile')
127
if requestFileConfig is not None:
128
nameFileConfig=requestFileConfig
130
#get all the scripts in the scripts directory
131
listScripts=readScriptsDir(os.path.join(documentRoot,'python/rrdalarm/scripts/'))
133
ntop.printHTMLHeader('RRD Alarm Configurator', 1, 0)
135
file_name = os.path.join(pathTempFile,nameFileConfig)
138
configFile= open(file_name, 'rt')
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'))
149
open(file_name, 'w').close() # Create an empty file if missing
152
print>>sys.stderr, "RRDAlarm: empty configFile created "+file_name
154
print>>sys.stderr, "RRDAlarm: Error reading configFile "+os.path.join(pathTempFile,nameFileConfig)
156
#the elaboration will continue but no data will be displayed.
158
#if requestFileConfig is not None: #if the nameFileConfig was specified by user show error
160
# open(os.path.join(pathTempFile,nameFileConfig), 'w')
164
#nameFileConfig='rrdAlarmConfig.txt'
165
#ntop.sendString(exceptions.html_error_template().render())
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)
174
if(help =='true'): #show help page
177
ctx = Context(buf, configRows=rows,tempFilePath=pathTempFile, nameFileConfig=nameFileConfig,listScripts=listScripts, pathRRDFiles=pathRRDFiles) #, rrdDirs=listAllDirs(pathRRDFiles+'rrd/')
179
myTemplate.render_context(ctx)
180
ntop.sendString(buf.getvalue())
182
ntop.sendString(exceptions.html_error_template().render())
184
ntop.printHTMLFooter()
187
'''Here starts the script'''
189
if exceptions_so_far == 0: