Exemple #1
0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
This module contains code for statistics extraction that is based
on having a connection to a Postgres database, and running queries through it.
"""

import psycopg2
import re

from postgresql_metrics.common import get_logger

LOG = get_logger()

# contains mappings of metric-name: (last_timestamp, last_value)
# used to derive metric value diffs between the current and the previous value
DERIVE_DICT = dict()

# regex used to extra host from conninfo string
CONNINFO_HOST_RE = re.compile(r'($|\s)host=(?P<host>.*?)(^|\s)')


def get_db_connection(database,
                      username,
                      password,
                      host='127.0.0.1',
                      port=5432,
                      connect_timeout=10):
Exemple #2
0
# limitations under the License.
"""
This module contains code for preparing each monitored database in the cluster
for the functionality required in postgresql-metrics project.

This includes creating an appropriate metrics user, enabling required extensions,
and creating required functions and views.
"""
import getpass

import psycopg2

from postgresql_metrics.postgres_queries import get_db_connection
from postgresql_metrics.common import get_logger

LOG = get_logger("postgresql-metrics-prepare-db")

REPLICATION_STATS_VIEW = 'public.pg_stat_repl'
PGSTATTUPLES_FUNC_NAME = 'pgstattuple_for_table_oid'
PGSTATTUPLES_FUNC = PGSTATTUPLES_FUNC_NAME + '(INT)'


def query_user_for_superuser_credentials():
    username = raw_input(
        "Provide a Postgres role name with superuser privileges "
        "in the configured cluster: ")
    password = getpass.getpass("Give the password: ")
    return username, password


def connect_as_super_user(db_name, conf):
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
This module contains code for statistics extraction that is based
on having a connection to a Postgres database, and running queries through it.
"""

import psycopg2

from postgresql_metrics.common import get_logger

LOG = get_logger()


# contains mappings of metric-name: (last_timestamp, last_value)
# used to derive metric value diffs between the current and the previous value
DERIVE_DICT = dict()


def get_db_connection(database, username, password, host="127.0.0.1", port=5432, connect_timeout=10):
    connection = psycopg2.connect(
        user=username, password=password, host=host, port=int(port), database=database, connect_timeout=connect_timeout
    )
    connection.autocommit = True
    return connection

# limitations under the License.
"""
This module contains code for preparing each monitored database in the cluster
for the functionality required in postgresql-metrics project.

This includes creating an appropriate metrics user, enabling required extensions,
and creating required functions and views.
"""
import getpass

import psycopg2

from postgresql_metrics.postgres_queries import get_db_connection
from postgresql_metrics.common import get_logger

LOG = get_logger("postgresql-metrics-prepare-db")

REPLICATION_STATS_VIEW = 'public.pg_stat_repl'
PGSTATTUPLES_FUNC_NAME = 'pgstattuple_for_table_oid'
PGSTATTUPLES_FUNC = PGSTATTUPLES_FUNC_NAME + '(INT)'


def query_user_for_superuser_credentials():
    username = raw_input("Provide a Postgres role name with superuser privileges "
                         "in the configured cluster: ")
    password = getpass.getpass("Give the password: ")
    return username, password


def connect_as_super_user(db_name, conf):
    db_connection = None