# Monitoring Grafana

<https://grafana.com/>

\
Grafana is an open-source platform for data analysis and monitoring. It enables you to query, visualize, and comprehend metrics from various systems, such as databases, servers, cloud services, and many others.

## Prometheus Install

#### 1 - Download latest software version

```
wget https://github.com/prometheus/prometheus/releases/download/v2.30.2/prometheus-2.30.2.linux-amd64
```

#### 2 - Unzip

```
tar -xvf prometheus-2.30.2.linux-amd64.tar.gz
```

#### 3 - Copying contents from the prometheus directory to the appropriate directory

```
sudo cp -r ./prometheus-2.30.2.linux-amd64 /usr/local/bin/prometheus
```

#### 4 - Remove downloaded files

```
rm prometheus-2.30.2.linux-amd64.tar.gz
rm -r prometheus-2.30.2.linux-amd64
```

### 5 - Creating service to run prometheus

#### 5.1 - Create Prometheus Service File

Create a file:

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

Put the following content inside it:

```
[Unit]
Description=Prometheus Service
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/bin/prometheus/prometheus --config.file /usr/local/bin/prometheus/prometheus.yml

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

#### 5.2 - Reload daemons

```
sudo systemctl daemon-reload
```

#### 6 - Execute the prometheus

```
sudo systemctl start prometheus
```

#### 7 - Checking if prometheus is really running

```
sudo systemctl status prometheus
```

Something like this will appear:

```
● prometheus.service - Prometheus Service
     Loaded: loaded (/etc/systemd/system/prometheus.service; disabled; vendor preset: enabled)
     Active: active (running) since Sun 2021-10-03 04:55:46 UTC; 7min ago
   Main PID: 2388 (prometheus)
      Tasks: 6 (limit: 1071)
     Memory: 22.2M
     CGroup: /system.slice/prometheus.service
             └─2388 /usr/local/bin/prometheus/prometheus --config.file /usr/local/bin/prometheus/prometheus.yml
```

#### 8 - Enabling service in boot

```
sudo systemctl enable prometheus
```

## Node exporter install

#### 1 - Download latest software version

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

#### 2 - Unzip

```
tar -xvf node_exporter-1.3.1.linux-amd64.tar.gz
```

#### 3 - Copying contents from the node\_exporter directory to the appropriate directory

```
sudo cp -r ./node_exporter-1.3.1.linux-amd64 /usr/local/bin/node_exporter
```

#### 4 - Remove downloaded files

```
rm node_exporter-1.3.1.linux-amd64.tar.gz
rm -r node_exporter-1.3.1.linux-amd64
```

### 5 - Creating service to run node\_exporter

#### 5.1 - Create Node exporter Service File

Create a file:

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

Put the following content inside it:

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

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

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

#### 5.2 - Reload daemons

```
sudo systemctl daemon-reload
```

#### 6 - Execute the node\_exporter

```
sudo systemctl start node_exporter
```

#### 7 - Checking if node\_exporter is really running

```
sudo systemctl status node_exporter
```

Something like this will appear:

```
sudo systemctl status node_exporter
● node_exporter.service - Node Exporter Service
     Loaded: loaded (/etc/systemd/system/node_exporter.service; disabled; vendor preset: enabled)
     Active: active (running) since Sun 2021-10-03 05:23:18 UTC; 14min ago
   Main PID: 2808 (node_exporter)
      Tasks: 3 (limit: 1071)
     Memory: 2.0M
     CGroup: /system.slice/node_exporter.service
             └─2808 /usr/local/bin/node_exporter/node_exporter
