【监控】Linux部署postgres_exporter及PG配置(非Docker)

news2024/11/15 21:46:36

目录

  • 一、下载及部署
  • 二、postgres_exporter配置
    • 1. 停止脚本stop.sh
    • 2. 启动脚本start.sh
    • 3. queries.yaml
  • 三、PostgreSQL数据库配置
    • 1. 修改postgresql.conf配置文件
    • 2. 创建用户、表、扩展等
  • 四、参考

一、下载及部署

下载地址

选一个amd64下载
在这里插入图片描述
上传至服务器,解压

tax -xvf postgres_exporter-0.11.1.linux-amd64.tar.gz

进入解压后的目录

二、postgres_exporter配置

1. 停止脚本stop.sh

建立停止脚本 stop.sh 。注意unix编码

#!/bin/sh
echo "stop"
#!/bin/bash

PID=$(ps -ef | grep postgres_exporter | grep -v grep | awk '{ print $2 }')
if [ "${PID}" ]
then
    echo 'Application is stpping...'
    echo kill $PID DONE
    kill $PID
else
    echo 'Application is already stopped...'
fi

2. 启动脚本start.sh

启动脚本start.sh

  • 后面会建立postgres_exporter用户,密码为password

  • –web.listen-address为监听的端口

  • –extend.query-path为自定义查询的文件

sh stop.sh
export DATA_SOURCE_NAME=postgresql://postgres_exporter:password@数据库IP:数据库端口/postgres?sslmode=disable

nohup ./postgres_exporter --web.listen-address=0.0.0.0:8001 --extend.query-path=queries.yaml >nohup.out 2>&1 &

3. queries.yaml

pg_replication:
  query: "SELECT CASE WHEN NOT pg_is_in_recovery() THEN 0 ELSE GREATEST (0, EXTRACT(EPOCH FROM (now() - pg_last_xact_replay_timestamp()))) END AS lag"
  master: true
  metrics:
    - lag:
        usage: "GAUGE"
        description: "Replication lag behind master in seconds"

pg_postmaster:
  query: "SELECT pg_postmaster_start_time as start_time_seconds from pg_postmaster_start_time()"
  master: true
  metrics:
    - start_time_seconds:
        usage: "GAUGE"
        description: "Time at which postmaster started"

pg_stat_user_tables:
  query: |
   SELECT
     current_database() datname,
     schemaname,
     relname,
     seq_scan,
     seq_tup_read,
     idx_scan,
     idx_tup_fetch,
     n_tup_ins,
     n_tup_upd,
     n_tup_del,
     n_tup_hot_upd,
     n_live_tup,
     n_dead_tup,
     n_mod_since_analyze,
     COALESCE(last_vacuum, '1970-01-01Z') as last_vacuum,
     COALESCE(last_autovacuum, '1970-01-01Z') as last_autovacuum,
     COALESCE(last_analyze, '1970-01-01Z') as last_analyze,
     COALESCE(last_autoanalyze, '1970-01-01Z') as last_autoanalyze,
     vacuum_count,
     autovacuum_count,
     analyze_count,
     autoanalyze_count
   FROM
     pg_stat_user_tables
  metrics:
    - datname:
        usage: "LABEL"
        description: "Name of current database"
    - schemaname:
        usage: "LABEL"
        description: "Name of the schema that this table is in"
    - relname:
        usage: "LABEL"
        description: "Name of this table"
    - seq_scan:
        usage: "COUNTER"
        description: "Number of sequential scans initiated on this table"
    - seq_tup_read:
        usage: "COUNTER"
        description: "Number of live rows fetched by sequential scans"
    - idx_scan:
        usage: "COUNTER"
        description: "Number of index scans initiated on this table"
    - idx_tup_fetch:
        usage: "COUNTER"
        description: "Number of live rows fetched by index scans"
    - n_tup_ins:
        usage: "COUNTER"
        description: "Number of rows inserted"
    - n_tup_upd:
        usage: "COUNTER"
        description: "Number of rows updated"
    - n_tup_del:
        usage: "COUNTER"
        description: "Number of rows deleted"
    - n_tup_hot_upd:
        usage: "COUNTER"
        description: "Number of rows HOT updated (i.e., with no separate index update required)"
    - n_live_tup:
        usage: "GAUGE"
        description: "Estimated number of live rows"
    - n_dead_tup:
        usage: "GAUGE"
        description: "Estimated number of dead rows"
    - n_mod_since_analyze:
        usage: "GAUGE"
        description: "Estimated number of rows changed since last analyze"
    - last_vacuum:
        usage: "GAUGE"
        description: "Last time at which this table was manually vacuumed (not counting VACUUM FULL)"
    - last_autovacuum:
        usage: "GAUGE"
        description: "Last time at which this table was vacuumed by the autovacuum daemon"
    - last_analyze:
        usage: "GAUGE"
        description: "Last time at which this table was manually analyzed"
    - last_autoanalyze:
        usage: "GAUGE"
        description: "Last time at which this table was analyzed by the autovacuum daemon"
    - vacuum_count:
        usage: "COUNTER"
        description: "Number of times this table has been manually vacuumed (not counting VACUUM FULL)"
    - autovacuum_count:
        usage: "COUNTER"
        description: "Number of times this table has been vacuumed by the autovacuum daemon"
    - analyze_count:
        usage: "COUNTER"
        description: "Number of times this table has been manually analyzed"
    - autoanalyze_count:
        usage: "COUNTER"
        description: "Number of times this table has been analyzed by the autovacuum daemon"

