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))
118 (defun char-is-commented(pos)
119 "True if the character at position pos is commented, nil otherwise."
122 (re-search-backward "#" (beginning-of-line-pos) t)
126 (defun char-is-commented-and-valid(pos)
127 "True if the character at position pos is commented and non-nil.
131 (char-is-commented pos)
136 (defun last-opening-brace()
137 "Returns the position of the last opening brace, with
138 respect to the current point. Ignores braces which
141 (let ((lob (re-search-backward "{" nil t)))
143 (while (char-is-commented-and-valid lob)
145 (setq lob (re-search-backward "{" nil t))
156 (defun last-closing-brace()
157 "Get the position of the last closing brace, with
158 respect to the current point. Ignores braces which
161 (let ((lcb (re-search-backward "}" nil t)))
163 (while (char-is-commented-and-valid lcb)
165 (setq lcb (re-search-backward "}" nil t))
175 (defun nagios-in-block()
176 "Determine if the point is inside of a {} block."
178 ;; If the last brace seen in the buffer is an opening brace, we're
179 ;; in a block. Otherwise, we aren't.
180 (if (>= (last-closing-brace) (last-opening-brace))
186 (defun brace-on-line()
187 ;; Is there a curly brace on this line?
190 (re-search-forward "[{}]" (end-of-line-pos) t)
195 (defun nagios-calculate-indent()
196 "Calculate the level of indentation."
198 ;; We're either inside a block, or we aren't.
199 ;; Initialize the indent variable to either nagios-indent-level
200 ;; or 0 depending on whether or not we're in a block.
201 (let ((indent (if (nagios-in-block)
207 ;; Set the indentation level to 0 if we find either brace on this
209 (if (and (brace-on-line) (not (char-is-commented (brace-on-line))))
219 (defun nagios-insert-right-brace-and-indent()
220 "Insert a '}' character, and indent the line."
227 (defvar nagios-mode-map()
228 "Keymap used in nagios mode.")
230 (when (not nagios-mode-map)
231 (setq nagios-mode-map (make-sparse-keymap))
232 (define-key nagios-mode-map
234 'nagios-insert-right-brace-and-indent)
239 (defconst nagios-directives
241 (concat "^[ \t\r\n]*"
244 '("active_checks_enabled" "address" "alias" "check_command"
245 "check_freshness" "check_interval" "check_period" "checks_enabled"
246 "command_line" "command_name" "contactgroups" "contact_groups"
247 "contactgroup_members" "contact_name" "contactgroup_name" "contacts"
248 "dependent_host_name" "dependent_service_description" "email"
249 "event_handler" "event_handler_enabled" "execution_failure_criteria"
250 "failure_prediction_enabled" "first_notification"
251 "first_notification_delay" "flap_detection_enabled" "freshness_threshold"
252 "friday" "high_flap_threshold" "host_name" "host_notification_commands"
253 "host_notification_options" "host_notification_period"
254 "host_notifications_enabled" "hostgroup_name" "hostgroups"
255 "is_volatile" "last_notification" "low_flap_threshold"
256 "max_check_attempts" "members" "monday" "normal_check_interval"
257 "notes" "notification_failure_criteria"
258 "notification_interval" "notification_options"
259 "notification_period" "notifications_enabled"
260 "obsess_over_service" "pager" "parallelize_check"
261 "parents" "passive_checks_enabled"
262 "process_perf_data" "retain_nonstatus_information"
263 "retain_status_information" "retry_check_interval"
264 "retry_interval" "saturday" "service_description"
265 "service_notification_commands" "service_notification_options"
266 "service_notification_period" "service_notifications_enabled"
267 "servicegroup_name" "stalking_options"
268 "sunday" "thursday" "timeperiod_name" "tuesday" "wednesday") t)
276 (defconst nagios-macros
316 "$CONTACTGROUPALIAS$"
317 "$CONTACTGROUPMEMBERS$"
319 "$CONTACTGROUPNAMES$"
325 "$HOSTACKAUTHORALIAS$"
326 "$HOSTACKAUTHORNAME$"
339 "$HOSTEXECUTIONTIME$"
340 "$HOSTGROUPACTIONURL$"
346 "$HOSTGROUPNOTESURL$"
351 "$HOSTNOTIFICATIONID$"
352 "$HOSTNOTIFICATIONNUMBER$"
354 "$HOSTPERCENTCHANGE$"
365 "$LASTHOSTPROBLEMID$"
367 "$LASTHOSTSTATECHANGE$"
369 "$LASTHOSTUNREACHABLE$"
372 "$LASTSERVICECRITICAL$"
373 "$LASTSERVICEEVENTID$"
375 "$LASTSERVICEPROBLEMID$"
377 "$LASTSERVICESTATECHANGE$"
378 "$LASTSERVICESTATEID$"
379 "$LASTSERVICEUNKNOWN$"
380 "$LASTSERVICEWARNING$"
384 "$LONGSERVICEOUTPUT$"
387 "$MAXSERVICEATTEMPTS$"
389 "$NOTIFICATIONAUTHOR$"
390 "$NOTIFICATIONAUTHORALIAS$"
391 "$NOTIFICATIONAUTHORNAME$"
392 "$NOTIFICATIONCOMMENT$"
393 "$NOTIFICATIONISESCALATED$"
394 "$NOTIFICATIONNUMBER$"
395 "$NOTIFICATIONRECIPIENTS$"
400 "$RETENTIONDATAFILE$"
402 "$SERVICEACKAUTHORALIAS$"
403 "$SERVICEACKAUTHORNAME$"
404 "$SERVICEACKCOMMENT$"
407 "$SERVICECHECKCOMMAND$"
410 "$SERVICEDISPLAYNAME$"
413 "$SERVICEDURATIONSEC$"
415 "$SERVICEEXECUTIONTIME$"
416 "$SERVICEGROUPACTIONURL$"
417 "$SERVICEGROUPALIAS$"
418 "$SERVICEGROUPMEMBERS$"
420 "$SERVICEGROUPNAMES$"
421 "$SERVICEGROUPNOTES$"
422 "$SERVICEGROUPNOTESURL$"
423 "$SERVICEISVOLATILE$"
427 "$SERVICENOTIFICATIONID$"
428 "$SERVICENOTIFICATIONNUMBER$"
430 "$SERVICEPERCENTCHANGE$"
432 "$SERVICEPERFDATAFILE$"
443 "$TOTALHOSTPROBLEMS$"
444 "$TOTALHOSTPROBLEMSUNHANDLED$"
446 "$TOTALHOSTSDOWNUNHANDLED$"
447 "$TOTALHOSTSERVICES$"
448 "$TOTALHOSTSERVICESCRITICAL$"
449 "$TOTALHOSTSERVICESOK$"
450 "$TOTALHOSTSERVICESUNKNOWN$"
451 "$TOTALHOSTSERVICESWARNING$"
452 "$TOTALHOSTSUNREACHABLE$"
453 "$TOTALHOSTSUNREACHABLEUNHANDLED$"
455 "$TOTALSERVICEPROBLEMS$"
456 "$TOTALSERVICEPROBLEMSUNHANDLED$"
457 "$TOTALSERVICESCRITICAL$"
458 "$TOTALSERVICESCRITICALUNHANDLED$"
460 "$TOTALSERVICESUNKNOWN$"
461 "$TOTALSERVICESUNKNOWNUNHANDLED$"
462 "$TOTALSERVICESWARNING$"
463 "$TOTALSERVICESWARNINGUNHANDLED$"
724 (defconst nagios-definitions
727 (concat "^[ \t\r\n]*"
729 "\\(" ;; Stick parenthesis around whatever comes out
730 ;; of regexp-opt. We use this to match a
731 ;; subexpression during font-lock.
735 "define contactgroup"
737 "define hostdependency"
738 "define hostescalation"
741 "define hostgroupescalation"
744 "define servicedependency"
745 "define serviceescalation"
746 "define serviceextinfo"
747 "define servicegroup"
748 "define timeperiod"))
749 ;; This closes the parentheses that we opened
750 "\\)" ;; before regexp-opt.
752 ;; These can be "terminated" by either an opening curly
753 ;; brace, or a space.
760 (defconst nagios-special
762 (concat "^[ \t\r\n]*"
765 '("name" "register" "use") t)
772 ;; The One True Font Locking Variable
774 (defvar nagios-font-lock-keywords
776 (cons nagios-special font-lock-keyword-face)
777 (cons nagios-directives font-lock-variable-name-face)
778 (cons nagios-macros font-lock-constant-face)
779 (cons nagios-definitions '(1 font-lock-function-name-face)))
781 "Rules for highlighting Nagios configuration files."
786 (defvar nagios-mode-syntax-table nil
787 "Syntax table used in nagios-mode buffers.")
788 (if nagios-mode-syntax-table
790 (setq nagios-mode-syntax-table (make-syntax-table))
791 (modify-syntax-entry ?# "< b" nagios-mode-syntax-table) ;; Comment style 1
792 (modify-syntax-entry ?\; "< b" nagios-mode-syntax-table) ;; Comment style 2
793 (modify-syntax-entry ?\n "> b" nagios-mode-syntax-table) ;; End comment
797 ;; Main Mode Function
800 "Major mode for editing Nagios configuration files."
803 (kill-all-local-variables)
804 (make-local-variable 'font-lock-defaults)
805 (make-local-variable 'comment-start)
806 (make-local-variable 'comment-start-skip)
807 (make-local-variable 'comment-end)
808 (make-local-variable 'indent-line-function)
809 (make-local-variable 'syntax-begin-function)
811 (set-syntax-table nagios-mode-syntax-table)
813 (setq mode-name "nagios"
814 major-mode 'nagios-mode
815 indent-line-function 'nagios-indent-line
816 font-lock-defaults '(nagios-font-lock-keywords)
818 comment-start-skip "#\|; +"
821 ;; Since comments and strings do not span multiple lines,
822 ;; the syntax parser can safely start parsing at the beginning
824 syntax-begin-function 'beginning-of-line
828 (use-local-map nagios-mode-map)
830 ;; I don't /think/ I need to define this before attempting
831 ;; to run it. Users can define it if they want.
832 (run-hooks 'nagios-mode-hook)
836 (provide 'nagios-mode)