监控远程Linux
被监控的主机装上node_exporter-1.3.1.linux-amd64.tar.gz
解压缩
[root@localhost ~]# tar xf node_exporter-1.8.1.linux-amd64.tar.gz -C /usr/local/
创建软连接
[root@localhost ~]# ln -sv /usr/local/node_exporter-1.8.1.linux-amd64/ /usr/local/node_exporter
"/usr/local/node_exporter" -> "/usr/local/node_exporter-1.8.1.linux-amd64/"
创建用户
[root@localhost ~]# useradd prometheus -M -s /sbin/nologin
修改权限
[root@localhost ~]# -R prometheus.prometheus /usr/local/node_exporter/*
创建服务脚本
[root@localhost ~]# vim /usr/lib/systemd/system/node_exporter.service
[Unit]
Description=node_exporter
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/node_exporter/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target
添加系统服务
[root@localhost ~]# vim /usr/lib/systemd/system/node_exporter.service
[Unit]
Description=node_exporter
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/node_exporter/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target
启动服务
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl start node_exporter.service
[root@localhost ~]# systemctl status node_exporter.service
[root@localhost ~]# ss -lnupt | grep 9100
tcp LISTEN 0 128 :::9100 :::* users:(("node_exporter",pid=1417,fd=3))
验证
通过浏览器访问http://被监控端IP:9100/metrics就可以查看到node_exporter在被监控端收集的监控信息
添加Prometheus相关配置
注意:在server端配置
在主配置最后加上下面三行
[root@localhost ~]# cd /usr/local/prometheus
[root@localhost prometheus]# vim prometheus.yml
这里的IP是被监控的主机IP
重启Prometheus
采集到了
监控mysql数据库
解压mysqld_exporter组件
[root@localhost ~]# tar xf mysqld_exporter-0.15.1.linux-amd64.tar.gz -C /usr/local/
创建软连接
[root@localhost ~]# ln -sv /usr/local/mysqld_exporter-0.15.1.linux-amd64/ /usr/local/mysqld_exporter
"/usr/local/mysqld_exporter" -> "/usr/local/mysqld_exporter-0.15.1.linux-amd64/"
安装mariadb数据库,并授权
[root@localhost ~]# yum install -y mariadb-server
设置开机自启
[root@localhost ~]# systemctl enable --now mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
授权用户
mysql -e "grant select,replication client,process ON *.* to 'mysql_monitor'@'localhost' identified by '123'"
(注意:授权ip为localhost,因为不是prometheus服务器来直接找mariadb获取数据,而是prometheus 服务器找mysql_exporter,mysql_exporter再找mariadb。所以这个localhost是指的 mysql_exporter的IP)
创建一个mariadb配置文件,写上连接的用户名与密码(和上面的授权的用户名和密码要对应)
[root@localhost ~]# vim /usr/local/mysqld_exporter/.my.cnf
[client]
user=mysql_monitor
password=123
添加系统服务
[root@localhost ~]# vim /usr/lib/systemd/system/mysql_exporter.service
[Unit]
Description=mysqld_exporter
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/mysqld_exporter/mysqld_exporter --config.my-cnf=/usr/local/mysqld_exporter/.my.cnf
Restart=on-failure
[Install]
WantedBy=multi-user.target
启动服务
[root@localhost mysqld_exporter]# chown -R prometheus.prometheus /usr/local/mysqld_exporter/*
[root@localhost mysqld_exporter]# systemctl daemon-reload
[root@localhost mysqld_exporter]# systemctl start mysql_exporter.service
[root@localhost mysqld_exporter]# netstat -lnupt | grep 9104
tcp6 0 0 :::9104 :::* LISTEN 2004/mysqld_exporte
在server端一样修改 在底部多添加三行
- job_name: "mariadb"
static_configs:
- targets: ["192.168.136.148:9104"]
重启Prometheus