mirror of
https://github.com/cmur2/dyndnsd.git
synced 2025-08-09 04:48:39 +02:00
Compare commits
51 Commits
v2.0.0.rc1
...
v2.1.0
Author | SHA1 | Date | |
---|---|---|---|
0317189057 | |||
73dbf2a5fa | |||
6d0457d70c | |||
9ab080072f | |||
fdcd6e8da4 | |||
31be09c1c2 | |||
3406e22588 | |||
![]() |
a42a864f56 | ||
![]() |
07fd8681e4 | ||
![]() |
6831744e7b | ||
![]() |
7d49b861fc | ||
![]() |
2ad816b866 | ||
![]() |
f4f10c94c4 | ||
![]() |
bb7302407a | ||
![]() |
9f132b4a7d | ||
![]() |
32d6d01d9d | ||
![]() |
9d80220d24 | ||
![]() |
b00aa0e464 | ||
![]() |
a55d882d4d | ||
![]() |
aa5de52f4d | ||
![]() |
ee56fe8d07 | ||
![]() |
051d561700 | ||
![]() |
ccaa39e871 | ||
![]() |
b69aa6dc5b | ||
7a03415dd9 | |||
77914c42e1 | |||
![]() |
a9d1cc644a | ||
![]() |
902e5868b3 | ||
ffe01467c5 | |||
79db1b1911 | |||
![]() |
f37a77094f | ||
![]() |
2e233422d3 | ||
![]() |
6f044ec5f2 | ||
![]() |
988e30dac9 | ||
![]() |
31233df5ee | ||
1f35f60c55 | |||
![]() |
6310343e45 | ||
![]() |
1dd0eb7d1a | ||
280bfbeb82 | |||
![]() |
bd7c786c9c | ||
![]() |
4f3e591f13 | ||
![]() |
c1b5e14862 | ||
![]() |
d1dc91ed8d | ||
![]() |
14fe8ebedf | ||
![]() |
5264b3e6f1 | ||
d2ac6890aa | |||
a01276c348 | |||
![]() |
22d686dec7 | ||
fe019515eb | |||
3d64c2f2a3 | |||
472d9aaa98 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,3 +1,4 @@
|
|||||||
.DS_Store
|
.DS_Store
|
||||||
*.lock
|
*.lock
|
||||||
pkg/*
|
pkg/*
|
||||||
|
.yardoc
|
||||||
|
35
.rubocop.yml
35
.rubocop.yml
@@ -1,15 +1,25 @@
|
|||||||
inherit_from: .rubocop_todo.yml
|
|
||||||
|
|
||||||
AllCops:
|
AllCops:
|
||||||
TargetRubyVersion: '2.3'
|
TargetRubyVersion: '2.3'
|
||||||
|
|
||||||
Gemspec/OrderedDependencies:
|
Gemspec/OrderedDependencies:
|
||||||
Enabled: false
|
Enabled: false
|
||||||
|
|
||||||
|
Layout/EmptyLineAfterGuardClause:
|
||||||
|
Enabled: false
|
||||||
|
|
||||||
# allows nicer usage of private_class_method
|
# allows nicer usage of private_class_method
|
||||||
Layout/EmptyLinesAroundArguments:
|
Layout/EmptyLinesAroundArguments:
|
||||||
Enabled: false
|
Enabled: false
|
||||||
|
|
||||||
|
Layout/HashAlignment:
|
||||||
|
Enabled: false
|
||||||
|
|
||||||
|
Layout/LeadingEmptyLines:
|
||||||
|
Enabled: false
|
||||||
|
|
||||||
|
Layout/LineLength:
|
||||||
|
Max: 200
|
||||||
|
|
||||||
Layout/SpaceInsideHashLiteralBraces:
|
Layout/SpaceInsideHashLiteralBraces:
|
||||||
Enabled: false
|
Enabled: false
|
||||||
|
|
||||||
@@ -25,15 +35,18 @@ Metrics/ClassLength:
|
|||||||
Metrics/CyclomaticComplexity:
|
Metrics/CyclomaticComplexity:
|
||||||
Enabled: false
|
Enabled: false
|
||||||
|
|
||||||
Metrics/LineLength:
|
|
||||||
Max: 200
|
|
||||||
|
|
||||||
Metrics/MethodLength:
|
Metrics/MethodLength:
|
||||||
Enabled: false
|
Enabled: false
|
||||||
|
|
||||||
Metrics/PerceivedComplexity:
|
Metrics/PerceivedComplexity:
|
||||||
Enabled: false
|
Enabled: false
|
||||||
|
|
||||||
|
Naming/MethodParameterName:
|
||||||
|
Enabled: false
|
||||||
|
|
||||||
|
Naming/MemoizedInstanceVariableName:
|
||||||
|
Enabled: false
|
||||||
|
|
||||||
Style/ConditionalAssignment:
|
Style/ConditionalAssignment:
|
||||||
Enabled: false
|
Enabled: false
|
||||||
|
|
||||||
@@ -49,6 +62,15 @@ Style/FrozenStringLiteralComment:
|
|||||||
Style/GuardClause:
|
Style/GuardClause:
|
||||||
Enabled: false
|
Enabled: false
|
||||||
|
|
||||||
|
Style/HashEachMethods:
|
||||||
|
Enabled: true
|
||||||
|
|
||||||
|
Style/HashTransformKeys:
|
||||||
|
Enabled: true
|
||||||
|
|
||||||
|
Style/HashTransformValues:
|
||||||
|
Enabled: true
|
||||||
|
|
||||||
Style/IdenticalConditionalBranches:
|
Style/IdenticalConditionalBranches:
|
||||||
Enabled: false
|
Enabled: false
|
||||||
|
|
||||||
@@ -58,5 +80,8 @@ Style/InverseMethods:
|
|||||||
Style/NegatedIf:
|
Style/NegatedIf:
|
||||||
Enabled: false
|
Enabled: false
|
||||||
|
|
||||||
|
Style/RescueModifier:
|
||||||
|
Enabled: false
|
||||||
|
|
||||||
Style/SymbolArray:
|
Style/SymbolArray:
|
||||||
Enabled: false
|
Enabled: false
|
||||||
|
@@ -1,7 +0,0 @@
|
|||||||
# This configuration was generated by
|
|
||||||
# `rubocop --auto-gen-config`
|
|
||||||
# on 2018-02-23 12:54:10 +0100 using RuboCop version 0.52.1.
|
|
||||||
# The point is for the user to remove these configuration records
|
|
||||||
# one by one as the offenses are removed from the code base.
|
|
||||||
# Note that changes in the inspected code, or installation of new
|
|
||||||
# versions of RuboCop, may require this file to be generated again.
|
|
16
.solargraph.yml
Normal file
16
.solargraph.yml
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
---
|
||||||
|
include:
|
||||||
|
- "**/*.rb"
|
||||||
|
- "bin/dyndnsd"
|
||||||
|
exclude:
|
||||||
|
- spec/**/*
|
||||||
|
- test/**/*
|
||||||
|
- vendor/**/*
|
||||||
|
- ".bundle/**/*"
|
||||||
|
require: []
|
||||||
|
domains: []
|
||||||
|
reporters:
|
||||||
|
- rubocop
|
||||||
|
- require_not_found
|
||||||
|
require_paths: []
|
||||||
|
max_files: 5000
|
13
.travis.yml
13
.travis.yml
@@ -1,7 +1,12 @@
|
|||||||
---
|
---
|
||||||
sudo: false
|
os: linux
|
||||||
language: ruby
|
language: ruby
|
||||||
rvm:
|
rvm:
|
||||||
- 2.5
|
- 2.7
|
||||||
- 2.4
|
- 2.6
|
||||||
- 2.3
|
- 2.5
|
||||||
|
- 2.4
|
||||||
|
- 2.3
|
||||||
|
|
||||||
|
script:
|
||||||
|
- bundle exec rake travis
|
||||||
|
15
CHANGELOG.md
15
CHANGELOG.md
@@ -1,6 +1,14 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
## 2.0.0
|
## 2.1.0 (March 1, 2020)
|
||||||
|
|
||||||
|
IMPROVEMENTS:
|
||||||
|
|
||||||
|
- Add Ruby 2.7 support
|
||||||
|
- Add [solargraph](https://github.com/castwide/solargraph) to dev tooling as Ruby Language Server usable e.g. for IDEs (used solargraph version not compatible with Ruby 2.7 as bundler-audit 0.6.x requires old `thor` gem)
|
||||||
|
- Document code using YARD tags, e.g. for type information and better code completion
|
||||||
|
|
||||||
|
## 2.0.0 (January 25, 2019)
|
||||||
|
|
||||||
IMPROVEMENTS:
|
IMPROVEMENTS:
|
||||||
|
|
||||||
@@ -8,9 +16,10 @@ IMPROVEMENTS:
|
|||||||
- Better protocol compliance by returning `badauth` in response body on HTTP 401 errors
|
- Better protocol compliance by returning `badauth` in response body on HTTP 401 errors
|
||||||
- Better code maintainability by refactorings
|
- Better code maintainability by refactorings
|
||||||
- Update dependencies, mainly `rack` to new major version 2
|
- Update dependencies, mainly `rack` to new major version 2
|
||||||
- Add Ruby 2.5 support
|
- Add Ruby 2.5 and Ruby 2.6 support
|
||||||
- Add experimental [OpenTracing](http://opentracing.io/) support with [CNCF Jaeger](https://github.com/jaegertracing/jaeger)
|
- Add experimental [OpenTracing](https://opentracing.io/) support with [CNCF Jaeger](https://github.com/jaegertracing/jaeger)
|
||||||
- Support host offlining by deleting the associated DNS records
|
- Support host offlining by deleting the associated DNS records
|
||||||
|
- Add textfile reporter to write Graphite-style metrics (also compatible with [Prometheus](https://prometheus.io/)) into a file
|
||||||
|
|
||||||
## 1.6.1 (October 31, 2017)
|
## 1.6.1 (October 31, 2017)
|
||||||
|
|
||||||
|
@@ -135,7 +135,7 @@ The [Debian 6 init.d script](init.d/debian-6-dyndnsd) assumes that dyndnsd.rb is
|
|||||||
|
|
||||||
### Monitoring
|
### Monitoring
|
||||||
|
|
||||||
For monitoring dyndnsd.rb uses the [metriks](https://github.com/eric/metriks) framework and exposes several metrics like the number of unauthenticated requests, requests that did (not) update a hostname, etc. By default the most important metrics are shown in the [proctitle](https://github.com/eric/metriks#proc-title-reporter) but you can also configure a [Graphite](https://graphiteapp.org/) backend for central monitoring.
|
For monitoring dyndnsd.rb uses the [metriks](https://github.com/eric/metriks) framework and exposes several metrics like the number of unauthenticated requests, requests that did (not) update a hostname, etc. By default the most important metrics are shown in the [proctitle](https://github.com/eric/metriks#proc-title-reporter) but you can also configure a [Graphite](https://graphiteapp.org/) backend for central monitoring or the [textfile_reporter](https://github.com/prometheus/node_exporter/#textfile-collector) which outputs Graphite-style metrics that are also compatible with Prometheus to a file.
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
host: "0.0.0.0"
|
host: "0.0.0.0"
|
||||||
@@ -147,6 +147,10 @@ graphite:
|
|||||||
host: localhost # defaults for host and port of a carbon server
|
host: localhost # defaults for host and port of a carbon server
|
||||||
port: 2003
|
port: 2003
|
||||||
prefix: "my.graphite.metrics.naming.structure.dyndnsd"
|
prefix: "my.graphite.metrics.naming.structure.dyndnsd"
|
||||||
|
# OR configure the textfile reporter instead of Graphite/proctitle
|
||||||
|
textfile:
|
||||||
|
file: /path/to/file.prom
|
||||||
|
prefix: "my.graphite.metrics.naming.structure.dyndnsd"
|
||||||
# configure the updater, here we use command_with_bind_zone, params are updater-specific
|
# configure the updater, here we use command_with_bind_zone, params are updater-specific
|
||||||
updater:
|
updater:
|
||||||
name: "command_with_bind_zone"
|
name: "command_with_bind_zone"
|
||||||
|
16
Rakefile
16
Rakefile
@@ -1,8 +1,22 @@
|
|||||||
require 'bundler/gem_tasks'
|
require 'bundler/gem_tasks'
|
||||||
require 'rspec/core/rake_task'
|
require 'rspec/core/rake_task'
|
||||||
require 'rubocop/rake_task'
|
require 'rubocop/rake_task'
|
||||||
|
require 'bundler/audit/task'
|
||||||
|
|
||||||
RSpec::Core::RakeTask.new(:spec)
|
RSpec::Core::RakeTask.new(:spec)
|
||||||
RuboCop::RakeTask.new
|
RuboCop::RakeTask.new
|
||||||
|
Bundler::Audit::Task.new
|
||||||
|
|
||||||
task default: [:rubocop, :spec]
|
desc 'Should be run by developer once to prepare initial solargraph usage (fill caches etc.)'
|
||||||
|
task :'solargraph:init' do
|
||||||
|
sh 'solargraph download-core'
|
||||||
|
end
|
||||||
|
|
||||||
|
desc 'Run experimental solargraph type checker'
|
||||||
|
task :'solargraph:tc' do
|
||||||
|
sh 'solargraph typecheck'
|
||||||
|
end
|
||||||
|
|
||||||
|
task default: [:rubocop, :spec, 'bundle:audit']
|
||||||
|
|
||||||
|
task travis: [:default, :'solargraph:tc']
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
|
|
||||||
$LOAD_PATH.push File.expand_path('../lib', __FILE__)
|
$LOAD_PATH.push File.expand_path('lib', __dir__)
|
||||||
|
|
||||||
require 'dyndnsd/version'
|
require 'dyndnsd/version'
|
||||||
|
|
||||||
@@ -23,14 +23,15 @@ Gem::Specification.new do |s|
|
|||||||
s.add_runtime_dependency 'rack', '~> 2.0'
|
s.add_runtime_dependency 'rack', '~> 2.0'
|
||||||
s.add_runtime_dependency 'json'
|
s.add_runtime_dependency 'json'
|
||||||
s.add_runtime_dependency 'metriks'
|
s.add_runtime_dependency 'metriks'
|
||||||
s.add_runtime_dependency 'opentracing', '~> 0.3'
|
s.add_runtime_dependency 'opentracing', '~> 0.5.0'
|
||||||
s.add_runtime_dependency 'rack-tracer', '~> 0.4'
|
s.add_runtime_dependency 'rack-tracer', '~> 0.9.0'
|
||||||
s.add_runtime_dependency 'spanmanager', '~> 0.3'
|
s.add_runtime_dependency 'jaeger-client', '~> 0.10.0'
|
||||||
s.add_runtime_dependency 'jaeger-client', '~> 0.4'
|
|
||||||
|
|
||||||
s.add_development_dependency 'bundler'
|
s.add_development_dependency 'bundler'
|
||||||
s.add_development_dependency 'rake'
|
s.add_development_dependency 'rake'
|
||||||
s.add_development_dependency 'rspec'
|
s.add_development_dependency 'rspec'
|
||||||
s.add_development_dependency 'rack-test'
|
s.add_development_dependency 'rack-test'
|
||||||
s.add_development_dependency 'rubocop', '~> 0.52.1'
|
s.add_development_dependency 'rubocop', '~> 0.80.0'
|
||||||
|
s.add_development_dependency 'bundler-audit', '~> 0.6.0'
|
||||||
|
s.add_development_dependency 'solargraph'
|
||||||
end
|
end
|
||||||
|
@@ -10,7 +10,6 @@ require 'metriks'
|
|||||||
require 'metriks/reporter/graphite'
|
require 'metriks/reporter/graphite'
|
||||||
require 'opentracing'
|
require 'opentracing'
|
||||||
require 'rack/tracer'
|
require 'rack/tracer'
|
||||||
require 'spanmanager'
|
|
||||||
|
|
||||||
require 'dyndnsd/generator/bind'
|
require 'dyndnsd/generator/bind'
|
||||||
require 'dyndnsd/updater/command_with_bind_zone'
|
require 'dyndnsd/updater/command_with_bind_zone'
|
||||||
@@ -18,24 +17,36 @@ require 'dyndnsd/responder/dyndns_style'
|
|||||||
require 'dyndnsd/responder/rest_style'
|
require 'dyndnsd/responder/rest_style'
|
||||||
require 'dyndnsd/database'
|
require 'dyndnsd/database'
|
||||||
require 'dyndnsd/helper'
|
require 'dyndnsd/helper'
|
||||||
|
require 'dyndnsd/textfile_reporter'
|
||||||
require 'dyndnsd/version'
|
require 'dyndnsd/version'
|
||||||
|
|
||||||
module Dyndnsd
|
module Dyndnsd
|
||||||
|
# @return [Logger]
|
||||||
def self.logger
|
def self.logger
|
||||||
@logger
|
@logger
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @param logger [Logger]
|
||||||
|
# @return [Logger]
|
||||||
def self.logger=(logger)
|
def self.logger=(logger)
|
||||||
@logger = logger
|
@logger = logger
|
||||||
end
|
end
|
||||||
|
|
||||||
class LogFormatter
|
class LogFormatter
|
||||||
|
# @param lvl [Object]
|
||||||
|
# @param _time [DateTime]
|
||||||
|
# @param _progname [String]
|
||||||
|
# @param msg [Object]
|
||||||
|
# @return [String]
|
||||||
def call(lvl, _time, _progname, msg)
|
def call(lvl, _time, _progname, msg)
|
||||||
format("[%s] %-5s %s\n", Time.now.strftime('%Y-%m-%d %H:%M:%S'), lvl, msg.to_s)
|
format("[%s] %-5s %s\n", Time.now.strftime('%Y-%m-%d %H:%M:%S'), lvl, msg.to_s)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Daemon
|
class Daemon
|
||||||
|
# @param config [Hash{String => Object}]
|
||||||
|
# @param db [Dyndnsd::Database]
|
||||||
|
# @param updater [#update]
|
||||||
def initialize(config, db, updater)
|
def initialize(config, db, updater)
|
||||||
@users = config['users']
|
@users = config['users']
|
||||||
@domain = config['domain']
|
@domain = config['domain']
|
||||||
@@ -51,6 +62,9 @@ module Dyndnsd
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @param username [String]
|
||||||
|
# @param password [String]
|
||||||
|
# @return [Boolean]
|
||||||
def authorized?(username, password)
|
def authorized?(username, password)
|
||||||
Helper.span('check_authorized') do |span|
|
Helper.span('check_authorized') do |span|
|
||||||
span.set_tag('dyndnsd.user', username)
|
span.set_tag('dyndnsd.user', username)
|
||||||
@@ -64,6 +78,8 @@ module Dyndnsd
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @param env [Hash{String => String}]
|
||||||
|
# @return [Array{Integer,Hash{String => String},Array{String}}]
|
||||||
def call(env)
|
def call(env)
|
||||||
return [422, {'X-DynDNS-Response' => 'method_forbidden'}, []] if env['REQUEST_METHOD'] != 'GET'
|
return [422, {'X-DynDNS-Response' => 'method_forbidden'}, []] if env['REQUEST_METHOD'] != 'GET'
|
||||||
return [422, {'X-DynDNS-Response' => 'not_found'}, []] if env['PATH_INFO'] != '/nic/update'
|
return [422, {'X-DynDNS-Response' => 'not_found'}, []] if env['PATH_INFO'] != '/nic/update'
|
||||||
@@ -71,6 +87,7 @@ module Dyndnsd
|
|||||||
handle_dyndns_request(env)
|
handle_dyndns_request(env)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @return [void]
|
||||||
def self.run!
|
def self.run!
|
||||||
if ARGV.length != 1
|
if ARGV.length != 1
|
||||||
puts 'Usage: dyndnsd config_file'
|
puts 'Usage: dyndnsd config_file'
|
||||||
@@ -109,6 +126,8 @@ module Dyndnsd
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
# @param params [Hash{String => String}]
|
||||||
|
# @return [Array{String}]
|
||||||
def extract_v4_and_v6_address(params)
|
def extract_v4_and_v6_address(params)
|
||||||
return [] if !(params['myip'])
|
return [] if !(params['myip'])
|
||||||
begin
|
begin
|
||||||
@@ -120,6 +139,9 @@ module Dyndnsd
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @param env [Hash{String => String}]
|
||||||
|
# @param params [Hash{String => String}]
|
||||||
|
# @return [Array{String}]
|
||||||
def extract_myips(env, params)
|
def extract_myips(env, params)
|
||||||
# require presence of myip parameter as valid IPAddr (v4) and valid myip6
|
# require presence of myip parameter as valid IPAddr (v4) and valid myip6
|
||||||
return extract_v4_and_v6_address(params) if params.key?('myip6')
|
return extract_v4_and_v6_address(params) if params.key?('myip6')
|
||||||
@@ -134,6 +156,9 @@ module Dyndnsd
|
|||||||
[env['REMOTE_ADDR']]
|
[env['REMOTE_ADDR']]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @param hostnames [String]
|
||||||
|
# @param myips [Array{String}]
|
||||||
|
# @return [Array{Symbol}]
|
||||||
def process_changes(hostnames, myips)
|
def process_changes(hostnames, myips)
|
||||||
changes = []
|
changes = []
|
||||||
Helper.span('process_changes') do |span|
|
Helper.span('process_changes') do |span|
|
||||||
@@ -158,6 +183,7 @@ module Dyndnsd
|
|||||||
changes
|
changes
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @return [void]
|
||||||
def update_db
|
def update_db
|
||||||
@db['serial'] += 1
|
@db['serial'] += 1
|
||||||
Dyndnsd.logger.info "Committing update ##{@db['serial']}"
|
Dyndnsd.logger.info "Committing update ##{@db['serial']}"
|
||||||
@@ -166,6 +192,8 @@ module Dyndnsd
|
|||||||
Metriks.meter('updates.committed').mark
|
Metriks.meter('updates.committed').mark
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @param env [Hash{String => String}]
|
||||||
|
# @return [Array{Integer,Hash{String => String},Array{String}}]
|
||||||
def handle_dyndns_request(env)
|
def handle_dyndns_request(env)
|
||||||
params = Rack::Utils.parse_query(env['QUERY_STRING'])
|
params = Rack::Utils.parse_query(env['QUERY_STRING'])
|
||||||
|
|
||||||
@@ -204,6 +232,8 @@ module Dyndnsd
|
|||||||
|
|
||||||
# SETUP
|
# SETUP
|
||||||
|
|
||||||
|
# @param config [Hash{String => Object}]
|
||||||
|
# @return [void]
|
||||||
private_class_method def self.setup_logger(config)
|
private_class_method def self.setup_logger(config)
|
||||||
if config['logfile']
|
if config['logfile']
|
||||||
Dyndnsd.logger = Logger.new(config['logfile'])
|
Dyndnsd.logger = Logger.new(config['logfile'])
|
||||||
@@ -215,6 +245,7 @@ module Dyndnsd
|
|||||||
Dyndnsd.logger.formatter = LogFormatter.new
|
Dyndnsd.logger.formatter = LogFormatter.new
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @return [void]
|
||||||
private_class_method def self.setup_traps
|
private_class_method def self.setup_traps
|
||||||
Signal.trap('INT') do
|
Signal.trap('INT') do
|
||||||
Dyndnsd.logger.info 'Quitting...'
|
Dyndnsd.logger.info 'Quitting...'
|
||||||
@@ -226,6 +257,8 @@ module Dyndnsd
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @param config [Hash{String => Object}]
|
||||||
|
# @return [void]
|
||||||
private_class_method def self.setup_monitoring(config)
|
private_class_method def self.setup_monitoring(config)
|
||||||
# configure metriks
|
# configure metriks
|
||||||
if config['graphite']
|
if config['graphite']
|
||||||
@@ -235,6 +268,12 @@ module Dyndnsd
|
|||||||
options[:prefix] = config['graphite']['prefix'] if config['graphite']['prefix']
|
options[:prefix] = config['graphite']['prefix'] if config['graphite']['prefix']
|
||||||
reporter = Metriks::Reporter::Graphite.new(host, port, options)
|
reporter = Metriks::Reporter::Graphite.new(host, port, options)
|
||||||
reporter.start
|
reporter.start
|
||||||
|
elsif config['textfile']
|
||||||
|
file = config['textfile']['file'] || '/tmp/dyndnsd-metrics.prom'
|
||||||
|
options = {}
|
||||||
|
options[:prefix] = config['textfile']['prefix'] if config['textfile']['prefix']
|
||||||
|
reporter = Dyndnsd::TextfileReporter.new(file, options)
|
||||||
|
reporter.start
|
||||||
else
|
else
|
||||||
reporter = Metriks::Reporter::ProcTitle.new
|
reporter = Metriks::Reporter::ProcTitle.new
|
||||||
reporter.add 'good', 'sec' do
|
reporter.add 'good', 'sec' do
|
||||||
@@ -247,6 +286,8 @@ module Dyndnsd
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @param config [Hash{String => Object}]
|
||||||
|
# @return [void]
|
||||||
private_class_method def self.setup_tracing(config)
|
private_class_method def self.setup_tracing(config)
|
||||||
# configure OpenTracing
|
# configure OpenTracing
|
||||||
if config.dig('tracing', 'jaeger')
|
if config.dig('tracing', 'jaeger')
|
||||||
@@ -259,14 +300,14 @@ module Dyndnsd
|
|||||||
host: host, port: port, service_name: service_name, flush_interval: 1
|
host: host, port: port, service_name: service_name, flush_interval: 1
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
# always use SpanManager
|
|
||||||
OpenTracing.global_tracer = SpanManager::Tracer.new(OpenTracing.global_tracer)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @param config [Hash{String => Object}]
|
||||||
|
# @return [void]
|
||||||
private_class_method def self.setup_rack(config)
|
private_class_method def self.setup_rack(config)
|
||||||
# configure daemon
|
# configure daemon
|
||||||
db = Database.new(config['db'])
|
db = Database.new(config['db'])
|
||||||
updater = Updater::CommandWithBindZone.new(config['domain'], config['updater']['params']) if config['updater']['name'] == 'command_with_bind_zone'
|
updater = Updater::CommandWithBindZone.new(config['domain'], config.dig('updater', 'params')) if config.dig('updater', 'name') == 'command_with_bind_zone'
|
||||||
daemon = Daemon.new(config, db, updater)
|
daemon = Daemon.new(config, db, updater)
|
||||||
|
|
||||||
# configure rack
|
# configure rack
|
||||||
|
@@ -7,10 +7,12 @@ module Dyndnsd
|
|||||||
|
|
||||||
def_delegators :@db, :[], :[]=, :each, :has_key?
|
def_delegators :@db, :[], :[]=, :each, :has_key?
|
||||||
|
|
||||||
|
# @param db_file [String]
|
||||||
def initialize(db_file)
|
def initialize(db_file)
|
||||||
@db_file = db_file
|
@db_file = db_file
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @return [void]
|
||||||
def load
|
def load
|
||||||
if File.file?(@db_file)
|
if File.file?(@db_file)
|
||||||
@db = JSON.parse(File.open(@db_file, 'r', &:read))
|
@db = JSON.parse(File.open(@db_file, 'r', &:read))
|
||||||
@@ -20,6 +22,7 @@ module Dyndnsd
|
|||||||
@db_hash = @db.hash
|
@db_hash = @db.hash
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @return [void]
|
||||||
def save
|
def save
|
||||||
Helper.span('database_save') do |_span|
|
Helper.span('database_save') do |_span|
|
||||||
File.open(@db_file, 'w') { |f| JSON.dump(@db, f) }
|
File.open(@db_file, 'w') { |f| JSON.dump(@db, f) }
|
||||||
@@ -27,6 +30,7 @@ module Dyndnsd
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @return [Boolean]
|
||||||
def changed?
|
def changed?
|
||||||
@db_hash != @db.hash
|
@db_hash != @db.hash
|
||||||
end
|
end
|
||||||
|
@@ -2,6 +2,8 @@
|
|||||||
module Dyndnsd
|
module Dyndnsd
|
||||||
module Generator
|
module Generator
|
||||||
class Bind
|
class Bind
|
||||||
|
# @param domain [String]
|
||||||
|
# @param config [Hash{String => Object}]
|
||||||
def initialize(domain, config)
|
def initialize(domain, config)
|
||||||
@domain = domain
|
@domain = domain
|
||||||
@ttl = config['ttl']
|
@ttl = config['ttl']
|
||||||
@@ -10,6 +12,8 @@ module Dyndnsd
|
|||||||
@additional_zone_content = config['additional_zone_content']
|
@additional_zone_content = config['additional_zone_content']
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @param db [Dyndnsd::Database]
|
||||||
|
# @return [String]
|
||||||
def generate(db)
|
def generate(db)
|
||||||
out = []
|
out = []
|
||||||
out << "$TTL #{@ttl}"
|
out << "$TTL #{@ttl}"
|
||||||
|
@@ -3,6 +3,9 @@ require 'ipaddr'
|
|||||||
|
|
||||||
module Dyndnsd
|
module Dyndnsd
|
||||||
class Helper
|
class Helper
|
||||||
|
# @param hostname [String]
|
||||||
|
# @param domain [String]
|
||||||
|
# @return [Boolean]
|
||||||
def self.fqdn_valid?(hostname, domain)
|
def self.fqdn_valid?(hostname, domain)
|
||||||
return false if hostname.length < domain.length + 2
|
return false if hostname.length < domain.length + 2
|
||||||
return false if !hostname.end_with?(domain)
|
return false if !hostname.end_with?(domain)
|
||||||
@@ -11,30 +14,54 @@ module Dyndnsd
|
|||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @param ip [String]
|
||||||
|
# @return [Boolean]
|
||||||
def self.ip_valid?(ip)
|
def self.ip_valid?(ip)
|
||||||
IPAddr.new(ip)
|
IPAddr.new(ip)
|
||||||
return true
|
true
|
||||||
rescue ArgumentError
|
rescue ArgumentError
|
||||||
return false
|
false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @param username [String]
|
||||||
|
# @param password [String]
|
||||||
|
# @param users [Hash]
|
||||||
|
# @return [Boolean]
|
||||||
def self.user_allowed?(username, password, users)
|
def self.user_allowed?(username, password, users)
|
||||||
(users.key? username) && (users[username]['password'] == password)
|
(users.key? username) && (users[username]['password'] == password)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @param hostname [String]
|
||||||
|
# @param myips [Array]
|
||||||
|
# @param hosts [Hash]
|
||||||
|
# @return [Boolean]
|
||||||
def self.changed?(hostname, myips, hosts)
|
def self.changed?(hostname, myips, hosts)
|
||||||
# myips order is always deterministic
|
# myips order is always deterministic
|
||||||
((!hosts.include? hostname) || (hosts[hostname] != myips)) && !myips.empty?
|
((!hosts.include? hostname) || (hosts[hostname] != myips)) && !myips.empty?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @param operation [String]
|
||||||
|
# @param block [Proc]
|
||||||
|
# @return [void]
|
||||||
def self.span(operation, &block)
|
def self.span(operation, &block)
|
||||||
span = OpenTracing.start_span(operation)
|
scope = OpenTracing.start_active_span(operation)
|
||||||
|
span = scope.span
|
||||||
span.set_tag('component', 'dyndnsd')
|
span.set_tag('component', 'dyndnsd')
|
||||||
span.set_tag('span.kind', 'server')
|
span.set_tag('span.kind', 'server')
|
||||||
begin
|
begin
|
||||||
block.call(span)
|
block.call(span)
|
||||||
|
rescue StandardError => e
|
||||||
|
span.set_tag('error', true)
|
||||||
|
span.log_kv(
|
||||||
|
event: 'error',
|
||||||
|
'error.kind': e.class.to_s,
|
||||||
|
'error.object': e,
|
||||||
|
message: e.message,
|
||||||
|
stack: e.backtrace.join("\n")
|
||||||
|
)
|
||||||
|
raise
|
||||||
ensure
|
ensure
|
||||||
span.finish
|
scope.close
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@@ -2,10 +2,13 @@
|
|||||||
module Dyndnsd
|
module Dyndnsd
|
||||||
module Responder
|
module Responder
|
||||||
class DynDNSStyle
|
class DynDNSStyle
|
||||||
|
# @param app [#call]
|
||||||
def initialize(app)
|
def initialize(app)
|
||||||
@app = app
|
@app = app
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @param env [Hash{String => String}]
|
||||||
|
# @return [Array{Integer,Hash{String => String},Array{String}}]
|
||||||
def call(env)
|
def call(env)
|
||||||
@app.call(env).tap do |status_code, headers, body|
|
@app.call(env).tap do |status_code, headers, body|
|
||||||
if headers.key?('X-DynDNS-Response')
|
if headers.key?('X-DynDNS-Response')
|
||||||
@@ -18,6 +21,10 @@ module Dyndnsd
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
# @param status_code [Integer]
|
||||||
|
# @param headers [Hash{String => String}]
|
||||||
|
# @param body [Array{String}]
|
||||||
|
# @return [Array{Integer,Hash{String => String},Array{String}}]
|
||||||
def decorate_dyndnsd_response(status_code, headers, body)
|
def decorate_dyndnsd_response(status_code, headers, body)
|
||||||
if status_code == 200
|
if status_code == 200
|
||||||
[200, {'Content-Type' => 'text/plain'}, [get_success_body(body[0], body[1])]]
|
[200, {'Content-Type' => 'text/plain'}, [get_success_body(body[0], body[1])]]
|
||||||
@@ -26,6 +33,10 @@ module Dyndnsd
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @param status_code [Integer]
|
||||||
|
# @param headers [Hash{String => String}]
|
||||||
|
# @param _body [Array{String}]
|
||||||
|
# @return [Array{Integer,Hash{String => String},Array{String}}]
|
||||||
def decorate_other_response(status_code, headers, _body)
|
def decorate_other_response(status_code, headers, _body)
|
||||||
if status_code == 400
|
if status_code == 400
|
||||||
[status_code, headers, ['Bad Request']]
|
[status_code, headers, ['Bad Request']]
|
||||||
@@ -34,10 +45,14 @@ module Dyndnsd
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @param changes [Array{Symbol}]
|
||||||
|
# @param myips [Array{String}]
|
||||||
|
# @return [String]
|
||||||
def get_success_body(changes, myips)
|
def get_success_body(changes, myips)
|
||||||
changes.map { |change| "#{change} #{myips.join(' ')}" }.join("\n")
|
changes.map { |change| "#{change} #{myips.join(' ')}" }.join("\n")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @return [Hash{String => Object}]
|
||||||
def error_response_map
|
def error_response_map
|
||||||
{
|
{
|
||||||
# general http errors
|
# general http errors
|
||||||
|
@@ -2,10 +2,13 @@
|
|||||||
module Dyndnsd
|
module Dyndnsd
|
||||||
module Responder
|
module Responder
|
||||||
class RestStyle
|
class RestStyle
|
||||||
|
# @param app [#call]
|
||||||
def initialize(app)
|
def initialize(app)
|
||||||
@app = app
|
@app = app
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @param env [Hash{String => String}]
|
||||||
|
# @return [Array{Integer,Hash{String => String},Array{String}}]
|
||||||
def call(env)
|
def call(env)
|
||||||
@app.call(env).tap do |status_code, headers, body|
|
@app.call(env).tap do |status_code, headers, body|
|
||||||
if headers.key?('X-DynDNS-Response')
|
if headers.key?('X-DynDNS-Response')
|
||||||
@@ -18,6 +21,10 @@ module Dyndnsd
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
# @param status_code [Integer]
|
||||||
|
# @param headers [Hash{String => String}]
|
||||||
|
# @param body [Array{String}]
|
||||||
|
# @return [Array{Integer,Hash{String => String},Array{String}}]
|
||||||
def decorate_dyndnsd_response(status_code, headers, body)
|
def decorate_dyndnsd_response(status_code, headers, body)
|
||||||
if status_code == 200
|
if status_code == 200
|
||||||
[200, {'Content-Type' => 'text/plain'}, [get_success_body(body[0], body[1])]]
|
[200, {'Content-Type' => 'text/plain'}, [get_success_body(body[0], body[1])]]
|
||||||
@@ -26,6 +33,10 @@ module Dyndnsd
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @param status_code [Integer]
|
||||||
|
# @param headers [Hash{String => String}]
|
||||||
|
# @param _body [Array{String}]
|
||||||
|
# @return [Array{Integer,Hash{String => String},Array{String}}]
|
||||||
def decorate_other_response(status_code, headers, _body)
|
def decorate_other_response(status_code, headers, _body)
|
||||||
if status_code == 400
|
if status_code == 400
|
||||||
[status_code, headers, ['Bad Request']]
|
[status_code, headers, ['Bad Request']]
|
||||||
@@ -34,10 +45,14 @@ module Dyndnsd
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @param changes [Array{Symbol}]
|
||||||
|
# @param myips [Array{String}]
|
||||||
|
# @return [String]
|
||||||
def get_success_body(changes, myips)
|
def get_success_body(changes, myips)
|
||||||
changes.map { |change| change == :good ? "Changed to #{myips.join(' ')}" : "No change needed for #{myips.join(' ')}" }.join("\n")
|
changes.map { |change| change == :good ? "Changed to #{myips.join(' ')}" : "No change needed for #{myips.join(' ')}" }.join("\n")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @return [Hash{String => Object}]
|
||||||
def error_response_map
|
def error_response_map
|
||||||
{
|
{
|
||||||
# general http errors
|
# general http errors
|
||||||
|
123
lib/dyndnsd/textfile_reporter.rb
Normal file
123
lib/dyndnsd/textfile_reporter.rb
Normal file
@@ -0,0 +1,123 @@
|
|||||||
|
|
||||||
|
# Adapted from https://github.com/eric/metriks-graphite/blob/master/lib/metriks/reporter/graphite.rb
|
||||||
|
|
||||||
|
require 'metriks'
|
||||||
|
|
||||||
|
module Dyndnsd
|
||||||
|
class TextfileReporter
|
||||||
|
# @return [String]
|
||||||
|
attr_reader :file
|
||||||
|
|
||||||
|
# @param file [String]
|
||||||
|
# @param options [Hash{Symbol => Object}]
|
||||||
|
def initialize(file, options = {})
|
||||||
|
@file = file
|
||||||
|
|
||||||
|
@prefix = options[:prefix]
|
||||||
|
|
||||||
|
@registry = options[:registry] || Metriks::Registry.default
|
||||||
|
@interval = options[:interval] || 60
|
||||||
|
@on_error = options[:on_error] || proc { |ex| }
|
||||||
|
end
|
||||||
|
|
||||||
|
# @return [void]
|
||||||
|
def start
|
||||||
|
@thread ||= Thread.new do
|
||||||
|
loop do
|
||||||
|
sleep @interval
|
||||||
|
|
||||||
|
Thread.new do
|
||||||
|
begin
|
||||||
|
write
|
||||||
|
rescue StandardError => e
|
||||||
|
@on_error[e] rescue nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# @return [void]
|
||||||
|
def stop
|
||||||
|
@thread&.kill
|
||||||
|
@thread = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
# @return [void]
|
||||||
|
def restart
|
||||||
|
stop
|
||||||
|
start
|
||||||
|
end
|
||||||
|
|
||||||
|
# @return [void]
|
||||||
|
def write
|
||||||
|
File.open(@file, 'w') do |f|
|
||||||
|
@registry.each do |name, metric|
|
||||||
|
case metric
|
||||||
|
when Metriks::Meter
|
||||||
|
write_metric f, name, metric, [
|
||||||
|
:count, :one_minute_rate, :five_minute_rate,
|
||||||
|
:fifteen_minute_rate, :mean_rate
|
||||||
|
]
|
||||||
|
when Metriks::Counter
|
||||||
|
write_metric f, name, metric, [
|
||||||
|
:count
|
||||||
|
]
|
||||||
|
when Metriks::UtilizationTimer
|
||||||
|
write_metric f, name, metric, [
|
||||||
|
:count, :one_minute_rate, :five_minute_rate,
|
||||||
|
:fifteen_minute_rate, :mean_rate,
|
||||||
|
:min, :max, :mean, :stddev,
|
||||||
|
:one_minute_utilization, :five_minute_utilization,
|
||||||
|
:fifteen_minute_utilization, :mean_utilization
|
||||||
|
], [
|
||||||
|
:median, :get_95th_percentile
|
||||||
|
]
|
||||||
|
when Metriks::Timer
|
||||||
|
write_metric f, name, metric, [
|
||||||
|
:count, :one_minute_rate, :five_minute_rate,
|
||||||
|
:fifteen_minute_rate, :mean_rate,
|
||||||
|
:min, :max, :mean, :stddev
|
||||||
|
], [
|
||||||
|
:median, :get_95th_percentile
|
||||||
|
]
|
||||||
|
when Metriks::Histogram
|
||||||
|
write_metric f, name, metric, [
|
||||||
|
:count, :min, :max, :mean, :stddev
|
||||||
|
], [
|
||||||
|
:median, :get_95th_percentile
|
||||||
|
]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# @param file [String]
|
||||||
|
# @param base_name [String]
|
||||||
|
# @param metric [Object]
|
||||||
|
# @param keys [Array{Symbol}]
|
||||||
|
# @param snapshot_keys [Array{Symbol}]
|
||||||
|
# @return [void]
|
||||||
|
def write_metric(file, base_name, metric, keys, snapshot_keys = [])
|
||||||
|
time = Time.now.to_i
|
||||||
|
|
||||||
|
base_name = base_name.to_s.gsub(/ +/, '_')
|
||||||
|
base_name = "#{@prefix}.#{base_name}" if @prefix
|
||||||
|
|
||||||
|
keys.flatten.each do |key|
|
||||||
|
name = key.to_s.gsub(/^get_/, '')
|
||||||
|
value = metric.send(key)
|
||||||
|
file.write("#{base_name}.#{name} #{value} #{time}\n")
|
||||||
|
end
|
||||||
|
|
||||||
|
unless snapshot_keys.empty?
|
||||||
|
snapshot = metric.snapshot
|
||||||
|
snapshot_keys.flatten.each do |key|
|
||||||
|
name = key.to_s.gsub(/^get_/, '')
|
||||||
|
value = snapshot.send(key)
|
||||||
|
file.write("#{base_name}.#{name} #{value} #{time}\n")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@@ -2,18 +2,22 @@
|
|||||||
module Dyndnsd
|
module Dyndnsd
|
||||||
module Updater
|
module Updater
|
||||||
class CommandWithBindZone
|
class CommandWithBindZone
|
||||||
|
# @param domain [String]
|
||||||
|
# @param config [Hash{String => Object}]
|
||||||
def initialize(domain, config)
|
def initialize(domain, config)
|
||||||
@zone_file = config['zone_file']
|
@zone_file = config['zone_file']
|
||||||
@command = config['command']
|
@command = config['command']
|
||||||
@generator = Generator::Bind.new(domain, config)
|
@generator = Generator::Bind.new(domain, config)
|
||||||
end
|
end
|
||||||
|
|
||||||
def update(zone)
|
# @param db [Dyndnsd::Database]
|
||||||
|
# @return [void]
|
||||||
|
def update(db)
|
||||||
Helper.span('updater_update') do |span|
|
Helper.span('updater_update') do |span|
|
||||||
span.set_tag('dyndnsd.updater.name', self.class.name.split('::').last)
|
span.set_tag('dyndnsd.updater.name', self.class.name.split('::').last)
|
||||||
|
|
||||||
# write zone file in bind syntax
|
# write zone file in bind syntax
|
||||||
File.open(@zone_file, 'w') { |f| f.write(@generator.generate(zone)) }
|
File.open(@zone_file, 'w') { |f| f.write(@generator.generate(db)) }
|
||||||
# call user-defined command
|
# call user-defined command
|
||||||
pid = fork do
|
pid = fork do
|
||||||
exec @command
|
exec @command
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
|
|
||||||
module Dyndnsd
|
module Dyndnsd
|
||||||
VERSION = '2.0.0.rc1'.freeze
|
VERSION = '2.1.0'.freeze
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user