From 29a92055086379e63b0fb1fff754552a93be3a74 Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Mon, 23 May 2022 14:17:11 +0200 Subject: [PATCH] small fixes. when-then-style --- lib/collector/dovecot.rb | 42 +++----- lib/collector/postfix.rb | 228 +++++++++++++++++---------------------- 2 files changed, 112 insertions(+), 158 deletions(-) diff --git a/lib/collector/dovecot.rb b/lib/collector/dovecot.rb index 74c58eb..463c3dc 100644 --- a/lib/collector/dovecot.rb +++ b/lib/collector/dovecot.rb @@ -37,16 +37,11 @@ class Collector::Dovecot def collect entry, msg case msg - when /\AConnect from / - @connect.increment - when /\ADisconnect from / - @disconnect.increment - when /saved mail to / - @saved_mail_to_mailbox.increment labels: {process: @process} - when /\Asieve: (.*)/ - @sieve.collect entry, @process, $1 - else - STDERR.puts "# #{entry._systemd_unit} #{entry.syslog_identifier} delivery| #{entry.message}" + when /\AConnect from / then @connect.increment + when /\ADisconnect from / then @disconnect.increment + when /saved mail to / then @saved_mail_to_mailbox.increment labels: {process: @process} + when /\Asieve: (.*)/ then @sieve.collect entry, @process, $1 + else STDERR.puts "# #{entry._systemd_unit} #{entry.syslog_identifier} delivery| #{entry.message}" end end end @@ -109,7 +104,7 @@ class Collector::Dovecot case msg when /\ALogin: user=/ @logged_in.increment - when /\ADisconnected \((.*?)\): user=)?: (.*)/ then @imap.collect entry, $1 + when /\Almtp\([^ ]+\)<[^ ]+>: (.*)/ then @lmtp.collect entry, $1 + when /\Almtp\([^ ]+\): (.*)/ then @lmtp.collect entry, $1 + when /\Adeliver(?:[^:]+): (.*)/ then @deliver.collect entry, $1 when /\Aauth: Error: (.*)/ case $1 when /\ALDAP: Connection lost to LDAP server, / - @auth_ldap_connection_lost.increment + @auth_errors.increment labels: {error: "ldap connection lost"} else STDERR.puts "# #{entry._systemd_unit} #{entry.syslog_identifier}| #{entry.message}" - @auth_errors.increment + @auth_errors.increment labels: {error: ""} end - when /\Aimap\([^)]+\)(?:<[^ ]+>)?: (.*)/ - @imap.collect entry, $1 - when /\Almtp\([^ ]+\)<[^ ]+>: (.*)/ - @lmtp.collect entry, $1 - when /\Almtp\([^ ]+\): (.*)/ - @lmtp.collect entry, $1 - when /\Adeliver(?:[^:]+): (.*)/ - @deliver.collect entry, $1 - else - STDERR.puts "# #{entry._systemd_unit} #{entry.syslog_identifier}| #{entry.message}" + else STDERR.puts "# #{entry._systemd_unit} #{entry.syslog_identifier}| #{entry.message}" end end end diff --git a/lib/collector/postfix.rb b/lib/collector/postfix.rb index 5ef2f3d..aef248e 100644 --- a/lib/collector/postfix.rb +++ b/lib/collector/postfix.rb @@ -32,8 +32,8 @@ class Collector::Postfix class Cache def initialize store, prometheus @store = store - @full_cleanup = prometheus.counter :full_cleanup_total, docstring: 'A counter of total cache cleanups', labels: %i[file] - @full_cleanup_retained = prometheus.gauge :full_cleanup_retained_entries, docstring: 'Retained entries of last cache cleanups', labels: %i[file] + @full_cleanup = prometheus.counter :full_cleanup_total, docstring: 'A counter of total cache cleanups', labels: %i[file] + @full_cleanup_retained = prometheus.gauge :full_cleanup_retained_entries, docstring: 'Retained entries of last cache cleanups', labels: %i[file] @full_cleanup_dropped = prometheus.counter :full_cleanup_dropped_entries_total, docstring: 'Total dropped entries of cache cleanups', labels: %i[file] end @@ -54,45 +54,45 @@ class Collector::Postfix class Postscreen def initialize store, prometheus @store = store - @noqueue = Noqueue.new store, Collector::PrefixProxy.new( prometheus, :noqueue) - @cache = Cache.new store, Collector::PrefixProxy.new( prometheus, :cache) - @connect_from = prometheus.counter :connect_from_total, docstring: 'A counter of connections to postscreen' - @whitelisted = prometheus.counter :whitelisted_total, docstring: 'A counter of WHITELISTED connections to postscreen' - @pass_old = prometheus.counter :pass_old_total, docstring: 'A counter of PASS OLD connections to postscreen' - @pass_new = prometheus.counter :pass_new_total, docstring: 'A counter of PASS NEW connections to postscreen' - @dnsbl = prometheus.counter :dnsbl_total, docstring: 'A counter of DNSBL-blocked to postscreen' - @bare_newline = prometheus.counter :bare_newline_total, docstring: 'A counter of BARE NEWLINE-blocked to postscreen' - @command_pipelining = prometheus.counter :command_pipelining_total, docstring: 'A counter of COMMAND PIPELINING-blocked to postscreen' - @command_time_limit = prometheus.counter :command_time_limit_total, docstring: 'A counter of COMMAND TIME LIMIT-blocked to postscreen' - @hangup = prometheus.counter :hangup_total, docstring: 'A counter of HANGUP to postscreen' - @bdat = prometheus.counter :bdat_total, docstring: 'A counter of BDAT to postscreen' - @pregreet = prometheus.counter :pregreet_total, docstring: 'A counter of PREGREET to postscreen' - @disconnect = prometheus.counter :disconnect_total, docstring: 'A counter of DISCONNECT to postscreen' - @unknown = prometheus.counter :unknown_total, docstring: 'A counter of unknown loglines by postscreen' - @psc_cache_update_delay = prometheus.summary :psc_cache_update_delay_total, docstring: 'A counter of PSC cache update delays by file', labels: %i[file] - @curr_unavailable = prometheus.counter :service_currently_unavailable_total, docstring: 'A counter for rejected mails, because service currently unavailable - so greylisted.' - @dnsblog_reply_timeout = prometheus.counter :dnsblog_reply_timeout_total, docstring: 'Total timedout requests for dnsblog' - @data_without_valid_rcpt= prometheus.counter :data_without_valid_rcpt_total, docstring: 'A counter of DATA without valid RCPT events' - @warnings = prometheus.counter :warnings_total, docstring: 'A counter of any warnings' + @noqueue = Noqueue.new store, Collector::PrefixProxy.new( prometheus, :noqueue) + @cache = Cache.new store, Collector::PrefixProxy.new( prometheus, :cache) + @connect_from = prometheus.counter :connect_from_total, docstring: 'A counter of connections to postscreen' + @whitelisted = prometheus.counter :whitelisted_total, docstring: 'A counter of WHITELISTED connections to postscreen' + @pass_old = prometheus.counter :pass_old_total, docstring: 'A counter of PASS OLD connections to postscreen' + @pass_new = prometheus.counter :pass_new_total, docstring: 'A counter of PASS NEW connections to postscreen' + @dnsbl = prometheus.counter :dnsbl_total, docstring: 'A counter of DNSBL-blocked to postscreen' + @bare_newline = prometheus.counter :bare_newline_total, docstring: 'A counter of BARE NEWLINE-blocked to postscreen' + @command_pipelining = prometheus.counter :command_pipelining_total, docstring: 'A counter of COMMAND PIPELINING-blocked to postscreen' + @command_time_limit = prometheus.counter :command_time_limit_total, docstring: 'A counter of COMMAND TIME LIMIT-blocked to postscreen' + @hangup = prometheus.counter :hangup_total, docstring: 'A counter of HANGUP to postscreen' + @bdat = prometheus.counter :bdat_total, docstring: 'A counter of BDAT to postscreen' + @pregreet = prometheus.counter :pregreet_total, docstring: 'A counter of PREGREET to postscreen' + @disconnect = prometheus.counter :disconnect_total, docstring: 'A counter of DISCONNECT to postscreen' + @unknown = prometheus.counter :unknown_total, docstring: 'A counter of unknown loglines by postscreen' + @warnings = prometheus.counter :warnings_total, docstring: 'A counter of any warnings' + @psc_cache_update_delay = prometheus.summary :psc_cache_update_delay_total, docstring: 'A counter of PSC cache update delays by file', labels: %i[file] + @curr_unavailable = prometheus.counter :service_currently_unavailable_total, docstring: 'A counter for rejected mails, because service currently unavailable - so greylisted.' + @dnsblog_reply_timeout = prometheus.counter :dnsblog_reply_timeout_total, docstring: 'Total timedout requests for dnsblog' + @data_without_valid_rcpt = prometheus.counter :data_without_valid_rcpt_total, docstring: 'A counter of DATA without valid RCPT events' end def collect entry #STDERR.puts "postscreen: #{entry.message}" case entry.message - when /\ACONNECT from / - @connect_from.increment - when /\AWHITELISTED / - @whitelisted.increment - when /\APASS OLD / - @pass_old.increment - when /\APASS NEW / - @pass_new.increment - when /\ADISCONNECT / - @disconnect.increment - when /\APREGREET / - @pregreet.increment - when /\ABDAT / - @bdat.increment + when /\ACONNECT from / then @connect_from.increment + when /\AWHITELISTED / then @whitelisted.increment + when /\APASS OLD / then @pass_old.increment + when /\APASS NEW / then @pass_new.increment + when /\ADISCONNECT / then @disconnect.increment + when /\APREGREET / then @pregreet.increment + when /\ABDAT / then @bdat.increment + when /\AHANGUP / then @hangup.increment + when /\ADNSBL rank / then @dnsbl.increment + when /\ABARE NEWLINE / then @bare_newline.increment + when /\ACOMMAND PIPELINING / then @command_pipelining.increment + when /\ACOMMAND TIME LIMIT / then @command_time_limit.increment + when /\ADATA without valid RCPT / then @data_without_valid_rcpt.increment + when /\Acache / then @cache.collect entry when /\ANOQUEUE: (.*)/ case msg = $1 when /\Areject: RCPT from [^ ]+: 450 4.3.2 Service currently unavailable; / @@ -100,18 +100,6 @@ class Collector::Postfix else @noqueue.collect entry end - when /\AHANGUP / - @hangup.increment - when /\ADNSBL rank / - @dnsbl.increment - when /\ABARE NEWLINE / - @bare_newline.increment - when /\ACOMMAND PIPELINING / - @command_pipelining.increment - when /\ACOMMAND TIME LIMIT / - @command_time_limit.increment - when /\ADATA without valid RCPT / - @data_without_valid_rcpt.increment when /\Awarning: (.*)/ @warnings.increment case $1 @@ -123,8 +111,6 @@ class Collector::Postfix else STDERR.puts "# postscreen warnings: #{entry.message}" end - when /\Acache / - @cache.collect entry else STDERR.puts "# postscreen: #{entry.message}" @unknown.increment @@ -145,18 +131,18 @@ class Collector::Postfix class Smtp def initialize store, prometheus @store = store - @connection_refused = prometheus.counter :connection_refused_total, docstring: 'A counter of refused connections on smtp' + @connection_refused = prometheus.counter :connection_refused_total, docstring: 'A counter of refused connections on smtp' @connection_timed_out = prometheus.counter :connection_timed_out_total, docstring: 'A counter of timed out connections on smtp' - @tls = prometheus.counter :tls_total, docstring: 'A counter of TLS connections on smtp with TLS-version and cipher', labels: %i[trust tls cipher] + @tls = prometheus.counter :tls_total, docstring: 'A counter of TLS connections on smtp with TLS-version and cipher', labels: %i[trust tls cipher] Collector::Postfix.tls_posibilities {|t, s, c| @tls.increment by: 0, labels: {trust: t, tls: s, cipher: c} } - @status = prometheus.histogram :status, docstring: 'A histogram of message status by status', labels: %i[status] - @sent = prometheus.counter :sent_total, docstring: 'A counter of sent messages by smtp' - @deferred = prometheus.counter :deferred_total, docstring: 'A counter of deferred messages by smtp' - @bounced = prometheus.counter :bounced_total, docstring: 'A counter of bounced messages by smtp' - @deliverable = prometheus.counter :deliverable_total, docstring: 'A counter of deliverable messages by smtp' - @undeliverable = prometheus.counter :undeliverable_total, docstring: 'A counter of undeliverable messages by smtp' + @status = prometheus.histogram :status, docstring: 'A histogram of message status by status', labels: %i[status] + @sent = prometheus.counter :sent_total, docstring: 'A counter of sent messages by smtp' + @deferred = prometheus.counter :deferred_total, docstring: 'A counter of deferred messages by smtp' + @bounced = prometheus.counter :bounced_total, docstring: 'A counter of bounced messages by smtp' + @deliverable = prometheus.counter :deliverable_total, docstring: 'A counter of deliverable messages by smtp' + @undeliverable = prometheus.counter :undeliverable_total, docstring: 'A counter of undeliverable messages by smtp' @status_unknown = prometheus.counter :status_unknown_total, docstring: 'A counter of unknown status by smtp' - @unknown = prometheus.counter :unknown_total, docstring: 'A counter of unknown loglines by smtp' + @unknown = prometheus.counter :unknown_total, docstring: 'A counter of unknown loglines by smtp' end def collect entry @@ -164,10 +150,8 @@ class Collector::Postfix case entry.message when /\Aconnect to / case entry.message - when / Connection refused\z/ - @connection_refused.increment - when / Connection timed out\z/ - @connection_timed_out.increment + when / Connection refused\z/ then @connection_refused.increment + when / Connection timed out\z/ then @connection_timed_out.increment else STDERR.puts "# #{entry._systemd_unit} #{entry.syslog_identifier} connect to: #{entry.message}" end @@ -178,16 +162,11 @@ class Collector::Postfix delay, status = $1.to_f, $2.downcase @status.observe delay, labels: {status: status} case status - when 'sent' - @sent.increment - when 'deferred' - @deferred.increment - when 'bounced' - @bounced.increment - when 'deliverable' - @deliverable.increment - when 'undeliverable' - @undeliverable.increment + when 'sent' then @sent.increment + when 'deferred' then @deferred.increment + when 'bounced' then @bounced.increment + when 'deliverable' then @deliverable.increment + when 'undeliverable' then @undeliverable.increment else STDERR.puts "# #{entry._systemd_unit} #{entry.syslog_identifier} status: #{entry.message}" @status_unknown.increment @@ -280,18 +259,16 @@ class Collector::Postfix class Tlsproxy def initialize store, prometheus @store = store - @connect = prometheus.counter :connect_total, docstring: 'A counter of COUNNECT from tlsproxy' + @connect = prometheus.counter :connect_total, docstring: 'A counter of COUNNECT from tlsproxy' @disconnect = prometheus.counter :disconnect_total, docstring: 'A counter of DISCOUNNECT from tlsproxy' - @tls = prometheus.counter :tls_total, docstring: 'A counter of TLS connections to smtpd with TLS-version and cipher', labels: %i[trust tls cipher] + @tls = prometheus.counter :tls_total, docstring: 'A counter of TLS connections to smtpd with TLS-version and cipher', labels: %i[trust tls cipher] Collector::Postfix.tls_posibilities {|t, s, c| @tls.increment by: 0, labels: {trust: t, tls: s, cipher: c} } end def collect entry case entry.message - when /\ADISCONNECT / - @disconnect.increment - when /\ACONNECT / - @connect.increment + when /\ADISCONNECT / then @disconnect.increment + when /\ACONNECT / then @connect.increment when /\A([^ ]+) TLS connection established from .*: ([^ ]+) with cipher ([^ ]+) / @tls.increment labels: {trust: $1, tls: $2, cipher: $3} else @@ -303,7 +280,7 @@ class Collector::Postfix class Bounce def initialize store, prometheus @store = store - @non_delivery = prometheus.counter :sender_non_delivery_notification, docstring: 'A counter of notifications to sender because mail cannot deliveried' + @non_delivery = prometheus.counter :sender_non_delivery_notification, docstring: 'A counter of notifications to sender because mail cannot deliveried' end def collect entry @@ -320,12 +297,12 @@ class Collector::Postfix class SCache def initialize store, prometheus @store = store - @start = prometheus.gauge :start, docstring: 'Start timestamp of SCache statistics' - @domain_lookup_hits = prometheus.counter :domain_lookup_hits_total, docstring: 'Count of hits for domain lookups of SCache' - @domain_lookup_miss = prometheus.counter :domain_lookup_miss_total, docstring: 'Count of misses for domain lookups of SCache' - @domain_lookup_success_rate = prometheus.gauge :domain_lookup_success_rate, docstring: 'Success rate of domain lookups of SCache' - @max_simultaneaus_domains = prometheus.gauge :max_simultaneaus_domains, docstring: 'Max simultaneaus connections Domains of SCache' - @max_simultaneaus_addresses = prometheus.gauge :max_simultaneaus_addresses, docstring: 'Max simultaneaus connections Addresses of SCache' + @start = prometheus.gauge :start, docstring: 'Start timestamp of SCache statistics' + @domain_lookup_hits = prometheus.counter :domain_lookup_hits_total, docstring: 'Count of hits for domain lookups of SCache' + @domain_lookup_miss = prometheus.counter :domain_lookup_miss_total, docstring: 'Count of misses for domain lookups of SCache' + @domain_lookup_success_rate = prometheus.gauge :domain_lookup_success_rate, docstring: 'Success rate of domain lookups of SCache' + @max_simultaneaus_domains = prometheus.gauge :max_simultaneaus_domains, docstring: 'Max simultaneaus connections Domains of SCache' + @max_simultaneaus_addresses = prometheus.gauge :max_simultaneaus_addresses, docstring: 'Max simultaneaus connections Addresses of SCache' @max_simultaneaus_connections = prometheus.gauge :max_simultaneaus_connections, docstring: 'Max simultaneaus connections of SCache' end @@ -358,8 +335,7 @@ class Collector::Postfix def collect entry case entry.message - when /\Acache / - @cache.collect entry + when /\Acache / then @cache.collect entry else STDERR.puts "# #{entry._systemd_unit} #{entry.syslog_identifier}: #{entry.message}" end @@ -375,55 +351,35 @@ class Collector::Postfix @tlsproxy = Tlsproxy.new store, Collector::PrefixProxy.new( prometheus, :tlsproxy) @scache = SCache.new store, Collector::PrefixProxy.new( prometheus, :scache) @bounce = Bounce.new store, Collector::PrefixProxy.new( prometheus, :bounce) - @qmgr = prometheus.counter :qmgr_total, docstring: 'A counter of qmgr actions' - @cleanup = prometheus.counter :cleanup_total, docstring: 'A counter of cleanup actions' - @lmtp = prometheus.counter :lmtp_total, docstring: 'A counter of ltmp actions' - @pickup = prometheus.counter :pickup_total, docstring: 'A counter of pickup actions' - @spf = prometheus.counter :spf_total, docstring: 'A counter of prepended SPF header-lines with state', labels: %i[status] + @qmgr = prometheus.counter :qmgr_total, docstring: 'A counter of qmgr actions' + @cleanup = prometheus.counter :cleanup_total, docstring: 'A counter of cleanup actions' + @lmtp = prometheus.counter :lmtp_total, docstring: 'A counter of ltmp actions' + @pickup = prometheus.counter :pickup_total, docstring: 'A counter of pickup actions' + @dnsblog = prometheus.counter :dnsblog_total, docstring: 'A counter for DNS-Blacklisted IP by DNSBL', labels: %i[dnsbl] @spf_fail = prometheus.counter :spf_fail_total, docstring: 'A counter of policyd-SPF failed SPF' + @spf = prometheus.counter :spf_total, docstring: 'A counter of prepended SPF header-lines with state', labels: %i[status] %w[pass none].each {|s| @spf.increment by: 0, labels: {status: s} } - @dnsblog = prometheus.counter :dnsblog_total, docstring: 'A counter for DNS-Blacklisted IP by DNSBL', labels: %i[dnsbl] - @anvil_max_connection_rate_per_minute = prometheus.gauge :anvil_max_connection_per_minute_tate, docstring: "Rate of max connections per minute to listener", labels: %i{listener} - @anvil_max_connection_count = prometheus.gauge :anvil_max_connection_count, docstring: "Count of max connections to listener", labels: %i{listener} - @anvil_max_cache_size = prometheus.gauge :anvil_max_cache_size, docstring: "Current max cache size" + @anvil_max_connection_rate_per_minute = prometheus.gauge :anvil_max_connection_per_minute_rate, docstring: "Rate of max connections per minute to listener", labels: %i{listener} + @anvil_max_connection_count = prometheus.gauge :anvil_max_connection_count, docstring: "Count of max connections to listener", labels: %i{listener} + @anvil_max_cache_size = prometheus.gauge :anvil_max_cache_size, docstring: "Current max cache size" end def collect entry #STDERR.puts "postfix: #{entry.syslog_identifier}: #{entry.message}" case entry.syslog_identifier - when 'postfix/tlsproxy/tlsproxy' - @tlsproxy.collect entry - when 'postfix/smtp/postscreen', 'postfix/smtp/postscreen' - @postscreen.collect entry - when 'postfix/smtp', 'postfix/smtp/smtp' - @smtp.collect entry - when 'postfix/smtpd', 'postfix/smtpd/smtpd' - @smtpd.collect entry - when 'postfix/submission/smtpd' - @submission.collect entry - when 'postfix/bounce', 'postfix/bounce/bounce' - @bounce.collect entry - when 'postifx/verify', 'postfix/verify/verify' - @verify.collect entry - when 'policyd-spf' - case entry.message - when /\Aprepend Received-SPF: (\w+) / - @spf.increment labels: {status: $1.downcase} - when /\A[^ ]+ [^ ]+ Message rejected due to: SPF fail / - # postfix@-.service policyd-spf: 550 5.7.23 Message rejected due to: SPF fail - not authorized. - @spf_fail.increment - else - STDERR.puts "# #{entry._systemd_unit} #{entry.syslog_identifier}: #{entry.message}" - end - when 'postfix/cleanup', 'postfix/cleanup/cleanup' - @cleanup.increment - when 'postfix/qmgr', 'postfix/qmgr/qmgr' - @qmgr.increment - when 'postfix/lmtp', 'postfix/lmtp/lmtp' - @lmtp.increment - when 'postfix/pickup', 'postfix/pickup/pickup' - @pickup.increment - when 'postfix/dnsblog', 'postfix/dnsblog/dnsblog' + when 'postfix/tlsproxy', 'postfix/tlsproxy/tlsproxy' then @tlsproxy.collect entry + when 'postfix/postscreen', 'postfix/smtp/postscreen' then @postscreen.collect entry + when 'postfix/smtp', 'postfix/smtp/smtp' then @smtp.collect entry + when 'postfix/smtpd', 'postfix/smtpd/smtpd' then @smtpd.collect entry + when 'postfix/submission/smtpd' then @submission.collect entry + when 'postfix/bounce', 'postfix/bounce/bounce' then @bounce.collect entry + when 'postifx/verify', 'postfix/verify/verify' then @verify.collect entry + when 'postfix/cleanup', 'postfix/cleanup/cleanup' then @cleanup.increment + when 'postfix/qmgr', 'postfix/qmgr/qmgr' then @qmgr.increment + when 'postfix/lmtp', 'postfix/lmtp/lmtp' then @lmtp.increment + when 'postfix/pickup', 'postfix/pickup/pickup' then @pickup.increment + when 'postfix/scache', 'postfix/scache/scache' then @scache.collect entry + when 'postfix/dnsblog', 'postfix/dnsblog/dnsblog' case entry.message when /\Aaddr [^ ]+ listed by domain ([^ ]+) as / @dnsblog.increment labels: {dnsbl: $1.downcase} @@ -441,8 +397,16 @@ class Collector::Postfix else STDERR.puts "# #{entry._systemd_unit} #{entry.syslog_identifier}: #{entry.message}" end - when 'postfix/scache', 'postfix/scache/scache' - @scache.collect entry + when 'policyd-spf' + case entry.message + when /\Aprepend Received-SPF: (\w+) / + @spf.increment labels: {status: $1.downcase} + when /\A[^ ]+ [^ ]+ Message rejected due to: SPF fail / + # postfix@-.service policyd-spf: 550 5.7.23 Message rejected due to: SPF fail - not authorized. + @spf_fail.increment + else + STDERR.puts "# #{entry._systemd_unit} #{entry.syslog_identifier}: #{entry.message}" + end else STDERR.puts "# #{entry._systemd_unit}: #{entry.syslog_identifier} #{entry.message}" end