Skip to content

Instantly share code, notes, and snippets.

@smopucilowski
Created August 1, 2016 23:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smopucilowski/11e84c14252d256f139455b27e3dcc04 to your computer and use it in GitHub Desktop.
Save smopucilowski/11e84c14252d256f139455b27e3dcc04 to your computer and use it in GitHub Desktop.
Simple temperature dump of the drives in my Norco RPC-4224
#!/bin/env python3
#______________________________________________________________________________
from multiprocessing import Pool
import subprocess
from collections import defaultdict
from os import path
#______________________________________________________________________________
# (device_exists, smart_status, smart_output)
def get_smart(device):
if not path.exists(device):
return (False, None, None)
try:
output = subprocess.check_output([
'/usr/sbin/smartctl',
'-a',
device,
], universal_newlines=True)
return (True, 0, output)
# Thrown if we have a bad smart log
except subprocess.CalledProcessError as e:
return (True, e.returncode, e.output)
def get_temperature(device):
smart = get_smart(device)
if not smart[0]:
return (False, None, None)
smart_warning = smart[1] != 0
lines = smart[2].split('\n')
for line in lines:
# For normal SATA drives
if 'Temperature_Celsius' in line:
tokens = line.split()
temperature = int(tokens[9])
return (True, smart_warning, temperature)
# For SAS drives
if 'Current Drive Temperature:' in line:
tokens = line.split()
temperature = int(tokens[3])
return (True, smart_warning, temperature)
return (True, smart_warning, None)
def format_temperature(status):
if not status[0]:
return '-'
if status[1]:
return 'warn %dC' % status[2]
else:
return '%dC' % status[2]
#______________________________________________________________________________
# Prepare output table
ROWS = 7
COLUMNS = 5
cell = defaultdict(str)
# Table headers
for column in range(1, COLUMNS):
cell[0, column] = 'across-%d' % column
for row in range(1, ROWS):
cell[row, 0] = 'down-%d' % row
# Populate table
temperatures = dict()
with Pool() as pool:
for down in range(1,ROWS):
for across in range(1,COLUMNS):
temperatures[down, across] = pool.apply_async(
get_temperature, ('/dev/disk/by-vdev/down-%d-across-%d' % (down, across),)
)
for key, value in temperatures.items():
temperatures[key] = value.get()
cell[key] = format_temperature(temperatures[key])
# Display table
for row in range(ROWS):
for column in range(COLUMNS):
print('{:>8} '.format(cell[row, column]), end='')
print('')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment