ntpstats-ng - syslog-ntp

Übertragung von NTP Statistken mit Hilfe von syslog-ng - cool!

Important
Dieses Kapitel wird editiert und ist noch nicht abgeschlossen.

Syslog

Die ersten Schritte mit Logstash haben die Grundlagen der Verarbeitung einer lokalen Log-Datei gezeigt.

Mit Hilfe eines Daemon aus der Syslog-Familie (rsyslog, syslog-ng u. a.) können natürlich auch Logs an einen oder mehrere Syslog-Server “remote” übertragen werden. Als Ziel- bzw. Destination-Port kommt meist 514 zur Anwendung.[1]

syslog-ntp_syslog
Figure 1. Syslog Client und Server

NTP Statistken sind Log-Dateien

Vergleichen wir nun die erste Zeile aus unserem Logstash-Beispiel mit der aus peerstats.

head -n 1 /var/log/ntp/ntp.log
29 Jan 22:15:59 ntpd[19633]: ntpd [email protected] Thu Nov 24 08:57:19 UTC 2016 (1): Starting
head -n 1 /var/log/ntp/stats/HOSTNAME.peerstats
57782 12.556 192.168.1.7 963a 0.006794870 0.000430754 0.015162412 0.006043270
Tip
Beide Dateien enthalten zeilenweise Informationen.
Warum also nicht auch NTP Statistiken auf dem Syslog-Weg übertragen?

System- und NTP-Statistik-Logging

Ich habe nach einer Möglichkeit gesucht, die NTP Statistiken “parallel” auf bereits benutzten Pfaden zu übertragen. Ein zusätzlicher Port gestattet es, einen zentralen NTP-Statistik-Server zusammen mit einem System-Log-Server oder unabhängig von ihm zu betreiben.

Tip
Als Port-Nummer habe ich willkürlich 51123 festgelegt.[2]
syslog-ntp
Figure 2. System- und NTP-Statistik-Logging-Varianten

syslog-ng Client

syslog-ng-3.7.3
# /etc/syslog-ng/syslog-ng.conf.syslog-ntp.client

options { ... } (1)

source loopstats { ... } (2)
source peerstats { ... } (3)

destination syslog-ntp { ... } (4)

log { ... } (5)
Note
Die komplette Datei findet sich hier: syslog-ng.conf.syslog-ntp.client

options

options {
    # ...
    ts-format(iso);
};

source loopstats

source loopstats {
    file(
        "/var/log/ntp/stats/HOSTNAME.loopstats"
        follow-freq(5)
        flags(no-parse)
    );
};

source peerstats

source peerstats {
    file(
        "/var/log/ntp/stats/HOSTNAME.peerstats"
        follow-freq(5)
        flags(no-parse)
    );
};

destination

destination syslog-ntp {
    network(
        "localhost"
        port(51123)
        transport("tcp")
        template("${TAGS} ${MSG}\n") (1)
        template-escape(no)
        # TLS settings ...
    );
};
  1. "TAGS" = ".source.loopstats" bzw. "TAGS" = ".source.peerstats"

log

log {
    source(loopstats);
    source(peerstats);
    destination(syslog-ntp);
};

syslog-ng Server

syslog-ng-3.7.3
# /etc/syslog-ng/syslog-ng.conf.syslog-ntp.server

options { ... } (1)

source syslog-ntp { ... } (2)

destination ntpstats { ... } (3)

log { ... } (4)
Note
Die komplette Datei findet sich hier: syslog-ng.conf.syslog-ntp.server

options

options {
    # ...
    ts-format(iso);
};

source syslog-ntp

source syslog-ntp {
    network(
        port(51123)
        transport("tcp")
        flags(no-parse)
    );
};

destination ntpstats

destination ntpstats {
    file(
        "/tmp/ntpstats-ng-syslog.log"
    );
};

log

log {
    source(syslog-ntp);
    destination(ntpstats);
};

Es funktioniert!

syslog-ntp_logflow
Figure 3. syslog-ng Datenfluss

Schauen wir uns zwei Zeilen auf dem Server an.

SERVER:/tmp/ntpstats-ng-syslog.log
2017-02-19T17:49:02+00:00 ntpmon .source.loopstats 57803 64140.505 -0.000000242 -17.058 0.000001620 0.001482 4
2017-02-19T17:49:17+00:00 ntpmon .source.peerstats 57803 64154.505 127.127.20.0 966a -0.000880746 0.000000000 0.000395621 0.001026945
Tip
syslog-ng hat für uns drei neue Felder vor den Inhalt jeder NTP-Statistik-Zeile eingefügt.
  1. loopstats

    1. "timestamp" = "2017-02-19T17:49:02+00:00"

    2. "host" = "ntpmon"

    3. "tag" = ".source.loopstats"

    4. "message" = "57803 64140.505 -0.000000242 -17.058 0.000001620 0.001482 4"

  2. peerstats

    1. "timestamp" = "2017-02-19T17:49:17+00:00"

    2. "host" = "ntpmon"

    3. "tag" = ".source.peerstats"

    4. "message" = "57803 64154.505 127.127.20.0 966a -0.000880746 0.000000000 0.000395621 0.001026945"

Datenfluss

Logstash kann mit logstash-input-tcp sehr einfach selbst als Syslog-Server fungieren.

syslog-ntp_dataflow
Figure 4. syslog-ntp Datenfluss

Logstash

logstash-5.1.2
# /etc/logstash/conf.d/ntpstats-ng.conf.syslog-ntp
input {
    tcp { ... } (1)
}

filter {
    grok { ... } (2)
    mutate { ... } (3)
}

output {
    file { ... } (4)
    elasticsearch { ... } (5)
}
Note
Die komplette Datei findet sich hier: ntpstats-ng.conf.syslog-ntp

input

tcp

input {
    tcp {
        port => 51123
        mode => "server"
        type => "syslog-ntp"
        # TLS settings ...
    }
}

filter

Die folgenden Filter werden nur auf die typisierten Events angewandt.

filter {
    if [type] == "syslog-ntp" {

grok

        grok {
            match        => { "message" => "%{WORD:syslog_tags} %{GREEDYDATA:message}" }
            overwrite    => [ "message" ]
            remove_field => [ "port" ]
        }

mutate

        if [syslog_tags] =~ "stats" {
            mutate {
                replace      => { "type" => "%{syslog_tags}" }
                add_field    => { "path" => "%{host}.%{type}" }
                remove_field => [ "syslog_tags" ]
            }
        }
    }
}

output

output {
    if [type] == "loopstats"
    or [type] == "peerstats" {

file

        # DEBUG
        file {
            path => "/tmp/%{elastic_index}.json"
        }

elasticsearch

        if  ! ( "_grokparsefailure" in [tags] ) {
            elasticsearch {
                hosts => [ "localhost:9200" ]
                index => "%{elastic_index}"
            }
        }
    }
}

Die Details der Datenhaltung sind im Kapitel Elasicsearch beschrieben.


ntpstats-ng © MMXV-MMXVII WOLfgang Schricker

results matching ""

    No results matching ""