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 last-opening-brace()
78 ;; Get the position of the last opening brace, with
79 ;; respect to the current point.
81 (let ((lob (re-search-backward "{" nil t)))
89 (defun last-closing-brace()
90 ;; Get the position of the last closing brace, with
91 ;; respect to the current point.
93 (let ((lcb (re-search-backward "}" nil t)))
101 (defun nagios-in-block()
102 "Determine if the point is inside of a {} block."
104 ;; If the last brace seen in the buffer is an opening brace, we're
105 ;; in a block. Otherwise, we aren't.
106 (if (>= (last-closing-brace) (last-opening-brace))
113 (defun nagios-calculate-indent()
114 "Calculate the level of indentation."
115 ;; We're either inside a block, or we aren't.
119 (if (nagios-in-block)
120 (setq indent nagios-indent-level))
128 ;; Set the indentation level to 0 if we find either brace on this
130 (if (re-search-forward "[{}]" eol t)
142 (defun nagios-insert-right-brace-and-indent()
143 "Insert a '}' character, and indent the line."
150 (defvar nagios-mode-map()
151 "Keymap used in nagios mode.")
153 (when (not nagios-mode-map)
154 (setq nagios-mode-map (make-sparse-keymap))
155 (define-key nagios-mode-map
157 'nagios-insert-right-brace-and-indent)
162 (defconst nagios-directives
164 (concat "^[ \t\r\n]*"
167 '("active_checks_enabled" "address" "alias" "check_command"
168 "check_freshness" "check_interval" "check_period" "checks_enabled"
169 "command_line" "command_name" "contactgroups" "contact_groups"
170 "contactgroup_members" "contact_name" "contactgroup_name" "contacts"
171 "dependent_host_name" "dependent_service_description" "email"
172 "event_handler" "event_handler_enabled" "execution_failure_criteria"
173 "failure_prediction_enabled" "first_notification"
174 "first_notification_delay" "flap_detection_enabled" "freshness_threshold"
175 "friday" "high_flap_threshold" "host_name" "host_notification_commands"
176 "host_notification_options" "host_notification_period"
177 "host_notifications_enabled" "hostgroup_name" "hostgroups"
178 "is_volatile" "last_notification" "low_flap_threshold"
179 "max_check_attempts" "members" "monday" "normal_check_interval"
180 "notes" "notification_failure_criteria"
181 "notification_interval" "notification_options"
182 "notification_period" "notifications_enabled"
183 "obsess_over_service" "pager" "parallelize_check"
184 "parents" "passive_checks_enabled"
185 "process_perf_data" "retain_nonstatus_information"
186 "retain_status_information" "retry_check_interval"
187 "retry_interval" "saturday" "service_description"
188 "service_notification_commands" "service_notification_options"
189 "service_notification_period" "service_notifications_enabled"
190 "servicegroup_name" "stalking_options"
191 "sunday" "thursday" "timeperiod_name" "tuesday" "wednesday") t)
199 (defconst nagios-macros
239 "$CONTACTGROUPALIAS$"
240 "$CONTACTGROUPMEMBERS$"
242 "$CONTACTGROUPNAMES$"
248 "$HOSTACKAUTHORALIAS$"
249 "$HOSTACKAUTHORNAME$"
262 "$HOSTEXECUTIONTIME$"
263 "$HOSTGROUPACTIONURL$"
269 "$HOSTGROUPNOTESURL$"
274 "$HOSTNOTIFICATIONID$"
275 "$HOSTNOTIFICATIONNUMBER$"
277 "$HOSTPERCENTCHANGE$"
288 "$LASTHOSTPROBLEMID$"
290 "$LASTHOSTSTATECHANGE$"
292 "$LASTHOSTUNREACHABLE$"
295 "$LASTSERVICECRITICAL$"
296 "$LASTSERVICEEVENTID$"
298 "$LASTSERVICEPROBLEMID$"
300 "$LASTSERVICESTATECHANGE$"
301 "$LASTSERVICESTATEID$"
302 "$LASTSERVICEUNKNOWN$"
303 "$LASTSERVICEWARNING$"
307 "$LONGSERVICEOUTPUT$"
310 "$MAXSERVICEATTEMPTS$"
312 "$NOTIFICATIONAUTHOR$"
313 "$NOTIFICATIONAUTHORALIAS$"
314 "$NOTIFICATIONAUTHORNAME$"
315 "$NOTIFICATIONCOMMENT$"
316 "$NOTIFICATIONISESCALATED$"
317 "$NOTIFICATIONNUMBER$"
318 "$NOTIFICATIONRECIPIENTS$"
323 "$RETENTIONDATAFILE$"
325 "$SERVICEACKAUTHORALIAS$"
326 "$SERVICEACKAUTHORNAME$"
327 "$SERVICEACKCOMMENT$"
330 "$SERVICECHECKCOMMAND$"
333 "$SERVICEDISPLAYNAME$"
336 "$SERVICEDURATIONSEC$"
338 "$SERVICEEXECUTIONTIME$"
339 "$SERVICEGROUPACTIONURL$"
340 "$SERVICEGROUPALIAS$"
341 "$SERVICEGROUPMEMBERS$"
343 "$SERVICEGROUPNAMES$"
344 "$SERVICEGROUPNOTES$"
345 "$SERVICEGROUPNOTESURL$"
346 "$SERVICEISVOLATILE$"
350 "$SERVICENOTIFICATIONID$"
351 "$SERVICENOTIFICATIONNUMBER$"
353 "$SERVICEPERCENTCHANGE$"
355 "$SERVICEPERFDATAFILE$"
366 "$TOTALHOSTPROBLEMS$"
367 "$TOTALHOSTPROBLEMSUNHANDLED$"
369 "$TOTALHOSTSDOWNUNHANDLED$"
370 "$TOTALHOSTSERVICES$"
371 "$TOTALHOSTSERVICESCRITICAL$"
372 "$TOTALHOSTSERVICESOK$"
373 "$TOTALHOSTSERVICESUNKNOWN$"
374 "$TOTALHOSTSERVICESWARNING$"
375 "$TOTALHOSTSUNREACHABLE$"
376 "$TOTALHOSTSUNREACHABLEUNHANDLED$"
378 "$TOTALSERVICEPROBLEMS$"
379 "$TOTALSERVICEPROBLEMSUNHANDLED$"
380 "$TOTALSERVICESCRITICAL$"
381 "$TOTALSERVICESCRITICALUNHANDLED$"
383 "$TOTALSERVICESUNKNOWN$"
384 "$TOTALSERVICESUNKNOWNUNHANDLED$"
385 "$TOTALSERVICESWARNING$"
386 "$TOTALSERVICESWARNINGUNHANDLED$"
647 (defconst nagios-definitions
650 (concat "^[ \t\r\n]*"
652 "\\(" ;; Stick parenthesis around whatever comes out
653 ;; of regexp-opt. We use this to match a
654 ;; subexpression during font-lock.
658 "define contactgroup"
660 "define hostdependency"
661 "define hostescalation"
664 "define hostgroupescalation"
667 "define servicedependency"
668 "define serviceescalation"
669 "define serviceextinfo"
670 "define servicegroup"
671 "define timeperiod"))
672 ;; This closes the parentheses that we opened
673 "\\)" ;; before regexp-opt.
675 ;; These can be "terminated" by either an opening curly
676 ;; brace, or a space.
683 (defconst nagios-special
685 (concat "^[ \t\r\n]*"
688 '("name" "register" "use") t)
695 ;; The One True Font Locking Variable
697 (defvar nagios-font-lock-keywords
699 (cons nagios-special font-lock-keyword-face)
700 (cons nagios-directives font-lock-variable-name-face)
701 (cons nagios-macros font-lock-constant-face)
702 (cons nagios-definitions '(1 font-lock-function-name-face)))
704 "Rules for highlighting Nagios configuration files."
709 (defvar nagios-mode-syntax-table nil
710 "Syntax table used in nagios-mode buffers.")
711 (if nagios-mode-syntax-table
713 (setq nagios-mode-syntax-table (make-syntax-table))
714 (modify-syntax-entry ?# "< b" nagios-mode-syntax-table) ;; Comment style 1
715 (modify-syntax-entry ?\; "< b" nagios-mode-syntax-table) ;; Comment style 2
716 (modify-syntax-entry ?\n "> b" nagios-mode-syntax-table) ;; End comment
720 ;; Main Mode Function
723 "Major mode for editing Nagios configuration files."
726 (kill-all-local-variables)
727 (make-local-variable 'font-lock-defaults)
728 (make-local-variable 'comment-start)
729 (make-local-variable 'comment-start-skip)
730 (make-local-variable 'comment-end)
731 (make-local-variable 'indent-line-function)
732 (make-local-variable 'syntax-begin-function)
734 (set-syntax-table nagios-mode-syntax-table)
736 (setq mode-name "nagios"
737 major-mode 'nagios-mode
738 indent-line-function 'nagios-indent-line
739 font-lock-defaults '(nagios-font-lock-keywords)
741 comment-start-skip "#\|; +"
744 ;; Since comments and strings do not span multiple lines,
745 ;; the syntax parser can safely start parsing at the beginning
747 syntax-begin-function 'beginning-of-line
751 (use-local-map nagios-mode-map)
753 ;; I don't /think/ I need to define this before attempting
754 ;; to run it. Users can define it if they want.
755 (run-hooks 'nagios-mode-hook)
759 (provide 'nagios-mode)