Go to main content

Oracle® ZFS Storage Appliance Administration Guide, Release OS8.7.x

Exit Print View

Updated: November 2018
 
 

Using the List Function

In a context with dynamic children, it can be very useful to iterate over those children programmatically. This can be done by using the list function, which returns an array of dynamic children.

  1. The following example script iterates over every share in every project, printing out the amount of space consumed and space available:
    script
           run('shares');
           projects = list();
    
           for (i = 0; i < projects.length; i++) {
                   run('select ' + projects[i]);
                   shares = list();
    
                   for (j = 0; j < shares.length; j++) {
                           run('select ' + shares[j]);
                           printf("%s/%s %1.64g %1.64g\n", projects[i], shares[j],
                               get('space_data'), get('space_available'));
                           run('cd ..');
                   }
    
                   run('cd ..');
           }
  2. Here is the output of running the script, assuming it were saved to a file named "space.aksh":
    % ssh root@koi < space.aksh
    Password: 
    admin/accounts 18432 266617007104
    admin/exports 18432 266617007104
    admin/primary 18432 266617007104
    admin/traffic 18432 266617007104
    admin/workflow 18432 266617007104
    aleventhal/hw_eng 18432 266617007104
    bcantrill/analytx 1073964032 266617007104
    bgregg/dashbd 18432 266617007104
    bgregg/filesys01 26112 107374156288
    bpijewski/access_ctrl 18432 266617007104
    ...
  3. If one would rather a "pretty printed" (though more difficult to handle programmatically) variant of this, one could directly parse the output of the get command:
    script
           run('shares');
           projects = list();
    
           printf('%-40s %-10s %-10s\n', 'SHARE', 'USED', 'AVAILABLE');
    
           for (i = 0; i < projects.length; i++) {
                   run('select ' + projects[i]);
                   shares = list();
    
                   for (j = 0; j < shares.length; j++) {
                           run('select ' + shares[j]);
    
                           share = projects[i] + '/' + shares[j];
                           used = run('get space_data').split(/\s+/)[3];
                           avail = run('get space_available').split(/\s+/)[3];
    
                           printf('%-40s %-10s %-10s\n', share, used, avail);
                           run('cd ..');
                   }
    
                   run('cd ..');
           }
  4. Here is the output of running this new script, assuming it were named "prettyspace.aksh":
    % ssh root@koi < prettyspace.aksh
    Password:
    SHARE                                    USED       AVAILABLE 
    admin/accounts                           18K        248G      
    admin/exports                            18K        248G      
    admin/primary                            18K        248G      
    admin/traffic                            18K        248G      
    admin/workflow                           18K        248G      
    aleventhal/hw_eng                        18K        248G      
    bcantrill/analytx                        1.00G      248G      
    bgregg/dashbd                            18K        248G       
    bgregg/filesys01                         25.5K      100G      
    bpijewski/access_ctrl                    18K        248G      
    ...
  5. The list function supports optional arguments depth and filter.

    The format is: list ([depth, [filter]]). The argument depth can be defined by a number. The greater number of depth, the more details will be returned. The argument filter is formatted as {<prop1>:<val1>, <prop2>:<val2> ...}. If filter is specified, depth must also be specified.

    Usage and input behavior:

    • list() - Returns only node names.

    • list(0) - Return properties of node and only children names.

    • list(0, {kiosk_mode: true}) - Return a filtered list for kiosk_mode is true with names of children.

    • list(1) - Return properties of node, names and properties of children, only names of grandchildren.

    • list(1, {kiosk_mode: true}) - Return a filtered list for kiosk_mode is true with details up to depth=1.

    • list(2) - Return properties of node, names and properties of children and list(0) output of grandchildren.

    • list(2, {fullname:'Super*', kiosk_mode: true}) - Return a filtered list for fullname containing Super and kiosk_mode is true with details up to depth=2.

  6. This is an example output for a list with depth=2:

    The label name shows the name of the list item (that is, a node). The label properties shows the properties of the list item. The label children shows static children of the list item. The label list shows dynamic children of the list item.

    script
            ("." to run)> dump(list(2));
            ("." to run)> .
    
            [{
                name: 'restuser',
                properties: {
                    kiosk_screen: 'status/dashboard',
                    kiosk_mode: false,
                    roles: ['basic'],
                    require_annotation: false,
                    initial_password: 'DummyPassword',
                    fullname: 'REST User',
                    logname: 'restuser'
                },
                children: [{
                    name: 'preferences',
                    properties: {
                        advanced_analytics: false,
                        session_timeout: 15,
                        login_screen: 'status/dashboard',
                        locale: 'C'
                    }
                }, {
                    name: 'exceptions',
                    list: [{
                        name: 'auth-000',
                        properties: {
                             allow_configure: false,
                             scope: 'alert'
                        }
                    }, {
                        name: 'auth-001',
                        properties: {
                             allow_workgroup: false,
                             allow_domain: false,
                             name: '*',
                             scope: 'ad'
                        } 
                    }]
                }]
            }]