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
base: master
Are you sure you want to change the base?
Add needrestart_info.py to monitore needrestart. #195
Conversation
Signed-off-by: RomainMou <58464216+RomainMou@users.noreply.github.com>
5b4eb3b
to
eb588ef
Compare
OBSELETE = 2 | ||
|
||
|
||
class NeedrestartParser: |
There was a problem hiding this comment.
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(": ") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
key, value = line.split(": ") | |
key, value = line.split(": ", maxsplit=1) |
g.set(needrestart_data.sessions_count) | ||
|
||
|
||
def _main(): |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
# | ||
# | ||
# 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# | |
# | |
# 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.
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: