Wednesday, April 19, 2017

Konfigurasi SNMP Trapper di Zabbix

Dalam NMS (Network Monitoring System) metode untuk menerima SNMP Traps adalah metode yang terbalik dengan query SNMP-Enabled ke device tujuan. Dalam kasus SNMP Traps, informasi di SNMP-Enabled device dikirim dan dianalisa di server NMS (Dalam tutorial ini NMS yang dimaksud ialah Zabbix). Biasanya Traps dikirim ke port 162 di NMS.

Notes
  • Tutorial ini dengan asumsi bahwa zabbix sudah di install di tempat Anda. Untuk tutorial Zabbix bisa liat di link ini Installasi Zabbix di CentOS.
  • Selinux disabled.

Dibawah ini langkah-langkah untuk konfigurasinya.

1. Apabila CentOS Anda Mengaktifkan IPTables, Allow Port UDP 162
[root@localhost ~]# vi /etc/sysconfig/iptables:
###tambahkan line dibawah ini
-A INPUT -p udp -m udp --dport 162 -j ACCEPT

###Lalu restart service iptables
[root@localhost ~]# /etc/init.d/iptables restart

2. Setting Zabbix Agar Dapat Menerima SNMP Traps Menggunakan zabbix_trap_receiver.pl
[root@localhost ~]# yum install -y net-snmp-utils net-snmp-perl

Download zabbix_trap_receiver.pl di URL ini -> https://github.com/miraclelinux/MIRACLE-ZBX-2.0.3-NoSQL/blob/master/misc/snmptrap/zabbix_trap_receiver.pl

Isi dari script perl tersebut kurang lebih seperti dibawah ini.

[root@localhost ~]# cat /usr/bin/zabbix_trap_receiver.pl
#!/usr/bin/perl

#
# Zabbix
# Copyright (C) 2001-2014 Zabbix SIA
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#

#########################################
#### ABOUT ZABBIX SNMP TRAP RECEIVER ####
#########################################

# This is an embedded perl SNMP trapper receiver designed for sending data to the server.
# The receiver will pass the received SNMP traps to Zabbix server or proxy running on the
# same machine. Please configure the server/proxy accordingly.
#
# Read more about using embedded perl with Net-SNMP:
#       http://net-snmp.sourceforge.net/wiki/index.php/Tut:Extending_snmpd_using_perl

#################################################
#### ZABBIX SNMP TRAP RECEIVER CONFIGURATION ####
#################################################

### Option: SNMPTrapperFile
#       Temporary file used for passing data to the server (or proxy). Must be the same
#       as in the server (or proxy) configuration file.
#
# Mandatory: yes
# Default:
$SNMPTrapperFile = '/tmp/zabbix_traps.tmp';

### Option: DateTimeFormat
#       The date time format in strftime() format. Please make sure to have a corresponding
#       log time format for the SNMP trap items.
#
# Mandatory: yes
# Default:
$DateTimeFormat = '%H:%M:%S %Y/%m/%d';

###################################
#### ZABBIX SNMP TRAP RECEIVER ####
###################################

use Fcntl qw(O_WRONLY O_APPEND O_CREAT);
use POSIX qw(strftime);

sub zabbix_receiver
{
        my (%pdu_info) = %{$_[0]};
        my (@varbinds) = @{$_[1]};

        # open the output file
        unless (sysopen(OUTPUT_FILE, $SNMPTrapperFile, O_WRONLY|O_APPEND|O_CREAT, 0666))
        {
                print STDERR "Cannot open [$SNMPTrapperFile]: $!\n";
                return NETSNMPTRAPD_HANDLER_FAIL;
        }

        # get the host name
        my $hostname = $pdu_info{'receivedfrom'} || 'unknown';
        if ($hostname ne 'unknown') {
                $hostname =~ /\[(.*?)\].*/;                    # format: "UDP: [127.0.0.1]:41070->[127.0.0.1]"
                $hostname = $1 || 'unknown';
        }

        # print trap header
        #       timestamp must be placed at the beggining of the first line (can be omitted)
        #       the first line must include the header "ZBXTRAP [IP/DNS address] "
        #              * IP/DNS address is the used to find the corresponding SNMP trap items
        #              * this header will be cut during processing (will not appear in the item value)
        printf OUTPUT_FILE "%s ZBXTRAP %s\n", strftime($DateTimeFormat, localtime), $hostname;

        # print the PDU info
        print OUTPUT_FILE "PDU INFO:\n";
        foreach my $key(keys(%pdu_info))
        {
                printf OUTPUT_FILE "  %-30s %s\n", $key, $pdu_info{$key};
        }

        # print the variable bindings:
        print OUTPUT_FILE "VARBINDS:\n";
        foreach my $x (@varbinds)
        {
                printf OUTPUT_FILE "  %-30s type=%-2d value=%s\n", $x->[0], $x->[2], $x->[1];
        }

        close (OUTPUT_FILE);

        return NETSNMPTRAPD_HANDLER_OK;
}

