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?
71 (+ (pos-offset) (skip-chars-forward " \t"))
75 (defun first-char-pos()
76 ;; What's the position of the first character on this line?
77 (+ (beginning-of-line-pos) (first-char-offset))
80 (defun nagios-indent-to(indent-column)
81 "Indent the current line to column indent-column."
82 ;; Store the point in orig-pos.
83 (let ((orig-point (point)))
85 ;; And store the offset of the first character (with respect to the
86 ;; beginning of the line) in orig-first-char-offset.
87 (let ((orig-first-char-offset (first-char-offset)))
89 ;; Delete any leading whitespace, and move the point to the
90 ;; beginning of the line.
91 (delete-region (beginning-of-line-pos) (first-char-pos))
94 ;; Now insert indent-column spaces.
95 (while (< 0 indent-column)
97 (setq indent-column (- indent-column 1)))
99 ;; The text on the current line just moved left/right some amount;
100 ;; call it text-delta. We want to move the point that same distance.
101 (let ((text-delta (- (first-char-offset) orig-first-char-offset)))
102 (goto-char (+ orig-point text-delta))
110 (defun last-opening-brace()
111 ;; Get the position of the last opening brace, with
112 ;; respect to the current point.
114 (let ((lob (re-search-backward "{" nil t)))
122 (defun last-closing-brace()
123 ;; Get the position of the last closing brace, with
124 ;; respect to the current point.
126 (let ((lcb (re-search-backward "}" nil t)))
134 (defun nagios-in-block()
135 "Determine if the point is inside of a {} block."
137 ;; If the last brace seen in the buffer is an opening brace, we're
138 ;; in a block. Otherwise, we aren't.
139 (if (>= (last-closing-brace) (last-opening-brace))
145 (defun brace-on-line()
146 ;; Is there a curly brace on this line?
148 (re-search-forward "[{}]" (end-of-line-pos) t)
152 (defun nagios-calculate-indent()
153 "Calculate the level of indentation."
154 ;; We're either inside a block, or we aren't.
156 ;; Initialize the indent variable to either nagios-indent-level
157 ;; or 0 depending on whether or not we're in a block.
158 (let ((indent (if (nagios-in-block)
159 (setq indent nagios-indent-level)
164 ;; Set the indentation level to 0 if we find either brace on this
177 (defun nagios-insert-right-brace-and-indent()
178 "Insert a '}' character, and indent the line."
185 (defvar nagios-mode-map()
186 "Keymap used in nagios mode.")
188 (when (not nagios-mode-map)
189 (setq nagios-mode-map (make-sparse-keymap))
190 (define-key nagios-mode-map
192 'nagios-insert-right-brace-and-indent)
197 (defconst nagios-directives
199 (concat "^[ \t\r\n]*"
202 '("active_checks_enabled" "address" "alias" "check_command"
203 "check_freshness" "check_interval" "check_period" "checks_enabled"
204 "command_line" "command_name" "contactgroups" "contact_groups"
205 "contactgroup_members" "contact_name" "contactgroup_name" "contacts"
206 "dependent_host_name" "dependent_service_description" "email"
207 "event_handler" "event_handler_enabled" "execution_failure_criteria"
208 "failure_prediction_enabled" "first_notification"
209 "first_notification_delay" "flap_detection_enabled" "freshness_threshold"
210 "friday" "high_flap_threshold" "host_name" "host_notification_commands"
211 "host_notification_options" "host_notification_period"
212 "host_notifications_enabled" "hostgroup_name" "hostgroups"
213 "is_volatile" "last_notification" "low_flap_threshold"
214 "max_check_attempts" "members" "monday" "normal_check_interval"
215 "notes" "notification_failure_criteria"
216 "notification_interval" "notification_options"
217 "notification_period" "notifications_enabled"
218 "obsess_over_service" "pager" "parallelize_check"
219 "parents" "passive_checks_enabled"
220 "process_perf_data" "retain_nonstatus_information"
221 "retain_status_information" "retry_check_interval"
222 "retry_interval" "saturday" "service_description"
223 "service_notification_commands" "service_notification_options"
224 "service_notification_period" "service_notifications_enabled"
225 "servicegroup_name" "stalking_options"
226 "sunday" "thursday" "timeperiod_name" "tuesday" "wednesday") t)
234 (defconst nagios-macros
274 "$CONTACTGROUPALIAS$"
275 "$CONTACTGROUPMEMBERS$"
277 "$CONTACTGROUPNAMES$"
283 "$HOSTACKAUTHORALIAS$"
284 "$HOSTACKAUTHORNAME$"
297 "$HOSTEXECUTIONTIME$"
298 "$HOSTGROUPACTIONURL$"
304 "$HOSTGROUPNOTESURL$"
309 "$HOSTNOTIFICATIONID$"
310 "$HOSTNOTIFICATIONNUMBER$"
312 "$HOSTPERCENTCHANGE$"
323 "$LASTHOSTPROBLEMID$"
325 "$LASTHOSTSTATECHANGE$"
327 "$LASTHOSTUNREACHABLE$"
330 "$LASTSERVICECRITICAL$"
331 "$LASTSERVICEEVENTID$"
333 "$LASTSERVICEPROBLEMID$"
335 "$LASTSERVICESTATECHANGE$"
336 "$LASTSERVICESTATEID$"
337 "$LASTSERVICEUNKNOWN$"
338 "$LASTSERVICEWARNING$"
342 "$LONGSERVICEOUTPUT$"
345 "$MAXSERVICEATTEMPTS$"
347 "$NOTIFICATIONAUTHOR$"
348 "$NOTIFICATIONAUTHORALIAS$"
349 "$NOTIFICATIONAUTHORNAME$"
350 "$NOTIFICATIONCOMMENT$"
351 "$NOTIFICATIONISESCALATED$"
352 "$NOTIFICATIONNUMBER$"
353 "$NOTIFICATIONRECIPIENTS$"
358 "$RETENTIONDATAFILE$"
360 "$SERVICEACKAUTHORALIAS$"
361 "$SERVICEACKAUTHORNAME$"
362 "$SERVICEACKCOMMENT$"
365 "$SERVICECHECKCOMMAND$"
368 "$SERVICEDISPLAYNAME$"
371 "$SERVICEDURATIONSEC$"
373 "$SERVICEEXECUTIONTIME$"
374 "$SERVICEGROUPACTIONURL$"
375 "$SERVICEGROUPALIAS$"
376 "$SERVICEGROUPMEMBERS$"
378 "$SERVICEGROUPNAMES$"
379 "$SERVICEGROUPNOTES$"
380 "$SERVICEGROUPNOTESURL$"
381 "$SERVICEISVOLATILE$"
385 "$SERVICENOTIFICATIONID$"
386 "$SERVICENOTIFICATIONNUMBER$"
388 "$SERVICEPERCENTCHANGE$"
390 "$SERVICEPERFDATAFILE$"
401 "$TOTALHOSTPROBLEMS$"
402 "$TOTALHOSTPROBLEMSUNHANDLED$"
404 "$TOTALHOSTSDOWNUNHANDLED$"
405 "$TOTALHOSTSERVICES$"
406 "$TOTALHOSTSERVICESCRITICAL$"
407 "$TOTALHOSTSERVICESOK$"
408 "$TOTALHOSTSERVICESUNKNOWN$"
409 "$TOTALHOSTSERVICESWARNING$"
410 "$TOTALHOSTSUNREACHABLE$"
411 "$TOTALHOSTSUNREACHABLEUNHANDLED$"
413 "$TOTALSERVICEPROBLEMS$"
414 "$TOTALSERVICEPROBLEMSUNHANDLED$"
415 "$TOTALSERVICESCRITICAL$"
416 "$TOTALSERVICESCRITICALUNHANDLED$"
418 "$TOTALSERVICESUNKNOWN$"
419 "$TOTALSERVICESUNKNOWNUNHANDLED$"
420 "$TOTALSERVICESWARNING$"
421 "$TOTALSERVICESWARNINGUNHANDLED$"
682 (defconst nagios-definitions
685 (concat "^[ \t\r\n]*"
687 "\\(" ;; Stick parenthesis around whatever comes out
688 ;; of regexp-opt. We use this to match a
689 ;; subexpression during font-lock.
693 "define contactgroup"
695 "define hostdependency"
696 "define hostescalation"
699 "define hostgroupescalation"
702 "define servicedependency"
703 "define serviceescalation"
704 "define serviceextinfo"
705 "define servicegroup"
706 "define timeperiod"))
707 ;; This closes the parentheses that we opened
708 "\\)" ;; before regexp-opt.
710 ;; These can be "terminated" by either an opening curly
711 ;; brace, or a space.
718 (defconst nagios-special
720 (concat "^[ \t\r\n]*"
723 '("name" "register" "use") t)
730 ;; The One True Font Locking Variable
732 (defvar nagios-font-lock-keywords
734 (cons nagios-special font-lock-keyword-face)
735 (cons nagios-directives font-lock-variable-name-face)
736 (cons nagios-macros font-lock-constant-face)
737 (cons nagios-definitions '(1 font-lock-function-name-face)))
739 "Rules for highlighting Nagios configuration files."
744 (defvar nagios-mode-syntax-table nil
745 "Syntax table used in nagios-mode buffers.")
746 (if nagios-mode-syntax-table
748 (setq nagios-mode-syntax-table (make-syntax-table))
749 (modify-syntax-entry ?# "< b" nagios-mode-syntax-table) ;; Comment style 1
750 (modify-syntax-entry ?\; "< b" nagios-mode-syntax-table) ;; Comment style 2
751 (modify-syntax-entry ?\n "> b" nagios-mode-syntax-table) ;; End comment
755 ;; Main Mode Function
758 "Major mode for editing Nagios configuration files."
761 (kill-all-local-variables)
762 (make-local-variable 'font-lock-defaults)
763 (make-local-variable 'comment-start)
764 (make-local-variable 'comment-start-skip)
765 (make-local-variable 'comment-end)
766 (make-local-variable 'indent-line-function)
767 (make-local-variable 'syntax-begin-function)
769 (set-syntax-table nagios-mode-syntax-table)
771 (setq mode-name "nagios"
772 major-mode 'nagios-mode
773 indent-line-function 'nagios-indent-line
774 font-lock-defaults '(nagios-font-lock-keywords)
776 comment-start-skip "#\|; +"
779 ;; Since comments and strings do not span multiple lines,
780 ;; the syntax parser can safely start parsing at the beginning
782 syntax-begin-function 'beginning-of-line
786 (use-local-map nagios-mode-map)
788 ;; I don't /think/ I need to define this before attempting
789 ;; to run it. Users can define it if they want.
790 (run-hooks 'nagios-mode-hook)
794 (provide 'nagios-mode)