2017년 2월 20일 월요일

특정 기간에 수정된 파일 찾기 및 복사.

장애 시점에 생성된 파일 찾기.


특별한 내용은 없는데 할 때마다 문서를 찾아야 하는 나쁜 머리를 위해서 기록해둠.

1. find.sh

   코드로 내용은 대체.

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/bin/sh
 
#if [ "$#" -eq 1 ] || [ "$#" -eq 2 ]; then
#   echo "$0 $1 $2"
#   touch -t $1 ./startdatetime.noop
#   if [ "$#" -eq 2 ]; then
#       touch -t $2 ./enddatetime.noop
#       find ./ -newer ./startdatetime.noop -a ! -newer ./enddatetime.noop
#   else
#       find ./ -newer ./startdatetime.noop
#   fi
#   rm ./startdatetime.noop
#   if [ "$#" -eq 2 ]; then
#       rm ./enddatetime.noop
#   fi
#   exit 0
#fi
 
recursive_mode=false
enddatetime=$(date +%Y%m%d%H%M.%S)
touch -t $enddatetime ./enddatetime.noop
 
function usage(){
    echo "findr.sh [-r] 200710282133.24 [201502102359.59]"
    echo "          -r : recursive mode (optional)"
    echo "              200710282133.24 : startdatetime (necessary)"
    echo "              [201502102359.59] : enddatetime (optional)"
    echo "  If the end time parameter is omitted, the current time is the end time."
    exit 1
}
 
while getopts ":r :s: :e:" opt; do
    case $opt in
        r)
            echo "-r - Recursive Mode Enable"
            recursive_mode=true
            ;;
        \?)
            echo "Invalid command."
            exit 1
            ;;
    esac
done
 
if [ "$recursive_mode" = true ]; then
    if [ "$#" -ne 2 ] && [ "$#" -ne 3 ]; then
        usage
    else
        touch -t $2 ./startdatetime.noop
        if [ "$#" -eq 3 ]; then
            touch -t $3 ./enddatetime.noop
        fi
        find . -newer ./startdatetime.noop -! -newer ./enddatetime.noop |egrep -"^(./enddatetime.noop)"
    fi
else
    if [ "$#" -ne 1 ] && [ "$#" -ne 2 ]; then
        usage
    else
        touch -t $1 ./startdatetime.noop
        if [ "$#" -eq 2 ]; then
            touch -t $2 ./enddatetime.noop
        fi
        find . -maxdepth 1 -newer ./startdatetime.noop -! -newer ./enddatetime.noop |egrep -"^(./enddatetime.noop)"
    fi
fi
cs

2. cp.

   찾은 파일들을 어딘가로 옮기고 싶을 때는 다음과 같이 한다.

1
mv `find.sh 201702161500.00 201702161505.00` ./../queue/
cs

2017년 2월 16일 목요일

Grafana & graphite 그리고 모니터링 #05.

그런데 그래프가 하루만 그려진다.



모든 준비가 끝나 예쁜 그래프를 보면서 모니터링을 잘 할 수 있을거라 생각했지만...

1. 하루 살이.


   위 그림과 같이 이틀 동안의 그래프를 보도록 설정하고 데이터는 이미 충분히 오랫동안
   수집하고 있는 상태지만 다음 그림과 같이 보인다.


2. storage-schemas.conf.

   파일 이름과 같이 whisper database를 위한 schema를 정의하는 파일입니다.
   파일을 열어보면 앞 부분에 친절하게 적혀 있듯이 위에서부터 순서대로 적용되게 되어
   있어 파일 제일 뒤에 내용을 추가하면 적용되지 않습니다.
   이유는 [default_1min_for_1day] 항목에서 모두 만족하기 때문에 그 다음 항목이 적용
   대상인지 확인할 필요가 없기 때문입니다.

   어쨌든, 그럼 우리가 필요한 schema를 정의해 보도록 합니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Schema definitions for Whisper files. Entries are scanned in order,
# and first match wins. This file is scanned for changes every 60 seconds.
#  [name]
#  pattern = regex
#  retentions = timePerPoint:timeToStore, timePerPoint:timeToStore, ...
 
# Carbon's internal metrics. This entry should match what is specified in
# CARBON_METRIC_PREFIX and CARBON_METRIC_INTERVAL settings
[carbon]
pattern = ^carbon\.
retentions = 60:90d
 
[sample]
pattern = ^sample\.
retentions = 60s:1d,5m:7d,30m:1y
 
[default_1min_for_1day]
pattern = .*
retentions = 60s:1d
cs

   그리고 graphite container를 재시작 해보지만 역시나 하루만 보이게 됩니다.

