Advertisement
Guest User

Untitled

a guest
Jan 17th, 2016
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # coding=utf-8
  3.  
  4. import time
  5. import vertica_python
  6. from checks import AgentCheck
  7. from hashlib import md5
  8.  
  9.  
  10. class VerticaCheck(AgentCheck):
  11.  
  12. def check(self, instance):
  13. # Load values from config
  14. default_host = 'localhost'
  15. default_port = '5433'
  16. default_database = ''
  17. default_user = 'dbadmin'
  18. default_password = ''
  19. default_schema = 'public'
  20. default_name = 'name'
  21. default_timeout = self.init_config.get('default_timeout', 5)
  22.  
  23. if 'host' not in instance:
  24. self.log.info("No host configured, using %s" % default_host)
  25. elif 'port' not in instance:
  26. self.log.info("No port configured, using %s" % default_port)
  27. elif 'database' not in instance:
  28. self.log.info("No host configured, using %s" % default_database)
  29. elif 'user' not in instance:
  30. self.log.info("No user configured, using %s" % default_user)
  31. elif 'password' not in instance:
  32. self.log.info("No password configured, using %s" % default_password)
  33. elif 'schema' not in instance:
  34. self.log.info("No schema configured, using %s" % default_schema)
  35. elif 'name' not in instances:
  36. self.log.info("No name configure, using %s" % default_name)
  37.  
  38. host = instance.get('host', default_host)
  39. port = instance.get('port', default_port)
  40. database = instance.get('database', default_database)
  41. user = instance.get('user', default_user)
  42. password = instance.get('password', default_password)
  43. schema = instance.get('schema', default_schema)
  44. name = instance.get('name', default_name)
  45. aggregation_key = md5(host).hexdigest()
  46. conn_info = {'host': host, 'port': int(port), 'user': user, 'password': password, 'database': database, 'schema': schema}
  47.  
  48. try:
  49. c = vertica_python.connect(**conn_info)
  50. except vertica_python.errors.ConnectionError as e:
  51. self.log.info("Connection error: %s" % e)
  52. self.vertica_error_event(e, "Could not connect to: %s" % host, host, aggregation_key)
  53. return
  54.  
  55. cursor = c.cursor('dict')
  56. cursor.execute('select * from system;')
  57. row = cursor.fetchone()
  58. cursor.close()
  59. for col, value in row.iteritems():
  60. self.gauge('%s.%s' % (name, col), value, tags=['service:vertica'])
  61. if col == 'node_down_count' and value > 0:
  62. self.vertica_error_event('%s Vertica node down' % name, '%s Vertica node down' % name, host, aggregation_key)
  63.  
  64. def vertica_error_event(self, msg_text, msg_title, host, aggregation_key):
  65. self.event({
  66. 'timestamp': int(time.time()),
  67. 'event_type': 'vertica_check',
  68. 'msg_title': '%s' % msg_title,
  69. 'msg_text': 'Error Mesage:\n%s' % msg_text,
  70. 'aggregation_key': aggregation_key,
  71. 'alert_type': 'error',
  72. 'host': host,
  73. 'tags': ['service:vertica']
  74. })
  75.  
  76. if __name__ == '__main__':
  77. check, instances = VerticaCheck.from_yaml('/etc/dd-agent/conf.d/vertica.yaml')
  78. for instance in instances:
  79. check.check(instances)
  80. print "\nChecking %s" % (instance['name'])
  81. if check.has_events():
  82. print 'Events: %s' % (check.get_events())
  83. print '%s: %s' % (instance['name'], check.get_metrics())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement