前言
如果想對postgres_exporter內容做修改,需要自己進行編譯。下面是postgres_exporter的README說明
# PostgreSQL Server Exporter
Prometheus exporter for PostgreSQL server metrics.
CI Tested PostgreSQL versions: `9.4`, `9.5`, `9.6`, `10`, `11`
## Quick Start
This package is available for Docker:
```
# Start an example database
docker run --net=host -it --rm -e POSTGRES_PASSWORD=password postgres
# Connect to it
docker run --net=host -e DATA_SOURCE_NAME="postgresql://postgres:password@localhost:5432/postgres?sslmode=disable" wrouesnel/postgres_exporter
```
## Building and running
The build system is based on [Mage](https://magefile.org)
The default make file behavior is to build the binary:
```
$ go get github.com/wrouesnel/postgres_exporter
$ cd ${GOPATH-$HOME/go}/src/github.com/wrouesnel/postgres_exporter
$ go run mage.go binary
$ export DATA_SOURCE_NAME="postgresql://login:password@hostname:port/dbname"
$ ./postgres_exporter <flags>
```
To build the dockerfile, run `go run mage.go docker`.
This will build the docker image as `wrouesnel/postgres_exporter:latest`. This
is a minimal docker image containing *just* postgres_exporter. By default no SSL
certificates are included, if you need to use SSL you should either bind-mount
`/etc/ssl/certs/ca-certificates.crt` or derive a new image containing them.
下載安裝
去官網https://golang.google.cn/dl/,下載go安裝程序(推薦linux,後面使用起來會很方便)
在下載頁中會有一套安裝使用教程,大概意思就是解壓、配置環境變量、寫個hello word、build生成可執行文件
這些過程最好都做一遍,也算對go的瞭解。
構建和運行
構建運行就四步
The default make file behavior is to build the binary:
```
$ go get github.com/wrouesnel/postgres_exporter
$ cd ${GOPATH-$HOME/go}/src/github.com/wrouesnel/postgres_exporter
$ go run mage.go binary
$ export DATA_SOURCE_NAME="postgresql://login:password@hostname:port/dbname"
$ ./postgres_exporter <flags>
```
[root@baidu ~]# go get github.com/wrouesnel/postgres_exporter
go: missing Git command. See https://golang.org/s/gogetcmd
package github.com/wrouesnel/postgres_exporter: exec: "git": executable file not found in $PATH
[root@baidu ~]# yum install git
Loaded plugins: fastestmirror, langpacks
base | 3.6 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.tuna.tsinghua.edu.cn
* updates: mirrors.tuna.tsinghua.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package git.x86_64 0:1.8.3.1-23.el7_8 will be installed
......
.....
git.x86_64 0:1.8.3.1-23.el7_8
Dependency Installed:
perl-Git.noarch 0:1.8.3.1-23.el7_8 perl-TermReadKey.x86_64 0:2.30-20.el7
Complete!
[root@baidu ~]# go get github.com/wrouesnel/postgres_exporter
package github.com/wrouesnel/postgres_exporter: build constraints exclude all Go files in
/root/go/src/github.com/wrouesnel/postgres_exporter
1)執行go get github.com/wrouesnel/postgres_exporter可能會缺少依賴包,yum install去下載即可
2)進入/root/go/src/github.com/wrouesnel/postgres_exporter目錄,執行go run mage.go binary
[root@baidu postgres_exporter]# ll
total 84
drwxr-xr-x. 3 root root 41 Jun 19 16:45 assets
drwxr-xr-x. 3 root root 50 Jun 19 16:45 bin
drwxr-xr-x. 3 root root 31 Jun 19 16:34 cmd
-rw-r--r--. 1 root root 178 Jun 19 16:34 Dockerfile
-rw-r--r--. 1 root root 3470 Jun 19 16:34 example.alerts.yml
-rwxr-xr-x. 1 root root 524 Jun 19 16:34 gh-assets-clone.sh
-rwxr-xr-x. 1 root root 697 Jun 19 16:34 gh-metrics-push.sh
-rw-r--r--. 1 root root 584 Jun 19 16:34 LICENSE
-rw-r--r--. 1 root root 19795 Jun 19 16:34 magefile.go
-rw-r--r--. 1 root root 121 Jun 19 16:34 mage.go
lrwxrwxrwx. 1 root root 110 Jun 19 16:47 postgres_exporter -> /root/go/src/github.com/wrouesnel/postgres_exporter/bin/postgres_exporter_v0.8.0_linux-amd64/postgres_exporter
-rwxr-xr-x. 1 root root 384 Jun 19 16:34 postgres_exporter_integration_test_script
-rw-r--r--. 1 root root 3196 Jun 19 16:34 postgres_exporter.rc
-rwxr-xr-x. 1 root root 1154 Jun 19 16:34 postgres-metrics-get-changes.sh
-rw-r--r--. 1 root root 8934 Jun 19 16:34 queries.yaml
-rw-r--r--. 1 root root 11265 Jun 19 16:34 README.md
-rw-r--r--. 1 root root 1558 Jun 19 16:34 README-RDS.md
drwxr-xr-x. 2 root root 6 Jun 19 16:43 release
drwxr-xr-x. 4 root root 77 Jun 19 16:43 tools
drwxr-xr-x. 5 root root 77 Jun 19 16:34 vendor
[root@baidu postgres_exporter]#
進行編譯,會多出幾個文件夾(時間較新的),比較重要的是bin,存放postgres_exporter源碼,以及postgres_exporter外連接
3)設置環境變量,export DATA_SOURCE_NAME=postgresql://hgdb:[email protected]:4102/hgdb?sslmode=disable
4)運行應用
[root@baidu postgres_exporter]# export DATA_SOURCE_NAME=postgresql://hgdb:[email protected]:4102/hgdb?sslmode=disable
[root@baidu postgres_exporter]# ./postgres_exporter --log.level="debug"
INFO[0000] Established new database connection to "192.168.90.124:4102". source="postgres_exporter.go:881"
DEBU[0000] Querying Postgres Version on "192.168.90.124:4102" source="postgres_exporter.go:1391"
INFO[0000] Semantic Version Changed on "192.168.90.124:4102": 0.0.0 -> 12.1.0 source="postgres_exporter.go:1408"
DEBU[0000] procpid is being forced to discard due to version incompatibility. source="postgres_exporter.go:599"
DEBU[0000] pg_xlog_location_diff is being forced to discard due to version incompatibility. source="postgres_exporter.go:599"
DEBU[0000] write_location is being forced to discard due to version incompatibility. source="postgres_exporter.go:599"
DEBU[0000] replay_location is being forced to discard due to version incompatibility. source="postgres_exporter.go:599"
DEBU[0000] flush_location is being forced to discard due to version incompatibility. source="postgres_exporter.go:599"
DEBU[0000] sent_location is being forced to discard due to version incompatibility. source="postgres_exporter.go:599"
DEBU[0000] Querying pg_setting view on "192.168.90.124:4102" source="pg_setting.go:15"
DEBU[0000] Querying namespace: pg_stat_replication source="postgres_exporter.go:1326"
DEBU[0000] Querying namespace: pg_stat_archiver source="postgres_exporter.go:1326"
DEBU[0000] Querying namespace: pg_stat_activity source="postgres_exporter.go:1326"
DEBU[0000] Querying namespace: pg_stat_bgwriter source="postgres_exporter.go:1326"
DEBU[0000] Querying namespace: pg_stat_database source="postgres_exporter.go:1326"
DEBU[0000] Querying namespace: pg_stat_database_conflicts source="postgres_exporter.go:1326"
DEBU[0000] Querying namespace: pg_locks source="postgres_exporter.go:1326"
INFO[0000] Starting Server: :9187 source="postgres_exporter.go:1675"
DEBU[0011] Querying Postgres Version on "192.168.90.124:4102" source="postgres_exporter.go:1391"
DEBU[0011] Querying pg_setting view on "192.168.90.124:4102" source="pg_setting.go:15"
DEBU[0011] Querying namespace: pg_stat_replication source="postgres_exporter.go:1326"
DEBU[0011] Querying namespace: pg_stat_archiver source="postgres_exporter.go:1326"
DEBU[0011] Querying namespace: pg_stat_activity source="postgres_exporter.go:1326"
DEBU[0011] Querying namespace: pg_stat_bgwriter source="postgres_exporter.go:1326"
DEBU[0011] Querying namespace: pg_stat_database source="postgres_exporter.go:1326"
DEBU[0011] Querying namespace: pg_stat_database_conflicts source="postgres_exporter.go:1326"
DEBU[0011] Querying namespace: pg_locks source="postgres_exporter.go:1326"
到此爲止,二進制文件啓動就搞完了。
docker文件
官方還給出了打包成dockerfile,這是一個包含的最小 Docker 圖像,只包含 postgres_exporter
1)執行go run mage.go docker打包命令,發現docker不在$PATH,然後yum install docker,
[root@baidu postgres_exporter]# go run mage.go docker
2020/06/19 16:58:45 exec: git rev-parse --abbrev-ref HEAD
2020/06/19 16:58:45 exec: git rev-parse HEAD
2020/06/19 16:58:45 exec: git describe --dirty
2020/06/19 16:58:45 exec: git describe --abbrev=0
2020/06/19 16:58:45 exec: go list ./...
Building /root/go/src/github.com/wrouesnel/postgres_exporter/bin/postgres_exporter_v0.8.0_linux-amd64/postgres_exporter
Error: failed to run "docker build --build-arg=binary=bin/postgres_exporter_v0.8.0_linux-amd64/postgres_exporter -t wrouesnel/postgres_exporter:latest .: exec: "docker": executable file not found in $PATH"
exit status 1
[root@baidu postgres_exporter]# yum install docker
...
...
Complete!
[root@baidu postgres_exporter]#
2) 再次執行,出現超時問題
[root@baidu postgres_exporter]# go run mage.go docker
2020/06/19 10:42:41 exec: git rev-parse --abbrev-ref HEAD
2020/06/19 10:42:41 exec: git rev-parse HEAD
2020/06/19 10:42:41 exec: git describe --dirty
2020/06/19 10:42:41 exec: git describe --abbrev=0
2020/06/19 10:42:41 exec: go list ./...
Building /root/go/src/github.com/wrouesnel/postgres_exporter/bin/postgres_exporter_v0.8.0_linux-amd64/postgres_exporter
Sending build context to Docker daemon 13.72 MB
Step 1/7 : FROM debian:10-slim
Trying to pull repository docker.io/library/debian ...
10-slim: Pulling from docker.io/library/debian
8559a31e96f4: Pulling fs layer
error pulling image configuration: Get https://production.cloudflare.docker.com/registry-v2/docker/registry/v2/blobs/sha256/43/43e3995ee54ac008271bfcf2d8ac7278c33f4c5e83d2f02bfcddd350034e3357/data?verify=1592537642-YR%2BjUslatzaYnU4y8s5krkaEcz0%3D: dial tcp 104.18.123.25:443: i/o timeout
Error: running "docker build --build-arg=binary=bin/postgres_exporter_v0.8.0_linux-amd64/postgres_exporter -t wrouesnel/postgres_exporter:latest ." failed with exit code 1
exit status 1
[root@baidu postgres_exporter]#
在/etc/resolv.conf文件中新增一個參數,nameserver 8.8.8.8
然後重啓docker,再次執行
[root@baidu postgres_exporter]# service docker restart
Redirecting to /bin/systemctl restart docker.service
[root@baidu postgres_exporter]# go run mage.go docker
2020/06/19 17:06:45 exec: git rev-parse --abbrev-ref HEAD
2020/06/19 17:06:45 exec: git rev-parse HEAD
2020/06/19 17:06:45 exec: git describe --dirty
2020/06/19 17:06:45 exec: git describe --abbrev=0
2020/06/19 17:06:45 exec: go list ./...
Building /root/go/src/github.com/wrouesnel/postgres_exporter/bin/postgres_exporter_v0.8.0_linux-amd64/postgres_exporter
Sending build context to Docker daemon 13.72 MB
Step 1/7 : FROM debian:10-slim
...
...
Step 7/7 : ENTRYPOINT /postgres_exporter
---> Running in 75c1d2148368
---> 7844bb03e427
Removing intermediate container 75c1d2148368
Successfully built 7844bb03e427
[root@baidu postgres_exporter]#
根據生成目錄Building ,進入查看
[root@baidu postgres_exporter]# cd /root/go/src/github.com/wrouesnel/postgres_exporter/bin/postgres_exporter_v0.8.0_linux-amd64
[root@baidu postgres_exporter_v0.8.0_linux-amd64]# ll
total 13396
-rwxr-xr-x. 1 root root 13716800 Jun 19 17:08 postgres_exporter
[root@baidu postgres_exporter_v0.8.0_linux-amd64]#
到此docker打包結束