Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 謎のコマンド ostype は uname を使ってなんとかすべし。
- # Windows 環境下での uname の戻り値は環境差がきっと大きいです。
- # あと、PS1_GET_EVENTLOG_OPT も需要に応じて適当に変更するとよし。
- # Windows で nkf を使っている部分も環境に応じて調整するよろし!!
- SYSLOG=/var/log/messages
- # get application log output (*nix => syslog, windows => event log)
- # -f <logfile> specify log file name (*nix only)
- # -n <count> output the latest <count> lines
- # -m <word> output the lines which contains the specified (alphanumeric) word in message
- # more filters can be applied by external command such as grep, cut, etc.
- #
- # Requirements:
- # Windows: PowerShell 2.0 or later
- getaplog()
- {
- OPT_LOGPATH=$SYSLOG
- OPT_WORDINCL=""
- OPT_LINECOUNT=""
- OPT_ERROR=0
- OPTIND_OLD=$OPTIND
- OPTIND=1
- while getopts "f:m:n:" OPT; do
- case $OPT in
- f) OPT_LOGPATH="$OPTARG";;
- m) OPT_WORDINCL="$OPTARG";;
- n) OPT_LINECOUNT="$OPTARG";;
- ?) return 1;;
- esac
- done
- shift $(($OPTIND - 1))
- OPTIND=$OPTIND_OLD
- if [ `ostype` = "Windows" ]; then
- #PS1_GET_EVENTLOG_OPT="Application -Source XXX, YYY"
- PS1_GET_EVENTLOG_OPT="Application"
- if [ ! -z "$OPT_WORDINCL" ]; then
- PS1_GET_EVENTLOG_OPT="$PS1_GET_EVENTLOG_OPT -Message \"*$OPT_WORDINCL*\""
- fi
- if [ ! -z "$OPT_LINECOUNT" ]; then
- PS1_GET_EVENTLOG_OPT="$PS1_GET_EVENTLOG_OPT -Newest $OPT_LINECOUNT"
- fi
- yes "" | powershell -Command "\$OutputEncoding = [console]::OutputEncoding;" Get-EventLog $PS1_GET_EVENTLOG_OPT "| Sort-Object TimeGenerated | foreach { Write-Host (\$_.TimeGenerated.ToString('yyyy/MM/dd HH:mm:ss') + ' ' + \$_.MachineName + ' ' + \$_.Source + ': ' + \$_.Message) }" 2>/dev/null | nkf -s -w8
- else
- if [ ! -f "$OPT_LOGPATH" ]; then
- echo "getaplog: ${OPT_LOGPATH} file not exist" 2>&1
- return 1
- fi
- # lazy wildcard => regex conversion
- OPT_WORDINCL=`echo "$OPT_WORDINCL" | sed -e s/*/.*/g`
- if [ ! -z "$OPT_LINECOUNT" ]; then
- tail "-$OPT_LINECOUNT" $OPT_LOGPATH | grep "$OPT_WORDINCL"
- else
- grep "$OPT_WORDINCL" $OPT_LOGPATH
- fi
- fi
- return 0
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement