From: Zack A Date: Fri, 25 Oct 2013 13:49:58 +0000 (-0400) Subject: Cleaned up commits as seen by Habbie's comment. Updated scripting xml file with a... X-Git-Tag: rec-3.6.0-rc1~371^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5c9df68f63f6f24db24d78341ea1270674137240;p=pdns Cleaned up commits as seen by Habbie's comment. Updated scripting xml file with a table of new entries. Added some new pdnslog functions within the powerdns-example-script.lua to show new functionality. --- diff --git a/pdns/docs/pdns.xml b/pdns/docs/pdns.xml index d576213b4..7e40551fc 100755 --- a/pdns/docs/pdns.xml +++ b/pdns/docs/pdns.xml @@ -15058,6 +15058,25 @@ end To log messages with the main PowerDNS Recursor process, use pdnslog(message). Available since version 3.2. + pdnslog can also write out to a syslog loglevel if specified. Use pdnslog(message, pdns.loglevels.LEVEL) with the correct pdns.loglevels entry. Entries are listed in the following table: + + pdnslog() loglevels + + + Allpdns.loglevels.All + NTLogpdns.loglevels.NTLog + Alertpdns.loglevels.Alert + Criticalpdns.loglevels.Critical + Errorpdns.loglevels.Error + Warningpdns.loglevels.Warning + Noticepdns.loglevels.Notice + Infopdns.loglevels.Info + Debugpdns.loglevels.Debug + Nonepdns.loglevels.None + + +
+ pdnslog(message) will write out to Info by default.
To retrieve the IP address on which a query was received, use getlocaladdress(). Available since version 3.2. diff --git a/pdns/lua-pdns.cc b/pdns/lua-pdns.cc index 3d69d45a6..5726fc392 100644 --- a/pdns/lua-pdns.cc +++ b/pdns/lua-pdns.cc @@ -71,6 +71,7 @@ static bool getFromTable(lua_State *lua, const std::string &key, uint32_t& value lua_gettable(lua, -2); // replace by the first entry of our table we hope bool ret=false; + if(!lua_isnil(lua, -1)) { value = (uint32_t)lua_tonumber(lua, -1); ret=true; @@ -110,9 +111,17 @@ void pushResourceRecordsTable(lua_State* lua, const vector& r lua_settable(lua, -3); // pushes the table we just built into the master table at position pushed above } } -// this function takes the global lua_state from the PowerDNSLua constructor and populates it with the syslog enums values +// override the __index metatable under loglevels to return Logger::Error to account for nil accesses to the loglevels table +int loglevels_index(lua_State* lua) +{ + lua_pushnumber(lua, Logger::Error); + return 1; +} +// push the loglevel subtable onto the stack that will eventually be the pdns table void pushSyslogSecurityLevelTable(lua_State* lua) { + lua_newtable(lua); +// this function takes the global lua_state from the PowerDNSLua constructor and populates it with the syslog enums values lua_pushnumber(lua, Logger::All); lua_setfield(lua, -2, "All"); lua_pushnumber(lua, Logger::NTLog); @@ -133,6 +142,10 @@ void pushSyslogSecurityLevelTable(lua_State* lua) lua_setfield(lua, -2, "Debug"); lua_pushnumber(lua, Logger::None); lua_setfield(lua, -2, "None"); + lua_createtable(lua, 0, 1); + lua_pushcfunction(lua, loglevels_index); + lua_setfield(lua, -2, "__index"); + lua_setmetatable(lua, -2); } int getLuaTableLength(lua_State* lua, int depth) { @@ -248,42 +261,7 @@ int logLua(lua_State *lua) } else if(argc >= 2) { string message=lua_tostring(lua, 1); int urgencylevel = lua_tonumber(lua, 2); - switch(urgencylevel) - { - case Logger::Alert: - theL()<= #send and s:find(send, #s-#send+1, true) and true or false end function preresolve ( remoteip, domain, qtype ) + print ("prequery handler called for: ", remoteip, getlocaladdress(), domain, qtype) - pdnslog("a test message.. received query from "..remoteip.." on "..getlocaladdress()); + pdnslog("a test message.. received query from "..remoteip.." on "..getlocaladdress(), pdns.loglevels.Info); if endswith(domain, "f.f.7.7.b.1.2.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.e.f.ip6.arpa.") then @@ -14,7 +16,7 @@ function preresolve ( remoteip, domain, qtype ) if domain == "www.donotcache.org." then - print("making sure www.donotcache.org will never end up in the cache") + print("making sure www.donotcache.org will never end up in the cache", pdns.loglevels.Debug) setvariable() return -1, {} end @@ -45,8 +47,14 @@ end function nxdomain ( remoteip, domain, qtype ) print ("nxhandler called for: ", remoteip, getlocaladdress(), domain, qtype, pdns.AAAA) - if qtype ~= pdns.A then return -1, {} end -- only A records - if not string.find(domain, "^www%.") then return -1, {} end -- only things that start with www. + if qtype ~= pdns.A then + pdnslog("Only A records", pdns.loglevels.Error) + return -1, {} + end -- only A records + if not string.find(domain, "^www%.") then + pdnslog("Only strings that start with www.", pdns.loglevels.Error) + return -1, {} + end -- only things that start with www. setvariable() if matchnetmask(remoteip, {"127.0.0.1/32", "10.1.0.0/16"}) @@ -88,7 +96,7 @@ end -- records contains the entire packet, ready for your modifying pleasure function postresolve ( remoteip, domain, qtype, records, origrcode ) - print ("postresolve called for: ", remoteip, getlocaladdress(), domain, qtype, origrcode) + print ("postresolve called for: ", remoteip, getlocaladdress(), domain, qtype, origrcode, pdns.loglevels.Info) for key,val in ipairs(records) do @@ -114,15 +122,15 @@ function prequery ( dnspacket ) pdnslog ("q: ".. qname.." "..qtype) if qtype == pdns.A and qname == "www.domain.com" then - pdnslog ("calling dnspacket:setRcode") + pdnslog ("calling dnspacket:setRcode", pdns.loglevels.Debug) dnspacket:setRcode(pdns.NXDOMAIN) - pdnslog ("called dnspacket:setRcode") - pdnslog ("adding records") + pdnslog ("called dnspacket:setRcode", pdns.loglevels.Debug) + pdnslog ("adding records", pdns.loglevels.Debug) ret = {} ret[1] = {qname=qname, qtype=qtype, content="1.2.3.4", place=2} ret[2] = {qname=qname, qtype=pdns.TXT, content=os.date("Retrieved at %Y-%m-%d %H:%M"), ttl=ttl} dnspacket:addRecords(ret) - pdnslog ("returning true") + pdnslog ("returning true", pdns.loglevels.Debug) return true end pdnslog ("returning false")