Advertisement
Guest User

splunk_license_alert_if_over

a guest
May 14th, 2015
1,073
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.40 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #
  4. # Jem Jensen
  5. # May 14, 2015
  6. #
  7. # Splunk license usage checker
  8. #
  9.  
  10. ### CONFIG ############
  11.  
  12. USER="admin"
  13. PASS="changeme"
  14. SEARCH='search index=_internal source=*license_usage.log type="RolloverSummary" earliest=-1d@d   | eval _time=_time - 1440 | bin _time span=1d | stats latest(b) AS b latest(stacksz) AS stacksz by slave, pool, _time | stats sum(b) AS volumeB max(stacksz) AS stacksz by _time | eval pctused=round(volumeB/stacksz*100,0) | head 2 |table pctused'
  15.  
  16. #######################
  17.  
  18. DEBUG=false
  19.  
  20. # Handle program arguments
  21. if [ "$1" != "" ]
  22. then
  23.         if [ "$1" == "-d" ]
  24.         then
  25.                 DEBUG=true
  26.         else
  27.                 BASE=`basename $0`
  28.                 echo "Usage: $BASE [-d]"
  29.                 echo "  d: show debugging output"
  30.                 exit
  31.         fi
  32. fi
  33.  
  34. # Intercept any errors that occur
  35. function error_interceptor() {
  36.         # If this error is a 2, display an error message and exit
  37.         if [ "$1" == "2" ]
  38.         then
  39.                 echo "invalid REST result"
  40.                 exit $?
  41.         fi
  42.  
  43.         return
  44. }
  45.  
  46. trap 'error_interceptor ${?}' ERR
  47.  
  48. function isSplunkLicenseOver() {
  49.         if $DEBUG
  50.         then
  51.                 echo "$SEARCH"
  52.         fi
  53.  
  54.         # Just grab the last 2 lines to avoid the column header
  55.         RESULTS=`curl -k -u $USER:$PASS --data-urlencode search="$SEARCH" -d "output_mode=csv" https://localhost:8089/servicesNS/admin/search/search/jobs/export 2>/dev/null`
  56.         LINECOUNT=`echo "$RESULTS"|wc -l`
  57.         RESULTS=`echo "$RESULTS"|tail -2`
  58.  
  59.         if $DEBUG
  60.         then
  61.                 echo "# Results: $LINECOUNT"
  62.                 echo "Results: $RESULTS"
  63.         fi
  64.  
  65.         # If there were more than 3 lines returned, something went wrong
  66.         if [ $LINECOUNT -gt 3 ]
  67.         then
  68.                 # Return err
  69.                 return 2
  70.         fi
  71.  
  72.         # Check the results to see if any are over 100% usage
  73.         for i in $RESULTS
  74.         do
  75.                 if [ $i -gt 100 ]
  76.                 then
  77.                         # Return true - it's over!
  78.                         return 0
  79.                 fi
  80.         done
  81.  
  82.         # Return false - it's under
  83.         return 1
  84. }
  85.  
  86. isSplunkLicenseOver
  87. RETVAL=$?
  88.  
  89. if $DEBUG
  90. then
  91.         echo "isSplunkLicenseOver returned: $RETVAL"
  92. fi
  93.  
  94. if [ $RETVAL -eq 0 ]
  95. then
  96.         echo "Splunk license exceeded!"
  97. fi
  98.  
  99. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement