Skip to content

Commit

Permalink
Merge pull request #5 from hostinger/feat/option_to_select_loglevel
Browse files Browse the repository at this point in the history
src: Option to select loglevel
  • Loading branch information
deividasraila authored Oct 3, 2024
2 parents 557768d + e564fba commit 1235d0e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: pdns-razor
version: 0.5.5
version: 0.5.6

dependencies:
redis:
Expand Down
33 changes: 26 additions & 7 deletions src/razor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,30 @@ class Razor
@redis_unixsocket = "/tmp/redis.sock"
@debug = false
@log = Logger.new(STDERR)
@loglevel = 4
@hash_method = "edns"
@zone = nil
@geoip_db_path = "/usr/share/GeoIP/GeoIP2-Country.mmdb"

parse_arguments

case @loglevel
when 0
@log.level = Logger::DEBUG
when 1
@log.level = Logger::INFO
when 2
@log.level = Logger::WARN
when 3
@log.level = Logger::ERROR
when 4
@log.level = Logger::FATAL
when 5
@log.level = Logger::UNKNOWN
else
@log.level = Logger::FATAL
end

@geoip = GeoIP2.open(@geoip_db_path)
@geoip_db_checksum = geoip_db_checksum
@redis = Redis.new(host: @redis_host,
Expand All @@ -56,8 +74,8 @@ class Razor
exit
end
parser.invalid_option do |flag|
@log.error("#{flag} is not a valid option")
@log.error(parser)
@log.fatal("#{flag} is not a valid option")
@log.fatal(parser)
exit(1)
end
end
Expand All @@ -66,7 +84,7 @@ class Razor
config = JSON.parse(file)

unless config["contexts"]
@log.error("can't parse JSON #{@config} configuration file")
@log.fatal("can't parse JSON #{@config} configuration file")
exit(1)
end

Expand All @@ -75,6 +93,7 @@ class Razor

@banner = context["banner"].as_s if context["banner"]?
@debug = context["debug"].as_bool if context["debug"]?
@loglevel = context["loglevel"].as_i if context["loglevel"]?
@hash_method = context["hash_method"].as_s if context["hash_method"]?
@redis_host = context["redis_host"].as_s if context["redis_host"]?
@redis_port = context["redis_port"].as_i if context["redis_port"]?
Expand All @@ -84,7 +103,7 @@ class Razor
@geoip_db_path = context["geoip_db_path"].as_s
end
rescue
@log.error("no such context found #{@context} or missing configuration in JSON")
@log.fatal("no such context found #{@context} or missing configuration in JSON")
exit(1)
end

Expand All @@ -99,6 +118,7 @@ class Razor

def mainLoop
Schedule.every(1.hour) do
@log.debug("Hourly schedule starts")
geoip_db_check
end

Expand Down Expand Up @@ -152,6 +172,7 @@ class Razor
# reopen to handle this internally, without PowerDNS restart.
new_geoip_db_checksum = geoip_db_checksum
if @geoip_db_checksum != new_geoip_db_checksum
@log.debug("Reloading GeoIP database")
@geoip = GeoIP2.open(@geoip_db_path)
@geoip_db_checksum = new_geoip_db_checksum
end
Expand Down Expand Up @@ -181,9 +202,7 @@ class Razor
country = rec.country.iso_code
state = rec.subdivisions.size > 0 ? rec.subdivisions[0].iso_code : nil
if !continent && !country && !state
if @debug
@log.warn("No continent/country/state found for #{ip}")
end
@log.warn("No continent/country/state found for #{ip}")
return nil, nil, nil
end
return continent, country, state
Expand Down
1 change: 1 addition & 0 deletions tools/razor.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"zone": "example.org",
"hash_method": "edns",
"debug": true,
"loglevel": 0,
"redis_host": "127.0.0.1",
"redis_port": 6379,
"redis_unixsocket": "/tmp/redis.sock",
Expand Down

0 comments on commit 1235d0e

Please sign in to comment.