github.com/cyverse/go-irodsclient@v0.13.2/irods/fs/system.go (about)

     1  package fs
     2  
     3  import (
     4  	"strconv"
     5  
     6  	"github.com/cyverse/go-irodsclient/irods/common"
     7  	"github.com/cyverse/go-irodsclient/irods/connection"
     8  	"github.com/cyverse/go-irodsclient/irods/message"
     9  	"github.com/cyverse/go-irodsclient/irods/types"
    10  	"github.com/cyverse/go-irodsclient/irods/util"
    11  	"golang.org/x/xerrors"
    12  )
    13  
    14  // StatProcess stats processes.
    15  func StatProcess(conn *connection.IRODSConnection, address string, zone string) ([]*types.IRODSProcess, error) {
    16  	// lock the connection
    17  	conn.Lock()
    18  	defer conn.Unlock()
    19  
    20  	processes := []*types.IRODSProcess{}
    21  	req := message.NewIRODSMessageGetProcessstatRequest(address, zone)
    22  
    23  	queryResult := message.IRODSMessageQueryResponse{}
    24  	err := conn.Request(req, &queryResult, nil)
    25  	if err != nil {
    26  		return nil, xerrors.Errorf("failed to receive a process stat result message: %w", err)
    27  	}
    28  
    29  	err = queryResult.CheckError()
    30  	if err != nil {
    31  		if types.GetIRODSErrorCode(err) == common.CAT_NO_ROWS_FOUND {
    32  			// empty
    33  			return processes, nil
    34  		}
    35  		return nil, xerrors.Errorf("received a process stat query error: %w", err)
    36  	}
    37  
    38  	if queryResult.RowCount == 0 {
    39  		return processes, nil
    40  	}
    41  
    42  	if queryResult.AttributeCount > len(queryResult.SQLResult) {
    43  		return nil, xerrors.Errorf("failed to receive process stat attributes - requires %d, but received %d attributes", queryResult.AttributeCount, len(queryResult.SQLResult))
    44  	}
    45  
    46  	pagenatedProcesses := make([]*types.IRODSProcess, queryResult.RowCount)
    47  
    48  	for attr := 0; attr < queryResult.AttributeCount; attr++ {
    49  		sqlResult := queryResult.SQLResult[attr]
    50  		if len(sqlResult.Values) != queryResult.RowCount {
    51  			return nil, xerrors.Errorf("failed to receive process stat rows - requires %d, but received %d attributes", queryResult.RowCount, len(sqlResult.Values))
    52  		}
    53  
    54  		for row := 0; row < queryResult.RowCount; row++ {
    55  			value := sqlResult.Values[row]
    56  
    57  			if pagenatedProcesses[row] == nil {
    58  				// create a new
    59  				pagenatedProcesses[row] = &types.IRODSProcess{
    60  					ID:            -1,
    61  					ProxyUser:     "",
    62  					ProxyZone:     "",
    63  					ClientUser:    "",
    64  					ClientZone:    "",
    65  					ClientAddress: "",
    66  					ServerAddress: "",
    67  					ClientProgram: "",
    68  				}
    69  			}
    70  
    71  			switch sqlResult.AttributeIndex {
    72  			case int(common.ICAT_COLUMN_PROCESS_ID):
    73  				processID, err := strconv.ParseInt(value, 10, 64)
    74  				if err != nil {
    75  					return nil, xerrors.Errorf("failed to parse process id '%s': %w", value, err)
    76  				}
    77  				pagenatedProcesses[row].ID = processID
    78  			case int(common.ICAT_COLUMN_STARTTIME):
    79  				sT, err := util.GetIRODSDateTime(value)
    80  				if err != nil {
    81  					return nil, xerrors.Errorf("failed to parse start time '%s': %w", value, err)
    82  				}
    83  				pagenatedProcesses[row].StartTime = sT
    84  			case int(common.ICAT_COLUMN_CLIENT_NAME):
    85  				pagenatedProcesses[row].ClientUser = value
    86  			case int(common.ICAT_COLUMN_CLIENT_ZONE):
    87  				pagenatedProcesses[row].ClientZone = value
    88  			case int(common.ICAT_COLUMN_PROXY_NAME):
    89  				pagenatedProcesses[row].ProxyUser = value
    90  			case int(common.ICAT_COLUMN_PROXY_ZONE):
    91  				pagenatedProcesses[row].ProxyZone = value
    92  			case int(common.ICAT_COLUMN_REMOTE_ADDR):
    93  				pagenatedProcesses[row].ClientAddress = value
    94  			case int(common.ICAT_COLUMN_PROG_NAME):
    95  				pagenatedProcesses[row].ClientProgram = value
    96  			case int(common.ICAT_COLUMN_SERVER_ADDR):
    97  				pagenatedProcesses[row].ServerAddress = value
    98  			default:
    99  				// ignore
   100  			}
   101  		}
   102  	}
   103  
   104  	processes = append(processes, pagenatedProcesses...)
   105  	return processes, nil
   106  }