Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add needrestart_info.py to monitore needrestart. #195

Merged
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
164 changes: 164 additions & 0 deletions needrestart_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
#!/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
"""

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

from prometheus_client import (
RomainMou marked this conversation as resolved.
Show resolved Hide resolved
CollectorRegistry,
Gauge,
generate_latest,
)


class KernelStatus(Enum):
UNKNOWN = 0
CURRENT = 1
ABI_UPGRADE = 2
VERSION_UPGRADE = 3


class MicroCodeStatus(Enum):
UNKNOWN = 0
CURRENT = 1
OBSELETE = 2
RomainMou marked this conversation as resolved.
Show resolved Hide resolved


class NeedRestartData:
def __init__(self, needrestart_output):
# Some default value
self.timestamp = int(time.time())
self.version = None
self.kernel_status = None
self.microcode_status = None
self.kernel_current_version = ""
self.kernel_expected_version = ""
self.microcode_current_version = ""
self.microcode_expected_version = ""
needrestart_counter = Counter()

# Parse the cmd output
for line in needrestart_output.splitlines():
key, value = line.split(": ", maxsplit=1)
if key == "NEEDRESTART-VER":
self.version = value
# Kernel informations
elif key == "NEEDRESTART-KCUR":
self.kernel_current_version = value
elif key == "NEEDRESTART-KEXP":
self.kernel_expected_version = value
elif key == "NEEDRESTART-KSTA":
self.kernel_status = KernelStatus(int(value))
# Microcode informations
elif key == "NEEDRESTART-UCCUR":
self.microcode_current_version = value
elif key == "NEEDRESTART-UCEXP":
self.microcode_expected_version = value
elif key == "NEEDRESTART-UCSTA":
self.microcode_status = MicroCodeStatus(int(value))
# Count the others
else:
needrestart_counter.update({key})

self.services_count = needrestart_counter["NEEDRESTART-SVC"]
self.containers_count = needrestart_counter["NEEDRESTART-CONT"]
self.sessions_count = needrestart_counter["NEEDRESTART-SESS"]


def write_timestamp(registry, needrestart_data):
g = Gauge(
"needrestart_timestamp",
"information about the version and when it was last run",
labelnames=["version"],
registry=registry,
)
g.labels(needrestart_data.version).set(needrestart_data.timestamp)


def write_kernel(registry, needrestart_data):
if needrestart_data.kernel_status:
e = Gauge(
"needrestart_kernel_status",
"information about the kernel status",
labelnames=["current", "expected"],
registry=registry,
)
e.labels(
needrestart_data.kernel_current_version,
needrestart_data.kernel_expected_version,
).set(needrestart_data.kernel_status.value)


def write_microcode(registry, needrestart_data):
if needrestart_data.microcode_status:
e = Gauge(
"needrestart_microcode_status",
"information about the microcode status",
labelnames=["current", "expected"],
registry=registry,
)
e.labels(
needrestart_data.microcode_current_version,
needrestart_data.microcode_expected_version,
).set(needrestart_data.microcode_status.value)


def write_services(registry, needrestart_data):
g = Gauge(
"needrestart_services_count",
"number of services requiring a restart",
registry=registry,
)
g.set(needrestart_data.services_count)


def write_containers(registry, needrestart_data):
g = Gauge(
"needrestart_containers_count",
"number of containers requiring a restart",
registry=registry,
)
g.set(needrestart_data.containers_count)


def write_sessions(registry, needrestart_data):
g = Gauge(
"needrestart_sessions_count",
"number of sessions requiring a restart",
registry=registry,
)
g.set(needrestart_data.sessions_count)


def main():
registry = CollectorRegistry()

needrestart_output = subprocess.run(
["needrestart", "-b"], capture_output=True, text=True
).stdout
RomainMou marked this conversation as resolved.
Show resolved Hide resolved
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()
Loading