Advertisement
Guest User

Untitled

a guest
Sep 5th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.56 KB | None | 0 0
  1. #!/bin/bash
  2. ##
  3. # Tart Monitoring
  4. #
  5. # Copyright (c) 2013, Tart İnternet Teknolojileri Ticaret AŞ
  6. #
  7. # Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby
  8. # granted, provided that the above copyright notice and this permission notice appear in all copies.
  9. #
  10. # The software is provided "as is" and the author disclaims all warranties with regard to the software including all
  11. # implied warranties of merchantability and fitness. In no event shall the author be liable for any special, direct,
  12. # indirect, or consequential damages or any damages whatsoever resulting from loss of use, data or profits, whether
  13. # in an action of contract, negligence or other tortious action, arising out of or in connection with the use or
  14. # performance of this software.
  15. ##
  16.  
  17. #
  18. # Fetching the parameters
  19. #
  20.  
  21. while getopts "H:P:u:p:s:c:w:qh" opt; do
  22. case $opt in
  23. H ) connectionString=$connectionString"--host=$OPTARG " ;;
  24. P ) connectionString=$connectionString"--port $OPTARG " ;;
  25. u ) connectionString=$connectionString"--user=$OPTARG " ;;
  26. p ) connectionString=$connectionString"--password=$OPTARG " ;;
  27. s ) secondsArray=(${secondsArray[*]} $OPTARG) ;;
  28. c ) criticalLimitsArray=(${criticalLimitsArray[*]} $OPTARG) ;;
  29. w ) warningLimitsArray=(${warningLimitsArray[*]} $OPTARG) ;;
  30. q ) queryMode=1 ;;
  31. h ) echo "a script to monitor MySQL processlist"
  32. echo "Usage:"
  33. echo "$0 -h"
  34. echo "$0 [-H hostname] [-P port] [-u username] [-p password] \\"
  35. echo " [-q] [-s seconds] [-w limits] [-c limits]"
  36. echo "Source:"
  37. echo "github.com/tart/CheckMySQLProcesslist"
  38. exit 3 ;;
  39. \? ) echo "UNKNOWN wrong parameter"
  40. exit 3
  41. esac
  42. done
  43.  
  44. if [ ! "$secondsArray" ]; then
  45. secondsArray=(0)
  46. fi
  47.  
  48. #
  49. # Querying the database
  50. #
  51.  
  52. processlist=$(mysql $connectionString--execute="Show processlist" | sed 1d | sed "/^[^\t]*\tsystem user/d")
  53. if [ ! "$processlist" ]; then
  54. echo "UNKNOWN no processlist"
  55. exit 3
  56. fi
  57.  
  58. globalVariables=$(mysql $connectionString--execute="Show global variables")
  59. if [ ! "$globalVariables" ]; then
  60. echo "UNKNOWN no global variables"
  61. exit 3
  62. fi
  63.  
  64. #
  65. # Parsing the resources
  66. #
  67.  
  68. maxConnections=$(echo "$globalVariables" | grep ^max_connections | cut -f 2)
  69. if [ $queryMode ]; then
  70. timeout=$(echo "$globalVariables" | grep ^interactive_timeout | cut -f 2)
  71. else
  72. timeout=$(echo "$globalVariables" | grep ^wait_timeout | cut -f 2)
  73. fi
  74.  
  75. id=0
  76. for seconds in ${secondsArray[*]}; do
  77. for second in $(echo $seconds | sed "s/,/ /g"); do
  78. secondPercent=$(echo $second | grep % | sed "s/%//")
  79. if [ $secondPercent ]; then
  80. secondArray[$id]=$[$[$timeout*$secondPercent]/100]
  81. elif [ $second ]; then
  82. secondArray[$id]=$second
  83. fi
  84. id=$[$id+1]
  85.  
  86. if [ ! $shortestSecond ] || [ $second -lt $shortestSecond ]; then
  87. shortestSecond=$second
  88. fi
  89. done
  90. done
  91.  
  92. id=0
  93. for criticalLimits in ${criticalLimitsArray[*]}; do
  94. for criticalLimit in $(echo $criticalLimits | sed "s/,/ /g"); do
  95. criticalLimitPercent=$(echo $criticalLimit | grep % | sed "s/%//")
  96. if [ $criticalLimitPercent ]; then
  97. criticalLimitArray[$id]=$[$[$maxConnections*$criticalLimitPercent]/100]
  98. else
  99. criticalLimitArray[$id]=$criticalLimit
  100. fi
  101. id=$[$id+1]
  102. done
  103. done
  104.  
  105. id=0
  106. for warningLimits in ${warningLimitsArray[*]}; do
  107. for warningLimit in $(echo $warningLimits | sed "s/,/ /g"); do
  108. warningLimitPercent=$(echo $warningLimit | grep % | sed "s/%//")
  109. if [ $warningLimitPercent ]; then
  110. warningLimitArray[$id]=$[$[$maxConnections*$warningLimitPercent]/100]
  111. else
  112. warningLimitArray[$id]=$warningLimit
  113. fi
  114. id=$[$id+1]
  115. done
  116. done
  117.  
  118. for id in ${!secondArray[*]}; do
  119. countArray[$id]=0
  120. done
  121.  
  122. if [ $queryMode ]; then
  123. processlist=$(echo "$processlist" | sed "/^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\tNULL/d")
  124. fi
  125.  
  126. for time in $(echo "$processlist" | cut -f 6); do
  127. for id in ${!secondArray[*]}; do
  128. if [ $time -ge ${secondArray[$id]} ]; then
  129. countArray[$id]=$[${countArray[$id]}+1]
  130. fi
  131. done
  132.  
  133. if [ ! $longestTime ] || [ $time -gt $longestTime ]; then
  134. longestTime=$time
  135. fi
  136. done
  137.  
  138. if [ ! $queryMode ]; then
  139. unauthenticatedConnections=0
  140. for user in $(echo "$processlist" | cut -f 2); do
  141. case $user in
  142. "unauthenticated user") unauthenticatedConnections=$[$unauthenticatedConnections+1] ;;
  143. esac
  144. done
  145.  
  146. queringConnections=0
  147. connectingConnections=0
  148. fetchingConnections=0
  149. executingConnections=0
  150. sleepingConnections=0
  151. for command in $(echo "$processlist" | cut -f 5); do
  152. case $command in
  153. "Query") queringConnections=$[$queringConnections+1] ;;
  154. "Connect") connectingConnections=$[$connectingConnections+1] ;;
  155. "Fetch") fetchingConnections=$[$fetchingConnections+1] ;;
  156. "Execute") executingConnections=$[$executingConnections+1] ;;
  157. "Sleep") sleepingConnections=$[$sleepingConnections+1] ;;
  158. esac
  159. done
  160. fi
  161.  
  162. if [ $queryMode ]; then
  163. tempTableUsingQueries=0
  164. preparingQueries=0
  165. sortingQueries=0
  166. lockedQueries=0
  167. returningQueries=0
  168. for state in $(echo "$processlist" | cut -f 7); do
  169. case $state in
  170. "Copying to group table") tempTableUsingQueries=$[$tempTableUsingQueries+1] ;;
  171. "Copying to tmp table") tempTableUsingQueries=$[$tempTableUsingQueries+1] ;;
  172. "Copying to tmp table on disk") tempTableUsingQueries=$[$tempTableUsingQueries+1] ;;
  173. "Creating tmp table") tempTableUsingQueries=$[$tempTableUsingQueries+1] ;;
  174. "optimizing") preparingQueries=$[$preparingQueries+1] ;;
  175. "preparing") preparingQueries=$[$preparingQueries+1] ;;
  176. "statistics") preparingQueries=$[$preparingQueries+1] ;;
  177. "Sorting for group") sortingQueries=$[$sortingQueries+1] ;;
  178. "Sorting for order") sortingQueries=$[$sortingQueries+1] ;;
  179. "Sorting index") sortingQueries=$[$sortingQueries+1] ;;
  180. "Sorting result") sortingQueries=$[$sortingQueries+1] ;;
  181. "Locked") lockedQueries=$[$lockedQueries+1] ;;
  182. "System lock") lockedQueries=$[$lockedQueries+1] ;;
  183. "Table lock") lockedQueries=$[$lockedQueries+1] ;;
  184. "Waiting for commit lock") lockedQueries=$[$lockedQueries+1] ;;
  185. "Waiting for global read lock") lockedQueries=$[$lockedQueries+1] ;;
  186. "Waiting for table level lock") lockedQueries=$[$lockedQueries+1] ;;
  187. "Waiting for event metadata lock") lockedQueries=$[$lockedQueries+1] ;;
  188. "Waiting for schema metadata lock") lockedQueries=$[$lockedQueries+1] ;;
  189. "Waiting for stored function metadata lock") lockedQueries=$[$lockedQueries+1] ;;
  190. "Waiting for stored procedure metadata lock") lockedQueries=$[$lockedQueries+1] ;;
  191. "Waiting for table metadata lock") lockedQueries=$[$lockedQueries+1] ;;
  192. "Waiting for trigger metadata lock") lockedQueries=$[$lockedQueries+1] ;;
  193. "Sending data") returningQueries=$[$returningQueries+1] ;;
  194. "Writing to net") returningQueries=$[$returningQueries+1] ;;
  195. esac
  196. done
  197. fi
  198.  
  199. #
  200. # Preparing the output
  201. #
  202.  
  203. for id in ${!secondArray[*]}; do
  204. second=${secondArray[$id]}
  205. if [ $queryMode ]; then
  206. title="${countArray[$id]} queries"
  207. performanceData=$performanceData"queries"
  208. else
  209. title="${countArray[$id]} connections"
  210. performanceData=$performanceData"connections"
  211. fi
  212.  
  213.  
  214. if [ $second == 0 ]; then
  215. title=$title" of $maxConnections"
  216. else
  217. title=$title" active for"
  218. performanceData=$performanceData"ActiveFor$second"
  219. if [ $second == 3600 ]; then
  220. title=$title" an hour"
  221. elif [ $[$second%3600] == 0 ]; then
  222. title=$title" $[$second/3600] hours"
  223. elif [ $second == 60 ]; then
  224. title=$title" a minute"
  225. elif [ $[$second%60] == 0 ]; then
  226. title=$title" $[$second/60] minutes"
  227. elif [ second == 1 ]; then
  228. title=$title" a second"
  229. else
  230. title=$title" $second seconds"
  231. fi
  232. fi
  233. performanceData=$performanceData"=${countArray[$id]};${warningLimitArray[$id]};${criticalLimitArray[$id]};0;$maxConnections "
  234.  
  235. if [ ${criticalLimitArray[$id]} ] && [ ${countArray[$id]} -ge ${criticalLimitArray[$id]} ]; then
  236. criticalString=$criticalString"$title reached ${criticalLimitArray[$id]}; "
  237. elif [ ${warningLimitArray[$id]} ] && [ ${countArray[$id]} -ge ${warningLimitArray[$id]} ]; then
  238. warningString=$warningString"$title reached ${warningLimitArray[$id]}; "
  239. elif [ $second == $shortestSecond ]; then
  240. okString="$title; "
  241. fi
  242. done
  243.  
  244. if [ ! $queryMode ]; then
  245. performanceData=$performanceData"unauthenticatedConnections=$unauthenticatedConnections;;;0;$maxConnections "
  246. performanceData=$performanceData"queringConnections=$queringConnections;;;0;$maxConnections "
  247. performanceData=$performanceData"connectingConnections=$connectingConnections;;;0;$maxConnections "
  248. performanceData=$performanceData"fetchingConnections=$fetchingConnections;;;0;$maxConnections "
  249. performanceData=$performanceData"executingConnections=$executingConnections;;;0;$maxConnections "
  250. performanceData=$performanceData"sleepingConnections=$sleepingConnections;;;0;$maxConnections "
  251. fi
  252.  
  253. if [ $queryMode ]; then
  254. performanceData=$performanceData"tempTableUsingQueries=$tempTableUsingQueries;;;0;$maxConnections "
  255. performanceData=$performanceData"preparingQueries=$preparingQueries;;;0;$maxConnections "
  256. performanceData=$performanceData"sortingQueries=$sortingQueries;;;0;$maxConnections "
  257. performanceData=$performanceData"lockedQueries=$lockedQueries;;;0;$maxConnections "
  258. performanceData=$performanceData"returningQueries=$returningQueries;;;0;$maxConnections "
  259.  
  260. if [ $longestTime -gt 0 ]; then
  261. longestQuery=$(echo "$processlist" | grep -P "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t$longestTime" | head -n 1)
  262. longestQueryString=$longestQueryString"time is $(echo "$longestQuery" | cut -f 6); "
  263. longestQueryString=$longestQueryString"id is $(echo "$longestQuery" | cut -f 1); "
  264. longestQueryString=$longestQueryString"user is $(echo "$longestQuery" | cut -f 2); "
  265. longestQueryHost=$(echo "$longestQuery" | cut -f 3)
  266. if [ $longestQueryHost != "localhost" ]; then
  267. longestQueryString=$longestQueryString"host is $(echo "$longestQuery" | cut -f 3); "
  268. fi
  269. longestQuerySchema=$(echo "$longestQuery" | cut -f 4)
  270. if [ $longestQuerySchema != "NULL" ]; then
  271. longestQueryString=$longestQueryString"schema is $(echo "$longestQuery" | cut -f 4); "
  272. fi
  273. longestQueryString=$longestQueryString"executing \"$(echo "$longestQuery" | cut -f 8)\"; "
  274. longestQueryString=$longestQueryString"state is $(echo "$longestQuery" | cut -f 7); "
  275. fi
  276. fi
  277. performanceData=$performanceData"longestTime=$longestTime;;;0;$timeout "
  278.  
  279. #
  280. # Quiting
  281. #
  282.  
  283. if [ "$criticalString" ]; then
  284. echo -n "CRITICAL CheckMySQLProcesslist $criticalString"
  285. fi
  286. if [ "$warningString" ]; then
  287. echo -n "WARNING CheckMySQLProcesslist $warningString"
  288. fi
  289. if [ ! "$criticalString" ] && [ ! "$warningString" ]; then
  290. echo -n "OK CheckMySQLProcesslist $okString"
  291. fi
  292.  
  293. if [ "$longestQueryString" ]; then
  294. echo -n "longest query: $longestQueryString"
  295. fi
  296. echo "| $performanceData"
  297.  
  298. if [ "$criticalString" ]; then
  299. exit 2
  300. fi
  301. if [ "$warningString" ]; then
  302. exit 1
  303. fi
  304. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement