基于Python的Citrix Netscaler Prometheus导出器
项目描述
netscaler_exporter
概述
描述
Prometheus导出器,用于Netscaler(CITRIX ADC)
此导出器从netscaler HTTP REST API收集指标。
它是一个Python HTTP服务器,将指标暴露给http(默认端口9247),然后可以被Prometheus抓取。
此导出器深受citrix-adc-metrics-exporter的启发。
它允许您通过在YAML配置文件中添加它们并指定Prometheus配置中的目标参数来收集多个netscaler。
配置:(参见conf/netscaler_exporter.yml)
netscalers:
- host: host.domain
# port: 443
user: 'user'
password: 'password'
# protocol: https
# verify_ssl: false
# timeout: 20
# keep_session: true # default
# default_labels:
# - name: netscaler_specific_data
# value: my_label
# proxy:
# url: http://my.proxy.domain:port/
# protocol: https
weblisten:
address: 0.0.0.0
port: 9259
logger:
level: info
facility: syslog
metrics_file: "conf/metrics/*_metrics.yml"
用法
导出器可以作为带有模块安装的Unix命令运行,也可以作为无安装的独立Python脚本运行。
最简单的方法是使用pip安装
pip3 install --upgrade netscaler_exporter
然后您可以使用模块安装器在/usr/local/bin/netscaler_exporter或[venv]/bin/netscaler_exporter(对于venv环境)创建的条目点。推荐在venv中使用。
要使用导出器,需要安装一些包。这可以通过以下方式完成
pip3 install -r requirements.txt
requirements.txt的内容
Prometheus-client>=0.8.0
requests==2.23.0
PyYAML==5.3.1
tenacity==6.2.0
urllib3>=1.25.9
Jinja2>=2.11.2
python-dateutil>=0.6.12
- 考虑,在/tmp文件夹中提取归档文件;这将生成一个文件夹/tmp/netscaler_exporter_[version]。
- 在您希望的位置创建一个目录,例如/opt/netscaler_exporter_[version],
- 将/tmp/netscaler_exporter_[version]/netscaler_exporter_package目录移动到/opt/eeam_exporter_[version]
- 在/opt/netscaler_exporter_[version]目录中创建一个命令文件以启动导出器
vi /opt/netscaler_exporter_X.Y.Z/netscaler_exporter_cmd
#!/usr/libexec/platform-python
# -*- coding: utf-8 -*-
import re
import sys
from netscaler_exporter.netscaler_exporter import main
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
sys.exit(main())
- 然后编辑conf/config.yml文件并添加您的设置。
- 通过执行命令在尝试模式下测试您的配置
默认dumy配置文件的示例
python3 veeam_exporter_cmd -n -v
导出器命令行选项
要启动导出器
./netscaler_exporter &
默认情况下,它将加载config.yml文件以执行操作。
详细选项
usage: netscaler_exporter_cmd [-h] [-b BASE_PATH] [-c CONFIG_FILE]
[-F FILTER_PATH] [-f LOGGER.FACILITY]
[-l {error,warning,info,debug}]
[-o METRICS_FILE] [-m METRIC] [-n]
[-t TARGET] [-w WEB.LISTEN_ADDRESS] [-V] [-v]
collector for Citrix Netscaler.
optional arguments:
-h, --help show this help message and exit
-b BASE_PATH, --base_path BASE_PATH
set base directory to find default files.
-c CONFIG_FILE, --config_file CONFIG_FILE
path to config files.
-F FILTER_PATH, --filter_path FILTER_PATH
set filter directory to find filter files.
-f LOGGER.FACILITY, --logger.facility LOGGER.FACILITY
logger facility (syslog or file path).
-l {error,warning,info,debug}, --logger.level {error,warning,info,debug}
logger level.
-o METRICS_FILE, --metrics_file METRICS_FILE
collect the metrics from the specified file instead of
config.
-m METRIC, --metric METRIC
collect only the specified metric name from the
metrics_file.
-n , --dry_mode collect the metrics then exit; display results to
stdout.
-t TARGET, --target TARGET
In dry_mode collect metrics on specified target.i
Default first from config file.
-w WEB.LISTEN_ADDRESS, --web.listen-address WEB.LISTEN_ADDRESS
Address to listen on for web interface and telemetry.
-V, --version display program version and exit..
-v , --verbose verbose mode; display log message to stdout.
为了测试您的配置,您可以通过启动导出程序以dry_mode模式进行。
./netscaler_exporter -v -n -t host.domain
此命令将尝试连接到'host.domain' netscaler,使用config.yml中指定的参数,暴露收集的指标,然后退出。
Prometheus配置
由于导出器中可以设置多个netscaler,Prometheus通过在url中添加target参数来标识每个服务器。 "target"必须在导出器配置文件中相同(按字典顺序)。
- job_name: "citrix_netscaler"
scrape_interval: 30s
scrape_timeout: 20s
metrics_path: /metrics
static_configs:
- targets: [ netscalerhost.domain ]
labels:
environment: "PROD"
- targets: [ netscalerhost2.domain]
labels:
environment: "PROD"
# file_sd_configs:
# - files: [ "/etc/prometheus/citrix_exp/*.yml" ]
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: "netscaler-exporter-hostname.domain:9258" # The netscaler exporter's real hostname.
指标
收集的指标在conf/metrics文件夹中的单独文件中定义。所有值、计算和标签都在指标文件中定义,这意味着导出器在内部不对值进行任何操作。配置完全驱动值的渲染方式。
目前收集的指标有
所有指标都在配置文件中定义(conf/metrics/*.yml)。您可以从这里检索所有指标名称。其中大部分都有帮助文本。
STATS | NS nitro名称 |
---|---|
LB vserver统计 | "lbvserver" |
CS vserver统计 | "csvserver" |
HTTP统计 | "protocolhttp" |
TCP统计 | "protocoltcp" |
UDP统计 | "protocoludp" |
IP统计 | "protocolip" |
接口统计 | "Interface"(大写“I”) |
服务统计 | "service" |
服务组统计 | "services" |
带宽容量统计 | "nscapacity" |
SSL统计 | "ssl" |
SSL证书统计 | "sslcertkey" |
SSL vserver统计 | "sslvserver" |
系统信息统计 | "system" |
系统内存统计 | "systemmemory" |
系统CPU统计 | "systemcpu" |
高可用性统计 | "hanode.yml" |
AAA统计 | "aaa" |
ADC探测成功 | "1"如果登录成功,否则"0" |
扩展指标
导出的指标定义在YAML配置文件中。值可以使用Jinja2模板语言。配置格式灵感来源于Ansible任务表示。因此,指标配置文件由执行的动作列表组成。
有五种可能的操作
- url:从HTTP API收集指标
- set_fact:将值分配给变量
- actions:执行一系列(子)操作
- metrics:定义要暴露给Prometheus的指标
- debug:将调试文本显示到记录器。
所有操作都有默认"属性"
- name:操作或指标计数器的名称。
- vars:将变量设置到全局符号表中。
- with_items:在当前操作上循环列表项。
- loop_var:设置将接收循环中当前值的变量的名称。默认为'item'。
- when:一组条件(和)必须检查并为真才能执行操作。
"属性"按之前表中指定的顺序进行分析;这意味着您不能在vars部分使用"item"变量(从'with_items'指令获得),因为它在'vars'部分评估时尚未定义。如果您需要该功能,您将不得不考虑在'actions'部分中包含'with_items'(请参阅metrics/backup_jobs_sessions_metrics.yml)。
action | parameter | description | remark |
---|---|---|---|
url | 表示要收集的实体(不带'/nitro/v1')的字符串 | 例如:/stat/system | |
var_name | 存储结果的变量的名称。默认为'_root',表示结果JSON对象直接存储在符号表中。 | ||
set_fact | 要定义的变量列表 | ||
var_name: value | |||
metrics | 定义要暴露的指标列表 | ||
metric_prefix | 要添加到所有指标名称的前缀 | 最终名称将为[metric_prefix]_[metric_name] | |
'a metric' | name | 指标的名称 | |
help | 添加到指标中的帮助消息(并在Grafana资源管理器中显示) | ||
type 'gauge' or 'counter' | Prometheus指标的类型 | ||
value | 数值本身 | ||
labels | 一组名称值对,用于确定指标 |
指标配置文件至少包含包含两个操作的动作,一个用于在netscaler上收集具有'url'的值,另一个用于使用'metrics'定义结果指标给prometheus
示例
---
- name: my_custom_metric
actions:
# first action
- name: collect elements
url: /stat/system
# second action
- name: proceed elements
metric_prefix: citrixadc_custmetric
metrics:
- name: cpu_number
help: constant number of cpu for appliance
type: counter
value: "{{ system.numcpus }}"
...