NetSNMP::TrapReceiver::register("all", \&zabbix_receiver) or
        die "failed to register Zabbix SNMP trap receiver\n";

print STDOUT "Loaded Zabbix SNMP trap receiver\n";

3. Copy Script Perl tersebut ke /usr/bin
[root@localhost ~]# cp zabbix_trap_receiver.pl /usr/bin
[root@localhost ~]# chmod +x /usr/bin/zabbix_trap_receiver.pl

4. Update snmptrad.conf
[root@localhost ~]# vi /etc/snmp/snmptrapd.conf
### tambahkan line dibawah ini
authCommunity execute public
perl do "/usr/bin/zabbix_trap_receiver.pl";

5. Enable Zabbix SNMP Trapper 
[root@localhost ~]# vi /etc/zabbix/zabbix_server.conf
### Enable SNMP Trap di Zabbix Server 
StartSNMPTrapper=1
### Konfigurasi SNMPTrapperFile harus sama dengan yang ada di zabbix_trap_receiver.pl file.
SNMPTrapperFile=/tmp/zabbix_traps.tmp 

Setelah selesai save & quit, lalu restart zabbix server services.
[root@localhost ~]# /etc/init.d/zabbix_server restart

Notes : 
  • Untuk cek SNMPTrapperFile di zabbix_trap_receiver.pl dapat gunakan command ini, pastikan outputnya sama dengan di zabbix_server.conf
    [root@localhost ~]# cat /usr/bin/zabbix_trap_receiver.pl | grep \$SNMPTrapperFile\ =
    $SNMPTrapperFile = '/tmp/zabbix_traps.tmp';
  • [root@localhost ~]# cat /usr/local/etc/zabbix_server.conf | grep SNMPTrapperFile

  • SNMPTrapperFile=/tmp/zabbix_traps.tmp

6. Setting snmptrad Agar Start on Reboot 
[root@localhost ~]# chkconfig snmptrapd on
[root@localhost ~]# /etc/init.d/snmptrapd restart

7. Enable SNMP Trap Transmission File Rotation
[root@localhost ~]# mkdir -p /var/log/zabbix_traps_archive
[root@localhost ~]# chmod 777 /var/log/zabbix_traps_archive

Lalu tambahkan script dibawah ini untuk rotasi log di file /etc/logrotate.d/zabbix_traps.
/tmp/zabbix_traps.tmp {
    weekly
    size 10M
    compress
    compresscmd /usr/bin/bzip2
    compressoptions -9
    notifempty
    dateext
    dateformat -%Y%m%d
    missingok
    olddir /var/log/zabbix_traps_archive
    maxage 365
    rotate 10
}

8. Testing Send Traps
Langkah terakhir ialah send traps ke NMS atau localhost untuk memastikan bahwa NMS Zabbix yang kita configure dapat menerima traps dari device lain.

Gunakan Command Dibawah ini Untuk Sendtraps:
[root@localhost ~]# snmptrap -v 1 -c public 127.0.0.1 '.1.3.6.1.6.3.1.1.5.4' '0.0.0.0' 6 33 '55' \ .1.3.6.1.6.3.1.1.5.4 s "eth0"

Lalu cek di file /tmp/zabbix_trapper.tmp
[root@localhost ~]# cat /tmp/zabbix_traps.tmp
PDU INFO:
  notificationtype               TRAP
  version                        0
  receivedfrom                   UDP: [127.0.0.1]:33161->[127.0.0.1]
  errorstatus                    0
  messageid                      0
  community                      public
  transactionid                  1
  errorindex                     0
  requestid                      0
VARBINDS:
  iso.3.6.1.2.1.1.3.0            type=67 value=Timeticks: (55) 0:00:00.55
  iso.3.6.1.6.3.1.1.4.1.0        type=6  value=OID: iso.3.6.1.6.3.1.1.5.4.0.33
  iso.3.6.1.6.3.1.1.5.4          type=4  value=STRING: "eth0"
  iso.3.6.1.6.3.18.1.4.0         type=4  value=STRING: "public"
  iso.3.6.1.6.3.1.1.4.3.0        type=6  value=OID: iso.3.6.1.6.3.1.1.5.4


