ADC

使用 Prometheus 监视 NetScaler、应用和应用安全

指标是在特定时间段内测量的数据的数字表示形式。指标数据对于跟踪系统随时间推移的 运行状况很有用。Prometheus 是一种开源监视工具,用于收集指标数据并使用记录数据的时间戳存储这些数据。

通过监视和分析指标,您可以跟踪应用程序的运行状况,检测任何异常,创建警报并采取必要的纠正措施来确保稳健的软件交付。

NetScaler 现在支持将指标直接导出到 Prometheus。您可以使用 NetScaler ADC 提供的丰富指标集来监视 NetScaler 的运行状况和应用程序运行状况。例如,您可以收集有关 CPU 和内存使用情况的指标以了解 NetScaler 的运行状况。同样,您可以使用每秒收到的 HTTP 请求数或活跃客户端数量等指标来监视应用程序运行状况。

将指标从 NetScaler 导出到普罗米修斯

NetScaler 支持 Prometheus 拉取模式和推送模式。在拉取模式下,您需要配置一个时间序列配置文件,Prometheus 定期查询该配置文件并直接提取指标数据,中间无需导出器资源。在提取模式下,您可以为没有超级用户权限的用户启用只读访问权限,以便将指标导出到 Prometheus。使用 Grafana,您可以可视化导出到 Prometheus 的 NetScaler 指标,以便于解释和理解。

下图显示了 Prometheus 和 Grafana 与 NetScaler 的集成。

NetScaler 与 Prometheus 和 Grafana 集成

使用 Grafana 配置将指标从 NetScaler 导出到 Prometheus 并进行可视化

您必须执行以下步骤来配置从 NetScaler 导出到 Prometheus 的指标并使用 Grafana 对其进行可视化。

  1. 使用时间序列分析配置文件配置 NetScaler,用于将指标导出到 Prometheus。
  2. 安装 prometheus 并使用 NetScaler 的特定参数对其进行配置。
  3. 在 Grafana 中将普罗米修斯添加为数据源。
  4. 在 Grafana 中创建可视化

在 NetScaler 上配置时间序列分析配置文件以支持 Prometheus 拉取模式

使用 NetScaler CLI 执行以下步骤配置拉取模式:

  1. 创建类型为时间序列的分析配置文件。指定包含所需的 NetScaler 指标的架构文件。

     add analytics profile <timeseries_profile_name> -type timeseries -schemaFile <name_of_schema_file> -outputMode Prometheus -serveMode PULL -metrics ENABLED
    <!--NeedCopy-->
    

    在此命令中:

    -timeseries_profile_name:指定时间序列配置文件名称。 -schemaFile: 使用 NetScaler 计数器指定架构文件的名称。默认情况下,会配置一个包含计数器列表的架构文件 /var/metrics_conf/schema.json。路径 /var/metrics_conf/ 下还提供了包含所有支持计数器的参考架构文件 reference_schema.json。此架构文件可用作构建自定义计数器列表的参考。 指定架构文件时,会自动添加架构文件 /var/metrics_conf/ 的路径,您只需提及架构文件名即可。例如,如果您在 /var/metrics_conf/ 位置处创建了带有自定义计数器列表的架构文件 schema1.json, 则只需将文件名指定为 schema1.json。 -outputMode:将输出模式设置为 Prometheus。 -serveMode:指定 Prometheus 拉取模式。 -metrics: 启用从 NetScaler 收集指标。

    注意:

    您可以使用 add 命令配置包含所有必要参数的分析配置文件。如果在创建配置文件后需要进行更改,则可以使用 set 命令采取相应的操作,例如禁用指标和更改服务器模式。您可以为非超级用户配置只读 Prometheus 访问权限。有关更多信息,请参阅为 非超级用户配置只读 Prometheus 访问权限

安装和配置 Prometheus 以从 NetScaler 导出指标

可以从 DockerHub 或 Quay 等存储库或 Prometheus 官方存储库下载 Prometheus。

要将 Prometheus 作为 Docker 容器运行,请使用以下命令:

docker run -dp 39090:9090 -v /tmp/prometheus.yml:/etc/prometheus/prometheus.yml --name native_prom prom/prometheus:latest
<!--NeedCopy-->

注意:

这里, /tmp/prometheus.yml 用作 prometheus.yml 文件的路径。取而代之的是,您可以指定虚拟机上的路径。

您必须使用 NetScaler 参数 prometheus.yml 进行编辑。

要从 NetScaler 中导出指标,必须在 Prometheus YAML 片段配置部分指定以下 NetScaler 特定参数。抓取配置部分指定了一组目标和配置参数,描述了如何抓取它们。

  • metrics_path:在 NetScaler 中指定 HTTP 资源路径以获取指标。此参数是可选的,默认路径为 /metrics。如果未定义任何值,则 Prometheus 会添加“/metrics”作为路径。
  • username:指定 NetScaler 用户名。
  • password:指定 NetScaler 密码。
  • targets:指定需要从中导出指标的 NetScaler 的 IP 地址和要公开的端口。
  • profilename :指定配置文件的名称来代替 profilenameprofilename 参数是可选的,接受用户定义的时间序列配置文件名称。如果 profile name 参数不存在, 则将 ns_analytics_time_series_profile 视为配置文件名称。
  • follow_redirects:配置 HTTP 请求是否遵循 HTTP 3xx 重定向。此参数是可选的,默认值由 Prometheus 设置为 true。如果要配置此参数,则必须将该值设置为 true

下面是 Prometheus YAML 的片段配置部分,用于将 NetScaler IP 地址添加为 Prometheus 上的目标以导出指标。在这里,使用 HTTP 作为方案。可以使用 HTTP 或 HTTPS。

  scrape_configs:
    - job_name: 'vpx2_metrics_direct'
      metrics_path: /metrics
      params:
        profilename:
        - 'profile_name'
      basic_auth:
        username: 'prom_user'
        password: 'user_password'
      scheme: http
      scrape_interval: 30s
      follow_redirects: true
      static_configs:
      - targets: ['10.102.34.231:80']
<!--NeedCopy-->

在 Grafana 中将 Prometheus 添加为数据源

如果需要使用 Grafana 控制板对指标进行可视化,则需要在 Grafana 中将 Prometheus 添加为数据源。有关更多信息,请参阅 在 Grafana 中将 Prometheus 添加为数据源

在 Grafana 中创建指标的可视化

可以创建 Grafana 控制板并选择关键指标和相应的可视化类型。

以下过程显示向 Grafana 面板添加指标和创建示例可视化控制板。

  1. 指定面板标题。
  2. 在查询选项卡中,为查询 A 指定所需的指标。
  3. 在“设置”选项卡中,选择“可视化”类型。

您可以在 Grafana 中修改数据及其表示形式。有关更多信息,请参阅 Grafana 文档

下面是包含一些 NetScaler 指标的 Grafana 控制板示例:

Grafana 控制板示例

在此控制板中,您可以查看不同 NetScaler 指标的图表,例如:

  • vsvr_tot_Hits:显示虚拟服务器收到的请求数。
  • cc_cpu_use:显示 CPU 利用率百分比。
  • http_tot_Requests:显示收到的 HTTP 请求。
  • serv_tot_serviced:显示正在处理的请求。
  • mem_cur_used_size: 显示 NetScaler 设备当前使用的内存。

普罗米修斯图表示例

使用 Prometheus 表达式浏览器,您可以显示 Prometheus 服务器收集的时间序列指标。您可以通过在浏览器 prometheu-server-ip-address/graph 中指向来访问表达式浏览器。 您可以输入表达式,然后以表格或图表的形式查看一段时间内的结果。通过在表达式字段中键入指标名称来指定要显示的确切指标。您可以使用不同的面板指定多个计数器。

下图显示了两个 NetScaler 指标的 Prometheus 图形 cpu_usehttp_tot_requests

普罗米修斯控制板示例

Grafana 控制板示例

NetScaler 支持 Grafana 上的示例控制板。有关 Grafana 支持的示例控制板的完整列表及其详细信息,请参阅 Grafana 的示例控制板

可以从 NetScaler 下载页面下载示例控制板。

其他信息

包含需要导出的 NetScaler 计数器的架构

指标收集器导出配置的架构文件中存在的计数器。/var/metrics_conf/schema.json 文件是在分析配置文件中配置的默认架构文件。

架构文件是实体类型和关联计数器的列表。在架构中,所有全局或系统级计数器都按实体类型 netscaler 分组。一些全局计数器是 CPU 使用率 (cpu_use)、管理 CPU 使用率 (mgmt_cpu_use) 和收到的 HTTP 请求总数 (http_tot_Requests)。特定于服务组 lbvservercsvserver 等的计数器列在相应的实体类型下。

以下是身份验证虚拟服务器 (vserver_authn) 实体的 schema.json 文件中计数器的示例。

