标签:png 本地 create url cpu else 网络 ln -s res
使用Grafana和Prometheus对Linux服务器性能进行监控,主要通过node_exporter进行监控,指标如 CPU、内存、磁盘等。Prometheus通过HTTP协议从远程的机器收集数据并存储Prometheus本地时序数据库。wget https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.linux-amd64.tar.gz
tar xzvf /root/node_exporter-0.18.1.linux-amd64.tar.gz
mv /root/node_exporter-0.18.1.linux-amd64 /opt/prometheus/
创建prometheus用户,用于启动node_exporter.service服务。
useradd -s /sbin/nologin -M prometheus
创建/usr/lib/systemd/system/node_exporter.service文件。
[Unit]
Description=node_exporter
Documentation=https://prometheus.io/
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/opt/prometheus/node_exporter/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target
启动:
systemctl start node_exporter.service
systemctl enable node_exporter.service
每台机器手动安装太繁琐,而且效率低,还要考虑防火墙端口等问题。这里附上各个机器安装node_exporter的脚本。
vim install_node_exporter_prometheus.sh
#!/bin/bash
#Author: Joey-Yu
cat << EOF
+------------------------------------------------------------------+
| ** Welcome to install prometheus node_exporter for CentOS 7 OS|
+------------------------------------------------------------------+
EOF
function format() {
echo -e "\033[32m Success!!! \033[0m\n"
echo "#########################################################"
}
#mkdir dir
echo "set prometheus node-exporter dir"
dir=/opt/prometheus/
if [ ! -d $dir ]; then
mkdir -p /opt/prometheus/
fi
format
#download node_exporter-0.18.1.linux-amd64.tar.gz
echo "download and tar prometheus node_exporter-0.18.1.linux-amd64.tar.gz in root"
#cd /root && wget https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.linux-amd64.tar.gz
node_exporter_file=/root/node_exporter-0.18.1.linux-amd64.tar.gz
if [ ! -f $node_exporter_file ]; then
echo "no node_exporter-0.18.1.linux-amd64.tar.gz in root!"
exit
else
cd /root/
tar xzvf /root/node_exporter-0.18.1.linux-amd64.tar.gz
mv /root/node_exporter-0.18.1.linux-amd64 /opt/prometheus/
format
fi
# useadd prometheus
echo "useadd prometheus user"
useradd -s /sbin/nologin -M prometheus
format
# chown prometheus and ln -s
echo "chown prometheus and ln -s"
cd /opt/prometheus/
chown -R prometheus.prometheus /opt/prometheus/node_exporter-0.18.1.linux-amd64/
ln -s /opt/prometheus/node_exporter-0.18.1.linux-amd64/ /opt/prometheus/node_exporter
format
# config prometheus /usr/lib/systemd/system/node_exporter.service
echo "touch /usr/lib/systemd/system/node_exporter.service"
touch /usr/lib/systemd/system/node_exporter.service
cat > /usr/lib/systemd/system/node_exporter.service << EOF
[Unit]
Description=node_exporter
Documentation=https://prometheus.io/
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/opt/prometheus/node_exporter/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
systemctl start node_exporter.service
systemctl enable node_exporter.service
format
- job_name: ‘node-db‘
file_sd_configs:
- files: [‘/usr/local/prometheus/node-db.yml‘]
refresh_interval: 5s
- job_name: ‘node-pre‘
file_sd_configs:
- files: [‘/usr/local/prometheus/node-pre.yml‘]
refresh_interval: 5s
- job_name: ‘node-application‘
file_sd_configs:
- files: [‘/usr/local/prometheus/node-application.yml‘]
refresh_interval: 5s
- job_name: ‘node-pro‘
file_sd_configs:
- files: [‘/usr/local/prometheus/node-pro.yml‘]
refresh_interval: 5s
从上面配置可看出,不同的应用、不同环境尽量放置一个yml文件中。如应用/usr/local/prometheus/node-application.yml文件:
- targets: ["10.20.1.1:9100"]
labels:
instance: grafana
- targets: ["10.20.1.2:9100"]
labels:
instance: Zabbix-proxy
- targets: ["10.20.1.3:9100"]
labels:
instance: prometheus
修改配置后重启prometheus服务端
systemctl restart prometheus.service
Grafana+Prometheus监控Linux服务器性能
标签:png 本地 create url cpu else 网络 ln -s res
原文地址:https://blog.51cto.com/10874766/2498731