Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save spidersea/8110448 to your computer and use it in GitHub Desktop.
Save spidersea/8110448 to your computer and use it in GitHub Desktop.
该功能用户小型个人或公司网站,由于硬件资源有限不能实用数据库进行搭建syslog系统使用。
一、系统环境变量设置
在/etc/profile中添加:
USER_IP=`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g'`
TTY_TERMINAL=`who -u am i|awk '{print $2}'`
DATE=`date +"%Y%m%d"`
if [ "$USER_IP" = "" ]
then
USER_IP=`hostname`
fi
if [ ! -d /tmp/history ]
then
mkdir /tmp/history
chmod 777 /tmp/history
fi
if [ ! -d /tmp/history/${DATE}/${LOGNAME} ]
then
mkdir -p /tmp/history/${DATE}/${LOGNAME}
chmod 300 /tmp/history/${DATE}/${LOGNAME}
fi
export HISTSIZE=200000
DT=`date +"%Y%m%d_%H%M%S"`
export HISTFILE="/tmp/history/${DATE}/${LOGNAME}/${USER_IP}_history.$DT"
chmod 600 /tmp/history/${DATE}/${LOGNAME}/*history* 2>/dev/null
二、历史命令发送脚本
#!/bin/bash
DATE_LAST=`date +"%Y%m%d" -d "1 days ago"`
HISTORY_LOG_DIR="/tmp/history/${DATE_LAST}/"
HISTORY_LOG_LIST="`cd ${HISTORY_LOG_DIR};ls -lrt */*|grep history|awk '{print $8}'`"
for i in ${HISTORY_LOG_LIST}
do
echo "=========================================${HISTORY_LOG_DIR}${i}"
cat ${HISTORY_LOG_DIR}/${i}|while read line
do
if echo $line|grep -q "^#"
then
DATE_OLD_FORMAE=`echo $line | cut -c2-`
DATE_NEW_FORMEE=`date -d "@${DATE_OLD_FORMAE}"`
echo -n "${DATE_NEW_FORMEE} "
else
echo $line
fi
done
done |mail -s "${DATE_LAST}服务器历史操作命令记录邮件" [email protected]
三、设置crontab邮件发送
1 0 * * * cd /opt/script;sh history_log_mail.sh >/dev/null 2>&1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment