 
       ;; Send out updated ticks to each agent
       (doseq [host hosts]
-        (send host update-host curr-time))
+        (send host update-host curr-time)
+        (when-let [error (agent-error host)]
+          (log/error error (format "[%s] agent failed; restarting" host))
+          ;; Restart it with exactly the same state. Hopefully that's okay!
+          (restart-agent host @host)))
 
       (Thread/sleep 10)
       (recur curr-time))))
 
 (defn associate-catalog-with-host
-  "Takes the given catalog and transforms it to appear related to
+  "Takes the given `catalog` and transforms it to appear related to
   `hostname`"
   [hostname catalog]
   (-> catalog
       (assoc-in ["data" "name"] hostname)
       (assoc-in ["data" "resources"] (for [resource (get-in catalog ["data" "resources"])]
                                        (update-in resource ["tags"] conj hostname)))))
+
+(defn associate-report-with-host
+  "Takes the given `report` and transforms it to appear related to
+  `hostname`"
+  [hostname report]
+  (assoc report "certname" hostname))
+
 (defn -main
   [& args]
   (let [[options _]     (cli! args
                               ["-C" "--catalogs" "Path to a directory containing sample JSON catalogs (files must end with .json)"]
+                              ["-R" "--reports" "Path to a directory containing sample JSON reports (files must end with .json)"]
                               ["-i" "--runinterval" "What runinterval (in minutes) to use during simulation"]
                               ["-n" "--numhosts" "How many hosts to use during simulation"]
                               ["-rp" "--rand-perc" "What percentage of submitted catalogs are tweaked (int between 0 and 100)"])

    hosts catalog changes in accordance to the users preference)

  * Submit the resulting catalog"
  [{:keys [host lastrun catalog puppetdb-host puppetdb-port run-interval rand-percentage] :as state} clock]
  (if (> (- clock lastrun) run-interval)
    (let [catalog (if (< (rand 100) rand-percentage)
                    (tweak-catalog catalog)
                    catalog)]
      ;; Submit the catalog in a separate thread, so as to not disturb
      ;; the world-loop and otherwise distort the space-time
      ;; continuum.
      (future
        (log/info (format "[%s] submitting catalog" host))
        (submit-catalog puppetdb-host puppetdb-port catalog))
      (assoc state :lastrun clock :catalog catalog))
    state))


                             :rand-percentage rand-percentage
                             :run-interval run-interval
                             :lastrun (- (System/currentTimeMillis) (rand-int run-interval))
                             :catalog (if catalogs (associate-catalog-with-host % (rand-nth catalogs)))
                             :report (if reports (associate-report-with-host % (rand-nth reports)))})
                    hostnames)]

    (when-not catalogs
      (log/info "No catalogs specified; skipping catalog submission"))
    (when-not reports
      (log/info "No reports specified; skipping report submission"))
    ;; Loop forever
    (world-loop hosts)))
