Advertisement
Guest User

Untitled

a guest
Mar 19th, 2013
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.00 KB | None | 0 0
  1. #
  2. # Splunk UI module python renderer
  3. # This module is imported by the module loader (lib.module.ModuleMapper) into
  4. # the splunk.appserver.mrsparkle.controllers.module.* namespace.
  5. #
  6.  
  7.  
  8. # required imports
  9. import cherrypy, datetime
  10. import controllers.module as module
  11.  
  12. # common imports
  13. import xml.sax.saxutils as su
  14. import splunk, splunk.search, splunk.util, splunk.entity
  15. import lib.util as util
  16. import lib.i18n as i18n
  17.  
  18. import json
  19. from splunk.appserver.mrsparkle.lib import jsonresponse
  20.  
  21. # logging setup
  22. import logging
  23. logger = logging.getLogger('splunk.appserver.controllers.module.GeneratorVisualisation')
  24.  
  25. # define standard time field name        
  26. TIME_FIELD = '_time'
  27. RAW_FIELD = '_raw'
  28.  
  29. # define a delimiter that can be used to pass content and a css class down to the module via generateResults
  30. # and css class name to add when the module is displaying an exception
  31. CSS_CLASS_DELIMITER = '//!-!//'
  32. EXCEPTION_CSS_CLASS = 'GeneratorVisualisationWithException'
  33.  
  34.            
  35. class GeneratorVisualisation(module.ModuleHandler):
  36.    
  37.     def generateResults(self, host_app, client_app, sid, entity_name='results',
  38.             postprocess=None, show_preview='0'):
  39.    
  40.         # assert input
  41.         if not sid:
  42.             raise Exception('GeneratorVisualisation.generateResults - sid not passed!')
  43.  
  44.         try:
  45.             job = splunk.search.JobLite(sid)
  46.         except Exception, e:
  47.             return self.generateErrorMessage(_('Job not available'))
  48.        
  49.         job.setFetchOption(output_time_format=i18n.ISO8609_MICROTIME)
  50.  
  51.        # pass in any field list
  52. #        if (field_list) :
  53. #            job.setFetchOption(fieldList=field_list, show_empty_fields=False)
  54.        
  55.         if postprocess:
  56.             job.setFetchOption(search=postprocess)
  57.  
  58.   #      if splunk.util.normalizeBoolean(show_preview) and entity_name == 'results':
  59.   #          entity_name = 'results_preview'
  60.  
  61.         try:
  62.             rs = job.getResults(entity_name, 0, 1)
  63.         except Exception, e:
  64.             return self.generateErrorMessage(_('Results not available'))
  65.  
  66.         if rs == None:
  67.             return _('<p class="resultStatusMessage">The job appears to have expired or has been canceled. Splunk could not retrieve data for this search.</p>')            
  68.  
  69.         fieldNames = [x for x in rs.fieldOrder() if (not x.startswith('_') or x in (TIME_FIELD, RAW_FIELD) )]
  70.  
  71.         dataset = rs.results()
  72.  
  73.         outputJSON = {}
  74.    
  75.     rowCount = 0
  76.         for row in dataset:
  77.         rowData = {}
  78.                 for fieldName in fieldNames:
  79.             try:
  80.                 rowData[fieldName] = str(row[fieldName][0].value)
  81.             except:
  82.                 pass
  83.  
  84.         outputJSON[row['sectionCode'][0].value] = rowData
  85.        
  86.         cherrypy.response.headers['Content-Type'] = 'text/json'
  87.  
  88.     outputJSON['testCounterDataset'] = len(dataset)
  89.  
  90.         return json.dumps(outputJSON, sort_keys=True)
  91.  
  92.     def generateErrorMessage(self, errorString):
  93.         return '%s%s%s' % (su.escape(errorString), CSS_CLASS_DELIMITER, EXCEPTION_CSS_CLASS)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement