linux network monitoring hands-on pratice

107
網路監控 實作 by netman<[email protected]>

Upload: kenny-chen

Post on 13-Jul-2015

1.060 views

Category:

Internet


1 download

TRANSCRIPT

Page 1: Linux network monitoring hands-on pratice

網路監控實作

by netman<[email protected]>

Page 2: Linux network monitoring hands-on pratice

主題大綱● Tcpdump● Wireshark● Ntop● Mrtg● Cacti● Nagios● OpenNMS

Page 3: Linux network monitoring hands-on pratice

tcpdump

Page 4: Linux network monitoring hands-on pratice

tcpdump

● http://www.tcpdump.org/● CLI 模式的封包截取工具● 使用 libpcap 作為封包處理函式庫● 大多數 Unix-based 作業系統均內建

Page 5: Linux network monitoring hands-on pratice

截取參數-D

顯示所有可截取的網卡

-i [interface]

選擇網卡

any 表示所有的網卡

-p

不將網卡切入 promiscuous mode

-F [file]

由指定的檔案讀取 Filter Expression

-s

限制封包長度 ( 預設為 68 bytes)

0 表示不限制,錄下完整封包

Page 6: Linux network monitoring hands-on pratice

儲存參數-c [封包數 ]

錄下指令數量的封包後停止

-C [檔案大小 ]

限制記錄檔案之大小 ( 搭配 -w 指定檔名 )

達到上限時將開新檔繼續儲存

單位為 1,000,000 bytes

-W [檔案數 ]

限制 -C 參數儲存之檔案數量

Page 7: Linux network monitoring hands-on pratice

檔案參數-w [file]

將截取封包寫入至檔案

-U

搭配 -w 寫入檔案時,不作緩存

-r [file]

由指定檔案取得封包

Page 8: Linux network monitoring hands-on pratice

顯視參數-A

以 ASCII 字元顯示封包內容

-e

顯示 Link Layer 資訊 (例如MAC-Address)

-n

不作名稱解析 ( …主機名稱、服務名稱 etc)

-v

顯示較詳細的封包資訊

-v 、 -vv 、 -vvv 三種等級

-x / -xx

將封包內容以 hex 格式顯示

-X / -XX

將封包內容以 hex 及 ASCII 格式顯示

Page 9: Linux network monitoring hands-on pratice

時間參數-t

不顯示時間戳記

-tt

顯示 UNIX timestamp 格式

-ttt

顯示每個封包之間隔 (micro seconds)

-tttt

顯示標準格式 (yyyy-mm-dd hh:mm:ss.ms)

Page 10: Linux network monitoring hands-on pratice

協定參數Layer 2

arp

rarp

stp

vlan

Layer 3

ip

ip6

Layer 4

tcp

udp

icmp

Page 11: Linux network monitoring hands-on pratice

目標參數src/dst

ether

host

net

port

Page 12: Linux network monitoring hands-on pratice

封包參數broadcast

multicast

Page 13: Linux network monitoring hands-on pratice

數值運算= : equal

< : less

> : greater

Page 14: Linux network monitoring hands-on pratice

羅輯運算! : not

&& : and

|| : or

Page 15: Linux network monitoring hands-on pratice

範例● 觀察 eth0網頁連線

tcpdump -i eth0 tcp port 80● 觀察 eth1 arp 封包

tcpdump -i eth1 arp● 觀察對 168.95.1.1 之 DNS 查詢

tcpdump host 168.95.1.1 and udp port 53● 錄下 POP3封包

tcpdump -w pop3.pcap tcp port 110● 觀察 TCP SYN 封包

tcpdump ‘tcp[tcpflags] == 2’

Page 16: Linux network monitoring hands-on pratice

練習 1● 請問如下是什麼指令引起的封包?● 從哪個來源發起至哪個目的地?

Page 17: Linux network monitoring hands-on pratice

練習 2● 請從下載的封包檔中提取管理員 (root)的密碼

Page 18: Linux network monitoring hands-on pratice

wireshark

Page 19: Linux network monitoring hands-on pratice

Wireshark

● http://www.wireshark.org/● 早期名稱為 ethereal

● GUI 界面的封包截取工具● 可作為 sniffer 截取封包來分析● 亦可重組封包取出資料區

Page 20: Linux network monitoring hands-on pratice

下載

http://www.wireshark.org/download.html

Page 21: Linux network monitoring hands-on pratice

安裝

yum -y install wireshark wireshark-gnome

Page 22: Linux network monitoring hands-on pratice

Sample Captures

http://wiki.wireshark.org/SampleCaptures

Page 23: Linux network monitoring hands-on pratice

執行

Page 24: Linux network monitoring hands-on pratice

配置

Page 25: Linux network monitoring hands-on pratice

截取選項

Page 26: Linux network monitoring hands-on pratice

截取

Page 27: Linux network monitoring hands-on pratice

分析● Follow Stream

Page 28: Linux network monitoring hands-on pratice

IP來源分析● MaxMind GeoIP Database

http://geolite.maxmind.com/download/geoip/database/

● How To Use GeoIP With Wireshark

http://wiki.wireshark.org/HowToUseGeoIP

Page 29: Linux network monitoring hands-on pratice

IP來源分析● 起用 GeoIP

Page 30: Linux network monitoring hands-on pratice
Page 31: Linux network monitoring hands-on pratice

統計● 總覽

Page 32: Linux network monitoring hands-on pratice

統計● 協定層級

Page 33: Linux network monitoring hands-on pratice

統計● 對談

Page 34: Linux network monitoring hands-on pratice

另存為 ...

Page 35: Linux network monitoring hands-on pratice

參考資源● User’s Guide

http://www.wireshark.org/docs/wsug_html_chunked/

● Display Filter Referencehttp://www.wireshark.org/docs/dfref/

● Wikihttp://wiki.wireshark.org/

● Manualhttp://www.wireshark.org/docs/man-pages/

● FAQhttp://www.wireshark.org/faq.html

Page 36: Linux network monitoring hands-on pratice

ntop

Page 37: Linux network monitoring hands-on pratice

ntop● http://www.ntop.org/● 即時的流量分析 /統計工具● 功能

– Mirror Traffic

– NetFlow/sFlow

– In-Line Analysis

Page 38: Linux network monitoring hands-on pratice

下載http://www.ntop.org/download.html

Page 39: Linux network monitoring hands-on pratice

安裝

wget http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.2-2.el5.rf.i386.rpm

rpm -ivh rpmforge-release-0.5.2-2.el5.rf.i386.rpm

yum install ntop

Page 40: Linux network monitoring hands-on pratice

設定● 編輯 /etc/ntop.conf

– --http-server=3000● 可變更 http server port

– --interface=eth0● 可變更聆聽之網路界面

– --local-subnets=xx.xx.xx.xx/yy● 定義本地端網段

– 以及其他開放項目都要增加 =

Page 41: Linux network monitoring hands-on pratice

設定● 編輯 /etc/init.d/ntop (CentOS Only)

– 把這行daemon $prog -d -L @/etc/ntop.conf

– 修改為:daemon $prog @/etc/ntop.conf -d -L

Page 42: Linux network monitoring hands-on pratice

啟動● ntop -A

– 輸入登入密碼– Ctrl+C 停止

● service ntop start● chkconfig ntop on

Page 43: Linux network monitoring hands-on pratice

登入http://[IP]:3000/

Page 44: Linux network monitoring hands-on pratice

參考資源● Ntop Blog

http://www.ntop.org/blog/

● Ntop Overview

http://www.ntop.org/ntop-overview.pdf

● Ntop Presentation

http://www.ntop.org/OpenSourceConf_Athens2008.pdf

● Manpage

http://www.ntop.org/ntop-man.html

Page 45: Linux network monitoring hands-on pratice

mrtg

Page 46: Linux network monitoring hands-on pratice

mrtg● http://www.mrtg.org/● Multi Router Traffic Grapher● 利用 SNMP、或客製指令抓取資料,並產生趨勢圖表

● 可產生日 /週 /月 / 年下之平均數據● 常用來紀錄

– 網路界面流量– 主機負載 (CPU/Memory/ … etc)

– 磁碟使用率 In-Line Analysis

Page 47: Linux network monitoring hands-on pratice

安裝

yum install mrtg net-snmp net-snmp-utils

Page 48: Linux network monitoring hands-on pratice

設定● 修改 /etc/snmp/snmpd.conf● 在如下句子下修改:

# YOU SHOULD CHANGE THE "COMMUNITY" TOKEN BELOW TO A NEW KEYWORD ONLY

