logrotate.confのlastaction/endscriptの置き場所による動作変化

logrotateで利用するlastaction/endscriptの置き場所による実行変化について確認しました。

```:環境
CentOS Linux release 7.6.1810 (Core)
logrotate-3.8.6-17.el7.x86_64
```

なお、確認にあたり以下コマンドを実行しています。

**デバッグ**
`logrotate -d /etc/logrotate.conf`
**手動実行**
`/etc/cron.daily/logrotate`



</br>
#1.個別ログローテーションの中に置く場合
```sh:case1.logrotate.conf(抜粋)
/test/log/wtmp.log {
    monthly
    create 0664 root utmp
        minsize 1M
    rotate 1

lastaction
        echo `date "+%Y/%m/%d-%H:%M:%S"` "done:lastaction in /test/log/wtmp.log" >> /test/log/lastaction.log
endscript
}
```

lastaction/endscriptが個別ログ条件の中にあるパターンです。
（一番よくあるパターンかと思います）

この場合、個別ログローテーションの動作条件を満たした場合、lastaction/endscriptが動作します。
動作条件を満たさない場合、`not running last action script`ということでlastaction/endscriptが動作しませんでした。

<br>
###1-1.動作条件を満たす場合
lastaction/endscript：**動作する**

```sh:case1-1.debug
rotating pattern: /test/log/wtmp.log  monthly (1 rotations)
empty log files are rotated, only log files >= 1048576 bytes are rotated, old logs are removed
considering log /test/log/wtmp.log
  log needs rotating
rotating log /test/log/wtmp.log, log->rotateCount is 1
dateext suffix '-20220122'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
glob finding old rotated logs failed
renaming /test/log/wtmp.log to /test/log/wtmp.log-20220122
creating new /test/log/wtmp.log mode = 0664 uid = 0 gid = 22
running last action script
running script with arg /test/log/wtmp.log : "
        echo `date "+%Y/%m/%d-%H:%M:%S"` "done:lastaction in /test/log/wtmp.log" >> /test/log/lastaction.log
"
```

<br>
###1-2.動作条件を満たさない場合(期間未達)
lastaction/endscript：**動作しない**

>動作条件を満たさない理由
log has been already rotated

```sh:case1-3.debug
rotating pattern: /test/log/wtmp.log  monthly (1 rotations)
empty log files are rotated, only log files >= 1048576 bytes are rotated, old logs are removed
considering log /test/log/wtmp.log
  log does not need rotating (log has been already rotated)not running last action script, since no logs will be rotated
```

<br>
###1-3.動作条件を満たさない場合(容量未達)
lastaction/endscript：**動作しない**

>動作条件を満たさない理由
'misinze' directive is used and the log size is smaller than the minsize value

```sh:case1-2.debug
rotating pattern: /test/log/wtmp.log  monthly (1 rotations)
empty log files are rotated, only log files >= 1048576 bytes are rotated, old logs are removed
considering log /test/log/wtmp.log
  log does not need rotating ('misinze' directive is used and the log size is smaller than the minsize valuenot running last action script, since no logs will be rotated
```

<br>
###1-4.動作条件を満たさない場合(期間未達かつ容量未達)
lastaction/endscript：**動作しない**
>動作条件を満たさない理由
log has been already rotated
＃最上位条件で弾いたため、sizeの方は無視されたようです。

```sh:case1-4.debug
rotating pattern: /test/log/wtmp.log  monthly (1 rotations)
empty log files are rotated, only log files >= 1048576 bytes are rotated, old logs are removed
considering log /test/log/wtmp.log
  log does not need rotating (log has been already rotated)not running last action script, since no logs will be rotated
```




<br>
#2.lorotate本文中に置く場合
```sh:case2.logrotate.conf(抜粋)
lastaction
        echo `date "+%Y/%m/%d-%H:%M:%S"` "done:lastaction in body" >> /test/log/lastaction.log
endscript

/test/log/wtmp1.log {
    monthly
    create 0664 root utmp
        minsize 1M
    rotate 1
}

/test/log/wtmp2.log {
    monthly
    create 0664 root utmp
        minsize 1M
    rotate 1
}
```

lastaction/endscriptが個別ログ条件の外にあるパターンです。

この場合、lastaction/endscriptの記述以降の記載された個別ログ条件の動作条件を満たした場合、
**それぞれの個別ログ条件ごとにlastaction/endscriptが動作**します。
動作条件を満たさない場合、`not running last action script`ということでlastaction/endscriptが動作しませんでした。

テストログファイルは以下の2つを準備しています。
`/test/log/wtmp1.log`
`/test/log/wtmp2.log`

<br>
###2-1.動作条件を満たす場合
lastaction/endscript：**動作する**

