文章目錄
一、Flume監控之Ganglia
1.1 前言
Ganglia
是UC Berkeley
發起的一個開源監視項目,設計用於測量數以千計的節點。每臺計算機都運行一個收集和發送度量數據(如處理器速度、內存使用量等)的名爲gmond
的守護進程。它將從操作系統和指定主機中收集。接收所有度量數據的主機可以顯示這些數據並且可以將這些數據的精簡表單傳遞到層次結構中。正因爲有這種層次結構模式,才使得Ganglia
可以實現良好的擴展。gmond
帶來的系統負載非常少,這使得它成爲在集羣中各臺計算機上運行的一段代碼,而不會影響用戶性能。
Ganglia
由gmond
、gmetad
和gweb
三部分組成。
gmond(Ganglia Monitoring Daemon):一種輕量級服務,安裝在每臺需要收集指標數據的節點主機上。使用gmond
,你可以很容易收集很多系統指標數據,如CPU
、內存、磁盤、網絡和活躍進程的數據等。
gmetad(Ganglia Meta Daemon):整合所有信息,並將其以RRD
格式存儲至磁盤的服務。
gweb(Ganglia Web):Ganglia
可視化工具,gweb
是一種利用瀏覽器顯示gmetad
所存儲數據的PHP
前端。在Web
界面中以圖表方式展現集羣的運行狀態下收集的多種不同指標數據
1.2 Ganglia的安裝與部署
- 安裝
httpd
服務與php
[root@hadoop101 ~]# yum -y install httpd php
- 默認源找不到安裝包,所以要安裝
epel
源
[root@hadoop101 ~]# yum -y install epel-release
- 安裝
ganglia-gmetad
、ganglia-web
、ganglia-gmond
[root@hadoop101 ~]# yum -y install ganglia-gmetad
[root@hadoop101 ~]# yum -y install ganglia-web
[root@hadoop101 ~]# yum -y install ganglia-gmond
- 修改配置文件
/etc/httpd/conf.d/ganglia.conf
[root@hadoop101 ~]# vi /etc/httpd/conf.d/ganglia.conf
# Ganglia monitoring system php web frontend
Alias /ganglia /usr/share/ganglia
<Location /ganglia>
#Order deny,allow
#Deny from all
#Allow from all
#Allow from 127.0.0.1
#Allow from ::1
# Allow from.example.com
Require all granted
</Location>
- 修改配置文件
/etc/ganglia/gmetad.conf
[root@hadoop101 ~]# vim /etc/ganglia/gmetad.conf
修改爲:data_source "hadoop101" 192.168.182.101
- 修改配置文件
/etc/ganglia/gmond.conf
cluster {
name = "hadoop101"
owner = "unspecified"
latlong = "unspecified"
url = "unspecified"
}
udp_send_channel {
#bind_hostname = yes # Highly recommended, soon to be default.
# This option tells gmond to use a source address
# that resolves to the machine's hostname. Without
# this, the metrics may appear to come from any
# interface and the DNS names associated with
# those IPs will be used to create the RRDs.
# mcast_join = 239.2.11.71
host = 192.168.182.101
port = 8649
ttl = 1
}
udp_recv_channel {
# mcast_join = 239.2.11.71
port = 8649
bind = 192.168.182.101
retry_bind = true
# Size of the UDP buffer. If you are handling lots of metrics you really
# should bump it up to e.g. 10MB or even higher.
# buffer = 10485760
}
- 修改配置文件
/etc/selinux/config
[root@hadoop101 ~]# vim /etc/selinux/config
修改:SELINUX=disabled
後重啓
- 啓動
ganglia
[root@hadoop101 ~]# systemctl start httpd
[root@hadoop101 ~]# systemctl start gmetad
[root@hadoop101 ~]# systemctl start gmond
- 打開網頁瀏覽
ganglia
頁面:http://192.168.182.101/ganglia
注:如果完成以上操作依然出現權限不足錯誤,請修改/var/lib/ganglia
目錄的權限:
[root@hadoop101 ~]# udo chmod -R 777 /var/lib/ganglia
1.3 操作Flume測試監控
- 修改
/opt/module/flume/conf
目錄下的flume-env.sh
配置:
JAVA_OPTS="-Dflume.monitoring.type=ganglia
-Dflume.monitoring.hosts=192.168.182.101:8649
-Xms100m
-Xmx200m"
- 啓動
Flume
任務
[root@hadoop100 flume]$ bin/flume-ng agent \
--conf conf/ \
--name a1 \
--conf-file job/flume-netcat-logger.conf \
-Dflume.root.logger==INFO,console \
-Dflume.monitoring.type=ganglia \
-Dflume.monitoring.hosts=192.168.1.101:8649
- 發送數據觀察
ganglia
監測圖
[root@hadoop100 flume]$ nc localhost 44444
圖例說明:
字段(圖表名稱) | 字段含義 |
---|---|
EventPutAttemptCount | source嘗試寫入channel的事件總數量 |
EventPutSuccessCount | 成功寫入channel且提交的事件總數量 |
EventTakeAttemptCount | sink嘗試從channel拉取事件的總數量。這不意味着每次事件都被返回,因爲sink拉取的時候channel可能沒有任何數據。 |
EventTakeSuccessCount | sink成功讀取的事件的總數量 |
StartTime | channel啓動的時間(毫秒) |
StopTime | channel停止的時間(毫秒) |
ChannelSize | 目前channel中事件的總數量 |
ChannelFillPercentage | channel佔用百分比 |
ChannelCapacity | channel的容量 |
二、自定義Source
2.1 介紹
Source
是負責接收數據到Flume Agent
的組件。Source
組件可以處理各種類型、各種格式的日誌數據,包括avro
、thrift
、exec
、jms
、spooling directory
、netcat
、sequence generator
、syslog
、http
、legacy
。
官方提供的source
類型已經很多,但是有時候並不能滿足實際開發當中的需求,此時我們就需要根據實際需求自定義某些source
。官方也提供了自定義source
需要繼承AbstractSource
類並實現Configurable
和PollableSource
接口。
2.2 編碼
需求: 使用flume
接收數據,並給每條數據添加後綴,輸出到控制檯。前綴可從flume
配置文件中配置。
引入依賴:
<dependency>
<groupId>org.apache.flume</groupId>
<artifactId>flume-ng-core</artifactId>
<version>1.7.0</version>
</dependency>
編碼:
public class MySource extends AbstractSource implements Configurable,
PollableSource {
/**
* 定義需要從配置中讀取的字段
*/
//兩條數據之間的間隔
private Long delay;
private String field;
/**
* 接收數據,將數據封裝成一個個的Event,寫入Channel。
*/
@Override
public Status process() throws EventDeliveryException {
try {
Map<String, String> header = new HashMap<>();
SimpleEvent event = new SimpleEvent();
for (int i = 0; i < 5; i++) {
event.setHeaders(header);
event.setBody((field + i).getBytes());
getChannelProcessor().processEvent(event);
Thread.sleep(delay);
}
} catch (Exception e) {
return Status.BACKOFF;
}
return Status.READY;
}
@Override
public long getBackOffSleepIncrement() {
return 0;
}
@Override
public long getMaxBackOffSleepInterval() {
return 0;
}
/**
*讀取配置文件(xx.conf)中的配置信息
*/
@Override
public void configure(Context context) {
delay = context.getLong("delay", 2000l);
field = context.getString("field", "Custom Source");
}
}
2.3 測試
打包: 將寫好的代碼打包,並放到flume
的lib
目錄下。
配置文件
# Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1
# Describe/configure the source
a1.sources.r1.type = com.hucheng.flume.MySource
a1.sources.r1.delay = 1000
#a1.sources.r1.field = hello
# Describe the sink
a1.sinks.k1.type = logger
# Use a channel which buffers events in memory
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
# Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
開啓任務:
[root@hadoop100 flume]$ bin/flume-ng agent -c conf/ -f job/mysource.conf
-n a1 -Dflume.root.logger=INFO,console
三、自定義Sink
2.1 介紹
Sink
不斷地輪詢Channel
中的事件且批量地移除它們,並將這些事件批量寫入到存儲或索引系統、或者被髮送到另一個Flume Agent
。
Sink
是完全事務性的。在從Channel
批量刪除數據之前,每個Sink
用Channel
啓動一個事務。批量事件一旦成功寫出到存儲系統或下一個Flume Agent
,Sink
就利用Channel
提交事務。事務一旦被提交,該Channel
從自己的內部緩衝區刪除事件。
Sink
組件目的地包括hdfs
、logger
、avro
、thrift
、ipc
、file
、null
、HBase
、solr
、自定義。官方提供的Sink
類型已經很多,但是有時候並不能滿足實際開發當中的需求,此時我們就需要根據實際需求自定義某些Sink
,官方也提供了自定義Sink
的接口需要繼承AbstractSink
類並實現Configurable
接口。
2.2 編碼
需求: 使用flume
接收數據,並給每條數據添加後綴,輸出到控制檯。前綴可從flume
配置文件中配置。
public class MySink extends AbstractSink implements Configurable {
//創建Logger對象
private static final Logger LOG =
LoggerFactory.getLogger(AbstractSink.class);
private String prefix;
private String suffix;
@Override
public Status process() throws EventDeliveryException {
//聲明返回值狀態信息
Status status;
//獲取當前Sink綁定的Channel
Channel channel = getChannel();
//獲取事務
Transaction transaction = channel.getTransaction();
//聲明事件
Event event;
//開啓事務
transaction.begin();
//讀取Channel中的事件,直到讀取到事件結束循環
while (true) {
event = channel.take();
if (event != null) {
break;
}
}
try {
//處理事件(打印)
LOG.info(prefix + new String(event.getBody()) + suffix);
//事務提交
transaction.commit();
status = Status.READY;
} catch (Exception e) {
//遇到異常,事務回滾
transaction.rollback();
status = Status.BACKOFF;
} finally {
transaction.close();
}
return status;
}
@Override
public void configure(Context context) {
//讀取配置文件內容,有默認值
prefix = context.getString("prefix", "hello:");
//讀取配置文件內容,無默認值
suffix = context.getString("suffix");
}
}
2.3 測試
打包: 將寫好的代碼打包,並放到flume
的lib
目錄下。
配置文件
# Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1
# Describe/configure the source
a1.sources.r1.type = netcat
a1.sources.r1.bind = localhost
a1.sources.r1.port = 44444
# Describe the sink
a1.sinks.k1.type = com.hucheng.flume.MySink
#a1.sinks.k1.prefix = hello:
a1.sinks.k1.suffix = :hello
# Use a channel which buffers events in memory
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
# Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
開啓任務:
[root@hadoop100 flume]# bin/flume-ng agent -c conf/ -f job/mysink.conf -n a1
-Dflume.root.logger=INFO,console
[root@hadoop100 ~]# nc localhost 44444
hello
四、企業真實面試題
4.1 你是如何實現Flume數據傳輸的監控的
使用第三方框架Ganglia
實時監控Flume
。
4.2 Flume的Source,Sink,Channel的作用?你們Source是什麼類型?
①作用
Source
組件是專門用來收集數據的,可以處理各種類型、各種格式的日誌數據,包括avro
、thrift
、exec
、jms
、spooling directory
、netcat
、sequence generator
、syslog
、http
、legacy
Channel
組件對採集到的數據進行緩存,可以存放在Memory
或File
中。Sink
組件是用於把數據發送到目的地的組件,目的地包括Hdfs
、Logger
、avro
、thrift
、ipc
、file
、Hbase
、solr
、自定義。
②我公司採用的Source
類型爲:
- 監控後臺日誌:
exec
- 監控後臺產生日誌的端口:
netcat
、Exec
、spooldir
4.3 Flume的Channel Selectors
Channel Selectors
可以讓不同的項目日誌通過不同的ChanneI
到不同的Sink
中去。Channel Selectors
有兩種類型:Replicating Channel Selector (default)
和Multiplex ing Channel Selector
。
這兩種Selector
的區別是:Replicating
會將source
過來的events
發往所有channel
,而Multiplexing
可以選擇該發往哪些Channel
。
4.4 Flume參數調優
①Source
增加Source
個數(使用Tair Dir Source
時可增加FileGroups
個數)可以增大Source
的讀取數據的能力。例如:當某一個目錄產生的文件過多時需要將這個文件目錄拆分成多個文件目錄,同時配置好多個Source
以保證Source
有足夠的能力獲取到新產生的數據。
batchSize
參數決定Source
一次批量運輸到Channel
的event
條數,適當調大這個參數可以提高Source
搬運Event
到Channel
時的性能。
②Channel
type
選擇memory
時Channel
的性能最好,但是如果Flume
進程意外掛掉可能會丟失數據。type
選擇file
時Channel
的容錯性更好,但是性能上會比memory channel
差。
使用file Channel
時dataDirs
配置多個不同盤下的目錄可以提高性能。
Capacity
參數決定Channel
可容納最大的event
條數。transactionCapacity
參數決定每次Source
往channel
裏面寫的最大event
條數和每次Sink
從channel
裏面讀的最大event
條數。transactionCapacity
需要大於Source和Sink
的batchSize
參數。
③Sink
增加Sink
的個數可以增加Sink
消費event
的能力。Sink
也不是越多越好夠用就行,過多的Sink
會佔用系統資源,造成系統資源不必要的浪費。
batchSize
參數決定Sink
一次批量從Channel
讀取的event
條數,適當調大這個參數可以提高Sink
從Channel
搬出event
的性能。
4.5 Flume的事務機制
Flume
的事務機制(類似數據庫的事務機制):Flume
使用兩個獨立的事務分別負責從Soucrce
到Channel
,以及從Channel
到Sink
的事件傳遞。比如spooling directory source
爲文件的每一行創建一個事件,一旦事務中所有的事件全部傳遞到Channel
且提交成功,那麼Soucrce
就將該文件標記爲完成。同理,事務以類似的方式處理從Channel
到Sink
的傳遞過程,如果因爲某種原因使得事件無法記錄,那麼事務將會回滾。且所有的事件都會保持到Channel
中,等待重新傳遞。
4.6 Flume採集數據會丟失嗎?
不會,Channel
存儲可以存儲在File
中,數據傳輸自身有事務。