● 主要修改:com2sec local localhost netadmin

com2sec mynetwork 10.10.10.0/24 netadmin

Page 49: Linux network monitoring hands-on pratice

設定● 取消下行註解

group MyRWGroup any local

group MyROGroup any mynetwork

view all included .1 80

access MyROGroup "" any noauth 0 all none none

access MyRWGroup "" any noauth 0 all all all

Page 50: Linux network monitoring hands-on pratice

設定● 重新運行 snmpd

service snmpd restart

chkconfig snmpd on● 并以 snmpwalk 進行測試

snmpwalk localhost -c netadmin -v 1

Page 51: Linux network monitoring hands-on pratice

設定● 產生mrtg設定檔

cd /etc/mrtg

cfgmaker netadmin@[IP] >> mrtg.cfg● 執行

LANG=C mrtg /etc/mrtg/mrtg.cfg● 執行三次

● 產生mrtg首頁indexmaker --columns=1 \

--output=/var/www/mrtg/index.html \

/etc/mrtg/mrtg.cfg

Page 52: Linux network monitoring hands-on pratice

觀察結果● 起動 apache:

service httpd restartchkconfig httpd on

● 用瀏覽器察看:

http://[ip]/mrtg

● 或需修改:

/etc/httpd/conf.d/mrtg.conf

Page 53: Linux network monitoring hands-on pratice

cacti

Page 54: Linux network monitoring hands-on pratice

cacti● http://www.cacti.net/● 利用 php + mysql + rrdtool 畫出各種圖表的網管軟體

● 常用來取代傳統的 mrtg

● 可為各常用設備定義 template,套用方便

Page 55: Linux network monitoring hands-on pratice

需求● php

– 執行網頁界面● mysql

– 存放網頁界面資料庫● rrdtool

– 處理 rrd 資料● net-snmp

– 抓取 snmp 資料

Page 56: Linux network monitoring hands-on pratice

下載http://www.cacti.net/download_cacti.php

Page 57: Linux network monitoring hands-on pratice

安裝

yum -y install cacti mysql-server

Page 58: Linux network monitoring hands-on pratice

資料庫處理● 啟動 MySQL

– service mysqld start– chkconfig mysqld on

● 建立 cacti 資料庫、帳號– mysql

● mysql> create database cacti;● mysql> grant all on cacti.* to cacti@localhost

identified by 'password';● mysql> quit

● 匯入資料庫– mysql cacti -u cacti -p <

/var/www/cacti/cacti.sql

Page 59: Linux network monitoring hands-on pratice

● 編輯設定檔– vim /var/www/cacti/include/config.php

● $database_type = "mysql";● $database_default = "cacti";● $database_hostname = "localhost";● $database_username = "cacti";● $database_password = "password";● $database_port = "3306";● $url_path = “/cacti/”;

● 重新起點 apache

service apache restart

Page 60: Linux network monitoring hands-on pratice

進入安裝界面http://[IP]/cacti/install/

Page 61: Linux network monitoring hands-on pratice

選擇 New Install

Page 62: Linux network monitoring hands-on pratice

選擇 Finish

Page 63: Linux network monitoring hands-on pratice

登入● http://[IP]/cacti/● 預設帳密為 admin/admin,登入後需改密碼

Page 64: Linux network monitoring hands-on pratice

新增步驟● 新增主機

– Devices → Add● 填入 Description、 Hostname● 選擇 Host Template● 設定 SNMP Options(community)● Create

Page 65: Linux network monitoring hands-on pratice

新增主機

Page 66: Linux network monitoring hands-on pratice

新增步驟● 建立圖表

– Devices → → 點進該設備 Create Graphs for this Host

● 勾選欲建立之圖表後● Create

Page 67: Linux network monitoring hands-on pratice

建立圖表建立圖表

Page 68: Linux network monitoring hands-on pratice

新增步驟● 放置圖表

– Devices → 勾選選擇該設備(最右邊)● 下拉選單:

– Place on a Tree● Go & Continue

Page 69: Linux network monitoring hands-on pratice

放置圖表

Page 70: Linux network monitoring hands-on pratice

● 管理圖表– Graph Trees → Add

● 可建出各類設備之樹狀結構

– Graph Management● 勾選欲置放之圖表● Choose an action: Place on a Tree

