Advertisement
Guest User

SplunkCustomJSON

a guest
Mar 18th, 2013
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.02 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.CustomJSONResults')
  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 = 'CustomJSONResultsWithException'
  33.  
  34.            
  35. class CustomJSONResults(module.ModuleHandler):
  36.    
  37.     def generateResults(self, host_app, client_app, sid, count=1000,
  38.             earliest_time=None, latest_time=None, field_list=None,
  39.             offset=0, max_lines=None, reverse_order=0, entity_name='results',
  40.             postprocess=None, display_row_numbers='True', show_preview='0', mark_interactive=None,
  41.             sortField=None, sortDir=None):
  42.    
  43.         # assert input
  44.         if not sid:
  45.             raise Exception('CustomJSONResults.generateResults - sid not passed!')
  46.  
  47.         try:
  48.             job = splunk.search.JobLite(sid)
  49.         except Exception, e:
  50.             return self.generateErrorMessage(_('Job not available'))
  51.        
  52.         job.setFetchOption(output_time_format=i18n.ISO8609_MICROTIME)
  53.  
  54.        # pass in any field list
  55.         if (field_list) :
  56.             job.setFetchOption(fieldList=field_list, show_empty_fields=False)
  57.        
  58.         if postprocess:
  59.             job.setFetchOption(search=postprocess)
  60.  
  61.         if splunk.util.normalizeBoolean(show_preview) and entity_name == 'results':
  62.             entity_name = 'results_preview'
  63.  
  64.         rs = job.getResults(entity_name, offset, count)
  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.         for row in dataset:
  75.         rowData = {}
  76.                 for fieldName in fieldNames:
  77.             try:
  78.                 rowData[fieldName] = str(row[fieldName][0].value)
  79.             except:
  80.                 pass
  81.  
  82.         outputJSON[row['sectionCode'][0].value] = rowData
  83.        
  84.         cherrypy.response.headers['Content-Type'] = 'text/json'
  85.         return json.dumps(outputJSON, sort_keys=True)
  86.  
  87.     def generateErrorMessage(self, errorString):
  88.         return '%s%s%s' % (su.escape(errorString), CSS_CLASS_DELIMITER, EXCEPTION_CSS_CLASS)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement