Initializing System...

Discover Intelligence

Press Enter to search or Esc to close

HƯỚNG DẪN GIÁM SÁT TRẠNG THÁI HOST VCENTER TOÀN DIỆN VỚI GRAFANA & INFLUXDB (FLUX)

HƯỚNG DẪN GIÁM SÁT TRẠNG THÁI HOST VCENTER TOÀN DIỆN VỚI GRAFANA & INFLUXDB (FLUX) - FixNhanh

Trong các hạ tầng ảo hóa VMware vCenter quy mô lớn, việc theo dõi trạng thái sức khỏe của các ESXi Host là cực kỳ quan trọng. Bài viết này sẽ hướng dẫn bạn cách xây dựng một Dashboard giám sát trạng thái Host chuyên sâu sử dụng InfluxDB (Flux Query)Grafana, dựa trên cấu trúc dữ liệu thực tế.

1. Cấu Trúc Dữ Liệu (Schema)

Để hệ thống hoạt động, dữ liệu cần được đẩy vào InfluxDB với cấu trúc sau:

  • Bucket: vcenter-check-host
  • Measurement: vcenter_host_status
  • Fields: status (1: Connected, 0: Not Responding, 2: Maintenance)
  • Tags: host, pool, text_status

2. Visualizing Trạng Thái Với Stat Panels

Hệ thống giám sát này sử dụng các Stat Panels để hiển thị nhanh các con số quan trọng ngay đầu Dashboard.

Flux Query - Total Hosts
from(bucket: "vcenter-check-host")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "vcenter_host_status")
  |> filter(fn: (r) => r["_field"] == "status")
  |> filter(fn: (r) => r["pool"] =~ /^${pool:regex}$/)
  |> last()
  |> group()
  |> count()

Tương tự, chúng ta sử dụng hàm filter với tag text_status để đếm các trạng thái cụ thể như: Connected, Maintenance, và Not Responding.

3. Bảng Trạng Thái Chi Tiết (Current Host Status Table)

Bảng này giúp quản trị viên xác diện nhanh chóng host nào đang nằm trong Pool nào và trạng thái thực tế (Real State) của nó.

Time Host Pool Real State
2024-03-31 14:00 esxi-prod-01 Cluster-A Connected
2024-03-31 14:05 esxi-backup-02 Cluster-B Maintenance

4. Theo Dõi Lịch Sử Lỗi (Failed Hosts History)

Sử dụng biểu đồ Time Series để theo dõi tần suất các host bị mất kết nối (Not Responding) theo thời gian. Điều này giúp phát hiện ra các sự cố mạng hoặc phần cứng mang tính chu kỳ.

from(bucket: "vcenter-check-host")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "vcenter_host_status")
  |> filter(fn: (r) => r["text_status"] == "Not Responding")
  |> aggregateWindow(every: v.windowPeriod, fn: last, createEmpty: false)
  |> group(columns: ["_time"])
  |> count()

💡 Pro Tip: Luôn cấu hình Variables trong Grafana cho PoolStatus để có thể lọc dữ liệu nhanh chóng khi cần xử lý sự cố trên diện rộng.

5. Full Dashboard JSON (Grafana)

Dưới đây là toàn bộ mã JSON của Dashboard. Bạn có thể sao chép và Import trực tiếp vào Grafana của mình.

