Skip to content

Instantly share code, notes, and snippets.

@pingkunga
Last active June 25, 2023 08:55
Show Gist options
  • Save pingkunga/3d54bd84d6f703390e05068c8f22bd39 to your computer and use it in GitHub Desktop.
Save pingkunga/3d54bd84d6f703390e05068c8f22bd39 to your computer and use it in GitHub Desktop.
Lab Monitoring
Basic Skill
* Linux Command
* Docker
Monitor จะมั 2 กลุ่ม
* Uptime-kuma
* Prometheus(Time Series DB) + Node Exporter (จัด Mwtric ตามต้องการ) >> Grafana (Visual)
Uptime-kuma (https://github.com/louislam/uptime-kuma)
# 1. Start uptime-kuma with docker
docker run -d --restart=always -p 3001:3001 -v uptime-kuma:/app/data --name uptime-kuma louislam/uptime-kuma:1
# 2. เปิดหน้าเว็บเข้าพอร์ต 3001 สำหรับใช้งาน ครั้งแรกจะเป็นการสร้าง account admin
# 3. สร้างช่องทางในการแจ้งเตือน (Alert)
# 4. สร้างรายการที่จะตรวจวัด ตัวอย่าง Web กับ PostgreSQL DB
Monitor Type: HTTP(s)
Friendly Name: web
URL: https://naiwaen.debuggingsoft.com/
Notify มีหลายแบบ Line / Mattermost / Discord หรือ อะไรก็ได้
แจ้งได้ทั้ง Cert / Uptime แต่ข้อเสัย มันแจ้งตอนคุย
==================================================================
Prometheus
https://prometheus.io/docs/prometheus/latest/installation/
# 2. Start Prometheus container
# 2.1 เริ่มการทำงานของ Prometheus
sudo docker run -d -p 9090:9090 -v prometheus:/prometheus --name prometheus prom/prometheus
# 2.2 ดูสถานะการทำงานของ docker
sudo docker ps [-a]
# 2.3 เปิดเว็บไปที่ http://localhost:9090/
# 3. แก้ไขคอนฟิก prometheus
# 3.1 สร้างไดเรกทอรีไว้เก็บข้อมูลและคอนฟิกไฟล์
mkdir prometheus
# 3.2 เริ่มการทำงานของ prometheus
cd prometheus
# 3.3 คัดลอก prometheus.yml ออกมาจากใน container
sudo docker cp prometheus:/etc/prometheus/prometheus.yml .
# 3.4 เพิ่มคอนฟิกของเครื่องที่เราจะมอร์นิเตอร์ ในที่นี้จะใช้ "node exporter"
nano prometheus.yml //ใช้ตัวไหนก็ได้นะ nano / vi ก็ได้
# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: "prometheus"
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ["localhost:9090"]
- job_name: node
static_configs:
- targets: ['192.168.123.130:9100']
# 3.5 หยุด และ ลบ prometheus container เดิมทิ้ง
sudo docker stop prometheus
sudo docker rm prometheus
# 3.6 เริ่มการทำงาน prometheus container ใหม่ พร้อมเชื่อมโยงไฟล์ config จากโฮสต์ เข้าไปใน container
sudo docker run -d --restart=always \
-p 9090:9090 \
-v prometheus:/prometheus \
-v /root/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml \
--name prometheus \
prom/prometheus
# 4. run "node-exporter"
# Redhat
yum install epel-release
yum install golang-github-prometheus-node-exporter.x86_64
systemctl enable --now prometheus-node-exporter
# Debien
sudo apt install prometheus-node-exporter
systemctl enable --now prometheus-node-exporter
# ref. https://grafana.com/docs/grafana-cloud/quickstart/docker-compose-linux/
sudo docker run -d -p 9100:9100 --name=node-exporter -v /proc:/host/proc:ro -v /sys:/host/sys:ro -v /:/rootfs:ro prom/node-exporter
# 5. ดูว่า prometheus เห็น metric node ที่เพิ่มเข้าไปใหม่หรือไม่ เปิดเว็บไป http://localhost:9090 ดูที่เมนู Status --> Targets
# Grafana
# Ref. https://grafana.com/docs/grafana/latest/setup-grafana/installation/docker/
# 6. Start Grafana
# 6.1 start grafana
sudo docker run -d --restart=always -p 3000:3000 --name=grafana --network=host -v grafana-storage:/var/lib/grafana grafana/grafana
# 6.2 open web to http://localhost:3000/
User/pass: admin/admin
# 6.3 Add Data sources
Home --> Connections --> Your connections --> Data sources --> Prometheus
# 6.4 Import Dashboard
Home --> Dashboards --> Import dashboard
ดูว่ามี Dashboard พร้อมใช้อะไรบ้างที่ https://grafana.com/grafana/dashboards/
แล้วนำ Dashboard ID มาใส่ที่ช่อง "Import via grafana.com" ตัวอย่างเช่น Node Exporter Full รหัส 1860 แล้วกด Load
จากนั้นมาเลือก ที่ Prometheus แล้วจึงกด "import"
# 7. Alert
Home --> Alerting --> Contact points
# 7.1 Line
เลือก + Add new contact point
- กำหนดชื่อที่ Name เช่น Line Noti to ...
- ที่ integration เลือก LINE
- ใส่ Token ที่ได้จาก https://notify-bot.line.me/en/ ล็อกอินแล้ว เลือกที่ชื่อ --> My page แล้วเลือก Generate token
- กด Test เพื่อทดลองให้ Grafana ส่ง Alert มาที่ไลน์
# 7.2 Email
grafana-default-email
จะต้องกำหนดค่า SMTP ใน /etc/grafana/grafana.ini
sudo docker exec -it -u 0 grafana /bin/bash
vi /etc/grafana/grafana.ini
#################################### SMTP / Emailing ##########################
[smtp]
enabled = true
host = smtp.gmail.com:587
user = user@gmail.com
# If the password contains # or ; you have to wrap it with triple quotes. Ex """#password;"""
password = [from app password]
** ถ้าจะใช้ gmail ต้องไปกำหนด App password ใน Google Account ให้ล็อกอินกูเกิลแล้วไปที่ Google Account --> App passwords
จากนั้น Select app --> Other fill Grafana
คัดลอกรหัสที่ได้ไปใส่ใน password
# exit from container
exit
# restart grafana
docker restart grafana
7.3 ปรับแต่งที่ notification policies
- ที่ Default policies ให้กดที่ "สามจุดด้านขวา" เลือก "edit"
- แล้วเปลี่ยน "Default contact point" เป็น Line Notify ของเดิมเป็น Email
# 8.Alert rule
# 8.1 Disk usage > 80%
100 - ((node_filesystem_avail_bytes{mountpoint="/"} * 100) / node_filesystem_size_bytes{mountpoint="/"})
สถานะถ้าถึงเกณฑ์จะเป็น Pending พอครบระยะเวลารอคอย 5 นาที เปลี่ยนสถานะเป็น firing และแจ้งเตือน
# ทดลองสร้างไฟล์
dd if=/dev/zero of=test2.img bs=1G count=10
# รอ 1-2 นาที มันจะขึ้น pending
# รอ 5 นาที มันจะเด้งไลน์
# ลองลบไฟล์ ตรวจสอบไลน์
Q&A
- How to Scale Prometheus
> K8S + Prometheus + Thanos
---> https://thanos.io/
---> https://www.opsramp.com/guides/prometheus-monitoring/prometheus-thanos/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment