commit 23e4baf1d0d1f2577c24a615c99b8a965966a4b2 Author: Karsten Loesing karsten.loesing@gmx.net Date: Fri Oct 14 10:09:49 2011 +0200
Make consensus-health checker handle non-fresh consensuses. --- src/org/torproject/chc/Downloader.java | 28 ++++++++++++++++++++++- src/org/torproject/chc/NagiosReport.java | 34 ++++++++++++++++++++-------- src/org/torproject/chc/StdOutReport.java | 36 +++++++++++++++++++++-------- 3 files changed, 77 insertions(+), 21 deletions(-)
diff --git a/src/org/torproject/chc/Downloader.java b/src/org/torproject/chc/Downloader.java index 94ed30f..bdd23fe 100644 --- a/src/org/torproject/chc/Downloader.java +++ b/src/org/torproject/chc/Downloader.java @@ -4,6 +4,7 @@ package org.torproject.chc;
import java.io.*; import java.net.*; +import java.text.*; import java.util.*; import java.util.zip.*;
@@ -66,6 +67,8 @@ public class Downloader { } }
+ /* Downloads a consensus or vote in a separate thread that can be + * interrupted after a timeout. */ private static class DownloadRunnable implements Runnable { Thread mainThread; String url; @@ -121,6 +124,13 @@ public class Downloader { return response; }
+ /* Date-time formats to parse and format timestamps. */ + private static SimpleDateFormat dateTimeFormat; + static { + dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + dateTimeFormat.setTimeZone(TimeZone.getTimeZone("UTC")); + } + /* Parse the downloaded consensus to find fingerprints of directory * authorities publishing the corresponding votes. */ private List<String> fingerprints = new ArrayList<String>(); @@ -131,7 +141,23 @@ public class Downloader { this.downloadedConsensus)); String line; while ((line = br.readLine()) != null) { - if (line.startsWith("dir-source ")) { + if (line.startsWith("valid-after ")) { + try { + long validAfterMillis = dateTimeFormat.parse(line.substring( + "valid-after ".length())).getTime(); + if (validAfterMillis + 60L * 60L * 1000L < + System.currentTimeMillis()) { + /* Consensus is more than 1 hour old. We won't be able to + * download the corresponding votes anymore. */ + break; + } + } catch (ParseException e) { + System.err.println("Could not parse valid-after timestamp " + + "in line '" + line + "' of a downloaded consensus. " + + "Not downloading votes."); + break; + } + } else if (line.startsWith("dir-source ")) { String[] parts = line.split(" "); if (parts.length < 3) { System.err.println("Bad dir-source line '" + line diff --git a/src/org/torproject/chc/NagiosReport.java b/src/org/torproject/chc/NagiosReport.java index 26d61a7..11bdaa3 100644 --- a/src/org/torproject/chc/NagiosReport.java +++ b/src/org/torproject/chc/NagiosReport.java @@ -50,21 +50,35 @@ public class NagiosReport implements Report { * file. */ public void writeReport() { if (this.downloadedConsensus != null) { - this.checkConsensusMethods(); - this.checkRecommendedVersions(); - this.checkConsensusParameters(); - this.checkAuthorityKeys(); - this.checkMissingVotes(); - this.checkBandwidthScanners(); - this.checkConsensusAge(this.downloadedConsensus); + if (this.isConsensusFresh(this.downloadedConsensus)) { + this.checkConsensusMethods(); + this.checkRecommendedVersions(); + this.checkConsensusParameters(); + this.checkAuthorityKeys(); + this.checkMissingVotes(); + this.checkBandwidthScanners(); + } } else if (this.cachedConsensus != null) { - this.checkConsensusAge(this.cachedConsensus); + this.checkConsensusValid(this.cachedConsensus); } else { this.nagiosUnknowns.add("No consensus known"); } this.writeNagiosStatusFile(); }
+ /* Check if the most recent consensus is older than 1 hour. */ + private boolean isConsensusFresh(Status consensus) { + if (consensus.getValidAfterMillis() < + System.currentTimeMillis() - 60L * 60L * 1000L) { + this.nagiosCriticals.add("The last known consensus published at " + + dateTimeFormat.format(consensus.getValidAfterMillis()) + + " is more than 1 hour old and therefore not fresh anymore"); + return false; + } else { + return true; + } + } + /* Check supported consensus methods of all votes. */ private void checkConsensusMethods() { for (Status vote : this.downloadedVotes) { @@ -192,12 +206,12 @@ public class NagiosReport implements Report {
/* Check that the most recent consensus is not more than 3 hours * old. */ - public void checkConsensusAge(Status consensus) { + public void checkConsensusValid(Status consensus) { if (consensus.getValidAfterMillis() < System.currentTimeMillis() - 3L * 60L * 60L * 1000L) { this.nagiosCriticals.add("The last known consensus published at " + dateTimeFormat.format(consensus.getValidAfterMillis()) - + " is more than 3 hours old"); + + " is more than 3 hours old and therefore not valid anymore"); } }
diff --git a/src/org/torproject/chc/StdOutReport.java b/src/org/torproject/chc/StdOutReport.java index cf7665d..f99c2eb 100644 --- a/src/org/torproject/chc/StdOutReport.java +++ b/src/org/torproject/chc/StdOutReport.java @@ -41,15 +41,16 @@ public class StdOutReport implements Report { public void writeReport() { this.readLastWarned(); if (this.downloadedConsensus != null) { - this.checkConsensusMethods(); - this.checkRecommendedVersions(); - this.checkConsensusParameters(); - this.checkAuthorityKeys(); - this.checkMissingVotes(); - this.checkBandwidthScanners(); - this.checkConsensusAge(this.downloadedConsensus); + if (this.isConsensusFresh(this.downloadedConsensus)) { + this.checkConsensusMethods(); + this.checkRecommendedVersions(); + this.checkConsensusParameters(); + this.checkAuthorityKeys(); + this.checkMissingVotes(); + this.checkBandwidthScanners(); + } } else if (this.cachedConsensus != null) { - this.checkConsensusAge(this.cachedConsensus); + this.checkConsensusValid(this.cachedConsensus); } else { this.warnings.put("No consensus known", 0L); } @@ -94,6 +95,20 @@ public class StdOutReport implements Report { } }
+ /* Check if the most recent consensus is older than 1 hour. */ + private boolean isConsensusFresh(Status consensus) { + if (consensus.getValidAfterMillis() < + System.currentTimeMillis() - 60L * 60L * 1000L) { + this.warnings.put("The last known consensus published at " + + dateTimeFormat.format(consensus.getValidAfterMillis()) + + " is more than 1 hour old and therefore not fresh anymore", + 0L); + return false; + } else { + return true; + } + } + /* Check supported consensus methods of all votes. */ private void checkConsensusMethods() { for (Status vote : this.downloadedVotes) { @@ -228,12 +243,13 @@ public class StdOutReport implements Report { }
/* Check if the most recent consensus is older than 3 hours. */ - private void checkConsensusAge(Status consensus) { + private void checkConsensusValid(Status consensus) { if (consensus.getValidAfterMillis() < System.currentTimeMillis() - 3L * 60L * 60L * 1000L) { this.warnings.put("The last known consensus published at " + dateTimeFormat.format(consensus.getValidAfterMillis()) - + " is more than 3 hours old", 0L); + + " is more than 3 hours old and therefore not valid anymore", + 0L); } }
tor-commits@lists.torproject.org