prometheus_keepalived: add role
This commit is contained in:
parent
8b5d587f26
commit
833d25078d
5 changed files with 125 additions and 0 deletions
66
roles/prometheus_keepalived/files/prometheus-keepalived
Normal file
66
roles/prometheus_keepalived/files/prometheus-keepalived
Normal file
|
@ -0,0 +1,66 @@
|
|||
#!/usr/bin/env python3
|
||||
import argparse
|
||||
import asyncio
|
||||
|
||||
from dbus_next import InterfaceNotFoundError
|
||||
from dbus_next.aio import MessageBus
|
||||
from dbus_next.constants import BusType
|
||||
from dbus_next.introspection import Node
|
||||
from prometheus_client import CollectorRegistry, Enum, write_to_textfile
|
||||
|
||||
|
||||
def patch(introspect):
|
||||
xml = introspect.to_xml()
|
||||
name = xml.find(
|
||||
'interface[@name="org.keepalived.Vrrp1.Instance"]'
|
||||
'/property[@name="Name"]'
|
||||
)
|
||||
if name is not None:
|
||||
name.attrib["type"] = "(s)"
|
||||
return Node.from_xml(xml, is_root=True)
|
||||
|
||||
|
||||
async def traverse(bus, path="/org/keepalived/Vrrp1/Instance"):
|
||||
|
||||
introspect = patch(await bus.introspect("org.keepalived.Vrrp1", path))
|
||||
proxy = bus.get_proxy_object("org.keepalived.Vrrp1", path, introspect)
|
||||
|
||||
try:
|
||||
interface = proxy.get_interface("org.keepalived.Vrrp1.Instance")
|
||||
_, state = await interface.get_state()
|
||||
(name,) = await interface.get_name()
|
||||
yield name, state
|
||||
except InterfaceNotFoundError:
|
||||
pass
|
||||
|
||||
for path in proxy.child_paths:
|
||||
async for state in traverse(bus, path):
|
||||
yield state
|
||||
|
||||
|
||||
async def main():
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("filename")
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
bus = await MessageBus(bus_type=BusType.SYSTEM).connect()
|
||||
|
||||
registry = CollectorRegistry()
|
||||
enum = Enum(
|
||||
"keepalived_vrrp_state",
|
||||
"Keepalived VRRP state",
|
||||
["name"],
|
||||
registry=registry,
|
||||
states=("stop", "init", "backup", "master", "fault", "deleted"),
|
||||
)
|
||||
|
||||
async for name, state in traverse(bus):
|
||||
enum.labels(name=name).state(state.lower())
|
||||
|
||||
write_to_textfile(args.filename, registry)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
5
roles/prometheus_keepalived/handlers/main.yml
Normal file
5
roles/prometheus_keepalived/handlers/main.yml
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
- name: Run daemon-reload
|
||||
systemd:
|
||||
daemon_reload: true
|
||||
...
|
34
roles/prometheus_keepalived/tasks/main.yml
Normal file
34
roles/prometheus_keepalived/tasks/main.yml
Normal file
|
@ -0,0 +1,34 @@
|
|||
---
|
||||
- name: Install dependencies
|
||||
apt:
|
||||
name:
|
||||
- python3-prometheus-client
|
||||
- python3-dbus-next
|
||||
|
||||
- name: Install prometheus-keepalived
|
||||
copy:
|
||||
src: prometheus-keepalived
|
||||
dest: /usr/local/bin/prometheus-keepalived
|
||||
mode: u=rwx,g=rx,o=rx
|
||||
owner: root
|
||||
group: root
|
||||
|
||||
- name: Configure prometheus-keepalived timer
|
||||
template:
|
||||
src: "{{ item }}.j2"
|
||||
dest: "/etc/systemd/system/{{ item }}"
|
||||
mode: u=rw,g=r,o=r
|
||||
owner: root
|
||||
group: root
|
||||
loop:
|
||||
- prometheus-keepalived.service
|
||||
- prometheus-keepalived.timer
|
||||
notify:
|
||||
- Run daemon-reload
|
||||
|
||||
- name: Enable prometheus-keepalived timer
|
||||
systemd:
|
||||
name: prometheus-keepalived.timer
|
||||
enabled: true
|
||||
state: started
|
||||
...
|
|
@ -0,0 +1,10 @@
|
|||
{{ ansible_managed | comment }}
|
||||
|
||||
[Unit]
|
||||
Description=Prometheus Keepalived
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
User=prometheus
|
||||
Group=prometheus
|
||||
ExecStart=/usr/local/bin/prometheus-keepalived {{ prometheus_keepalived__dest | enquote }}
|
|
@ -0,0 +1,10 @@
|
|||
{{ ansible_managed | comment }}
|
||||
|
||||
[Unit]
|
||||
Description=Timer for Prometheus Keepalived
|
||||
|
||||
[Timer]
|
||||
OnUnitActiveSec=15s
|
||||
|
||||
[Install]
|
||||
WantedBy=timers.target
|
Loading…
Reference in a new issue