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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

RomainMou
Copy link

Hi,

I've made a small script to retrieve some metrics from needrestart, and perhaps it could be useful for others?

The script runs needrestart -b (batch mode), parses the output, and displays some metrics. It looks like this:

# HELP needrestart_timestamp information about the version and when it was last run
# TYPE needrestart_timestamp gauge
needrestart_timestamp{version="3.5"} 1.702655157e+09
# HELP needrestart_kernel_status information about the kernel status
# TYPE needrestart_kernel_status gauge
needrestart_kernel_status{current="5.10.0-26-amd64",expected="5.10.0-26-amd64"} 1.0
# HELP needrestart_services_count number of services requiring a restart
# TYPE needrestart_services_count gauge
needrestart_services_count 0.0
# HELP needrestart_containers_count number of containers requiring a restart
# TYPE needrestart_containers_count gauge
needrestart_containers_count 0.0
# HELP needrestart_sessions_count number of sessions requiring a restart
# TYPE needrestart_sessions_count gauge
needrestart_sessions_count 0.0

Signed-off-by: RomainMou <58464216+RomainMou@users.noreply.github.com>
OBSELETE = 2


class NeedrestartParser:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems more consistent to name this NeedRestartData. it could also be a dataclass and the parsing would be implemented in a function.


# Parse the cmd output
for line in needrestart_output.stdout.decode().splitlines():
key, value = line.split(": ")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
key, value = line.split(": ")
key, value = line.split(": ", maxsplit=1)

g.set(needrestart_data.sessions_count)


def _main():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

imo, this doesn't need to be marked as private. per convention. this modules isn't intended to be used as library, so i'd question any marking of functions as private.

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
subprocess.run(["needrestart", "-b"], stdout=subprocess.PIPE)
needrestart_output = subprocess.run(["needrestart", "-b"], capture_output=True, text=True).stdout
needrestart_data = NeedrestartParser(needrestart_output)

i think that makes it clearer that the process' output is to be used, the text option makes is unnecessary to decode it in the parsing logic.

the extra statement for capturing the output makes it better comprehensible imo.

import subprocess
from collections import Counter
from enum import Enum
from prometheus_client import (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
from prometheus_client import (
from prometheus_client import (

per convention, 3rd party module imports are separated w/ an empty line from the ones from stdlib.

Comment on lines +2 to +11
#
#
# 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#
#
# 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
"""

This is considered as module docstring by Python interpreters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants