You are browsing a read-only backup copy of Wikitech. The live site can be found at

Logstash: Difference between revisions

From Wikitech-static
Jump to navigation Jump to search
imported>Filippo Giunchedi
Line 239: Line 239:
Puppet will not restart logstash.  This must be done manually in a rolling fashion, and it's strongly suggested to perform this in step with the plugin deploy.  
Puppet will not restart logstash.  This must be done manually in a rolling fashion, and it's strongly suggested to perform this in step with the plugin deploy.  
<br />
<br />
==Prototype (Beta) Logstash==
{{anchor|Prototype (Beta) Logstash}}
==Beta Cluster Logstash==

;Web interface
;Web interface
:It hosts a functional Logstash + Elasticsearch + Kibana stack at [] that aggregates log data produced by the [[Nova_Resource:Deployment-prep|beta cluster]]. Credentials for this can be found on deployment-deploy01.deployment-prep.eqiad.wmflabs in /root/secrets.txt.
:It hosts a functional Logstash + Elasticsearch + Kibana stack at [] that aggregates log data produced by the [[Nova_Resource:Deployment-prep|beta cluster]]. It is hosted on the VM [[toolforge:openstack-browser/server/deployment-logstash2.deployment-prep.eqiad.wmflabs|deployment-logstash2.deployment-prep.eqiad.wmflabs]].
;Access control
:Unlike production Logstash, Beta Cluster may not use LDAP for authentication. Credentials for Beta's Logstash can be found on <code>deployment-deploy01.deployment-prep.eqiad.wmflabs</code> in <code>/root/secrets.txt</code>.


Revision as of 22:34, 7 February 2020

Logstash is a tool for managing events and logs. When used generically, the term encompasses a larger system of log collection, processing, storage and searching activities.

Overview ("ELK+")

File:ELK Tech Talk 2015-08-20.pdf

Wikipedia request flow

File:Using Kibana4 to read logs at Wikimedia Tech Talk 2016-11-14.pdf

Various Wikimedia applications send log events to Logstash, which gathers the messages, converts them into JSON documents, and stores them in an Elasticsearch cluster. Wikimedia uses Kibana as a front-end client to filter and display messages from the Elasticsearch cluster. Below are the core components of our ELK stack, and additional components used. Since we utilize more than the core ELK components, we'll refer to our stack as "ELK+".


Elasticsearch is a multi-node Lucene implementation. The same technology powers the CirrusSearch on WMF wikis.


Logstash is a tool to collect, process, and forward events and log messages. Collection is accomplished via configurable input plugins including raw socket/packet communication, file tailing, and several message bus clients. Once an input plugin has collected data it can be processed by any number of filters which modify and annotate the event data. Finally logstash routes events to output plugins which can forward the events to a variety of external programs including Elasticsearch, local files and several message bus implementations.


Kibana is a browser-based analytics and search interface for Elasticsearch that was developed primarily to view Logstash event data.


Apache Kafka is a distributed steaming system. In our ELK stack Kafka buffers the stream of log messages produced by rsyslog (on behalf of applications) for consumption by Logstash. Nothing should output logs to logstash directly, logs should always be sent by way of Kafka.


Rsyslog is the "rocket-fast system for log processing". In our ELK stack rsyslog is used as the host "log agent". Rsyslog ingests log messages in various formats and from varying protocols, normalizes them and outputs to Kafka.


Elastalert is a utility to query log message data in order to generate alerts. Many conditions, thresholds and output mechanisms are supported.

Systems feeding into logstash

See 2015-08 Tech talk slides

Writing new filters is easy.

Supported log shipping protocols & formats ("interfaces")

Support of logs shipped directly from application to Logstash has been deprecated.

Please see Logstash/Interface for details regarding long-term supported log shipping interfaces.

Systems not feeding into logstash

  • EventLogging (of program-defined events with schemas), despite its name, uses a different pipeline.
  • Varnish logs of the billions of pageviews of WMF wikis would require a lot more hardware. Instead we use Kafka to feed web requests into Hadoop. A notable exception to this rule: varnish user-facing errors (HTTP status 500-599) are sent to logstash to make debugging easier.
  • MediaWiki logs usually go to both logstash and log files, but a few log channels aren't. You can check which in $wmgMonologChannels in InitialiseSettings.php.

Production Logstash Architecture

As of FY2019 Logstash infrastructure is owned by SRE. See also Logstash/SRE_onboard for more information on how to migrate services/applications.

