Search code examples
pythonreplacenetapp

Selecting characters from command and replacing some of them


I have this result from a NetApp query

Object: nfsv4_diag
Instance: nfs4_diag
Start-time: 6/4/2020 16:55:40
End-time: 6/4/2020 16:55:40
Scope: NODE1

Counter                                                     Value
-------------------------------- --------------------------------
storePool_ByteLockAlloc                                         0
storePool_ClientAlloc                                          60
storePool_CopyStateAlloc                                        0
storePool_DelegAlloc                                            0
storePool_DelegStateAlloc                                       0
storePool_LayoutAlloc                                           0
storePool_LayoutStateAlloc                                      0
storePool_LockStateAlloc                                        0
storePool_OpenAlloc                                            86
storePool_OpenStateAlloc                                       86
storePool_OwnerAlloc                                           10
storePool_StringAlloc                                          70

Object: nfsv4_diag
Instance: nfs4_diag
Start-time: 6/4/2020 16:55:40
End-time: 6/4/2020 16:55:40
Scope: NODE2

Counter                                                     Value
-------------------------------- --------------------------------
storePool_ByteLockAlloc                                      1246
storePool_ClientAlloc                                          29          
storePool_CopyStateAlloc                                        0          
storePool_DelegAlloc                                            0          
storePool_DelegStateAlloc                                       0          
storePool_LayoutAlloc                                           0          
storePool_LayoutStateAlloc                                      0          
storePool_LockStateAlloc                                      468          
storePool_OpenAlloc                                           811          
storePool_OpenStateAlloc                                      811          
storePool_OwnerAlloc                                          519          
storePool_StringAlloc                                         548          

Object: nfsv4_diag                                                             
Instance: nfs4_diag                                                            
Start-time: 6/4/2020 16:55:40                                                  
End-time: 6/4/2020 16:55:40                                                    
Scope: NODE3 



Counter                                                     Value          
-------------------------------- --------------------------------          
storePool_ByteLockAlloc                                       165          
storePool_ClientAlloc                                          27          
storePool_CopyStateAlloc                                        0          
storePool_DelegAlloc                                            0          
storePool_DelegStateAlloc                                       0          
storePool_LayoutAlloc                                           0          
storePool_LayoutStateAlloc                                      0          
storePool_LockStateAlloc                                      135          
storePool_OpenAlloc                                           272          
storePool_OpenStateAlloc                                      272          
storePool_OwnerAlloc                                          152          
storePool_StringAlloc                                         179 

And I would like to transform it to something like:

NODE1.storePool_ByteLockAlloc=0;NODE1.storePool_ClientAlloc=60;NODE1.storePool_CopyStateAlloc=0;............NODEn.storePool_ByteLockAlloc=165;

For all the nodes I have from the output (not only 3).

I have tried replacing the output using this:

output.replace(' ', '').replace('Alloc', 'Alloc=').replace('\r','').replace('\n',' ')

But this is not giving me the result that I need.

Some ideas?

Thanks a lot in advance for your help.


Solution

  • If you capture the output into one long string, you can use a regex to find the lines of interest and parse them into the format you're requesting:

    import re
    
    output = """
    Object: nfsv4_diag
    Instance: nfs4_diag
    Start-time: 6/4/2020 16:55:40
    End-time: 6/4/2020 16:55:40
    Scope: NODE1
    
    Counter                                                     Value
    -------------------------------- --------------------------------
    storePool_ByteLockAlloc                                         0
    storePool_ClientAlloc                                          60
    storePool_CopyStateAlloc                                        0
    storePool_DelegAlloc                                            0
    .
    .
    .
    """
    
    rv = []
    current_node = None
    
    for match in re.findall(r'Scope: (NODE\d+)|(storePool_.*)', output):
        node, metric = match
        if node and current_node != node:
            current_node = node
    
        if current_node and metric:
            name, value = metric.strip().split()
            rv.append(f'{current_node.strip()}.{name}={value}')
    
    print(';'.join(rv))
    

    Output:

    NODE1.storePool_ByteLockAlloc=0;NODE1.storePool_ClientAlloc=60;NODE1.storePool_CopyStateAlloc=0;NODE1.storePool_DelegAlloc=0;...