Conversation
Updated subprocess to use popen included Hard drive functions, as well as status scoring for sata and nvme
Added new exception for paths not used.
Removed enum harddrive status Fixed attributes issue if 0
Moved scoring function to two parts to add score as an attribute.
miaucl
left a comment
There was a problem hiding this comment.
For a clean PR, please add instructions to the Readme.md.
- What is the frequency you are planning to run this?
- Fix the "conflicts"
- Cleanup unused code as good as possibe (comments are not a problem, but unused code lines are confusing)
Otherwise, I really like this extension and will test it as soon as it is in a clean state and I can run the workflows.
Thanks already @bimal12
linux2mqtt/linux2mqtt.py
Outdated
| main_logger = logging.getLogger("main") | ||
| mqtt_logger = logging.getLogger("mqtt") | ||
|
|
||
| logging.basicConfig(format="%(asctime)s - %(name)s - %(levelname)s - %(message)s") |
There was a problem hiding this comment.
please use the same logging system as already present or create a new logger if you need one
There was a problem hiding this comment.
No need for this line, I use specific purpose loggers and do not want to pollute the default logging config as library.
See following code:
def configure_logger(
logger: logging.Logger, verbosity: int, logdir: str | None
) -> None:
"""Configure main logger.
Parameters
----------
logger
The logger to configure
verbosity
The verbosity level
logdir
The log directory
"""
if verbosity >= 5:
logger.setLevel(logging.DEBUG)
elif verbosity == 4:
logger.setLevel(logging.INFO)
elif verbosity == 3:
logger.setLevel(logging.WARNING)
elif verbosity == 2:
logger.setLevel(logging.ERROR)
elif verbosity == 1:
logger.setLevel(logging.CRITICAL)
# Configure logger
logger.propagate = False
log_format = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
formatter = logging.Formatter(log_format)
console_handler = logging.StreamHandler()
console_handler.setFormatter(formatter)
logger.addHandler(console_handler)
if logdir:
try:
logdirpath = Path(logdir)
absolute_logdir = (
logdirpath.resolve() if not logdirpath.is_absolute() else logdirpath
)
absolute_logdir.mkdir(parents=True, exist_ok=True)
log_file = path.join(absolute_logdir, f"linux2mqtt-{logger.name}.log")
file_handler = RotatingFileHandler(
log_file, maxBytes=1_000_000, backupCount=5
)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
except Exception as ex:
logger.warning(
"Failed to initialize logging to directory %s : %s",
logdir,
str(ex),
)
There was a problem hiding this comment.
I think I removed the line, and pulled in the updated loggers.
linux2mqtt/metrics.py
Outdated
| self.metric.polled_result = { | ||
| **self.harddrive.attributes, # type: ignore[unused-ignore] | ||
| } | ||
| # self.metric._name = self.harddrive.attributes['model_name'] |
| def get_score(self): | ||
| score = 0 | ||
|
|
||
| # Critical warnings (bitmask) |
There was a problem hiding this comment.
What is your scoring based on? Is it an official baseline?
There was a problem hiding this comment.
Nope, just an arbitrary scoring metric that I put together to assist with monitoring drive health.
There was a problem hiding this comment.
Ok, in that case it would be helpful to document it somewhere for users to understand :)
There was a problem hiding this comment.
I have added this into the readme now.
linux2mqtt/harddrive.py
Outdated
|
|
||
| ata_regex = "^ata.*(?<!part\d)$" | ||
| nvme_regex = "^nvme-eui.*(?<!part\d)$" | ||
| # potential_disks = os.listdir("/dev/disk/by-id/") |
miaucl
left a comment
There was a problem hiding this comment.
looks better already, still some points open but workflows are already happy :)
|
|
||
| self.attributes["Model Name"] = self._attributes["model_name"] # type: ignore | ||
| self.attributes["Device"] = self._attributes["device"]["name"] # type: ignore | ||
| self.attributes["Model Name"] = self._attributes["model_name"] # type: ignore[index] |
There was a problem hiding this comment.
It was what mypy was saying was needed.
error: "type: ignore" comment without error code (consider "type: ignore[index]" instead) [ignore-without-code]
linux2mqtt/linux2mqtt.py
Outdated
| main_logger = logging.getLogger("main") | ||
| mqtt_logger = logging.getLogger("mqtt") | ||
|
|
||
| logging.basicConfig(format="%(asctime)s - %(name)s - %(levelname)s - %(message)s") |
There was a problem hiding this comment.
No need for this line, I use specific purpose loggers and do not want to pollute the default logging config as library.
See following code:
def configure_logger(
logger: logging.Logger, verbosity: int, logdir: str | None
) -> None:
"""Configure main logger.
Parameters
----------
logger
The logger to configure
verbosity
The verbosity level
logdir
The log directory
"""
if verbosity >= 5:
logger.setLevel(logging.DEBUG)
elif verbosity == 4:
logger.setLevel(logging.INFO)
elif verbosity == 3:
logger.setLevel(logging.WARNING)
elif verbosity == 2:
logger.setLevel(logging.ERROR)
elif verbosity == 1:
logger.setLevel(logging.CRITICAL)
# Configure logger
logger.propagate = False
log_format = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
formatter = logging.Formatter(log_format)
console_handler = logging.StreamHandler()
console_handler.setFormatter(formatter)
logger.addHandler(console_handler)
if logdir:
try:
logdirpath = Path(logdir)
absolute_logdir = (
logdirpath.resolve() if not logdirpath.is_absolute() else logdirpath
)
absolute_logdir.mkdir(parents=True, exist_ok=True)
log_file = path.join(absolute_logdir, f"linux2mqtt-{logger.name}.log")
file_handler = RotatingFileHandler(
log_file, maxBytes=1_000_000, backupCount=5
)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
except Exception as ex:
logger.warning(
"Failed to initialize logging to directory %s : %s",
logdir,
str(ex),
)
Pull harddrive stats from smartctl for HDDs and NVME drives