Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (ns nagcommentyanker.core
- (:require [org.httpkit.client :as http]
- [clojure.string :as string]
- [clojure.tools.cli :refer [parse-opts]]
- [dire.core :refer [with-handler supervise]])
- (:import (jline.console ConsoleReader))
- (:gen-class))
- (def cli-options
- [["-s" "--startnum STARTNUM" "Starting comment number: required."
- :parse-fn #(Integer/parseInt %)
- :missing "Startnum must be specified."
- ]
- ["-e" "--endnum ENDNUM" "Ending comment number: required."
- :parse-fn #(Integer/parseInt %)
- :missing "Endnum must be specified."
- ]
- ["-u" "--username NAGIOS_USERNAME" "Nagios username: required."
- :validate [#(< 3 (count %)) "Username must be specified."]
- :missing "Username must be specified."
- ]
- ["-a" "--hostname NAGIOS_HOSTNAME" "Nagios host FQDN: required."
- :validate [#(< 3 (count %)) "Hostname must be specified."]
- :missing "Hostname must be specified."
- ]
- ["-h"
- :id :help]])
- ;; this is a helper function for the tools.cli stuff. If your input isn't what it
- ;; is expecting it will call this.
- (defn error-msg [errors]
- (str "The following errors occurred while parsing your command:\n\n"
- (string/join \newline errors)))
- ;; this is a helper fucntion for the tools.cli stuff. This contains the logic that
- ;; makes your -h output look cool on the command line.
- (defn usage [options-summary]
- (->> ["Delete comments from Nagios."
- ""
- "Usage: nagcommentdeleter [options]"
- ""
- "Options:"
- options-summary]
- (string/join \newline)))
- ;; this is a helper function for the tools.cli stuff.
- (defn exit [status msg]
- (println msg)
- (System/exit status))
- ;; get the user's password
- (defn get-user-input []
- (let [cr (ConsoleReader.) ;; create the object and assign it to "cr".
- secretpassword (.readLine cr "Password: " \*)]
- secretpassword))
- ;; this is the meat and potatoes.
- (defn doit [cmdline-options get-user-input]
- ;; here we sequence through the output of range, and for every step through the
- ;; sequence we feed the sequence data into the http-kit client/get.
- (let [rng
- (range (:startnum cmdline-options) (:endnum cmdline-options))
- requests
- (doall
- (for [r rng]
- (let
- [http-options {
- :query-params {:cmd_typ "4" :cmd_mod "2" :com_id r :btnSubmit "Commit"}
- :timeout 3000
- :basic-auth [(:username cmdline-options) get-user-input]
- :keepalive 60000}
- url (str "https://" (:hostname cmdline-options) "/cgi-bin/nagios3/cmd.cgi?")
- futures (http/get url http-options)]
- (for [resp futures]
- (println (-> @resp :opt :url))))))]))
- ;; this is my first attempt at handling the JVM Exceptions in a "nicer looking" way.
- (with-handler #'doit
- "Here's an optional docstring about the handler."
- java.net.UnknownHostException
- ;;; 'e' is the exception object, 'args' are the original arguments to the task.
- (fn [e & args] (println "Hostname lookup failed.")))
- (with-handler #'doit
- "Here's an optional docstring about the handler."
- [java.net.ConnectException,
- java.net.SocketTimeoutException]
- (fn [e & args] (println "Connection refused or timed out.")))
- (with-handler #'doit
- "Here's an optional docstring about the handler."
- [java.net.URISyntaxException]
- (fn [e & args] (println "Your URL has a syntax error, you probably have a malformed host fqdn.")))
- (with-handler #'doit
- "Here's an optional docstring about the handler."
- [:status 404]
- (fn [e & args] (println "http status code unsuccessful: " e)))
- (with-handler #'doit
- "Here's an optional docstring about the handler."
- [:status 401]
- (fn [e & args] (println "http status code unsuccessful: " e)))
- (defn -main
- "Right now this uses http-kit to http/get stuff. Helpers are added for getting cmdline args,
- handling exceptions, masking a user password, etc."
- [& args]
- ;; This is the point where we call the tools.cli functions to transform
- ;; the cmdline args and perform logic like making -h print out
- ;; the available options to the user.
- (let [{:keys [options arguments errors summary]} (parse-opts args cli-options)]
- ;; Handle help and error conditions
- (cond
- (:help options) (exit 0 (usage summary))
- (<= (count args) 1) (exit 1 (usage summary))
- (< (count errors) 0) (exit 1 (error-msg errors)))
- (supervise #'doit options (get-user-input))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement