Logo

Personal Ops Runbook

Personal runbook covering infrastructure operations for Cloud, Kubernetes, OpenStack, and Ceph environments. Includes deployment and teardown procedures, node management, cluster monitoring setup, and incident response workflows compiled from day-to-day operational work. Intended strictly for personal reference — configurations and scripts are environment-specific and not guaranteed to work as-is elsewhere.

Hướng dẫn triển khai Telegraf plugin

Cài đặt Telegraf.

Sử dụng scipt sau để cài Telegraf

curl --silent --location -O \
https://repos.influxdata.com/influxdata-archive.key \
&& echo "943666881a1b8d9b849b74caebf02d3465d6beb716510d86a39f6c8e8dac7515  influxdata-archive.key" \
| sha256sum -c - && cat influxdata-archive.key \
| gpg --dearmor \
| tee /etc/apt/trusted.gpg.d/influxdata-archive.gpg > /dev/null \
&& echo 'deb [signed-by=/etc/apt/trusted.gpg.d/influxdata-archive.gpg] https://repos.influxdata.com/debian stable main' \
| tee /etc/apt/sources.list.d/influxdata.list
apt-get update && apt-get install telegraf

Install the required packages

apt update
apt install python3-pip python3-venv -y

Create the virtual environment

mkdir /etc/telegraf/telegraf.d
python3 -m venv /etc/telegraf/venv

Activate the virtual environment

source /etc/telegraf/venv/bin/activate

Create the requirements file

cat > /etc/telegraf/telegraf.d/requirements.txt << 'OEF'
requests
pyyaml
OEF

Install the required packages

/etc/telegraf/venv/bin/python -m pip install -r /etc/telegraf/telegraf.d/requirements.txt

Taoj file /etc/telegraf/telegraf.conf

cat > /etc/telegraf/telegraf.conf << 'OEF'
[global_tags]
[agent]
  interval = "60s"
  round_interval = true
  metric_batch_size = 5000
  metric_buffer_limit = 100000
  collection_jitter = "0s"
  flush_interval = "60s"
  flush_jitter = "0s"
  precision = ""
  debug = true
  quiet = false
  logfile = "/var/log/telegraf/telegraf.log"
  hostname = ""
  omit_hostname = false

[[outputs.influxdb]]
   urls = ["http://127.0.0.1:8086"]
   timeout = "60s"
   database = "staging"
   tagexclude = ["hash", "last_update", "status_changed_at", "put_timestamp", "reported_put_timestamp"]
   namedrop = ["*-scanner"]

[[outputs.prometheus_client]]
  listen = ":9273"
  path = "/metrics"
  expiration_interval = "60s"
OEF

Tạo file /etc/telegraf/telegraf.d/staging.py

cat > /etc/telegraf/telegraf.d/staging.py << 'OEF'
import subprocess
import re
import json
from concurrent.futures import ThreadPoolExecutor

list_hosts = ['1.1.1.1', '8.8.8.8', '8.8.4.4', '8.8.8.7']

def icmp_response_time(target_ipaddr):
    try:
        response = subprocess.check_output(
            f"ping -c 4 -W 0.5 {target_ipaddr}",
            shell=True,
            stderr=subprocess.STDOUT,
            universal_newlines=True
        )

        rtt_match = re.search(r"time=([\d.]+) ms", response)
        if rtt_match:
            rtt = float(rtt_match.group(1))
            response = rtt
        else:
            response = 0
    except subprocess.CalledProcessError:
        response = 0
    return response

def generate_json_output(hosts):
    output = []
    with ThreadPoolExecutor(max_workers=10) as executor:
        futures = {executor.submit(icmp_response_time, host): host for host in hosts}
        for future in futures:
            host = futures[future]
            try:
                status = future.result()
                output.append({
                    "name": "check_host",
                    "role": "icmp",
                    "ip": host,
                    "value": status
                })
            except Exception as e:
                print(f"Error checking {host}: {e}")
                output.append({
                    "name": "check_host",
                    "role": "icmp",
                    "ip": host,
                    "value": 0
                })
    return json.dumps(output)

if __name__ == "__main__":
    json_output = generate_json_output(list_hosts)
    print(json_output)
OEF

Phân quyền cho script.

chmod +x /etc/telegraf/telegraf.d/staging.py

Chạy thử script.

shell> python3 /etc/telegraf/telegraf.d/staging.py
[{"name": "check-host", "role": "icmp", "ip": "1.1.1.1", "status": 36.4}, {"name": "check-host", "role": "icmp", "ip": "8.8.8.8", "status": 39.1}, {"name": "check-host", "role": "icmp", "ip": "8.8.4.4", "status": 30.5}, {"name": "check-host", "role": "icmp", "ip": "8.8.8.7", "status": 0}, {"name": "check-host", "role": "icmp", "ip": "192.168.100.253", "status": 0.171}]

Tạo file /etc/telegraf/telegraf.d/staging.conf

cat > /etc/telegraf/telegraf.d/staging.conf << 'OEF'
[[inputs.exec]]
    commands = ["python3 /etc/telegraf/telegraf.d/staging.py"]
    interval = "30s"
    json_name_key = "name"
    tag_keys = ["role", "ip"]
    timeout = "60s"
    data_format = "json"
OEF

Khởi động Telegraf

systemctl restart telegraf
systemctl enable telegraf
systemctl status telegraf

Debug

