2 ;; nagios-mode, an Emacs mode for Nagios <http://www.nagios.org/>
3 ;; configuration files.
5 ;; Copyright Michael Orlitzky
7 ;; http://michael.orlitzky.com/
9 ;; This program is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; http://www.fsf.org/licensing/licenses/gpl.html
28 (defcustom nagios-indent-level 2
29 "Number of spaces in one indentation (tab)."
30 :type 'integer :group 'nagios
37 (defun nagios-indent-line(&optional flag)
38 "Indents a line, taking nesting into account."
39 (nagios-indent-to (nagios-calculate-indent))
44 (defun nagios-indent-to(indent-column)
45 "Indent the current line to column indent-column."
49 (setq pos-offset (- pos bol))
51 (setq first-char-offset
52 (skip-chars-forward " \t"))
55 (+ bol first-char-offset))
57 (delete-region bol first-char-pos)
61 (setq pos-change (- indent-column first-char-offset))
62 (setq pos-offset (+ pos-offset pos-change))
64 (if (<= pos-offset indent-column)
65 (setq pos-offset indent-column))
67 (while (< 0 indent-column)
69 (setq indent-column (- indent-column 1)))
71 (goto-char (+ bol pos-offset))
77 (defun nagios-in-block()
78 "Determine if the point is inside of a {} block."
82 ;; Get the position of the last opening and closing braces, with
83 ;; respect to the current point
84 (setq last-opening-brace (re-search-backward "{" nil t))
87 (setq last-closing-brace (re-search-backward "}" nil t))
90 ;; If either is nil (not found) just set it to -1, so the comparison
92 (if (not last-opening-brace)
93 (setq last-opening-brace -1))
95 (if (not last-closing-brace)
96 (setq last-closing-brace -1))
98 ;; If the last brace seen in the buffer is an opening brace, we're
99 ;; in a block. Otherwise, we aren't.
100 (if (>= last-closing-brace last-opening-brace)
107 (defun nagios-calculate-indent()
108 "Calculate the level of indentation."
109 ;; We're either inside a block, or we aren't.
113 (if (nagios-in-block)
114 (setq indent nagios-indent-level))
122 ;; Set the indentation level to 0 if we find either brace on this
124 (if (re-search-forward "[{}]" eol t)
136 (defun nagios-insert-right-brace-and-indent()
137 "Insert a '}' character, and indent the line."
144 (defvar nagios-mode-map()
145 "Keymap used in nagios mode.")
147 (when (not nagios-mode-map)
148 (setq nagios-mode-map (make-sparse-keymap))
149 (define-key nagios-mode-map
151 'nagios-insert-right-brace-and-indent)
156 ;; Regular Expression Transformations
158 (defun regexp-alt-raw(element-list)
159 "Takes a list of elements, and returns the string '\\(element1\\|element2...\\)'"
161 ;; This is necessary since regexp-opt does not accept regular
162 ;; expressions as arguments. We use regexp-opt when we can, of
165 (let ((regexp "\\("))
166 (mapcar (lambda(elem)
167 (setq regexp (concat regexp "\\(" elem "\\)" "\\|")))
169 (concat (substring regexp 0 -2) ; Cut the last "\\|"
176 (defconst nagios-directives
178 (concat "^[ \t\r\n]*"
181 '("active_checks_enabled" "address" "alias" "check_command"
182 "check_freshness" "check_interval" "check_period" "checks_enabled"
183 "command_line" "command_name" "contactgroups" "contact_groups"
184 "contactgroup_members" "contact_name" "contactgroup_name" "contacts"
185 "dependent_host_name" "dependent_service_description" "email"
186 "event_handler" "event_handler_enabled" "execution_failure_criteria"
187 "failure_prediction_enabled" "first_notification"
188 "first_notification_delay" "flap_detection_enabled" "freshness_threshold"
189 "friday" "high_flap_threshold" "host_name" "host_notification_commands"
190 "host_notification_options" "host_notification_period"
191 "host_notifications_enabled" "hostgroup_name" "hostgroups"
192 "is_volatile" "last_notification" "low_flap_threshold"
193 "max_check_attempts" "members" "monday" "normal_check_interval"
194 "notes" "notification_failure_criteria"
195 "notification_interval" "notification_options"
196 "notification_period" "notifications_enabled"
197 "obsess_over_service" "pager" "parallelize_check"
198 "parents" "passive_checks_enabled"
199 "process_perf_data" "retain_nonstatus_information"
200 "retain_status_information" "retry_check_interval"
201 "retry_interval" "saturday" "service_description"
202 "service_notification_commands" "service_notification_options"
203 "service_notification_period" "service_notifications_enabled"
204 "servicegroup_name" "stalking_options"
205 "sunday" "thursday" "timeperiod_name" "tuesday" "wednesday") t)
213 (defconst nagios-macros
253 "$CONTACTGROUPALIAS$"
254 "$CONTACTGROUPMEMBERS$"
256 "$CONTACTGROUPNAMES$"
262 "$HOSTACKAUTHORALIAS$"
263 "$HOSTACKAUTHORNAME$"
276 "$HOSTEXECUTIONTIME$"
277 "$HOSTGROUPACTIONURL$"
283 "$HOSTGROUPNOTESURL$"
288 "$HOSTNOTIFICATIONID$"
289 "$HOSTNOTIFICATIONNUMBER$"
291 "$HOSTPERCENTCHANGE$"
302 "$LASTHOSTPROBLEMID$"
304 "$LASTHOSTSTATECHANGE$"
306 "$LASTHOSTUNREACHABLE$"
309 "$LASTSERVICECRITICAL$"
310 "$LASTSERVICEEVENTID$"
312 "$LASTSERVICEPROBLEMID$"
314 "$LASTSERVICESTATECHANGE$"
315 "$LASTSERVICESTATEID$"
316 "$LASTSERVICEUNKNOWN$"
317 "$LASTSERVICEWARNING$"
321 "$LONGSERVICEOUTPUT$"
324 "$MAXSERVICEATTEMPTS$"
326 "$NOTIFICATIONAUTHOR$"
327 "$NOTIFICATIONAUTHORALIAS$"
328 "$NOTIFICATIONAUTHORNAME$"
329 "$NOTIFICATIONCOMMENT$"
330 "$NOTIFICATIONISESCALATED$"
331 "$NOTIFICATIONNUMBER$"
332 "$NOTIFICATIONRECIPIENTS$"
337 "$RETENTIONDATAFILE$"
339 "$SERVICEACKAUTHORALIAS$"
340 "$SERVICEACKAUTHORNAME$"
341 "$SERVICEACKCOMMENT$"
344 "$SERVICECHECKCOMMAND$"
347 "$SERVICEDISPLAYNAME$"
350 "$SERVICEDURATIONSEC$"
352 "$SERVICEEXECUTIONTIME$"
353 "$SERVICEGROUPACTIONURL$"
354 "$SERVICEGROUPALIAS$"
355 "$SERVICEGROUPMEMBERS$"
357 "$SERVICEGROUPNAMES$"
358 "$SERVICEGROUPNOTES$"
359 "$SERVICEGROUPNOTESURL$"
360 "$SERVICEISVOLATILE$"
364 "$SERVICENOTIFICATIONID$"
365 "$SERVICENOTIFICATIONNUMBER$"
367 "$SERVICEPERCENTCHANGE$"
369 "$SERVICEPERFDATAFILE$"
380 "$TOTALHOSTPROBLEMS$"
381 "$TOTALHOSTPROBLEMSUNHANDLED$"
383 "$TOTALHOSTSDOWNUNHANDLED$"
384 "$TOTALHOSTSERVICES$"
385 "$TOTALHOSTSERVICESCRITICAL$"
386 "$TOTALHOSTSERVICESOK$"
387 "$TOTALHOSTSERVICESUNKNOWN$"
388 "$TOTALHOSTSERVICESWARNING$"
389 "$TOTALHOSTSUNREACHABLE$"
390 "$TOTALHOSTSUNREACHABLEUNHANDLED$"
392 "$TOTALSERVICEPROBLEMS$"
393 "$TOTALSERVICEPROBLEMSUNHANDLED$"
394 "$TOTALSERVICESCRITICAL$"
395 "$TOTALSERVICESCRITICALUNHANDLED$"
397 "$TOTALSERVICESUNKNOWN$"
398 "$TOTALSERVICESUNKNOWNUNHANDLED$"
399 "$TOTALSERVICESWARNING$"
400 "$TOTALSERVICESWARNINGUNHANDLED$"
661 (defconst nagios-definitions
664 (concat "^[ \t\r\n]*"
669 "define contactgroup"
671 "define hostdependency"
672 "define hostescalation"
675 "define hostgroupescalation"
678 "define servicedependency"
679 "define serviceescalation"
680 "define serviceextinfo"
681 "define servicegroup"
682 "define timeperiod"))
684 ;; These can be "terminated" by either an opening curly
685 ;; brace, or a space.
692 (defconst nagios-special
694 (concat "^[ \t\r\n]*"
697 '("name" "register" "use") t)
704 ;; The One True Font Locking Variable
706 (defvar nagios-font-lock-keywords
708 (cons nagios-special font-lock-keyword-face)
709 (cons nagios-directives font-lock-variable-name-face)
710 (cons nagios-macros font-lock-constant-face)
711 (cons nagios-definitions '(1 font-lock-function-name-face)))
713 "Rules for highlighting Nagios configuration files."
718 (defvar nagios-mode-syntax-table nil
719 "Syntax table used in nagios-mode buffers.")
720 (if nagios-mode-syntax-table
722 (setq nagios-mode-syntax-table (make-syntax-table))
723 (modify-syntax-entry ?# "< b" nagios-mode-syntax-table) ;; Comment style 1
724 (modify-syntax-entry ?\; "< b" nagios-mode-syntax-table) ;; Comment style 2
725 (modify-syntax-entry ?\n "> b" nagios-mode-syntax-table) ;; End comment
729 ;; Main Mode Function
732 "Major mode for editing Nagios configuration files."
735 (kill-all-local-variables)
736 (make-local-variable 'font-lock-defaults)
737 (make-local-variable 'comment-start)
738 (make-local-variable 'comment-start-skip)
739 (make-local-variable 'comment-end)
740 (make-local-variable 'indent-line-function)
741 (make-local-variable 'syntax-begin-function)
743 (set-syntax-table nagios-mode-syntax-table)
745 (setq mode-name "nagios"
746 major-mode 'nagios-mode
747 indent-line-function 'nagios-indent-line
748 font-lock-defaults '(nagios-font-lock-keywords)
750 comment-start-skip "#\|; +"
753 ;; Since comments and strings do not span multiple lines,
754 ;; the syntax parser can safely start parsing at the beginning
756 syntax-begin-function 'beginning-of-line
760 (use-local-map nagios-mode-map)
762 ;; I don't /think/ I need to define this before attempting
763 ;; to run it. Users can define it if they want.
764 (run-hooks 'nagios-mode-hook)
768 (provide 'nagios-mode)