"vserver_authn":
  [
      {"name":"si_tot_Requests","rate":"True"},
      {"name":"si_tot_Responses","rate":"True"},
      {"name":"si_tot_RequestBytes","rate":"True"},
      {"name":"si_cur_state","rate":"False"},
      {"name":"si_tot_ResponseBytes","rate":"True"},
      {"name":"si_peer_port","rate":"True"},
      {"name":"vsvr_Protocol","rate":"False"}
  ]
<!--NeedCopy-->

下表说明了此示例中提到的计数器:

计数器名称 说明
si_tot_Requests 在此服务或虚拟服务器上收到的请求总数。
si_tot_Responses 在此服务或虚拟服务器上收到的响应总数。
si_tot_RequestBytes 在此服务或虚拟服务器上收到的请求字节总数。
si_cur_state 虚拟服务器的当前状态。
si_tot_ResponseBytes 在此服务或虚拟服务器上收到的响应字节总数。
si_peer_port 运行服务的端口。
vsvr_Protocol 与虚拟服务器相关的协议。

可以将 rate 字段设置为 True 需要导出计数器的速率值。例如,如果为 si_tot_Requestsrate 设置为 True,则会导出速率 si_tot_Requests

以下是来自 netscaler 实体的计数器示例。

"netscaler":
  [
      {"name":"cpu_use","rate":"False"},
      {"name":"mgmt_cpu_use","rate":"False"},
      {"name":"tcp_tot_rxpkts","rate":"True"},
      {"name":"tcp_tot_rxbytes","rate":"True"},
      {"name":"tcp_tot_txpkts","rate":"True"},
      {"name":"tcp_tot_txbytes","rate":"True"},
      {"name":"tcp_cur_ClientConnEst","rate":"False"},
      {"name":"tcp_cur_ServerConnEst","rate":"False"},
      {"name":"tcp_cur_ClientConn","rate":"False"},
      {"name":"tcp_cur_ClientConnClosing","rate":"False"},
      {"name":"tcp_tot_ClientOpen","rate":"True"},
      {"name":"tcp_cur_ServerConn","rate":"False"},
      {"name":"tcp_cur_ServerConnClosing","rate":"False"},
      {"name":"http_tot_Requests","rate":"True"},
      {"name":"http_tot_Responses","rate":"True"},
      {"name":"http_tot_Gets","rate":"True"},
      {"name":"http_tot_Posts","rate":"True"},
      {"name":"http_tot_Others","rate":"True"},
  ]
<!--NeedCopy-->

下表说明了此示例中提到的计数器:

计数器名称 说明
cpu_use 跟踪 CPU 利用率百分比(CPU 利用率百分比 x 10)。
tcp_tot_rxpkts 已接收 TCP 数据包。
tcp_tot_rxbytes 收到的 TCP 数据的字节数。
tcp_tot_txpkts 传输的 TCP 数据包。
tcp_tot_txbytes 传输的 TCP 数据的字节数。
tcp_cur_ClientConnEst 当前客户端连接处于“已建立”状态,这表明 NetScaler 设备和客户端之间可以进行数据传输。
tcp_cur_ServerConnEst 当前服务器连接处于“已建立”状态,这表示可以在 NetScaler 设备和服务器之间进行数据传输。
tcp_cur_ClientConn 客户端连接,包括处于“正在打开”、“已建立”和“正在关闭”状态的连接。服务器连接,包括处于“正在打开”、“已建立”和“正在关闭”状态的连接。
tcp_cur_ClientConnClosing 处于“关闭”状态的客户端连接,这表示连接终止过程已启动但尚未完成。
tcp_cur_ServerConn 服务器连接,包括处于“正在打开”、“已建立”和“正在关闭”状态的连接。
tcp_cur_ServerConnClosing 服务器连接处于“关闭”状态,这表示连接终止过程已启动但尚未完成。
http_tot_Requests 此计数器跟踪使用 GET 方法收到的 HTTP 请求。
http_tot_Responses 此计数器跟踪使用 POST 方法收到的 HTTP 请求。
http_tot_Gets 此计数器跟踪使用 GET 方法收到的 HTTP 请求。
http_tot_Posts 此计数器跟踪收到的 HTTP 请求。
http_tot_Others 此计数器跟踪使用 GET 和 POST 以外的方法收到的 HTTP 请求。

以下是来自 vserver_ssl 实体的计数器示例。

"vserver_ssl":
  [
    {"name":"ssl_ctx_tot_session_hits","rate":"True"},
    {"name":"ssl_ctx_tot_session_new","rate":"True"},
    {"name":"ssl_ctx_tot_enc_bytes","rate":"True"},
    {"name":"ssl_ctx_tot_dec_bytes","rate":"True"},
  ]