shell> tail -f /var/log/telegraf/telegraf.log
2025-01-09T16:37:02Z I! Tags enabled: host=netbox-server
2025-01-09T16:37:02Z I! [agent] Config: Interval:1m0s, Quiet:false, Hostname:"netbox-server", Flush Interval:1m0s
2025-01-09T16:37:02Z D! [agent] Initializing plugins
2025-01-09T16:37:02Z D! [agent] Connecting outputs
2025-01-09T16:37:02Z D! [agent] Attempting connection to [outputs.influxdb]
2025-01-09T16:37:02Z D! [agent] Successfully connected to outputs.influxdb
2025-01-09T16:37:02Z D! [agent] Attempting connection to [outputs.prometheus_client]
2025-01-09T16:37:02Z I! [outputs.prometheus_client] Listening on http://[::]:9273/metrics
2025-01-09T16:37:02Z D! [agent] Successfully connected to outputs.prometheus_client
2025-01-09T16:37:02Z D! [agent] Starting service inputs
2025-01-09T16:38:02Z D! [outputs.prometheus_client] Wrote batch of 5 metrics in 175.866µs
2025-01-09T16:38:02Z D! [outputs.prometheus_client] Buffer fullness: 0 / 100000 metrics
2025-01-09T16:38:02Z D! [outputs.influxdb] Wrote batch of 5 metrics in 14.119955ms
2025-01-09T16:38:02Z D! [outputs.influxdb] Buffer fullness: 0 / 100000 metrics

Kiểm tra cấu hình Telegraf

shell> telegraf --test
2025-01-09T16:36:28Z I! Loading config: /etc/telegraf/telegraf.conf
2025-01-09T16:36:28Z I! Loading config: /etc/telegraf/telegraf.d/staging.conf
> check-host,host=netbox-server,ip=1.1.1.1,role=icmp status=34.6 1736440592000000000
> check-host,host=netbox-server,ip=8.8.8.8,role=icmp status=37.8 1736440592000000000
> check-host,host=netbox-server,ip=8.8.4.4,role=icmp status=27.9 1736440592000000000
> check-host,host=netbox-server,ip=8.8.8.7,role=icmp status=0 1736440592000000000
> check-host,host=netbox-server,ip=192.168.100.253,role=icmp status=0.06 1736440592000000000

Bạn có thể sử dụng tùy chọn --config để chỉ định đường dẫn đến file cấu hình cụ thể mà bạn muốn kiểm tra, ví dụ như /etc/telegraf/telegraf.d/common/pve_sensor.conf.

shell> telegraf --config /etc/telegraf/telegraf.d/staging.conf --test
2025-01-09T16:35:17Z I! Loading config: /etc/telegraf/telegraf.d/staging.conf
2025-01-09T16:35:17Z I! Starting Telegraf 1.33.0 brought to you by InfluxData the makers of InfluxDB
2025-01-09T16:35:17Z I! Available plugins: 236 inputs, 9 aggregators, 33 processors, 26 parsers, 63 outputs, 6 secret-stores
2025-01-09T16:35:17Z I! Loaded inputs: exec
2025-01-09T16:35:17Z I! Loaded aggregators:
2025-01-09T16:35:17Z I! Loaded processors:
2025-01-09T16:35:17Z I! Loaded secretstores:
2025-01-09T16:35:17Z W! Outputs are not used in testing mode!
2025-01-09T16:35:17Z I! Tags enabled: host=netbox-server
> check-host,host=netbox-server,ip=1.1.1.1,role=icmp status=34.6 1736440522000000000
> check-host,host=netbox-server,ip=8.8.8.8,role=icmp status=38.8 1736440522000000000
> check-host,host=netbox-server,ip=8.8.4.4,role=icmp status=29.4 1736440522000000000
> check-host,host=netbox-server,ip=8.8.8.7,role=icmp status=0 1736440522000000000
> check-host,host=netbox-server,ip=192.168.100.253,role=icmp status=0.043 1736440522000000000

Bạn có thể thêm cờ --debug để nhận thêm thông tin chi tiết trong quá trình kiểm tra:

shell> telegraf --config /etc/telegraf/telegraf.d/staging.conf --test --debug
2025-01-09T16:35:58Z I! Loading config: /etc/telegraf/telegraf.d/staging.conf
2025-01-09T16:35:58Z I! Starting Telegraf 1.33.0 brought to you by InfluxData the makers of InfluxDB
2025-01-09T16:35:58Z I! Available plugins: 236 inputs, 9 aggregators, 33 processors, 26 parsers, 63 outputs, 6 secret-stores
2025-01-09T16:35:58Z I! Loaded inputs: exec
2025-01-09T16:35:58Z I! Loaded aggregators:
2025-01-09T16:35:58Z I! Loaded processors:
2025-01-09T16:35:58Z I! Loaded secretstores:
2025-01-09T16:35:58Z W! Outputs are not used in testing mode!
2025-01-09T16:35:58Z I! Tags enabled: host=netbox-server
2025-01-09T16:35:58Z D! [agent] Initializing plugins
2025-01-09T16:35:58Z D! [agent] Starting service inputs
2025-01-09T16:36:02Z D! [agent] Stopping service inputs
2025-01-09T16:36:02Z D! [agent] Input channel closed
> check-host,host=netbox-server,ip=1.1.1.1,role=icmp status=36.8 1736440562000000000
2025-01-09T16:36:02Z D! [agent] Stopped Successfully
> check-host,host=netbox-server,ip=8.8.8.8,role=icmp status=39.6 1736440562000000000
> check-host,host=netbox-server,ip=8.8.4.4,role=icmp status=29 1736440562000000000
> check-host,host=netbox-server,ip=8.8.8.7,role=icmp status=0 1736440562000000000
> check-host,host=netbox-server,ip=192.168.100.253,role=icmp status=0.041 1736440562000000000