pg_statio_user_tables:
  query: "SELECT current_database() datname, schemaname, relname, heap_blks_read, heap_blks_hit, idx_blks_read, idx_blks_hit, toast_blks_read, toast_blks_hit, tidx_blks_read, tidx_blks_hit FROM pg_statio_user_tables"
  metrics:
    - datname:
        usage: "LABEL"
        description: "Name of current database"
    - schemaname:
        usage: "LABEL"
        description: "Name of the schema that this table is in"
    - relname:
        usage: "LABEL"
        description: "Name of this table"
    - heap_blks_read:
        usage: "COUNTER"
        description: "Number of disk blocks read from this table"
    - heap_blks_hit:
        usage: "COUNTER"
        description: "Number of buffer hits in this table"
    - idx_blks_read:
        usage: "COUNTER"
        description: "Number of disk blocks read from all indexes on this table"
    - idx_blks_hit:
        usage: "COUNTER"
        description: "Number of buffer hits in all indexes on this table"
    - toast_blks_read:
        usage: "COUNTER"
        description: "Number of disk blocks read from this table's TOAST table (if any)"
    - toast_blks_hit:
        usage: "COUNTER"
        description: "Number of buffer hits in this table's TOAST table (if any)"
    - tidx_blks_read:
        usage: "COUNTER"
        description: "Number of disk blocks read from this table's TOAST table indexes (if any)"
    - tidx_blks_hit:
        usage: "COUNTER"
        description: "Number of buffer hits in this table's TOAST table indexes (if any)"

