cmk_send_agentdata.sh 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/bin/bash
  2. # +------------------------------------------------------------------+
  3. # | ____ _ _ __ __ _ __ |
  4. # | / ___| |__ ___ ___| | __ | \/ | |/ / |
  5. # | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
  6. # | | |___| | | | __/ (__| < | | | | . \ |
  7. # | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
  8. # | |
  9. # | Copyright Jonas Nickl 2016 development@ichalsroot.de |
  10. # +------------------------------------------------------------------+
  11. #
  12. # This file is an addon for Check_MK.
  13. # The official homepage for this check is at https://blog.ichalsroot.de
  14. #
  15. # check_mk is free software; you can redistribute it and/or modify it
  16. # under the terms of the GNU General Public License as published by
  17. # the Free Software Foundation in version 2. check_mk is distributed
  18. # in the hope that it will be useful, but WITHOUT ANY WARRANTY; with-
  19. # out even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  20. # PARTICULAR PURPOSE. See the GNU General Public License for more de-
  21. # tails. You should have received a copy of the GNU General Public
  22. # License along with GNU Make; see the file COPYING. If not, write
  23. # to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
  24. # Boston, MA 02110-1301 USA.
  25. HOSTNAME=`hostname`
  26. MK_CONFDIR=/etc/check_mk
  27. ## DEBUG Level (BOOL)
  28. if [ "$1" == "--debug" ] ; then DEBUG="1"; fi
  29. be_done()
  30. {
  31. echo "RC=$STATUS"
  32. ## bad returncode if status nok
  33. if [ "$STATUS" != "OK" ]; then exit 1; fi
  34. }
  35. validate()
  36. ## Verify config dir exists and that the config file exists
  37. {
  38. if [ -d $MK_CONFDIR ] &&
  39. [ -r $MK_CONFDIR/cmkserver.cfg ] &&
  40. OMDURL=`grep OMDURL $MK_CONFDIR/cmkserver.cfg | cut -f2 -d\= | sed 's/"//g'` &&
  41. OMDUSER=`grep OMDUSER $MK_CONFDIR/cmkserver.cfg | cut -f2 -d\= | sed 's/"//g'` &&
  42. OMDPASS=`grep OMDPASS $MK_CONFDIR/cmkserver.cfg | cut -f2 -d\= | sed 's/"//g'`
  43. then
  44. export OMDURL OMDUSER OMDPASS
  45. else
  46. echo "Please set OMDURL, OMDUSER and OMDPASS in"
  47. echo "$MK_CONFDIR/cmkserver.cfg"
  48. exit 1
  49. fi
  50. if ! `which check_mk_agent >/dev/null`; then
  51. echo "check_mk_agent was not found in path, please install it."
  52. exit 1
  53. fi
  54. if ! `which curl >/dev/null`; then
  55. echo "curl was not found in path, please install it."
  56. exit 1
  57. fi
  58. }
  59. get_result()
  60. {
  61. ## decode agent output with base64, remove line breaks and replace plus with html +
  62. export DATA=$(check_mk_agent | base64 --wrap=0 | sed 's/+/%2b/g' )
  63. }
  64. send_result()
  65. {
  66. [ $DEBUG ] && echo "curl -X POST --silent --user $OMDUSER:$OMDPASS --data \"DATA=$DATA&HOSTNAME=$HOSTNAME&DEBUG=$DEBUG\" $OMDURL"
  67. STATUS=$(curl -X POST --silent --user $OMDUSER:$OMDPASS --data "DATA=$DATA&HOSTNAME=$HOSTNAME&DEBUG=$DEBUG" "$OMDURL")
  68. if [ $? != 0 ]; then
  69. echo "Failed to submit agent output via CURL"
  70. fi
  71. }
  72. ## main :)
  73. validate &&
  74. get_result &&
  75. send_result
  76. ## print return
  77. be_done