slapdのログをsyslogに渡す

■デフォルトではモノ言わぬslapdらしいが、「/var/log/syslog」に出るのは、
 DEBUGレベル256の簡易ログといったところか。。。
 環境は以下。

 WheezyでLDAP認証、SSHログイン
 http://d.hatena.ne.jp/labunix/20130418

 slapdのDBのバックアップ(3パターン+1)、DBの初期化、リストア
 http://d.hatena.ne.jp/labunix/20130419

■slpdのログを取る。
 デフォルトのLOCAL4から変更することもできる。

$ sudo slapd -V
@(#) $OpenLDAP: slapd  (Jun 27 2012 03:42:36) $
        vorlon@borges:/home/devel/openldap/build-area/openldap-2.4.31/debian/build/servers/slapd

$ sudo grep OPTIONS /etc/default/slapd
SLAPD_OPTIONS=""

$ man slapd | grep -A 6 "\-s syslog\-level\$"
       -s syslog-level
              This option tells slapd at what debug-level debugging statements
              should be logged to the syslog(8) facility.  The  value  syslog-
              level  can  be set to any value or combination allowed by the -d
              switch.  Slapd logs all messages selected  by  syslog-leveli  at
              the  syslog(3) severity debug-level DEBUG, on the unit specified
              with -l.

$ man slapd | grep -A 5 "\-l syslog\-local\-user\$"
       -l syslog-local-user
              Selects  the  local user of the syslog(8) facility. Value can be
              LOCAL0, through LOCAL7, as well as USER and DAEMON.  The default
              is  LOCAL4.   However,  this option is only permitted on systems
              that support local users with the syslog(8)  facility.   Logging
              to syslog(8) occurs at the "DEBUG" severity debug-level.

■結局のところ、「DEBUGって何?」の説明を避けては通れない。
 これが各所で説明が異なる。結局使ってみるしかない。

$ sudo slapd -d ?
Installed log subsystems:

        Any                            (-1, 0xffffffff)
        Trace                          (1, 0x1)
        Packets                        (2, 0x2)
        Args                           (4, 0x4)
        Conns                          (8, 0x8)
        BER                            (16, 0x10)
        Filter                         (32, 0x20)
        Config                         (64, 0x40)
        ACL                            (128, 0x80)
        Stats                          (256, 0x100)
        Stats2                         (512, 0x200)
        Shell                          (1024, 0x400)
        Parse                          (2048, 0x800)
        Sync                           (16384, 0x4000)
        None                           (32768, 0x8000)

NOTE: custom log subsystems may be later installed by specific code

■2048までは下記に日本語の説明がある。
 「admin23」という文言から分かるように()OpenLDAP2.3の説明である。

$ w3m -dump_source "http://www5f.biglobe.ne.jp/~inachi/openldap/admin23/runningslapd-ja.html" | \
  nkf -Lu -c -w | grep -B 1 -A 121 "表7.1: デバッグレベル"  | grep -v '^<' | \
  awk  '{if(NR%2==0)print;else printf "%5d,",$1}'
   -1,すべてのデバッグレベルを有効にする
    0,デバッグしない
    1,関数呼出しのトレース
    2,パケット処理のデバッグ
    4,詳細なデバッグトレース
    8,接続管理
   16,パケット送受信の印字
   32,検索フィルタ処理
   64,設定ファイル処理
  128,アクセス制御リスト処理
  256,接続/操作/結果の統計ログ
  512,エントリ送信の統計ログ
 1024,shell バックエンドとの通信の印字
 2048,エントリ解析のデバッグ印字

■英語の3列の説明では「admin24」なので、残りの2つも説明されている。
 日本語にするとしたら、以下のような感じかな。
 結局「None」は出すのか出さないのか。。。

 「syncreplコンシューマ(プロバイダ間との)処理」

$ w3m -dump_source "http://www.openldap.org/doc/admin24/runningslapd.html" | \
  grep -B 1 -A 188 Levels | grep -v '^<' | \
  sed s/"("//g | sed s/"\&nbsp;"/"0x0"/ | \
  awk  '{if(NR%3==0)print;else printf "%8s,",$1}'
      -1,     any,enable all debugging
       0,     0x0,no debugging
       1,     0x1,trace function calls
       2,     0x2,debug packet handling
       4,     0x4,heavy trace debugging
       8,     0x8,connection management
      16,    0x10,print out packets sent and received
      32,    0x20,search filter processing
      64,    0x40,configuration processing
     128,    0x80,access control list processing
     256,   0x100,stats log connections/operations/results
     512,   0x200,stats log entries sent
    1024,   0x400,print communication with shell backends
    2048,   0x800,print entry parsing debugging
   16384,  0x4000,syncrepl consumer processing
   32768,  0x8000,only messages that get logged whatever log level is set

■ちなみにAWK文を使わないで書くなら、「TABLE BORDER=0」にして罫線を消せば良いのです。

$ w3m -dump_source "http://www.openldap.org/doc/admin24/runningslapd.html" | \
  grep -B 1 -A 188 Levels | \
  sed s/"(\|)"//g | sed s/"\&nbsp;"/"0x0"/ | sed s/BORDER/"&=0"/ | w3m -dump -T text/html
                       Table 7.1: Debugging Levels
Level Keyword      Description
   -1 any          enable all debugging
    0 0x0          no debugging
    1 0x1 trace    trace function calls
    2 0x2 packets  debug packet handling
    4 0x4 args     heavy trace debugging
    8 0x8 conns    connection management
   16 0x10 BER     print out packets sent and received
   32 0x20 filter  search filter processing
   64 0x40 config  configuration processing
  128 0x80 ACL     access control list processing
  256 0x100 stats  stats log connections/operations/results
  512 0x200 stats2 stats log entries sent
 1024 0x400 shell  print communication with shell backends
 2048 0x800 parse  print entry parsing debugging
16384 0x4000 sync  syncrepl consumer processing
32768 0x8000 none  only messages that get logged whatever log level is set


■「設定に依存しない最低限の出力のみ」が「None」ですね。
 こちらはthなので、AWK文は不要です。

$ w3m -dump_source "http://www.atmarkit.co.jp/ait/articles/1001/20/news107.html" | \
  nkf -Lu -c -w | grep -B 3 -A 86 "th.*ログレベル.*10進数" | w3m -dump -T text/html -cols 140
ログレベル(10進数) ログレベル(16進数) ログレベル(文字列)               ログ出力内容
         -1          -                    any                  すべてのログを出力
         0           -                    -                    まったくログを出力しない
         1           0x1                  trace                内部の関数呼び出し
         2           0x2                  packets              パケット操作
         4           0x4                  args                 処理の詳細なトレース
         8           0x8                  cons                 コネクション管理
         16          0x10                 BER                  パケット送受信
         32          0x20                 filter               検索フィルタ処理
         64          0x40                 config               設定ファイルの処理
        128          0x80                 ACL                  アクセス制御リストの処理
        256          0x100                stats                ステータスログ(デフォルト)
        512          0x200                stats2               ステータスログ2
        1024         0x400                shell                シェルバックエンドとの通信
        2048         0x800                parse                エントリの解析
       16384         0x4000               sync                 syncrepl コンシューマ処理
       32768         0x8000               none                 設定に依存しない最低限の出力のみ

■参考

 OpenLDAPとログの設定
 http://gihyo.jp/admin/serial/01/ldap/0009

■上記ではLOCAL4、ログレベル「256」あたりが良いとされている。
 「conn」は「8(0x8)」も設定しているのではないだろうか。

 設定先はauthの手前とする。
 ユーザ名/グループ名は共に「openldap」

$ sed s%"\(SLAPD_OPTIONS=\"\)\""%"\1-s 256\""% /etc/default/slapd | grep OPTIONS
SLAPD_OPTIONS="-s 256"

$ sudo sed s%"^auth.*"%"LOCAL4.*\t\t\t/var/log/slapd.log\n&"% /etc/rsyslog.conf | grep -B 1 ^auth
LOCAL4.*                        /var/log/slapd.log
auth,authpriv.*                 /var/log/auth.log

$ grep openldap /etc/passwd /etc/group | awk -F\: '{print $1,$2}'
/etc/passwd openldap
/etc/group openldap

■syslogに書き込む設定

$ sudo sed -i s%"\(SLAPD_OPTIONS=\"\)\""%"\1-s 256\""% /etc/default/slapd

■rsyslog側で受け取る設定

$ sudo sed -i s%"^auth.*"%"LOCAL4.*\t\t\t/var/log/slapd.log\n&"% /etc/rsyslog.conf

■AWK文は最後尾の番号を覚えてます。

$ sudo touch /var/log/slapd.log
$ sudo chown openldap:openldap /var/log/slapd.log
$ ls -l /var/log/slapd.log | awk '{print $3,$4,$NF}'
openldap openldap /var/log/slapd.log

■まず最初に見るべきは「/var/log/syslog」でしょうね。

$ sudo /etc/init.d/rsyslog restart
$ sudo /etc/init.d/slapd restart
$ sudo tail -10 /var/log/syslog | grep "slapd starting" | awk -F']: ' '{print $2}'
slapd starting

$ sudo /etc/init.d/slapd restart
daemon: shutdown requested and initiated.
slapd shutdown: waiting for 0 operations/tasks to finish
slapd stopped.
@(#) $OpenLDAP: slapd  (Jun 27 2012 03:42:36) $#012#011vorlon@borges:/home/devel/openldap/build-area/openldap-2.4.31/debian/build/servers/slapd
slapd starting


■「ldap.h」だけ取り出して正常終了のステータスを確認。
 どこからの戻りかも考慮しないといけないのですね。。。

$ sudo apt-get source --download-only slapd
$ tar ztvf openldap_2.4.31.orig.tar.gz | grep "\/ldap\.h\$"
-rw-r--r-- vorlon/vorlon  64743 2012-04-23 05:25 openldap_2.4.31.orig/include/ldap.h

$ gunzip -c openldap_2.4.31.orig.tar.gz | tar xvf - openldap_2.4.31.orig/include/ldap.h
openldap_2.4.31.orig/include/ldap.h

$ grep "0x00*\$" openldap_2.4.31.orig/include/ldap.h | sed s/"\t"/" "/g | awk '{printf "%-17s %5s\n",$2,$3}'
LDAP_OPT_API_INFO 0x0000
LDAP_SYNC_NONE     0x00
LDAP_SUCCESS       0x00
LDAP_DEREF_NEVER   0x00
LDAP_MSG_ONE       0x00

■見る限り0x80(128)以下でおおよそ基本的なエラーはカバーしていています。
 0x100(256)は統計情報を得る為のものです。

$ grep ERROR openldap_2.4.31.orig/include/ldap.h
#define LDAP_OPT_ERROR_NUMBER                   LDAP_OPT_RESULT_CODE
#define LDAP_OPT_ERROR_STRING                   LDAP_OPT_DIAGNOSTIC_MESSAGE
#define LDAP_OPT_ERROR          (-1)
#define LDAP_OPERATIONS_ERROR           0x01
#define LDAP_PROTOCOL_ERROR                     0x02
#define LDAP_ATTR_ERROR(n)      LDAP_RANGE((n),0x10,0x15) /* 16-21 */
#define LDAP_NAME_ERROR(n)      LDAP_RANGE((n),0x20,0x24) /* 32-34,36 */
#define LDAP_SECURITY_ERROR(n)  LDAP_RANGE((n),0x2F,0x32) /* 47-50 */
#define LDAP_SERVICE_ERROR(n)   LDAP_RANGE((n),0x33,0x36) /* 51-54 */
#define LDAP_UPDATE_ERROR(n)    LDAP_RANGE((n),0x40,0x47) /* 64-69,71 */
#define LDAP_VLV_ERROR                          0x4C
#define LDAP_E_ERROR(n) LDAP_RANGE((n),0x1000,0x3FFF)
#define LDAP_X_ERROR(n) LDAP_RANGE((n),0x4000,0xFFFF)
#define LDAP_API_ERROR(n)               ((n)<0)
#define LDAP_LOCAL_ERROR                                (-2)
#define LDAP_ENCODING_ERROR                             (-3)
#define LDAP_DECODING_ERROR                             (-4)
#define LDAP_FILTER_ERROR                               (-7)
#define LDAP_PARAM_ERROR                                (-9)
#define LDAP_CONNECT_ERROR                              (-11)

■結局のところ、以下のように欲しいログを組み合わせる為に、
 明示的に足し算して指定した方が良いと思う。

$ sudo slapd -d ? | \
  grep "Conns\|Stats\|Conf"/" "/g | \
  awk '{sum+=$2;printf "%-10s %10d\n",$1,$2}END{printf "%-10s %10d\n","Total",sum}'
Conns               8
Config             64
Stats             256
Stats2            512
Total             840

$ sudo grep OPT /etc/default/slapd
# OPT = Conns,Config,Stats,Stats2
SLAPD_OPTIONS="-s 840"