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 (kill-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-word(regexp)
159 "Takes a regular expression as an argument, and adds the word boundary condition to the beginning and end of it."
161 ;; I consider neither a hyphen nor an underscore to be a word
162 ;; boundary for the purpose of syntax highlighting.
163 (concat "[^_-]\\<\\(" regexp "\\)\\>[^_-]")
167 (defun regexp-alt-raw(element-list)
168 "Takes a list of elements, and returns the string '\\(element1\\|element2...\\)'"
170 ;; This is necessary since regexp-opt does not accept regular
171 ;; expressions as arguments. We use regexp-opt when we can, of
174 (let ((regexp "\\("))
175 (mapcar (lambda(elem)
176 (setq regexp (concat regexp "\\(" elem "\\)" "\\|")))
178 (concat (substring regexp 0 -2) ; Cut the last "\\|"
185 ;; Syntax Highlighting Patterns
187 (defconst nagios-comments
195 (defconst nagios-directives
198 '("active_checks_enabled" "address" "alias" "check_command"
199 "check_freshness" "check_interval" "check_period" "checks_enabled"
200 "command_line" "command_name" "contact_groups" "contact_name"
201 "contactgroup_name" "dependent_host_name"
202 "dependent_service_description" "email" "event_handler"
203 "event_handler_enabled" "execution_failure_criteria"
204 "failure_prediction_enabled" "first_notification"
205 "flap_detection_enabled" "freshness_threshold" "friday"
206 "high_flap_threshold" "host_name" "host_notification_commands"
207 "host_notification_options" "host_notification_period"
208 "hostgroup_name" "hostgroups" "is_volatile" "last_notification"
209 "low_flap_threshold" "max_check_attempts"
210 "members" "monday" "normal_check_interval"
211 "notification_failure_criteria"
212 "notification_interval" "notification_options"
213 "notification_period" "notifications_enabled"
214 "obsess_over_service" "pager" "parallelize_check"
215 "parents" "passive_checks_enabled"
216 "process_perf_data" "retain_nonstatus_information"
217 "retain_status_information" "retry_check_interval"
218 "retry_interval" "saturday" "service_description"
219 "service_notification_commands"
220 "service_notification_options" "service_notification_period"
221 "servicegroup_name" "stalking_options"
222 "sunday" "thursday" "timeperiod_name" "tuesday" "wednesday") t))
227 (defconst nagios-macros
230 '("\\$CONTACT\\(NAME\\|ALIAS\\|EMAIL\\|PAGER\\)\\$"
231 "\\$HOST\\(NAME\\|ALIAS\\|ADDRESS\\|STATE\\)\\$"
232 "\\$\\(ARG\\|USER\\)\\([1-9]\\|[1-2][0-9]\\|3[0-2]\\)\\$"
233 "\\$SERVICE\\(DESC\\|STATE\\)\\$"
234 "\\$\\(OUTPUT\\|PERFDATA\\|EXECUTIONTIME\\|LATENCY\\)\\$"
235 "\\$NOTIFICATION\\(TYPE\\|NUMBER\\)\\$"
236 "\\$\\(\\(SHORT\\)?DATETIME\\|DATE\\|TIME\\|TIMET\\)\\$"
237 "\\$\\(LASTSTATECHANGE\\|STATETYPE\\)\\$"
238 "\\$ADMIN\\(EMAIL\\|PAGER\\)\\$"
239 "\\$\\(SERVICE\\|HOST\\)ATTEMPT\\$")))
244 (defconst nagios-definitions
247 '("define +\\(host\\|service\\|timeperiod\\|contact\\|command\\)"
248 "define +\\(host\\|contact\\|service\\)group"
249 "define +\\(service\\|host\\)dependency"
250 "define +\\(service\\|host\\|hostgroup\\)escalation")))
255 (defconst nagios-special
258 '("name" "register" "use") t))
263 ;; The One True Font Locking Variable
265 (defvar nagios-font-lock-keywords
268 ;; This first bit of ugliness allows us to override any
269 ;; other font-locking with the comment font.
270 (cons nagios-comments '(0 font-lock-comment-delimiter-face t))
272 ;; The rest just map regular expressions to font faces.
273 (cons (regexp-word nagios-directives) font-lock-variable-name-face)
274 (cons (regexp-word nagios-macros) font-lock-constant-face)
275 (cons (regexp-word nagios-definitions) font-lock-function-name-face)
276 (cons (regexp-word nagios-special) font-lock-keyword-face))
278 "Rules for highlighting Nagios configuration files."
283 ;; Main Mode Function
286 "Major mode for editing Nagios configuration files."
290 ;; Initializing. This is actually important to cover
291 ;; up some Emacs stupidity. Font locking won't occur
293 (kill-all-local-variables)
295 ;; Set up indentation handling using the functions
297 (make-local-variable 'indent-line-function)
298 (setq indent-line-function 'nagios-indent-line)
300 ;; Configure font locking. Set the defaults to something
301 ;; sensible, defined earlier.
302 (make-local-variable 'font-lock-defaults)
303 (setq font-lock-defaults '(nagios-font-lock-keywords nil t))
306 (use-local-map nagios-mode-map)
309 (setq mode-name "nagios"
310 major-mode 'nagios-mode)
312 ;; I don't /think/ I need to define this before attempting
313 ;; to run it. Users can define it if they want.
314 (run-hooks 'nagios-mode-hook)
318 (provide 'nagios-mode)