ricoberger / script_exporter

Prometheus exporter to execute scripts and collect metrics from the output or the exit status.
MIT License
354 stars 82 forks source link

Ability to parse environment variable in config.yaml #109

Closed deviarchscs closed 10 months ago

deviarchscs commented 10 months ago

Problem

I have a scenario where I have to use the same script multiple times with different parameters. (ie: I have to count emails number in several mailboxes). The script_exporter is able to do that well but it is insecure and cumbersome to do this since I have to hardcode passwords directly in the config file. Ssee example below:

    scripts:
      - name: myemail_1
        command: java
        args:
          - -jar
          - /usr/src/CountMail.jar
        env:
          EMAIL: myemail_1@domain.com
          EMAIL_USERNAME: my_username_1
          EMAIL_PASSWORD: my_password_1
      - name: myemail_2
        command: java
        args:
          - -jar
          - /usr/src/CountMail.jar
        env:
          EMAIL: myemail_2@domain.com
          EMAIL_USERNAME: my_username_2
          EMAIL_PASSWORD: my_password_2
      - name: myemail_3
        command: java
        args:
          - -jar
          - /usr/src/CountMail.jar
        env:
          EMAIL: myemail_3@domain.com
          EMAIL_USERNAME: my_username_3
          EMAIL_PASSWORD: my_password_3

Solution

The script exporter should be able to parse environment variables written in the "args" and "env" section of a script inside the config.yaml file. This way, no sensitive information would be written onto the config file (and it will also improve readability IMO)

Example:

Prerequisites existing environment variables:

MYEMAIL_1 MYEMAIL_USERNAME_1 MYEMAIL_PASSWORD_1 MYEMAIL_2 MYEMAIL_USERNAME_2 MYEMAIL_PASSWORD_2 MYEMAIL_3 MYEMAIL_USERNAME_3 MYEMAIL_PASSWORD_3

    scripts:
      - name: myemail_1
        command: java
        args:
          - -jar
          - CountMail.jar
        env:
          EMAIL: ${MYEMAIL_1} 
          EMAIL_USERNAME: ${MYEMAIL_USERNAME_1}
          EMAIL_PASSWORD: ${MYEMAIL_PASSWORD_1}
      - name: myemail_2
        command: java
        args:
          - -jar
          - /usr/src/CountMail.jar
        env:
          EMAIL: ${MYEMAIL_2}
          EMAIL_USERNAME: ${MYEMAIL_USERNAME_2}
          EMAIL_PASSWORD: ${MYEMAIL_PASSWORD_2}
      - name: myemail_3
        command: java
        args:
          - -jar
          - /usr/src/CountMail.jar
        env:
          EMAIL: ${MYEMAIL_3}
          EMAIL_USERNAME: ${MYEMAIL_USERNAME_3}
          EMAIL_PASSWORD: ${MYEMAIL_PASSWORD_3}
deviarchscs commented 10 months ago

@ricoberger any thoughts? Thank you!

ricoberger commented 10 months ago

Hi @deviarchscs, I like the idea and this should be supported once #110 is merged.

Once it is merged, I will publish a new version. Please let me know if sth. isn't working as expected.