Skip to content

Commit

Permalink
Nodes are grouped by types now, with group controls
Browse files Browse the repository at this point in the history
  • Loading branch information
dsabanin committed Oct 22, 2013
1 parent 08b7e17 commit fb96139
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 22 deletions.
7 changes: 5 additions & 2 deletions config.clj.example
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{
:name "My Project"
:nodes ["myhost-1"
"myhost-2"] ;; The names need to be configured in your ~/.ssh/config file
:nodes ["Web" ["myhost-1"
"myhost-2"]
"File" ["ftp-1"
"ftp-2"]] ;; The names need to be configured in your ~/.ssh/config file
:host "127.0.0.1"
:port 9090
:cmd-timeout 10000 ;; Time any command may take
:ssh-template "ssh -tt %s '%s'" ;; two args: node and command we'll be running
Expand Down
94 changes: 74 additions & 20 deletions src/monitmonit/service.clj
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@
(control-panel auto-refresh?)
[:a.navbar-brand {:href "#"} (title)]]]]
[:div.container
body]])))
body
[:div.footer
[:hr]
[:p "served by " [:a {:href "https://github.com/dsabanin/monitmonit"} "monitmonit"]
" © " [:a {:href "http://dsabanin.com"} "dsabanin"] " 2013"]
[:br]]]])))

(defn ssh
[node cmd]
Expand Down Expand Up @@ -126,13 +131,19 @@
(bad-states status) (status-btn "btn-danger" node process status)
:else (status-btn "btn-default" node process status))))

(defn render-process
[node [process status]]
[:tr
[:td process]
[:td (render-status node process status)]])

(defn render-summary
[node summary]
(if (map? summary)
[:table.table.table-striped {:id "accordion"}
(map (fn [[process status]]
(vector :tr [:td process] [:td (render-status node process status)]))
summary)]
(->> summary
(into (sorted-map))
(map (partial render-process node)))]
[:p summary]))

(defn top-button
Expand Down Expand Up @@ -167,14 +178,15 @@
(top-button "stop" (str "/run?cmd=stop&node=" node) "default")]
[:div.panel-collapse.collapse.out.nodes {:id node-id}
[:div.panel-body
(render-summary node @summary)
[:div.well.small-well [:small (:out @uptime)]]]]]))
(render-summary node @summary)]
[:div.panel-footer
[:small (clojure.string/replace-first (str (:out @uptime)) #",\s*load" "<br />load")]]]]))

(defn nav-button
[name href]
[:a.btn.btn-default.navbar-btn.btn-sm {:href href} name])

(defn group-action-btn
(defn all-action-btn
[class name href confirm]
[:a.btn.navbar-btn.btn-sm
{:class class
Expand All @@ -194,32 +206,63 @@
(nav-button "Stop Auto-Refresh" "/")
(nav-button "Auto-Refresh" "/?refresh=true"))
" "
(group-action-btn "btn-success"
(all-action-btn "btn-success"
"Start All" "/run?cmd=start"
(str "Are you sure you want to start all servers in "
(:name *config*) "?"))
" "
(group-action-btn "btn-danger"
(all-action-btn "btn-danger"
"Stop All" "/run?cmd=stop"
(str "Are you sure you want to stop all servers in "
(:name *config*) "?"))
" "
(group-action-btn "btn-danger"
(all-action-btn "btn-danger"
"Restart All" "/run?cmd=restart"
(str "Are you sure you want to restart all servers in "
(:name *config*) "?"))))

(defn group-action-btn
[class name href confirm]
[:a.btn.btn-xs
{:class class
:href href
:onclick (format "return confirm('%s')" confirm)}
name])

(defn render-group
[[group nodes]]
(html
[:h3 group
" "
(group-action-btn "btn-default"
"Start All" (format "/run?cmd=start&group=%s" group)
(str "Are you sure you want to start all " group " servers in "
(:name *config*) "?"))
" "
(group-action-btn "btn-default"
"Stop All" (format "/run?cmd=stop&group=%s" group)
(str "Are you sure you want to stop all " group " servers in "
(:name *config*) "?"))
" "
(group-action-btn "btn-default"
"Restart All" (format "/run?cmd=restart&group=%s" group)
(str "Are you sure you want to restart all " group " servers in "
(:name *config*) "?"))]
(->> nodes
(map #(future (render-node %)))
(doall)
(map deref)
(partition-all 3)
(mapcat (fn [nodes]
(vector [:div.row (map #(vector :div.col-md-4 %) nodes)]))))))

(defn dashboard
[request]
(ring-resp/response
(layout request
(->> (:nodes *config*)
(map #(future (render-node %)))
(doall)
(map deref)
(partition-all 3)
(mapcat (fn [nodes]
(vector [:div.row (map #(vector :div.col-md-4 %) nodes)])))))))
(partition 2)
(map render-group)))))

(defn validate-cmd
[cmd]
Expand All @@ -233,15 +276,25 @@
[node process]
((into #{} (keys (monit-summary node))) process))

(defn monit-all
[cmd nodes]
(->> nodes
(pmap #(prn (monit % (str cmd " all"))))
(doall)))

(defn run [req]
(when-let [cmd (validate-cmd (get-in req [:query-params :cmd]))]
(if-let [node (validate-node (get-in req [:query-params :node]))]
(if-let [process (validate-process node (get-in req [:query-params :process]))]
(prn (monit node (str cmd " " process)))
(prn (monit node (str cmd " all"))))
(->> (:nodes *config*)
(pmap #(prn (monit % (str cmd " all"))))
(doall))))
(if-let [group (get-in req [:query-params :group])]
(if-let [group-nodes (get (apply hash-map (:nodes *config*)) group)]
(monit-all cmd group-nodes))
(->> (apply hash-map (:nodes *config*))
(vals)
(flatten)
(monit-all cmd)))))
(ring-resp/redirect "/"))

(defroutes routes
Expand Down Expand Up @@ -277,4 +330,5 @@
;; to enable Tomcat)
;;::bootstrap/host "localhost"
::bootstrap/type :jetty
::bootstrap/port (:port *config* 8080)})
::bootstrap/port (:port *config* 8080)
::bootstrap/host (:host *config* "0.0.0.0")})

0 comments on commit fb96139

Please sign in to comment.