postgres_exporter编译运行过程

下载安装

去官网https://golang.google.cn/dl/,下载go安装程序(推荐linux,后面使用起来会很方便)

在下载页中会有一套安装使用教程,大概意思就是解压、配置环境变量、写个hello word、build生成可执行文件

这些过程最好都做一遍,也算对go的了解。

构建和运行

git clone https://github.com/prometheus-community/postgres_exporter.git
cd postgres_exporter
make build
./postgres_exporter 

DATA_SOURCE_NAME=postgresql://postgres_exporter:password@localhost:5432/postgres?sslmode=disable

连接超时点击这里 

目录结构如下:

[root@baidu1 postgres_exporter]# pwd
/opt/postgres_exporter
[root@baidu1 postgres_exporter]# ll
total 16640
-rw-r--r--. 1 root root     7476 Mar 16 16:24 CHANGELOG.md
drwxr-xr-x. 3 root root       31 Mar 16 16:24 cmd
-rw-r--r--. 1 root root      343 Mar 16 16:24 Dockerfile
-rwxr-xr-x. 1 root root      524 Mar 16 16:24 gh-assets-clone.sh
-rwxr-xr-x. 1 root root      697 Mar 16 16:24 gh-metrics-push.sh
-rw-r--r--. 1 root root      471 Mar 16 16:24 go.mod
-rw-r--r--. 1 root root    44092 Mar 16 16:24 go.sum
-rw-r--r--. 1 root root      584 Mar 16 16:24 LICENSE
-rw-r--r--. 1 root root      338 Mar 16 16:24 Makefile
-rw-r--r--. 1 root root    10036 Mar 16 16:24 Makefile.common
-rwxr-xr-x. 1 root root 16902099 Mar 16 16:53 postgres_exporter
-rwxr-xr-x. 1 root root      384 Mar 16 16:24 postgres_exporter_integration_test_script
-rw-r--r--. 1 root root     3196 Mar 16 16:24 postgres_exporter.rc
-rwxr-xr-x. 1 root root     1154 Mar 16 16:24 postgres-metrics-get-changes.sh
drwxr-xr-x. 4 root root      126 Mar 16 16:24 postgres_mixin
-rw-r--r--. 1 root root    10391 Mar 16 16:24 queries.yaml
-rw-r--r--. 1 root root    11609 Mar 16 16:24 README.md
-rw-r--r--. 1 root root     1594 Mar 16 16:24 README-RDS.md
drwxr-xr-x. 2 root root       35 Mar 16 16:24 scripts
-rw-r--r--. 1 root root        6 Mar 16 16:24 VERSION

例:我想在代码中打印pg的版本,编辑cmd/postgres_exporter/postgres_exporter.go

func parseVersion(versionString string) (semver.Version, error) {
	submatches := versionRegex.FindStringSubmatch(versionString)
    //versionString是pg_version的值,submatches[1]是获取pg版本
    for i := 0; i < len(submatches ); i++ {
        fmt.Println(i," => ", (submatches [i]))
    }
	if len(submatches) > 1 {
		return semver.ParseTolerant(submatches[1])
	}
	return semver.Version{},
		errors.New(fmt.Sprintln("Could not find a postgres version in string:", versionString))
}

make build

3)设置环境变量,export DATA_SOURCE_NAME=postgresql://highgo:[email protected]:5866/highgo?sslmode=disable

4)运行应用

[root@baidu postgres_exporter]# export DATA_SOURCE_NAME=postgresql://hgdb:[email protected]:4102/hgdb?sslmode=disable

[root@baidu1 postgres_exporter]# ./postgres_exporter
INFO[0000] Established new database connection to "192.168.90.124:4102".  source="postgres_exporter.go:881"
0  =>  PostgreSQL 9.5.7
1  =>  9.5.7
2  =>  9
3  =>  .5
4  =>  .7
INFO[0000] Semantic Version Changed on "192.168.90.124:4102": 0.0.0 -> 9.5.7  source="postgres_exporter.go:1408"
INFO[0000] Starting Server: :9187                        source="postgres_exporter.go:1675"

到此为止,二进制文件启动就搞完了。

如果报: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"

解决办法:yum install docker -y

如果报:Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running? 

解决办法:service docker restart

如果报超时: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

解决办法:在/etc/resolv.conf文件中新增一个参数,nameserver 8.8.8.8,重启docker

postgres_exporter编译运行过程_第1张图片

 

你可能感兴趣的:(#,Prometheus,#,PostgreSQL,exporter,postgres)