# WARNING: This set of metrics can be very expensive on a busy server as every unique query executed will create an additional time series
pg_stat_statements:
  query: "SELECT t2.rolname, t3.datname, queryid, calls, total_time / 1000 as total_time_seconds, min_time / 1000 as min_time_seconds, max_time / 1000 as max_time_seconds, mean_time / 1000 as mean_time_seconds, stddev_time / 1000 as stddev_time_seconds, rows, shared_blks_hit, shared_blks_read, shared_blks_dirtied, shared_blks_written, local_blks_hit, local_blks_read, local_blks_dirtied, local_blks_written, temp_blks_read, temp_blks_written, blk_read_time / 1000 as blk_read_time_seconds, blk_write_time / 1000 as blk_write_time_seconds FROM pg_stat_statements t1 JOIN pg_roles t2 ON (t1.userid=t2.oid) JOIN pg_database t3 ON (t1.dbid=t3.oid) WHERE t2.rolname != 'rdsadmin'"
  master: true
  metrics:
    - rolname:
        usage: "LABEL"
        description: "Name of user"
    - datname:
        usage: "LABEL"
        description: "Name of database"
    - queryid:
        usage: "LABEL"
        description: "Query ID"
    - calls:
        usage: "COUNTER"
        description: "Number of times executed"
    - total_time_seconds:
        usage: "COUNTER"
        description: "Total time spent in the statement, in milliseconds"
    - min_time_seconds:
        usage: "GAUGE"
        description: "Minimum time spent in the statement, in milliseconds"
    - max_time_seconds:
        usage: "GAUGE"
        description: "Maximum time spent in the statement, in milliseconds"
    - mean_time_seconds:
        usage: "GAUGE"
        description: "Mean time spent in the statement, in milliseconds"
    - stddev_time_seconds:
        usage: "GAUGE"
        description: "Population standard deviation of time spent in the statement, in milliseconds"
    - rows:
        usage: "COUNTER"
        description: "Total number of rows retrieved or affected by the statement"
    - shared_blks_hit:
        usage: "COUNTER"
        description: "Total number of shared block cache hits by the statement"
    - shared_blks_read:
        usage: "COUNTER"
        description: "Total number of shared blocks read by the statement"
    - shared_blks_dirtied:
        usage: "COUNTER"
        description: "Total number of shared blocks dirtied by the statement"
    - shared_blks_written:
        usage: "COUNTER"
        description: "Total number of shared blocks written by the statement"
    - local_blks_hit:
        usage: "COUNTER"
        description: "Total number of local block cache hits by the statement"
    - local_blks_read:
        usage: "COUNTER"
        description: "Total number of local blocks read by the statement"
    - local_blks_dirtied:
        usage: "COUNTER"
        description: "Total number of local blocks dirtied by the statement"
    - local_blks_written:
        usage: "COUNTER"
        description: "Total number of local blocks written by the statement"
    - temp_blks_read:
        usage: "COUNTER"
        description: "Total number of temp blocks read by the statement"
    - temp_blks_written:
        usage: "COUNTER"
        description: "Total number of temp blocks written by the statement"
    - blk_read_time_seconds:
        usage: "COUNTER"
        description: "Total time the statement spent reading blocks, in milliseconds (if track_io_timing is enabled, otherwise zero)"
    - blk_write_time_seconds:
        usage: "COUNTER"
        description: "Total time the statement spent writing blocks, in milliseconds (if track_io_timing is enabled, otherwise zero)"

pg_process_idle:
  query: |
    WITH
      metrics AS (
        SELECT
          application_name,
          SUM(EXTRACT(EPOCH FROM (CURRENT_TIMESTAMP - state_change))::bigint)::float AS process_idle_seconds_sum,
          COUNT(*) AS process_idle_seconds_count
        FROM pg_stat_activity
        WHERE state = 'idle'
        GROUP BY application_name
      ),
      buckets AS (
        SELECT
          application_name,
          le,
          SUM(
            CASE WHEN EXTRACT(EPOCH FROM (CURRENT_TIMESTAMP - state_change)) <= le
              THEN 1
              ELSE 0
            END
          )::bigint AS bucket
        FROM
          pg_stat_activity,
          UNNEST(ARRAY[1, 2, 5, 15, 30, 60, 90, 120, 300]) AS le
        GROUP BY application_name, le
        ORDER BY application_name, le
      )
    SELECT
      application_name,
      process_idle_seconds_sum as seconds_sum,
      process_idle_seconds_count as seconds_count,
      ARRAY_AGG(le) AS seconds,
      ARRAY_AGG(bucket) AS seconds_bucket
    FROM metrics JOIN buckets USING (application_name)
    GROUP BY 1, 2, 3
  metrics:
    - application_name:
        usage: "LABEL"
        description: "Application Name"
    - seconds:
        usage: "HISTOGRAM"
        description: "Idle time of server processes"

三、PostgreSQL数据库配置

1. 修改postgresql.conf配置文件

  1. 先根据命令在服务器上找到配置文件在哪
