Prometheus监控Mongodb副本集
副本集结构
10.140.10.27 主节点 10.140.10.28 从节点 10.140.10.29 仲裁节点
注意:
1、安装mongodb_exporter可以不在仲裁节点安装,仲裁节点设置的用户名密码是无法使用的,若仲裁节点没有安装,监控中mongodb_up是无法显示,只会显示主从的值
2、启动方式建议使用第二种,第二种mongo_up可以查看主从的值,mongodb_replset_my_state=3是正常的,如果选择第一种 mongodb_replset_my_state=4 是错误的,若只在一个节点执行启动方式一的话mongodb_replset_my_state的值也是错误的
3、mongodb设置用户名密码时尽量不要使用@这个特殊符号,启动的时候是会导致无法链接
4、最好是单独设置一个监控的用户名密码指定admin库 试过指定对应的库未成功
安装golang
yum -y install golang
编辑环境变量并生效
export GOROOT=/usr/lib/golang export GOPATH=/usr/local/golang export PATH=$PATH:$GOROOT/bin source /etc/profile
创建目录
mkdir /usr/local/golang
安装插件
go get github.com/Masterminds/glide go install github.com/Masterminds/glide cd $GOPATH/src/github.com/Masterminds/glide go build cp glide /usr/local/bin
安装mongodb_exporter
git clone https://github.com/dcu/mongodb_exporter.git $GOPATH/src/github.com/dcu/mongodb_exporter cd $GOPATH/src/github.com/dcu/mongodb_exporter make build
成功后,出现这个命令
[root@test mongodb_exporter]# ll total 10672 drwxr-xr-x 3 root root 4096 Sep 8 16:48 collector -rw-r--r-- 1 root root 516 Sep 8 16:48 Dockerfile -rw-r--r-- 1 root root 1472 Sep 8 16:48 glide.lock -rw-r--r-- 1 root root 358 Sep 8 16:48 glide.yaml drwxr-xr-x 2 root root 28 Sep 8 16:48 grafana_dashboards -rw-r--r-- 1 root root 35252 Sep 8 16:48 groups.yml -rw-r--r-- 1 root root 1082 Sep 8 16:48 LICENSE -rw-r--r-- 1 root root 783 Sep 8 16:48 Makefile -rwxr-xr-x 1 root root 10846790 Sep 8 16:48 mongodb_exporter -rw-r--r-- 1 root root 8123 Sep 8 16:48 mongodb_exporter.go -rw-r--r-- 1 root root 4683 Sep 8 16:48 README.md drwxr-xr-x 2 root root 37 Sep 8 16:48 screenshots drwxr-xr-x 2 root root 111 Sep 8 16:48 shared drwxr-xr-x 2 root root 79 Sep 8 16:48 snap drwxr-xr-x 4 root root 40 Sep 8 16:48 vendor
第一种方式启动
分别在主从节点执行此命令
cd /usr/local/golang/src/github.com/dcu/mongodb_exporter ./mongodb_exporter -mongodb.uri mongodb://10.140.10.27:27017,10.140.10.28:27017
第二种方式启动
启动主节点
cd /usr/local/golang/src/github.com/dcu/mongodb_exporter
./mongodb_exporter -mongodb.uri mongodb://10.140.10.27:27017
启动从节点
cd /usr/local/golang/src/github.com/dcu/mongodb_exporter
./mongodb_exporter -mongodb.uri mongodb://10.140.10.28:27017
启动脚本
cp /usr/local/golang/src/github.com/dcu/mongodb_exporter/mongodb_exporter /usr/local/bin
cat /etc/init.d/mongodb_exporter
#!/bin/bash IP=`ip addr | grep -v virbr |grep -o -e 'inet [0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}'|grep -v "127.0.0"|awk '{print $2}'` PORT=27017 COMMAND=/usr/local/bin/mongodb_exporter SERVER=mongodb_exporter USER=txkj PASSWORD=123456 PIDNUM=`pidof $SERVER` start(){ if [ -z "$PIDNUM" ];then $COMMAND -mongodb.uri mongodb://$USER:$PASSWORD@$IP:$PORT >/dev/null 2>&1 & echo "Now,the server has runing" else echo "$0 is running" fi } stop(){ if [ -z "$PIDNUM" ];then echo "$0 is not running" else echo "shutting down $0" kill -9 "$PIDNUM" && echo "PID $PIDNUM was killed." fi } status(){ if [[ -z "$PIDNUM" ]];then echo "$0 is not runing" else echo "$0 is runing,it's PID is $PIDNUM" fi } case $1 in start) start ;; stop) stop ;; status) status ;; restart) stop start ;; *) echo "Usage:$0 {start|stop|status|restart}" ;; esac
添加执行权限
chmod +x /etc/init.d/mongodb_exporter
检测
curl http://10.140.10.27:9001/metrics
到此,mongodb_exporter部署完成了,现在开始修改peometheus的配置文件
- job_name: 'mongodb-Rep'
static_configs:
- targets: ['10.140.10.27:9001']
labels:
instance: 10.140.10.27
- targets: ['10.140.10.28:9001']
labels:
instance: 10.140.10.28