[CalendarServer-changes] [8839] CalendarServer/trunk/contrib/tools/request_monitor.py

source_changes at macosforge.org source_changes at macosforge.org
Mon Mar 12 09:45:35 PDT 2012


Revision: 8839
          http://trac.macosforge.org/projects/calendarserver/changeset/8839
Author:   cdaboo at apple.com
Date:     2012-03-12 09:45:34 -0700 (Mon, 12 Mar 2012)
Log Message:
-----------
Support CPU and freemem across multiple OS's.

Modified Paths:
--------------
    CalendarServer/trunk/contrib/tools/request_monitor.py

Modified: CalendarServer/trunk/contrib/tools/request_monitor.py
===================================================================
--- CalendarServer/trunk/contrib/tools/request_monitor.py	2012-03-10 22:08:06 UTC (rev 8838)
+++ CalendarServer/trunk/contrib/tools/request_monitor.py	2012-03-12 16:45:34 UTC (rev 8839)
@@ -1,6 +1,6 @@
 #!/usr/bin/env python
 ##
-# Copyright (c) 2009-2010 Apple Inc. All rights reserved.
+# Copyright (c) 2009-2012 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.
@@ -24,14 +24,44 @@
 import time
 import traceback
 
-NETSTAT = "/usr/sbin/netstat"
-enableListenQueue = os.path.exists(NETSTAT)
-IOSTAT = "/usr/sbin/iostat"
-enableCpuIdle = os.path.exists(IOSTAT)
-VMSTAT = "/usr/bin/vm_stat"
-enableFreeMem = os.path.exists(VMSTAT)
+# Detect which OS this is being run on
+child = Popen(
+    args=[
+        "uname",
+    ],
+    stdout=PIPE, stderr=STDOUT,
+)
+output, _ignore_error = child.communicate()
+output = output.strip()
+if output == "Darwin":
+    OS = "OS X"
+elif output == "Linux":
+    OS = "Linux"
+else:
+    print "Unknown OS: %s" % (output,)
+    sys.exit(1)
 
+# Some system commands we need to detect
+if OS == "OS X":
+    NETSTAT = "/usr/sbin/netstat"
+    enableListenQueue = os.path.exists(NETSTAT)
+elif OS == "Linux":
+    enableListenQueue = False
 
+if OS == "OS X":
+    IOSTAT = "/usr/sbin/iostat"
+    enableCpuIdle = os.path.exists(IOSTAT)
+elif OS == "Linux":
+    IOSTAT = "/usr/bin/iostat"
+    enableCpuIdle = os.path.exists(IOSTAT)
+
+if OS == "OS X":
+    VMSTAT = "/usr/bin/vm_stat"
+    enableFreeMem = os.path.exists(VMSTAT)
+elif OS == "Linux":
+    VMSTAT = "/usr/bin/vmstat"
+    enableFreeMem = os.path.exists(VMSTAT)
+
 sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) 
 
 
@@ -96,7 +126,7 @@
     return results
 
 def cpuPerDaemon():
-    a = []
+    a = {}
     child = Popen(
         args=[
             "ps", "auxw",
@@ -107,37 +137,68 @@
     for l in output.split("\n"):
         if "ProcessType=" in l:
             f = l.split()
-            a.append(f[2])
-    return ", ".join(a)
+            for l in f:
+                if l.startswith("LogID="):
+                    logID = int(l[6:])
+                    break
+            else:
+                logID = None
+            if logID is not None:
+                a[logID] = f[2]
+    return ", ".join([v for _ignore_k, v in sorted(a.items(), key=lambda i:i[0])])
 
 
 def cpuidle():
-    child = Popen(
-        args=[
-            IOSTAT, "-c", "2", "-n", "0",
-        ],
-        stdout=PIPE, stderr=STDOUT,
-    )
-    output, _ignore_ = child.communicate()
-    return output.split("\n")[-2].split()[2]
-
-def freemem():
-    try:
+    if OS == "OS X":
         child = Popen(
             args=[
-                VMSTAT,
+                IOSTAT, "-c", "2", "-n", "0",
             ],
             stdout=PIPE, stderr=STDOUT,
         )
         output, _ignore_ = child.communicate()
-        lines = output.split("\n")
-        
-        line = lines[0]
-        pageSize = int(line[line.find("page size of")+12:].split()[0])
-        line = lines[1]
-        freeSize = int(line[line.find("Pages free:")+11:].split()[0][:-1])
-        freed = freeSize * pageSize
-        return "%d bytes (%.1f GB)" % (freed, freed / (1024.0 * 1024 * 1024),)
+        return output.splitlines[-2].split()[2]
+    elif OS == "Linux":
+        child = Popen(
+            args=[
+                IOSTAT, "-c",
+            ],
+            stdout=PIPE, stderr=STDOUT,
+        )
+        output, _ignore_ = child.communicate()
+        return output.splitlines()[-2].split()[5]
+
+def freemem():
+    try:
+        if OS == "OS X":
+            child = Popen(
+                args=[
+                    VMSTAT,
+                ],
+                stdout=PIPE, stderr=STDOUT,
+            )
+            output, _ignore_ = child.communicate()
+            lines = output.split("\n")
+            
+            line = lines[0]
+            pageSize = int(line[line.find("page size of")+12:].split()[0])
+            line = lines[1]
+            freeSize = int(line[line.find("Pages free:")+11:].split()[0][:-1])
+            freed = freeSize * pageSize
+            return "%d bytes (%.1f GB)" % (freed, freed / (1024.0 * 1024 * 1024),)
+        elif OS == "Linux":
+            child = Popen(
+                args=[
+                    VMSTAT, "-s", "-S", "K"
+                ],
+                stdout=PIPE, stderr=STDOUT,
+            )
+            output, _ignore_ = child.communicate()
+            lines = output.splitlines()
+            
+            line = lines[4]
+            freed = int(line.split()[0]) * 1024
+            return "%d bytes (%.1f GB)" % (freed, freed / (1024.0 * 1024 * 1024),)
     except Exception, e:
         if debug:
             print "freemem failure", e
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20120312/c30d5bbd/attachment.html>


More information about the calendarserver-changes mailing list