'Linux'에 해당되는 글 3건

  1. 2012.03.12 iproute2 (tc)
  2. 2011.07.21 inotify 함수 예제 (inotify function example/sample) (1)
  3. 2009.03.24 bash와 csh의 차이점

iproute2 (tc)

c or linux 2012.03.12 22:55

http://www.tldp.org/HOWTO/Traffic-Control-HOWTO/software.html#s-iproute2

iproute2 is a suite of command line utilities which manipulate kernel structures for IP networking configuration on a machine.

네트워크 bandwidth를 control할 수 있는 utility
Posted by 김용환 '김용환'
TAG Linux

 Source : watch.c
파일(디렉토리) 의 create, modify, delete, move에 대해서 모니터링
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/inotify.h>
#define EVENT_SIZE  ( sizeof (struct inotify_event) )
#define BUF_LEN     ( 1024 * ( EVENT_SIZE + 16 ) )
int main( int argc, char **argv ) {
  char target[20]; /* monitoring directory name */
  int fd;
  int wd; /* watch desc */
  char buffer[BUF_LEN];
  fd = inotify_init();
  if (fd < 0) {
    perror("inotify_init");
  }
  if (argc < 2) {
    fprintf (stderr, "Watching the current directory\n");
    strcpy (target, ".");
  } else {
    fprintf (stderr, "Watching '%s' directory\n", argv[1]);
    strcpy (target, argv[1]);
  }
  wd = inotify_add_watch(fd, "/home1/knight", IN_MODIFY | IN_CREATE | IN_DELETE | IN_MOVED_FROM | IN_MOVED_TO | IN_MOVE_SELF);
  while(1) {
    int length, i = 0;
    length = read(fd, buffer, BUF_LEN);
    if (length < 0) {
      perror("read");
    }
    while (i < length) {
      struct inotify_event *event = (struct inotify_event *) &buffer[i];
      printf ("[debug] wd=%d mask=%d cookie=%d len=%d dir=%s\n", event->wd, event->mask, event->cookie, event->len, (event->mask & IN_ISDIR)?"yes":"no");
      if (event->len) {
        if (event->mask & IN_CREATE) {
          if (event->mask & IN_ISDIR) {
            printf("The directory %s was created.\n", event->name);      
          } else {
            printf("The file %s was created.\n", event->name);
          }
        } else if (event->mask & IN_DELETE) {
          if (event->mask & IN_ISDIR) {
            printf("The directory %s was deleted.\n", event->name);      
          } else {
            printf("The file %s was deleted.\n", event->name);
          }
        } else if (event->mask & IN_MODIFY) {
          if (event->mask & IN_ISDIR) {
            printf("The directory %s was modified.\n", event->name);
          } else {
            printf("The file %s was modified.\n", event->name);
          }
        } else if (event->mask & IN_MOVED_FROM || event->mask & IN_MOVED_TO || event->mask & IN_MOVE_SELF) {
          if (event->mask & IN_ISDIR) {
            printf("The directory %s was moved.\n", event->name);
          } else {
            printf("The file %s was moved.\n", event->name);
          }
        }
      }
      i += EVENT_SIZE + event->len;
    }
  }
  /*
  inotify_rm_watch(fd, wd);
  close(fd);
  */
  return 0;
}



빌드 및 실행
[/home/knight]  gcc -o watch.out watch.c
[/home/knight] ./watch.out
Watching the current directory


터미널 하나를 띄워서 테스트해본다.

[/home/knight] mkdir xx1
[/home/knight] mkdir xx2
[/home/knight] mv xx2 xx222
[/home/knight] rm -rf xx222

watch.out을 실행시켰던 터미널에서 결과를 확인한다.


./watch.out
Watching the current directory
[debug] wd=1 mask=1073742080 cookie=0 len=16 dir=yes
The directory xx1 was created.
[debug] wd=1 mask=1073742080 cookie=0 len=16 dir=yes
The directory xx2 was created.
[debug] wd=1 mask=1073741888 cookie=33508 len=16 dir=yes
The directory xx2 was moved.
[debug] wd=1 mask=1073741952 cookie=33508 len=16 dir=yes
The directory xx222 was moved.
[debug] wd=1 mask=1073742336 cookie=0 len=16 dir=yes
The directory xx222 was deleted.


괜찮은 Referneces
http://ko.wikipedia.org/wiki/Inotify
http://www.ibm.com/developerworks/kr/library/l-ubuntu-inotify/index.html
http://linux.die.net/man/7/inotify
http://darkeside.blogspot.com/2007/12/linux-inotify-example.html
Posted by 김용환 '김용환'

csh 스크립트를 정리하다가. 발견한 사실.. (이미 오래전에 적었을지도 모르지만.. ^^;;)

 

#!/bin/csh

 wget -O serverStatusLog 'http://127.0.0.1/server-status?auto';
cat serverStatusLog | awk -F: '{ print $2 }' > statusCurrent

set current=(`cat statusCurrent`)

 

csh에서의 ( ) 마크는 bash 에서는 다르게 해석한다고 말했는데..  

csh에서는 개행문자뿐 아니라 사이띄기도 ( )를 이용하면 배열로 만들 수 있는데 반해서

bash에서는 오직 개행문자만 배열로 만들 수 있다.

 

만약 개행문자로 나눌 수 있는 값이면, 아래처럼 그래도 쓸 수 있다.

 

#!/bin/csh

 wget -O serverStatusLog 'http://127.0.0.1/server-status?auto';
cat serverStatusLog | awk -F: '{ print $2 }' > nstatusCurrent

current=(`cat nstatusCurrent`)

 

그러나, 스페이로 이루어질 때는 tr을 이용해서 배열로 만들면 된다.

 

#!/bin/csh

 wget -O serverStatusLog 'http://127.0.0.1/server-status?auto';
cat serverStatusLog | awk -F: '{ print $2 }' > nstatusCurrent

current=`echo nstatusCurrent | tr ' ' ' '`

 

 

Posted by 김용환 '김용환'