prometheus+grafana基础配置

##服务端##

#安装软件
tar -xf prometheus-2.17.2.linux-386.tar.gz
mv prometheus-2.17.2.linux-386 /usr/local/prometheus

#修改配置文件
vim /usr/local/prometheus/prometheus.yml
static_configs:
    - targets: ['本机IP:9090']      
#修改最后一行,将IP地址改为本机IP
/usr/local/prometheus/promtool check config /usr/local/prometheus/prometheus.yml
#检查配置配置文件是否有语法错误

#编写service文件,使用systemd管理服务
vim /usr/lib/systemd/system/prometheus.service
[Unit]
Description=Prometheus Monitoring System
After=network.target
[Service]
ExecStart=/usr/local/prometheus/prometheus \
  --config.file=/usr/local/prometheus/prometheus.yml \
  --storage.tsdb.path=/usr/local/prometheus/data/
[Install]
WantedBy=multi-user.target
#备注:
#--config.file和--storage.tsdb.path都是prometheus这个程序的参数
#可以通过prometheus -h查看这个程序支持哪些参数
#--config.file参数后面指定该程序使用哪个配置文件启动服务
#--storage.tsdb.path参数后面指定该程序将数据存储在哪个目录下
systemctl  enable prometheus.service --now
#服务端端口为9090
#设置服务器开机自启动服务,并立刻启动该服务

#如果要添加监控节点,可以修改配置文件,在末尾添加节点信息(需要注意格式,参考主机配置)
vim /usr/local/prometheus/prometheus.yml
- job_name: 'node1'                    #监控任务取任意名称
    static_configs:
    - targets: ['被监控节点IP:9100']    #被监控端主机和端口

#如果要添加数据库监控节点,可以修改配置文件,在末尾添加节点信息(需要注意格式,参考主机配置)
vim /usr/local/prometheus/prometheus.yml
- job_name: 'mysql'                    #监控任务取任意名称
    static_configs:
    - targets: ['被监控节点IP:9104']    #被监控端IP端口
#修改配置文件后重启prometheus

##被监控端##

#安装软件
tar -xf node_exporter-1.0.0-rc.0.linux-amd64.tar.gz
mv node_exporter-1.0.0-rc.0.linux-amd64 /usr/local/node_exporter

#编写service文件
vim /usr/lib/systemd/system/node_exporter.service
[Unit]
Description=node_exporter
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/node_exporter/node_exporter
[Install]
WantedBy=multi-user.target
systemctl  enable node_exporter --now 
#被监控端端口为9100

##监控数据库##

#安装软件
tar -xf mysqld_exporter-0.12.1.linux-amd64.tar.gz
mv mysqld_exporter-0.12.1.linux-amd64 /usr/local/mysqld_exporter
#需要添加监控数据库信息
vim /usr/local/mysqld_exporter/.my.cnf
[client]
host=127.0.0.1
port=3306
user=用户名
password=密码

#编写service文件
vim /usr/lib/systemd/system/mysqld_exporter.service
[Unit]
Description=node_exporter
After=network.target
[Service]
ExecStart=/usr/local/mysqld_exporter/mysqld_exporter \
--config.my-cnf=/usr/local/mysqld_exporter/.my.cnf
[Install]
WantedBy=multi-user.target
#备注:
#--config.my-cnf是mysqld_exporter程序的参数,该参数后面指定数据库的配置文件
#可以使用mysqld_exporter -h查看该程序支持哪些参数
#数据库监控端口为9104
systemctl enable mysqld_exporter --now

##监控主机安装grafana##

#安装软件启动服务
yum -y install grafana-6.7.3-1.x86_64.rpm
systemctl enable grafana-server.service --now

#登陆网页
grafana默认启动的是3000端口
重置登录密码
浏览器访问Grafana控制台,http://主机IP:3000
默认用户名和密码都是:admin。

#导入监控模板
#需要在查看监控的主机上导入(不在被监控端)
#页面+号-import-upload.json file,选择对应要使用的监控模板即可