find / -name postgresql.conf
  1. 修改配置文件postgresql.conf,添加下面三行
shared_preload_libraries = 'pg_stat_statements'      
pg_stat_statements.max = 1000
pg_stat_statements.track = all
  1. 重启pg服务(pg不同安装方式启动方式可能不同)
pg_ctl restart

2. 创建用户、表、扩展等

官网的文档里提示比pg10高或低版本的数据库执行的SQL不同,但是我pg11只执行高版本的SQL失败了。最终高低版本都执行成功。

最好在postgres库下的public模式执行

版本>=10的pg,以下三段SQL都要执行

-- To use IF statements, hence to be able to check if the user exists before
-- attempting creation, we need to switch to procedural SQL (PL/pgSQL)
-- instead of standard SQL.
-- More: https://www.postgresql.org/docs/9.3/plpgsql-overview.html
-- To preserve compatibility with <9.0, DO blocks are not used; instead,
-- a function is created and dropped.
CREATE OR REPLACE FUNCTION __tmp_create_user() returns void as $$
BEGIN
  IF NOT EXISTS (
          SELECT                       -- SELECT list can stay empty for this
          FROM   pg_catalog.pg_user
          WHERE  usename = 'postgres_exporter') THEN
    CREATE USER postgres_exporter;
  END IF;
END;
$$ language plpgsql;

SELECT __tmp_create_user();
DROP FUNCTION __tmp_create_user();

ALTER USER postgres_exporter WITH PASSWORD 'password';
ALTER USER postgres_exporter SET SEARCH_PATH TO postgres_exporter,pg_catalog;

-- If deploying as non-superuser (for example in AWS RDS), uncomment the GRANT
-- line below and replace <MASTER_USER> with your root user.
-- GRANT postgres_exporter TO <MASTER_USER>;

GRANT CONNECT ON DATABASE postgres TO postgres_exporter;
GRANT pg_monitor to postgres_exporter;

版本<10的pg,只执行下面的SQL

CREATE SCHEMA IF NOT EXISTS postgres_exporter;
GRANT USAGE ON SCHEMA postgres_exporter TO postgres_exporter;

CREATE OR REPLACE FUNCTION get_pg_stat_activity() RETURNS SETOF pg_stat_activity AS
$$ SELECT * FROM pg_catalog.pg_stat_activity; $$
LANGUAGE sql
VOLATILE
SECURITY DEFINER;

CREATE OR REPLACE VIEW postgres_exporter.pg_stat_activity
AS
  SELECT * from get_pg_stat_activity();

GRANT SELECT ON postgres_exporter.pg_stat_activity TO postgres_exporter;

CREATE OR REPLACE FUNCTION get_pg_stat_replication() RETURNS SETOF pg_stat_replication AS
$$ SELECT * FROM pg_catalog.pg_stat_replication; $$
LANGUAGE sql
VOLATILE
SECURITY DEFINER;

CREATE OR REPLACE VIEW postgres_exporter.pg_stat_replication
AS
  SELECT * FROM get_pg_stat_replication();

GRANT SELECT ON postgres_exporter.pg_stat_replication TO postgres_exporter;

CREATE EXTENSION IF NOT EXISTS pg_stat_statements;
CREATE OR REPLACE FUNCTION get_pg_stat_statements() RETURNS SETOF pg_stat_statements AS
$$ SELECT * FROM public.pg_stat_statements; $$
LANGUAGE sql
VOLATILE
SECURITY DEFINER;

CREATE OR REPLACE VIEW postgres_exporter.pg_stat_statements
AS
  SELECT * FROM get_pg_stat_statements();

GRANT SELECT ON postgres_exporter.pg_stat_statements TO postgres_exporter;

来到postgres_exporter安装目录,启动postgres_exporter

sh start.sh

观察nohup.out文件,看是否有报错信息。

如果集成了Grafana,可以发现页面已经能采集到数据了
在这里插入图片描述
Grafana+prometheus+postgres_exporter参考

四、参考

  • Github地址
  • postgres_exporter使用过程中的注意事项

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/381884.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

