Advertisement
Guest User

Untitled

a guest
Apr 16th, 2012
1,058
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. #
  4. # Write a command to the Nagios command file to cause
  5. # it to schedule service downtime.
  6. #
  7. # Author: Sam Tilders sam@jovianprojects.com.au
  8. #
  9. # Based on the example event handler scripts and cmd.c/.cgi
  10. #
  11. # Requires:
  12. # GNU Date (for date formatting options)
  13. # Bash > 2.x (for inline variable regex substitution on the comment_data)
  14.  
  15. # Notes:
  16. # 1) In order for Nagios to process any commands that
  17. # are written to the command file, you must enable
  18. # the check_external_commands option in the main
  19. # configuration file.
  20.  
  21. # Caveats:
  22. # Using "date" to validate the command line date format doesn't always pick
  23. # up all mistakes in the date format. Sometimes "date" seems to invent something
  24. # that might have been what you intented.
  25. # Makes no attempt to verify the hostname or service desc before passing them to
  26. # the command pipe. Nagios seems to ignore the command if it doesn't match.
  27. # Write a command to the Nagios command file to cause
  28. # it to schedule host downtime
  29.  
  30. # Example:
  31. # A cron entry to schedule downtime for the nntp service every day at 0400 while
  32. # other cron jobs (locate database update) slow the machine down.
  33. # 30 0 * * * schedule_svc_downtime news NNTP "`date --iso-8601` 04:00:00" "`date --iso-8601` 05:00:00" 1 3600 auto "while cron does things"
  34. # There are utilities such as "shellsupport" that will do date manipulation allowing shell scripts
  35. # to wrap around this command to schedule for days in advance instead of same date.
  36.  
  37. usage()
  38. {
  39. echo "Usage: $0 <hostname> <service desc> <start time> <end time> <fixed> <duration> <user> <comment>"
  40. echo " Times must be in the form \"CCYY-MM-DD HH:mm:ss\" and will probably need to be quoted."
  41. echo " fixed is either 1 or 0 and indicates that the time period is fixed and duration should be ignored"
  42. echo " length of the downtime in seconds, only used if not fixed downtime"
  43. echo " user who is requesting the downtime"
  44. echo " comment to place on the downtime, probably will need to be quoted."
  45. }
  46.  
  47. if [ $# -lt 5 ]; then
  48. echo "$0: Too few parameters"
  49. usage
  50. exit 1
  51. fi
  52. if [ $# -gt 5 ]; then
  53. echo "$0: Too many parameters"
  54. usage
  55. exit 1
  56. fi
  57.  
  58. hostname=$1
  59. raw_start=$2
  60. raw_end=$3
  61. fixed=1
  62. duration=""
  63. comment_author=$4
  64. # command is comment with ; replaced for space
  65. comment_data=$5
  66. #comment_data=${8/;/ }
  67. trigger_id=0
  68.  
  69.  
  70. echocmd="/bin/echo"
  71.  
  72. CommandFile="/usr/local/nagios/var/rw/nagios.cmd"
  73.  
  74. # get the current date/time in seconds since UNIX epoch
  75. datetime=`date +%s`
  76.  
  77.  
  78. start_time=`date -d "$raw_start" +%s`
  79. if [ $? != 0 ]; then
  80. echo "Bad format for start time."
  81. usage
  82. exit 1
  83. fi
  84. end_time=`date -d "$raw_end" +%s`
  85. if [ $? != 0 ]; then
  86. echo "Bad format for end time."
  87. usage
  88. exit 1
  89. fi
  90. if [ $fixed != 1 -a $fixed != 0 ]; then
  91. echo "Fixed must be 1 or 0"
  92. usage
  93. exit 1
  94. fi
  95.  
  96. # create the command line to add to the command file
  97. cmdline="[$datetime] SCHEDULE_HOST_DOWNTIME;$hostname;$start_time;$end_time;$fixed;$trigger_id;$duration;$comment_author;$comment_data"
  98.  
  99. # append the command to the end of the command file
  100. `$echocmd $cmdline >> $CommandFile`
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement