Advertisement
Guest User

Python Script used with NiFi

a guest
Jun 4th, 2015
520
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import datetime
  4. import getopt
  5. import httplib
  6. import sys
  7. import time
  8. import urllib
  9. import zlib
  10. import ast
  11. from collections import OrderedDict
  12. import json
  13. import os
  14.  
  15.  
  16. # defaults
  17.  
  18. ## URL stuff
  19. URL = "data.ip.net"
  20. PORT = "80"
  21. API_ENDPOINT = URL+":"+PORT
  22.  
  23.  
  24. ## POST body stuff
  25. APIKEY = "123456789abcdefghi"
  26. FORMAT = "json"
  27. METHOD = "delta"
  28. TS = ""
  29.  
  30.  
  31. ## other stuff
  32. timestamp = time.time()
  33. timestamp_extended = datetime.datetime.fromtimestamp(timestamp).strftime('%Y%m%d_%H%M')
  34. verbose = ""
  35. print_console = False
  36. morph_json = True
  37.  
  38. # /defaults
  39.  
  40. headers = {"Content-type": "application/x-www-form-urlencoded"}
  41.  
  42. options, remainder = getopt.getopt(sys.argv[1:], 'a:f:m:r:c:t:v:o:j:gspVh', [
  43. 'apikey=',
  44. 'format=',
  45. 'method=',
  46. 'ts=',
  47. 'version=',
  48. 'output=',
  49. ])
  50.  
  51. def usage():
  52. print ' -------------------------------------------------------------------------'
  53. print ' Typical usage:'
  54. print ' '
  55. print ' -a, --apikey api key'
  56. print ' -f, --format format [ csv, json, splunk ]'
  57. print ' -m, --method method [ full, delta, docs, heartbeat ]'
  58. print ' -t, --ts time slice [ UNIX timestamp ]'
  59. print ' -j, --morph-json turn format=json into a real json string [ singleline, multiline ]'
  60. print ' -p, --print print output to console'
  61. print ' -V, --verbose enable verbose output'
  62. print ' -h, --help this help'
  63.  
  64. print ' -------------------------------------------------------------------------'
  65. sys.exit(' ')
  66.  
  67. for opt, arg in options:
  68. if opt in ('-a', '--apikey'):
  69. APIKEY = arg
  70. elif opt in ('-f', '--format'):
  71. FORMAT = arg
  72. elif opt in ('-m', '--method'):
  73. METHOD = arg
  74. elif opt in ('-t', '--ts'):
  75. TS = arg
  76. elif opt in ('-v', '--version'):
  77. VERSION = arg
  78. elif opt in ('-j', '--morph-json'):
  79. morph_json = arg
  80. if FORMAT != "json":
  81. print "[!] json format required to use -j or --morph-json"
  82. sys.exit(1)
  83. elif opt in ('-V', '--verbose'):
  84. verbose = True
  85. elif opt in ('-h', '--help'):
  86. usage()
  87.  
  88. params = urllib.urlencode(
  89. {
  90. 'apikey': APIKEY,
  91. 'method': METHOD,
  92. 'ts' : TS,
  93. 'v': VERSION,
  94. }
  95. )
  96.  
  97. if verbose:
  98. print 'posting data to :', API_ENDPOINT
  99. for k, v in headers.items():
  100. print 'using headers :', k, v
  101. print 'using post string :', params
  102.  
  103. conn = httplib.HTTPConnection(API_ENDPOINT)
  104. conn.request("POST", "/", params, headers)
  105.  
  106. response = conn.getresponse()
  107.  
  108. if morph_json:
  109.  
  110. data = response.read().splitlines()
  111.  
  112. last_line = data.pop(-1)
  113.  
  114. if last_line != "Version:2":
  115. print "something went wrong"
  116. sys.exit(1)
  117.  
  118. header = data.pop(0)
  119. header = ast.literal_eval(header)
  120. entry_header = header[-1][0]
  121. records = []
  122.  
  123. for line in data:
  124. line = line.replace("null", "\"\"")
  125. line = ast.literal_eval(line)
  126. out = OrderedDict(zip(header, line[0:-1]))
  127. entries = []
  128. for entry in line[-1]:
  129. entries.append(dict(zip(entry_header, entry)))
  130.  
  131. out["entries"] = entries
  132. records.append(out)
  133. #print out
  134. sys.stdout.write(out)
  135.  
  136. conn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement