OpenWrt log화일을 일정시간마다 이메일로 전송한다든지 또는 장애/보안 등의 알림을 위한 이메일 전송 도구가 필요해 가볍고 설정하기 쉬운 SMTP client인 msmtp를 설치해 보았습니다.
gmail 설정
SMTP 액세스를 허용하도록 gmail을 구성하는 방법은
- 2중 인증이 활성화하여 앱 비밀번호 를 생성해 비밀번호를 이용하는 방법
- API Console에서 gmail api 활성화 및 gmail용 OAuth2 인증을 통한 방법
gmail 계정을 추가로 만드는 것에 대한 제한도 없고 저는 이미 계정을 만들어 놓고 안쓰는 계정이 있어서 두가지 방법 중 첫번째 방법을 이용했습니다.
패키지 설치
opkg update
opkg install msmtp
sendmail 심볼릭링크 생성
대부분의 데몬은 메일하면 sendmail로 인식하므로 msmtp를 sendmail로 심볼릭링크를 걸어줍니다.
ln -s /usr/bin/msmtp /usr/sbin/sendmail
alias 설정
이메일이 분실되지 않도록 하기 위한 안전조치로 aliases 파일을 생성해 로컬 사용자(root, ftp, nobody 등)에게 보내는 모든 메일이 실제로 지정된 이메일 주소로 보내지도록 합니다. 아래는 aliases 화일의 예이며 로컬 사용자별로 따로 메일 계정이 필요 없으므로 default 만 설정합니다.
<alias 사용 예>
# root로 메일을 보낼 경우 root : [email protected] ... # 나머지는 모두 default에 설정한 메일로 보냅니다. default : [email protected]
echo "default : [email protected]" > /etc/aliases
msmtp 설정
gmail 계정 정보를 /etc/msmtprc 에 기입해 줍니다. gmail이 아닌 SMTP를 지원하는 다른 메일을 써도 좋습니다.
사용자명은 gmail 사용자 계정을 패스워드는 앱패스워드를 기입합니다. 앱패스워드는 16자리로 공백을 포함할 경우 ""로 묶어야 하며 공백없이 붙여써도 상관없습니다.
vi /etc/msmtprc
# Example for a system wide configuration file # A system wide configuration file is optional. # If it exists, it usually defines a default account. # This allows msmtp to be used like /usr/sbin/sendmail. defaults port 465 tls on tls_starttls off tls_trust_file /etc/ssl/certs/ca-certificates.crt from %U@%H aliases /etc/aliases syslog LOG_MAIL # Authenticate to GMail # # If your Gmail account is secured with two-factor authentication, you need # to generate a unique App Password to use in ssmtp.conf. # See https://support.google.com/mail/answer/185833 # App Passwords can be generated on https://myaccount.google.com/apppasswords # # Use you Gmail username (not the App Name) in the user line and use the # generated 16-character password in the password line, spaces in the password # can be omitted. account gmail host smtp.gmail.com auth on user [email protected] password apppasswords # other SMTP account account othermail host smtp.other.example auth on user usern[email protected] password yourpassword account default : gmail
메일보내기 테스트
echo -e "Subject: mail test \n\nhello" | sendmail root
echo -e "Subject: mail test \n\nhello" | sendmail [email protected]
echo -e "Subject: mail test \n\nhello" | sendmail -a othermail [email protected]
- 로컬 사용자인 root로 메일을 보내면 /etc/aliases에서 설정한 default : [email protected] 로 메일을 보내게 됩니다.
- account를 따로 지정하지 않으면 default로 지정한 gmail SMTP를 통해 이메일을 보내게 됩니다.
- -a 옵션을 사용하면 default gmail이 아닌 추가 설정한 다른 account를 지정할 수 있습니다.
응용
OpenWrt는 임베디드 기기의 특성상 저장장치의 용량제한으로 인해 log를 메모리에 저장할 뿐 기본설정은 화일로 기록하지 않습니다. 화일 저장을 위해서는 별도 usb저장장치 마운트 필요합니다. 별도 원격 로그서버에 따로 로그를 저장하면 좋지만 여의치 않을 경우를 고려해 일정시간 마다 이메일로 log를 보내는 간단한 쉘스크립트(logwatch.sh)를 작성해 보았습니다. 사용법은 diffutils 패키지 설치하고 적당한 시간 간격으로 crontab에 추가하면 됩니다. 단점은 일정시간마다 로그를 긁어서 이메일로 보낼뿐 실시간 로그를 기록하지 않아 효용가치는 별로 없습니다.)
#!/bin/sh # This script filters the logs and send it via mail # Installation on OpenWrt # - opkg update && opkg install diffutils # - add it to crontab OLD_LOG=/tmp/oldlog NEW_LOG=/tmp/newlog MAIL="root" if [ ! -e $OLD_LOG ]; then logread > $OLD_LOG fi logread > $NEW_LOG DIFF=$(diff $NEW_LOG $OLD_LOG) if [ -z "$DIFF" ]; then logger logwatch "No changes" else BODY=$(tail -n +$(diff $NEW_LOG $OLD_LOG | grep -v ^\< | cut -f1 -d, | tail -n 1) $NEW_LOG) echo -e "Subject: Log activity detected on $HOSTNAME\n\n$BODY" | sendmail $MAIL logger logwatch "Log sent to $MAIL" fi mv $NEW_LOG $OLD_LOG