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."
40 (nagios-indent-to (nagios-calculate-indent))
44 (defun beginning-of-line-pos()
45 ;; Return the point position corresponding to the beginning
46 ;; of the current line.
53 (defun end-of-line-pos()
54 ;; Return the point position corresponding to the end
55 ;; of the current line.
63 ;; How far are we from the beginning of the line?
64 (- (point) (beginning-of-line-pos))
67 (defun first-char-offset()
68 ;; How far is the first character on this line
69 ;; from the beginning of the line?
72 (skip-chars-forward " \t")
76 (defun first-char-pos()
77 ;; What's the position of the first character on this line?
78 (+ (beginning-of-line-pos) (first-char-offset))
81 (defun nagios-indent-to(indent-column)
82 "Indent the current line to column indent-column."
83 ;; Store the point in orig-pos.
84 (let ((orig-point (point)))
86 ;; And store the offset of the first character (with respect to the
87 ;; beginning of the line) in orig-first-char-offset.
88 (let ((orig-first-char-offset (first-char-offset)))
90 ;; Delete any leading whitespace, and move the point to the
91 ;; beginning of the line.
92 (delete-region (beginning-of-line-pos) (first-char-pos))
95 ;; Now insert indent-column spaces.
96 (while (< 0 indent-column)
98 (setq indent-column (- indent-column 1)))
100 ;; The text on the current line just moved left/right some amount;
101 ;; call it text-delta. We want to move the point that same distance.
102 (let ((text-delta (- (first-char-offset) orig-first-char-offset)))
103 (goto-char (+ orig-point text-delta))
106 ;; The point should never wind up to the left of indent-column, so
107 ;; if it's there, move it over to indent-column.
108 (if (< (point-offset) indent-column)
109 (goto-char indent-column)
117 (defun last-opening-brace()
118 ;; Get the position of the last opening brace, with
119 ;; respect to the current point.
121 (let ((lob (re-search-backward "{" nil t)))
129 (defun last-closing-brace()
130 ;; Get the position of the last closing brace, with
131 ;; respect to the current point.
133 (let ((lcb (re-search-backward "}" nil t)))
141 (defun nagios-in-block()
142 "Determine if the point is inside of a {} block."
144 ;; If the last brace seen in the buffer is an opening brace, we're
145 ;; in a block. Otherwise, we aren't.
146 (if (>= (last-closing-brace) (last-opening-brace))
152 (defun brace-on-line()
153 ;; Is there a curly brace on this line?
155 (re-search-forward "[{}]" (end-of-line-pos) t)
159 (defun nagios-calculate-indent()
160 "Calculate the level of indentation."
161 ;; We're either inside a block, or we aren't.
163 ;; Initialize the indent variable to either nagios-indent-level
164 ;; or 0 depending on whether or not we're in a block.
165 (let ((indent (if (nagios-in-block)
166 (setq indent nagios-indent-level)
171 ;; Set the indentation level to 0 if we find either brace on this
184 (defun nagios-insert-right-brace-and-indent()
185 "Insert a '}' character, and indent the line."
192 (defvar nagios-mode-map()
193 "Keymap used in nagios mode.")
195 (when (not nagios-mode-map)
196 (setq nagios-mode-map (make-sparse-keymap))
197 (define-key nagios-mode-map
199 'nagios-insert-right-brace-and-indent)
204 (defconst nagios-directives
206 (concat "^[ \t\r\n]*"
209 '("active_checks_enabled" "address" "alias" "check_command"
210 "check_freshness" "check_interval" "check_period" "checks_enabled"
211 "command_line" "command_name" "contactgroups" "contact_groups"
212 "contactgroup_members" "contact_name" "contactgroup_name" "contacts"
213 "dependent_host_name" "dependent_service_description" "email"
214 "event_handler" "event_handler_enabled" "execution_failure_criteria"
215 "failure_prediction_enabled" "first_notification"
216 "first_notification_delay" "flap_detection_enabled" "freshness_threshold"
217 "friday" "high_flap_threshold" "host_name" "host_notification_commands"
218 "host_notification_options" "host_notification_period"
219 "host_notifications_enabled" "hostgroup_name" "hostgroups"
220 "is_volatile" "last_notification" "low_flap_threshold"
221 "max_check_attempts" "members" "monday" "normal_check_interval"
222 "notes" "notification_failure_criteria"
223 "notification_interval" "notification_options"
224 "notification_period" "notifications_enabled"
225 "obsess_over_service" "pager" "parallelize_check"
226 "parents" "passive_checks_enabled"
227 "process_perf_data" "retain_nonstatus_information"
228 "retain_status_information" "retry_check_interval"
229 "retry_interval" "saturday" "service_description"
230 "service_notification_commands" "service_notification_options"
231 "service_notification_period" "service_notifications_enabled"
232 "servicegroup_name" "stalking_options"
233 "sunday" "thursday" "timeperiod_name" "tuesday" "wednesday") t)
241 (defconst nagios-macros
281 "$CONTACTGROUPALIAS$"
282 "$CONTACTGROUPMEMBERS$"
284 "$CONTACTGROUPNAMES$"
290 "$HOSTACKAUTHORALIAS$"
291 "$HOSTACKAUTHORNAME$"
304 "$HOSTEXECUTIONTIME$"
305 "$HOSTGROUPACTIONURL$"
311 "$HOSTGROUPNOTESURL$"
316 "$HOSTNOTIFICATIONID$"
317 "$HOSTNOTIFICATIONNUMBER$"
319 "$HOSTPERCENTCHANGE$"
330 "$LASTHOSTPROBLEMID$"
332 "$LASTHOSTSTATECHANGE$"
334 "$LASTHOSTUNREACHABLE$"
337 "$LASTSERVICECRITICAL$"
338 "$LASTSERVICEEVENTID$"
340 "$LASTSERVICEPROBLEMID$"
342 "$LASTSERVICESTATECHANGE$"
343 "$LASTSERVICESTATEID$"
344 "$LASTSERVICEUNKNOWN$"
345 "$LASTSERVICEWARNING$"
349 "$LONGSERVICEOUTPUT$"
352 "$MAXSERVICEATTEMPTS$"
354 "$NOTIFICATIONAUTHOR$"
355 "$NOTIFICATIONAUTHORALIAS$"
356 "$NOTIFICATIONAUTHORNAME$"
357 "$NOTIFICATIONCOMMENT$"
358 "$NOTIFICATIONISESCALATED$"
359 "$NOTIFICATIONNUMBER$"
360 "$NOTIFICATIONRECIPIENTS$"
365 "$RETENTIONDATAFILE$"
367 "$SERVICEACKAUTHORALIAS$"
368 "$SERVICEACKAUTHORNAME$"
369 "$SERVICEACKCOMMENT$"
372 "$SERVICECHECKCOMMAND$"
375 "$SERVICEDISPLAYNAME$"
378 "$SERVICEDURATIONSEC$"
380 "$SERVICEEXECUTIONTIME$"
381 "$SERVICEGROUPACTIONURL$"
382 "$SERVICEGROUPALIAS$"
383 "$SERVICEGROUPMEMBERS$"
385 "$SERVICEGROUPNAMES$"
386 "$SERVICEGROUPNOTES$"
387 "$SERVICEGROUPNOTESURL$"
388 "$SERVICEISVOLATILE$"
392 "$SERVICENOTIFICATIONID$"
393 "$SERVICENOTIFICATIONNUMBER$"
395 "$SERVICEPERCENTCHANGE$"
397 "$SERVICEPERFDATAFILE$"
408 "$TOTALHOSTPROBLEMS$"
409 "$TOTALHOSTPROBLEMSUNHANDLED$"
411 "$TOTALHOSTSDOWNUNHANDLED$"
412 "$TOTALHOSTSERVICES$"
413 "$TOTALHOSTSERVICESCRITICAL$"
414 "$TOTALHOSTSERVICESOK$"
415 "$TOTALHOSTSERVICESUNKNOWN$"
416 "$TOTALHOSTSERVICESWARNING$"
417 "$TOTALHOSTSUNREACHABLE$"
418 "$TOTALHOSTSUNREACHABLEUNHANDLED$"
420 "$TOTALSERVICEPROBLEMS$"
421 "$TOTALSERVICEPROBLEMSUNHANDLED$"
422 "$TOTALSERVICESCRITICAL$"
423 "$TOTALSERVICESCRITICALUNHANDLED$"
425 "$TOTALSERVICESUNKNOWN$"
426 "$TOTALSERVICESUNKNOWNUNHANDLED$"
427 "$TOTALSERVICESWARNING$"
428 "$TOTALSERVICESWARNINGUNHANDLED$"
689 (defconst nagios-definitions
692 (concat "^[ \t\r\n]*"
694 "\\(" ;; Stick parenthesis around whatever comes out
695 ;; of regexp-opt. We use this to match a
696 ;; subexpression during font-lock.
700 "define contactgroup"
702 "define hostdependency"
703 "define hostescalation"
706 "define hostgroupescalation"
709 "define servicedependency"
710 "define serviceescalation"
711 "define serviceextinfo"
712 "define servicegroup"
713 "define timeperiod"))
714 ;; This closes the parentheses that we opened
715 "\\)" ;; before regexp-opt.
717 ;; These can be "terminated" by either an opening curly
718 ;; brace, or a space.
725 (defconst nagios-special
727 (concat "^[ \t\r\n]*"
730 '("name" "register" "use") t)
737 ;; The One True Font Locking Variable
739 (defvar nagios-font-lock-keywords
741 (cons nagios-special font-lock-keyword-face)
742 (cons nagios-directives font-lock-variable-name-face)
743 (cons nagios-macros font-lock-constant-face)
744 (cons nagios-definitions '(1 font-lock-function-name-face)))
746 "Rules for highlighting Nagios configuration files."
751 (defvar nagios-mode-syntax-table nil
752 "Syntax table used in nagios-mode buffers.")
753 (if nagios-mode-syntax-table
755 (setq nagios-mode-syntax-table (make-syntax-table))
756 (modify-syntax-entry ?# "< b" nagios-mode-syntax-table) ;; Comment style 1
757 (modify-syntax-entry ?\; "< b" nagios-mode-syntax-table) ;; Comment style 2
758 (modify-syntax-entry ?\n "> b" nagios-mode-syntax-table) ;; End comment
762 ;; Main Mode Function
765 "Major mode for editing Nagios configuration files."
768 (kill-all-local-variables)
769 (make-local-variable 'font-lock-defaults)
770 (make-local-variable 'comment-start)
771 (make-local-variable 'comment-start-skip)
772 (make-local-variable 'comment-end)
773 (make-local-variable 'indent-line-function)
774 (make-local-variable 'syntax-begin-function)
776 (set-syntax-table nagios-mode-syntax-table)
778 (setq mode-name "nagios"
779 major-mode 'nagios-mode
780 indent-line-function 'nagios-indent-line
781 font-lock-defaults '(nagios-font-lock-keywords)
783 comment-start-skip "#\|; +"
786 ;; Since comments and strings do not span multiple lines,
787 ;; the syntax parser can safely start parsing at the beginning
789 syntax-begin-function 'beginning-of-line
793 (use-local-map nagios-mode-map)
795 ;; I don't /think/ I need to define this before attempting
796 ;; to run it. Users can define it if they want.
797 (run-hooks 'nagios-mode-hook)
801 (provide 'nagios-mode)