#!/usr/bin/env python2

import io
import os
import yaml

with open('/etc/prometheus.yml', 'r') as stream:
    prom_config = yaml.load(stream)
pmm_config = {'users': [[x for x in prom_config['scrape_configs'] if x['job_name'] == 'linux'][0]['basic_auth']]}

if pmm_config['users'][0]['username'] != 'pmm' or pmm_config['users'][0]['password'] != 'pmm':
    with io.open('/srv/update/pmm-manage.yml', 'w', encoding='utf8') as outfile:
        yaml.dump(pmm_config, outfile, default_flow_style=False, allow_unicode=True)
    os.chmod('/srv/update/pmm-manage.yml', 0640)
