服务器从Win2KR2换成CentOS7了既然是虚拟化平台那就可以把Grafana单独放在一台机子上
准备:
一台全新系统CentOS7
一台要被监控的服务器
(两台服务器可以合并成一台)
一、安装Grafana
vim /etc/yum.repos.d/grafana.repo
没有VIM的自己yum install
插入以下代码以添加grafana源
[grafana]
name=grafana
baseurl=https://packages.grafana.com/enterprise/rpm
repo_gpgcheck=1
enabled=1
gpgcheck=1
gpgkey=https://packages.grafana.com/gpg.key
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
然后使用yum安装grafana
yum install grafana
配置grafana自启
systemctl enable grafana-server
二、被监视服务器配置
从某HUB上下载NODE_EXPORTER、MYSQLD_EXPORTER和PROMETHEUS并上传到服务器备用
将上传的tar包解压到usr/local/prometheus、usr/local/node_exporter、usr/local/mysqld_exporter
配置prometheus自启
cd /etc/systemd/system
vim prometheus.service
输入
[Unit]
Description=Prometheus Monitoring System
Documentation=Prometheus Monitoring System
[Service]
ExecStart=/usr/local/prometheus/prometheus \
--config.file=/usr/local/prometheus/prometheus.yml \
--web.listen-address=:9090
[Install]
WantedBy=multi-user.target
BASH敲
systemctl daemon-reload
systemctl enable prometheus
systemctl start prometheus
完成prometheus的自启设置
cd到/usr/local/mysqld_exporter
vim my.cnf
输入
[client]
host=被监视数据库地址
password=密码
user=账号(root权限)
配置mysqld_exporter自启:
cd /etc/systemd/system
vim mysqld_exporter.service
键入
[Unit]
Description=Mysqld Monitor
After=network.target
[Service]
ExecStart=/usr/local/mysqld_exporter/mysqld_exporter --config.my-cnf=/usr/local/mysqld_exporter/my.cnf --web.listen-address=:9104
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5
TimeoutStopSec=5
KillMode=mixed
[Install]
WantedBy=multi-user.target
BASH敲
systemctl daemon-reload
systemctl enable mysqld_exporter
systemctl start mysqld_exporter
完成mysqld_exporter的自启设置
node_exporter就简单了,直接封装成服务就行
vim node_exporter.service
键入
[Unit]
Description=Node Monitor
After=network.target
[Service]
ExecStart=/usr/local/node_exporter/node_exporter --web.listen-address=:9100
TimeoutStopSec=5
KillMode=mixed
[Install]
WantedBy=multi-user.target
保存
BASH敲
systemctl daemon-reload
systemctl enable node_exporter
systemctl start node_exporter
完成node_exporter的自启设置
三、Prometheus配置
cd到/usr/local/prometheus
vim prometheus.yml
拉到最下面scrape_configs:那里
为了避免缩进的麻烦,删除所有注释(prometheus对缩进貌似敏感)
以下是我的配置,照抄需要把IP换了
# my global config
global:
scrape_interval: 5s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 5s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- job_name: 'web'
static_configs:
- targets: ['192.168.30.51:9100']
- job_name: 'mysqld'
static_configs:
- targets: ['192.168.30.51:9104']
保存
敲service prometheus restart
重启prometheus
四、Grafana配置
Grafana的默认端口是3000,做反向代理直接
ProxyRequests Off
ProxyPass / http://$IP:3000/
ProxyPassReverse / http://$IP:3000/
就行,自己看着换IP
在Grafana面板添加数据源Prometheus,服务器地址应该是http://localhost:9090
自己看着写,连通性测试通过后下一步
在模板面板添加模板,我用的两个模板代码是8919(Node_Exporter)和7362(Mysqld_Exporter)
通过模板代码导入模板后你就能看到你服务器的实时信息了(我的Prometheus设置是5s一更新)
五、仪表板共享(可选)
如果你不想让别人看到仪表板可以不执行这一项
开启匿名访问:
cd /etc/grafana/
你应该能看到一个grafana.ini
vim grafana.ini
在306行左右你就能看到有关匿名访问的设置([auth.anonymous])
一般选项都是被注释掉的,把enable那一段的注释去掉
然后把enable=false改成true
就可以让别人匿名访问你的仪表板了
六、备忘
通过链接分享仪表板的时候可能会带有固定的时间段,如果要分享实时信息需要把时间段的末时间设置为now
友联吗老哥 我看了你的文章写的很好