南大通用GBase 8a集群通过grep过滤快速获得coordinator和gnode的IP地址的方法

GBase 8a提供了gcadmin 命令可以返回集群节点的IP地址/主机名,以及各个服务的状态,如果用户要循环每个IP做一些操作,需要解析该输出。本文提供一种采用grep,快捷的获得coordinator和gnode的IP地址的方法。

参考

Linux grep 只返回匹配到的所需部分内容,不返回整行

环境

2节点集群,1个调度节点,2个数据节点。集群版本8.5.2.43

[root@rh6-1 ~]# gcadmin
CLUSTER STATE:  ACTIVE
CLUSTER MODE:   NORMAL

=================================================================
|             GBASE COORDINATOR CLUSTER INFORMATION             |
=================================================================
|   NodeName   |     IpAddress     |gcware |gcluster |DataState |
-----------------------------------------------------------------
| coordinator1 |    10.0.2.201     | OPEN  |  OPEN   |    0     |
-----------------------------------------------------------------
=============================================================
|              GBASE DATA CLUSTER INFORMATION               |
=============================================================
|NodeName |     IpAddress     |gnode |syncserver |DataState |
-------------------------------------------------------------
|  node1  |    10.0.2.201     | OPEN |   OPEN    |    0     |
-------------------------------------------------------------
|  node2  |    10.0.2.202     |CLOSE |   CLOSE   |    1     |
-------------------------------------------------------------
[root@rh6-1 ~]#

获得调度节点coordinator的所有IP或主机名

获得coordinator的XML格式的信息

root@rh6-1 ~]# gcadmin showcluster c f
<?xml version='1.0' encoding="utf-8"?>
<ClusterInfo>
    <ClusterState>ACTIVE</ClusterState>
    <ClusterMode>NORMAL</ClusterMode>

    <CoordinatorNodes>
        <CoordinatorNode>
            <NodeName>coordinator1</NodeName>
            <IpAddress>10.0.2.201</IpAddress>
            <gcware>OPEN</gcware>
            <gcluster>OPEN</gcluster>
            <DataState>0</DataState>
        </CoordinatorNode>
    </CoordinatorNodes>
</ClusterInfo>

用grep解析XML格式获得coordinator的IP信息

只需要grep,而且对于一行有多个IP的也能解析出来。

[root@rh6-1 ~]# gcadmin showcluster c f | grep -oP '(?<=<IpAddress>)(.+?)(?=</IpAddress>)'
10.0.2.201

获得数据节点coordinator的所有IP或主机名

获得数据节点的XML格式的信息

[root@rh6-1 ~]# gcadmin showcluster d f
<?xml version='1.0' encoding="utf-8"?>
<ClusterInfo>
    <ClusterState>ACTIVE</ClusterState>
    <ClusterMode>NORMAL</ClusterMode>

    <DataserverNodes>
        <DataServerNode>
            <NodeName>node1</NodeName>
            <IpAddress>10.0.2.201</IpAddress>
            <gnode>OPEN</gnode>
            <syncserver>OPEN</syncserver>
            <DataState>0</DataState>
        </DataServerNode>
        <DataServerNode>
            <NodeName>node2</NodeName>
            <IpAddress>10.0.2.202</IpAddress>
            <gnode>CLOSE</gnode>
            <syncserver>CLOSE</syncserver>
            <DataState>1</DataState>
        </DataServerNode>
    </DataserverNodes>
</ClusterInfo>

用grep解析数据节点的XML信息获得IP

[root@rh6-1 ~]# gcadmin showcluster d f | grep -oP '(?<=<IpAddress>)(.+?)(?=</IpAddress>)'
10.0.2.201
10.0.2.202

获得集群所有节点的IP信息

由于端口不同,除非是操作系统级操作需要每个集群IP信息。

[root@rh6-1 ~]# gcadmin showcluster f | grep -oP '(?<=<IpAddress>)(.+?)(?=</IpAddress>)'
10.0.2.201
10.0.2.201
10.0.2.202

因为有复合节点,要排序后去重一下sort+uniq

[root@rh6-1 ~]# gcadmin showcluster f | grep -oP '(?<=<IpAddress>)(.+?)(?=</IpAddress>)' | sort | uniq
10.0.2.201
10.0.2.202
[root@rh6-1 ~]#