$ 6 :选择、循环

if-else语句 #include <stdio.h> //判断输入值是否大于0 int main() {int i;while (scanf("%d",&i)){if (i > 0)//不要在括号后加分号{printf("i is bigger than O\n");}else {printf("i is not bigger than O\n");}}return O; } …

cglib代理解析

工作原理 使用 <dependency><groupId>cglib</groupId><artifactId>cglib</artifactId><version>3.3.0</version></dependency>对类和接口分别进行代理 DemoService package com.fanqiechaodan.user.service;/*** author fa…

itop-3568 开发板系统编程学习笔记(3)目录 IO

【北京迅为】嵌入式学习之Linux系统编程篇 https://www.bilibili.com/video/BV1zV411e7Cy/ 个人学习笔记 文章目录mkdir() 函数opendir() 和 closedir() 函数readdir() 函数综合实验mkdir() 函数 头文件&#xff1a; #include <sys/types.h> #include <sys/stat.h&g…

linux代码调试-gdb

在windows调试各类代码经常依托相关便利的IDE工具&#xff0c;如Microsoft的Visual Studio,TI的Code Composer Studio,ADI的CrossCore Embedded Studio ,ADI的VisualDSP&#xff0c;Renesas的CS for CC,NXP的S32 Design Studio…这些调试&#xff0c;或借助软、硬件仿真&#x…

JPA 之 Hibernate EntityManager 使用指南

Hibernate EntityManager 专题 参考&#xff1a; JPA – EntityManager常用API详解EntityManager基本概念 基本概念及获得 EntityManager 对象 基本概念 在使用持久化工具的时候&#xff0c;一般都有一个对象来操作数据库&#xff0c;在原生的Hibernate中叫做Session&…

排序之损失函数List-wise loss(系列3)

排序系列篇&#xff1a; 排序之指标集锦(系列1)原创 排序之损失函数pair-wise loss(系列2)排序之损失函数List-wise loss(系列3) 最早的关于list-wise的文章发表在Learning to Rank: From Pairwise Approach to Listwise Approach中&#xff0c;后面陆陆续续出了各种变形&#…

SpringBoot入门 - SpringBoot HelloWorld

我们了解了SpringBoot和SpringFramework的关系之后&#xff0c;我们可以开始创建一个Hello World级别的项目了。创建 SpringBoot Web 应用为快速进行开发&#xff0c;推荐你使用IDEA这类开发工具&#xff0c;它将大大提升你学习和开发的效率。选择 Spring InitializeSpring提供…

开源的 OA 办公系统 — 勾股 OA 4.3.01 发布

勾股 OA 办公系统是一款简单实用的开源的企业办公系统。系统集成了系统设置、人事管理、行政管理、消息管理、企业公告、知识库、审批流程设置、办公审批、日常办公、财务管理、客户管理、合同管理、项目管理、任务管理等功能模块。系统简约&#xff0c;易于功能扩展&#xff0…

【vue】图标选择(elementUI和svg结合)

目标&#xff1a;在做菜单权限的时候需要选择图标&#xff0c;如果既想要用elementUI自带的图标&#xff0c;还想要自定义的图标&#xff0c;这时就需要二者结合一下如果用的是vue-admin-template&#xff0c;那svg组件和引入elementUI是不需要操作的&#xff0c;直接使用即可。…

pytest学习和使用17-Pytest如何重复执行用例?(pytest-repeat)

17-Pytest如何重复执行用例&#xff1f;&#xff08;pytest-repeat&#xff09;1 使用场景2 pytest-repeat插件2.1 环境要求2.2 插件安装3 pytest-repeat使用3.1 重复测试直到失败3.2 用例标记执行重复多次3.3 命令行参数--repeat-scope详解3.3.1 class示例3.3.2 module示例1 使…

如何在软件测试面试中脱颖而出?(附教程)天花板都这样回答

面试软件测试工程师岗位&#xff0c;是否真的如网上所说&#xff0c;需要不停刷面试题?面试题可能掌握的技巧实际是一样的&#xff0c;只是题目形式不一样&#xff0c;那么应该如何在面试中脱颖而出呢?今天我们就来聊一聊。 我录制了一整套完整的软件测试面试的话术教程&…

拿下32k成功入职阿里软件测试面试常见问题及回答技巧

1、什么是兼容性测试&#xff1f;兼容性测试侧重哪些方面&#xff1f; 参考答案&#xff1a; 兼容测试主要是检查软件在不同的硬件平台、软件平台上是否可以正常的运行&#xff0c;即是通常说的软件的可移植性。 兼容的类型&#xff0c;如果细分的话&#xff0c;有平台的兼容…

测试结束参考标准

在软件消亡之前&#xff0c;如果没有测试的结束点&#xff0c;那么软件测试就永无休止&#xff0c;永远不可能结束。软件测试的结束点&#xff0c;要依据自己公司具体情况来制定&#xff0c;不能一概而论!个人认为测试结束点由以下几个条件决定&#xff1a; 1.基于“测试阶段”…

预览版Edge申请微软new Bing失败解决方案

文章目录1.首先需要配置科学上网2.下载预览版Edge浏览器卡它bug&#xff01;卡它bug&#xff01;卡它bug&#xff01;没有申请上ChatGPT的朋友们&#xff0c;试试new Bing吧&#xff0c;更新更强大&#xff0c;关于申请方式&#xff0c;网上已经有很多帖子了&#xff0c;其中一…

WebRTC 拥塞控制 | Trendline 滤波器

1.指数平滑1.1一次指数平滑法&#xff08;Single Exponential Smoothing&#xff09;指数平滑法&#xff08;Exponential Smoothing&#xff09; 是在移动平均法基础上发展起来的一种时间序列分析预测法&#xff0c;它是通过计算指数平滑值&#xff0c;配合一定的时间序列预测模…

fiddler抓包实战(1),模拟手机弱网测试,判断BUG来自客户端还是服务端

手机app中常见的测试之一就是弱网测试&#xff0c;什么是弱网测试呢?顾名思义就是模拟弱网的时候用户对于手机的一些操作和响应是否成功&#xff0c;在使用的过程中是否能够正常的使用 手机端常见弱网测试方法就是切换5G、4G、3G、2G这样 Web中就可以直接模拟打开F12然后就可…

Linux C++ 内存映射文件及其应用

Linux C 内存映射文件及其应用一. 原理二. 应用场景1. 实现多进程通信2. 实现内存持久化3.实现读写大文件三. 问答参考链接一. 原理 内存映射文件&#xff0c;是从一个文件到一块内存的映射。 内存映射文件与虚拟内存有些类似&#xff0c;通过内存映射文件可以将文件与内存中一…

【ESP32+freeRTOS学习笔记-(十)任务通知机制】

目录1、任务通知&#xff08;Task Notifications&#xff09;概念1.1 概念1.2 使用任务通知的优势1.3 无法使用任务通知的场景2、使用任务通知2.1 xTaskNotifyGive()2.2 ulTaskNotifyTake()2.3 使用任务通知代替信号量&#xff0c;方法12.4 使用任务通知代替信号量&#xff0c;…

mtd和嵌入式系统分区

一. MTD介绍 嵌入式系统的磁盘分区情况&#xff0c;一般由mtd来表示。 MTD&#xff08;Memory Technology Device)&#xff1a;内存技术设备是Linux的存储设备中的一个子系统。其设计此系统的目的是&#xff0c;对于内存类的设备&#xff0c;提供一个抽象层&#xff0c;一个接口…

【计算机三级网络技术】 第五篇 局域网技术基础及应用

文章目录一、局域网组网基础知识1.局域网的主要特征2.局域网标准3.交换式局域网4.虚拟局域网技术二、综合布线基本概念1.综合布线系统的组成2.综合布线的优点3.综合布线的设计等级4综合布线系统标准三、局域网互联设备1.中继器2.集线器3.网桥&#xff08;Bridge&#xff09;4.交…