cmk_send_agentdata.sh 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/bin/bash
  2. # +------------------------------------------------------------------+
  3. # | ____ _ _ __ __ _ __ |
  4. # | / ___| |__ ___ ___| | __ | \/ | |/ / |
  5. # | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
  6. # | | |___| | | | __/ (__| < | | | | . \ |
  7. # | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
  8. # | |
  9. # | Copyright Jonas Nickl 2021 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. temp_file=$(mktemp)
  28. ## DEBUG Level (BOOL)
  29. if [ "$1" == "--debug" ] ; then DEBUG="1"; fi
  30. be_done()
  31. {
  32. rm ${temp_file}
  33. echo "RC=$STATUS"
  34. ## bad returncode if status nok
  35. if [ "$STATUS" != "OK" ]; then exit 1; fi
  36. }
  37. validate()
  38. ## Verify config dir exists and that the config file exists
  39. {
  40. if [ -d $MK_CONFDIR ] &&
  41. [ -r $MK_CONFDIR/cmkserver.cfg ] &&
  42. OMDURL=`grep OMDURL $MK_CONFDIR/cmkserver.cfg | cut -f2 -d\= | sed 's/"//g'` &&
  43. OMDUSER=`grep OMDUSER $MK_CONFDIR/cmkserver.cfg | cut -f2 -d\= | sed 's/"//g'` &&
  44. OMDPASS=`grep OMDPASS $MK_CONFDIR/cmkserver.cfg | cut -f2 -d\= | sed 's/"//g'`
  45. then
  46. export OMDURL OMDUSER OMDPASS
  47. else
  48. echo "Please set OMDURL, OMDUSER and OMDPASS in"
  49. echo "$MK_CONFDIR/cmkserver.cfg"
  50. exit 1
  51. fi
  52. if ! `which check_mk_agent >/dev/null`; then
  53. echo "check_mk_agent was not found in path, please install it."
  54. exit 1
  55. fi
  56. if ! `which curl >/dev/null`; then
  57. echo "curl was not found in path, please install it."
  58. exit 1
  59. fi
  60. }
  61. get_result()
  62. {
  63. ## decode agent output with base64, remove line breaks and replace plus with html +
  64. export DATA=`check_mk_agent | base64 --wrap=0 | sed 's/+/%2b/g' > ${temp_file}`
  65. }
  66. send_result()
  67. {
  68. [ $DEBUG ] && echo '$(echo "HOSTNAME=$HOSTNAME&DEBUG=$DEBUG&DATA=cat1 ${temp_file}" | curl -X POST --silent --user $OMDUSER:$OMDPASS --data @- $OMDURL'
  69. STATUS=$(echo "HOSTNAME=${HOSTNAME}&DEBUG=${DEBUG}&DATA=`cat ${temp_file}`" | curl -X POST --silent --user ${OMDUSER}:${OMDPASS} --data @- ${OMDURL} )
  70. if [ $? != 0 ]; then
  71. echo "Failed to submit agent output via CURL"
  72. fi
  73. }
  74. ## main :)
  75. validate &&
  76. get_result &&
  77. send_result
  78. ## print return
  79. be_done