普罗米修斯!Ubuntu下prometheus监控软件安装使用
*Prometheus* 是一个开源的服务监控系统和时间序列数据库
官方网站:prometheus.io
一、安装prometheus
cd /usr/local/ #进入安装目录
wget https://github.com/prometheus/prometheus/releases/download/v2.27.1/prometheus-2.27.1.linux-amd64.tar.gz #wget下载prometheus安装包
tar xf prometheus-2.27.1.linux-amd64.tar.gz #解压
ln -s prometheus-2.27.1.linux-amd64 prometheus #建立软链接
chown -R root.root prometheus-2.27.1.linux-amd64 #授予root权限普罗米修斯默认前台启动,构建启动脚本完成后台启动:
vim /lib/systemd/system/prometheus.service
[Unit]
Description=Prometheus server daemon
After=network.target[Service]
Type=simple
User=root
Group=root
ExecStart="/usr/local/prometheus/prometheus" --config.file="/usr/local/prometheus/prometheus.yml" --storage.tsdb.path="/usr/local/prometheus/data" --storage.tsdb.retention=5d --web.console.
templates="/usr/local/prometheus/consoles" --web.console.libraries="/usr/local/prometheus/console_libraries" --web.max-connections=512 --web.external-url="http://192.168.149.133:9090" --web.listen-address="0.0.0.0:9090"Restart=on-failure[Install]
WantedBy=multi-user.targetsystemctl daemon-reload
systemctl restart prometheus.service
systemctl status prometheus.service
启动完成可访问页面 默认为9090端口
二、添加监控服务器选项(监控主机)
在配置文件中加入监控配置信息,比如要监控xxx.xxx.xxx.134服务器,如下配置
在xxx.xxx.xxx.134安装node_exporter插件,在普罗米修斯官网就可下载
xxx.xxx.xxx.134主机进行以下操作:
cd /usr/local #进入安装插件目录
wget https://github.com/prometheus/node_exporter/releases/download/v1.1.2/node_exporter-1.1.2.linux-amd64.tar.gz #wget下载插件安装包
tar xf node_exporter-1.1.2.linux-amd64.tar.gz #解压
ln -s node_exporter-1.1.2.linux-amd64 node_exporter #建立软连接
chown -R root.root node_exporter-1.1.2.linux-amd64 #授予root权限构建node_exporter后台启动脚本:
vim /lib/systemd/system/node_exporter.service
[Unit]
Description=node_exporter
Documentation=https://prometheus.io/
After=network.target[Service]
Type=simple
ExecStart=/usr/local/node_exporter/node_exporter \
--collector.mountstats \
--collector.systemd \
--collector.ntp \
--collector.tcpstat
ExecReload=/bin/kill -HUP $MAINPID
TimeoutStopSec=2s
Restart=always[Install]
WantedBy=multi-user.targetsystemctl daemon-reload #加载配置文件
systemctl start node_exporter.service #启动node插件
systemctl status node_exporter.service #查看运行状态回到普罗米修斯主机,修改配置文件(只修改以下蓝色部分内容):
vim /usr/local/prometheus/prometheus.yml......
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'prometheus'# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.static_configs:
- targets: ['localhost:9090']- job_name: 'node_exporter'
metrics_path '/metrics'
static_configs:
- targets:
- 'xxx.xxx.xxx.134:9100'
......
systemctl daemon-reload
systemctl restart prometheus.service
重启完成可访问页面查看
三、添加监控应用级选项(监控各种应用,以mysql为例)
在配置文件中加入监控配置信息,比如要监控xxx.xxx.xxx.135服务器上的mysql,如下配置
在xxx.xxx.xxx.135安装mysqld_exporter插件,在普罗米修斯官网就可下载
在xxx.xxx.xxx.135主机操作:
cd /usr/local
wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.13.0/mysqld_exporter-0.13.0.linux-amd64.tar.gz
tar xf mysqld_exporter-0.13.0.linux-amd64.tar.gz
ln -s mysqld_exporter-0.13.0.linux-amd64 mysqld_exporter
chown -R root.root mysqld_exporter-0.13.0.linux-amd64构建mysqld_exporter后台启动脚本:
vim /lib/systemd/system/mysqld_exporter.service
[Unit]
Description=Prometheus MySQL Exporter
After=network.target[Service]
Type=simple
Restart=always
User=root
Group=root
ExecStart=/usr/local/mysqld/mysqld_exporter --config.my-cnf=/usr/local/mysqld/mysqld_exporter.cnf --collect.global_status --collect.auto_increment.columns --collect.info_schema.processlist --collect.binlog_size
--collect.info_schema.tablestats --collect.global_variables --collect.info_schema.innodb_metrics --collect.info_schema.query_response_time --collect.info_schema.userstats --collect.info_schema.tables --collect.perf_schema.tablelocks --collect.perf_schema.file_events --collect.perf_schema.eventswaits --collect.perf_schema.indexiowaits --collect.perf_schema.tableiowaits --collect.slave_status --web.listen-address=0.0.0.0:9104
[Install]
WantedBy=multi-user.target创建mysql进入启动配置文件(同时确保mysql数据库有root用户并授权了远程可登录):
vim /usr/local/mysqld_exporter/mysqld_exporter.cnf
[client]
user=root
password=123456
检测mysql配置文件是否正确:
./mysqld_exporter --config.my-cnf="./mysqld_exporter.cnf"systemctl daemon-reload #加载配置
systemctl start mysqld_exporter #启动mysqld监控插件回到普罗米修斯主机,修改配置文件:
vim /usr/local/prometheus/prometheus.yml......
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'prometheus'# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.static_configs:
- targets: ['localhost:9090']- job_name: 'node_exporter'
metrics_path '/metrics'
static_configs:
- targets:
- 'xxx.xxx.xxx.134:9100'- job_name: 'mysqld_exporter'
metrics_path '/metrics'
static_configs:
- targets:
- 'xxx.xxx.xxx.135:9104'
......systemctl daemon-reload #加载配置
systemctl restart prometheus.service #重启prometheus服务访问xxx.xxx.xxx.xxx:9090就可看到主机级和mysql的监控情况
注意:各类监控服务配置和上面配置大同小异,各类监控服务插件在prometheus官网就可直接下载使用:https://prometheus.io/