-
Notifications
You must be signed in to change notification settings - Fork 12
check_nvme_health: Add NVMe disk remaining life check #368
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
Open
lamaral
wants to merge
1
commit into
master
Choose a base branch
from
l_nvme
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| #!/usr/bin/env python3 | ||
| """InnoGames Monitoring Plugins - NVMe Health Check | ||
|
|
||
| This script checks the health of NVMe disks by discovering devices via sysfs | ||
| and querying SMART data with nvme-cli. It raises a warning or critical state | ||
| when the remaining life falls below the configured thresholds. | ||
|
|
||
| Copyright (c) 2026 InnoGames GmbH | ||
| """ | ||
| # Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| # of this software and associated documentation files (the "Software"), to deal | ||
| # in the Software without restriction, including without limitation the rights | ||
| # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| # copies of the Software, and to permit persons to whom the Software is | ||
| # furnished to do so, subject to the following conditions: | ||
| # | ||
| # The above copyright notice and this permission notice shall be included in | ||
| # all copies or substantial portions of the Software. | ||
| # | ||
| # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
| # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| # THE SOFTWARE. | ||
|
|
||
| import json | ||
| import os | ||
| import sys | ||
| from argparse import ArgumentParser | ||
| from subprocess import CalledProcessError, STDOUT, check_output | ||
|
|
||
|
|
||
| class ExitCodes(): | ||
| OK = 0 | ||
| WARNING = 1 | ||
| CRITICAL = 2 | ||
| UNKNOWN = 3 | ||
|
|
||
| @classmethod | ||
| def label(cls, code): | ||
| return {cls.OK: 'OK', cls.WARNING: 'WARNING', cls.CRITICAL: 'CRITICAL', cls.UNKNOWN: 'UNKNOWN'}[code] | ||
|
|
||
|
|
||
| def parse_args(): | ||
| parser = ArgumentParser( | ||
| description='Check NVMe disk remaining life via nvme-cli smart-log', | ||
| ) | ||
| parser.add_argument( | ||
| '--warning', '-w', | ||
| help='warning threshold for remaining life percentage (default: 20)', | ||
| default=20, | ||
| type=int, | ||
| ) | ||
| parser.add_argument( | ||
| '--critical', '-c', | ||
| help='critical threshold for remaining life percentage (default: 10)', | ||
| default=10, | ||
| type=int, | ||
| ) | ||
| return parser.parse_args() | ||
|
|
||
|
|
||
| def main(): | ||
| args = parse_args() | ||
|
|
||
| devices = get_nvme_devices() | ||
| if not devices: | ||
| print('UNKNOWN - No NVMe devices found in /sys/class/nvme') | ||
| sys.exit(ExitCodes.UNKNOWN) | ||
|
|
||
| code = ExitCodes.OK | ||
| summaries = [] | ||
| perfdata = [] | ||
|
|
||
| for device in devices: | ||
| name = os.path.basename(device) | ||
|
|
||
| try: | ||
| smart = get_smart_log(device) | ||
| except CalledProcessError as e: | ||
| print(f'UNKNOWN - Failed to query {device}: {e.output.decode().strip()}') | ||
| sys.exit(ExitCodes.UNKNOWN) | ||
| except (OSError, ValueError) as e: | ||
| print(f'UNKNOWN - Error reading {device}: {e}') | ||
| sys.exit(ExitCodes.UNKNOWN) | ||
|
|
||
| percent_used = smart.get('percent_used') | ||
| if percent_used is None: | ||
| print(f'UNKNOWN - percent_used missing in smart-log output for {device}') | ||
| sys.exit(ExitCodes.UNKNOWN) | ||
|
|
||
| remaining = 100 - percent_used | ||
|
|
||
| if remaining <= args.critical: | ||
| code = max(code, ExitCodes.CRITICAL) | ||
| elif remaining <= args.warning: | ||
| code = max(code, ExitCodes.WARNING) | ||
|
|
||
| summaries.append(f'{name} life={remaining}%') | ||
| perfdata.append(f'{name}_life={remaining}%;{args.warning};{args.critical};0;100') | ||
|
|
||
| status = ExitCodes.label(code) | ||
| if len(summaries) == 1: | ||
| lines = [f'{status} - {summaries[0]} | {perfdata[0]}'] | ||
| else: | ||
| lines = [f'{status} | {" ".join(perfdata)}'] | ||
| lines.extend(summaries) | ||
| print('\n'.join(lines)) | ||
| sys.exit(code) | ||
|
|
||
|
|
||
| def get_nvme_devices(): | ||
| """Return sorted list of /dev/nvmeN device paths discovered via sysfs""" | ||
| sysfs_path = '/sys/class/nvme' | ||
| if not os.path.exists(sysfs_path): | ||
| return [] | ||
| return sorted('/dev/' + entry for entry in os.listdir(sysfs_path)) | ||
|
|
||
|
|
||
| def get_smart_log(device): | ||
| """Run nvme smart-log on device and return parsed JSON dict""" | ||
| raw = check_output( | ||
| ['nvme', 'smart-log', '--output-format=json', device], | ||
| stderr=STDOUT, | ||
| ) | ||
| return json.loads(raw.decode()) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| main() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.