#!/usr/bin/env python3

# -*- coding: iso8859-1 -*-
#
# check_multipath.py
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License along
#   with this program; if not, write to the Free Software Foundation, Inc.,
#   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

"""Package versioning
"""

import os.path
import sys
import re
import subprocess

os.environ['LANG'] = "C"

__author__ = "Marc Schoechlin <ms@256bit.org>"
__version__ = "$Revision$"

def log(line):
    sys.stderr.write("%s: %s\n" % (sys.argv[0], line))

#########################################################################
# check if multipathing is enabled

cmd = "lsmod"
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
out, err = process.communicate()

multipath_system = False
for line in out.decode('utf8').splitlines():
    match = re.match(r"^dm_multipath", line)
    if match:
        multipath_system = 1
        break

if not multipath_system:
    log("STATUS OK : No multipathing here")
    print("OK: No multipathing here")
    sys.exit(0)

#########################################################################
# check multipath setup

paths = 0
broken_paths = 0
luns = 0
output = ""

cmd = "multipath -ll"
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
out, err = process.communicate()

multipath_system = False
for line in out.decode('utf8').splitlines():

    output += sys.argv[0] + " " + line
    match = re.match(r"^.+\s.*([a-f0-9]{33})", line)
    if match:
        ghosts = 0
        lun = match.group(1)
        luns += 1
        continue
    match = re.match(r"^.*(\d:\d:\d:\d).*$", line)
    # A path
    if not match:
        continue
    paths += 1

    match = re.match(r"^.*\d:\d:\d:\d.*(faulty|failed).*$", line)
    if match:
        broken_paths += 1

if (broken_paths > 0):
    print("ERROR: L:%i,P:%i,B%i" % (luns, paths, broken_paths))
    log(output)
else:
    print("OK: L:%i,P:%i,B%i" % (luns, paths, broken_paths))
    log("OK: LUNS:%i, PATHS:%i, BROKEN PATHS %i" % (luns, paths, broken_paths))

# vim: ai et ts=2 shiftwidth=2 expandtab tabstop=3 tw=120
