Advertisement
Guest User

Untitled

a guest
Jul 18th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.58 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. from subprocess import Popen,PIPE
  4. from socket import gethostname
  5.  
  6. USER="myuser"
  7. PASS="mypassword"
  8. DB="mydb"
  9. HOST="somehost"
  10.  
  11. vsql = "/opt/vertica/bin/vsql -d %s -h %s -U %s -w %s -X -t -c " % (DB, HOST, USER, PASS)
  12.  
  13. node = 'v_rpt_node0001'
  14.  
  15. metrics = {
  16. 'active_user_session_count': {
  17. 'table': 'v_monitor.query_metrics',
  18. 'time_max': 60,
  19. 'value_type': 'uint32',
  20. 'units': 'Sessions',
  21. 'slope': 'both',
  22. 'format': '%u',
  23. 'description': 'Active User Sessions',
  24. 'groups': 'vertica'
  25. },
  26. 'active_system_session_count': {
  27. 'table': 'v_monitor.query_metrics',
  28. 'time_max': 60,
  29. 'value_type': 'uint32',
  30. 'units': 'Sessions',
  31. 'slope': 'both',
  32. 'format': '%u',
  33. 'description': 'Active System Sessions',
  34. 'groups': 'vertica'
  35. },
  36. 'total_user_session_count': {
  37. 'table': 'v_monitor.query_metrics',
  38. 'time_max': 60,
  39. 'value_type': 'uint32',
  40. 'units': 'Sessions',
  41. 'slope': 'both',
  42. 'format': '%u',
  43. 'description': 'Total User Sessions',
  44. 'groups': 'vertica'
  45. },
  46. 'total_system_session_count': {
  47. 'table': 'v_monitor.query_metrics',
  48. 'time_max': 60,
  49. 'value_type': 'uint32',
  50. 'units': 'Sessions',
  51. 'slope': 'both',
  52. 'format': '%u',
  53. 'description': 'Total System Sessions',
  54. 'groups': 'vertica'
  55. },
  56. 'total_active_session_count': {
  57. 'table': 'v_monitor.query_metrics',
  58. 'time_max': 60,
  59. 'value_type': 'uint32',
  60. 'units': 'Sessions',
  61. 'slope': 'both',
  62. 'format': '%u',
  63. 'description': 'Total Active Sessions',
  64. 'groups': 'vertica'
  65. },
  66. 'total_session_count': {
  67. 'table': 'v_monitor.query_metrics',
  68. 'time_max': 60,
  69. 'value_type': 'uint32',
  70. 'units': 'Sessions',
  71. 'slope': 'both',
  72. 'format': '%u',
  73. 'description': 'Total Sessions',
  74. 'groups': 'vertica'
  75. },
  76. 'running_query_count': {
  77. 'table': 'v_monitor.query_metrics',
  78. 'time_max': 60,
  79. 'value_type': 'uint32',
  80. 'units': 'Sessions',
  81. 'slope': 'both',
  82. 'format': '%u',
  83. 'description': 'Running Queries',
  84. 'groups': 'vertica'
  85. },
  86. 'executed_query_count': {
  87. 'table': 'v_monitor.query_metrics',
  88. 'time_max': 60,
  89. 'value_type': 'uint32',
  90. 'units': 'Sessions',
  91. 'slope': 'both',
  92. 'format': '%u',
  93. 'description': 'Executed Queries',
  94. 'groups': 'vertica'
  95. },
  96. 'request_count': {
  97. 'table': 'v_monitor.resource_usage',
  98. 'time_max': 60,
  99. 'value_type': 'uint32',
  100. 'units': '',
  101. 'slope': 'both',
  102. 'format': '%u',
  103. 'description': 'Request Count',
  104. 'groups': 'vertica'
  105. },
  106. 'local_request_count': {
  107. 'table': 'v_monitor.resource_usage',
  108. 'time_max': 60,
  109. 'value_type': 'uint32',
  110. 'units': 'Requests',
  111. 'slope': 'both',
  112. 'format': '%u',
  113. 'description': 'Local Request Count',
  114. 'groups': 'vertica'
  115. },
  116. 'request_queue_depth': {
  117. 'select': 'nvl(request_queue_depth, 0)',
  118. 'table': 'v_monitor.resource_usage',
  119. 'time_max': 60,
  120. 'value_type': 'uint32',
  121. 'units': 'Requests',
  122. 'slope': 'both',
  123. 'format': '%u',
  124. 'description': 'Request Queue Depth',
  125. 'groups': 'vertica'
  126. },
  127. 'active_thread_count': {
  128. 'table': 'v_monitor.resource_usage',
  129. 'time_max': 60,
  130. 'value_type': 'uint32',
  131. 'units': 'Threads',
  132. 'slope': 'both',
  133. 'format': '%u',
  134. 'description': 'Active Threads',
  135. 'groups': 'vertica'
  136. },
  137. 'open_file_handle_count': {
  138. 'table': 'v_monitor.resource_usage',
  139. 'time_max': 60,
  140. 'value_type': 'uint32',
  141. 'units': 'File Handles',
  142. 'slope': 'both',
  143. 'format': '%u',
  144. 'description': 'Open File Handles',
  145. 'groups': 'vertica'
  146. },
  147. 'memory_requested_kb': {
  148. 'table': 'v_monitor.resource_usage',
  149. 'time_max': 60,
  150. 'value_type': 'uint32',
  151. 'units': 'kb',
  152. 'slope': 'both',
  153. 'format': '%u',
  154. 'description': 'Memory Requested',
  155. 'groups': 'vertica'
  156. },
  157. 'address_space_requested_kb': {
  158. 'table': 'v_monitor.resource_usage',
  159. 'time_max': 60,
  160. 'value_type': 'uint32',
  161. 'units': 'kb',
  162. 'slope': 'both',
  163. 'format': '%u',
  164. 'description': 'Address Space Requested',
  165. 'groups': 'vertica'
  166. },
  167. 'wos_used_bytes': {
  168. 'table': 'v_monitor.resource_usage',
  169. 'time_max': 60,
  170. 'value_type': 'uint32',
  171. 'units': 'bytes',
  172. 'slope': 'both',
  173. 'format': '%u',
  174. 'description': 'wos Used',
  175. 'groups': 'vertica'
  176. },
  177. 'wos_row_count': {
  178. 'table': 'v_monitor.resource_usage',
  179. 'time_max': 60,
  180. 'value_type': 'uint32',
  181. 'units': 'Rows',
  182. 'slope': 'both',
  183. 'format': '%u',
  184. 'description': 'wos Rows',
  185. 'groups': 'vertica'
  186. },
  187. 'ros_used_bytes': {
  188. 'table': 'v_monitor.resource_usage',
  189. 'time_max': 60,
  190. 'value_type': 'uint32',
  191. 'units': 'bytes',
  192. 'slope': 'both',
  193. 'format': '%u',
  194. 'description': 'ros Used',
  195. 'groups': 'vertica'
  196. },
  197. 'ros_row_count': {
  198. 'table': 'v_monitor.resource_usage',
  199. 'time_max': 60,
  200. 'value_type': 'uint32',
  201. 'units': 'Rows',
  202. 'slope': 'both',
  203. 'format': '%u',
  204. 'description': 'row Rows',
  205. 'groups': 'vertica'
  206. },
  207. 'total_used_bytes': {
  208. 'table': 'v_monitor.resource_usage',
  209. 'time_max': 60,
  210. 'value_type': 'uint32',
  211. 'units': 'bytes',
  212. 'slope': 'both',
  213. 'format': '%u',
  214. 'description': 'Total Used',
  215. 'groups': 'vertica'
  216. },
  217. 'total_row_count': {
  218. 'table': 'v_monitor.resource_usage',
  219. 'time_max': 60,
  220. 'value_type': 'uint32',
  221. 'units': 'Rows',
  222. 'slope': 'both',
  223. 'format': '%u',
  224. 'description': 'Total Rows',
  225. 'groups': 'vertica'
  226. }
  227. }
  228.  
  229. class curry:
  230. def __init__(self, fun, *args, **kwargs):
  231. self.fun = fun
  232. self.pending = args[:]
  233. self.kwargs = kwargs.copy()
  234.  
  235. def __call__(self, *args, **kwargs):
  236. if kwargs and self.kwargs:
  237. kw = self.kwargs.copy()
  238. kw.update(kwargs)
  239. else:
  240. kw = kwargs or self.kwargs
  241.  
  242. return self.fun(*(self.pending + args), **kw)
  243.  
  244.  
  245. def get_metric(metric, name):
  246. select = metric[1].get('select', name)
  247. table = metric[1].get('table')
  248. sql = "select %s from %s where node_name = '%s'" % (select, table, node)
  249. sqlcmd = "%s \"%s\"" % (vsql, sql)
  250. res = Popen(sqlcmd, shell=True, stdout=PIPE).stdout.read()
  251. rows = clean_output(res)
  252. if rows[0][0] == '':
  253. return 0
  254. return int(rows[0][0])
  255.  
  256. def clean_output(vsql_output):
  257. """
  258. takes output from vsql, parses it into column names and rows, returns a dict
  259. could use some extra checking for # rows returned, or non-standard output
  260. """
  261. lines = vsql_output.split("\n")
  262. #cols = []
  263. #for value in lines[0].split('|'):
  264. # cols.append(value.strip())
  265. rows = []
  266. for line in lines:
  267. values = []
  268. for value in line.split('|'):
  269. values.append(value.strip())
  270. rows.append(values)
  271. return rows
  272.  
  273. def metric_init(params):
  274. global descriptors
  275.  
  276. descriptors = []
  277.  
  278. for metric in metrics.items():
  279. descriptor = {
  280. 'name': metric[0],
  281. 'call_back': curry(get_metric, metric),
  282. 'time_max': metric[1].get('time_max'),
  283. 'value_type': metric[1].get('value_type'),
  284. 'units': metric[1].get('units'),
  285. 'slope': metric[1].get('slope'),
  286. 'format': metric[1].get('format'),
  287. 'description': metric[1].get('description'),
  288. 'groups': metric[1].get('groups')
  289. }
  290. descriptors.append(descriptor)
  291.  
  292. return descriptors
  293.  
  294. def metric_cleanup():
  295. '''Clean up the metric module.'''
  296. pass
  297.  
  298. if __name__ == '__main__':
  299. descriptors = metric_init(None)
  300.  
  301. for d in descriptors:
  302. v = d['call_back'](d['name'])
  303. print 'value for %s is %u' % (d['name'], v)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement