Advertisement
Anonymouse-cat

OMG-head-spinning starts @ line 131

Mar 20th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns nagcommentyanker.core
  2.   (:require [org.httpkit.client :as http]
  3.             [clojure.string :as string]
  4.             [clojure.tools.cli :refer [parse-opts]]
  5.             [dire.core :refer [with-handler supervise]])
  6.   (:import (jline.console ConsoleReader))
  7.   (:gen-class))
  8.  
  9. (def cli-options
  10.   [["-s" "--startnum STARTNUM" "Starting comment number: required."
  11.     :parse-fn #(Integer/parseInt %)
  12.     :missing "Startnum must be specified."
  13.     ]
  14.  
  15.    ["-e" "--endnum ENDNUM" "Ending comment number: required."
  16.     :parse-fn #(Integer/parseInt %)
  17.     :missing "Endnum must be specified."
  18.     ]
  19.  
  20.    ["-u" "--username NAGIOS_USERNAME" "Nagios username: required."
  21.     :validate [#(< 3 (count %)) "Username must be specified."]
  22.     :missing "Username must be specified."
  23.     ]
  24.  
  25.    ["-a" "--hostname NAGIOS_HOSTNAME" "Nagios host FQDN: required."
  26.     :validate [#(< 3 (count %)) "Hostname must be specified."]
  27.     :missing "Hostname must be specified."
  28.     ]
  29.  
  30.    ["-h"
  31.     :id :help]])
  32.  
  33.  
  34. ;; this is a helper function for the tools.cli stuff.  If your input isn't what it
  35. ;; is expecting it will call this.
  36. (defn error-msg [errors]
  37.     (str "The following errors occurred while parsing your command:\n\n"
  38.          (string/join \newline errors)))
  39.  
  40.  
  41. ;; this is a helper fucntion for the tools.cli stuff.  This contains the logic that
  42. ;; makes your -h output look cool on the command line.
  43. (defn usage [options-summary]
  44.   (->> ["Delete comments from Nagios."
  45.         ""
  46.         "Usage: nagcommentdeleter [options]"
  47.         ""
  48.         "Options:"
  49.         options-summary]
  50.        (string/join \newline)))
  51.  
  52.  
  53. ;; this is a helper function for the tools.cli stuff.
  54. (defn exit [status msg]
  55.   (println msg)
  56.     (System/exit status))
  57.  
  58.  
  59. ;; get the user's password
  60. (defn get-user-input []
  61.    (let [cr (ConsoleReader.) ;; create the object and assign it to "cr".
  62.          secretpassword (.readLine cr "Password: " \*)]
  63.      secretpassword))
  64.  
  65.  
  66. ;; this is the meat and potatoes.
  67. (defn doit [cmdline-options get-user-input]
  68.  
  69.   ;; here we sequence through the output of range, and for every step through the
  70.   ;; sequence we feed the sequence data into the http-kit client/get.
  71.   (let [rng
  72.         (range (:startnum cmdline-options) (:endnum cmdline-options))
  73.         requests
  74.         (doall
  75.          (for [r rng]
  76.            (let
  77.                [http-options {
  78.                               :query-params {:cmd_typ "4" :cmd_mod "2" :com_id r :btnSubmit "Commit"}
  79.                               :timeout 3000
  80.                               :basic-auth [(:username cmdline-options) get-user-input]
  81.                               :keepalive 60000}
  82.                 url (str "https://" (:hostname cmdline-options) "/cgi-bin/nagios3/cmd.cgi?")
  83.                 futures (http/get url http-options)]
  84.              (for [resp futures]
  85.                (println (-> @resp :opt :url))))))]))
  86.  
  87. ;; this is my first attempt at handling the JVM Exceptions in a "nicer looking" way.
  88. (with-handler #'doit
  89.   "Here's an optional docstring about the handler."
  90.   java.net.UnknownHostException
  91.   ;;; 'e' is the exception object, 'args' are the original arguments to the task.
  92.   (fn [e & args] (println "Hostname lookup failed.")))
  93.  
  94. (with-handler #'doit
  95.   "Here's an optional docstring about the handler."
  96.   [java.net.ConnectException,
  97.    java.net.SocketTimeoutException]
  98.   (fn [e & args] (println "Connection refused or timed out.")))
  99.  
  100. (with-handler #'doit
  101.   "Here's an optional docstring about the handler."
  102.   [java.net.URISyntaxException]
  103.   (fn [e & args] (println "Your URL has a syntax error, you probably have a malformed host fqdn.")))
  104.  
  105. (with-handler #'doit
  106.   "Here's an optional docstring about the handler."
  107.   [:status 404]
  108.   (fn [e & args] (println "http status code unsuccessful: " e)))
  109.  
  110. (with-handler #'doit
  111.   "Here's an optional docstring about the handler."
  112.   [:status 401]
  113.   (fn [e & args] (println "http status code unsuccessful: " e)))
  114.  
  115. (defn -main
  116.   "Right now this uses http-kit to http/get stuff.  Helpers are added for getting cmdline args,
  117.   handling exceptions, masking a user password, etc."
  118.   [& args]
  119.  
  120.   ;; This is the point where we call the tools.cli functions to transform
  121.   ;; the cmdline args and perform logic like making -h print out
  122.   ;; the available options to the user.
  123.   (let [{:keys [options arguments errors summary]} (parse-opts args cli-options)]
  124.     ;; Handle help and error conditions
  125.  
  126.    (cond
  127.       (:help options) (exit 0 (usage summary))
  128.       (<= (count args) 1) (exit 1 (usage summary))
  129.       (< (count errors) 0) (exit 1 (error-msg errors)))
  130.  
  131.     (supervise #'doit options (get-user-input))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement