cd /usr/local/prometheus wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.14.0/mysqld_exporter-0.14.0.linux-amd64.tar.gz
tar -zxvf mysqld_exporter-0.14.0.linux-amd64.tar.gz mv -f mysqld_exporter-0.14.0.linux-amd64 mysqld_exporter
登录需要监控的数据库,创建一个用户仅用于mysqld_exporter
#给予所有权限的创建,仅允许某个ip访问 grant all on *.* TO 'mysqld_exporter'@"192.168.102.130" identified by "mysqld_exporter"; #刷新权限 flush privileges; #退出登陆 quit;
创建数据库连接配置文件
touch /usr/local/prometheus/mysqld_exporter/mysql.cnf vim /usr/local/prometheus/mysqld_exporter/mysql.cnf
[client] user=mysqld_exporter password=mysqld_exporter host=192.168.102.129 port=3306
nohup /usr/local/prometheus/mysqld_exporter/mysqld_exporter --log.level=info --config.my-cnf="/usr/local/prometheus/mysqld_exporter/mysql.cnf" >/usr/local/prometheus/mysqld_exporter/mysqld_exporter.log 2>&1 &
systemctl status firewalld firewall-cmd --zone=public --add-port=9104/tcp --permanent firewall-cmd --reload firewall-cmd --zone=public --query-port=9104/tcp
浏览器访问
http://IP:9104/
把 mysqld_exporter 注册到consul
注册服务(重启失效)
curl --request PUT --data '{"ID":"mysqld_exporter","Name":"mysqld_exporter","Tags":["primary","v1"],"address":"localhost","port":9104,"check":{"http":"http://localhost:9104/metrics","interval":"10s"}}' http://localhost:8500/v1/agent/service/register
删除服务(url最后面为服务ID)
curl --request PUT http://localhost:8500/v1/agent/service/deregister/mysqld_exporter
修改 prometheus 的 prometheus.yml 文件,然后重启prometheus
# my global config global: scrape_interval: 15s # 将抓取间隔设置为每 15 秒。 默认为每 1 分钟。 evaluation_interval: 15s # 每 15 秒评估一次规则。 默认值为每 1 分钟。 # scrape_timeout 设置为全局默认值(10 秒)。 # Alertmanager configuration alerting: alertmanagers: - static_configs: - targets: # 如果有alertmanager,则需要取消下面这一行注释 # - alertmanager:9093 # 加载规则一次并根据全局“evaluation_interval”定期评估它们。 rule_files: # - "first_rules.yml" # - "second_rules.yml" # 一个抓取配置,只包含一个要抓取的端点: # 这里是prometheus本身。 scrape_configs: # job名称作为标签 `job=<job_name>` 添加到从此配置中抓取的任何时间序列中。 - job_name: "prometheus" # metrics_path defaults to '/metrics' # scheme defaults to 'http'. static_configs: - targets: ["localhost:9090"] #可以添加新的job,需要设置job名称和对应的IP、端口 - job_name: "consul_exporter" static_configs: - targets: ["localhost:9107"] #可以添加新的job,需要设置job名称和对应的IP、端口 - job_name: "node_exporter" static_configs: - targets: ["localhost:9100"] - job_name: "mysqld_exporter" static_configs: - targets: ["localhost:9104"]
touch mysqld_exporter.sh && chmod +x mysqld_exporter.sh
#!/bin/bash #用于安装mysqld_exporter #版本号 MYSQLD_EXPORTER_VER=0.15.0 #安装位置 MYSQLD_EXPORTER_DIR=/app/mysqld_exporter #安装包存放路径 INSTALL_PACKAGE=/app/install #日志文件 LOG_FILE=/tmp/mysqld_exporter.log # MYSQLD_EXPORTER的用户名 MYSQLD_EXPORTER_USER=prometheus # MYSQLD_EXPORTER监听端口 MYSQLD_EXPORTER_PORT=9104 check_err() { if [ $? -ne 0 ]; then echo "$1" exit 1 else echo "$2" fi } start() { [ ! -d /app ] && mkdir -p /app #检查是否存在wget命令,没有则安装 type wget &> /dev/null if [ $? -ne 0 ]; then yum install wget -y check_err "\033[31myum安装依赖包 wget 失败,请检查\033[0m" "\033[36myum安装依赖包 wget 成功\033[0m" fi #检查是否存在netstat命令,没有则安装 type netstat &> /dev/null if [ $? -ne 0 ]; then yum install net-tools -y check_err "\033[31myum安装依赖包 net-tools 失败,请检查\033[0m" "\033[36myum安装依赖包 net-tools 成功\033[0m" fi netstat -tln | grep ":$MYSQLD_EXPORTER_PORT" CHECK_RESULT=$? if [ $CHECK_RESULT -eq 0 ];then echo "MYSQLD_EXPORTER 已启动,请勿重复启动应用!" exit 1 fi echo "正在启动 MYSQLD_EXPORTER....." [ ! -d $INSTALL_PACKAGE ] && mkdir -p $INSTALL_PACKAGE [ ! -f $INSTALL_PACKAGE/mysqld_exporter-${MYSQLD_EXPORTER_VER}.linux-amd64.tar.gz ] && wget -O $INSTALL_PACKAGE/mysqld_exporter-${MYSQLD_EXPORTER_VER}.linux-amd64.tar.gz https://github.com/prometheus/mysqld_exporter/releases/download/v${MYSQLD_EXPORTER_VER}/mysqld_exporter-${MYSQLD_EXPORTER_VER}.linux-amd64.tar.gz [ ! -d $MYSQLD_EXPORTER_DIR ] && mkdir -p $MYSQLD_EXPORTER_DIR count=`ls $MYSQLD_EXPORTER_DIR | wc -l` if [ $count -gt 0 ]; then echo "目录 $MYSQLD_EXPORTER_DIR 不为空,不再重复解压压缩包" else tar -xvzf $INSTALL_PACKAGE/mysqld_exporter-${MYSQLD_EXPORTER_VER}.linux-amd64.tar.gz --strip-components 1 -C $MYSQLD_EXPORTER_DIR fi cat /etc/group | grep $MYSQLD_EXPORTER_USER if [ $? -ne 0 ]; then groupadd -g 10002 $MYSQLD_EXPORTER_USER fi cat /etc/passwd | grep $MYSQLD_EXPORTER_USER if [ $? -ne 0 ]; then useradd -u 10002 -g $MYSQLD_EXPORTER_USER -d /home/$MYSQLD_EXPORTER_USER -m $MYSQLD_EXPORTER_USER fi chown -R $MYSQLD_EXPORTER_USER:$MYSQLD_EXPORTER_USER $MYSQLD_EXPORTER_DIR [ ! -f $LOG_FILE ] && touch $LOG_FILE chown -R $MYSQLD_EXPORTER_USER:$MYSQLD_EXPORTER_USER $LOG_FILE if [ ! -f $MYSQLD_EXPORTER_DIR/mysql.cnf ]; then if [[ ! -n "$1" ]] ; then echo "连接数据库的用户名不能为空!" exit 1 fi if [[ ! -n "$2" ]] ; then echo "连接数据库的密码不能为空!" exit 1 fi if [[ ! -n "$3" ]] ; then echo "数据库主机不能为空!" exit 1 fi if [[ ! -n "$4" ]] ; then echo "数据库端口不能为空!" exit 1 fi touch $MYSQLD_EXPORTER_DIR/mysql.cnf cat > $MYSQLD_EXPORTER_DIR/mysql.cnf << EOF [client] user=$1 password=$2 host=$3 port=$4 EOF fi chown -R $MYSQLD_EXPORTER_USER:$MYSQLD_EXPORTER_USER $MYSQLD_EXPORTER_DIR/mysql.cnf # 切换用户 su - $MYSQLD_EXPORTER_USER << EOF # 不加反斜杠,取的值是切换用户前的 nohup $MYSQLD_EXPORTER_DIR/mysqld_exporter --log.level=info --config.my-cnf="$MYSQLD_EXPORTER_DIR/mysql.cnf" >> $LOG_FILE 2>&1 & #每2s检查一次监听端口是否启动,总共检查30次共1分钟,超时未启动则退出 CHECK_RESULT=1 CHECK_COUNT=30 while [ \$CHECK_RESULT -ne 0 ];do if [ \$CHECK_COUNT -gt 0 ];then echo "等待监听端口启动..." sleep 2 netstat -tln | grep ":$MYSQLD_EXPORTER_PORT" CHECK_RESULT=\$? CHECK_COUNT=\$((\$CHECK_COUNT-1)) else echo "ERROR: 启动$MYSQLD_EXPORTER_PORT端口超时,请检查!" exit 1 fi done echo "MYSQLD_EXPORTER 启动成功!" EOF #检查防火墙是否已启动,如果启动则开放端口 systemctl status firewalld > /dev/null if [ $? -eq 0 ];then firewall-cmd --add-port=$MYSQLD_EXPORTER_PORT/tcp --permanent firewall-cmd --reload fi } stop(){ netstat -tln | grep ":$MYSQLD_EXPORTER_PORT" check_err "MYSQLD_EXPORTER 未启动!" "MYSQLD_EXPORTER 正在运行,开始停止应用....." su - $MYSQLD_EXPORTER_USER << EOF PROCESS=`ps aux | grep $MYSQLD_EXPORTER_DIR/mysqld_exporter | grep -v "grep" | awk '{print $2}'` for i in \${PROCESS} do echo "Kill the MYSQLD_EXPORTER process [ \$i ]" kill -9 \$i done EOF CHECK_RESULT=0 CHECK_COUNT=30 while [ $CHECK_RESULT -eq 0 ];do if [ $CHECK_COUNT -gt 0 ];then echo "等待监听端口关闭..." sleep 2 netstat -tln | grep ":$MYSQLD_EXPORTER_PORT" CHECK_RESULT=$? CHECK_COUNT=$(($CHECK_COUNT-1)) else echo "ERROR: 关闭$MYSQLD_EXPORTER_PORT端口超时,请检查!" exit 1 fi done echo "关闭 MYSQLD_EXPORTER 成功!" } restart(){ stop start } status(){ su $MYSQLD_EXPORTER_USER -c "ps aux | grep $MYSQLD_EXPORTER_DIR/mysqld_exporter | grep -v \"grep\" | grep -v \"su $MYSQLD_EXPORTER_USER\"" } remove(){ rm -rf $LOG_FILE rm -rf $MYSQLD_EXPORTER_DIR #userdel -r $MYSQLD_EXPORTER_USER } reset(){ if [ ! -f $MYSQLD_EXPORTER_DIR/mysql.cnf ]; then echo "$MYSQLD_EXPORTER_DIR/mysql.cnf 文件不存在!" exit 1 fi if [[ ! -n "$1" ]] ; then echo "连接数据库的用户名不能为空!" exit 1 fi if [[ ! -n "$2" ]] ; then echo "连接数据库的密码不能为空!" exit 1 fi if [[ ! -n "$3" ]] ; then echo "数据库主机不能为空!" exit 1 fi if [[ ! -n "$4" ]] ; then echo "数据库端口不能为空!" exit 1 fi ROW_USER=`cat $MYSQLD_EXPORTER_DIR/mysql.cnf | grep -n -w "user" | awk -F ":" '{print $1}'` sed -i "${ROW_USER}"' s/=.*$/='"$1"'/g' $MYSQLD_EXPORTER_DIR/mysql.cnf; ROW_PASSWORD=`cat $MYSQLD_EXPORTER_DIR/mysql.cnf | grep -n -w "password" | awk -F ":" '{print $1}'` sed -i "${ROW_PASSWORD}"' s/=.*$/='"$2"'/g' $MYSQLD_EXPORTER_DIR/mysql.cnf; ROW_HOST=`cat $MYSQLD_EXPORTER_DIR/mysql.cnf | grep -n -w "host" | awk -F ":" '{print $1}'` sed -i "${ROW_HOST}"' s/=.*$/='"$3"'/g' $MYSQLD_EXPORTER_DIR/mysql.cnf; ROW_PORT=`cat $MYSQLD_EXPORTER_DIR/mysql.cnf | grep -n -w "port" | awk -F ":" '{print $1}'` sed -i "${ROW_PORT}"' s/=.*$/='"$4"'/g' $MYSQLD_EXPORTER_DIR/mysql.cnf; restart } usage () { echo " " echo "请输入 start 启动 MYSQLD_EXPORTER。(首次启动需要指定监听的数据库,示例:./mysqld_exporter.sh start \"mysqld_exporter\" \"mysqld_exporter\" \"192.168.102.129\" \"3306\"。start 后面第一个参数是连接数据的用户名,第二个参数是用户对应的密码,第三个是数据库的主机,第四个是数据库的端口)" echo "请输入 stop 停止 MYSQLD_EXPORTER。" echo "请输入 status 获取 MYSQLD_EXPORTER 状态。" echo "请输入 restart 重启 MYSQLD_EXPORTER。" echo "请输入 remove 删除 MYSQLD_EXPORTER。" echo "请输入 reset 重置数据库连接信息。(示例:./mysqld_exporter.sh reset \"mysqld_exporter\" \"mysqld_exporter\" \"192.168.102.129\" \"3306\"。reset 后面第一个参数是连接数据的用户名,第二个参数是用户对应的密码,第三个是数据库的主机,第四个是数据库的端口)" echo " " } INPUT_ACTIVE=$1 ACTIVE=${INPUT_ACTIVE:=start} case ${ACTIVE} in start) start $2 $3 $4 $5 ;; stop) stop ;; status) status ;; restart) restart ;; remove) remove ;; reset) reset $2 $3 $4 $5 ;; *) usage ;; esac