Architecture Diagram

Logging Pipeline Arch Diag.jpg

Web interface runs Kibana
wikitech LDAP username and password and membership in one of the following LDAP groups: nda, ops, wmf
The cluster contains two types of nodes, configured by puppet.
  • role::logstash manages the Logstash "collector" instances. These run logstash, a no-data Elasticsearch node, and an Apache vhost serving the Kibana application. The Apache vhosts also act as reverse proxies to the Elasticsearch cluster and perform LDAP-based authentication to restrict access to the potentially sensitive log information.
  • role::logstash::elasticsearch manages the Elasticsearch data nodes, and the Kafka-logging brokers. These provide the inbound message buffering, and long-term storage layer for log data.
Hostname Convention
logstash1NNN Logstash related servers in Eqiad.
logstash2NNN Logstash related servers in Codfw.
logstashNNNN - Logstash "collector" hosts
elasticsearch-loggingNNNN - Logstash Elasticsearch hosts
kafka-loggingNNNN - Logstash Kafka broker hosts
Operating Systems

All hosts run Debian Stretch as a base operating system

Load Balancing and TLS

The misc Varnish cluster is being used to provide ssl termination and load balancing support for the Kibana application.

Kibana quick intro

  • Start from one of the blue Dashboard links near the top, more are available from the Load icon near the top right.
  • In "Events over time" click to zoom out to see what you want, or select a region with the mouse to zoom in.
    • smaller time intervals are faster
    • be careful you may see no events at all... because you're viewing the future
  • When you get lost, click the Home icon near the top right
  • As an example query, wfDebugLog( 'Flow', ...) in MediaWiki PHP corresponds to type:mediawiki AND channel:flow

Read slide 11 and onwards in the TechTalk on ELK by Bryan Davis, they highlight features of the Kibana web page.


The Elasticsearch API is accessible at

Note: The _search endpoint can only be used without a request body (see task T174960). Use _msearch instead for complex queries that need a request body.

Extract data from Logstash with Python

To get the last 100 log entries matching the Lucene query logger_name:varnishfetcherr AND layer:backend

#!/usr/bin/env python3
import os
import sys
import json
import requests

query = "logger_name:varnishfetcherr AND layer:backend"
results = 100

ldap_user = os.getenv("LDAP_USER")
ldap_pass = os.getenv("LDAP_PASS")

if ldap_user is None or ldap_pass is None:
    print("You need to set LDAP_USER and LDAP_PASS")

url = "{}&q={}".format(
    results, query

resp = requests.get(url, auth=requests.auth.HTTPBasicAuth(ldap_user, ldap_pass))
if resp.status_code != 200:
    print("Something's wrong, response status code={}".format(resp.status_code))

data = resp.json()
for line in data["hits"]["hits"]:

Note: Certain queries with whitespace characters may require additional url-encoding (via urllib.parse.quote or similar) when using python requests. If requests to the logstash API consistently return 504 http status codes, even for relatively lightweight queries, this may be the issue.

Extract data from Logstash (elasticsearch) with curl and jq

logstash-server:~$ cat
curl -XGET 'localhost:9200/_search?pretty&size=10000' -d '
    "query": {
        "query_string" : {
            "query" : "facility:19,local3 AND host:csw2-esams AND @timestamp:[2019-08-04T03:00 TO 2019-08-04T03:15] NOT program:mgd"
    "sort": ["@timestamp"]
} '
logstash-server:~$ bash | jq '.hits.hits[]._source | {timestamp,host,level,message}' | head -20
  "timestamp": "2019-08-04T03:00:00+00:00",
  "host": "csw2-esams",
  "level": "INFO",
  "message": " %-: (root) CMD (newsyslog)"
  "timestamp": "2019-08-04T03:00:00+00:00",
  "host": "csw2-esams",
  "level": "INFO",
  "message": " %-: (root) CMD (   /usr/libexec/atrun)"
  "timestamp": "2019-08-04T03:01:00+00:00",
  "host": "csw2-esams",
  "level": "INFO",
  "message": " %-: (root) CMD (adjkerntz -a)"
$ bash | jq -r '.hits.hits[]._source | {timestamp,host,level,program,message} | map(.) | @csv' > asw2-d2-eqiad-crash.csv


We maintain a WMF specific process to build and distribute Logstash plugins to servers.

The build script and plugin git repository is located at

Plugin build process

  • An up to date Debian stable host with the Logstash package installed
  • Membership to LDAP group cn=archiva-deployers,ou=groups,dc=wikimedia,dc=org
Build Process

First, create a ~/.m2/settings.xml file containing the below (see Archiva#Deploy to Archiva for additional details)

# ~/.m2/settings.xml

Then, check out

git clone

In the newly created directory 'plugins' , initialize git-fat

cd plugins
git-fat init

Then execute the build script, providing the logstash version for which plugins should be built as the first argument

./ 5.6.15    # 5.6.15 is used as an example, substitute in desired logstash version

Or, if rebuilding plugins at the same version, for example to add a new plugin, you may append a local version number like so

# To optionally rebuild plugins for the same logstash version

./ 5.6.15-1   # 5.6.15 local version 1

Also git add and git commit the changes. Then send it for review

git add .
git commit -m "Upgrade logstash plugin to 5.X.X"
git review -R

After review and merging the change, you'll need to pull the change onto the deployment server, and run a scap deploy. Currently the deploy server is deploy1001

# ssh to deploy master, currently deploy1001

cd /srv/deployment/logstash/plugins/

# have a look at what is in the local working copy. you'll want to bring this up to date
# with the gerrit repository but also not inadvertantly clobber manual changes

git pull

# if you wish to see what would happen without actually running the deploy
scap deploy --dry-run --no-log-message

# deploy the updated plugins
scap deploy "updating logstash plugins to $version"

After a successful scap deploy, puppet should do the right thing upon the next agent run to fetch and install the update plugins

Puppet will not restart logstash. This must be done manually in a rolling fashion, and it's strongly suggested to perform this in step with the plugin deploy.

Beta Cluster Logstash

Web interface
It hosts a functional Logstash + Elasticsearch + Kibana stack at that aggregates log data produced by the beta cluster. It is hosted on the VM deployment-logstash2.deployment-prep.eqiad.wmflabs.
Access control
Unlike production Logstash, Beta Cluster may not use LDAP for authentication. Credentials for Beta's Logstash can be found on deployment-deploy01.deployment-prep.eqiad.wmflabs in /root/secrets.txt.


GELF transport

Make sure logging events sent to the GELF input don't have a "type" or "_type" field set, or if set, that it contains the value "gelf". The gelf/logstash config discards any events that have a different value set for "type" or "_type". The final "type" seen in Kibana/Elasticsearch will be take from the "facility" element of the original GELF packet. The application sending the log data to Logstash should set "facility" to a reasonably unique value that identifies your application.



Kafka consumer lag

For a host of reasons it might happen that there's a buildup of messages on Kafka. For example:

elasticsearch is refusing to index messages, thus logstash can't consume properly from kafka.
The reason for index failure is usually conflicting fields, see also bug T150106 for a detailed discussion of the problem. The solution is to find what programs are generating the conflicts and drop them on logstash accordingly, see also bug T228089


Configuration changes

Puppet will restart logstash as needed upon configuration changes (supporting reload is TODO), after merging your configuration change it is usually enough to run cumin in batches of one with >60s of sleep:

 cumin -b1 -s60 'O:logstash' 'run-puppet-agent -q'

Indexing errors

We're alerting on errors that Logstash gets from Elasticsearch whenever there's an "indexing conflict" between fields of the same index (see also bug T236343). The reason usually is because two applications send logs with the same field name but two different types, e.g. response will be sent as a string in one case but as nested object in another. bug T239458 is a good example of this, where different parts of mediawiki send logs formatted in a different way.


Documents and bytes counts

The Elasticsearch cat API provides a simple way to extract general statistics about log storage, e.g. total logs and bytes (not including replication)

 logstash1010:~$ curl -s 'localhost:9200/_cat/indices?v&bytes=b' | awk '/logstash-/ {b+=$10; d+=$7} END {print d; print b}'

Or logs per day (change $3 to $7 to get bytes sans replication)

 logstash1010:~$ curl -s 'localhost:9200/_cat/indices?v&bytes=b' | awk '/logstash-/ { gsub(/logstash-[^0-9]*/, "", $3); sum[$3] += $7 } END { for (i in sum) print i, sum[i] }' | sort

Or logs per month:

 logstash1010:~$ curl -s 'localhost:9200/_cat/indices?v&bytes=b' | awk '/logstash-/ { gsub(/logstash-[^0-9]*/, "", $3); gsub(/\.[0-9][0-9]$/, "", $3); sum[$3] += $7 } END { for (i in sum) print i, sum[i] }' | sort

See also