As the saying goes, a system without monitoring is running naked, and a good monitoring is the third hand and the third eye of the operation and maintenance personnel. This article will use prometheus and Grafana to build a monitoring system to monitor the host and database ( MySQL , Redis ).
1. Underwear Grafana
Grafana is a visualization panel (Dashboard) with a very beautiful chart and layout display, a full-featured measurement dashboard and a graph editor, and supports Graphite, zabbix, InfluxDB, Prometheus and other data sources.
1.1 Download and install
Download link: https://grafana.com/grafana/download
After downloading, you can install it:
sudo dpkg -i grafana_5.0.4_amd64.deb
Start to view status:
sudo systemctl start grafana-server
sudo systemctl status grafana-server
Next, set up the prometheus user and directory:
vagrant@vagrant:/htdocs/share$ sudo useradd --no-create-home --shell /bin/false prometheus
vagrant@vagrant:/htdocs/share$ sudo useradd --no-create-home --shell /bin/false node_exporter
vagrant@vagrant:/htdocs/share$ sudo mkdir /etc/prometheus
vagrant@vagrant:/htdocs/share$ sudo mkdir /var/lib/prometheus
vagrant@vagrant:/htdocs/share$ sudo chown prometheus:prometheus /etc/prometheus
vagrant@vagrant:/htdocs/share$ sudo chown prometheus:prometheus /var/lib/prometheus
Next download prometheus:
vagrant@vagrant:/htdocs/share$ cd
vagrant@vagrant:~$ curl -LO https://github.com/prometheus/prometheus/releases/download/v2.0.0/prometheus-2.0.0.linux-amd64.tar.gz
Unzip and enter the directory:
vagrant@vagrant:/htdocs/share$ sudo cp prometheus-2.0.0.linux-amd64/prometheus /usr/local/bin/
vagrant@vagrant:/htdocs/share$ sudo cp prometheus-2.0.0.linux-amd64/promtool /usr/local/bin/
vagrant@vagrant:/htdocs/share$ sudo chown prometheus:prometheus /usr/local/bin/prometheus
vagrant@vagrant:/htdocs/share$ sudo chown prometheus:prometheus /usr/local/bin/promtool
vagrant@vagrant:/htdocs/share$ sudo cp -r prometheus-2.0.0.linux-amd64/consoles /etc/prometheus
vagrant@vagrant:/htdocs/share$ sudo cp -r prometheus-2.0.0.linux-amd64/console_libraries /etc/prometheus
vagrant@vagrant:/htdocs/share$ sudo chown -R prometheus:prometheus /etc/prometheus/consoles
vagrant@vagrant:/htdocs/share$ sudo chown -R prometheus:prometheus /etc/prometheus/console_libraries
Add monitoring task:
The contents of /etc/prometheus/prometheus.yml are as follows:
global:
scrape_interval: 15s
scrape_configs:
- job_name: 'prometheus'
scrape_interval: 5s
static_configs:
- targets: ['localhost:9090']
- job_name: 'Mysql'
static_configs:
- targets: ['192.168.0.200:9104']
labels:
instance: '192.168.0.200'
- job_name: 'redis'
static_configs:
- targets: ['192.168.0.200:9121']
labels:
instance: '127.0.0.1'
The contents of /etc/systemd/system/prometheus.service are as follows:
[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target
[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=/usr/local/bin/prometheus \
--config.file /etc/prometheus/prometheus.yml \
--storage.tsdb.path /var/lib/prometheus/ \
--web.console.templates=/etc/prometheus/consoles \
--web.console.libraries=/etc/prometheus/console_libraries
[Install]
WantedBy=multi-user.target
Save the restart configuration:
vagrant@vagrant:/htdocs/share$ sudo systemctl daemon-reload
vagrant@vagrant:/htdocs/share$ sudo systemctl start prometheus
vagrant@vagrant:/htdocs/share$ sudo systemctl enable prometheus
Open the home page: http://192.168.0.200:3000
Default password: admin/admin
http://192.168.0.200:9090/graph
Add data source:
Import the Kanban template separately:
$ tar xvfz mysqld_exporter-0.11.0.linux-amd64.tar.gz
$ cd mysqld_exporter-0.11.0.linux-amd64/
$ sudo vim my.cnf
The content is as follows:
[client]
user=exporter
password=123456
port=3306
After saving, run:
$
nohup ./mysqld_exporter --config.my-cnf="my.cnf" &
download:
wget https://github.com/oliver006/redis_exporter/releases/download/v0.13/redis_exporter-v0.13.l
Unzip:
tar -xvf redis_exporter-v0.13.linux-amd64.tar.gz
Download the prometheus-redis_rev1.json template of grafana's redis:
wget https://grafana.com/api/dashboards/763/revisions/1/download
Import the json template in grafana:
Start redis_exporter:
## 无密码
./redis_exporter redis//192.168.0.200:6379 &
## 有密码
./redis_exporter -redis.addr 192.168.0.200:6379 -redis.password 123456
Download my2.sql:
git clone https://github.com/john1337/my2Collector.git
After the download is complete, import the database.
You can see these monitors in Kanban: