# Getting Started with Prometheus and Grafana

### \# What is Prometheus ?

Prometheus is a tool that watches over your computer systems, collects data about how they're doing, and alerts you if anything goes wrong.

**STEP 1** :- Create an AWS EC2 instance of t2.micro or t3.micro with Ubuntu 20.04 LTS .

**STEP2**:- Connect to the instance ( I use Git Bash and I connect using the command --&gt; ssh -i mypemfile.pem ubuntu@ip\_address\_of\_instance )

**STEP3**:- The below code will install 2 services , namely Prometheus and the Prometheus Node Exporter.

```bash
sudo apt update
sudo apt install prometheus
```

**STEP4**:- We can verify the status of Prometheus and the Prometheus Node Exporter using the below commands.

```bash
sudo service prometheus status
sudo service prometheus-node-exporter status
```

We will see --&gt; **Active: active (running)**

**STEP5:-** Edit the inbound rules of the security groups of the EC2 instance to include port **9090**.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1708511480403/750469e4-61c6-423d-9a6b-461857ea7837.png align="right")

**STEP6:-** We can visit Prometheus at --&gt; *http://\[IP address of the EC2 instance\]:9090*

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1708511679741/5e266b66-e0f9-4206-9aa7-5d962d6358f2.png align="center")

Similarly if we include port **9100** in the inbound rules of security groups, then we can visit Prometheus Node Exporter at **\--&gt;***http://\[IP address of the EC2 instance\]:9100*

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1708602148185/01ebbc6e-5476-43a3-b4c7-f02c6d2e41a9.png align="center")

We can see which processes are running by using the command :-

`ps -u prometheus`

It will give us details like PID , TTY , TIME , CMD.

### #Reverse proxying Prometheus with Nginx

It helps to improve security, manage traffic, and provide additional features like adding SSL , basic Authentication ,etc.

```bash
sudo apt install nginx
sudo service nginx status
```

On visiting the **http://ipaddress\_of\_ec2instance** :- the default Nginx welcome page opens.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1708513776677/6ec4736d-792f-4fde-bf2f-65873a1c5645.png align="center")

### #Providing Domain Name for our Prometheus Server

**STEP1 :-** Visit the link [https://freedns.afraid.org/signup/](https://freedns.afraid.org/signup/) . After signing up, create your own Domain there **for FREE**.

**STEP2** :- Then follow the following set of commands :-

```bash
cd /etc/nginx/sites-enabled
sudo nano prometheus
```

Then copy-paste the below text :-

> *server {*
> 
> *listen 80;*
> 
> *listen \[::\]:80;*
> 
> *server\_name YOUR-DOMAIN-NAME;*
> 
> *location / {*
> 
> *proxy\_pass*[*http://localhost:9090/*](http://localhost:9090/)*;*
> 
> *}*
> 
> *}*

```bash
sudo nginx -t
sudo service nginx restart
```

So we can visit Prometheus Server at --&gt; ***http://YOUR-DOMAIN-NAME***

### #Adding SSL to Prometheus Reverse Proxy

After adding SSL, accessing the server via HTTPS ensures secure communication by encrypting data transmitted between the server and clients, enhancing privacy and security.

```bash
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
```

Now we can also visit the Server at --&gt; ***https://YOUR-DOMAIN-NAME***

### #Add Basic Authentication

```bash
cd /etc/nginx
sudo apt install apache2-utils
htpasswd -c /etc/nginx/.htpasswd admin #creating user called "admin"
```

Then we need to enter a password.

Then we need to open the Nginx Prometheus config file using the below command :- `sudo nano /etc/nginx/sites-enabled/prometheus`

`server { ...`

`auth_basic "Protected Area";`

`auth_basic_user_file /etc/nginx/.htpasswd;`

`...`

`}`

```bash
sudo nginx -t
sudo service nginx restart
```

So now we need to enter **Username and Password** :-

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1708600861815/d18c1399-2781-4423-b584-3fb40eefddfa.png align="center")

# **#Monitoring Jenkins with Prometheus and Grafana :-**

**STEP1**:- Install JENKINS by following this video [https://youtu.be/lRpS2CovMrs?si=2tju9q0HrD43HaIn](https://youtu.be/lRpS2CovMrs?si=2tju9q0HrD43HaIn)

**STEP2**:- Install the **Prometheus metrics** plugin in Jenkins

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1708670730237/9b8bfafb-47b6-43fb-9dea-8ebedaa4e9d8.png align="center")

**STEP3**:- Use the following command :-

`sudo nano /etc/prometheus/prometheus.yml`

Then copy-paste the following portion in the .yml file

```yaml
- job_name: 'jenkins'
    metrics_path: '/prometheus'
    static_configs:
      - targets: ['url_of_the_jenkins_server:8080']
```

**STEP4**:- Then I restarted the prometheus server

```bash
sudo service prometheus stop
sudo systemctl start prometheus
```

Then in the Prometheus server , I **monitored** the number of plugins active in my Jenkins server :-

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1708671681235/819742f1-0f97-4ce1-a456-25cdf0303213.png align="center")

In my case , I see that **92** plugins are active right now .

### **Step5 :-** INSTALL GRAFANA

Grafana is a data visualization tool that works seamlessly with Prometheus, allowing users to create dashboards and visualize the metrics collected by Prometheus in a user-friendly and customizable way.

```bash
sudo apt update
sudo apt-get install -y adduser libfontconfig1
wget https://dl.grafana.com/oss/release/grafana_9.3.2_amd64.deb
sudo dpkg -i grafana_9.3.2_amd64.deb
sudo service grafana-server start
sudo service grafana-server status #Check the status
```

Grafana server will be hosted at :-*http://\[Grafana server IP address\]:3000*

Default Grafana login is -&gt; Username : **admin** Password : **admin**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1708673454845/3feb1818-58e2-4411-84dd-18e532a5080d.png align="center")

After Logging in , it is recommended to change the Password.

Then follow the ***below*** steps :-

**Add Data Sources --&gt; Prometheus --&gt; Paste the URL of prometheus server in the HTTP(URL) Section. --&gt; Save & Test**

*Then create a new dashboard , then add a new panel . In visualizations I opted for****Stat****. Search for j****enkins\_plugins\_active****in the Query section.*

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1708673791051/ca76eb0c-75c0-49d2-9637-1615c333b313.png align="center")

So we see the result as **92**.

Then I saved the Dashboard with the title --&gt; ***How many active plugins in Jenkins***

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1708674067235/f33a1d30-98b7-4259-995d-d45b9d651d25.png align="center")

### **Now , suppose I want to monitor the results of my last builds in jenkins , then we see:-**

a) Result from my Jenkins Dashboard

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1708674791859/9926d8db-d64c-4bc9-89e0-e3c3421e9bbe.png align="center")

b) Result from Prometheus

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1708674875356/f2670a30-9997-4918-a807-5d17969a51e7.png align="center")

c) Result from Grafana

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1708674929463/7707ca07-b9f0-40a2-99de-2b0438641eab.png align="center")

*So all these results confirm the fact that my* ***last build*** *of the jenkins jobs* ***"first\_project"*** *and* ***"kkc\_builders"*** *are* ***successful*** but "***project\_test\_hook"*** *has* ***failed.***
