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

Add code to check zpool status #1

Merged
merged 2 commits into from Oct 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Expand Up @@ -13,6 +13,11 @@ Conduct a quick lookup of the kernel version and type of a given Linux host.
Prints the files and their sizes, in the /var/log/ directory of a given
Linux host.

## check-zpool.py

Prints the health status of the zpool on a given host, as well as the ZFS
volume information.

## TODO

* consider separating out parts into separate functions
Expand Down
98 changes: 98 additions & 0 deletions check-zpool.py
@@ -0,0 +1,98 @@
#!/usr/bin/python3

import sys, getopt, getpass
from fabric import Connection

def main(argv):

usageInfo = ("check-zpool-status.py "
"-d <destination hostname or IP> "
"-p <port number> "
"-u <user> "
"-i </path/to/identity_file>")

destinationHost = ''
destinationPort = 22
user = ''
identityFile = ''

#
# input validation
#

try:
opts, args = getopt.getopt(argv,"hd:p:u:i:",["host=","port=","user=","id="])
except getopt.GetoptError:
print(usageInfo)
exit(1)

for opt, arg in opts:
if opt == "-h":
print(usageInfo)
exit()
elif opt in ("-d","--host"):
destinationHost = arg
elif opt in ("-p","--port"):
destinationPort = arg
elif opt in ("-u","--user"):
user = arg
elif opt in ("-i","--id"):
identityFile = arg

if destinationHost == '':
print("Error: No host specified.")
exit(1)

if destinationPort == 0 or not destinationPort.isdigit():
print("Error: Invalid port specified.")
exit(1)

if user == '':
print("Error: No user specified.")
exit(1)

#
# if an identity is supplied, attempt to obtain the passphrase
#
identityPassphrase = getpass.getpass()

#
# attempt to connect using the given parameters
#

if identityFile == '' or identityPassphrase == '':
conn = Connection(host=destinationHost, port=destinationPort, user=user)
else:
conn = Connection(host=destinationHost, port=destinationPort, user=user,
connect_kwargs={'key_filename':identityFile,'passphrase':identityPassphrase})

if conn is None:
print("Error: Unable to initialize Connection object.")
exit(1)

print("The following connection was established: ", conn)

#
# obtain the status of the zpool and zfs volume information
#

result = conn.run("zpool status")
if result.exited != 0 or result.ok != True:
print("Warning: The connection was improper or terminated prematurely.")
print("Unable to obtain zpool status.")

result = conn.run("zfs list")
if result.exited != 0 or result.ok != True:
print("Warning: The connection was improper or terminated prematurely.")
print("Unable to obtain ZFS volume information.")

#
# close the connection since everything is done
#

conn.close()

#
# execute the main function declared above
#
main(sys.argv[1:])