Python Discussions

NFS status using python API

python_API_and_NFS_status
11,777 Views

Hello, 

 

just started with experimenting with python API for netapp, however, when i used the example nfs.py (from API documentation), the results are quite unexpected. Mainly i mean the example for getting the nfs status from the netapp. 

 

I just took the example script and run it, however i get "NFS status: disabled" instead of enabled. When i try the same powershell script (also provided from API docu), the result is expected "NFS: enabled"

 

Have anybody seen this behavior? Is it a bug in API? or i'm missing something else? 

 

I've tried it with python3 (3.4.3) and python2 (2.7.10). both is getting the same output "nfs disabled" even if the NFS is enabled.

(unfortunately it's not on linux machine, but run under cygwin)

 

#!/usr/bin/python3

#=======================================================================#
#                                                                       #
# $ID$                                                                  #
#                                                                       #
# nfs.py                                                                #
#                                                                       #
# Sample code for the following APIs:                                   #
#               nfs-enable, nfs-disable                                 #
#               nfs-status, nfs-exportfs-list-rules,                    #
#                                                                       #
#                                                                       #
#                                                                       #
# Copyright 2011 Network Appliance, Inc. All rights                     #
# reserved. Specifications subject to change without notice.            #
#                                                                       #
# This SDK sample code is provided AS IS, with no support or            #
# warranties of any kind, including but not limited to                  #
# warranties of merchantability or fitness of any kind,                 #
# expressed or implied.  This code is subject to the license            #
# agreement that accompanies the SDK.                                   #
#=======================================================================#


import re
import sys
sys.path.append("./NetApp")
from NaServer import *

def print_usage():
    print ("Usage:\n")
    print ("nfs.py <filer> <user> <password> <command>\n")
    print ("<filer> -- Filer name\n")
    print ("<user> -- User name\n")
    print ("<password> -- Password\n")
    print ("<command> -- enable, disable, status, list\n")
    sys.exit(2)


def do_nfs():
    s = NaServer(filer, 1, 3)
    out = s.set_transport_type('HTTPS')

    if(out and (out.results_errno() != 0)) :
        r = out.results_reason()
        print("Connection to filer failed" + r + "\n")
        sys.exit(2)

    out = s.set_style('LOGIN')

    if( out and (out.results_errno() != 0)):

        r = out.results_reason()
        print("Connection to filer failed" + r + "\n")
        sys.exit(2)

    out = s.set_admin_user(user, pw)

    if(cmd == "enable"):
        out = s.invoke("nfs-enable")

        if(out.results_status() == "failed"):
            print(out.results_reason() + "\n")
            sys.exit(2)

        else:
            print("Operation successful\n")

    elif (cmd == "disable"):
        out = s.invoke("nfs=disable")
gv[4]
do_nfs()

$ ./nfs.py hostname "user" "password" status
NFS Server is disabled

 Here is the same test, but using nfs.ps1 from API:gv[4]
do_nfs()

$ ./nfs.py hostname "user" "password" status
NFS Server is disabled

 Here is the same test, but using nfs.ps1 from API:

if(out.results_status == "failed"): print(out.results_reason() + "\n") sys.exit(2) else: print("Operation successful\n") elif (cmd == "status"): out = s.invoke("nfs-status") if(out.results_status == "failed"): print(out.results_reason() + "\n") sys.exit(2) enabled = out.child_get_string("is-enabled") if(enabled == "true"): print("NFS Server is enabled\n") else: print("NFS Server is disabled\n") elif ( cmd == "list") : out = s.invoke( "nfs-exportfs-list-rules" ) export_info = out.child_get("rules") if(export_info): result = export_info.children_get() else : sys.exit(2) for export in result: path_name = export.child_get_string("pathname") rw_list = "rw=" ro_list = "ro=" root_list = "root=" if(export.child_get("read-only")): ro_results = export.child_get("read-only") ro_hosts = ro_results.children_get() for ro in ro_hosts: if(ro.child_get_string("all-hosts")): all_hosts = ro.child_get_string("all-hosts") if(all_hosts == "true") : ro_list = ro_list + "all-hosts" break elif(ro.child_get_string("name")) : host_name = ro.child_get_string("name") ro_list = ro_list + host_name + ":" if(export.child_get("read-write")): rw_results = export.child_get("read-write") rw_hosts = rw_results.children_get() for rw in rw_hosts: if(rw.child_get_string("all-hosts")): all_hosts = rw.child_get_string("all-hosts") if(all_hosts == "true") : rw_list = rw_list + "all-hosts" break elif(rw.child_get_string("name")): host_name = rw.child_get_string("name") rw_list = rw_list + host_name + ":" if(export.child_get("root")): root_results = export.child_get("root") root_hosts = root_results.children_get() for root in root_hosts: if(root.child_get_string("all-hosts")): all_hosts = root.child_get_string("all-hosts") gv[4]
do_nfs()

$ ./nfs.py hostname "user" "password" status
NFS Server is disabled

 Here is the same test, but using nfs.ps1 from API:gv[4]
do_nfs()

$ ./nfs.py hostname "user" "password" status
NFS Server is disabled

 Here is the same test, but using nfs.ps1 from API:gv[4]
do_nfs()

$ ./nfs.py hostname "user" "password" status
NFS Server is disabled

 Here is the same test, but using nfs.ps1 from API:


if(all_hosts == "true"): root_list = root_list + "all-hosts" break elif(root.child_get_string("name")): host_name = root.child_get_string("name") root_list = root_list + host_name + ":" path_name = path_name + " " if(ro_list != "ro="): path_name = path_name + ro_list if(rw_list != "rw=") : path_name = path_name + "," + rw_list if(root_list != "root="): path_name = path_name + "," + root_list print(path_name + "\n") else : print ("Invalid operation\n") print_usage() args = len(sys.argv) - 1 if(args < 4): print_usage() filer = sys.argv[1] user = sys.argv[2] pw = sys.argv[3] cmd = sys.argv[4]
do_nfs()

$ ./nfs.py hostname "user" "password" status
NFS Server is disabled

 Here is the same test, but using nfs.ps1 from API:
gv[4] do_nfs()
$ ./nfs.py hostname "user" "password" status
NFS Server is disabled

 Here is the same test, but using nfs.ps1 from API:

PS C:\Users\CZZ02012\Documents> .\nfs.ps1 hostname "user" "password" status
NFS Server is enabled
5 REPLIES 5

yannb
11,662 Views

Can you fix the formatting of your message, I think it's all over the place, it would help understanding!

cams
11,445 Views

Not sure if you are still chasing help with this, but start with the typo

 

out = s.invoke("nfs=disable")

should be

out = s.invoke("nfs-disable")

 

This is a type on the NetApp example 

asulliva
11,412 Views

Are you executing this against a 7-mode or clustered Data ONTAP system?  With cDOT the results may be unpredictable since the NFS server belongs to the SVM and there can be more than one SVM.

 

Here is a python snippet which will print the NFS server and protocol status for each SVM which has the NFS protocol.  Note that if NFS has not been "enabled" for the SVM, regardless of the status of the service, it won't show up in this listing.

 

 

print("{:20s} {:12s} {:6s} {:6s} {:6s} {:6s}".format(
    "SVM",
    "NFS Enabled",
    "v2",
    "v3",
    "v4.0",
    "v4.1"))

result = server.invoke('nfs-service-get-iter')

if result.results_status() == "failed":
    reason = result.results_reason()
    print( reason + "\n" )
    sys.exit(2)

if result.child_get_int('num-records') == 0:
    print( "No SVMs returned" )
    sys.exit(0)

for service in result.child_get('attributes-list').children_get():
    svm_name = service.child_get_string('vserver')
    svm_nfs2_status = service.child_get_string('is-nfsv2-enabled');
    svm_nfs3_status = service.child_get_string('is-nfsv3-enabled');
    svm_nfs40_status = service.child_get_string('is-nfsv40-enabled');
    svm_nfs41_status = service.child_get_string('is-nfsv41-enabled');
    
    server.set_vserver(svm_name)
    query = server.invoke('nfs-status')
    svm_nfs_enabled = query.child_get_string('is-enabled')
    
    print("{:20s} {:12s} {:6s} {:6s} {:6s} {:6s}".format(
        svm_name,
        svm_nfs_enabled,
        svm_nfs2_status,
        svm_nfs3_status,
        svm_nfs40_status,
        svm_nfs41_status))

 

Alternatively, if you're executing against the SVM directly you wouldn't use the "nfs-service-get-iter" API against the cluster mangement interface, rather you would use the "nfs-service-get" API against the SVM management interface.  Just a subtle difference between them.

 

print("{:20s} {:12s} {:6s} {:6s} {:6s} {:6s}".format(
    "SVM",
    "NFS Enabled",
    "v2",
    "v3",
    "v4.0",
    "v4.1"))

result = server.invoke('nfs-service-get')

if result.results_status() == "failed":
    reason = result.results_reason()
    print( reason + "\n" )
    sys.exit(2)

for service in result.child_get('attributes').children_get():
    svm_name = service.child_get_string('vserver')
    svm_nfs2_status = service.child_get_string('is-nfsv2-enabled');
    svm_nfs3_status = service.child_get_string('is-nfsv3-enabled');
    svm_nfs40_status = service.child_get_string('is-nfsv40-enabled');
    svm_nfs41_status = service.child_get_string('is-nfsv41-enabled');
    
    query = server.invoke('nfs-status')
    svm_nfs_enabled = query.child_get_string('is-enabled')
    
    print("{:20s} {:12s} {:6s} {:6s} {:6s} {:6s}".format(
        svm_name,
        svm_nfs_enabled,
        svm_nfs2_status,
        svm_nfs3_status,
        svm_nfs40_status,
        svm_nfs41_status))

Hope that helps.

 

Andrew

 

If this post resolved your issue, please help others by selecting ACCEPT AS SOLUTION or adding a KUDO.

python_API_and_NFS_status
11,333 Views

Hello, testing it agains 7-mode only. We dont have cDot yet.

python_API_and_NFS_status
11,333 Views

seems that i was doing something wrongly in the first place.

So far it seems to work. Maybe a problem between keyboard and chair.

Public