[tor-commits] [metrics-web/master] Write nagios status file for possible consensus problems.

karsten at torproject.org karsten at torproject.org
Mon Mar 7 13:35:20 UTC 2011


commit 06bf8aadf8c2588dbab8582c7cb56c85eceebc92
Author: Karsten Loesing <karsten.loesing at gmx.net>
Date:   Mon Mar 7 12:54:18 2011 +0100

    Write nagios status file for possible consensus problems.
---
 config.template                                    |    4 +
 src/org/torproject/ernie/cron/Configuration.java   |    7 ++
 .../ernie/cron/ConsensusHealthChecker.java         |   59 +++++++++++++++++++-
 src/org/torproject/ernie/cron/Main.java            |    5 +-
 4 files changed, 72 insertions(+), 3 deletions(-)

diff --git a/config.template b/config.template
index fd37f4d..7936420 100644
--- a/config.template
+++ b/config.template
@@ -40,6 +40,10 @@
 ## website
 #WriteConsensusHealth 0
 #
+## Write Nagios status file containing potential problems with the latest
+## consensus
+#WriteNagiosStatusFile 0
+#
 ## Write bridge stats to disk
 #WriteBridgeStats 0
 #
diff --git a/src/org/torproject/ernie/cron/Configuration.java b/src/org/torproject/ernie/cron/Configuration.java
index e2c630b..4192f55 100644
--- a/src/org/torproject/ernie/cron/Configuration.java
+++ b/src/org/torproject/ernie/cron/Configuration.java
@@ -25,6 +25,7 @@ public class Configuration {
   private boolean writeRelayDescriptorsRawFiles = false;
   private String relayDescriptorRawFilesDirectory = "pg-import/";
   private boolean writeConsensusHealth = false;
+  private boolean writeNagiosStatusFile = false;
   private boolean writeBridgeStats = false;
   private boolean importWriteTorperfStats = false;
   private String torperfDirectory = "torperf/";
@@ -76,6 +77,9 @@ public class Configuration {
         } else if (line.startsWith("WriteConsensusHealth")) {
           this.writeConsensusHealth = Integer.parseInt(
               line.split(" ")[1]) != 0;
+        } else if (line.startsWith("WriteNagiosStatusFile")) {
+          this.writeNagiosStatusFile = Integer.parseInt(
+              line.split(" ")[1]) != 0;
         } else if (line.startsWith("WriteBridgeStats")) {
           this.writeBridgeStats = Integer.parseInt(
               line.split(" ")[1]) != 0;
@@ -147,6 +151,9 @@ public class Configuration {
   public boolean getWriteConsensusHealth() {
     return this.writeConsensusHealth;
   }
+  public boolean getWriteNagiosStatusFile() {
+    return this.writeNagiosStatusFile;
+  }
   public boolean getWriteBridgeStats() {
     return this.writeBridgeStats;
   }
diff --git a/src/org/torproject/ernie/cron/ConsensusHealthChecker.java b/src/org/torproject/ernie/cron/ConsensusHealthChecker.java
index b1ad6b6..ac07754 100644
--- a/src/org/torproject/ernie/cron/ConsensusHealthChecker.java
+++ b/src/org/torproject/ernie/cron/ConsensusHealthChecker.java
@@ -25,13 +25,22 @@ public class ConsensusHealthChecker {
    */
   private Logger logger;
 
+  private File statsDirectory;
+
+  private List<String> nagiosWarnings = new ArrayList<String>(),
+      nagiosCriticals = new ArrayList<String>(),
+      nagiosUnknowns = new ArrayList<String>();
+
   private SortedMap<String, byte[]> mostRecentVotes =
         new TreeMap<String, byte[]>();
 
-  public ConsensusHealthChecker() {
+  public ConsensusHealthChecker(File statsDirectory) {
+
     /* Initialize logger. */
     this.logger = Logger.getLogger(
         ConsensusHealthChecker.class.getName());
+
+    this.statsDirectory = statsDirectory;
   }
 
   public void processConsensus(String validAfterTime, byte[] data) {
@@ -78,6 +87,7 @@ public class ConsensusHealthChecker {
      * with a warning, because we might just not have learned about a new
      * consensus in this execution. */
     if (this.mostRecentConsensus == null) {
+      nagiosUnknowns.add("No consensus known");
       return;
     }
 
@@ -108,7 +118,6 @@ public class ConsensusHealthChecker {
     SortedMap<String, SortedMap<String, Integer>> flagsMissing =
         new TreeMap<String, SortedMap<String, Integer>>();
 
-
     /* Read consensus and parse all information that we want to compare to
      * votes. */
     String consensusConsensusMethod = null, consensusKnownFlags = null,
@@ -241,6 +250,8 @@ public class ConsensusHealthChecker {
         this.logger.warning(dirSource + " does not support consensus "
             + "method " + consensusConsensusMethod.split(" ")[1] + ": "
             + voteConsensusMethods);
+        nagiosWarnings.add(dirSource + " does not support consensus "
+            + "method " + consensusConsensusMethod.split(" ")[1]);
       } else {
         consensusMethodsResults.append("          <tr>\n"
                + "            <td>" + dirSource + "</td>\n"
@@ -263,6 +274,8 @@ public class ConsensusHealthChecker {
             + "          </tr>\n");
         this.logger.warning(dirSource + " recommends other client "
             + "versions than the consensus: " + voteClientVersions);
+        nagiosWarnings.add(dirSource + " recommends other client "
+            + "versions than the consensus");
       } else {
         versionsResults.append("          <tr>\n"
             + "            <td>" + dirSource + "</td>\n"
@@ -281,6 +294,8 @@ public class ConsensusHealthChecker {
             + "          </tr>\n");
         this.logger.warning(dirSource + " recommends other server "
             + "versions than the consensus: " + voteServerVersions);
+        nagiosWarnings.add(dirSource + " recommends other server "
+            + "versions than the consensus");
       } else {
         versionsResults.append("          <tr>\n"
             + "            <td></td>\n"
@@ -318,6 +333,8 @@ public class ConsensusHealthChecker {
             + "          </tr>\n");
         this.logger.warning(dirSource + " sets conflicting or invalid "
             + "consensus parameters: " + voteParams);
+        nagiosWarnings.add(dirSource + " sets conflicting or invalid "
+            + "consensus parameters");
       } else {
         paramsResults.append("          <tr>\n"
             + "            <td>" + dirSource + "</td>\n"
@@ -347,6 +364,8 @@ public class ConsensusHealthChecker {
               + "          </tr>\n");
           this.logger.warning(dirSource + "'s certificate expires in the "
               + "next 14 days: " + voteDirKeyExpires);
+          nagiosWarnings.add(dirSource + "'s certificate expires in the "
+              + "next 14 days");
         } else {
           authorityKeysResults.append("          <tr>\n"
               + "            <td>" + dirSource + "</td>\n"
@@ -381,6 +400,8 @@ public class ConsensusHealthChecker {
       }
       this.logger.warning("We're missing votes from the following "
           + "directory authorities: " + sb.toString().substring(2));
+      nagiosWarnings.add("We're missing votes from the following "
+          + "directory authorities: " + sb.toString().substring(2));
     }
 
     try {
@@ -464,6 +485,9 @@ public class ConsensusHealthChecker {
         this.logger.warning("The last consensus published at "
             + this.mostRecentValidAfterTime + " is more than 3 hours "
             + "old.");
+        nagiosCriticals.add("The last consensus published at "
+            + this.mostRecentValidAfterTime + " is more than 3 hours "
+            + "old");
       } else {
         bw.write(this.mostRecentValidAfterTime);
         this.logger.fine("The last consensus published at "
@@ -908,5 +932,36 @@ public class ConsensusHealthChecker {
     } catch (IOException e) {
     }
   }
+
+  public void writeNagiosStatusFile() {
+    try {
+      statsDirectory.mkdirs();
+      File nagiosStatusFile = new File(statsDirectory,
+          "consensus-health");
+      BufferedWriter bw = new BufferedWriter(new FileWriter(
+          nagiosStatusFile));
+      if (!nagiosUnknowns.isEmpty()) {
+        bw.write("UNKNOWN\nUNKNOWN");
+      } else if (!nagiosCriticals.isEmpty()) {
+        bw.write("CRITICAL\nCRITICAL");
+      } else if (!nagiosWarnings.isEmpty()) {
+        bw.write("WARNING\nWARNING");
+      } else {
+        bw.write("OK\nOK");
+      }
+      for (String message : nagiosUnknowns) {
+        bw.write(" " + message + ";");
+      }
+      for (String message : nagiosCriticals) {
+        bw.write(" " + message + ";");
+      }
+      for (String message : nagiosWarnings) {
+        bw.write(" " + message + ";");
+      }
+      bw.write("\n");
+      bw.close();
+    } catch (IOException e) {
+    }
+  }
 }
 
diff --git a/src/org/torproject/ernie/cron/Main.java b/src/org/torproject/ernie/cron/Main.java
index ec02130..b24df37 100644
--- a/src/org/torproject/ernie/cron/Main.java
+++ b/src/org/torproject/ernie/cron/Main.java
@@ -40,7 +40,7 @@ public class Main {
 
     // Prepare consensus health checker
     ConsensusHealthChecker chc = config.getWriteConsensusHealth() ?
-        new ConsensusHealthChecker() : null;
+        new ConsensusHealthChecker(statsDirectory) : null;
 
     // Prepare writing relay descriptors to database
     RelayDescriptorDatabaseImporter rddi =
@@ -75,6 +75,9 @@ public class Main {
     // Write consensus health website
     if (chc != null) {
       chc.writeStatusWebsite();
+      if (config.getWriteNagiosStatusFile()) {
+        chc.writeNagiosStatusFile();
+      }
       chc = null;
     }
 



More information about the tor-commits mailing list