在 SNMP 匯出器上設定 Prometheus 抓取時出現 Puppet 語法問題

在 SNMP 匯出器上設定 Prometheus 抓取時出現 Puppet 語法問題

一些背景

我有一個 SNMP 導出器作為服務在伺服器上運行,它允許我使用以下 URL 存取某些 PDU 的資料:http://my.host.name:9116/snmp?target=10.0.1.200&auth=public_v1&module=apcups

我有一個 Prometheus 伺服器,我在同一主機上使用 puppet 配置了該伺服器。我想新增一個抓取作業來檢索 PDU 提供的資訊。

依賴教程在本頁和其他一些來源,我能夠確認手動修改文件/etc/prometheus/prometheus.yaml以插入以下內容可以為我帶來預期的結果。為了簡短起見,我只包含設定檔的相關部分。

- job_name: snmp_scrapper
  scrape_interval: 60s
  scrape_timeout: 10s
  metrics_path: /snmp
  static_configs:
  - targets:
    - my.host.name:9116
  params:
    target: [10.0.1.200]          # IP of the PDU on the private network
    auth: [public_v1]             # authentication method
    module: [apcups]              # module to retrieve APC PDU/UPS information

這完全符合我的預期,我可以在 Prometheus 和 Grafana 等中繪製我的功耗。

我的問題

為了配置 Puppet 來產生上面的 prometheus 配置,我在配置了 prometheus 的配置文件中添加了以下作業(我還有另一項作業在許多主機上抓取 node_exporter,我可以從中獲得靈感):

        {
          'job_name'        => 'snmp_scrapper',
          'scrape_interval' => '60s',
          'scrape_timeout'  => '10s',
          'metrics_path'    => '/snmp',
          'static_configs'  => [{ 'targets' => ['my.host.name:9116'], }],
          'params' => {
            'target' => '[10.0.1.200]',
            'auth' => '[public_v1]',
            'module' => '[apcups]',
          }
        },

問題是這會產生下面的錯誤文件,其中和 的三個元素prometheus.yaml周圍有額外的引號。metrics_pathparams

 - job_name: snmp_scrapper
   scrape_interval: 60s
   scrape_timeout: 10s
   metrics_path: "/snmp"
   static_configs:
   - targets:
     - my.host.name:9116
   params:
     target: "[10.0.1.200]"
     auth: "[public_v1]"
     module: "[apcups]"

Prometheus 的配置解析器失敗並出現以下錯誤:

  FAILED: parsing YAML file /etc/prometheus/prometheus.yaml20231023-1016214-j3iqrx: yaml: unmarshal errors:
  line 47: cannot unmarshal !!str `[10.0.1...` into []string
  line 48: cannot unmarshal !!str `[public...` into []string
  line 49: cannot unmarshal !!str `[apcups]` into []string

/snmp天真地,我認為刪除參數中方括號周圍的引號可以解決我的問題,即:

          'job_name'        => 'snmp_scrapper',
          'scrape_interval' => '60s',
          'scrape_timeout'  => '10s',
          'metrics_path'    => /snmp,
          'static_configs'  => [{ 'targets' => ['my.host.name:9116'], }],
          'params' => {
            'target' => [10.0.1.200],
            'auth' => [public_v1],
            'module' => [apcups],

但這會導致 Puppet 語法錯誤。

我的問題

如何/etc/prometheus/prometheus.yaml透過 Puppet 獲得此問題頂部所需的顯示?我的猜測是一定有某種語法允許生成不帶引號的文字字串,但我不知道如何。

我正在使用 Puppet 版本 8.2.0 和木偶/普羅米修斯v13.3.0 模組。

答案1

你已經完成了一半 - 你不想刪除引號,而是移動它們:

'job_name'        => 'snmp_scrapper',
      'scrape_interval' => '60s',
      'scrape_timeout'  => '10s',
      'metrics_path'    => '/snmp',
      'static_configs'  => [{ 'targets' => ['my.example.com:9116'], }],
      'params' => {
        'target' => ['10.0.1.200'],
        'auth' => ['public_v1'],
        'module' => ['apcups'],

在 YAML 和 Puppet DDL 中,[and]表示數組,在本例中為字串數組;target: [10.0.1.200]YAML 中是一個包含單一字串 ( ) 的單元素數組10.0.1.200,也可以寫成:

      target:
        - 10.0.1.200

要產生正確的 Prometheus 配置,您需要確保targetauthmodule生成為數組,這意味著它們在傳入時必須是 Puppet 數組[] ,但由於Puppet 需要引用字串,因此需要引用您的實際值( 等),否則您會收到報告的 Puppet 語法錯誤10.0.1.200public_v1

Puppet Prometheus 模組只是scrape_configs直接轉換為 YAML,因此 Puppet 陣列表示為 YAML 陣列 - 並且錯誤的 Puppet 字串會表示為 YAML 字串,即使您不希望它們如此。

相關內容