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

Using SMART tool to get the devices instead of glob #14

Merged
Merged
Changes from all 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
45 changes: 14 additions & 31 deletions smartprom.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
#!/usr/bin/env python3

import glob
import re
import subprocess
import time
import json
from typing import List
from prometheus_client import start_http_server, Gauge


def isDrive(s: str) -> bool:
"""
checks if the device string matches an expected disk device name
"""
return re.match('^/dev/(sd[a-z]+|nvme[0-9]+)$', s)


def run(args: [str]):
def run(args: List[str]):
"""
runs the smartctl command on the system
"""
Expand All @@ -32,22 +22,20 @@ def run(args: [str]):
return stdout.decode("utf-8")


def get_types():
types = {}
results = run(['smartctl', '--scan-open'])
for result in results.split('\n'):
if not result:
continue

tokens = result.split()
if len(tokens) > 3:
types[tokens[0]] = tokens[2]

return types
def get_drives():
"""
returns a dictionary of devices and its types
"""
disks = {}
results = run(['smartctl', '--scan-open', '--json=c'])
devices = json.loads(results)['devices']
for device in devices:
disks[device["name"]] = device["type"]
print("Devices and its types", disks)
return disks


DRIVES = list(filter(lambda d: isDrive(d), glob.glob("/dev/*")))
TYPES = get_types()
DRIVES = get_drives()
HEADER = 'ID# ATTRIBUTE_NAME FLAG VALUE WORST THRESH TYPE UPDATED WHEN_FAILED RAW_VALUE'
METRICS = {}
LABELS = ['drive']
Expand Down Expand Up @@ -126,14 +114,9 @@ def collect():
Collect all drive metrics and save them as Gauge type
"""
global METRICS
global TYPES

for drive in DRIVES:
for drive, typ in DRIVES.items():
try:
# Grab all of the attributes that SMART gave us
if drive in TYPES:
typ = TYPES[drive]

if typ == 'sat':
attrs = smart_sat(drive)
elif typ == 'nvme':
Expand Down