Monitoring with Prometheus & Grafana

Tags: grafana, prometheus, monitoring
Published: 2021-08-10 19:45:02

How set a monitoring system with prometheus, node_exporter and grafana dashboard.

N.B: this tutorial is based on UBUNTU

### Prometheus

create prometheus user:
```
sudo useradd --no-create-home --shell /bin/false prome
```

create directories
```
sudo mkdir /etc/prometheus

sudo mkdir /var/lib/prometheus
```

download prometheus on your server:
```
wget https://github.com/prometheus/prometheus/releases/download/v2.29.0-rc.1/prometheus-2.29.0-rc.1.linux-amd64.tar.gz
```

extract the tar file
```
tar xvfz prometheus-*.tar.gz
```

enter in the folder:
```
cd prometheus-*

```
edit prometheus.yml
```
vim prometheus.yml
```

copy prometheus folder (not the root folder but the folder inside the root folder) in:
```
sudo cp -rf prometheus /usr/local/bin/

sudo cp -rf promtool /usr/local/bin/
```

set permission:
```
sudo chown prome:prome /usr/local/bin/prometheus

sudo chown prome:prome /usr/local/bin/promtool

```

copy files:
```
sudo cp -rf consoles /etc/prometheus

sudo cp -rf console_libraries/ /etc/prometheus/
```

set permissions in /etc

```
sudo chown -R prome:prome /etc/prometheus/consoles

sudo chown -R prome:prome /etc/prometheus/console_libraries

```

copy prometheus.yml
```
sudo cp prometheus.yml /etc/prometheus/prometheus.yml 
```
start prometheus
```
./prometheus --config.file=prometheus.yml
```

create service file:
```
sudo vim /etc/systemd/system/prometheus.service
```

and add this code:
```
[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target

[Service]
User=prome
Group=prome
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
```


reload daemon
```
sudo systemctl daemon-reload
```

system command:
```
sudo systemctl start prometheus

sudo systemctl enable prometheus

sudo systemctl status prometheus



```
check:

http://your-machine-ip:9090


### node exporter

download node exportyet:

```
wget https://github.com/prometheus/node_exporter/releases/download/v1.2.2/node_exporter-1.2.2.linux-amd64.tar.gz
```

extract:
```
tar xvfz node_exporter-1.2.2.linux-amd64.tar.gz

```

enter in the extracted folder:
```
 cd node_exporter-1.2.2.linux-amd64
```
run node exportyer
```
./node_exporter
```

check metrics on:
http://your-vps-ip:9100


back to prometheus.yml and add ip and port node_exporter
```
 - job_name: "node exporter test"
    static_configs:
      - targets: ['localhost:9100']

```

# node exporter as service

create user:
```
sudo useradd -rs /bin/false node_exporter
```

copy node exporter in bin folder
```
sudo cp -rf node_exporter-1.2.2.linux-amd64/node_exporter /usr/local/bin
```

create service file
```
vim /etc/systemd/system/node_exporter.service
```

```
[Unit]
Description=Node Exporter
After=network.target

[Service]
User=node_exporter
Group=node_exporter
Type=simple
ExecStart=/usr/local/bin/node_exporter

[Install]
WantedBy=multi-user.target
```

launch sevice commands:
```
sudo systemctl daemon-reload
sudo systemctl start node_exporter
sudo systemctl enable node_exporter
sudo systemctl status node_exporter
```

### grafana
install dependency library:
```
sudo apt-get install -y adduser libfontconfig1
```
download deb
```
wget https://dl.grafana.com/oss/release/grafana_8.1.0_amd64.deb
```

install:
```
sudo dpkg -i grafana_8.1.0_amd64.deb
```

check:

http://your-vps-ip:3000/login