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 (let ((indent-remaining indent-column))
97 (while (< 0 indent-remaining)
99 (setq indent-remaining (- indent-remaining 1)))
102 ;; The text on the current line just moved left/right some amount;
103 ;; call it text-delta. We want to move the point that same distance.
104 (let ((text-delta (- (first-char-offset) orig-first-char-offset)))
105 (goto-char (+ orig-point text-delta))
108 ;; The point should never wind up to the left of indent-column, so
109 ;; if it's there, move it over to indent-column.
110 (if (< (point-offset) indent-column)
111 (goto-char (+ (beginning-of-line-pos) indent-column))
119 (defun last-opening-brace()
120 ;; Get the position of the last opening brace, with
121 ;; respect to the current point.
123 (let ((lob (re-search-backward "{" nil t)))
131 (defun last-closing-brace()
132 ;; Get the position of the last closing brace, with
133 ;; respect to the current point.
135 (let ((lcb (re-search-backward "}" nil t)))
143 (defun nagios-in-block()
144 "Determine if the point is inside of a {} block."
146 ;; If the last brace seen in the buffer is an opening brace, we're
147 ;; in a block. Otherwise, we aren't.
148 (if (>= (last-closing-brace) (last-opening-brace))
154 (defun brace-on-line()
155 ;; Is there a curly brace on this line?
157 (re-search-forward "[{}]" (end-of-line-pos) t)
161 (defun nagios-calculate-indent()
162 "Calculate the level of indentation."
164 ;; We're either inside a block, or we aren't.
165 ;; Initialize the indent variable to either nagios-indent-level
166 ;; or 0 depending on whether or not we're in a block.
167 (let ((indent (if (nagios-in-block)
173 ;; Set the indentation level to 0 if we find either brace on this
186 (defun nagios-insert-right-brace-and-indent()
187 "Insert a '}' character, and indent the line."
194 (defvar nagios-mode-map()
195 "Keymap used in nagios mode.")
197 (when (not nagios-mode-map)
198 (setq nagios-mode-map (make-sparse-keymap))
199 (define-key nagios-mode-map
201 'nagios-insert-right-brace-and-indent)
206 (defconst nagios-directives
208 (concat "^[ \t\r\n]*"
211 '("active_checks_enabled" "address" "alias" "check_command"
212 "check_freshness" "check_interval" "check_period" "checks_enabled"
213 "command_line" "command_name" "contactgroups" "contact_groups"
214 "contactgroup_members" "contact_name" "contactgroup_name" "contacts"
215 "dependent_host_name" "dependent_service_description" "email"
216 "event_handler" "event_handler_enabled" "execution_failure_criteria"
217 "failure_prediction_enabled" "first_notification"
218 "first_notification_delay" "flap_detection_enabled" "freshness_threshold"
219 "friday" "high_flap_threshold" "host_name" "host_notification_commands"
220 "host_notification_options" "host_notification_period"
221 "host_notifications_enabled" "hostgroup_name" "hostgroups"
222 "is_volatile" "last_notification" "low_flap_threshold"
223 "max_check_attempts" "members" "monday" "normal_check_interval"
224 "notes" "notification_failure_criteria"
225 "notification_interval" "notification_options"
226 "notification_period" "notifications_enabled"
227 "obsess_over_service" "pager" "parallelize_check"
228 "parents" "passive_checks_enabled"
229 "process_perf_data" "retain_nonstatus_information"
230 "retain_status_information" "retry_check_interval"
231 "retry_interval" "saturday" "service_description"
232 "service_notification_commands" "service_notification_options"
233 "service_notification_period" "service_notifications_enabled"
234 "servicegroup_name" "stalking_options"
235 "sunday" "thursday" "timeperiod_name" "tuesday" "wednesday") t)
243 (defconst nagios-macros
283 "$CONTACTGROUPALIAS$"
284 "$CONTACTGROUPMEMBERS$"
286 "$CONTACTGROUPNAMES$"
292 "$HOSTACKAUTHORALIAS$"
293 "$HOSTACKAUTHORNAME$"
306 "$HOSTEXECUTIONTIME$"
307 "$HOSTGROUPACTIONURL$"
313 "$HOSTGROUPNOTESURL$"
318 "$HOSTNOTIFICATIONID$"
319 "$HOSTNOTIFICATIONNUMBER$"
321 "$HOSTPERCENTCHANGE$"
332 "$LASTHOSTPROBLEMID$"
334 "$LASTHOSTSTATECHANGE$"
336 "$LASTHOSTUNREACHABLE$"
339 "$LASTSERVICECRITICAL$"
340 "$LASTSERVICEEVENTID$"
342 "$LASTSERVICEPROBLEMID$"
344 "$LASTSERVICESTATECHANGE$"
345 "$LASTSERVICESTATEID$"
346 "$LASTSERVICEUNKNOWN$"
347 "$LASTSERVICEWARNING$"
351 "$LONGSERVICEOUTPUT$"
354 "$MAXSERVICEATTEMPTS$"
356 "$NOTIFICATIONAUTHOR$"
357 "$NOTIFICATIONAUTHORALIAS$"
358 "$NOTIFICATIONAUTHORNAME$"
359 "$NOTIFICATIONCOMMENT$"
360 "$NOTIFICATIONISESCALATED$"
361 "$NOTIFICATIONNUMBER$"
362 "$NOTIFICATIONRECIPIENTS$"
367 "$RETENTIONDATAFILE$"
369 "$SERVICEACKAUTHORALIAS$"
370 "$SERVICEACKAUTHORNAME$"
371 "$SERVICEACKCOMMENT$"
374 "$SERVICECHECKCOMMAND$"
377 "$SERVICEDISPLAYNAME$"
380 "$SERVICEDURATIONSEC$"
382 "$SERVICEEXECUTIONTIME$"
383 "$SERVICEGROUPACTIONURL$"
384 "$SERVICEGROUPALIAS$"
385 "$SERVICEGROUPMEMBERS$"
387 "$SERVICEGROUPNAMES$"
388 "$SERVICEGROUPNOTES$"
389 "$SERVICEGROUPNOTESURL$"
390 "$SERVICEISVOLATILE$"
394 "$SERVICENOTIFICATIONID$"
395 "$SERVICENOTIFICATIONNUMBER$"
397 "$SERVICEPERCENTCHANGE$"
399 "$SERVICEPERFDATAFILE$"
410 "$TOTALHOSTPROBLEMS$"
411 "$TOTALHOSTPROBLEMSUNHANDLED$"
413 "$TOTALHOSTSDOWNUNHANDLED$"
414 "$TOTALHOSTSERVICES$"
415 "$TOTALHOSTSERVICESCRITICAL$"
416 "$TOTALHOSTSERVICESOK$"
417 "$TOTALHOSTSERVICESUNKNOWN$"
418 "$TOTALHOSTSERVICESWARNING$"
419 "$TOTALHOSTSUNREACHABLE$"
420 "$TOTALHOSTSUNREACHABLEUNHANDLED$"
422 "$TOTALSERVICEPROBLEMS$"
423 "$TOTALSERVICEPROBLEMSUNHANDLED$"
424 "$TOTALSERVICESCRITICAL$"
425 "$TOTALSERVICESCRITICALUNHANDLED$"
427 "$TOTALSERVICESUNKNOWN$"
428 "$TOTALSERVICESUNKNOWNUNHANDLED$"
429 "$TOTALSERVICESWARNING$"
430 "$TOTALSERVICESWARNINGUNHANDLED$"
691 (defconst nagios-definitions
694 (concat "^[ \t\r\n]*"
696 "\\(" ;; Stick parenthesis around whatever comes out
697 ;; of regexp-opt. We use this to match a
698 ;; subexpression during font-lock.
702 "define contactgroup"
704 "define hostdependency"
705 "define hostescalation"
708 "define hostgroupescalation"
711 "define servicedependency"
712 "define serviceescalation"
713 "define serviceextinfo"
714 "define servicegroup"
715 "define timeperiod"))
716 ;; This closes the parentheses that we opened
717 "\\)" ;; before regexp-opt.
719 ;; These can be "terminated" by either an opening curly
720 ;; brace, or a space.
727 (defconst nagios-special
729 (concat "^[ \t\r\n]*"
732 '("name" "register" "use") t)
739 ;; The One True Font Locking Variable
741 (defvar nagios-font-lock-keywords
743 (cons nagios-special font-lock-keyword-face)
744 (cons nagios-directives font-lock-variable-name-face)
745 (cons nagios-macros font-lock-constant-face)
746 (cons nagios-definitions '(1 font-lock-function-name-face)))
748 "Rules for highlighting Nagios configuration files."
753 (defvar nagios-mode-syntax-table nil
754 "Syntax table used in nagios-mode buffers.")
755 (if nagios-mode-syntax-table
757 (setq nagios-mode-syntax-table (make-syntax-table))
758 (modify-syntax-entry ?# "< b" nagios-mode-syntax-table) ;; Comment style 1
759 (modify-syntax-entry ?\; "< b" nagios-mode-syntax-table) ;; Comment style 2
760 (modify-syntax-entry ?\n "> b" nagios-mode-syntax-table) ;; End comment
764 ;; Main Mode Function
767 "Major mode for editing Nagios configuration files."
770 (kill-all-local-variables)
771 (make-local-variable 'font-lock-defaults)
772 (make-local-variable 'comment-start)
773 (make-local-variable 'comment-start-skip)
774 (make-local-variable 'comment-end)
775 (make-local-variable 'indent-line-function)
776 (make-local-variable 'syntax-begin-function)
778 (set-syntax-table nagios-mode-syntax-table)
780 (setq mode-name "nagios"
781 major-mode 'nagios-mode
782 indent-line-function 'nagios-indent-line
783 font-lock-defaults '(nagios-font-lock-keywords)
785 comment-start-skip "#\|; +"
788 ;; Since comments and strings do not span multiple lines,
789 ;; the syntax parser can safely start parsing at the beginning
791 syntax-begin-function 'beginning-of-line
795 (use-local-map nagios-mode-map)
797 ;; I don't /think/ I need to define this before attempting
798 ;; to run it. Users can define it if they want.
799 (run-hooks 'nagios-mode-hook)
803 (provide 'nagios-mode)