[CalendarServer-changes] [2356] CalendarServer/branches/users/wsanchez/logging

source_changes at macosforge.org source_changes at macosforge.org
Fri Apr 25 19:38:18 PDT 2008


Revision: 2356
          http://trac.macosforge.org/projects/calendarserver/changeset/2356
Author:   wsanchez at apple.com
Date:     2008-04-25 19:38:17 -0700 (Fri, 25 Apr 2008)

Log Message:
-----------
Add accounting stuff, modeled roughly after Cyrus' loggig branch.

Modified Paths:
--------------
    CalendarServer/branches/users/wsanchez/logging/conf/caldavd-test.plist
    CalendarServer/branches/users/wsanchez/logging/twistedcaldav/config.py
    CalendarServer/branches/users/wsanchez/logging/twistedcaldav/schedule.py

Added Paths:
-----------
    CalendarServer/branches/users/wsanchez/logging/twistedcaldav/accounting.py

Modified: CalendarServer/branches/users/wsanchez/logging/conf/caldavd-test.plist
===================================================================
--- CalendarServer/branches/users/wsanchez/logging/conf/caldavd-test.plist	2008-04-26 02:21:28 UTC (rev 2355)
+++ CalendarServer/branches/users/wsanchez/logging/conf/caldavd-test.plist	2008-04-26 02:38:17 UTC (rev 2356)
@@ -184,7 +184,7 @@
   <!-- Principals with "DAV:all" access (relative URLs) -->
   <key>AdminPrincipals</key>
   <array>
-    <string>/principals/__uids__/admin/</string>
+    <string>/principals/users/admin/</string>
   </array>
 
   <!-- Principals that can pose as other principals -->
@@ -258,6 +258,17 @@
   <dict>
   </dict>
 
+  <!-- Accounting -->
+  <key>AccountingCategories</key>
+  <dict>
+    <key>iTIP</key><false/>
+  </dict>
+
+  <key>AccountingPrincipals</key>
+  <array>
+    <!--<string>/principals/users/foo/</string>-->
+  </array>
+
   <!-- Server statistics file -->
   <key>ServerStatsFile</key>
   <string>logs/stats.plist</string>

Added: CalendarServer/branches/users/wsanchez/logging/twistedcaldav/accounting.py
===================================================================
--- CalendarServer/branches/users/wsanchez/logging/twistedcaldav/accounting.py	                        (rev 0)
+++ CalendarServer/branches/users/wsanchez/logging/twistedcaldav/accounting.py	2008-04-26 02:38:17 UTC (rev 2356)
@@ -0,0 +1,107 @@
+##
+# Copyright (c) 2006-2007 Apple Inc. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+##
+
+"""
+Extended account-specific logging.
+Allows different sub-systems to log data on a per-principal basis.
+"""
+
+__all__ = [
+    "accountingEnabled",
+    "emitAccounting",
+]
+
+import datetime
+import os
+
+from twistedcaldav.config import config
+from twistedcaldav.log import Logger
+
+log = Logger()
+
+def accountingEnabled(category, principal):
+    """
+    Determine if accounting is enabled for the given category and principal.
+    """
+    return (
+        accountingEnabledForCategory(category) and
+        accountingEnabledForPrincipal(principal)
+    )
+
+def accountingEnabledForCategory(category):
+    """
+    Determine if accounting is enabled for the given category.
+    """
+    return config.AccountingCategories.get(category, False)
+
+def accountingEnabledForPrincipal(principal):
+    """
+    Determine if accounting is enabled for the given principal.
+    """
+    enabledPrincipalURIs = config.AccountingPrincipals
+
+    if principal.principalURL() in enabledPrincipalURIs:
+        return True
+
+    if principal.alternateURIs() in enabledPrincipalURIs:
+        return True
+
+    return False
+
+def emitAccounting(category, principal, data):
+    """
+    Write the supplied data to the appropriate location for the given
+    category and principal.
+
+    @param principal: the principal for whom a log entry is to be created.
+    @type principal: L{DirectoryPrincipalResource}
+    @param category: accounting category
+    @type category: C{tuple}
+    @param data: data to write.
+    @type data: C{str}
+    """    
+    if not accountingEnabled(category, principal):
+        return
+
+    #
+    # Obtain the accounting log file name
+    #
+    logRoot = config.AccountingLogRoot
+    logDirectory = os.path.join(logRoot, principal.record.recordType, principal.record.shortName, category)
+    logFilename = os.path.join(logDirectory, datetime.datetime.now().isoformat())
+
+    if not os.path.isdir(logDirectory):
+        os.makedirs(logDirectory)
+        logFilename = "%s-01" % (logFilename,)
+    else:
+        index = 1
+        while True:
+            path = "%s-%02d" % (logFilename, index)
+            if not os.path.isfile(path):
+                logFilename = path
+                break
+            if index == 1000:
+                log.error("Too many %s accounting files for %s" % (category, principal))
+                return
+
+    #
+    # Now write out the data to the log file
+    #
+    logFile = open(logFilename, "a")
+    try:
+        logFile.write(data)
+    finally:
+        logFile.close()

Modified: CalendarServer/branches/users/wsanchez/logging/twistedcaldav/config.py
===================================================================
--- CalendarServer/branches/users/wsanchez/logging/twistedcaldav/config.py	2008-04-26 02:21:28 UTC (rev 2355)
+++ CalendarServer/branches/users/wsanchez/logging/twistedcaldav/config.py	2008-04-26 02:38:17 UTC (rev 2356)
@@ -113,8 +113,13 @@
     "ServerStatsFile": "/var/run/caldavd/stats.plist",
     "PIDFile"        : "/var/run/caldavd.pid",
     "RotateAccessLog": False,
-    "DefaultLogLevel": None,
+    "DefaultLogLevel": "",
     "LogLevels": {},
+    "AccountingCategories": {
+        "iTIP": False,
+    },
+    "AccountingPrincipals": (),
+    "AccountingLogRoot": "/var/log/caldavd/accounting",
 
     #
     # SSL/TLS

Modified: CalendarServer/branches/users/wsanchez/logging/twistedcaldav/schedule.py
===================================================================
--- CalendarServer/branches/users/wsanchez/logging/twistedcaldav/schedule.py	2008-04-26 02:21:28 UTC (rev 2355)
+++ CalendarServer/branches/users/wsanchez/logging/twistedcaldav/schedule.py	2008-04-26 02:38:17 UTC (rev 2356)
@@ -40,6 +40,7 @@
 from twistedcaldav import caldavxml
 from twistedcaldav import itip
 from twistedcaldav.log import LoggingMixIn
+from twistedcaldav.accounting import accountingEnabled, emitAccounting
 from twistedcaldav.resource import CalDAVResource
 from twistedcaldav.caldavxml import caldav_namespace, TimeRange
 from twistedcaldav.config import config
@@ -338,6 +339,21 @@
             # Do regular invite (fan-out)
             freebusy = False
 
+        #
+        # Accounting
+        #
+        # Note that we associate logging with the organizer, not the
+        # originator, which is good for looking for why something
+        # shows up in a given principal's calendars, rather than
+        # tracking the activities of a specific user.
+        #
+        if accountingEnabled("iTIP", organizerPrincipal):
+            emitAccounting(
+                "iTIP", organizerPrincipal,
+                "Originator: %s\nRecipients: %s\n\n%s"
+                % (originator, ", ".join(recipients), str(calendar))
+            )
+
         # Prepare for multiple responses
         responses = ScheduleResponseQueue("POST", responsecode.OK)
     

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20080425/b022f0d8/attachment-0001.html


More information about the calendarserver-changes mailing list