3. 해결 방법.

   Whisper database는 rrd와 마찬가지로 최초 schema에 맞춰 database를 생성하고 그 상태
   그대로 유지합니다. 일반적으로는 whisper database file을 삭제하고 재시작 하게 되면
   schema 변경에 맞춰 적용되겠지만 고맙게도 whisper-resize(.py) 라는 도구를
   지원합니다.

   http://github.com/graphite-project/whisper

   위 사이트를 방문해서 whisper-resize에 대한 설명을 확인해 보면 다음과 같이 되어
   있습니다.

   Usage: whisper-resize.py path timePerPoint:timeToStore [timePerPoint:timeToStore]*

   timePerPoint and timeToStore specify lengths of time, for example:

   60:1440      60 seconds per datapoint, 1440 datapoints = 1 day of retention
   15m:8        15 minutes per datapoint, 8 datapoints = 2 hours of retention
   1h:7d        1 hour per datapoint, 7 days of retention
   12h:2y       12 hours per datapoint, 2 years of retention


   Options:
     -h, --help            show this help message and exit
     --xFilesFactor=XFILESFACTOR
                           Change the xFilesFactor
     --aggregationMethod=AGGREGATIONMETHOD
                           Change the aggregation function (average, sum, last,
                           max, min)
     --force               Perform a destructive change
     --newfile=NEWFILE     Create a new database file without removing the
                           existing one
     --nobackup            Delete the .bak file after successful execution
     --aggregate           Try to aggregate the values to fit the new archive
                           better. Note that this will make things slower and use
                           more memory.

   그럼 이제 graphite container에 접속해서 whisper database 수정을 해보자.

1
whisper-resize ./5032.wsp 60s:1d 5m:7d 30m:1y
cs


   이제 다음 그림과 같이 그래프의 왼쪽 영역에도 나오기 시작한다.


2017년 2월 15일 수요일

Syntax Highlight.

예쁘게 보이기.


블로그에 게시하거나 작업을 진행하면서 코드를 다른 사람과 공유할 일이 있을 때,
하얗고 까만것 보다는 알록달록 한 것이 더 낫다.

1. Pygments.

   서비스에 통합하는 용도로 자주 사용하고 있지만, 데모 페이지에서 바로
   사용하는 방법도 있다.
  1. http://pygments.org/demo/
  2. 코드를 입력한다.
  3. 변환, 복사 후 MS WORD에 붙여넣기.
  4. 그걸 다시 복사해서 네이버 블로그나 메일 어플리케이션에 붙여넣는다.

2. Color Scripter.

   Pygments demo 페이지보다는 깔끔하고 몇 가지 문제를 제외하면 사용성도 좋다.

  1. https://colorscripter.com/
  2. 언어 설정, 없는 경우 확장 스토어를 방문 후 검색해 본다.
  3. 코드 입력 후 클립 보드에 복사한다.
   이 과정에서 옵션을 조금 수정해야 blogspot에 정상 적용된다.
   다음 그림과 같이 세부 설정을 선택해 HTML 태그 자체 복사를 선택해준다.


   저장을 누르고 클립 보드에 복사를 한다.


   blogspot에 HTML 코드를 붙여넣기 한다.
   이 때, 굉장히 이상한 상황이 벌어지는데 글쓰기 모드와 HTML 모드를 왔다 갔다 하면
   코드가 제대로 보이지 않고 이상해진다.

3. SyntaxHighlighter.

   http://alexgorbatchev.com/SyntaxHighlighter/
   좋아 보이기는 하는데 하라는대로 해도 적용되지 않는다.
   웹 비전문가 입장에서 어렵다.

2017년 2월 14일 화요일

Grafana & graphite 그리고 모니터링 #04.

Local storage에 데이터 저장하기.


Docker container가 자동 시작 되었지만 데이터가 container에 저장되게 되어 있어서,
성능 및 유지 보수 관점에서 문제가 된다. 데이터를 host에 저장하자.

1. Run shell script.

1
2
3
4
5
6
#!/bin/sh
 
carbon-cache --config=/etc/carbon/carbon.conf start
chown -R carbon:carbon /var/lib/carbon/rrd/
chown -R carbon:carbon /var/lib/carbon/whisper/
python /usr/lib/python2.7/site-packages/graphite/manage.py runserver 0:8000
cs

   이전 글에서 없었던 4, 5번 라인이 추가되었다. 이게 좀 웃긴데 container를 실행할 때
   --volume 옵션을 사용해서 host에 저장하게 되면 container의 경로의 소유자가 강제로
   root로 변경된다. 하지만 carbon의 기본 계정은 carbon이 되고 따라서 데이터가 저장될
   경로의 소유자도 carbon이어야 시계열 데이터가 정상적으로 저장이 된다.


2. Docker build script.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# VERSION 1.0
FROM centos:7.2.1511
MAINTAINER ships <ships@darts.kr>
 
RUN yum install -y epel-release
RUN cp /usr/share/zoneinfo/Asia/Seoul /etc/localtime
 
RUN useradd pxuser
ADD ./Run.sh /home/pxuser/
RUN chmod +/home/pxuser/Run.sh
 
RUN yum install -y python-carbon
RUN yum install -y graphite-web
 
RUN django-admin syncdb --settings=graphite.settings --noinput
 
EXPOSE 8000
VOLUME ["/var/lib/carbon/rrd""/var/lib/carbon/whisper"]
 
WORKDIR /home/pxuser/
CMD ["./Run.sh"]
cs

   이전 글과 비교해서 특별한 것은 없고 작업 경로의 Run.sh이 실행 권한이 없을 경우
   이미지에 복사된 스크립트도 실행 권한이 없어서 실행이 안되는 문제를 해결하기 위해
   실행 권한을 부여하는 것과 저장소 관련 명령어 정도가 추가 되었다.


3. Docker run.

   이제 volume 옵션을 주고 실행하면 준비는 끝.
1
2
3
4
docker run --5338:2003 \              
              -/var/lib/carbon/rrd:/var/lib/carbon/rrd \              
              -/var/lib/carbon/whisper:/var/lib/carbon/whisper \              
              --name graphite graphite:1.0
cs