Advertisement
dak1n1

Nagios on-call calendar parser

Sep 13th, 2013
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.34 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. # Stefanie Forrester
  3. # Sept 13 2013
  4. #
  5. # parse_oncall.rb
  6. # A script to parse on-call calendars in iCal format, and generate corresponding Nagios timeperiods.
  7. # Only generates timeperiods for valid Nagios contacts, defined in 'contacts'.
  8. require "rubygems"
  9. require "ri_cal"
  10. require "open-uri"
  11.  
  12. # cfengine stuff
  13. #@@EVAL
  14. #cfprint "contacts = %w[";
  15. #foreach $contact ( keys %nagios_contacts ) {
  16. #        cfprint $contact;
  17. #        cfprint " ";
  18. #}
  19. #cfprint "]";
  20. #@@NOEVAL
  21.  
  22. # test data, since cfengine wont generate this until we apply it to the host
  23. contacts = %w[sedgar acosta abourne]
  24.  
  25. # Generate the schedule hash using cfengine contacts.
  26. # The hash will look like this:
  27. # schedule = { sedgar => ["2013-09-12  10:00-22:00", "2013-09-13  10:00-22:00"],
  28. #              acosta => ["2013-09-14  10:00-22:00", "2013-09-15  10:00-22:00"]
  29. #            }
  30. schedule = Hash.new
  31. contacts.each{|x| schedule[x] = [] }
  32.  
  33. #open("https://my.zimbra.server/home/username/calendar?fmt=ics&tz=UTC", "r") do |file|
  34. File.open("engopstest.ics", "r") do |file|
  35.     calendar = RiCal.parse(file)
  36.     calendar.each do |cal|
  37.         cal.events.each do |event|
  38.          
  39.             # if no end date exists, skip everything  
  40.             who = event.attendee.to_s
  41.  
  42.             unless who.empty?
  43.                 person = who.match(/mailto:(.*)@/)[1]
  44.  
  45.                 # 'person' must match one of our defined nagios contacts
  46.                 if contacts.grep(person).any?
  47.                    contact = contacts.grep(person).to_s
  48.  
  49.                    # don't bother with never-ending events
  50.                    unless event.dtend.to_s.empty?
  51.                        startdate = event.dtstart.strftime("%Y-%m-%d")
  52.                        enddate = event.dtend.strftime("%Y-%m-%d")
  53.                        starttime = event.dtstart.strftime("%R")
  54.                        endtime = event.dtend.strftime("%R")
  55.  
  56.                        # nagios timeperiod formatting, to be associated with this contact
  57.                        nagtime = "#{startdate} - #{enddate}  #{starttime}-#{endtime}"
  58.  
  59.                        # append to the list of on-call dates for a particular contact
  60.                        (schedule[contact] ||= []) << nagtime
  61.                    end
  62.                 end
  63.             end
  64.         end
  65.     end
  66. end
  67.  
  68. #debug
  69. #puts schedule.inspect
  70.  
  71. # Delete old timeperiods.cfg, and create a new one.
  72. # Since this file is entirely managed by this script, it's safe to regenerate from scratch.
  73. # Using the template + output generated in this script,
  74. # we'll create a new nagios config for our on-call timeperiods.
  75.  
  76. newfile = File.open('timeperiods.cfg', 'w')
  77.  
  78. # timeperiods-template.cfg already exists, and contains data such as timeperiod templates (defining workhours in various regions).
  79. # It also contains contact-specific timeperiods, such as 'sedgar_on_call', which define what time zone the contact is in.
  80. # We'll be using those templates to create the actual config that nagios uses.
  81. template = File.open('timeperiods-template.cfg', 'r')
  82. template.readlines.each do |line|
  83.     newfile.puts line
  84.  
  85.     # insert on-call schedules for each contact
  86.     contacts.each do |contact|
  87.         if line.match(/(\s*)timeperiod_name(\s*)#{contact}_on_call/)
  88.             schedule[contact].each { |entry| newfile.puts "    #{entry}" }
  89.         end
  90.     end
  91. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement