溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Logstash中怎么實(shí)現(xiàn)一個(gè)自動(dòng)化運(yùn)維系統(tǒng)

發(fā)布時(shí)間:2021-08-10 11:31:31 來(lái)源:億速云 閱讀:141 作者:Leah 欄目:云計(jì)算

本篇文章給大家分享的是有關(guān)Logstash中怎么實(shí)現(xiàn)一個(gè)自動(dòng)化運(yùn)維系統(tǒng),小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。

1、Nginx日志配置

編輯 /etc/nginx/nginx.conf 添加以下內(nèi)容

   log_format json '{"@timestamp":"$time_iso8601",'
        '"host":"$host",'
        '"scheme":"$scheme",'
        '"server_addr":"$server_addr",'
        '"client_ip":"$remote_addr",'
        '"server_protocol":"$server_protocol",'
        '"method":"$request_method",'
        '"query_string":"$query_string",'
        '"body_bytes_sent":$body_bytes_sent,'
        '"bytes_sent":$bytes_sent,'
        '"request_length":$request_length,'
        '"request_time":$request_time,'
        '"upstream_time":"$upstream_response_time",'
        '"upstream_host":"$upstream_addr",'
        '"upstream_status":"$upstream_status",'
        '"server_name":"$server_name",'
        '"url":"$uri",'
        '"request_url":"$request_uri",'
        '"http_x_forwarded_for":"$http_x_forwarded_for",'
        '"referer":"$http_referer",'
        '"agent":"$http_user_agent",'
        '"status":$status}';

    access_log  /home/nginx/log/access.log  json;
2.2 rsyslog配置

使用rsyslog推送日志到Logstash,編輯/etc/rsyslog.d/nginx.conf,輸入以下內(nèi)容,完成后記得重啟rsyslog服務(wù)。

$ModLoad imfile
$Modload mmjsonparse
action(type="mmjsonparse")
template(name="nginx-json" type="list") {
    property(name="$!all-json")
}

$InputFileName /home/nginx/log/access.log
$InputFileTag nginx-access
$InputFileStateFile nginx-accessfile
$InputFileSeverity info
$InputFileFacility local7
$InputRunFileMonitor
$InputFilePersistStateInterval 1

if $syslogtag startswith 'nginx' then @@10.160.209.10:514;nginx-json

3. 服務(wù)端配置

3.1 Logstash 配置

新建一個(gè)Logstash配置,名稱為metric_nginx.conf,內(nèi)容如下

input {
    syslog {
        host => "10.160.209.10"
        port => "514"
        codec => "json"
        }
}
filter {
      json {
        source => "msg"
        }
      mutate {
        convert => ["request_time",float]
        }
      mutate {
        remove_field => ["msg"]
        remove_field => ["@version"]
        remove_field => ["port"]
        remove_field => ["facility"]
        remove_field => ["priority"]
        remove_field => ["severity"]
        remove_field => ["severity_label"]
        remove_field => ["facility_label"]
        }
    metrics {
        meter => "error.%{status}" #按不同的status值建立計(jì)數(shù)器
        add_tag => "metric"
        ignore_older_than => 10
    }
}

output {
  if "metric" in [tags] {
      if [error.504][rate_1m] > 0.0 { # 當(dāng)達(dá)504計(jì)數(shù)器到達(dá)設(shè)定的閾值則告警
        stdout {
            codec => line {
            format => "alarm: %{[error.504][rate_1m]}" #模擬告警
          }
        }
      }
  }
}

之后使用下面的命令啟動(dòng)Logstash服務(wù)

 logstash -f metric_nginx.conf

關(guān)于上面[rate_1m]計(jì)數(shù)器的定義請(qǐng)參考:https://www.elastic.co/guide/en/logstash/current/plugins-filters-metrics.html#_literal_meter_literal_values

4. 基于Logstash模型的運(yùn)維自動(dòng)化模型

介紹完上面的實(shí)例,相信大家可以看到Logstash的作用遠(yuǎn)不止如此,在熟悉Logstsh這套日志處理流程以后,完全可以實(shí)現(xiàn)一套基于日志驅(qū)動(dòng)的自動(dòng)化運(yùn)維平臺(tái)。如下圖所示,可以將Mon、OSD等各類服務(wù)的日志接入Logtash,通過(guò)Logstash處理以后借助不同的output插件,可以選擇性進(jìn)行Alarm告警,或者是觸發(fā)Ansible playbook操作,亦或是存儲(chǔ)到redis、kafka一類的消息中間件從而打通和其他業(yè)務(wù)系統(tǒng)的數(shù)據(jù)通道。特別是當(dāng)你的運(yùn)維熟悉了Ceph的各種日志異常情況以后,完全可以做到一些線上故障的自動(dòng)化處理。

Logstash中怎么實(shí)現(xiàn)一個(gè)自動(dòng)化運(yùn)維系統(tǒng)

以上就是Logstash中怎么實(shí)現(xiàn)一個(gè)自動(dòng)化運(yùn)維系統(tǒng),小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)
推薦閱讀:
  1. logstash
  2. ELK——Logstash

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI