]> granicus.if.org Git - pdns/commitdiff
Cleaned up commits as seen by Habbie's comment. Updated scripting xml file with a...
authorZack A <zma4580@gmail.com>
Fri, 25 Oct 2013 13:49:58 +0000 (09:49 -0400)
committerZack A <zma4580@gmail.com>
Fri, 25 Oct 2013 13:49:58 +0000 (09:49 -0400)
pdns/docs/pdns.xml
pdns/lua-pdns.cc
pdns/powerdns-example-script.lua

index d576213b411cd4e006c370b468236cc8aa0a92d4..7e40551fcdf2caf0bc61920d7cdffc9add1bb32c 100755 (executable)
@@ -15058,6 +15058,25 @@ end
        </para>
        <para>
          To log messages with the main PowerDNS Recursor process, use <function>pdnslog(message)</function>. Available since version 3.2.
+    pdnslog can also write out to a syslog loglevel if specified. Use <function>pdnslog(message, pdns.loglevels.LEVEL)</function> with the correct pdns.loglevels entry. Entries are listed in the following table:
+    <table>
+    <title>pdnslog() loglevels</title>
+    <tgroup cols="2">
+      <tbody>
+        <row><entry>All</entry><entry>pdns.loglevels.All</entry></row>
+        <row><entry>NTLog</entry><entry>pdns.loglevels.NTLog</entry></row>
+        <row><entry>Alert</entry><entry>pdns.loglevels.Alert</entry></row>
+        <row><entry>Critical</entry><entry>pdns.loglevels.Critical</entry></row>
+        <row><entry>Error</entry><entry>pdns.loglevels.Error</entry></row>
+        <row><entry>Warning</entry><entry>pdns.loglevels.Warning</entry></row>
+        <row><entry>Notice</entry><entry>pdns.loglevels.Notice</entry></row>
+        <row><entry>Info</entry><entry>pdns.loglevels.Info</entry></row>
+        <row><entry>Debug</entry><entry>pdns.loglevels.Debug</entry></row>
+        <row><entry>None</entry><entry>pdns.loglevels.None</entry></row>
+      </tbody>
+    </tgroup>
+  </table>
+    pdnslog(message) will write out to Info by default.
        </para>
        <para>
          To retrieve the IP address on which a query was received, use <function>getlocaladdress()</function>. Available since version 3.2.
index 3d69d45a6b780e9d49fe0f71b342e43da62edcbe..5726fc392e42e2205d3501663b80bfc6177f13a1 100644 (file)
@@ -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<DNSResourceRecord>& 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()<<Logger::Alert<<message<<endl;
-        break;
-      case Logger::Critical:
-        theL()<<Logger::Critical<<message<<endl;
-        break;
-      case Logger::Error:
-        theL()<<Logger::Error<<message<<endl;
-        break;
-      case Logger::Warning:
-        theL()<<Logger::Warning<<message<<endl;
-        break;
-      case Logger::Notice:
-        theL()<<Logger::Notice<<message<<endl;
-        break;
-      case Logger::Info:
-        theL()<<Logger::Info<<message<<endl;
-        break;
-      case Logger::Debug:
-        theL()<<Logger::Debug<<message<<endl;
-        break;
-      case Logger::All:
-        theL()<<Logger::All<<message<<endl;
-        break;
-      case Logger::NTLog:
-        theL()<<Logger::All<<message<<endl;
-        break;
-      case Logger::None:
-        theL()<<Logger::None<<message<<endl;
-        break;
-      default:
-        theL()<<Logger::Error<<message<<endl;
-        break;
-    }
+    theL()<<urgencylevel<<" "<<message<<endl; 
   }
   return 0;
 }
@@ -320,6 +298,7 @@ PowerDNSLua::PowerDNSLua(const std::string& fname)
   lua_setfield(d_lua, -2, "REFUSED");
   // set syslog codes used by Logger/enum Urgency
   pushSyslogSecurityLevelTable(d_lua);
+  lua_setfield(d_lua, -2, "loglevels");
 
   lua_setglobal(d_lua, "pdns");
 
index c05bd8374d786d7abf4317f7f733d9fcd10e8857..31099b130f88977df096c9aaf78818a00a4a46e6 100644 (file)
@@ -1,10 +1,12 @@
+pdnslog("pdns-recursor starting!", pdns.loglevels.Info)
 function endswith(s, send)
         return #s >= #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")