{
  "annotations": {
    "list": [
      {
        "builtIn": 1,
        "datasource": {
          "type": "grafana",
          "uid": "-- Grafana --"
        },
        "enable": true,
        "hide": true,
        "iconColor": "rgba(0, 211, 255, 1)",
        "name": "Annotations & Alerts",
        "type": "dashboard"
      }
    ]
  },
  "editable": true,
  "fiscalYearStartMonth": 0,
  "graphTooltip": 0,
  "id": 583,
  "links": [],
  "panels": [
    {
      "datasource": {
        "type": "influxdb",
        "uid": "${DS_INFLUXDB}"
      },
      "fieldConfig": {
        "defaults": {
          "color": {
            "mode": "thresholds"
          },
          "mappings": [],
          "noValue": "0",
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "color": "blue"
              }
            ]
          }
        },
        "overrides": []
      },
      "gridPos": {
        "h": 8,
        "w": 6,
        "x": 0,
        "y": 0
      },
      "id": 9,
      "options": {
        "colorMode": "background",
        "graphMode": "none",
        "justifyMode": "auto",
        "orientation": "auto",
        "percentChangeColorMode": "standard",
        "reduceOptions": {
          "calcs": [
            "lastNotNull"
          ],
          "fields": "",
          "values": false
        },
        "showPercentChange": false,
        "textMode": "auto",
        "wideLayout": true
      },
      "pluginVersion": "12.0.0",
      "targets": [
        {
          "datasource": {
            "type": "influxdb",
            "uid": "${DS_INFLUXDB}"
          },
          "query": "from(bucket: \"vcenter-check-host\")\n  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)\n  |> filter(fn: (r) => r[\"_measurement\"] == \"vcenter_host_status\")\n  |> filter(fn: (r) => r[\"_field\"] == \"status\")\n  |> filter(fn: (r) => r[\"pool\"] =~ /^${pool:regex}$/)\n  |> last()\n  |> group()\n  |> count()",
          "refId": "A"
        }
      ],
      "title": "Total Hosts",
      "type": "stat"
    },
    {
      "datasource": {
        "type": "influxdb",
        "uid": "${DS_INFLUXDB}"
      },
      "fieldConfig": {
        "defaults": {
          "color": {
            "mode": "thresholds"
          },
          "mappings": [],
          "noValue": "0",
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "color": "red"
              },
              {
                "color": "green",
                "value": 1
              }
            ]
          }
        },
        "overrides": []
      },
      "gridPos": {
        "h": 8,
        "w": 6,
        "x": 6,
        "y": 0
      },
      "id": 8,
      "options": {
        "colorMode": "background",
        "graphMode": "none",
        "justifyMode": "auto",
        "orientation": "auto",
        "percentChangeColorMode": "standard",
        "reduceOptions": {
          "calcs": [
            "lastNotNull"
          ],
          "fields": "",
          "values": false
        },
        "showPercentChange": false,
        "textMode": "auto",
        "wideLayout": true
      },
      "pluginVersion": "12.0.0",
      "targets": [
        {
          "datasource": {
            "type": "influxdb",
            "uid": "${DS_INFLUXDB}"
          },
          "query": "from(bucket: \"vcenter-check-host\")\n  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)\n  |> filter(fn: (r) => r[\"_measurement\"] == \"vcenter_host_status\")\n  |> filter(fn: (r) => r[\"_field\"] == \"status\")\n  |> filter(fn: (r) => r[\"pool\"] =~ /^${pool:regex}$/)\n  |> last()\n  |> filter(fn: (r) => r[\"text_status\"] == \"Connected\")\n  |> group()\n  |> count()",
          "refId": "A"
        }
      ],
      "title": "Hosts Connected",
      "type": "stat"
    },
    {
      "datasource": {
        "type": "influxdb",
        "uid": "${DS_INFLUXDB}"
      },
      "fieldConfig": {
        "defaults": {
          "color": {
            "mode": "thresholds"
          },
          "mappings": [],
          "noValue": "0",
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "color": "green"
              },
              {
                "color": "yellow",
                "value": 1
              }
            ]
          }
        },
        "overrides": []
      },
      "gridPos": {
        "h": 8,
        "w": 6,
        "x": 12,
        "y": 0
      },
      "id": 7,
      "options": {
        "colorMode": "background",
        "graphMode": "none",
        "justifyMode": "auto",
        "orientation": "auto",
        "percentChangeColorMode": "standard",
        "reduceOptions": {
          "calcs": [
            "lastNotNull"
          ],
          "fields": "",
          "values": false
        },
        "showPercentChange": false,
        "textMode": "auto",
        "wideLayout": true
      },
      "pluginVersion": "12.0.0",
      "targets": [
        {
          "datasource": {
            "type": "influxdb",
            "uid": "${DS_INFLUXDB}"
          },
          "query": "from(bucket: \"vcenter-check-host\")\n  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)\n  |> filter(fn: (r) => r[\"_measurement\"] == \"vcenter_host_status\")\n  |> filter(fn: (r) => r[\"_field\"] == \"status\")\n  |> filter(fn: (r) => r[\"pool\"] =~ /^${pool:regex}$/)\n  |> last()\n  |> filter(fn: (r) => r[\"text_status\"] == \"Maintenance\")\n  |> group()\n  |> count()",
          "refId": "A"
        }
      ],
      "title": "Hosts in Maintenance",
      "type": "stat"
    },
    {
      "datasource": {
        "type": "influxdb",
        "uid": "${DS_INFLUXDB}"
      },
      "fieldConfig": {
        "defaults": {
          "color": {
            "mode": "thresholds"
          },
          "mappings": [],
          "noValue": "0",
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "color": "green"
              },
              {
                "color": "red",
                "value": 1
              }
            ]
          }
        },
        "overrides": []
      },
      "gridPos": {
        "h": 8,
        "w": 6,
        "x": 18,
        "y": 0
      },
      "id": 6,
      "options": {
        "colorMode": "background",
        "graphMode": "none",
        "justifyMode": "auto",
        "orientation": "auto",
        "percentChangeColorMode": "standard",
        "reduceOptions": {
          "calcs": [
            "lastNotNull"
          ],
          "fields": "",
          "values": false
        },
        "showPercentChange": false,
        "textMode": "auto",
        "wideLayout": true
      },
      "pluginVersion": "12.0.0",
      "targets": [
        {
          "datasource": {
            "type": "influxdb",
            "uid": "${DS_INFLUXDB}"
          },
          "query": "from(bucket: \"vcenter-check-host\")\n  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)\n  |> filter(fn: (r) => r[\"_measurement\"] == \"vcenter_host_status\")\n  |> filter(fn: (r) => r[\"_field\"] == \"status\")\n  |> filter(fn: (r) => r[\"pool\"] =~ /^${pool:regex}$/)\n  |> last()\n  |> filter(fn: (r) => r[\"text_status\"] == \"Not Responding\")\n  |> group()\n  |> count()",
          "refId": "A"
        }
      ],
      "title": "Hosts Not Responding",
      "type": "stat"
    },
    {
      "datasource": {
        "type": "influxdb",
        "uid": "${DS_INFLUXDB}"
      },
      "fieldConfig": {
        "defaults": {
          "color": {
            "mode": "thresholds"
          },
          "custom": {
            "align": "center",
            "cellOptions": {
              "type": "auto"
            },
            "filterable": true,
            "inspect": false
          },
          "mappings": [
            {
              "options": {
                "0": {
                  "color": "red",
                  "index": 0,
                  "text": "Not Responding"
                },
                "1": {
                  "color": "green",
                  "index": 1,
                  "text": "Connected"
                },
                "2": {
                  "color": "yellow",
                  "index": 2,
                  "text": "Maintenance"
                }
              },
              "type": "value"
            }
          ],
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "color": "green"
              }
            ]
          }
        },
        "overrides": [
          {
            "matcher": {
              "id": "byName",
              "options": "Real State"
            },
            "properties": [
              {
                "id": "mappings",
                "value": [
                  {
                    "options": {
                      "Connected": {
                        "color": "green",
                        "index": 0
                      },
                      "Disconnected": {
                        "color": "red",
                        "index": 3
                      },
                      "Maintenance": {
                        "color": "yellow",
                        "index": 1
                      },
                      "Not Responding": {
                        "color": "red",
                        "index": 2
                      }
                    },
                    "type": "value"
                  }
                ]
              }
            ]
          }
        ]
      },
      "gridPos": {
        "h": 10,
        "w": 24,
        "x": 0,
        "y": 8
      },
      "id": 4,
      "options": {
        "cellHeight": "sm",
        "footer": {
          "countRows": false,
          "fields": "",
          "reducer": [
            "sum"
          ],
          "show": false
        },
        "showHeader": true
      },
      "pluginVersion": "12.0.0",
      "targets": [
        {
          "datasource": {
            "type": "influxdb",
            "uid": "${DS_INFLUXDB}"
          },
          "query": "from(bucket: \"vcenter-check-host\")\n  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)\n  |> filter(fn: (r) => r[\"_measurement\"] == \"vcenter_host_status\")\n  |> filter(fn: (r) => r[\"_field\"] == \"status\")\n  |> filter(fn: (r) => r[\"pool\"] =~ /^${pool:regex}$/)\n  |> filter(fn: (r) => r[\"text_status\"] =~ /^${status:regex}$/)\n  |> last()\n  |> group()\n  |> keep(columns: [\"_time\", \"host\", \"pool\", \"text_status\"])\n  |> rename(columns: {_time: \"Time\", host: \"Host\", pool: \"Pool\", text_status: \"Real State\"})",
          "refId": "A"
        }
      ],
      "title": "Current Host Status Table",
      "type": "table"
    },
    {
      "datasource": {
        "type": "influxdb",
        "uid": "${DS_INFLUXDB}"
      },
      "fieldConfig": {
        "defaults": {
          "color": {
            "fixedColor": "semi-dark-green",
            "mode": "fixed"
          },
          "custom": {
            "hideFrom": {
              "legend": false,
              "tooltip": false,
              "viz": false
            }
          },
          "mappings": [
            {
              "options": {
                "Connected": {
                  "color": "#73BF69",
                  "index": 0
                },
                "Disconnected": {
                  "color": "#F2495C",
                  "index": 3
                },
                "Maintenance": {
                  "color": "#FF9900",
                  "index": 1
                },
                "Not Responding": {
                  "color": "#F2495C",
                  "index": 2
                }
              },
              "type": "value"
            }
          ]
        },
        "overrides": []
      },
      "gridPos": {
        "h": 12,
        "w": 12,
        "x": 0,
        "y": 18
      },
      "id": 10,
      "options": {
        "displayLabels": [],
        "legend": {
          "displayMode": "table",
          "placement": "right",
          "showLegend": true,
          "values": [
            "value",
            "percent"
          ]
        },
        "pieType": "donut",
        "reduceOptions": {
          "calcs": [
            "lastNotNull"
          ],
          "fields": "",
          "values": false
        },
        "tooltip": {
          "hideZeros": false,
          "maxHeight": 600,
          "mode": "single",
          "sort": "none"
        }
      },
      "pluginVersion": "12.0.0",
      "targets": [
        {
          "datasource": {
            "type": "influxdb",
            "uid": "${DS_INFLUXDB}"
          },
          "query": "from(bucket: \"vcenter-check-host\")\n  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)\n  |> filter(fn: (r) => r[\"_measurement\"] == \"vcenter_host_status\")\n  |> filter(fn: (r) => r[\"_field\"] == \"status\")\n  |> filter(fn: (r) => r[\"pool\"] =~ /^${pool:regex}$/)\n  |> last()\n  |> group(columns: [\"text_status\"])\n  |> count()\n  |> group()\n  |> sort(columns: [\"_value\"], desc: true)\n  |> rename(columns: {_value: \"Số lượng\", text_status: \"Status\"})",
          "refId": "A"
        }
      ],
      "title": "Host Status Distribution",
      "type": "piechart"
    },
    {
      "datasource": {
        "type": "influxdb",
        "uid": "${DS_INFLUXDB}"
      },
      "fieldConfig": {
        "defaults": {
          "color": {
            "mode": "palette-classic"
          },
          "custom": {
            "axisBorderShow": false,
            "axisCenteredZero": false,
            "axisColorMode": "text",
            "axisLabel": "",
            "axisPlacement": "auto",
            "barAlignment": 0,
            "barWidthFactor": 0.6,
            "drawStyle": "line",
            "fillOpacity": 10,
            "gradientMode": "none",
            "hideFrom": {
              "legend": false,
              "tooltip": false,
              "viz": false
            },
            "insertNulls": false,
            "lineInterpolation": "linear",
            "lineWidth": 1,
            "pointSize": 5,
            "scaleDistribution": {
              "type": "linear"
            },
            "showPoints": "auto",
            "spanNulls": false,
            "stacking": {
              "group": "A",
              "mode": "none"
            },
            "thresholdsStyle": {
              "mode": "off"
            }
          },
          "mappings": [],
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "color": "green"
              }
            ]
          }
        },
        "overrides": []
      },
      "gridPos": {
        "h": 12,
        "w": 12,
        "x": 12,
        "y": 18
      },
      "id": 11,
      "options": {
        "legend": {
          "calcs": [],
          "displayMode": "list",
          "placement": "bottom",
          "showLegend": true
        },
        "tooltip": {
          "hideZeros": false,
          "maxHeight": 600,
          "mode": "single",
          "sort": "none"
        }
      },
      "pluginVersion": "12.0.0",
      "targets": [
        {
          "datasource": {
            "type": "influxdb",
            "uid": "${DS_INFLUXDB}"
          },
          "query": "from(bucket: \"vcenter-check-host\")\n  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)\n  |> filter(fn: (r) => r[\"_measurement\"] == \"vcenter_host_status\")\n  |> filter(fn: (r) => r[\"_field\"] == \"status\")\n  |> filter(fn: (r) => r[\"pool\"] =~ /^${pool:regex}$/)\n  |> filter(fn: (r) => r[\"text_status\"] == \"Not Responding\")\n  |> aggregateWindow(every: v.windowPeriod, fn: last, createEmpty: false)\n  |> group(columns: [\"_time\"])\n  |> count()\n  |> rename(columns: {_value: \"Failed Hosts\"})",
          "refId": "A"
        }
      ],
      "title": "Failed Hosts History",
      "type": "timeseries"
    }
  ],
  "preload": false,
  "refresh": "1m",
  "schemaVersion": 41,
  "tags": [
    "vcenter",
    "vmware",
    "monitor"
  ],
  "templating": {
    "list": [
      {
        "current": {
          "text": "InfluxDB-16.31-fpt",
          "value": "tShT4yr4z"
        },
        "includeAll": false,
        "label": "Datasource",
        "name": "DS_INFLUXDB",
        "options": [],
        "query": "influxdb",
        "refresh": 1,
        "regex": "InfluxDB-16.31-fpt",
        "type": "datasource"
      },
      {
        "current": {
          "text": "All",
          "value": [
            "$__all"
          ]
        },
        "datasource": {
          "type": "influxdb",
          "uid": "${DS_INFLUXDB}"
        },
        "definition": "import \"influxdata/influxdb/schema\"\nschema.tagValues(bucket: \"vcenter-check-host\", tag: \"pool\")",
        "includeAll": true,
        "label": "Pool",
        "multi": true,
        "name": "pool",
        "options": [],
        "query": "import \"influxdata/influxdb/schema\"\nschema.tagValues(bucket: \"vcenter-check-host\", tag: \"pool\")",
        "refresh": 1,
        "regex": "",
        "sort": 1,
        "type": "query"
      },
      {
        "current": {
          "text": "All",
          "value": [
            "$__all"
          ]
        },
        "datasource": {
          "type": "influxdb",
          "uid": "${DS_INFLUXDB}"
        },
        "definition": "import \"influxdata/influxdb/schema\"\nschema.tagValues(bucket: \"vcenter-check-host\", tag: \"text_status\")",
        "includeAll": true,
        "label": "Status",
        "multi": true,
        "name": "status",
        "options": [],
        "query": "import \"influxdata/influxdb/schema\"\nschema.tagValues(bucket: \"vcenter-check-host\", tag: \"text_status\")",
        "refresh": 1,
        "regex": "",
        "sort": 1,
        "type": "query"
      }
    ]
  },
  "time": {
    "from": "now-5m",
    "to": "now"
  },
  "timepicker": {},
  "timezone": "",
  "title": "vCenter Check Host Monitor",
  "uid": "vcenter-check-host-monitor",
  "version": 8
}

Hy vọng bộ Dashboard này sẽ giúp bạn quản lý hạ tầng vCenter hiệu quả hơn. Nếu có bất kỳ câu hỏi nào, hãy để lại bình luận phía dưới!

FN

FixNhanh Infrastructure Team

Specialists in server administration, security, and cloud automation. We strive to provide the most accurate and up-to-date technical guides for the community.

Leave a Comment

FixNhanh Support
Hello! How can I help you with Linux or Windows today?