# Smart Monitoring with Prometheus & Grafana

Grafana is an open-source analytics and visualization platform that connects to data sources, such as Prometheus, to create dashboards, alerts, and visualize real-time performance metrics.

Prometheus is an open-source monitoring and alerting toolkit that collects time-series metrics from targets like Node Exporter, stores them locally, supports PromQL for querying, and integrates with tools like Grafana for visualization

Node Exporter is a Prometheus exporter that exposes hardware and operating system metrics, including CPU usage, memory, disk and filesystem statistics, network I/O, and system load.

## Prerequisite

* Ensure that the Grafana server is up and running. If it's not, refer to this [article](https://docs.neevcloud.com/neevcloud-guide/neevcloud-knowledgebase/set-up-grafana/install-grafana-and-prometheus-on-ubuntu-22.04)

Now, set up Prometheus and Node Exporter on another node and visualize the data on the Grafana dashboard.

## Create a Prometheus System User

Prometheus should run as a non-login system user:

```
sudo useradd --system --no-create-home --shell /bin/false Prometheus
```

Then create a directory

```
sudo mkdir prometheus
```

Download and install the Prometheus

```
wget https://github.com/prometheus/prometheus/releases/download/v2.47.1/prometheus-2.47.1.linux-amd64.tar.gz
```

Extract Prometheus files, move them, and create directories:

```
tar -xvf prometheus-2.47.1.linux-amd64.tar.gz
cd prometheus-2.47.1.linux-amd64/
sudo mkdir -p /data /etc/prometheus
sudo mv prometheus promtool /usr/local/bin/
sudo mv consoles/ console_libraries/ /etc/prometheus/
sudo mv prometheus.yml /etc/prometheus/prometheus.yml
```

Ensure Prometheus owns its files:

```
sudo chown -R prometheus:prometheus /etc/prometheus/ /data/    
```

Create a systemd unit configuration file for Prometheus:&#x20;

```
vi /etc/systemd/system/prometheus.service
```

```
[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target
StartLimitIntervalSec=500
StartLimitBurst=5

[Service]
User=prometheus
Group=prometheus
Type=simple
Restart=on-failure
RestartSec=5s
ExecStart=/usr/local/bin/prometheus \
  --config.file=/etc/prometheus/prometheus.yml \
  --storage.tsdb.path=/data \
  --web.console.templates=/etc/prometheus/consoles \
  --web.console.libraries=/etc/prometheus/console_libraries \
  --web.listen-address=0.0.0.0:9090 \
  --web.enable-lifecycle

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

Reload systemd and restart the service:

```
sudo systemctl enable prometheus
sudo systemctl start Prometheus
sudo systemctl status Prometheus
```

You can access Prometheus in a web browser using your server’s IP and port 9090

```
http://your-server-ip:9090
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2Fb9cFEGcmKjwSgvtdyh82%2Fimage.png?alt=media&#x26;token=ae72f9bb-94ef-4530-ab1e-15210e43f1cf" alt=""><figcaption></figcaption></figure>

## Install Node Exporter

Create User

```
sudo useradd --system --no-create-home --shell /bin/false node_exporter
```

Create a directory

```
sudo mkdir node-exporter
```

Download and install the Node-Exporter

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

Extract Prometheus files, move them, and create directories:

```
tar -xvf node_exporter-1.6.1.linux-amd64.tar.gz
sudo mv node_exporter-1.6.1.linux-amd64/node_exporter /usr/local/bin/
```

Create a systemd unit configuration file for Node-exporter

```
vi /etc/systemd/system/node_exporter.service
```

```
[Unit]
Description=Node Exporter
Wants=network-online.target
After=network-online.target
StartLimitIntervalSec=500
StartLimitBurst=5

[Service]
User=node_exporter
Group=node_exporter
Type=simple
Restart=on-failure
RestartSec=5s
ExecStart=/usr/local/bin/node_exporter --collector.logind

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

Reload systemd and restart the service:

```
sudo systemctl enable node_exporter
sudo systemctl start node_exporter
sudo systemctl status node_exporter
```

You can access Node exporter in a web browser using your server’s IP and port 9100

```
http://your-server-ip:9100
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FXX2wFlGoKqxqB9eRR0Vz%2Fimage.png?alt=media&#x26;token=a97a07b6-572c-4693-a161-cde8d6bff92b" alt=""><figcaption></figcaption></figure>