– Destination Branch (如有子節點 )– Yes

Page 71: Linux network monitoring hands-on pratice

● 觀看圖表– 切換至 Graphs 標簽

● 展開 Graph Tree● 選擇主機

Page 72: Linux network monitoring hands-on pratice

觀看圖表

Page 73: Linux network monitoring hands-on pratice

取得 Host Templatehttp://forums.cacti.net/forum-12.html

Page 74: Linux network monitoring hands-on pratice

http://www.debianhelp.co.uk/cactitemplates.htm

Page 75: Linux network monitoring hands-on pratice

匯入 Template

Page 76: Linux network monitoring hands-on pratice

poller● cmd.php

– 以 php 開發– cacti 預設的 poller script

– 偵測主機多時效率不佳

● spine (cactid) – 使用 C 語言開發– 以multi-process + multi-thread執行,效率較好

Page 77: Linux network monitoring hands-on pratice

spine (cactid)http://www.cacti.net/spine_download.php

Page 78: Linux network monitoring hands-on pratice

安裝 spine ● yum install cacti-spine

Page 79: Linux network monitoring hands-on pratice

設定 spine● 編輯設定檔

– vim /etc/spine.conf● DB_Host localhost● DB_Database cacti● DB_User cacti● DB_Pass password● DB_Port 3306

Page 80: Linux network monitoring hands-on pratice

使用 spine 設定 spine 路徑 (settings → Path: /usr/bin/spine)

Page 81: Linux network monitoring hands-on pratice

更改 Poller Type

Page 82: Linux network monitoring hands-on pratice

opennms

Page 83: Linux network monitoring hands-on pratice

opennms● http://www.opennms.org/● Open Source / Free 的監控工具● 網路狀態監控工具● 可發出即時 Alert

● 常用於觀察● 主機服務● 網路節點狀態

Page 84: Linux network monitoring hands-on pratice

安裝yum install opennms opennms-plugins opennms-plugins-nrpe

* or 參考:http://eggchang.pixnet.net/blog/post/32659238

Page 85: Linux network monitoring hands-on pratice

安裝與設定 PostgreSQL● 安裝 postgresql

yum -y install postgresql postgresql-server postgresql-jdbc

● 讓 postgresql 於開機時自動執行

chkconfig --level 345 postgresql on● 第一次啟動 postgresql

service postgresql start

Page 86: Linux network monitoring hands-on pratice

● 修改 postgresql 啟動參數

cd /var/lib/pgsql/data

mv pg_hba.conf pg_hba.conf.bak

sed 's/ident sameuser/trust/g' pg_hba.conf.bak > pg_hba.conf

● 重新啟動 postgresql

service postgresql restart

Page 87: Linux network monitoring hands-on pratice

安裝與設定 JDK● 下載 jdk

wget http://download.oracle.com/otn-pub/java/jdk/6u22-b04/jdk-6u22-linux-i586-rpm.bin

● 安裝 JDK

bash jdk-6u22-linux-i586-rpm.bin

Page 88: Linux network monitoring hands-on pratice

● 設定環境變數cat << EOF > /etc/profile.d/java.sh

export JAVA_HOME=/usr/java/default

export PATH=\$JAVA_HOME/bin:\$PATH

EOF

cat << EOF > /etc/profile.d/java.csh

set JAVA_HOME=/usr/java/default

set PATH=\$JAVA_HOME/bin:\$PATH

EOF

source /etc/profile.d/java.sh

Page 89: Linux network monitoring hands-on pratice

下載與安裝 OpenNMS● 安裝 OpenNMS 的 yum repositories

rpm -ivh http://yum.opennms.org/repofiles/opennms-repo-stable-rhel5.noarch.rpm

● 建立與設定資料庫

sudo -u postgres createdb -U postgres -E UNICODE opennms

yum -y install iplike

Page 90: Linux network monitoring hands-on pratice

● 安裝 OpenNMS

yum -y install opennms mib2opennms opennms-*

● 設定 OpenNMS

/opt/opennms/bin/runjava -s

/opt/opennms/bin/install -dis

chkconfig opennms on● 啟動 OpenNMS

service opennms start

Page 91: Linux network monitoring hands-on pratice

http://[IP]:8980/opennms/

Page 92: Linux network monitoring hands-on pratice

參考資源● OpenNMS Documentation

