5分おきにCPUとかメモリとかの状況をログに出すスクリプト

sysstatはバージョン8以降じゃないとpidstatがインストールされません。

yum install sysstat
vim /usr/local/serverlog/serverlog.sh
---
#!/bin/sh

LOG_HOME=/var/log/serverlog
DATE_PREFIX=`date "+%Y%m%d_%H"`
DATE=`date "+%Y%m%d"`

LOG_PS=$LOG_HOME/$DATE/PS_$DATE_PREFIX
LOG_FREE=$LOG_HOME/$DATE/FREE_$DATE_PREFIX
LOG_PIDSTAT=$LOG_HOME/$DATE/PIDSTAT_$DATE_PREFIX

if [ ! -d $LOG_HOME/$DATE ]; then
  mkdir -p $LOG_HOME/$DATE
fi

TIMESTAMP=`date "+%Y/%m/%d %T"`
(echo "[$TIMESTAMP]"; ps aux --sort -pcpu,-rss; echo "") >> $LOG_PS
(echo "[$TIMESTAMP]"; free; echo "") >> $LOG_FREE
(echo "[$TIMESTAMP]"; pidstat -d; echo "") >> $LOG_PIDSTAT

# delete logs
PRE_DATE=`date "+%Y%m%d" --date="7 days ago"`

# prevent firstserver
if [ "$LOG_HOME" = "" ]; then
  echo "LOG_HOME is missing"
  exit 1
fi

if [ -d $LOG_HOME/$PRE_DATE ]; then
  rm -rf $LOG_HOME/$PRE_DATE
fi
---

crontab -e
---
*/5 * * * * /usr/local/serverlog/serverlog.sh
---