Skip to content

Commit

Permalink
Some fixes and improvments
Browse files Browse the repository at this point in the history
Signed-off-by: RomainMou <[email protected]>
  • Loading branch information
RomainMou committed Jun 3, 2024
1 parent bf06317 commit d805647
Showing 1 changed file with 34 additions and 30 deletions.
64 changes: 34 additions & 30 deletions needrestart_info.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
#!/usr/bin/env python3
#
#
# Description: Expose metrics from needrestart.
#
# This script runs needrestart in batch mode. It will never ask for input
# and will never restart or upgrade anything.
#
# Dependencies: python >= 3.5, python3-prometheus-client, needrestart
#
# Authors: RomainMou

"""
Description: Expose metrics from needrestart.
This script runs needrestart in batch mode. It will never ask for input
and will never restart or upgrade anything.
Dependencies: python >= 3.5, python3-prometheus-client, needrestart
Authors: RomainMou
"""

import time
import subprocess
from collections import Counter
from enum import Enum

from prometheus_client import (
CollectorRegistry,
Gauge,
Expand All @@ -34,7 +36,7 @@ class MicroCodeStatus(Enum):
OBSELETE = 2


class NeedrestartParser:
class NeedRestartData:
def __init__(self, needrestart_output):
# Some default value
self.timestamp = int(time.time())
Expand All @@ -48,8 +50,8 @@ def __init__(self, needrestart_output):
needrestart_counter = Counter()

# Parse the cmd output
for line in needrestart_output.stdout.decode().splitlines():
key, value = line.split(": ")
for line in needrestart_output.splitlines():
key, value = line.split(": ", maxsplit=1)
if key == "NEEDRESTART-VER":
self.version = value
# Kernel informations
Expand All @@ -75,7 +77,7 @@ def __init__(self, needrestart_output):
self.sessions_count = needrestart_counter["NEEDRESTART-SESS"]


def _write_timestamp(registry, needrestart_data):
def write_timestamp(registry, needrestart_data):
g = Gauge(
"needrestart_timestamp",
"information about the version and when it was last run",
Expand All @@ -85,7 +87,7 @@ def _write_timestamp(registry, needrestart_data):
g.labels(needrestart_data.version).set(needrestart_data.timestamp)


def _write_kernel(registry, needrestart_data):
def write_kernel(registry, needrestart_data):
if needrestart_data.kernel_status:
e = Gauge(
"needrestart_kernel_status",
Expand All @@ -99,7 +101,7 @@ def _write_kernel(registry, needrestart_data):
).set(needrestart_data.kernel_status.value)


def _write_microcode(registry, needrestart_data):
def write_microcode(registry, needrestart_data):
if needrestart_data.microcode_status:
e = Gauge(
"needrestart_microcode_status",
Expand All @@ -113,7 +115,7 @@ def _write_microcode(registry, needrestart_data):
).set(needrestart_data.microcode_status.value)


def _write_services(registry, needrestart_data):
def write_services(registry, needrestart_data):
g = Gauge(
"needrestart_services_count",
"number of services requiring a restart",
Expand All @@ -122,7 +124,7 @@ def _write_services(registry, needrestart_data):
g.set(needrestart_data.services_count)


def _write_containers(registry, needrestart_data):
def write_containers(registry, needrestart_data):
g = Gauge(
"needrestart_containers_count",
"number of containers requiring a restart",
Expand All @@ -131,7 +133,7 @@ def _write_containers(registry, needrestart_data):
g.set(needrestart_data.containers_count)


def _write_sessions(registry, needrestart_data):
def write_sessions(registry, needrestart_data):
g = Gauge(
"needrestart_sessions_count",
"number of sessions requiring a restart",
Expand All @@ -140,21 +142,23 @@ def _write_sessions(registry, needrestart_data):
g.set(needrestart_data.sessions_count)


def _main():
def main():
registry = CollectorRegistry()
needrestart_data = NeedrestartParser(
subprocess.run(["needrestart", "-b"], stdout=subprocess.PIPE)
)

_write_timestamp(registry, needrestart_data)
_write_kernel(registry, needrestart_data)
_write_microcode(registry, needrestart_data)
_write_services(registry, needrestart_data)
_write_containers(registry, needrestart_data)
_write_sessions(registry, needrestart_data)
needrestart_output = subprocess.run(
["needrestart", "-b"], capture_output=True, text=True
).stdout
needrestart_data = NeedRestartData(needrestart_output)

write_timestamp(registry, needrestart_data)
write_kernel(registry, needrestart_data)
write_microcode(registry, needrestart_data)
write_services(registry, needrestart_data)
write_containers(registry, needrestart_data)
write_sessions(registry, needrestart_data)

print(generate_latest(registry).decode(), end="")


if __name__ == "__main__":
_main()
main()

0 comments on commit d805647

Please sign in to comment.