#! /usr/local/bin/python
#
# Copyright C 2002-2003 by Isilon Systems, Inc. All rights not expressly
# granted herein are reserved.
# Created in the United States of America.
# 
# This digital media is protected by U.S. and international copyright and
# intellectual property laws. Unless otherwise specified, all information
# and screens appearing as part of this digital medium, including
# software, services, documents, text, images, icons, and logos design;
# the selection, assembly, arrangement, and design thereof; and the code
# that enables its presentation, are the sole property of Isilon Systems,
# Inc.
# 
# Except as otherwise required by applicable law, no part of any
# copyrighted material may be reproduced, modified, translated,
# distributed, published, or transmitted by any means, electronic or
# mechanical, for any purpose, without the prior written consent of Isilon
# Systems. 
#  
# Isilon and Isilon Systems are registered trademarks of Isilon Systems,
# Inc.  Legion is a trademark of Isilon Systems, Inc.  All other
# trademarks and registered trademarks are the property of their
# respective holders.

#
# This program runs a topology check of the SAS PHY subsystem by invoking
# the isi_sasphymon program in "immediate" mode. Any problems that are
# found by the program are echoed to stdout. For this reason, we redirect
# the output (stdout) to a temporary file so that the system console will 
# contain a simple "Checking Isilon SAS PHY Hardware: good/BAD" status 
# message for the user. 
#
# If problems are found, the contents of the temporary file are displayed
# on the system console so the user has some idea of what might be wrong.
#
# NOTE: Refer to the "Special Case" comments below for information regarding
#       exceptions to this rule.
#

import os
import os.path
import sys

sys.stdout.flush()

#
# Exit status from isi_sasphymon
#
# NOTE: Keep these, especially the "special" ones, in sync with what's
#       in "isi_sasphymon.c". If any more "specials" are added, make
#       sure the test below is also modified.
#
STATUS_GOOD = 0
STATUS_BAD = 1
STATUS_FW_NEEDS_UPGRADE = 2
STATUS_HWGEN_NOT_SUPPORTED = 3

#
# os.system's exit status multiplier
#
STATUS_MULTIPLIER = 256

#
# Special Cases -- 1) Vitesse firmware is too old
#                  2) Monitor was told to start on a DNA node
#
# If any of the special cases above occur, the monitor exits with a
# special status code which allows us to distinguish the difference
# between these benign failures and true, hardware problems. Further,
# we can avoid alarming the user by indicating the hardware is "BAD"
# in the banner message, but instead, we display the warning messages
# from the monitor itself.
#
# NOTE: The os.system() function multiplies the program's exit status by
#       256 in order to be able to return statuses indicating the program
#       was not able to be run.
#
val = os.system("/usr/bin/isi_hwtools/isi_sasphymon 1>/tmp/isi_sasphymon.tmp")
val = val / STATUS_MULTIPLIER
#
# Test for any special cases... (see "Exit status" comments above)
#
if val == STATUS_FW_NEEDS_UPGRADE or val == STATUS_HWGEN_NOT_SUPPORTED:
    os.system("cat /tmp/isi_sasphymon.tmp")
else:
    print "Checking Isilon SAS PHY Hardware:",
    if val != STATUS_GOOD:
        print "BAD"
        print "================================"
        print "SAS PHY Hardware problems found:"
        os.system("cat /tmp/isi_sasphymon.tmp")
        print "================================"
    else:
        print "good"

os.system("/bin/rm -f /tmp/isi_sasphymon.tmp")

sys.exit(0)
