From: Shih-Hao Li <shi...@vmware.com>

When output to a file, only allow 90% of the free disk space to be used.
---
 utilities/bugtool/ovs-bugtool.in |   49 ++++++++++++++++++++++++++++++--------
 1 file changed, 39 insertions(+), 10 deletions(-)

diff --git a/utilities/bugtool/ovs-bugtool.in b/utilities/bugtool/ovs-bugtool.in
index 61c21db..3c69077 100755
--- a/utilities/bugtool/ovs-bugtool.in
+++ b/utilities/bugtool/ovs-bugtool.in
@@ -207,7 +207,7 @@ CAP_MULTIPATH            = 'multipath'
 CAP_NETWORK_CONFIG       = 'network-config'
 CAP_NETWORK_INFO         = 'network-info'
 CAP_NETWORK_STATUS       = 'network-status'
-CAP_OPENVSWITCH_LOGS    = 'ovs-system-logs'
+CAP_OPENVSWITCH_LOGS     = 'ovs-system-logs'
 CAP_PROCESS_LIST         = 'process-list'
 CAP_SYSTEM_LOGS          = 'system-logs'
 CAP_SYSTEM_SERVICES      = 'system-services'
@@ -222,6 +222,7 @@ unlimited_data = False
 dbg = False
 # Default value for the number of rotated logs.
 log_days = 20
+free_disk_space = None
 
 def cap(key, pii=PII_MAYBE, min_size=-1, max_size=-1, min_time=-1,
         max_time=-1, mime=MIME_TEXT, checked=True, hidden=False):
@@ -290,6 +291,7 @@ def file_output(cap, path_list, newest_first=False):
     by file modification time in descending order, else its sorted
     in ascending order.
     """
+    global free_disk_space
     if cap in entries:
         path_entries = []
         for path in path_list:
@@ -302,10 +304,14 @@ def file_output(cap, path_list, newest_first=False):
         mtime = lambda(path, stat): stat.st_mtime
         path_entries.sort(key=mtime, reverse=newest_first)
         for p in path_entries:
-            if unlimited_data or caps[cap][MAX_SIZE] == -1 or \
-                    cap_sizes[cap] < caps[cap][MAX_SIZE]:
+            if free_disk_space is not None and p[1].st_size > free_disk_space:
+                output("Omitting %s, out of disk space" % p[0])
+            elif unlimited_data or caps[cap][MAX_SIZE] == -1 or \
+                     cap_sizes[cap] < caps[cap][MAX_SIZE]:
                 data[p] = {'cap': cap, 'filename': p[0]}
                 cap_sizes[cap] += p[1].st_size
+                if free_disk_space is not None:
+                    free_disk_space -= p[1].st_size
             else:
                 output("Omitting %s, size constraint of %s exceeded" % (p[0], 
cap))
 
@@ -337,6 +343,7 @@ def log_output(cap, logs, newest_first=False):
         newest_first=newest_first)
 
 def collect_data():
+    global free_disk_space
     process_lists = {}
 
     for (k, v) in data.items():
@@ -354,10 +361,15 @@ def collect_data():
                 f = open(v['filename'], 'r')
                 s = f.read()
                 f.close()
-                if unlimited_data or caps[cap][MAX_SIZE] == -1 or \
-                        cap_sizes[cap] < caps[cap][MAX_SIZE]:
+                l = len(s)
+                if free_disk_space is not None and l > free_disk_space:
+                    output("Omitting %s, out of disk space" % v['filename'])
+                elif unlimited_data or caps[cap][MAX_SIZE] == -1 or \
+                         cap_sizes[cap] < caps[cap][MAX_SIZE]:
                     v['output'] = StringIOmtime(s)
-                    cap_sizes[cap] += len(s)
+                    cap_sizes[cap] += l
+                    if free_disk_space is not None:
+                        free_disk_space -= l
                 else:
                     output("Omitting %s, size constraint of %s exceeded" % 
(v['filename'], cap))
             except:
@@ -367,10 +379,15 @@ def collect_data():
                 s = v['func'](cap)
             except Exception, e:
                 s = str(e)
-            if unlimited_data or caps[cap][MAX_SIZE] == -1 or \
-                    cap_sizes[cap] < caps[cap][MAX_SIZE]:
+            l = len(s)
+            if free_disk_space is not None and l > free_disk_space:
+                output("Omitting %s, out of disk space" % k)
+            elif unlimited_data or caps[cap][MAX_SIZE] == -1 or \
+                     cap_sizes[cap] < caps[cap][MAX_SIZE]:
                 v['output'] = StringIOmtime(s)
-                cap_sizes[cap] += len(s)
+                cap_sizes[cap] += l
+                if free_disk_space is not None:
+                    free_disk_space -= l
             else:
                 output("Omitting %s, size constraint of %s exceeded" % (k, 
cap))
 
@@ -379,7 +396,7 @@ def collect_data():
 
 def main(argv=None):
     global ANSWER_YES_TO_ALL, SILENT_MODE
-    global entries, data, dbg, unlimited_data, log_days
+    global entries, data, dbg, unlimited_data, log_days, free_disk_space
 
     # Filter flags
     only_ovs_info = False
@@ -478,6 +495,10 @@ def main(argv=None):
         print >>sys.stderr, "Cannot set both '--outfd' and '--outfile'"
         return 2
 
+    if output_file is not None:
+        # Use up to 90% of current free disk space.
+        free_disk_space = get_free_disk_space(output_file) * 90 / 100
+
     if ANSWER_YES_TO_ALL:
         output("Warning: '--yestoall' argument provided, will not prompt for 
individual files.")
 
@@ -1248,6 +1269,14 @@ def pidof(name):
     return pids
 
 
+def get_free_disk_space(path):
+    path = os.path.abspath(path)
+    while not os.path.ismount(path):
+        path = os.path.dirname(path)
+    s = os.statvfs(path)
+    return s.f_frsize * s.f_bfree
+
+
 class StringIOmtime(StringIO.StringIO):
     def __init__(self, buf=''):
         StringIO.StringIO.__init__(self, buf)
-- 
1.7.9.5

_______________________________________________
dev mailing list
dev@openvswitch.org
http://openvswitch.org/mailman/listinfo/dev

Reply via email to