Advertisement
Guest User

Untitled

a guest
Oct 24th, 2016
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.96 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # The psycopg you installed through apt-get is not visible from inside the virtual env.
  4.  
  5. # You should install it through pip
  6.  
  7. # pip install psycopg2
  8. # after sourcing your environment.
  9. import psycopg2
  10.  
  11. # start docker
  12. #docker run --name some-postgres -e POSTGRES_PASSWORD=geslo -e POSTGRES_USERNAME=tomaz -e POSTGRES_DB=tomaz -p 5432:5432 -d postgres
  13.  
  14. try:
  15.     fh = open('test.conf', 'w')
  16. except:
  17.     print("Can't open a file for writing")
  18.  
  19.  
  20. try:
  21.     conn = psycopg2.connect("dbname='tomazrazvoj' user='tomaz' host='127.0.0.1' password='tomaz'")
  22. except:
  23.     print("Unable to connect to database")
  24.  
  25. cur = conn.cursor()
  26.  
  27. cur.execute("""select datname from pg_database where datistemplate=false""")
  28.  
  29. rows = cur.fetchall()
  30.  
  31. document = """
  32. # This is the monitoring configuration for PostgreSQL.
  33. # Make sure the statistics collector is enabled in your PostgreSQL configuration.
  34. # NOTE: This configuration needs to be hand-edited in order to work.
  35. # Look for DATABASE_NAME, STATS_USER, STATS_PASS, POSTGRESQL_HOST and POSTGRESQL_PORT to adjust your configuration file.
  36. LoadPlugin postgresql
  37. <Plugin "postgresql">
  38.    # Each database needs a separate Database section.
  39.    # Replace DATABASE_NAME in the Database section with the name of the database."""
  40.  
  41. for row in rows:
  42.     document += """
  43.     <Database "{name}">
  44.        # Host and port are only used for TCP/IP connections.
  45.        # Leaving them out indicates you wish to connect via domain sockets.
  46.        # When using non-standard PostgreSQL configurations, replace the below with
  47.        ##Host "POSTGRESQL_HOST"
  48.        ##Port "POSTGRESQL_PORT"
  49.        Host "127.0.0.1"
  50.        Port "5432"
  51.        User "{name}"
  52.        Password "{name}1337"
  53.        Query backends
  54.        Query transactions
  55.        Query queries
  56.        Query table_states
  57.        Query disk_io
  58.        Query disk_usage
  59.    </Database>\n""".format(name=row[0])
  60.  
  61. document += "</Plugin>"
  62.  
  63. fh.write(document)
  64.  
  65. conn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement