Imagine one of your responsibilities is being second level support to a vendor app with a status page at http://server1:9000/
. Let’s assume the vendor app you’re monitoring the status of has a status page that looks something like this when everything is ok.
Status: Ok
and when something is down it looks like this:
Status: Error! System down
Wouldn’t it be great if you could write a script to run in the background to tell you automatically that it went down?
In this recipe we’ll build a mock website status page indicating system availability. Then we’ll update the system availability to give a negative result. Then we’ll build a status-checker to read the availability and take action based on the result. Let’s get started.
In this chapter we assume you have Leiningen installed and have a projects directory set up.
The benefit of this chapter is understanding and applying a use-case to integrate Clojure into your day job through a system monitoring application. It has a minimum of impact on the production application but can add a lot of value.
1. We’re going to create a mock for the vendor status page in order to test our checker. Open a new command prompt in your projects directory, create a new Leiningen Compojure project called display-status
, and change to that directory:
lein new compojure display-status
cd display-status
2. Modify the file src/display_status/handler.clj
to look like the following:
(ns display-status.handler
(:use compojure.core)
(:require [compojure.handler :as handler]
[compojure.route :as route]))
(defroutes app-routes
(GET "/" [] "Status: Ok")
(route/resources "/")
(route/not-found "Not Found"))
(def app
(handler/site app-routes))
3. In a new command prompt in the projects directory, create a new Leiningen project status-checker
, and change to that directory:
lein new status-checker
cd status-checker
4. Modify the file project.clj
to look like the following:
(defproject status-checker "0.1.0-SNAPSHOT"
:dependencies [[org.clojure/clojure "1.7.0"-beta2]]
:main status-checker.core)
5. Modify the file src/status_checker/core.clj
to look like the following:
(ns status-checker.core
(:gen-class)
(:import (java.net URL)))
(def status-page-url "http://localhost:9000/")
(def regex #"Error")
(defn fetch-url [url-to-fetch]
(let [url-obj (URL. url-to-fetch)]
(with-open [buf (clojure.java.io/reader url-obj)]
(apply str (line-seq buf)))))
(defn alert-exists-url []
(let [page-src (fetch-url status-page-url)
regex-result (re-find regex page-src)]
(if (nil? regex-result)
(println "OK")
(println "System down!!!"))))
(defn -main [& args]
(while true
(alert-exists-url)
(Thread/sleep 5000)))
Testing proceeds as follows:
1. At the command prompt in the directory display-status
, run the following:
lein ring server 9000
You should see a new web browser open to a page that looks like the one shown in Figure 28.1.
2. In the project directory display-status
, modify the file src/display_status/handler.clj
to change the line
(GET "/" [] "Status: Ok")
to
(GET "/" [] "Status: Error - System Down!")
and save the file.
3. Now click Reload in the web browser. You should see a page like the one shown in Figure 28.2.
4. In a new command prompt, in the display-status
directory, run the project in Leiningen:
lein run
You should see the following result:
System down!!!
5. Now modify the file in the projects/display-status
directory src/display_status/handler.clj
to change the line
(GET "/" [] "Status: Error - System Down!")
to
(GET "/" [] "Status: Ok")
and save the file. You should now see the status checker change the output message to the following:
Status: Ok
In handler.clj
, we’re using the elegant minimalism of Clojure to throw up a simple http response, indicating a potential system status, and change it on the fly.
In core.clj
we define a url
and a regex
. Using Java Interop, we open the URL and load the contents into a string. We then apply the regex to find a match. If we find a match, then the function outputs an error to stdout. Otherwise, it displays that the system is ok.
18.225.55.193