```

#### 8 - Enabling service in boot

```
sudo systemctl enable node_exporter
```

## Grafana Install

#### 1 - Download latest software version and install

```
sudo apt-get install -y adduser libfontconfig1
wget https://dl.grafana.com/oss/release/grafana_8.1.5_amd64.deb
sudo dpkg -i grafana_8.1.5_amd64.deb
```

#### 2 - Remove downloaded package

```
rm grafana_8.1.5_amd64.deb
```

#### 3 - Start grafana-server service

```
sudo systemctl start grafana-server
```

#### 4 - Checking if grafana-server is really running

```
sudo systemctl status grafana-server
```

Something like this will appear:

```
● grafana-server.service - Grafana instance
     Loaded: loaded (/lib/systemd/system/grafana-server.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2021-10-03 23:40:30 UTC; 2h 41min ago
       Docs: http://docs.grafana.org
   Main PID: 7047 (grafana-server)
      Tasks: 11 (limit: 4705)
     Memory: 32.7M
     CGroup: /system.slice/grafana-server.service
             └─7047 /usr/sbin/grafana-server --config=/etc/grafana/grafana.ini --pidfile=/var/run/grafana/grafana-server.pid --packaging=deb 
cfg:default.paths.logs=/var/log/grafana cfg:default.paths.data=/var/lib/grafana cfg:default.paths.plugins=/var/lib/grafana/plugins cfg:defaul
t.paths.provisioning=/etc/grafana/provisioning
```

#### 5 - Enabling service in boot

```
sudo systemctl enable grafana-server
```

#### 6 - Access Grafana

Open Grafana at the following URL: <http://host\\_ip:3000>

<figure><img src="https://577206089-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FilWUahKUPyBVUGJl2b1M%2Fuploads%2Fgit-blob-7505e283e48469f7531f5bf8e1bf294af048d68e%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

* Note: Open port 3000 in your firewall
* Grafana's default credentials are:
* username: admin password: admin

<figure><img src="https://577206089-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FilWUahKUPyBVUGJl2b1M%2Fuploads%2Fgit-blob-20973866921d3c74014e0b962f62753c00313378%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

Note: a good security practice is to change the admin user password after the first login.

#### 7 - Define Data Source

Select Data source option in Configuration section in left panel.

<figure><img src="https://577206089-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FilWUahKUPyBVUGJl2b1M%2Fuploads%2Fgit-blob-c9c0fbb1b4fcc140c7c507995a70a0b4c463a239%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

Select Prometheus datasource type.

<figure><img src="https://577206089-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FilWUahKUPyBVUGJl2b1M%2Fuploads%2Fgit-blob-593f2e641f5dc2693c6f8a4b51547b320b9cad6b%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

Define Prometheus name and URL.

URL: [http://localhost:9090](http://localhost:9090/)

<figure><img src="https://577206089-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FilWUahKUPyBVUGJl2b1M%2Fuploads%2Fgit-blob-b82f5242d51fbe53e77bb9b690fb92be17999bf7%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

Click in Save and test button.

<figure><img src="https://577206089-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FilWUahKUPyBVUGJl2b1M%2Fuploads%2Fgit-blob-babdb8c2993bc578ce5b51b232dc8f924c44483e%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

#### 8 - Import Dashboard

<figure><img src="https://577206089-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FilWUahKUPyBVUGJl2b1M%2Fuploads%2Fgit-blob-ce3ccde363e289afce251621e1d3855bff5a9e6a%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

Upload file and click in load button.

\\

<figure><img src="https://577206089-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FilWUahKUPyBVUGJl2b1M%2Fuploads%2Fgit-blob-abe2e9135b3944ce4a56e008e1a6b44b90fc6ebc%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

Define name of Dashboard, select folder and click in import button.

<figure><img src="https://577206089-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FilWUahKUPyBVUGJl2b1M%2Fuploads%2Fgit-blob-9f8ce35d1cb5b9455eda1ee3b09bc55de2fa6c2b%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

If everything happens normally, your dashboard will appear on the screen.

<figure><img src="https://577206089-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FilWUahKUPyBVUGJl2b1M%2Fuploads%2Fgit-blob-51e60ca760f865a471cfdbb3fe44af43f1f847b7%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

## Grafana Alerts

#### 1 - Access the Alert menu and then click Admin

<figure><img src="https://577206089-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FilWUahKUPyBVUGJl2b1M%2Fuploads%2Fgit-blob-5ee8c7d9e0160850d31a42cfd39744a767bac6c6%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

#### 2 - Click the "Add Alertmanager" button

<figure><img src="https://577206089-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FilWUahKUPyBVUGJl2b1M%2Fuploads%2Fgit-blob-f51eed1da7a7829851edf6d4eedf02e8044dd289%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

#### 3 - Set the URL where your external alertmanager instance is installed and click on the "Add alertmanagers" button

<figure><img src="https://577206089-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FilWUahKUPyBVUGJl2b1M%2Fuploads%2Fgit-blob-ebb6cebbcea3b5423b66aba1db1494574bb9486d%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

#### 4 - Access the panel inside the Dashboard that you want to add the alert, right-click on the panel and then click "Edit"

<figure><img src="https://577206089-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FilWUahKUPyBVUGJl2b1M%2Fuploads%2Fgit-blob-10ba971b820fadc9ffa3f976784f51dac6831e7b%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

#### 5 - Click the "Alert" tab and click the "Create alert rule from this panel" button to create an alert with a direct link to this panel.

<figure><img src="https://577206089-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FilWUahKUPyBVUGJl2b1M%2Fuploads%2Fgit-blob-b5f1f133fa3f837502a6d2ab7c2cf20c51b239d2%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

#### 6 - Define the name of the alert, the folder, the function, select the query and the limit that will be used for the alert.

<figure><img src="https://577206089-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FilWUahKUPyBVUGJl2b1M%2Fuploads%2Fgit-blob-f2c496101f2681300e3769cedc3787b9d569f7f4%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

#### 7 - Set the field "evaluate every" and "for", if necessary add a custom label and then click "Save and exit"

<figure><img src="https://577206089-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FilWUahKUPyBVUGJl2b1M%2Fuploads%2Fgit-blob-a1658b5502462a8b14dba23c8cfea2c7cd701073%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

## Nginx Install and enable HTTPS connections with Let's Encrypt

```
sudo apt install nginx
```

Enable Nginx in boot:

```
sudo systemctl enable nginx
```

Certbot snap version install:

```
sudo snap install core; sudo snap refresh core
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot
sudo certbot --nginx
```

Configure /etc/nginx/sites-enabled/default :

```
location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
                proxy_pass http://localhost:3000;
        }


        location /api/live {
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "Upgrade";
                proxy_set_header Host $http_host;
                proxy_pass http://localhost:3000/;
        }

        location /grafana/ {
                proxy_pass         http://localhost:3000/;
                proxy_set_header   Host $Host;
                proxy_max_temp_file_size 0;
        }
