Prometheus Monitoring Stack

Sets up Prometheus with Node Exporter for system monitoring

Script Author

Rowan de Haas's avatar
Rowan de Haas
Script Author

Script Details

Created 5 months ago
Size 3 KB

Tags

Script Content

Raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#cloud-config
package_update: true
package_upgrade: true

packages:
  - wget
  - tar

users:
  - name: prometheus
    system: true
    shell: /bin/false
    home: /var/lib/prometheus

write_files:
  - path: /etc/systemd/system/prometheus.service
    content: |
      [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 \
          --web.listen-address=0.0.0.0:9090 \
          --web.enable-lifecycle
      
      [Install]
      WantedBy=multi-user.target

  - path: /etc/prometheus/prometheus.yml
    content: |
      global:
        scrape_interval: 15s
      
      scrape_configs:
        - job_name: 'prometheus'
          static_configs:
            - targets: ['localhost:9090']
        
        - job_name: 'node'
          static_configs:
            - targets: ['localhost:9100']

runcmd:
  # Download and install Prometheus
  - cd /tmp
  - wget https://github.com/prometheus/prometheus/releases/download/v2.40.0/prometheus-2.40.0.linux-amd64.tar.gz
  - tar -xzf prometheus-2.40.0.linux-amd64.tar.gz
  - cp prometheus-2.40.0.linux-amd64/prometheus /usr/local/bin/
  - cp prometheus-2.40.0.linux-amd64/promtool /usr/local/bin/
  - mkdir -p /etc/prometheus /var/lib/prometheus
  - chown prometheus:prometheus /etc/prometheus /var/lib/prometheus
  
  # Download and install Node Exporter
  - wget https://github.com/prometheus/node_exporter/releases/download/v1.4.0/node_exporter-1.4.0.linux-amd64.tar.gz
  - tar -xzf node_exporter-1.4.0.linux-amd64.tar.gz
  - cp node_exporter-1.4.0.linux-amd64/node_exporter /usr/local/bin/
  
  # Start services
  - systemctl enable prometheus
  - systemctl start prometheus
  - systemctl enable node_exporter
  - systemctl start node_exporter
  
  # Configure firewall
  - ufw allow 9090/tcp
  - ufw allow 9100/tcp

How to Use This Script

Cloud Provider Examples

Amazon EC2

aws ec2 run-instances
  --image-id ami-12345678
  --instance-type t3.micro
  --user-data file://script.yaml

DigitalOcean

doctl compute droplet create
  --image ubuntu-22-04-x64
  --size s-1vcpu-1gb
  --user-data-file script.yaml
  my-droplet

Google Cloud

gcloud compute instances create
  my-instance
  --metadata-from-file
  user-data=script.yaml