Guest User

Untitled

a guest
Jun 22nd, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. import csv
  2. import sys
  3. import splunk.Intersplunk
  4. import string
  5. import re
  6.  
  7. ### Used for getting the
  8. # Get the regular expression string format
  9. def _get_re_compile_str(str_list):
  10. new_list = []
  11. for item in str_list:
  12. if "*" not in item:
  13. new_list.append("^" + item + "$")
  14. else:
  15. if not item.startswith('*'):
  16. item = '^' + item
  17. else:
  18. item = item[1:]
  19. if not item.endswith('*'):
  20. item = item + '$'
  21. new_list.append(item.replace('*','.*'))
  22. return new_list
  23.  
  24. # Get all fields for a specific row matching the regular expression string format given.
  25. def _get_fields(str_regex_list, keys):
  26. fields = []
  27. for str_regex in str_regex_list:
  28. regex = re.compile(str_regex)
  29. fields.extend([string for string in keys if re.match(regex, string)])
  30. return fields
  31.  
  32. # Fetch the results
  33. results = splunk.Intersplunk.readResults(None, None, True)
  34.  
  35. # args is a list of parameters which will look like ['argument1', 'field*']
  36. # kwargs is dictionary which will look like {'param1':'value1', 'param2': 'value2'}
  37. args, kwargs = splunk.Intersplunk.getKeywordsAndOptions()
  38.  
  39. str_regex_list = _get_re_compile_str(args)
  40.  
  41. # Run through each line and add a new field called new_field with 'static value' for its value
  42. for res in results:
  43. fields = _get_fields(str_regex_list, res.keys())
  44. res['new_field'] = 'static value'
  45.  
  46. # Send the result back
  47. splunk.Intersplunk.outputResults(results)
Add Comment
Please, Sign In to add comment