Apabila output diatas sudah sesuai berarti konfigurasi SNMP Trapper sudah berhasil. Untuk contoh SNMP Trap di device Cisco yang dikirim ke NMS Zabbix bisa liat tutorial ini Konfigurasi IPSLA di Router Cisco dan Send SNMP Trap ke Zabbix NMS.

Done...............

Konfigurasi Web Proxy di CentOS 6

Ada saat dimana OS CentOS yang kita install ada dibelakang proxy dan harus menggunakan proxy untuk mengakses internet baik itu browsing maupun instalasi package yang repository nya ada di Internet. 

Nah agar kita dapat koneksi internet maka kita harus menggunakan proxy tersebut. Dibawah ini merupakan langkah-langkahnya.

1. Konfigurasi di /etc/environment
[root@localhost ~]# vi /etc/environment 
http_proxy="http://proxysrv:8080/"
https_proxy="https://proxysrv:8080/"
ftp_proxy="ftp://proxysrv:8080/"
no_proxy=".mylan.local,.domain1.com,host1,host2"

2. Apabila Ingin Apply Setting Tanpa Restart Machine, Execute Command dibawah ini.
[root@localhost ~]# export http_proxy="http://proxysrv:8080/"
[root@localhost ~]# export https_proxy="https://proxysrv:8080/"
[root@localhost ~]# export ftp_proxy="ftp://proxysrv:8080/"
[root@localhost ~]# export no_proxy=".mylan.local,.domain1.com,host1,host2"

3. Apabila Anda Perlu Internet Untuk Instalasi Package Melalui Yum
[root@localhost ~]# vi /etc/yum.conf
proxy=http://proxysrv:8080/

Done...

Install Fping dan Menggunakan Fping di CentOS 6

Fping adalah sebuah program seperti ping yang didevelop untuk mengirimkan ICMP protocol ke target host, perbedaan utama dengan ping biasa ialah fping dapat mengirimkan ICMP protocol ke list target IP Address secara bersamaan dalam satu intruksi/command.

Contoh :






Notes
  • Beberapa aplikasi membutuhkan fping untuk dapat menjalankan sebuah function mereka, kenapa saya buat catatan ini karna salah satunya NMS Zabbix membutuhkan Fping untuk menjalankan simple check method ping mereka.
  • Sebelum installasi dan mengikuti tutorial ini, pastikan koneksi internet Anda normal karna package fping yang saya ambil ada di internet. (Langkah nomor 1 wget ....)


Langkah-langkah instalasi Fping ialah sebagai berikut ini.

1. Download Fping package.
[root@localhost ~]# wget http://fping.org/dist/fping-3.10.tar.gz

2. Install Fping dengan Command dibawah ini.
[root@localhost ~]# tar -xvf fping-3.10.tar.gz
[root@localhost ~]# cd fping-3.10
[root@localhost ~]# ./configure
[root@localhost ~]# make
[root@localhost ~]# make install

3. Apabila Anda Ingin Fping Support IPv6 Compile dengan Command dibawah ini.
[root@localhost ~]# ./configure --prefix=/usr/local --enable-ipv4 --enable-ipv6
[root@localhost ~]# make
[root@localhost ~]# make install

4. Contoh Penggunaan Fping (Multiple IP Address)
[root@localhost ~]# fping 8.8.8.8 8.8.4.4
8.8.8.8 is alive
8.8.4.4 is alive

5. Contoh Penggunaan Fping (Range IP Address)
[root@localhost ~]# fping -s -g 192.168.1.111 192.168.1.116
192.168.1.111 is alive
192.168.1.112 is alive
192.168.1.114 is alive
192.168.1.115 is alive
ICMP Host Unreachable from 192.168.1.102 for ICMP Echo sent to 192.168.1.113
ICMP Host Unreachable from 192.168.1.102 for ICMP Echo sent to 192.168.1.113
ICMP Host Unreachable from 192.168.1.102 for ICMP Echo sent to 192.168.1.113
ICMP Host Unreachable from 192.168.1.102 for ICMP Echo sent to 192.168.1.116
ICMP Host Unreachable from 192.168.1.102 for ICMP Echo sent to 192.168.1.116
ICMP Host Unreachable from 192.168.1.102 for ICMP Echo sent to 192.168.1.116
192.168.1.113 is unreachable
192.168.1.116 is unreachable

       6 targets
       4 alive
       2 unreachable
       0 unknown addresses

       2 timeouts (waiting for response)
      12 ICMP Echos sent
       4 ICMP Echo Replies received
       6 other ICMP received

 32.9 ms (min round trip time)
 42.2 ms (avg round trip time)
 55.7 ms (max round trip time)
 4.273 sec (elapsed real time)

