Index: trunk/src/VBox/ValidationKit/testmanager/batch/virtual_test_sheriff.py
===================================================================
--- trunk/src/VBox/ValidationKit/testmanager/batch/virtual_test_sheriff.py	(revision 61282)
+++ trunk/src/VBox/ValidationKit/testmanager/batch/virtual_test_sheriff.py	(revision 61283)
@@ -73,9 +73,10 @@
         self.oLogin = None;
         self.uidSelf = -1;
+        self.oLogFile = None;
 
         oParser = OptionParser();
-        oParser.add_option('--start-hours-ago', dest = 'cStartHoursAgo', metavar = '<hours>', default = 0,
+        oParser.add_option('--start-hours-ago', dest = 'cStartHoursAgo', metavar = '<hours>', default = 0, type = 'int',
                            help = 'When to start specified as hours relative to current time.  Defauls is right now.', );
-        oParser.add_option('--hours-period', dest = 'cPeriodInHours', metavar = '<period-in-hours>', default = 2,
+        oParser.add_option('--hours-period', dest = 'cHoursBack', metavar = '<period-in-hours>', default = 2, type = 'int',
                            help = 'Work period specified in hours.  Defauls is 2 hours.');
         oParser.add_option('--real-run-back', dest = 'fRealRun', action = 'store_true', default = False,
@@ -83,5 +84,5 @@
         oParser.add_option('-q', '--quiet', dest = 'fQuiet', action = 'store_true', default = False,
                            help = 'Quiet execution');
-        oParser.add_option('-l', '--log', dest = 'sBuildLogPath', metavar = '<url>',
+        oParser.add_option('-l', '--log', dest = 'sBuildLogPath', metavar = '<url>', default = None,
                            help = 'URL to the build logs (optional).');
         oParser.add_option('--debug', dest = 'fDebug', action = 'store_true', default = False,
@@ -90,4 +91,9 @@
         (self.oConfig, _) = oParser.parse_args();
 
+        if self.oConfig.sBuildLogPath is not None and len(self.oConfig.sBuildLogPath) > 0:
+            self.oLogFile = open(self.oConfig.sBuildLogPath, "a");
+            self.oLogFile.write('VirtualTestSheriff: $Revision$ \n');
+
+
     def eprint(self, sText):
         """
@@ -96,4 +102,6 @@
         """
         print 'error: %s' % (sText,);
+        if self.oLogFile is not None:
+            self.oLogFile.write('error: %s\n' % (sText,));
         return 1;
 
@@ -102,6 +110,9 @@
         Prints debug info.
         """
-        if not self.oConfig.fDebug:
-            print 'debug: %s' % (sText, );
+        if self.oConfig.fDebug:
+            if not self.oConfig.fQuiet:
+                print 'debug: %s' % (sText, );
+            if self.oLogFile is not None:
+                self.oLogFile.write('debug: %s\n' % (sText,));
         return 0;
 
@@ -112,4 +123,6 @@
         if not self.oConfig.fQuiet:
             print 'info: %s' % (sText,);
+        if self.oLogFile is not None:
+            self.oLogFile.write('info: %s\n' % (sText,));
         return 0;
 
@@ -127,5 +140,6 @@
           and (not self.oConfig.fDebug or self.oConfig.fRealRun):
             return rcExit;
-        tsNow = self.oConfig.tsNow if self.oConfig.fDebug else None;
+        tsNow      = self.tsNow              if self.oConfig.fDebug else None;
+        cHoursBack = self.oConfig.cHoursBack if self.oConfig.fDebug else 2;
         oTestBoxLogic = TestBoxLogic(self.oDb);
 
@@ -133,5 +147,5 @@
         # Get list of bad test boxes for given period and check them out individually.
         #
-        aidBadTestBoxes = self.oTestSetLogic.fetchBadTestBoxIds();
+        aidBadTestBoxes = self.oTestSetLogic.fetchBadTestBoxIds(cHoursBack = cHoursBack, tsNow = tsNow);
         for idTestBox in aidBadTestBoxes:
             # Skip if the testbox is already disabled or has a pending reboot command.
@@ -151,5 +165,5 @@
 
             # Get the most recent testsets for this box (descending on tsDone) and see how bad it is.
-            aoSets  = self.oTestSetLogic.fetchResultForTestBox(idTestBox, cHoursBack = 2, tsNow = tsNow);
+            aoSets  = self.oTestSetLogic.fetchResultForTestBox(idTestBox, cHoursBack = cHoursBack, tsNow = tsNow);
             cOkay      = 0;
             cBad       = 0;
@@ -160,6 +174,7 @@
                 else:
                     ## @todo maybe check the elapsed time here, it could still be a bad run.
-                    iFirstOkay = iSet;
                     cOkay += 1;
+                    if iFirstOkay > iSet:
+                        iFirstOkay = iSet;
                 if iSet > 10:
                     break;
@@ -168,8 +183,8 @@
             # history and at least three in the last 10 results.
             if iFirstOkay >= 2 and cBad > 2:
-                if oTestBoxLogic.hasTestBoxRecentlyBeenRebooted(idTestBox, cHoursBack = 2, tsNow = tsNow):
+                if oTestBoxLogic.hasTestBoxRecentlyBeenRebooted(idTestBox, cHoursBack = cHoursBack, tsNow = tsNow):
                     self.vprint('Disabling testbox #%u (%s) - iFirstOkay=%u cBad=%u cOkay=%u'
                                 % ( idTestBox, oTestBox.sName, iFirstOkay, cBad, cOkay));
-                    if self.oConfig.fRealRun:
+                    if self.oConfig.fRealRun is True:
                         try:
                             oTestBoxLogic.disableTestBox(idTestBox, self.uidSelf, fCommit = True,
@@ -181,5 +196,5 @@
                     self.vprint('Rebooting testbox #%u (%s) - iFirstOkay=%u cBad=%u cOkay=%u'
                                 % ( idTestBox, oTestBox.sName, iFirstOkay, cBad, cOkay));
-                    if self.oConfig.fRealRun:
+                    if self.oConfig.fRealRun is True:
                         try:
                             oTestBoxLogic.rebootTestBox(idTestBox, self.uidSelf, fCommit = True,
@@ -188,4 +203,7 @@
                         except Exception as oXcpt:
                             rcExit = self.eprint('Error rebooting testbox #%u (%u): %s\n' % (idTestBox, oTestBox.sName, oXcpt,));
+            else:
+                self.dprint('badTestBoxManagement: #%u (%s) looks ok:  iFirstOkay=%u cBad=%u cOkay=%u'
+                            % ( idTestBox, oTestBox.sName, iFirstOkay, cBad, cOkay));
         return rcExit;
 
@@ -234,4 +252,7 @@
         self.oDb.close();
         self.oDb = None;
+        if self.oLogFile is not None:
+            self.oLogFile.close();
+            self.oLogFile = None;
         return rcExit;
 
Index: trunk/src/VBox/ValidationKit/testmanager/core/testbox.py
===================================================================
--- trunk/src/VBox/ValidationKit/testmanager/core/testbox.py	(revision 61282)
+++ trunk/src/VBox/ValidationKit/testmanager/core/testbox.py	(revision 61283)
@@ -908,5 +908,5 @@
                           'WHERE  idTestBox      = %s\n'
                           '   AND tsExpire       < %s\n'
-                          '   AND tsExpire      >= %s - interval \'%u hours\'\n'
+                          '   AND tsExpire      >= %s - interval \'%s hours\'\n'
                           '   AND enmPendingCmd IN (%s, %s)\n'
                           , ( idTestBox, tsNow, tsNow, cHoursBack,
