Guest User

Untitled

a guest
Jan 29th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. """
  2. Datadog Postgres Query Translator
  3.  
  4. This script will translate the results of any Postgres into Datadog metrics
  5. and submit them to Datadog.
  6. """
  7. import psycopg2
  8.  
  9. try:
  10. from checks import AgentCheck
  11. except ImportError:
  12. from datadog_checks.checks import AgentCheck
  13.  
  14. HOSTNAME = 'localhost'
  15. USERNAME = 'postgres'
  16. PASSWORD = 'postgres'
  17. DATABASE = 'postgres'
  18. QUERY = 'SELECT * FROM CITY'
  19.  
  20. def execute_postgres_query(query):
  21. connection = psycopg2.connect(host=HOSTNAME, database=DATABASE,
  22. user=USERNAME, password=PASSWORD)
  23.  
  24. cursor = connection.cursor()
  25. cursor.execute(query)
  26. column_name_list = [description[0] for description in cursor.description]
  27. row_list = cursor.fetchall()
  28. cursor.close()
  29. connection.close()
  30.  
  31. return row_list, column_name_list
  32.  
  33.  
  34. class PostgresQueryCheck(AgentCheck):
  35. def check(self, instance):
  36. row_list, column_name_list = execute_postgres_query(QUERY)
  37. for row_entry in row_list:
  38. this_row_dict = {column_name: row_entry[index]
  39. for index, column_name
  40. in enumerate(column_name_list)}
  41.  
  42. self.gauge(
  43. 'postgres.custom.population',
  44. this_row_dict['population'],
  45. tags=['countrycode:{}'.format(this_row_dict['countrycode']),
  46. 'district:{}'.format(this_row_dict['district'])]
  47. )
Add Comment
Please, Sign In to add comment