步骤
github地址:https://github.com/percona/mongodb_exporter
mongodb_exporter可以本地部署,也可以用docker部署。我这里是放在了k8s里。namespace是rancher的cattle-monitoring-system。相关yaml如下:
mongo-exporter-deploy.yaml
注意修改mongodb.uri
的地址。格式是:mongodb.uri :mongodb://username:password@localhost:27017/mydatabase?authSource=admin&authenticationDatabase=myauthdb
kind: Deployment
apiVersion: apps/v1
metadata:
name: mongo-exporter
namespace: cattle-monitoring-system
labels:
app: mongo-exporter
spec:
replicas: 1
selector:
matchLabels:
app: mongo-exporter
template:
metadata:
creationTimestamp: null
labels:
app: mongo-exporter
spec:
containers:
- name: mongo-exporter
image: 'percona/mongodb_exporter:0.20'
args:
- '--web.listen-address=:9216'
- '--mongodb.uri=mongodb://admin:xxx@mongodb-svc.namespace:27017/'
ports:
- name: 9216tcp02
containerPort: 9216
protocol: TCP
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
imagePullPolicy: IfNotPresent
securityContext:
capabilities: {}
restartPolicy: Always
terminationGracePeriodSeconds: 30
dnsPolicy: ClusterFirst
securityContext: {}
schedulerName: default-scheduler
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 25%
maxSurge: 25%
revisionHistoryLimit: 10
progressDeadlineSeconds: 600
mongo-exporter-svc.yaml
apiVersion: v1
kind: Service
metadata:
labels:
app: mongo-exporter
name: mongo-exporter
namespace: cattle-monitoring-system
spec:
ports:
- name: http
port: 9216
protocol: TCP
targetPort: 9216
selector:
app: mongo-exporter
sessionAffinity: None
type: ClusterIP
mongo-exporter-servicemonitor.yaml
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
labels:
app: mongo-exporter
name: mongo-exporter
namespace: cattle-monitoring-system
spec:
endpoints:
- interval: 30s
path: /metrics
port: http
scrapeTimeout: 10s
jobLabel: mongo-exporter
selector:
matchLabels:
app: mongo-exporter
mongo-exporter-prometheusrule.yaml
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
name: mongo-exporter
namespace: cattle-monitoring-system
spec:
groups:
- name: PerconaMongodbExporter
rules:
- alert: MongodbDown
expr: 'mongodb_up == 0'
for: 0m
labels:
severity: critical
annotations:
summary: MongoDB Down (instance {{ $labels.instance }})
description: "MongoDB instance is down\n VALUE = {{ $value }}\n LABELS = {{ $labels }}"
- alert: MongodbReplicaMemberUnhealthy
expr: 'mongodb_rs_members_health == 0'
for: 0m
labels:
severity: critical
annotations:
summary: Mongodb replica member unhealthy (instance {{ $labels.instance }})
description: "MongoDB replica member is not healthy\n VALUE = {{ $value }}\n LABELS = {{ $labels }}"
- alert: MongodbReplicationLag
expr: '(mongodb_rs_members_optimeDate{member_state="PRIMARY"} - on (set) group_right mongodb_rs_members_optimeDate{member_state="SECONDARY"}) / 1000 > 10'
for: 0m
labels:
severity: critical
annotations:
summary: MongoDB replication lag (instance {{ $labels.instance }})
description: "Mongodb replication lag is more than 10s\n VALUE = {{ $value }}\n LABELS = {{ $labels }}"
- alert: MongodbReplicationHeadroom
expr: 'sum(avg(mongodb_mongod_replset_oplog_head_timestamp - mongodb_mongod_replset_oplog_tail_timestamp)) - sum(avg(mongodb_rs_members_optimeDate{member_state="PRIMARY"} - on (set) group_right mongodb_rs_members_optimeDate{member_state="SECONDARY"})) <= 0'
for: 0m
labels:
severity: critical
annotations:
summary: MongoDB replication headroom (instance {{ $labels.instance }})
description: "MongoDB replication headroom is <= 0\n VALUE = {{ $value }}\n LABELS = {{ $labels }}"
- alert: MongodbNumberCursorsOpen
expr: 'mongodb_ss_metrics_cursor_open{csr_type="total"} > 10 * 1000'
for: 2m
labels:
severity: warning
annotations:
summary: MongoDB number cursors open (instance {{ $labels.instance }})
description: "Too many cursors opened by MongoDB for clients (> 10k)\n VALUE = {{ $value }}\n LABELS = {{ $labels }}"
- alert: MongodbCursorsTimeouts
expr: 'increase(mongodb_ss_metrics_cursor_timedOut[1m]) > 100'
for: 2m
labels:
severity: warning
annotations:
summary: MongoDB cursors timeouts (instance {{ $labels.instance }})
description: "Too many cursors are timing out\n VALUE = {{ $value }}\n LABELS = {{ $labels }}"
- alert: MongodbTooManyConnections
expr: 'avg by(instance) (rate(mongodb_ss_connections{conn_type="current"}[1m])) / avg by(instance) (sum (mongodb_ss_connections) by (instance)) * 100 > 80'
for: 2m
labels:
severity: warning
annotations:
summary: MongoDB too many connections (instance {{ $labels.instance }})
description: "Too many connections (> 80%)\n VALUE = {{ $value }}\n LABELS = {{ $labels }}"
- alert: MongodbVirtualMemoryUsage
expr: '(sum(mongodb_ss_mem_virtual) BY (instance) / sum(mongodb_ss_mem_resident) BY (instance)) > 3'
for: 2m
labels:
severity: warning
annotations:
summary: MongoDB virtual memory usage (instance {{ $labels.instance }})
description: "High memory usage\n VALUE = {{ $value }}\n LABELS = {{ $labels }}
grafana模板
使用的是官网的模板:
https://grafana.com/grafana/dashboards/12079-mongodb/
https://grafana.com/grafana/dashboards/14997-mongodb/
https://grafana.com/grafana/dashboards/7359-mongodb-replset/
报错
如果使用的是最新版本的exporter,而mongodb版本较低,会报下面的错:server reports wire version 5,but this version of the Go driver requires at least 6 (MongoDB 3.6)