MindFlavor / prometheus_wireguard_exporter

A Prometheus exporter for WireGuard, written in Rust.
https://mindflavor.github.io/prometheus_wireguard_exporter
MIT License
494 stars 52 forks source link

Add friendly_name keyword in the comment #43

Closed psyhomb closed 4 years ago

psyhomb commented 4 years ago

It would be really nice to implement some kind of keyword that will be used to mark friendly name comment line, that way it would be possible to have multiple comments but only one with a specific keyword will be used to generate friendly_name label and all others will be silently ignored.

Example:

[Peer]
# friendly_name=frcognowin10
# Additional comment
PublicKey = lqYcojJMsIZXMUw1heAFbQHBoKjCEaeo7M1WXDh/KWc=
AllowedIPs = 10.70.0.40/32

# Additional comment
[Peer]
# friendly_name=OnePlus 5T
PublicKey = 928vO9Lf4+Mo84cWu4k1oRyzf0AR7FTGoPKHGoTMSHk=
AllowedIPs = 10.70.0.80/32
# Additional comment

I'm providing an example snippet code written in Python:

import re

peers = '''
[Peer]
# friendly_name=frcognowin10
# Additional comment
PublicKey = lqYcojJMsIZXMUw1heAFbQHBoKjCEaeo7M1WXDh/KWc=
AllowedIPs = 10.70.0.40/32

# Additional comment
[Peer]
# friendly_name=OnePlus 5T
PublicKey = 928vO9Lf4+Mo84cWu4k1oRyzf0AR7FTGoPKHGoTMSHk=
AllowedIPs = 10.70.0.80/32
# Additional comment
'''

l = []
for peer in peers.split('\n\n'):
  fn = re.search(r'friendly_name=(.*)', peer)
  pk = re.search(r'PublicKey = (.*)', peer)

  if fn:
    l.append({
      'friendly_name': fn.group(1),
      'public_key': pk.group(1)
    })

print(l)

Output:

[{'friendly_name': 'frcognowin10', 'public_key': 'lqYcojJMsIZXMUw1heAFbQHBoKjCEaeo7M1WXDh/KWc='}, {'friendly_name': 'OnePlus 5T', 'public_key': '928vO9Lf4+Mo84cWu4k1oRyzf0AR7FTGoPKHGoTMSHk='}]

Thanks 😊

MindFlavor commented 4 years ago

That is a great idea. I'll work on it ASAP.

psyhomb commented 4 years ago

FYI these are changes I've made (fork) and I'm currently testing it in my WireGuard environment. https://github.com/psyhomb/prometheus_wireguard_exporter/commit/6439f0ada8b524439ecfe81b2e808b99969caea6

MindFlavor commented 4 years ago

D'oh I missed your comment and replicated the same thing... :man_facepalming: Sorry!

psyhomb commented 4 years ago

Not a problem at all 😉 and thank you for making these changes.