– http://www.opennms.org/documentation● Official OpenNMS Documentation

– http://support.opennms.com/knowledgebase/officialdocs

● OpenNMS Wiki– http://wiki.opennms.org/index.php/Main_Page

● Installation:Yum– http://www.opennms.org/wiki/Installation:Yum

● OpenNMS Documentation Overview– http://www.opennms.org/wiki/Docu-overview

● OpenNMS FAQ– http://www.opennms.org/wiki/FAQ

Page 93: Linux network monitoring hands-on pratice

nagios

Page 94: Linux network monitoring hands-on pratice

Nagios

● http://www.nagios.org/● Open Source / Free 的監控工具● 網路狀態監控工具● 可發出即時 Alert

● 常用於觀察– 主機服務– 網路節點狀態

Page 95: Linux network monitoring hands-on pratice

安裝● YUM

yum install nagios*

Page 96: Linux network monitoring hands-on pratice

系統設定● 建立 httpd帳號

htpasswd -c /etc/nagios/htpasswd.users nagiosadmin

htpasswd /etc/nagios/htpasswd.users nagiosuser

● 啟動服務

service httpd restart

service nagios start

chkconfig httpd on

chkconfig nagios on

Page 97: Linux network monitoring hands-on pratice

測試連線● 檢查設定檔語法

nagios -v /etc/nagios/nagios.cfg

● http://127.0.0.1/nagios/– 以 nagiosadmin 登入

Page 98: Linux network monitoring hands-on pratice

設定● 修改管理員信箱

vi /etc/nagios/objects/contacts.cfg

define contact{email user@domain ;

Page 99: Linux network monitoring hands-on pratice

設定● 設定 nrpe

echo 'nrpe 5666/tcp' >> /etc/services

serivce nrpe start

chkconfig nrpe on

Page 100: Linux network monitoring hands-on pratice

設定● 測試 nrpe

/usr/lib/nagios/plugins/check_nrpe -H localhost -c check_users

/usr/lib/nagios/plugins/check_nrpe -H localhost -c check_load

/usr/lib/nagios/plugins/check_nrpe -H localhost -c check_total_procs

/usr/lib/nagios/plugins/check_nrpe -H localhost -c check_zombie_procs

Page 101: Linux network monitoring hands-on pratice

設定● 測試硬碟空間

vi /etc/nagios/nrpe.cfg

allowed_hosts=127.0.0.1,ip.ip.ip.ipcommand[check_root_space]=/usr/lib/nagios/plugins/check_disk -w 20% -c 10% -p /dev/VG/root_LV

serive nrpe restart

/usr/lib/nagios/plugins/check_nrpe -H localhost -c check_root_space

Page 102: Linux network monitoring hands-on pratice

設定● 建立主機組態

cp /etc/nagios/objects/windows.cfg /etc/nagios/objects/linux.cfg

vi /etc/nagios/objects/linux.cfg● use linux-server● host_name linuxserver● Address 10.10.2.1 ● hostgroup_name linux-servers

Page 103: Linux network monitoring hands-on pratice

設定● vi /etc/nagios/objects/linux.cfg

– 把全部 host_name 從 winserver 修改 為 linuxserver (前面定義的)

– 刪除多餘的 service { … }define service{

use generic-service

host_name linuxserver

service_description Online Users

check_command check_nrpe!check_users

}

Page 104: Linux network monitoring hands-on pratice

設定● 定義 nrpe 命令

vi /etc/nagios/objects/commands.cfg# 'check_nrpe' command definition

define command{

command_name check_nrpe

command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$

}

Page 105: Linux network monitoring hands-on pratice

設定● 修改 /etc/nagios/nagios.cfg :

cfg_file=/etc/nagios/objects/linux.cfg● 檢查組態

nagios -v /etc/nagios/nagios.cfg● 重新執行服務

service nagios restart

Page 106: Linux network monitoring hands-on pratice

參考資源● OpenNagios

– http://software.opensuse.org/ymp/openSUSE:11.3/standard/nagios.ymp

● Nagios Plugins– http://software.opensuse.org/ymp/openS

USE:11.3/standard/nagios-plugins.ymp● Nagios Addons Extras

– http://software.opensuse.org/ymp/openSUSE:11.3/standard/nagios-plugins-extras.ymp

Page 107: Linux network monitoring hands-on pratice

Q & A