**Prometheus Configuration:**

```
vi /etc/prometheus/prometheus.yml
```

```
global:
  scrape_interval: 15s

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']

  - job_name: 'node_exporter'
    static_configs:
      - targets: ['localhost:9100']

```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2F58Zw7E3wAsvx6Jm7aTn7%2Fimage.png?alt=media&#x26;token=11f711d9-31c5-4d6f-bc26-baca88f55665" alt=""><figcaption></figcaption></figure>

```
systemctl restart prometheous 
```

To install Grafana, follow this [link](https://docs.neevcloud.com/neevcloud-guide/neevcloud-knowledgebase/set-up-grafana/install-grafana-and-prometheus-on-ubuntu-22.04).

Now open the Grafana dashboard

```
http://your-grafana-server-ip:3000
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FFwijKnlCFscA1ltdLxnD%2Fimage.png?alt=media&#x26;token=811ea532-651d-41c5-9c3c-2db3bb5c69f9" alt=""><figcaption></figcaption></figure>

Add Prometheus Data Source:

To visualize metrics, you need to add a data source. Follow these steps:

Click on the Add new data source

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FN0eg4eaVL5Dp3UqcDUjw%2Fimage.png?alt=media&#x26;token=ec6a95c4-48bc-4432-8f3e-665348372771" alt=""><figcaption></figcaption></figure>

Now select the Prometheus

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FBc2GVpMWQ4rPZscYVwZD%2Fimage.png?alt=media&#x26;token=416c9ffa-34b0-4cae-a1ee-6b72ffc58e2e" alt=""><figcaption></figcaption></figure>

Set the name of your server, enter your worker's IP address with the corresponding port, and then click Save and Test.

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FKPwr9YXHQtuRlUeZ6lLx%2Fimage.png?alt=media&#x26;token=3c92eb99-ff60-4ace-b6a4-ce7e732f8ef2" alt=""><figcaption></figcaption></figure>

Now, go back to the dashboard section and add a new dashboard.

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FIItLmFVEuZ47eebjZqOX%2Fimage.png?alt=media&#x26;token=14f61571-ee82-4e90-bc62-b02804f514e0" alt=""><figcaption></figcaption></figure>

For the node exporter ID, navigate to the node exporter [website ](https://grafana.com/grafana/dashboards/1860-node-exporter-full/)

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FCcegvqzhPtSc9V4U3XVY%2Fimage.png?alt=media&#x26;token=550e4df3-5bb2-4159-bd1c-2745294e5bf1" alt=""><figcaption></figcaption></figure>

We are using the ID:- 1860

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FObHtONUe7Xom6oOC4vA9%2Fimage.png?alt=media&#x26;token=06b7e952-2863-4769-b38f-2660fba886d2" alt=""><figcaption></figcaption></figure>

To import a dashboard, fill in the details such as the server name, select the dashboard folder, enter the UID, and choose Prometheus as the data source type. Click on the import

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2F2lYxjGof9c0TOAaLurLv%2Fimage.png?alt=media&#x26;token=cc311264-991b-4b31-8d12-19adf399cb3c" alt=""><figcaption></figcaption></figure>

After clicking on the final import.

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FHvYx4y6tmB3BDCRy1CZr%2Fimage.png?alt=media&#x26;token=52a3ba03-01f6-4a4e-9366-aaaaeb44c7d6" alt=""><figcaption></figcaption></figure>

Now you can see the data is visible.

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FlVOFrtpgkhokHLPsymDF%2Fimage.png?alt=media&#x26;token=d78eb82b-6b09-4eb6-bcd8-0c52f2d78d4a" alt=""><figcaption></figcaption></figure>
