0

I'm new to Python, learning python now, I have Prometheus configuration file, need to add few fields and values in one node for now. when loading in rt mode, it goes to ordereddict format and unable to get into the nodes, to add fields

    # my global config
global:
  scrape_interval:     60s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 60s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
  alertmanagers:
    - scheme: http
      path_prefix: /
      static_configs:
        - targets:
          - 127.0.0.1:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  - "first_rules.yml"
  - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:

  - job_name: 'cassandra'
    static_configs:
    - targets: ['localhost:18080']

  - job_name: 'machine'
    static_configs:
    - targets: ['localhost:9100']

under scrape_configs, for job_name: 'machine' I need to following data in using Ruamel yaml python,

- job_name: 'machine'
    honor_labels: true
    static_configs:
    - targets: ['localhost:9100']
      labels:
        ip: 'x.x.x.x

below code I tried to but getting error

import sys
from ruamel.yaml import YAML
from collections import OrderedDict
from pathlib import Path

yaml=YAML()
yaml.preserve_quotes = True
yaml.explicit_start = True
yaml.default_flow_style = False
with open('D:\***\prometheus.yml') as fp:
    data = yaml.load(fp)

print("data = %s" %(data))

for elem in data:
    if elem['scrape_configs']['static_configs']['job_name']== 'machine':
      elem['honor_labels']='true'
    break  # no need to iterate further
yaml.dump(data, sys.stdout)

error is:

 if elem['scrape_configs']['static_configs']['job_name']== 'machine':
TypeError: string indices must be integers

Process finished with exit code 1

How can I add a new entry to existing node using python?

I followed the Anton's answer in this question but still unable to get the

for elem in data:
    print('elem', elem)
    if elem['job_name'] == 'machine':
        print('elem->honor_labels', elem['honor_labels'])
        elem['honor_labels'] = ['true']

error stack:

elem global
  File "D:/***/.idea/editYaml.py", line 19, in <module>
    if elem['job_name'] == 'machine':
TypeError: string indices must be integers

how to access job_name if we have more nodes in yaml? please guide me.

anand babu
  • 335
  • 1
  • 3
  • 13
  • Your `scrape_config` is a list you will have to address the right item of the list by its index or iterate over them. Also `job_name` is not below `static_config`. – Klaus D. Mar 16 '19 at 04:04
  • The library is called `ruamel.yaml`, the recommended extension for YAML files has been `.yaml` since at least 2006. And if you had taken the time to search for "TypeError string indices must be integers" here on [so] you would have found the duplicate as the first entry yourself. PEBKAC – Anthon Mar 16 '19 at 06:32
  • @KlausD. yes now reached the point, scrape_config its list of ordered dict, looking for adding those lines where condition matches in the list. – anand babu Mar 16 '19 at 19:46
  • This is a duplicate question, see the other topic and look at the answers. See "for key in data: value = data[key] if value['name'] == 'Stuart':" you are trying to do "for elem in data: if elem['job_name'] == 'machine':" at line 19. Do you see what you are doing wrong? – nickl- Mar 16 '19 at 20:24

0 Answers0