```sh:case2-1.debug
rotating pattern: /test/log/wtmp1.log  monthly (1 rotations)
empty log files are rotated, only log files >= 1048576 bytes are rotated, old logs are removed
considering log /test/log/wtmp1.log
  log needs rotating
rotating log /test/log/wtmp1.log, log->rotateCount is 1
dateext suffix '-20220123'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
glob finding old rotated logs failed
renaming /test/log/wtmp1.log to /test/log/wtmp1.log-20220123
creating new /test/log/wtmp1.log mode = 0664 uid = 0 gid = 22
running last action script
running script with arg /test/log/wtmp1.log : "
        echo `date "+%Y/%m/%d-%H:%M:%S"` "done:lastaction in body" >> /test/log/lastaction.log
"

rotating pattern: /test/log/wtmp2.log  monthly (1 rotations)
empty log files are rotated, only log files >= 1048576 bytes are rotated, old logs are removed
considering log /test/log/wtmp2.log
  log needs rotating
rotating log /test/log/wtmp2.log, log->rotateCount is 1
dateext suffix '-20220123'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
glob finding old rotated logs failed
renaming /test/log/wtmp2.log to /test/log/wtmp2.log-20220123
creating new /test/log/wtmp2.log mode = 0664 uid = 0 gid = 22
running last action script
running script with arg /test/log/wtmp2.log : "
        echo `date "+%Y/%m/%d-%H:%M:%S"` "done:lastaction in body" >> /test/log/lastaction.log
"
```

<br>
###2-2.動作条件を満たさない場合(期間未達)
億劫なので片方ずつ動作条件を変えています。
`/test/log/wtmp1.log`：容量未達
`/test/log/wtmp2.log`：期間未達

lastaction/endscript：**動作しない**
>動作条件を満たさない理由；
/test/log/wtmp1.log：'misinze' directive is used and the log size is smaller than the minsize value
/test/log/wtmp2.log：log has been already rotated

```sh:case2-2.debug
rotating pattern: /test/log/wtmp1.log  monthly (1 rotations)
empty log files are rotated, only log files >= 1048576 bytes are rotated, old logs are removed
considering log /test/log/wtmp1.log
  log does not need rotating ('misinze' directive is used and the log size is smaller than the minsize valuenot running last action script, since no logs will be rotated

rotating pattern: /test/log/wtmp2.log  monthly (1 rotations)
empty log files are rotated, only log files >= 1048576 bytes are rotated, old logs are removed
considering log /test/log/wtmp2.log
  log does not need rotating (log has been already rotated)not running last action script, since no logs will be rotated
```



<br>
#3.lorotate本文中と個別ログに置く場合
```sh:case3.logrotate.conf(抜粋)
lastaction
        echo `date "+%Y/%m/%d-%H:%M:%S"` "done:lastaction in body" >> /test/log/lastaction.log
endscript

/test/log/wtmp1.log {
    monthly
    create 0664 root utmp
        minsize 1M
    rotate 1
}

/test/log/wtmp2.log {
    monthly
    create 0664 root utmp
        minsize 1M
    rotate 1

lastaction
        echo `date "+%Y/%m/%d-%H:%M:%S"` "done:lastaction in /test/log/wtmp2.log" >> /test/log/lastaction.log
endscript
}
```

lastaction/endscriptが個別ログ条件の外と中の両方にあるパターンです。
(`/test/log/wtmp2.log`のみに適用)

この場合、個別ログローテーションの動作条件を満たした場合、
**個別ログ条件の中にあるlastaction/endscriptのみが動作**します。
動作条件を満たさない場合、`not running last action script`ということでlastaction/endscriptが動作しませんでした。

<br>
###3-1.動作条件を満たす場合
lastaction/endscript：**動作する**
`/test/log/wtmp1.log`：個別ログ条件の外のlastaction/endscriptが動作
`/test/log/wtmp2.log`：個別ログ条件の中のlastaction/endscriptのみ動作

```sh:case3-1.debug
rotating pattern: /test/log/wtmp1.log  monthly (1 rotations)
empty log files are rotated, only log files >= 1048576 bytes are rotated, old logs are removed
considering log /test/log/wtmp1.log
  log needs rotating
rotating log /test/log/wtmp1.log, log->rotateCount is 1
dateext suffix '-20220123'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
glob finding old rotated logs failed
renaming /test/log/wtmp1.log to /test/log/wtmp1.log-20220123
creating new /test/log/wtmp1.log mode = 0664 uid = 0 gid = 22
running last action script
running script with arg /test/log/wtmp1.log : "
        echo `date "+%Y/%m/%d-%H:%M:%S"` "done:lastaction in body" >> /test/log/lastaction.log
"

rotating pattern: /test/log/wtmp2.log  monthly (1 rotations)
empty log files are rotated, only log files >= 1048576 bytes are rotated, old logs are removed
considering log /test/log/wtmp2.log
  log needs rotating
rotating log /test/log/wtmp2.log, log->rotateCount is 1
dateext suffix '-20220123'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
glob finding old rotated logs failed
renaming /test/log/wtmp2.log to /test/log/wtmp2.log-20220123
creating new /test/log/wtmp2.log mode = 0664 uid = 0 gid = 22
running last action script
running script with arg /test/log/wtmp2.log : "
        echo `date "+%Y/%m/%d-%H:%M:%S"` "done:lastaction in body" >> /test/log/lastaction.log
"
```

<br>
#4.サマリ

1.個別ログローテーションの中に置く場合
→ローテート動作条件を満たす場合、lastaction/endscriptが動作します。

2.lorotate本文中に置く場合
→ローテート動作条件を満たす場合、以降の個別ログ条件についてlastaction/endscriptが動作します。

3.lorotate本文中と個別ログに置く場合
→ローテート動作条件を満たす場合、個別ログ条件内のlastaction/endscriptが動作します。本文中のlastaction/endscriptは動作しません。

#参考
https://qiita.com/Esfahan/items/a8058f1eb593170855a1

https://milestone-of-se.nesuke.com/sv-basic/linux-basic/logrotate/

https://isleofhoso.com/linux-logrotate/

https://hackers-high.com/linux/man-jp-logrotate/