Php/ログ圧縮バッチ
提供: 初心者エンジニアの簡易メモ
crontab -e
5 0 * * * /usr/local/bin/php /home/linux/mnglog.php /home/linux/mnglog_conf.php
mnglog_conf.php
<?php $log_path_fmt_list = array(); $log_path_fmt_list[] = '/home/linux/test%s.log';
mnglog.php
<?php
// ログ管理スクリプト
// 圧縮日指定(本日より何日前
$comp_before_day = 1;
// 削除日指定(本日より何日前
$del_before_day = 1 * 365;
// 圧縮日offset指定(デフォルト0)(障害で処理されなかったことを考慮して指定日から3日前まで、
$comp_offset_day = 3;
// 削除日offset指定(デフォルト0)(障害で処理されなかったことを考慮して指定日から3日前まで、
$del_offset_day = 3;
// --------------------------------------------------------------------------------------------------
echo '=============================== START '.date('Y/m/d H:i:s')." ===============================\n";
// 設定ファイルの読み込み
if ($argc > 1) {
$conf_path = $argv[1];
} else {
echo "config file does not specified\n";
echo '=============================== END '.date('Y/m/d H:i:s')." ===============================\n";
exit;
}
echo "config file is {$conf_path}\n";
if (!file_exists($conf_path)) {
echo "config file does not exists\n";
echo '=============================== END '.date('Y/m/d H:i:s')." ===============================\n";
exit;
}
require_once($conf_path);
// ログ管理クラスロード
$m = new MnglogModel();
// 対象ログパスリストをセット
$m->setLogPathFmtList($log_path_fmt_list);
// 圧縮処理
$m->compExecByBeforeDay($comp_before_day, $comp_offset_day);
// 削除処理
$m->delExecByBeforeDay($del_before_day, $del_offset_day);
echo '=============================== END '.date('Y/m/d H:i:s')." ===============================\n";
exit;
/**
* ログ管理モデルクラス
*/
class MnglogModel
{
// 対象ログパスリスト
private $_log_path_fmt_list = array();
// 年月日フォーマットリスト
private $_ymd_formats = array(
'Ymd',
'Y-m-d'
);
const COMP_CMD_FMT = 'gzip %s';
const DEL_CMD_FMT = 'rm -fr %s';
const LS_CMD_FMT = 'ls %s 2>&1';
// 対象ログパスリスト
public function setLogPathFmtList($log_path_fmt_list)
{
// 対象ログパスリスト
$this->_log_path_fmt_list = $log_path_fmt_list;
}
/**
* 圧縮処理
*
* @param int $before_day 今日から何日前
* @param int $offset 前項目指定より何日前までを全て処理
* @return void
*/
public function compExecByBeforeDay($before_day, $offset = 0)
{
// 処理対象日付を計算
$now_ts = time();
// 圧縮日付取得
$target_ts = $now_ts - 3600 * 24 * ($before_day + $offset);
echo "compress target date is " . date('Ymd', $target_ts) . "\n";
// 対象ログパスリストを取得
$log_path_list = $this->_getTargetLoglist($target_ts);
foreach ($log_path_list as $log_path) {
// 圧縮
$this->_compCommandExec($log_path);
}
if ($offset > 0) {
// offsetを1減らして再帰処理
$f = __FUNCTION__;
$this->$f($before_day, $offset - 1);
}
}
/**
* 削除処理
*
* @param int $before_day 今日から何日前
* @return void
*/
public function delExecByBeforeDay($before_day, $offset = 0)
{
// 処理対象日付を計算
$now_ts = time();
// 削除日付取得
$target_ts = $now_ts - 3600 * 24 * ($before_day + $offset);
$target_date = date('Ymd', $target_ts);
echo "delete target date is " . date('Ymd', $target_ts) . "\n";
// 対象ログパスリストを取得
$log_path_list = $this->_getTargetLoglist($target_ts);
foreach ($log_path_list as $log_path) {
// 削除(.gz
$this->_delCommandExec($log_path . '.gz');
// 削除(圧縮し忘れログ
$this->_delCommandExec($log_path);
}
if ($offset > 0) {
// offsetを1減らして再帰処理
$f = __FUNCTION__;
$this->$f($before_day, $offset - 1);
}
}
/**
* 対象ログパスリストを取得
*/
private function _getTargetLoglist($target_ts)
{
$log_paths = array();
// 処理を行う
foreach ($this->_log_path_fmt_list as $log_path_fmt) {
// 年月日フォーマット分だけループ
foreach ($this->_ymd_formats as $ymd_format) {
$target_date = date($ymd_format, $target_ts);
$log_paths[] = sprintf($log_path_fmt, $target_date);
}
}
return $log_paths;
}
/**
* 圧縮コマンド処理
*
* @param string $log_path 圧縮ファイルパス
* @return void
*/
private function _compCommandExec($log_path)
{
// 対象ファイルが存在しない時
if ($this->_checkFileExists($log_path) === FALSE) return FALSE;
$cmd = sprintf(self::COMP_CMD_FMT, $log_path);
shell_exec($cmd);
echo $cmd."\n";
return TRUE;
}
/**
* 削除コマンド処理
*
* @param string $log_path 削除ファイルパス
* @return void
*/
private function _delCommandExec($log_path)
{
// 対象ファイルが存在しない時
if ($this->_checkFileExists($log_path) === FALSE) return FALSE;
$cmd = sprintf(self::DEL_CMD_FMT, $log_path);
shell_exec($cmd);
echo $cmd."\n";
return TRUE;
}
/**
* ファイル存在チェック
*/
private function _checkFileExists($path)
{
// 省略パターンを含む時
if (strpos($path, "*") !== FALSE) {
// lsでチェック
$cmd = sprintf(self::LS_CMD_FMT, $path);
exec($cmd, $arr, $res);
// ファイルが存在しない時
if ($res == 2) {
return FALSE;
}
// 省略パターンを含まない時
} else {
if (!file_exists($path)) {
return FALSE;
}
}
// ファイルが存在する時
return TRUE;
}
}