<!--NeedCopy-->

下表说明了此示例中提到的 SSL 计数器:

计数器名称 说明
ssl_ctx_tot_session_hits 此计数器跟踪会话命中次数。
ssl_ctx_tot_session_new 此计数器跟踪创建的新会话的数量。
ssl_ctx_tot_enc_bytes 此计数器跟踪每个 SSL 虚拟服务器的加密字节数。
ssl_ctx_tot_dec_bytes 此计数器跟踪每个 SSL 虚拟服务器解密的字节数。

为非超级用户配置只读 Prometheus 访问权限

执行以下步骤为非超级用户配置只读 Prometheus 访问权限。

  1. 向 NetScaler 设备添加新用户。

    add system user <ns_user_name> <ns_user's_password> -externalAuth enabled -promptString user-%u-at-%T logging enaBLED
    <!--NeedCopy-->
    

    示例:

    add system user nspaul nspaul -externalAuth enabled -promptString user-%u-at-%T logging enaBLED
    <!--NeedCopy-->
    
  2. 为只读用户创建命令策略。此命令策略允许对下的任何文件进行只读访问 /var/nslog/ directory

    add system cmdPolicy read-only-prometheus ALLOW "(^man.*)|(^show\s+(?!system)(?!configstatus)(?!ns ns\.conf)(?!ns savedconfig)(?!ns runningConfig)(?!gslb runningConfig)(?!audit messages)(?!techsupport).*)|(^stat.*)|(show system file .* -filelocation "/var/nslog")"
    <!--NeedCopy-->
    
  3. 如果指标仅写入某个文件,您甚至可以限制用户访问权限,使他们只能获取该特定文件。

    add system cmdPolicy read-only-prometheus ALLOW "(^man.*)|(^show\s+(!system)(!configstatus)(!ns ns\.conf)(!ns savedconfig) (!ns runningConfig)(!gslb   runningConfig)(!audit messages)(!techsupport).*)|(^stat.*) |(show system file metrics_prom_<name_of_timeseries_profile>.log -filelocation "/var/nslog")"
    <!--NeedCopy-->
    

    注意:

    show system file 命令中,指定您已配置的时间序列配置文件名称,以代替 name_of_ timeseries_profile

  4. 使用命令策略绑定用户。

    bind system user <userName> ((<policyName> <priority>) | -partitionName <string>)
    <!--NeedCopy-->
    

    例如:

    bind system user user1 read-only-prometheus 0
    <!--NeedCopy-->
    

要取消绑定用户并将其从命令策略中删除,请使用以下命令:

  1. 取消已配置用户与系统命令策略的绑定。

    unbind system user <userName> (<policyName> | -partitionName <string>
    <!--NeedCopy-->
    

    例如:

    unbind system user user1 read-only-prometheus
    <!--NeedCopy-->
    
  2. 从 NetScaler 中删除命令策略。

    rm system cmdPolicy read-only-prometheus
    <!--NeedCopy-->
    

订阅多个时间序列配置文件的计数器

NetScaler 支持创建多个时间序列配置文件,并为每个配置文件指定不同的计数器集。此外,您只能根据需要导出计数器。

必须创建多个 schema.json 文件,其中包含必要的计数器,这些计数器具有唯一的名称和 .json 扩展名,才能配置多个时间序列配置文件。路径下有一个参考架构文件 reference_schema.json/var/metrics_conf/ 供您参考。

两个新的时间序列配置文件配置如下:

  add analytics profile ns_analytics_timeseries_profile_1   -type timeseries -schemaFile schema1.json

  set analytics profile  ns_analytics_timeseries_profile_1 -outputMode prometheus -serveMode PULL -metrics ENABLED

  add analytics profile ns_analytics_timeseries_profile_2   -type timeseries -schemaFile schema2.json

  set analytics profile  ns_analytics_timeseries_profile_2 -outputMode prometheus -serveMode PULL -metrics ENABLED
  <!--NeedCopy-->

在此示例中, schema1.jsonschema2.json 有不同的计数器集。

普罗米修斯配置

示例 prometheus.yml 文件的配置如下所示:

scrape_configs:
  - job_name: 'vpx2_metrics_direct'
    metrics_path: /metrics
    params:
     - 'profile_name'
    basic_auth:
     username: 'prom_user'
     password: 'user_password'
    scheme: https
    scrape_interval: 30s
    follow_redirects: true
    static_configs:
     - targets: ['<ADC1-ip>:<port>', '<ADC2-ip>:<port>']
<!--NeedCopy-->