#!/usr/local/bin/python3.4 # Copyright (c) 2015, # # This module is free software; you can redistribute it and/or modify it # under the terms of GNU General Public License (GPL) version 2. import argparse import paramiko import sshUtils import socket import logging import device_mgr def percentage(part, whole): return 100 * float(part) / float(whole) def check_pool_capacity(warning, critical): _METHOD_ = "check_pool_capacity" rc = 0 msg = "" results = sshUtils.invokeSshCommand( host, userid, password, "lsmdiskgrp -bytes -nohdr").splitlines() if len(results) < 1: logging.warn("WARNING %s::no pools found on svc [%s]", _METHOD_, host) rc = 1 msg += "No pools found" return (rc, msg) for line in results: totalCap = line.split()[5] usedCap = line.split()[9] poolName = line.split()[1] usage = percentage(usedCap, totalCap) if usage >= critical: rc = 2 msg += "Pool Critical: %s usage: %i%%\n" % (poolName, usage) elif usage >= warning: if (rc != 2): rc = 1 msg += "Pool Warning: %s usage: %i%%\n" % (poolName, usage) if rc == 0: msg = "All Pools are ok on capacity usage" return (rc, msg) def check_fcport(): _METHOD_ = "check_fcport" rc = 0 msg = "" results = sshUtils.invokeSshCommand( host, userid, password, "lsportfc -nohdr").splitlines() PORT_STATUS = ('active', 'inactive_unconfigured', 'inactive_configured') # the fcport info should not be null so won't check the output # inactive_unconfigured considered as OK as customer may not want to use it for line in results: portID = line.split()[0] nodeName = line.split()[6] portStatus = line.split()[9] if portStatus == PORT_STATUS[2]: rc = 2 msg += "FC Port Critical: Port %s on Node %s is configured but inactive\n" % ( portID, nodeName) # V7000 didn't list out all the value of status , so treat not listed # status as warning elif portStatus not in portStatus: if(rc != 2): rc = 1 msg += "FC Port Critical: Port %s on Node %s is in %s status\n" % ( portID, nodeName, portStatus) if rc == 0: msg = "All FC Ports are ok" return (rc, msg) def check_pool_status(warning=1, critical=1): # the pool status is important so default set the threshhold to be 1 _METHOD_ = "check_pool_status" rc = 0 msg = "" results = sshUtils.invokeSshCommand( host, userid, password, "lsmdiskgrp -bytes -nohdr").splitlines() if len(results) < 1: logging.warn("WARNING %s::no pools found on svc [%s]", _METHOD_, host) rc = 1 msg += "No pools found" return (rc, msg) warnPools = 0 critPools = 0 for line in results: poolName = line.split()[1] status = line.split()[2] if status == "offline": critPools += 1 if critPools >= critical: rc = 2 msg += "Pool Critical: %s is offline.\n" % (poolName) elif status != "online": warnPools += 1 if rc != 2 and warnPools >= warning: rc = 1 msg += "Pool Warning: %s is %s.\n" % (poolName, status) if rc == 0: msg = "All Pools are ok" return (rc, msg) def check_node_status(): _METHOD_ = "check_node_status" rc = 0 msg = "" results = sshUtils.invokeSshCommand( host, userid, password, "ldnodecanister -nohdr").splitlines() for line in results: nodeName = line.split()[1] nodeStatus = line.split()[3] if nodeStatus == "offline": rc = 2 msg += "Node Critical: Node %s is offline\n" % (nodeName) elif nodeStatus != "online": if rc != 2: rc = 1 msg += "Node Warning: Node %s is %s\n" % (nodeName, nodeStatus) if rc == 0: msg = "All Nodes are ok" return (rc, msg) if __name__ == '__main__': #logging.basicConfig(format="%(asctime)s %(levelname)s %(message)s",filename="/tmp/puremgr.log", level=logging.INFO) CMD = "check_svc" parser = argparse.ArgumentParser() parser.add_argument("operation") parser.add_argument('-H', '--Host', required=True) parser.add_argument('-w', '--warning', required=False, type=int) parser.add_argument('-c', '--critical', required=False, type=int) args = parser.parse_args() operation = args.operation host = args.Host warning = args.warning critical = args.critical performanceData = None try: (userid, password) = device_mgr.retrieveAccessInfoForDevice(host) if operation == "CAPACITY": (rc, msg) = check_pool_capacity(warning, critical) elif operation == "FCPORT": (rc, msg) = check_fcport() elif operation == "POOLSTATE": #set a default thresh hold if warning == None: warning = 1 if critical == None: critical = 1 (rc, msg) = check_pool_status(warning, critical) elif operation == "NODESTATE": (rc, msg) = check_node_status() else: msg = "Unknown operation: " + operation rc = 3 except paramiko.AuthenticationException: msg = "userid/password combination not valid" rc = 3 except socket.timeout: msg = "Connection timed out." rc = 3 except OSError: msg = "No route to host." rc = 3 except Exception as e: logging.error( "%s::Exception in function. Address=%s, Exception=%s", CMD, host, e) msg = "Unexpected error" rc = 3 if (performanceData is None): print(msg) else: print(msg + "|" + performanceData) exit(rc)