```

Configure /etc/grafana/grafana.ini :

```
[server]

root_url = %(protocol)s://%(domain)s:/grafana
serve_from_sub_path = true
```

Restart nginx and grafana-server services :

```
sudo systemctl restart grafana-server
sudo systemctl restart nginx
```

## Configuring names in hosts file

#### 1 - Edit /etc/hosts file

Define the names that will be used for each host, and put them in the file /etc/hosts, follow exemple:

```
sudo nano /etc/hosts

nameofhost1 10.0.0.1
nameofhost2 10.0.0.2
nameofhost3 10.0.0.3
```

#### 2 - Test configuration with ping command

```
ping nameofhost
```

Something like this will appear:

```
PING localhost (10.0.0.1) 56(84) bytes of data.
64 bytes from ip6-localhost (10.0.0.1): icmp_seq=1 ttl=64 time=0.012 ms
64 bytes from ip6-localhost (10.0.0.1): icmp_seq=2 ttl=64 time=0.042 ms
```

if the IP address that returns after the command matches the name you put in the /etc/hosts file, the configuration is correct

Now in the configuration of prometheus you can configure the host name instead of the IP

```
  - job_name: "Validators"
    static_configs:
      - targets: ["nameofhost1:9100","nameofhost2:9100","nameofhost3:9100"]
```

## GitHub OAuth2 Authentication

#### 1 - Register your application with GitHub

To enable the GitHub OAuth2 you must register your application with GitHub. GitHub will generate a client ID and secret key for you to use. When you create the application you will need to specify a callback URL. Specify this as callback:

```
https://myrealdomain.com/grafana/login/github
```

Access your organization

<figure><img src="https://577206089-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FilWUahKUPyBVUGJl2b1M%2Fuploads%2Fgit-blob-7e24a818db383337738d317aabfdee54426f160e%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

Access Settings of your organization

<figure><img src="https://577206089-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FilWUahKUPyBVUGJl2b1M%2Fuploads%2Fgit-blob-57aebd727048028f52013f20b705d5a862c19aeb%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

Access Developer section and select OAuth App

<figure><img src="https://577206089-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FilWUahKUPyBVUGJl2b1M%2Fuploads%2Fgit-blob-c6cd08613a8906cea90162bfe4131bd1e32bc6db%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

Click in "New OAuth App"

<figure><img src="https://577206089-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FilWUahKUPyBVUGJl2b1M%2Fuploads%2Fgit-blob-df02df477d573e574ae03374d7514a66a888fd22%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

Define OAuth app info

<figure><img src="https://577206089-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FilWUahKUPyBVUGJl2b1M%2Fuploads%2Fgit-blob-93725f77ed83537a00ba518185d06517c40a2b30%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

<figure><img src="https://577206089-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FilWUahKUPyBVUGJl2b1M%2Fuploads%2Fgit-blob-20a25d4356396f2d052656d8370d1e4bffb1a9d8%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

Get Client ID and generate new client secret

<figure><img src="https://577206089-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FilWUahKUPyBVUGJl2b1M%2Fuploads%2Fgit-blob-9bc32a2cd3134fa98008a8e1130abf790080f780%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

Store your Client ID and Client secret for alertmanager configuration

#### 2 - Set root\_url in grafana.ini file

```
sudo nano /etc/grafana/grafana.ini
[server]
root_url = https://myrealdomain.com/grafana
serve_from_sub_path = true
```

#### 3 - Enable GitHub in Grafana in grafana.ini file

```
sudo nano /etc/grafana/grafana.ini
[auth.github]
enabled = true
allow_sign_up = true
client_id = YOUR_GITHUB_APP_CLIENT_ID
client_secret = YOUR_GITHUB_APP_CLIENT_SECRET
scopes = user:email,read:org
auth_url = https://github.com/login/oauth/authorize
token_url = https://github.com/login/oauth/access_token
api_url = https://api.github.com/user
# space-delimited organization names
allowed_organizations = organization1 organization2
```

#### 4 - Restart grafana-server service

```
sudo systemctl restart grafana-server
```

#### 5 - After logout we have a login option with github

<figure><img src="https://577206089-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FilWUahKUPyBVUGJl2b1M%2Fuploads%2Fgit-blob-706f573b1e057dfc8fadea74d2369f81e6eaaedc%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>
