ToiVietBlog - Bài này mình sẽ hướng dẫn cách xóa target cũ trong database Prometheus. Trong quá trình sử dụng Prometheus để monitor hệ thống của bạn thì hệ thống có thể thay đổi.
Ví dụ như server đổi địa chỉ IP, domain cần monitor không còn sử dụng,..
Những lúc như thế, database vẫn sẽ load dữ liệu cũ của các target không còn tồn tại này. Vậy làm sao để xóa các target cũ này khỏi database?
Enable API admin
Đầu tiên thì bạn cần phải enable API admin trong Prometheus. Nếu không thì bạn có thể gặp phải lỗi kiểu như sau (hoặc một lỗi nào đó tương tự):
{"status":"error","errorType":"unavailable","error":"Admin APIs disabled"}
Để enable API cho admin, bạn mở file systemd của prometheus lên.
nano /etc/systemd/system/prometheus.service
Recommended Reading: Cài đặt Prometheus và Grafana trên Ubuntu 18
Thêm tham số dưới đây vào phần ExecStart
:
--web.enable-admin-api
Nội dung file sẽ như dưới:
[Unit] Description=Prometheus systemd service unit Wants=network-online.target After=network-online.target [Service] Type=simple User=prometheus Group=prometheus ExecReload=/bin/kill -HUP $MAINPID 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 \ --storage.tsdb.retention.time=60d \ --web.enable-admin-api SyslogIdentifier=prometheus Restart=always [Install] WantedBy=multi-user.target
Sau đó, bạn reload daemon và restart dịch vụ prometheus.
systemctl daemon-reload systemctl restart prometheus
Xóa target cũ trong database Prometheus

Để xóa một target cũ trong database của prometheus, bạn sử dụng cú pháp sau:
curl -X POST -g 'http://localhost:9090/api/v2/admin/tsdb/delete_series?match[]={instance="target-name"}'
Ví dụ mình muốn xóa target monitor là check ping 8.8.8.8.
curl -X POST -g 'http://localhost:9090/api/v2/admin/tsdb/delete_series?match[]={instance="8.8.8.8"}'
Khi bạn chạy lệnh trên, nếu cửa sổ terminal không trả về màn hình bất kì gì cả thì nghĩa là bạn xóa thành công.
Kết luận
Đây chỉ là một thao tác nhỏ, nhưng lại rất hữu ích khi bạn làm việc với prometheus. Hy vọng bài viết giúp ích cho bạn, hãy theo dõi các bài viết tiếp theo nhé.
If you appreciate what we share in this blog, you can support us by:
- Stay connected to: Facebook | Twitter | Google Plus | YouTube
- Subscribe email to recieve new posts from us: Sign up now.
- Start your own blog with SSD VPS - Free Let's Encrypt SSL ($2.5/month).
- Become a Supporter - Make a contribution via PayPal.
- Support us by purchasing Ribbon Lite Child theme being using on this website.
We are thankful for your support.