Monitoring Log via Email

Sebagai seorang admin yang handal tentu harus memonitor log server secara rutin, hal ini tentu sangat merepotkan apabila tiap pagi mengharuskan membuka log file satu persatu. Nah disini saya coba membahas bagaimana bila log2 tersebut dibundle jadi satu kemudian dikirim lewat email, tentu sangat memudahkan bukan?? (malas mode : on ^_^).

Saya tertuju pada smtp email client yaitu sendEmail (bukan sendmail , tapi sendEmail !!), jadi disini tidak perlu menginstall Mail server seperti postfix,zimbra dll, tapi cukup menggunakan smtp client yakni sendEmail. Berikut Kutipan diambil dari website nya http://caspian.dotconf.net/menu/Software/SendEmail/

Quote: (silakan di translate sendiri)!!
"About SendEmail SendEmail is a lightweight, command line SMTP email client. If you have the need to send email from a command line, this free program is perfect: simple to use and feature rich. It was designed to be used in bash scripts, batch files, Perl programs and web sites, but is quite adaptable and will likely meet your requirements. SendEmail is written in Perl and is unique in that it requires NO MODULES. It has an intuitive and flexible set of command-line options, making it very easy to learn and use. [Supported Platforms: Linux, BSD, OS X, Windows 98, Windows NT, Windows 2000, and Windows XP]"

Ok kita langsung bahas langkah2 nya !!
Download sendEmail :
$ wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz

Kemudian Ekstrak :
$ tar xzvf sendEmail-v1.56.tar.gz

Copy ke /usr/local/bin/
$ cd sendEmail-v1.56
$ sudo cp sendEmail /usr/local/bin/

Ubah Permissionnya :
$ sudo chmod +x /usr/local/bin/sendEmail

Install paket aplikasi tambahan nya :
$ sudo apt-get install libio-socket-ssl-perl libnet-ssleay-perl perl

Disini sendEmail sudah terinstall, perintah untuk kirim email :
$ sendEmail -v -f username@mail.com -s smtp.googlemail.com:587 -xu username -xp yourpassword -t emailtujuan -o tls=yes -u subject email -m isi email -a attachment jika ada

Note : Email Yahoo maupun Gmail sudah mendukung protocol pop3 dan smtp asalkan opsi untuk protocol tersebut diaktifkan, maka mengirim email lewat smtp seperti diatas akan sukses.

Contoh test kirim email : 
$ sendEmail -v -f s****y@gmail.com -s smtp.googlemail.com:587 -u testingemail -xu s****y -xp ***** -t sonny@s*****k.com -m test email -o tls=yes 
Penjelasan : 
-f     alamat pengirim
-t     alamat tujuan
-u     message subject
-m    message body
-s    smtp mail relay, default is localhost:25
-a    file attachment(s)
-cc  cc email address(es)
-bcc bcc email address(es)
-xu   username for SMTP authentication
-xp   password for SMTP authentication
-o    optional
Jika masih kurang jelas silakan baca file README nya !!

Kemudian kita download paket logwatch 
$ sudo apt-get install logwatch

Backup dan ubah konfigurasi logwatch 
$ sudo cp -v /usr/share/logwatch/default.conf/logwatch.conf /usr/share/logwatch/default.conf/logwatch.conf_backup
$ sudo nano /usr/share/logwatch/default.conf/logwatch.conf 
tambahkan tanda # didepan untuk
----------------------------------------------------------------
#MailTo = root 
#MailFrom = Logwatch 
#mailer = "/usr/sbin/sendmail -t" 
Detail = Med          ### kecuali yang ini ubah dari Low ke Med ----------------------------------------------------------------
Save and exit 

Buat Script untuk Log dan kirim email 
$ nano logtoemail.sh 
----------------------------------------------------------------
#!/bin/bash usr/sbin/logwatch --output file --filename ~/logcomplete
/bin/cat /var/log/boot.log >> ~/logcomplete 
/bin/cat /var/log/backup.log >> ~/logcomplete ##> tambahkan lagi jika msh ada log yg ingin disertakan 
/usr/local/bin/sendEmail -f t***@gmail.com -s smtp.googlemail.com:587 -xu t*** -xp ***** -t sonny@s*****k.com -o tls=yes -u report -m report -a ~/logcomplete ----------------------------------------------------------------
Tambahkan Permission : 
$ sudo chmod +x logtoemail.sh 

Tambahkan di crontab : 
$ sudo crontab -e 
----------------------------------------------------------------
0 8 * * * /home/sonny/logtoemail.sh 
----------------------------------------------------------------
Sekarang, email log akan dikirim setiap hari pada jam 8 pagi. 

Semoga Bermanfaat   ^_^ !! 

*Diolah dari sumber :

No comments:

Post a Comment

Related Posts with Thumbnails