Guest User

Untitled

a guest
Oct 1st, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. '''
  2. ____ __ ____
  3. / __ \____ _/ /_____ _/ __ \____ ____ _
  4. / / / / __ `/ __/ __ `/ / / / __ \/ __ `/
  5. / /_/ / /_/ / /_/ /_/ / /_/ / /_/ / /_/ /
  6. /_____/\__,_/\__/\__,_/_____/\____/\__, /
  7. /____/
  8. '''
  9.  
  10. import pyodbc #pyodbc is included with the Datadog agent, so you should not have to install any additional libraries
  11. from checks import AgentCheck #Datadog library for Custom Agent Check
  12.  
  13. class SQL_query(AgentCheck):
  14. def check(self, instance):
  15. server = 'tcp:127.0.0.1' #put Database connection path here
  16. database = '$databaseHere' # put Database name here
  17. username = '$usernameHere' # put Database username here
  18. password = '$passwordHere' # put Database Password here
  19.  
  20. cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
  21. cursor = cnxn.cursor()
  22. #Sample select query
  23. cursor.execute("SELECT * FROM dbo.Products;") #What to query?
  24. row = cursor.fetchone() #iterate through until you hit the end of table
  25. while row:
  26. dd_tags = [ #set datadog tags
  27. 'ProductName:' + row[1],
  28. 'ProductDescription:' + row[3],
  29. ]
  30. self.gauge('sqlserver.product.price', row[2], tags=dd_tags) #Set metric name and tags, and pass to Datadog agent
  31. row = cursor.fetchone()
  32. cnxn.close() #close connection
Add Comment
Please, Sign In to add comment