Done...

Konfigurasi IP SLA di Router Cisco dan Send SNMP Trap ke NMS

Objective dari konfigurasi ini ialah konfigurasi IP SLA dari Router Cisco ke sebuah IP Address dengan teknik IP SLA Tracking dan statusnya tersebut dikirim ke NMS (Network Monitoring System).

Topologi yang digunakan ialah seperti dibawah ini.


















Keterangan Masing2 Nodes :


  • R1 : 192.168.1.111 (Ada Konfigurasi IP SLA ke IP R3 192.168.1.113)
  • R2 : 192.168.1.112 (Ada Konfigurasi IP SLA ke IP R3 192.168.1.113)
  • NMS : 192.168.1.102 (NMS Zabbix)


Notes :
  • Konfigurasi R1 pada dasarnya sama dengan Konfigurasi di R2, namun yang membedakan hanya IP Address saja.


Konfigurasi IP SLA di R1 ialah sebagai berikut ini.
ip sla monitor logging traps
ip sla monitor 1
 type echo protocol ipIcmpEcho 192.168.1.113
 timeout 2000
 frequency 10
ip sla monitor reaction-configuration 1 react timeout threshold-type immediate action-type trapOnly
ip sla monitor schedule 1 life forever start-time now

Konfigurasi SNMP Trap di R1 ialah sebagai berikut ini.
snmp-server community public RW
snmp-server enable traps rtr
snmp-server enable traps syslog
snmp-server host 192.168.1.102 version 2c RW
snmp-server host 192.168.1.102 version 2c public syslog

Cek IP SLA Statistik

R1#sh ip sla monitor statistics
Round trip time (RTT)   Index 1
        Latest RTT: NoConnection/Busy/Timeout
Latest operation start time: *00:00:17.327 UTC Fri Mar 1 2002
Latest operation return code: Timeout
Number of successes: 0
Number of failures: 2
Operation time to live: Forever

Contoh Trap yang Diterima NMS dari R1 (/tmp/zabbix_traps.tmp)

1. Ketika Interface R3 UP (No Shutdown)

22:32:27 2017/04/18 ZBXTRAP 192.168.1.111
PDU INFO:
  notificationtype               TRAP
  version                        1
  receivedfrom                   UDP: [192.168.1.111]:49542->[192.168.1.102]
  errorstatus                    0
  messageid                      0
  community                      public
  transactionid                  24
  errorindex                     0
  requestid                      16
VARBINDS:
  iso.3.6.1.2.1.1.3.0            type=67 value=Timeticks: (497724) 1:22:57.24
  iso.3.6.1.6.3.1.1.4.1.0        type=6  value=OID: iso.3.6.1.4.1.9.9.42.2.0.2
  iso.3.6.1.4.1.9.9.42.1.2.1.1.3.1 type=4  value=""
  iso.3.6.1.4.1.9.9.42.1.4.1.1.5.1 type=4  value=Hex-STRING: C0 A8 01 71
  iso.3.6.1.4.1.9.9.42.1.2.9.1.6.1 type=2  value=INTEGER: 2

2. Ketika R3 Down (Shutdown)

22:33:27 2017/04/18 ZBXTRAP 192.168.1.111
PDU INFO:
  notificationtype               TRAP
  version                        1
  receivedfrom                   UDP: [192.168.1.111]:49542->[192.168.1.102]
  errorstatus                    0
  messageid                      0
  community                      public
  transactionid                  24
  errorindex                     0
  requestid                      16
VARBINDS:
  iso.3.6.1.2.1.1.3.0            type=67 value=Timeticks: (497724) 1:22:57.24
  iso.3.6.1.6.3.1.1.4.1.0        type=6  value=OID: iso.3.6.1.4.1.9.9.42.2.0.2
  iso.3.6.1.4.1.9.9.42.1.2.1.1.3.1 type=4  value=""
  iso.3.6.1.4.1.9.9.42.1.4.1.1.5.1 type=4  value=Hex-STRING: C0 A8 01 71
  iso.3.6.1.4.1.9.9.42.1.2.9.1.6.1 type=2  value=INTEGER: 1


Done.....
Di catetan selanjutnya akan bahas cara konfigurasi SNMP Traps di Zabbix.
......