0

Some context

I have an SNMP exporter running as a service on a server which allows me to access the data of some PDU using the following URL: http://my.host.name:9116/snmp?target=10.0.1.200&auth=public_v1&module=apcups

I have a Prometheus server which I have configured using puppet on the same host. I want to add a scrape job to retrieve the information provided by the PDUs.

Relying on the tutorial on this page and some other sources, I was able to confirm that manually modifying the /etc/prometheus/prometheus.yaml file to insert the following gives me the expected result. For shortness I only include the relevant part of the configuration file.

- 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

This works exactly as I expected, I can plot my power consumption in Prometheus and Grafana etc.

My Issue

To configure Puppet to produce the prometheus configuration above, I added the following job in the profile in which prometheus is configured (I have another job scraping node_exporter on a number of hosts from which I was able to take inspiration from):

        {
          '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]',
          }
        },

The problem is this produces the erroneous prometheus.yaml file below with additional quotes around the metrics_path and three elements inside params.

 - 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]"

The configuration parser of Prometheus then fails with the following error:

  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

Naively, I thought removing the quotes around /snmp and around the square brackets in the params would fix my problem, i.e.:

          '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],

But this results in a Puppet syntax error.

My question

How do I obtain the desired /etc/prometheus/prometheus.yaml showed at the top of this question through Puppet? My guess is there must be some sort of syntax that allows the production of a string of text without the quotes but I don't know how.

I am using Puppet version 8.2.0 and the puppet/prometheus v13.3.0 module.

1 Answer 1

1

You're halfway there - you don't want to remove the quotes, but move them:

'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'],

In both YAML and the Puppet DDL, [ and ] indicate arrays, in this case arrays of strings; target: [10.0.1.200] in YAML is a single-element array containing a single string (10.0.1.200), which could also be written as:

      target:
        - 10.0.1.200

To generate correct Prometheus config, you need to ensure that target, auth and module are generated as arrays, which simply means that they need to be Puppet arrays on the way in. This means having the [ and ] outside of the quotes, as they're part of the language syntax rather than part of the strings, but then because Puppet requires strings to be quoted, your actual values (10.0.1.200, public_v1 etc) need to be quoted, otherwise you get the Puppet syntax errors you're reporting.

The Puppet Prometheus module simply converts scrape_configs directly into YAML, so Puppet arrays are expressed as YAML arrays - and erroneous Puppet strings get expressed as YAML strings, even when you don't want them to be.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .