티스토리 뷰

오픈스택을 운영하다보면 가끔 컴퓨트 노드에 몇개의 인스턴스가 생성되어 실행되고 있을까? 궁금해 질때가 있다. 쉽게 한줄의 명령어만으로 알아볼 수 있는 방법은 없을까? 하다가 몇가지 유용한 리눅스 명령어들을 조합하여 쉽게 컴퓨트 노드별 인스턴스 개수를 셀수 있는 방법을 찾아냈다. 

[stack@dir ~]$ source overcloudrc
(overcloud) [stack@dir ~]$ openstack server list --all --long -c Host -f value | sort | uniq -c
     7 cn01.exm.com
     5 cn02.exm.com
     4 cn03.exm.com
     7 cn04.exm.com
     7 cn05.exm.com
    10 cn06.exm.com
     8 cn07.exm.com
     9 cn08.exm.com
     6 cn09.exm.com
     2 cn10.exm.com

openstack server list

openstack server list 명령어를 수행하면 overcloudrc라는 인증정보에 해당하는 프로젝트의 인스턴스 개수가 나온다. 나는 주로 admin 권한을 가진 사용자 정보를 이용하여 해당 명령어를 실행하고는 하는데, 이 경우 모든 프로젝트의 인스턴스 정보를 조회해 볼수는 없다. 그래서, openstack server list 명령어 뒤에 --all 옵션을 붙여주면 모든 프로젝트의 인스턴스를 확인할 수 있고, --long 옵션을 주면, 해당 인스턴스가 어느 컴퓨트 노드에서 실행중인지 여부도 확인할 수 있다. 여기서 -c 옵션을 사용하여 컬럼 중 Host 컬럼만 보겠노라고 선언하고, -f  옵션을 이용하여 결과값을 value만 보겠다고 선언하면 아래와 같은 결과가 도출된다.

(overcloudrc) [stack@dir ~]$ openstack server list --all --long -c Host -f value
 cn01.exm.com
 cn05.exm.com
 cn01.exm.com
 cn02.exm.com
 cn01.exm.com
 cn05.exm.com
 cn04.exm.com
 cn01.exm.com 
 cn02.exm.com
 cn03.exm.com
 cn02.exm.com
 cn06.exm.com
 cn03.exm.com
 cn07.exm.com
 cn04.exm.com
 cn01.exm.com
 ...

sort

문자열을 정렬해 주는 Utility로 기본적으로 오름차순 정렬을 해 준다. 앞에서 조회한 내용을 sorting 하고 싶다면 sort 명령어를 이용하면 좋다. 그러면 앞에서 조회한 내용이 뒤죽박죽이였지만, sort 를 통해 오름차순으로 정렬이 된다.

(overcloudrc) [stack@dir ~]$ openstack server list --all --long -c Host -f value | sort
 cn01.exm.com
 cn01.exm.com
 cn01.exm.com
 cn01.exm.com
 cn01.exm.com
 cn02.exm.com
 cn02.exm.com
 cn02.exm.com
 cn03.exm.com
 cn03.exm.com
 cn04.exm.com
 cn04.exm.com
 cn05.exm.com
 cn06.exm.com
 cn07.exm.com
 ...

uniq

마지막으로 uniq라는 Utility를 사용해 중복된 라인을 제거하고 해당 라인의 개수를 세어 보여주는 -c 옵션을 사용하면 컴퓨트 노드별로 몇개의 인스턴스가 있는지를 쉽게 확인할 수 있다. 궁금해서 uniq --help 명령어를 실행해보니 아래와 같은 내용을 확인 할 수 있었다.

[root@rhel8 ~]# uniq --help
Usage: uniq [OPTION]... [INPUT [OUTPUT]]
Filter adjacent matching lines from INPUT (or standard input),
writing to OUTPUT (or standard output).

With no options, matching lines are merged to the first occurrence.

Mandatory arguments to long options are mandatory for short options too.
  -c, --count           prefix lines by the number of occurrences
  -d, --repeated        only print duplicate lines, one for each group
  -D                    print all duplicate lines
      --all-repeated[=METHOD]  like -D, but allow separating groups
                                 with an empty line;
                                 METHOD={none(default),prepend,separate}
  -f, --skip-fields=N   avoid comparing the first N fields
      --group[=METHOD]  show all items, separating groups with an empty line;
                          METHOD={separate(default),prepend,append,both}
  -i, --ignore-case     ignore differences in case when comparing
  -s, --skip-chars=N    avoid comparing the first N characters
  -u, --unique          only print unique lines
  -z, --zero-terminated     line delimiter is NUL, not newline
  -w, --check-chars=N   compare no more than N characters in lines
      --help     display this help and exit
      --version  output version information and exit

A field is a run of blanks (usually spaces and/or TABs), then non-blank
characters.  Fields are skipped before chars.

Note: 'uniq' does not detect repeated lines unless they are adjacent.
You may want to sort the input first, or use 'sort -u' without 'uniq'.
Also, comparisons honor the rules specified by 'LC_COLLATE'.

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Full documentation at: <https://www.gnu.org/software/coreutils/uniq>
or available locally via: info '(coreutils) uniq invocation'

오늘은 여기까지~~~ 너무 열심히 공부했더니 좀 쉬어줘야겠다. ㅋㅋ

최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/03   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함