HOAB

History of a bug

ELK - parsing des logs Apache

Rédigé par gorki Aucun commentaire

Le problème :

Analyser les logs Apache (entre autre) avec un système de requêtage.

Alors il y a bien l'outil fait à la main, ça marche toujours :) mais je voulais tenter une stack ELK (Elasticsearch Logstash Kibana)

Solution :

Tout d'abord l'installation :

- je suis parti du docker qnib/elk (https://hub.docker.com/r/qnib/elk/).

- auquel j'ajoute le plugin elasticsearch-head pour la visulation des données

- création du docker initial (commité depuis avec le plugin elasticsearch) :

. envir.sh

sudo docker run -t -i --rm -h elk --name sgl-elk --privileged \
    ${DNS_STUFF} ${DEV_MOUNTS} ${LINK} \
    ${HTTP_PORT} ${LS_CONF} ${AP_LOG} \
    -e HTUSER=${HTUSER} -e HTPASSWD=${HTPASSWD} \
    ${ES_PERSIST} qnib/elk:latest bash

- fichier d'environnement (envir.sh) :

# To get all the /dev/* devices needed for sshd and alike:
export DEV_MOUNTS="-v /dev/null:/dev/null -v /dev/urandom:/dev/urandom -v /dev/random:/dev/random"
export DEV_MOUNTS="${DEV_MOUNTS} -v /dev/full:/dev/full -v /dev/zero:/dev/zero"
### OPTIONAL -> if you want to store Elasticsearchs data outside 
export ES_PERSIST="-v ${HOME}/elk/elasticsearch:/data/"
### OPTIONAL -> To use a mapped in configuration directory
# if not used, the default will be used within the container
export LS_CONF="-v ${HOME}/elk/logstash.d/:/etc/logstash/conf.d/"
### OPTIONAL -> map apache2 config into container
export AP_LOG="-v ${HOME}/elk/var/log/apache2/:/var/log/apache2"
### OPTIONAL -> set the external port to something else then 80
export HTTP_PORT="-e HTTPPORT=8080 -p 8080:80 -p 9200:9200 -p 5601:5601"
### OPTIONAL -> To secure kibana and elasticsearch user/passwd could be set
# if a user is set and no passwd, the user will be set as password
export HTUSER=kibana
export HTPASSWD=kibana

 

Utilisation en oneshot :

- je fais du parsing à la demande plutôt que d'utiliser la stack en monitoring de log continu :

bin/logstash -f /etc/logstash/conf.d/10_apache.conf

- un exemple de fichier configuration logstash pour Apache :

input {
  file {
    path => "/var/log/apache2/test.log"
    start_position => "beginning"
    type => "apache"
  }
}

filter {
    if [type] == "apache" {
        grok {
            match => [ "message", "%{COMBINEDAPACHELOG}" ]
            add_tag => ["apachelog"]
        }
        if "apachelog" in [tags]  {
	    date {
    		match => [ "timestamp" , "dd/MMM/yyyy:HH:mm:ss Z" ]
  	    }
            geoip {
              source => "clientip"
              target => "geoip"
              add_field => [ "[geoip][coordinates]", "%{[geoip][longitude]}" ]
              add_field => [ "[geoip][coordinates]", "%{[geoip][latitude]}"  ]
            }
            mutate {
              convert => [ "[geoip][coordinates]", "float" ]
            }
        }
    }
}

output {
    if [type] == "apache" {
        stdout { codec => rubydebug }
        elasticsearch {
            protocol => "http"
            cluster => "logstash"
        }
    }
}

- ensuite je me connecte sur elasticsearch pour vérifier que les données ont bien été insérées :

http://localhost:9200/_plugin/head/

- puis sur Kibana pour exploiter les logs :

http://localhost:8080 (kibana3)

http://localhost:5601 (kibana4)

 

Edit :

- Utilsation de grok : https://www.elastic.co/guide/en/logstash/current/plugins-filters-grok.html

- les patterns grok d'ELK : https://github.com/elastic/logstash/blob/v1.4.2/patterns/grok-patterns

Fil RSS des articles de ce mot clé