问题描述
我尝试使用DD Agent设置postgres检查,而postgres.py脚本抛出错误。如您在屏幕快照中所见,我正在使用此简单查询来获取与数据库的活动连接数。我将它放在/etc/datadog-agent/conf.d/postgres.d/conf.yaml中,如下所示:
- metric_prefix: postgresql
query: SELECT datname as db_name,count(pid) as active_connections FROM pg_stat_activity where state = 'active' group by db_name;
columns:
- name: active_connections
type: gauge
- name: db_name
type: tag
运行配置检查时出现的错误如下:
[root@my_Box postgres.d]# datadog-agent check postgres | grep -i -A 20 -B 20 active_connections
Error: postgres:953578488181a512 | (postgres.py:398) | non-numeric value `cldtx` for metric column `active_connections` of metric_prefix `postgresql`
如果我正确理解conf.yaml文件,则可以使用某些参数来调用postgres.py脚本。可以在这里找到postgres.py脚本: https://github.com/DataDog/integrations-core/blob/master/postgres/datadog_checks/postgres/postgres.py
解决方法
在这些检查中,响应顺序很重要,因为将从数据库返回的列将映射回YAML中指定的名称。
读取错误消息:
错误:postgres:953578488181a512 | (postgres.py:398)| metric_prefix
cldtx
的指标列active_connections
的非数字值postgresql
我们可以看到,cldtx
列返回了active_connections
的值,该列在YAML中被声明为一个量表,这是一个字符串。
通过重新排列YAML,修复应该很简单,就像这样:
...
columns:
- name: db_name
type: tag
- name: active_connections
type: gauge
或者,如果要保持YAML的顺序,请将查询更改为:
...
query: SELECT count(pid) as active_connections,datname as db_name FROM pg_stat_activity where state = 'active' group by db_name;
...